Steven Rostedt (VMware) | bcea3f9 | 2018-08-16 11:23:53 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2 | /* |
| 3 | * ring buffer based function tracer |
| 4 | * |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5 | * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6 | * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com> |
| 7 | * |
| 8 | * Originally taken from the RT patch by: |
| 9 | * Arnaldo Carvalho de Melo <acme@redhat.com> |
| 10 | * |
| 11 | * Based on code from the latency_tracer, that is: |
| 12 | * Copyright (C) 2004-2006 Ingo Molnar |
Nadia Yvette Chambers | 6d49e35 | 2012-12-06 10:39:54 +0100 | [diff] [blame] | 13 | * Copyright (C) 2004 Nadia Yvette Chambers |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 14 | */ |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 15 | #include <linux/ring_buffer.h> |
Sam Ravnborg | 273b281 | 2009-10-18 00:52:28 +0200 | [diff] [blame] | 16 | #include <generated/utsrelease.h> |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 17 | #include <linux/stacktrace.h> |
| 18 | #include <linux/writeback.h> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 19 | #include <linux/kallsyms.h> |
Steven Rostedt (VMware) | 17911ff | 2019-10-11 17:22:50 -0400 | [diff] [blame] | 20 | #include <linux/security.h> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 21 | #include <linux/seq_file.h> |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 22 | #include <linux/notifier.h> |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 23 | #include <linux/irqflags.h> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 24 | #include <linux/debugfs.h> |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 25 | #include <linux/tracefs.h> |
Steven Rostedt | 4c11d7a | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 26 | #include <linux/pagemap.h> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 27 | #include <linux/hardirq.h> |
| 28 | #include <linux/linkage.h> |
| 29 | #include <linux/uaccess.h> |
Steven Rostedt (Red Hat) | 76c813e | 2016-04-21 11:35:30 -0400 | [diff] [blame] | 30 | #include <linux/vmalloc.h> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 31 | #include <linux/ftrace.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/percpu.h> |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 34 | #include <linux/splice.h> |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 35 | #include <linux/kdebug.h> |
Frederic Weisbecker | 5f0c6c0 | 2009-03-27 14:22:10 +0100 | [diff] [blame] | 36 | #include <linux/string.h> |
Steven Rostedt (Red Hat) | f76180b | 2015-01-20 15:48:46 -0500 | [diff] [blame] | 37 | #include <linux/mount.h> |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 38 | #include <linux/rwsem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 40 | #include <linux/ctype.h> |
| 41 | #include <linux/init.h> |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 42 | #include <linux/poll.h> |
Steven Rostedt | b892e5c | 2012-03-01 22:06:48 -0500 | [diff] [blame] | 43 | #include <linux/nmi.h> |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 44 | #include <linux/fs.h> |
Chunyan Zhang | 478409d | 2016-11-21 15:57:18 +0800 | [diff] [blame] | 45 | #include <linux/trace.h> |
Chris Wilson | 3fd49c9 | 2018-03-30 16:01:31 +0100 | [diff] [blame] | 46 | #include <linux/sched/clock.h> |
Clark Williams | 8bd75c7 | 2013-02-07 09:47:07 -0600 | [diff] [blame] | 47 | #include <linux/sched/rt.h> |
Viktor Rosendahl (BMW) | 91edde2 | 2019-10-09 00:08:21 +0200 | [diff] [blame] | 48 | #include <linux/fsnotify.h> |
| 49 | #include <linux/irq_work.h> |
| 50 | #include <linux/workqueue.h> |
Ingo Molnar | 86387f7 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 51 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 52 | #include "trace.h" |
Steven Rostedt | f0868d1 | 2008-12-23 23:24:12 -0500 | [diff] [blame] | 53 | #include "trace_output.h" |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 54 | |
Frederic Weisbecker | 8e1b82e | 2008-12-06 03:41:33 +0100 | [diff] [blame] | 55 | /* |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 56 | * On boot up, the ring buffer is set to the minimum size, so that |
| 57 | * we do not waste memory on systems that are not using tracing. |
| 58 | */ |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 59 | bool ring_buffer_expanded; |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 60 | |
| 61 | /* |
Frederic Weisbecker | 8e1b82e | 2008-12-06 03:41:33 +0100 | [diff] [blame] | 62 | * We need to change this state when a selftest is running. |
Frederic Weisbecker | ff32504 | 2008-12-04 23:47:35 +0100 | [diff] [blame] | 63 | * A selftest will lurk into the ring-buffer to count the |
| 64 | * entries inserted during the selftest although some concurrent |
Ingo Molnar | 5e1607a | 2009-03-05 10:24:48 +0100 | [diff] [blame] | 65 | * insertions into the ring-buffer such as trace_printk could occurred |
Frederic Weisbecker | ff32504 | 2008-12-04 23:47:35 +0100 | [diff] [blame] | 66 | * at the same time, giving false positive or negative results. |
| 67 | */ |
Frederic Weisbecker | 8e1b82e | 2008-12-06 03:41:33 +0100 | [diff] [blame] | 68 | static bool __read_mostly tracing_selftest_running; |
Frederic Weisbecker | ff32504 | 2008-12-04 23:47:35 +0100 | [diff] [blame] | 69 | |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 70 | /* |
| 71 | * If a tracer is running, we do not want to run SELFTEST. |
| 72 | */ |
Li Zefan | 020e5f8 | 2009-07-01 10:47:05 +0800 | [diff] [blame] | 73 | bool __read_mostly tracing_selftest_disabled; |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 74 | |
Steven Rostedt (Red Hat) | 0daa2302 | 2014-12-12 22:27:10 -0500 | [diff] [blame] | 75 | /* Pipe tracepoints to printk */ |
| 76 | struct trace_iterator *tracepoint_print_iter; |
| 77 | int tracepoint_printk; |
Steven Rostedt (Red Hat) | 42391745 | 2016-11-23 15:52:45 -0500 | [diff] [blame] | 78 | static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key); |
Steven Rostedt (Red Hat) | 0daa2302 | 2014-12-12 22:27:10 -0500 | [diff] [blame] | 79 | |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 80 | /* For tracers that don't implement custom flags */ |
| 81 | static struct tracer_opt dummy_tracer_opt[] = { |
| 82 | { } |
| 83 | }; |
| 84 | |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 85 | static int |
| 86 | dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set) |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 87 | { |
| 88 | return 0; |
| 89 | } |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 90 | |
| 91 | /* |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 92 | * To prevent the comm cache from being overwritten when no |
| 93 | * tracing is active, only save the comm when a trace event |
| 94 | * occurred. |
| 95 | */ |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 96 | static DEFINE_PER_CPU(bool, trace_taskinfo_save); |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 97 | |
| 98 | /* |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 99 | * Kill all tracing for good (never come back). |
| 100 | * It is initialized to 1 but will turn to zero if the initialization |
| 101 | * of the tracer is successful. But that is the only place that sets |
| 102 | * this back to zero. |
| 103 | */ |
Hannes Eder | 4fd2735 | 2009-02-10 19:44:12 +0100 | [diff] [blame] | 104 | static int tracing_disabled = 1; |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 105 | |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 106 | cpumask_var_t __read_mostly tracing_buffer_mask; |
Steven Rostedt | ab46428 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 107 | |
Steven Rostedt | 944ac42 | 2008-10-23 19:26:08 -0400 | [diff] [blame] | 108 | /* |
| 109 | * ftrace_dump_on_oops - variable to dump ftrace buffer on oops |
| 110 | * |
| 111 | * If there is an oops (or kernel panic) and the ftrace_dump_on_oops |
| 112 | * is set, then ftrace_dump is called. This will output the contents |
| 113 | * of the ftrace buffers to the console. This is very useful for |
| 114 | * capturing traces that lead to crashes and outputing it to a |
| 115 | * serial console. |
| 116 | * |
| 117 | * It is default off, but you can enable it with either specifying |
| 118 | * "ftrace_dump_on_oops" in the kernel command line, or setting |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 119 | * /proc/sys/kernel/ftrace_dump_on_oops |
| 120 | * Set 1 if you want to dump buffers of all CPUs |
| 121 | * Set 2 if you want to dump the buffer of the CPU that triggered oops |
Steven Rostedt | 944ac42 | 2008-10-23 19:26:08 -0400 | [diff] [blame] | 122 | */ |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 123 | |
| 124 | enum ftrace_dump_mode ftrace_dump_on_oops; |
Steven Rostedt | 944ac42 | 2008-10-23 19:26:08 -0400 | [diff] [blame] | 125 | |
Steven Rostedt (Red Hat) | de7edd3 | 2013-06-14 16:21:43 -0400 | [diff] [blame] | 126 | /* When set, tracing will stop when a WARN*() is hit */ |
| 127 | int __disable_trace_on_warning; |
| 128 | |
Jeremy Linton | 681bec0 | 2017-05-31 16:56:53 -0500 | [diff] [blame] | 129 | #ifdef CONFIG_TRACE_EVAL_MAP_FILE |
| 130 | /* Map of enums to their values, for "eval_map" file */ |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 131 | struct trace_eval_map_head { |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 132 | struct module *mod; |
| 133 | unsigned long length; |
| 134 | }; |
| 135 | |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 136 | union trace_eval_map_item; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 137 | |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 138 | struct trace_eval_map_tail { |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 139 | /* |
| 140 | * "end" is first and points to NULL as it must be different |
Jeremy Linton | 00f4b65 | 2017-05-31 16:56:43 -0500 | [diff] [blame] | 141 | * than "mod" or "eval_string" |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 142 | */ |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 143 | union trace_eval_map_item *next; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 144 | const char *end; /* points to NULL */ |
| 145 | }; |
| 146 | |
Jeremy Linton | 1793ed9 | 2017-05-31 16:56:46 -0500 | [diff] [blame] | 147 | static DEFINE_MUTEX(trace_eval_mutex); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 148 | |
| 149 | /* |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 150 | * The trace_eval_maps are saved in an array with two extra elements, |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 151 | * one at the beginning, and one at the end. The beginning item contains |
| 152 | * the count of the saved maps (head.length), and the module they |
| 153 | * belong to if not built in (head.mod). The ending item contains a |
Jeremy Linton | 681bec0 | 2017-05-31 16:56:53 -0500 | [diff] [blame] | 154 | * pointer to the next array of saved eval_map items. |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 155 | */ |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 156 | union trace_eval_map_item { |
Jeremy Linton | 00f4b65 | 2017-05-31 16:56:43 -0500 | [diff] [blame] | 157 | struct trace_eval_map map; |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 158 | struct trace_eval_map_head head; |
| 159 | struct trace_eval_map_tail tail; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 160 | }; |
| 161 | |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 162 | static union trace_eval_map_item *trace_eval_maps; |
Jeremy Linton | 681bec0 | 2017-05-31 16:56:53 -0500 | [diff] [blame] | 163 | #endif /* CONFIG_TRACE_EVAL_MAP_FILE */ |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 164 | |
Masami Hiramatsu | 9c5b9d3 | 2020-01-11 01:06:17 +0900 | [diff] [blame] | 165 | int tracing_set_tracer(struct trace_array *tr, const char *buf); |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 166 | static void ftrace_trace_userstack(struct trace_buffer *buffer, |
Thomas Gleixner | c438f14 | 2019-04-25 11:45:15 +0200 | [diff] [blame] | 167 | unsigned long flags, int pc); |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 168 | |
Li Zefan | ee6c2c1 | 2009-09-18 14:06:47 +0800 | [diff] [blame] | 169 | #define MAX_TRACER_SIZE 100 |
| 170 | static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata; |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 171 | static char *default_bootup_tracer; |
Peter Zijlstra | d9e5407 | 2008-11-01 19:57:37 +0100 | [diff] [blame] | 172 | |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 173 | static bool allocate_snapshot; |
| 174 | |
Frederic Weisbecker | 1beee96 | 2009-10-14 20:50:32 +0200 | [diff] [blame] | 175 | static int __init set_cmdline_ftrace(char *str) |
Peter Zijlstra | d9e5407 | 2008-11-01 19:57:37 +0100 | [diff] [blame] | 176 | { |
Chen Gang | 67012ab | 2013-04-08 12:06:44 +0800 | [diff] [blame] | 177 | strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE); |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 178 | default_bootup_tracer = bootup_tracer_buf; |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 179 | /* We are using ftrace early, expand it */ |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 180 | ring_buffer_expanded = true; |
Peter Zijlstra | d9e5407 | 2008-11-01 19:57:37 +0100 | [diff] [blame] | 181 | return 1; |
| 182 | } |
Frederic Weisbecker | 1beee96 | 2009-10-14 20:50:32 +0200 | [diff] [blame] | 183 | __setup("ftrace=", set_cmdline_ftrace); |
Peter Zijlstra | d9e5407 | 2008-11-01 19:57:37 +0100 | [diff] [blame] | 184 | |
Steven Rostedt | 944ac42 | 2008-10-23 19:26:08 -0400 | [diff] [blame] | 185 | static int __init set_ftrace_dump_on_oops(char *str) |
| 186 | { |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 187 | if (*str++ != '=' || !*str) { |
| 188 | ftrace_dump_on_oops = DUMP_ALL; |
| 189 | return 1; |
| 190 | } |
| 191 | |
| 192 | if (!strcmp("orig_cpu", str)) { |
| 193 | ftrace_dump_on_oops = DUMP_ORIG; |
| 194 | return 1; |
| 195 | } |
| 196 | |
| 197 | return 0; |
Steven Rostedt | 944ac42 | 2008-10-23 19:26:08 -0400 | [diff] [blame] | 198 | } |
| 199 | __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops); |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 200 | |
Steven Rostedt (Red Hat) | de7edd3 | 2013-06-14 16:21:43 -0400 | [diff] [blame] | 201 | static int __init stop_trace_on_warning(char *str) |
| 202 | { |
Luis Claudio R. Goncalves | 933ff9f | 2014-11-12 21:14:00 -0200 | [diff] [blame] | 203 | if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0)) |
| 204 | __disable_trace_on_warning = 1; |
Steven Rostedt (Red Hat) | de7edd3 | 2013-06-14 16:21:43 -0400 | [diff] [blame] | 205 | return 1; |
| 206 | } |
Luis Claudio R. Goncalves | 933ff9f | 2014-11-12 21:14:00 -0200 | [diff] [blame] | 207 | __setup("traceoff_on_warning", stop_trace_on_warning); |
Steven Rostedt (Red Hat) | de7edd3 | 2013-06-14 16:21:43 -0400 | [diff] [blame] | 208 | |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 209 | static int __init boot_alloc_snapshot(char *str) |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 210 | { |
| 211 | allocate_snapshot = true; |
| 212 | /* We also need the main ring buffer expanded */ |
| 213 | ring_buffer_expanded = true; |
| 214 | return 1; |
| 215 | } |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 216 | __setup("alloc_snapshot", boot_alloc_snapshot); |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 217 | |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 218 | |
| 219 | static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata; |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 220 | |
| 221 | static int __init set_trace_boot_options(char *str) |
| 222 | { |
Chen Gang | 67012ab | 2013-04-08 12:06:44 +0800 | [diff] [blame] | 223 | strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE); |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | __setup("trace_options=", set_trace_boot_options); |
| 227 | |
Steven Rostedt | e1e232c | 2014-02-10 23:38:46 -0500 | [diff] [blame] | 228 | static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata; |
| 229 | static char *trace_boot_clock __initdata; |
| 230 | |
| 231 | static int __init set_trace_boot_clock(char *str) |
| 232 | { |
| 233 | strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE); |
| 234 | trace_boot_clock = trace_boot_clock_buf; |
| 235 | return 0; |
| 236 | } |
| 237 | __setup("trace_clock=", set_trace_boot_clock); |
| 238 | |
Steven Rostedt (Red Hat) | 0daa2302 | 2014-12-12 22:27:10 -0500 | [diff] [blame] | 239 | static int __init set_tracepoint_printk(char *str) |
| 240 | { |
| 241 | if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0)) |
| 242 | tracepoint_printk = 1; |
| 243 | return 1; |
| 244 | } |
| 245 | __setup("tp_printk", set_tracepoint_printk); |
Steven Rostedt (Red Hat) | de7edd3 | 2013-06-14 16:21:43 -0400 | [diff] [blame] | 246 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 247 | unsigned long long ns2usecs(u64 nsec) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 248 | { |
| 249 | nsec += 500; |
| 250 | do_div(nsec, 1000); |
| 251 | return nsec; |
| 252 | } |
| 253 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 254 | /* trace_flags holds trace_options default values */ |
| 255 | #define TRACE_DEFAULT_FLAGS \ |
| 256 | (FUNCTION_DEFAULT_FLAGS | \ |
| 257 | TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | \ |
| 258 | TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \ |
| 259 | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \ |
| 260 | TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS) |
| 261 | |
Steven Rostedt (Red Hat) | 1627014 | 2015-09-30 12:30:06 -0400 | [diff] [blame] | 262 | /* trace_options that are only supported by global_trace */ |
| 263 | #define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \ |
| 264 | TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD) |
| 265 | |
Steven Rostedt (Red Hat) | 2055062 | 2016-04-25 22:40:12 -0400 | [diff] [blame] | 266 | /* trace_flags that are default zero for instances */ |
| 267 | #define ZEROED_TRACE_FLAGS \ |
Namhyung Kim | 1e10486 | 2017-04-17 11:44:28 +0900 | [diff] [blame] | 268 | (TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK) |
Steven Rostedt (Red Hat) | 1627014 | 2015-09-30 12:30:06 -0400 | [diff] [blame] | 269 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 270 | /* |
Joel Fernandes | 67d04bb | 2017-02-16 20:10:58 -0800 | [diff] [blame] | 271 | * The global_trace is the descriptor that holds the top-level tracing |
| 272 | * buffers for the live tracing. |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 273 | */ |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 274 | static struct trace_array global_trace = { |
| 275 | .trace_flags = TRACE_DEFAULT_FLAGS, |
| 276 | }; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 277 | |
Steven Rostedt | ae63b31e | 2012-05-03 23:09:03 -0400 | [diff] [blame] | 278 | LIST_HEAD(ftrace_trace_arrays); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 279 | |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 280 | int trace_array_get(struct trace_array *this_tr) |
| 281 | { |
| 282 | struct trace_array *tr; |
| 283 | int ret = -ENODEV; |
| 284 | |
| 285 | mutex_lock(&trace_types_lock); |
| 286 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 287 | if (tr == this_tr) { |
| 288 | tr->ref++; |
| 289 | ret = 0; |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | mutex_unlock(&trace_types_lock); |
| 294 | |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | static void __trace_array_put(struct trace_array *this_tr) |
| 299 | { |
| 300 | WARN_ON(!this_tr->ref); |
| 301 | this_tr->ref--; |
| 302 | } |
| 303 | |
Divya Indi | 2887978 | 2019-11-20 11:08:38 -0800 | [diff] [blame] | 304 | /** |
| 305 | * trace_array_put - Decrement the reference counter for this trace array. |
| 306 | * |
| 307 | * NOTE: Use this when we no longer need the trace array returned by |
| 308 | * trace_array_get_by_name(). This ensures the trace array can be later |
| 309 | * destroyed. |
| 310 | * |
| 311 | */ |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 312 | void trace_array_put(struct trace_array *this_tr) |
| 313 | { |
Divya Indi | 2887978 | 2019-11-20 11:08:38 -0800 | [diff] [blame] | 314 | if (!this_tr) |
| 315 | return; |
| 316 | |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 317 | mutex_lock(&trace_types_lock); |
| 318 | __trace_array_put(this_tr); |
| 319 | mutex_unlock(&trace_types_lock); |
| 320 | } |
Divya Indi | 2887978 | 2019-11-20 11:08:38 -0800 | [diff] [blame] | 321 | EXPORT_SYMBOL_GPL(trace_array_put); |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 322 | |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 323 | int tracing_check_open_get_tr(struct trace_array *tr) |
| 324 | { |
Steven Rostedt (VMware) | 17911ff | 2019-10-11 17:22:50 -0400 | [diff] [blame] | 325 | int ret; |
| 326 | |
| 327 | ret = security_locked_down(LOCKDOWN_TRACEFS); |
| 328 | if (ret) |
| 329 | return ret; |
| 330 | |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 331 | if (tracing_disabled) |
| 332 | return -ENODEV; |
| 333 | |
| 334 | if (tr && trace_array_get(tr) < 0) |
| 335 | return -ENODEV; |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 340 | int call_filter_check_discard(struct trace_event_call *call, void *rec, |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 341 | struct trace_buffer *buffer, |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 342 | struct ring_buffer_event *event) |
| 343 | { |
| 344 | if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) && |
| 345 | !filter_match_preds(call->filter, rec)) { |
Steven Rostedt (Red Hat) | 0fc1b09 | 2016-05-03 17:15:43 -0400 | [diff] [blame] | 346 | __trace_event_discard_commit(buffer, event); |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 347 | return 1; |
| 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
Tom Zanussi | eb02ce0 | 2009-04-08 03:15:54 -0500 | [diff] [blame] | 352 | |
Steven Rostedt (Red Hat) | 76c813e | 2016-04-21 11:35:30 -0400 | [diff] [blame] | 353 | void trace_free_pid_list(struct trace_pid_list *pid_list) |
| 354 | { |
| 355 | vfree(pid_list->pids); |
| 356 | kfree(pid_list); |
| 357 | } |
| 358 | |
Steven Rostedt | d8275c4 | 2016-04-14 12:15:22 -0400 | [diff] [blame] | 359 | /** |
| 360 | * trace_find_filtered_pid - check if a pid exists in a filtered_pid list |
| 361 | * @filtered_pids: The list of pids to check |
| 362 | * @search_pid: The PID to find in @filtered_pids |
| 363 | * |
| 364 | * Returns true if @search_pid is fonud in @filtered_pids, and false otherwis. |
| 365 | */ |
| 366 | bool |
| 367 | trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid) |
| 368 | { |
| 369 | /* |
| 370 | * If pid_max changed after filtered_pids was created, we |
| 371 | * by default ignore all pids greater than the previous pid_max. |
| 372 | */ |
| 373 | if (search_pid >= filtered_pids->pid_max) |
| 374 | return false; |
| 375 | |
| 376 | return test_bit(search_pid, filtered_pids->pids); |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * trace_ignore_this_task - should a task be ignored for tracing |
| 381 | * @filtered_pids: The list of pids to check |
| 382 | * @task: The task that should be ignored if not filtered |
| 383 | * |
| 384 | * Checks if @task should be traced or not from @filtered_pids. |
| 385 | * Returns true if @task should *NOT* be traced. |
| 386 | * Returns false if @task should be traced. |
| 387 | */ |
| 388 | bool |
| 389 | trace_ignore_this_task(struct trace_pid_list *filtered_pids, struct task_struct *task) |
| 390 | { |
| 391 | /* |
| 392 | * Return false, because if filtered_pids does not exist, |
| 393 | * all pids are good to trace. |
| 394 | */ |
| 395 | if (!filtered_pids) |
| 396 | return false; |
| 397 | |
| 398 | return !trace_find_filtered_pid(filtered_pids, task->pid); |
| 399 | } |
| 400 | |
| 401 | /** |
Matthias Kaehlcke | f08367b | 2019-05-23 12:26:28 -0700 | [diff] [blame] | 402 | * trace_filter_add_remove_task - Add or remove a task from a pid_list |
Steven Rostedt | d8275c4 | 2016-04-14 12:15:22 -0400 | [diff] [blame] | 403 | * @pid_list: The list to modify |
| 404 | * @self: The current task for fork or NULL for exit |
| 405 | * @task: The task to add or remove |
| 406 | * |
| 407 | * If adding a task, if @self is defined, the task is only added if @self |
| 408 | * is also included in @pid_list. This happens on fork and tasks should |
| 409 | * only be added when the parent is listed. If @self is NULL, then the |
| 410 | * @task pid will be removed from the list, which would happen on exit |
| 411 | * of a task. |
| 412 | */ |
| 413 | void trace_filter_add_remove_task(struct trace_pid_list *pid_list, |
| 414 | struct task_struct *self, |
| 415 | struct task_struct *task) |
| 416 | { |
| 417 | if (!pid_list) |
| 418 | return; |
| 419 | |
| 420 | /* For forks, we only add if the forking task is listed */ |
| 421 | if (self) { |
| 422 | if (!trace_find_filtered_pid(pid_list, self->pid)) |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | /* Sorry, but we don't support pid_max changing after setting */ |
| 427 | if (task->pid >= pid_list->pid_max) |
| 428 | return; |
| 429 | |
| 430 | /* "self" is set for forks, and NULL for exits */ |
| 431 | if (self) |
| 432 | set_bit(task->pid, pid_list->pids); |
| 433 | else |
| 434 | clear_bit(task->pid, pid_list->pids); |
| 435 | } |
| 436 | |
Steven Rostedt (Red Hat) | 5cc8976 | 2016-04-20 15:19:54 -0400 | [diff] [blame] | 437 | /** |
| 438 | * trace_pid_next - Used for seq_file to get to the next pid of a pid_list |
| 439 | * @pid_list: The pid list to show |
| 440 | * @v: The last pid that was shown (+1 the actual pid to let zero be displayed) |
| 441 | * @pos: The position of the file |
| 442 | * |
| 443 | * This is used by the seq_file "next" operation to iterate the pids |
| 444 | * listed in a trace_pid_list structure. |
| 445 | * |
| 446 | * Returns the pid+1 as we want to display pid of zero, but NULL would |
| 447 | * stop the iteration. |
| 448 | */ |
| 449 | void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos) |
| 450 | { |
| 451 | unsigned long pid = (unsigned long)v; |
| 452 | |
| 453 | (*pos)++; |
| 454 | |
| 455 | /* pid already is +1 of the actual prevous bit */ |
| 456 | pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid); |
| 457 | |
| 458 | /* Return pid + 1 to allow zero to be represented */ |
| 459 | if (pid < pid_list->pid_max) |
| 460 | return (void *)(pid + 1); |
| 461 | |
| 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * trace_pid_start - Used for seq_file to start reading pid lists |
| 467 | * @pid_list: The pid list to show |
| 468 | * @pos: The position of the file |
| 469 | * |
| 470 | * This is used by seq_file "start" operation to start the iteration |
| 471 | * of listing pids. |
| 472 | * |
| 473 | * Returns the pid+1 as we want to display pid of zero, but NULL would |
| 474 | * stop the iteration. |
| 475 | */ |
| 476 | void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos) |
| 477 | { |
| 478 | unsigned long pid; |
| 479 | loff_t l = 0; |
| 480 | |
| 481 | pid = find_first_bit(pid_list->pids, pid_list->pid_max); |
| 482 | if (pid >= pid_list->pid_max) |
| 483 | return NULL; |
| 484 | |
| 485 | /* Return pid + 1 so that zero can be the exit value */ |
| 486 | for (pid++; pid && l < *pos; |
| 487 | pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l)) |
| 488 | ; |
| 489 | return (void *)pid; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * trace_pid_show - show the current pid in seq_file processing |
| 494 | * @m: The seq_file structure to write into |
| 495 | * @v: A void pointer of the pid (+1) value to display |
| 496 | * |
| 497 | * Can be directly used by seq_file operations to display the current |
| 498 | * pid value. |
| 499 | */ |
| 500 | int trace_pid_show(struct seq_file *m, void *v) |
| 501 | { |
| 502 | unsigned long pid = (unsigned long)v - 1; |
| 503 | |
| 504 | seq_printf(m, "%lu\n", pid); |
| 505 | return 0; |
| 506 | } |
| 507 | |
Steven Rostedt (Red Hat) | 76c813e | 2016-04-21 11:35:30 -0400 | [diff] [blame] | 508 | /* 128 should be much more than enough */ |
| 509 | #define PID_BUF_SIZE 127 |
| 510 | |
| 511 | int trace_pid_write(struct trace_pid_list *filtered_pids, |
| 512 | struct trace_pid_list **new_pid_list, |
| 513 | const char __user *ubuf, size_t cnt) |
| 514 | { |
| 515 | struct trace_pid_list *pid_list; |
| 516 | struct trace_parser parser; |
| 517 | unsigned long val; |
| 518 | int nr_pids = 0; |
| 519 | ssize_t read = 0; |
| 520 | ssize_t ret = 0; |
| 521 | loff_t pos; |
| 522 | pid_t pid; |
| 523 | |
| 524 | if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1)) |
| 525 | return -ENOMEM; |
| 526 | |
| 527 | /* |
| 528 | * Always recreate a new array. The write is an all or nothing |
| 529 | * operation. Always create a new array when adding new pids by |
| 530 | * the user. If the operation fails, then the current list is |
| 531 | * not modified. |
| 532 | */ |
| 533 | pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL); |
Wenwen Wang | 91862cc | 2019-04-19 21:22:59 -0500 | [diff] [blame] | 534 | if (!pid_list) { |
| 535 | trace_parser_put(&parser); |
Steven Rostedt (Red Hat) | 76c813e | 2016-04-21 11:35:30 -0400 | [diff] [blame] | 536 | return -ENOMEM; |
Wenwen Wang | 91862cc | 2019-04-19 21:22:59 -0500 | [diff] [blame] | 537 | } |
Steven Rostedt (Red Hat) | 76c813e | 2016-04-21 11:35:30 -0400 | [diff] [blame] | 538 | |
| 539 | pid_list->pid_max = READ_ONCE(pid_max); |
| 540 | |
| 541 | /* Only truncating will shrink pid_max */ |
| 542 | if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max) |
| 543 | pid_list->pid_max = filtered_pids->pid_max; |
| 544 | |
| 545 | pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3); |
| 546 | if (!pid_list->pids) { |
Wenwen Wang | 91862cc | 2019-04-19 21:22:59 -0500 | [diff] [blame] | 547 | trace_parser_put(&parser); |
Steven Rostedt (Red Hat) | 76c813e | 2016-04-21 11:35:30 -0400 | [diff] [blame] | 548 | kfree(pid_list); |
| 549 | return -ENOMEM; |
| 550 | } |
| 551 | |
| 552 | if (filtered_pids) { |
| 553 | /* copy the current bits to the new max */ |
Wei Yongjun | 67f20b0 | 2016-07-04 15:10:04 +0000 | [diff] [blame] | 554 | for_each_set_bit(pid, filtered_pids->pids, |
| 555 | filtered_pids->pid_max) { |
Steven Rostedt (Red Hat) | 76c813e | 2016-04-21 11:35:30 -0400 | [diff] [blame] | 556 | set_bit(pid, pid_list->pids); |
Steven Rostedt (Red Hat) | 76c813e | 2016-04-21 11:35:30 -0400 | [diff] [blame] | 557 | nr_pids++; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | while (cnt > 0) { |
| 562 | |
| 563 | pos = 0; |
| 564 | |
| 565 | ret = trace_get_user(&parser, ubuf, cnt, &pos); |
| 566 | if (ret < 0 || !trace_parser_loaded(&parser)) |
| 567 | break; |
| 568 | |
| 569 | read += ret; |
| 570 | ubuf += ret; |
| 571 | cnt -= ret; |
| 572 | |
Steven Rostedt (Red Hat) | 76c813e | 2016-04-21 11:35:30 -0400 | [diff] [blame] | 573 | ret = -EINVAL; |
| 574 | if (kstrtoul(parser.buffer, 0, &val)) |
| 575 | break; |
| 576 | if (val >= pid_list->pid_max) |
| 577 | break; |
| 578 | |
| 579 | pid = (pid_t)val; |
| 580 | |
| 581 | set_bit(pid, pid_list->pids); |
| 582 | nr_pids++; |
| 583 | |
| 584 | trace_parser_clear(&parser); |
| 585 | ret = 0; |
| 586 | } |
| 587 | trace_parser_put(&parser); |
| 588 | |
| 589 | if (ret < 0) { |
| 590 | trace_free_pid_list(pid_list); |
| 591 | return ret; |
| 592 | } |
| 593 | |
| 594 | if (!nr_pids) { |
| 595 | /* Cleared the list of pids */ |
| 596 | trace_free_pid_list(pid_list); |
| 597 | read = ret; |
| 598 | pid_list = NULL; |
| 599 | } |
| 600 | |
| 601 | *new_pid_list = pid_list; |
| 602 | |
| 603 | return read; |
| 604 | } |
| 605 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 606 | static u64 buffer_ftrace_now(struct array_buffer *buf, int cpu) |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 607 | { |
| 608 | u64 ts; |
| 609 | |
| 610 | /* Early boot up does not have a buffer yet */ |
Alexander Z Lam | 9457158 | 2013-08-02 18:36:16 -0700 | [diff] [blame] | 611 | if (!buf->buffer) |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 612 | return trace_clock_local(); |
| 613 | |
Alexander Z Lam | 9457158 | 2013-08-02 18:36:16 -0700 | [diff] [blame] | 614 | ts = ring_buffer_time_stamp(buf->buffer, cpu); |
| 615 | ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts); |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 616 | |
| 617 | return ts; |
| 618 | } |
| 619 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 620 | u64 ftrace_now(int cpu) |
Alexander Z Lam | 9457158 | 2013-08-02 18:36:16 -0700 | [diff] [blame] | 621 | { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 622 | return buffer_ftrace_now(&global_trace.array_buffer, cpu); |
Alexander Z Lam | 9457158 | 2013-08-02 18:36:16 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 625 | /** |
| 626 | * tracing_is_enabled - Show if global_trace has been disabled |
| 627 | * |
| 628 | * Shows if the global trace has been enabled or not. It uses the |
| 629 | * mirror flag "buffer_disabled" to be used in fast paths such as for |
| 630 | * the irqsoff tracer. But it may be inaccurate due to races. If you |
| 631 | * need to know the accurate state, use tracing_is_on() which is a little |
| 632 | * slower, but accurate. |
| 633 | */ |
Steven Rostedt | 9036990 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 634 | int tracing_is_enabled(void) |
| 635 | { |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 636 | /* |
| 637 | * For quick access (irqsoff uses this in fast path), just |
| 638 | * return the mirror variable of the state of the ring buffer. |
| 639 | * It's a little racy, but we don't really care. |
| 640 | */ |
| 641 | smp_rmb(); |
| 642 | return !global_trace.buffer_disabled; |
Steven Rostedt | 9036990 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 643 | } |
| 644 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 645 | /* |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 646 | * trace_buf_size is the size in bytes that is allocated |
| 647 | * for a buffer. Note, the number of bytes is always rounded |
| 648 | * to page size. |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 649 | * |
| 650 | * This number is purposely set to a low number of 16384. |
| 651 | * If the dump on oops happens, it will be much appreciated |
| 652 | * to not have to wait for all that output. Anyway this can be |
| 653 | * boot time and run time configurable. |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 654 | */ |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 655 | #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */ |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 656 | |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 657 | static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 658 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 659 | /* trace_types holds a link list of available tracers. */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 660 | static struct tracer *trace_types __read_mostly; |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 661 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 662 | /* |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 663 | * trace_types_lock is used to protect the trace_types list. |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 664 | */ |
Alexander Z Lam | a822741 | 2013-07-01 19:37:54 -0700 | [diff] [blame] | 665 | DEFINE_MUTEX(trace_types_lock); |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 666 | |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 667 | /* |
| 668 | * serialize the access of the ring buffer |
| 669 | * |
| 670 | * ring buffer serializes readers, but it is low level protection. |
| 671 | * The validity of the events (which returns by ring_buffer_peek() ..etc) |
| 672 | * are not protected by ring buffer. |
| 673 | * |
| 674 | * The content of events may become garbage if we allow other process consumes |
| 675 | * these events concurrently: |
| 676 | * A) the page of the consumed events may become a normal page |
| 677 | * (not reader page) in ring buffer, and this page will be rewrited |
| 678 | * by events producer. |
| 679 | * B) The page of the consumed events may become a page for splice_read, |
| 680 | * and this page will be returned to system. |
| 681 | * |
| 682 | * These primitives allow multi process access to different cpu ring buffer |
| 683 | * concurrently. |
| 684 | * |
| 685 | * These primitives don't distinguish read-only and read-consume access. |
| 686 | * Multi read-only access are also serialized. |
| 687 | */ |
| 688 | |
| 689 | #ifdef CONFIG_SMP |
| 690 | static DECLARE_RWSEM(all_cpu_access_lock); |
| 691 | static DEFINE_PER_CPU(struct mutex, cpu_access_lock); |
| 692 | |
| 693 | static inline void trace_access_lock(int cpu) |
| 694 | { |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 695 | if (cpu == RING_BUFFER_ALL_CPUS) { |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 696 | /* gain it for accessing the whole ring buffer. */ |
| 697 | down_write(&all_cpu_access_lock); |
| 698 | } else { |
| 699 | /* gain it for accessing a cpu ring buffer. */ |
| 700 | |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 701 | /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */ |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 702 | down_read(&all_cpu_access_lock); |
| 703 | |
| 704 | /* Secondly block other access to this @cpu ring buffer. */ |
| 705 | mutex_lock(&per_cpu(cpu_access_lock, cpu)); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | static inline void trace_access_unlock(int cpu) |
| 710 | { |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 711 | if (cpu == RING_BUFFER_ALL_CPUS) { |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 712 | up_write(&all_cpu_access_lock); |
| 713 | } else { |
| 714 | mutex_unlock(&per_cpu(cpu_access_lock, cpu)); |
| 715 | up_read(&all_cpu_access_lock); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | static inline void trace_access_lock_init(void) |
| 720 | { |
| 721 | int cpu; |
| 722 | |
| 723 | for_each_possible_cpu(cpu) |
| 724 | mutex_init(&per_cpu(cpu_access_lock, cpu)); |
| 725 | } |
| 726 | |
| 727 | #else |
| 728 | |
| 729 | static DEFINE_MUTEX(access_lock); |
| 730 | |
| 731 | static inline void trace_access_lock(int cpu) |
| 732 | { |
| 733 | (void)cpu; |
| 734 | mutex_lock(&access_lock); |
| 735 | } |
| 736 | |
| 737 | static inline void trace_access_unlock(int cpu) |
| 738 | { |
| 739 | (void)cpu; |
| 740 | mutex_unlock(&access_lock); |
| 741 | } |
| 742 | |
| 743 | static inline void trace_access_lock_init(void) |
| 744 | { |
| 745 | } |
| 746 | |
| 747 | #endif |
| 748 | |
Steven Rostedt (Red Hat) | d78a461 | 2015-09-25 13:30:47 -0400 | [diff] [blame] | 749 | #ifdef CONFIG_STACKTRACE |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 750 | static void __ftrace_trace_stack(struct trace_buffer *buffer, |
Steven Rostedt (Red Hat) | d78a461 | 2015-09-25 13:30:47 -0400 | [diff] [blame] | 751 | unsigned long flags, |
| 752 | int skip, int pc, struct pt_regs *regs); |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 753 | static inline void ftrace_trace_stack(struct trace_array *tr, |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 754 | struct trace_buffer *buffer, |
Steven Rostedt (Red Hat) | 73dddbb | 2015-09-29 15:38:55 -0400 | [diff] [blame] | 755 | unsigned long flags, |
| 756 | int skip, int pc, struct pt_regs *regs); |
Steven Rostedt (Red Hat) | ca475e8 | 2015-09-28 09:41:11 -0400 | [diff] [blame] | 757 | |
Steven Rostedt (Red Hat) | d78a461 | 2015-09-25 13:30:47 -0400 | [diff] [blame] | 758 | #else |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 759 | static inline void __ftrace_trace_stack(struct trace_buffer *buffer, |
Steven Rostedt (Red Hat) | d78a461 | 2015-09-25 13:30:47 -0400 | [diff] [blame] | 760 | unsigned long flags, |
| 761 | int skip, int pc, struct pt_regs *regs) |
| 762 | { |
| 763 | } |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 764 | static inline void ftrace_trace_stack(struct trace_array *tr, |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 765 | struct trace_buffer *buffer, |
Steven Rostedt (Red Hat) | 73dddbb | 2015-09-29 15:38:55 -0400 | [diff] [blame] | 766 | unsigned long flags, |
| 767 | int skip, int pc, struct pt_regs *regs) |
Steven Rostedt (Red Hat) | ca475e8 | 2015-09-28 09:41:11 -0400 | [diff] [blame] | 768 | { |
| 769 | } |
| 770 | |
Steven Rostedt (Red Hat) | d78a461 | 2015-09-25 13:30:47 -0400 | [diff] [blame] | 771 | #endif |
| 772 | |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 773 | static __always_inline void |
| 774 | trace_event_setup(struct ring_buffer_event *event, |
| 775 | int type, unsigned long flags, int pc) |
| 776 | { |
| 777 | struct trace_entry *ent = ring_buffer_event_data(event); |
| 778 | |
Cong Wang | 46710f3 | 2019-05-25 09:57:59 -0700 | [diff] [blame] | 779 | tracing_generic_entry_update(ent, type, flags, pc); |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | static __always_inline struct ring_buffer_event * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 783 | __trace_buffer_lock_reserve(struct trace_buffer *buffer, |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 784 | int type, |
| 785 | unsigned long len, |
| 786 | unsigned long flags, int pc) |
| 787 | { |
| 788 | struct ring_buffer_event *event; |
| 789 | |
| 790 | event = ring_buffer_lock_reserve(buffer, len); |
| 791 | if (event != NULL) |
| 792 | trace_event_setup(event, type, flags, pc); |
| 793 | |
| 794 | return event; |
| 795 | } |
| 796 | |
Steven Rostedt (VMware) | 2290f2c | 2017-04-20 11:46:03 -0400 | [diff] [blame] | 797 | void tracer_tracing_on(struct trace_array *tr) |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 798 | { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 799 | if (tr->array_buffer.buffer) |
| 800 | ring_buffer_record_on(tr->array_buffer.buffer); |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 801 | /* |
| 802 | * This flag is looked at when buffers haven't been allocated |
| 803 | * yet, or by some tracers (like irqsoff), that just want to |
| 804 | * know if the ring buffer has been disabled, but it can handle |
| 805 | * races of where it gets disabled but we still do a record. |
| 806 | * As the check is in the fast path of the tracers, it is more |
| 807 | * important to be fast than accurate. |
| 808 | */ |
| 809 | tr->buffer_disabled = 0; |
| 810 | /* Make the flag seen by readers */ |
| 811 | smp_wmb(); |
| 812 | } |
| 813 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 814 | /** |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 815 | * tracing_on - enable tracing buffers |
| 816 | * |
| 817 | * This function enables tracing buffers that may have been |
| 818 | * disabled with tracing_off. |
| 819 | */ |
| 820 | void tracing_on(void) |
| 821 | { |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 822 | tracer_tracing_on(&global_trace); |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 823 | } |
| 824 | EXPORT_SYMBOL_GPL(tracing_on); |
| 825 | |
Steven Rostedt (Red Hat) | 52ffabe3 | 2016-11-23 20:28:38 -0500 | [diff] [blame] | 826 | |
| 827 | static __always_inline void |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 828 | __buffer_unlock_commit(struct trace_buffer *buffer, struct ring_buffer_event *event) |
Steven Rostedt (Red Hat) | 52ffabe3 | 2016-11-23 20:28:38 -0500 | [diff] [blame] | 829 | { |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 830 | __this_cpu_write(trace_taskinfo_save, true); |
Steven Rostedt (Red Hat) | 52ffabe3 | 2016-11-23 20:28:38 -0500 | [diff] [blame] | 831 | |
| 832 | /* If this is the temp buffer, we need to commit fully */ |
| 833 | if (this_cpu_read(trace_buffered_event) == event) { |
| 834 | /* Length is in event->array[0] */ |
| 835 | ring_buffer_write(buffer, event->array[0], &event->array[1]); |
| 836 | /* Release the temp buffer */ |
| 837 | this_cpu_dec(trace_buffered_event_cnt); |
| 838 | } else |
| 839 | ring_buffer_unlock_commit(buffer, event); |
| 840 | } |
| 841 | |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 842 | /** |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 843 | * __trace_puts - write a constant string into the trace buffer. |
| 844 | * @ip: The address of the caller |
| 845 | * @str: The constant string to write |
| 846 | * @size: The size of the string. |
| 847 | */ |
| 848 | int __trace_puts(unsigned long ip, const char *str, int size) |
| 849 | { |
| 850 | struct ring_buffer_event *event; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 851 | struct trace_buffer *buffer; |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 852 | struct print_entry *entry; |
| 853 | unsigned long irq_flags; |
| 854 | int alloc; |
zhangwei(Jovi) | 8abfb87 | 2013-07-18 16:31:05 +0800 | [diff] [blame] | 855 | int pc; |
| 856 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 857 | if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) |
zhangwei(Jovi) | f0160a5 | 2013-07-18 16:31:18 +0800 | [diff] [blame] | 858 | return 0; |
| 859 | |
zhangwei(Jovi) | 8abfb87 | 2013-07-18 16:31:05 +0800 | [diff] [blame] | 860 | pc = preempt_count(); |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 861 | |
Steven Rostedt (Red Hat) | 3132e10 | 2014-01-23 12:27:59 -0500 | [diff] [blame] | 862 | if (unlikely(tracing_selftest_running || tracing_disabled)) |
| 863 | return 0; |
| 864 | |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 865 | alloc = sizeof(*entry) + size + 2; /* possible \n added */ |
| 866 | |
| 867 | local_save_flags(irq_flags); |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 868 | buffer = global_trace.array_buffer.buffer; |
Steven Rostedt (VMware) | 82d1b81 | 2020-01-16 08:20:18 -0500 | [diff] [blame] | 869 | ring_buffer_nest_start(buffer); |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 870 | event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc, |
| 871 | irq_flags, pc); |
Steven Rostedt (VMware) | 82d1b81 | 2020-01-16 08:20:18 -0500 | [diff] [blame] | 872 | if (!event) { |
| 873 | size = 0; |
| 874 | goto out; |
| 875 | } |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 876 | |
| 877 | entry = ring_buffer_event_data(event); |
| 878 | entry->ip = ip; |
| 879 | |
| 880 | memcpy(&entry->buf, str, size); |
| 881 | |
| 882 | /* Add a newline if necessary */ |
| 883 | if (entry->buf[size - 1] != '\n') { |
| 884 | entry->buf[size] = '\n'; |
| 885 | entry->buf[size + 1] = '\0'; |
| 886 | } else |
| 887 | entry->buf[size] = '\0'; |
| 888 | |
| 889 | __buffer_unlock_commit(buffer, event); |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 890 | ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL); |
Steven Rostedt (VMware) | 82d1b81 | 2020-01-16 08:20:18 -0500 | [diff] [blame] | 891 | out: |
| 892 | ring_buffer_nest_end(buffer); |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 893 | return size; |
| 894 | } |
| 895 | EXPORT_SYMBOL_GPL(__trace_puts); |
| 896 | |
| 897 | /** |
| 898 | * __trace_bputs - write the pointer to a constant string into trace buffer |
| 899 | * @ip: The address of the caller |
| 900 | * @str: The constant string to write to the buffer to |
| 901 | */ |
| 902 | int __trace_bputs(unsigned long ip, const char *str) |
| 903 | { |
| 904 | struct ring_buffer_event *event; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 905 | struct trace_buffer *buffer; |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 906 | struct bputs_entry *entry; |
| 907 | unsigned long irq_flags; |
| 908 | int size = sizeof(struct bputs_entry); |
Steven Rostedt (VMware) | 82d1b81 | 2020-01-16 08:20:18 -0500 | [diff] [blame] | 909 | int ret = 0; |
zhangwei(Jovi) | 8abfb87 | 2013-07-18 16:31:05 +0800 | [diff] [blame] | 910 | int pc; |
| 911 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 912 | if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) |
zhangwei(Jovi) | f0160a5 | 2013-07-18 16:31:18 +0800 | [diff] [blame] | 913 | return 0; |
| 914 | |
zhangwei(Jovi) | 8abfb87 | 2013-07-18 16:31:05 +0800 | [diff] [blame] | 915 | pc = preempt_count(); |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 916 | |
Steven Rostedt (Red Hat) | 3132e10 | 2014-01-23 12:27:59 -0500 | [diff] [blame] | 917 | if (unlikely(tracing_selftest_running || tracing_disabled)) |
| 918 | return 0; |
| 919 | |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 920 | local_save_flags(irq_flags); |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 921 | buffer = global_trace.array_buffer.buffer; |
Steven Rostedt (VMware) | 82d1b81 | 2020-01-16 08:20:18 -0500 | [diff] [blame] | 922 | |
| 923 | ring_buffer_nest_start(buffer); |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 924 | event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size, |
| 925 | irq_flags, pc); |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 926 | if (!event) |
Steven Rostedt (VMware) | 82d1b81 | 2020-01-16 08:20:18 -0500 | [diff] [blame] | 927 | goto out; |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 928 | |
| 929 | entry = ring_buffer_event_data(event); |
| 930 | entry->ip = ip; |
| 931 | entry->str = str; |
| 932 | |
| 933 | __buffer_unlock_commit(buffer, event); |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 934 | ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL); |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 935 | |
Steven Rostedt (VMware) | 82d1b81 | 2020-01-16 08:20:18 -0500 | [diff] [blame] | 936 | ret = 1; |
| 937 | out: |
| 938 | ring_buffer_nest_end(buffer); |
| 939 | return ret; |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 940 | } |
| 941 | EXPORT_SYMBOL_GPL(__trace_bputs); |
| 942 | |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 943 | #ifdef CONFIG_TRACER_SNAPSHOT |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 944 | void tracing_snapshot_instance_cond(struct trace_array *tr, void *cond_data) |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 945 | { |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 946 | struct tracer *tracer = tr->current_trace; |
| 947 | unsigned long flags; |
| 948 | |
Steven Rostedt (Red Hat) | 1b22e38 | 2013-03-09 00:56:08 -0500 | [diff] [blame] | 949 | if (in_nmi()) { |
| 950 | internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n"); |
| 951 | internal_trace_puts("*** snapshot is being ignored ***\n"); |
| 952 | return; |
| 953 | } |
| 954 | |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 955 | if (!tr->allocated_snapshot) { |
Steven Rostedt (Red Hat) | ca268da | 2013-03-09 00:40:58 -0500 | [diff] [blame] | 956 | internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n"); |
| 957 | internal_trace_puts("*** stopping trace here! ***\n"); |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 958 | tracing_off(); |
| 959 | return; |
| 960 | } |
| 961 | |
| 962 | /* Note, snapshot can not be used when the tracer uses it */ |
| 963 | if (tracer->use_max_tr) { |
Steven Rostedt (Red Hat) | ca268da | 2013-03-09 00:40:58 -0500 | [diff] [blame] | 964 | internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n"); |
| 965 | internal_trace_puts("*** Can not use snapshot (sorry) ***\n"); |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 966 | return; |
| 967 | } |
| 968 | |
| 969 | local_irq_save(flags); |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 970 | update_max_tr(tr, current, smp_processor_id(), cond_data); |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 971 | local_irq_restore(flags); |
| 972 | } |
Steven Rostedt (VMware) | cab5037 | 2017-04-20 11:34:06 -0400 | [diff] [blame] | 973 | |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 974 | void tracing_snapshot_instance(struct trace_array *tr) |
| 975 | { |
| 976 | tracing_snapshot_instance_cond(tr, NULL); |
| 977 | } |
| 978 | |
Steven Rostedt (VMware) | cab5037 | 2017-04-20 11:34:06 -0400 | [diff] [blame] | 979 | /** |
Chunyu Hu | 5a93bae | 2017-10-19 14:32:33 +0800 | [diff] [blame] | 980 | * tracing_snapshot - take a snapshot of the current buffer. |
Steven Rostedt (VMware) | cab5037 | 2017-04-20 11:34:06 -0400 | [diff] [blame] | 981 | * |
| 982 | * This causes a swap between the snapshot buffer and the current live |
| 983 | * tracing buffer. You can use this to take snapshots of the live |
| 984 | * trace when some condition is triggered, but continue to trace. |
| 985 | * |
| 986 | * Note, make sure to allocate the snapshot with either |
| 987 | * a tracing_snapshot_alloc(), or by doing it manually |
| 988 | * with: echo 1 > /sys/kernel/debug/tracing/snapshot |
| 989 | * |
| 990 | * If the snapshot buffer is not allocated, it will stop tracing. |
| 991 | * Basically making a permanent snapshot. |
| 992 | */ |
| 993 | void tracing_snapshot(void) |
| 994 | { |
| 995 | struct trace_array *tr = &global_trace; |
| 996 | |
| 997 | tracing_snapshot_instance(tr); |
| 998 | } |
Steven Rostedt (Red Hat) | 1b22e38 | 2013-03-09 00:56:08 -0500 | [diff] [blame] | 999 | EXPORT_SYMBOL_GPL(tracing_snapshot); |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1000 | |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 1001 | /** |
| 1002 | * tracing_snapshot_cond - conditionally take a snapshot of the current buffer. |
| 1003 | * @tr: The tracing instance to snapshot |
| 1004 | * @cond_data: The data to be tested conditionally, and possibly saved |
| 1005 | * |
| 1006 | * This is the same as tracing_snapshot() except that the snapshot is |
| 1007 | * conditional - the snapshot will only happen if the |
| 1008 | * cond_snapshot.update() implementation receiving the cond_data |
| 1009 | * returns true, which means that the trace array's cond_snapshot |
| 1010 | * update() operation used the cond_data to determine whether the |
| 1011 | * snapshot should be taken, and if it was, presumably saved it along |
| 1012 | * with the snapshot. |
| 1013 | */ |
| 1014 | void tracing_snapshot_cond(struct trace_array *tr, void *cond_data) |
| 1015 | { |
| 1016 | tracing_snapshot_instance_cond(tr, cond_data); |
| 1017 | } |
| 1018 | EXPORT_SYMBOL_GPL(tracing_snapshot_cond); |
| 1019 | |
| 1020 | /** |
| 1021 | * tracing_snapshot_cond_data - get the user data associated with a snapshot |
| 1022 | * @tr: The tracing instance |
| 1023 | * |
| 1024 | * When the user enables a conditional snapshot using |
| 1025 | * tracing_snapshot_cond_enable(), the user-defined cond_data is saved |
| 1026 | * with the snapshot. This accessor is used to retrieve it. |
| 1027 | * |
| 1028 | * Should not be called from cond_snapshot.update(), since it takes |
| 1029 | * the tr->max_lock lock, which the code calling |
| 1030 | * cond_snapshot.update() has already done. |
| 1031 | * |
| 1032 | * Returns the cond_data associated with the trace array's snapshot. |
| 1033 | */ |
| 1034 | void *tracing_cond_snapshot_data(struct trace_array *tr) |
| 1035 | { |
| 1036 | void *cond_data = NULL; |
| 1037 | |
| 1038 | arch_spin_lock(&tr->max_lock); |
| 1039 | |
| 1040 | if (tr->cond_snapshot) |
| 1041 | cond_data = tr->cond_snapshot->cond_data; |
| 1042 | |
| 1043 | arch_spin_unlock(&tr->max_lock); |
| 1044 | |
| 1045 | return cond_data; |
| 1046 | } |
| 1047 | EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data); |
| 1048 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 1049 | static int resize_buffer_duplicate_size(struct array_buffer *trace_buf, |
| 1050 | struct array_buffer *size_buf, int cpu_id); |
| 1051 | static void set_buffer_entries(struct array_buffer *buf, unsigned long val); |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 1052 | |
Steven Rostedt (VMware) | 2824f50 | 2018-05-28 10:56:36 -0400 | [diff] [blame] | 1053 | int tracing_alloc_snapshot_instance(struct trace_array *tr) |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 1054 | { |
| 1055 | int ret; |
| 1056 | |
| 1057 | if (!tr->allocated_snapshot) { |
| 1058 | |
| 1059 | /* allocate spare buffer */ |
| 1060 | ret = resize_buffer_duplicate_size(&tr->max_buffer, |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 1061 | &tr->array_buffer, RING_BUFFER_ALL_CPUS); |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 1062 | if (ret < 0) |
| 1063 | return ret; |
| 1064 | |
| 1065 | tr->allocated_snapshot = true; |
| 1066 | } |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
Fabian Frederick | ad1438a | 2014-04-17 21:44:42 +0200 | [diff] [blame] | 1071 | static void free_snapshot(struct trace_array *tr) |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 1072 | { |
| 1073 | /* |
| 1074 | * We don't free the ring buffer. instead, resize it because |
| 1075 | * The max_tr ring buffer has some state (e.g. ring->clock) and |
| 1076 | * we want preserve it. |
| 1077 | */ |
| 1078 | ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS); |
| 1079 | set_buffer_entries(&tr->max_buffer, 1); |
| 1080 | tracing_reset_online_cpus(&tr->max_buffer); |
| 1081 | tr->allocated_snapshot = false; |
| 1082 | } |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1083 | |
| 1084 | /** |
Tom Zanussi | 93e31ff | 2013-10-24 08:59:26 -0500 | [diff] [blame] | 1085 | * tracing_alloc_snapshot - allocate snapshot buffer. |
| 1086 | * |
| 1087 | * This only allocates the snapshot buffer if it isn't already |
| 1088 | * allocated - it doesn't also take a snapshot. |
| 1089 | * |
| 1090 | * This is meant to be used in cases where the snapshot buffer needs |
| 1091 | * to be set up for events that can't sleep but need to be able to |
| 1092 | * trigger a snapshot. |
| 1093 | */ |
| 1094 | int tracing_alloc_snapshot(void) |
| 1095 | { |
| 1096 | struct trace_array *tr = &global_trace; |
| 1097 | int ret; |
| 1098 | |
Steven Rostedt (VMware) | 2824f50 | 2018-05-28 10:56:36 -0400 | [diff] [blame] | 1099 | ret = tracing_alloc_snapshot_instance(tr); |
Tom Zanussi | 93e31ff | 2013-10-24 08:59:26 -0500 | [diff] [blame] | 1100 | WARN_ON(ret < 0); |
| 1101 | |
| 1102 | return ret; |
| 1103 | } |
| 1104 | EXPORT_SYMBOL_GPL(tracing_alloc_snapshot); |
| 1105 | |
| 1106 | /** |
Chunyu Hu | 5a93bae | 2017-10-19 14:32:33 +0800 | [diff] [blame] | 1107 | * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer. |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1108 | * |
Chunyu Hu | 5a93bae | 2017-10-19 14:32:33 +0800 | [diff] [blame] | 1109 | * This is similar to tracing_snapshot(), but it will allocate the |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1110 | * snapshot buffer if it isn't already allocated. Use this only |
| 1111 | * where it is safe to sleep, as the allocation may sleep. |
| 1112 | * |
| 1113 | * This causes a swap between the snapshot buffer and the current live |
| 1114 | * tracing buffer. You can use this to take snapshots of the live |
| 1115 | * trace when some condition is triggered, but continue to trace. |
| 1116 | */ |
| 1117 | void tracing_snapshot_alloc(void) |
| 1118 | { |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1119 | int ret; |
| 1120 | |
Tom Zanussi | 93e31ff | 2013-10-24 08:59:26 -0500 | [diff] [blame] | 1121 | ret = tracing_alloc_snapshot(); |
| 1122 | if (ret < 0) |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 1123 | return; |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1124 | |
| 1125 | tracing_snapshot(); |
| 1126 | } |
Steven Rostedt (Red Hat) | 1b22e38 | 2013-03-09 00:56:08 -0500 | [diff] [blame] | 1127 | EXPORT_SYMBOL_GPL(tracing_snapshot_alloc); |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 1128 | |
| 1129 | /** |
| 1130 | * tracing_snapshot_cond_enable - enable conditional snapshot for an instance |
| 1131 | * @tr: The tracing instance |
| 1132 | * @cond_data: User data to associate with the snapshot |
| 1133 | * @update: Implementation of the cond_snapshot update function |
| 1134 | * |
| 1135 | * Check whether the conditional snapshot for the given instance has |
| 1136 | * already been enabled, or if the current tracer is already using a |
| 1137 | * snapshot; if so, return -EBUSY, else create a cond_snapshot and |
| 1138 | * save the cond_data and update function inside. |
| 1139 | * |
| 1140 | * Returns 0 if successful, error otherwise. |
| 1141 | */ |
| 1142 | int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, |
| 1143 | cond_update_fn_t update) |
| 1144 | { |
| 1145 | struct cond_snapshot *cond_snapshot; |
| 1146 | int ret = 0; |
| 1147 | |
| 1148 | cond_snapshot = kzalloc(sizeof(*cond_snapshot), GFP_KERNEL); |
| 1149 | if (!cond_snapshot) |
| 1150 | return -ENOMEM; |
| 1151 | |
| 1152 | cond_snapshot->cond_data = cond_data; |
| 1153 | cond_snapshot->update = update; |
| 1154 | |
| 1155 | mutex_lock(&trace_types_lock); |
| 1156 | |
| 1157 | ret = tracing_alloc_snapshot_instance(tr); |
| 1158 | if (ret) |
| 1159 | goto fail_unlock; |
| 1160 | |
| 1161 | if (tr->current_trace->use_max_tr) { |
| 1162 | ret = -EBUSY; |
| 1163 | goto fail_unlock; |
| 1164 | } |
| 1165 | |
Steven Rostedt (VMware) | 1c347a9 | 2019-02-14 18:45:21 -0500 | [diff] [blame] | 1166 | /* |
| 1167 | * The cond_snapshot can only change to NULL without the |
| 1168 | * trace_types_lock. We don't care if we race with it going |
| 1169 | * to NULL, but we want to make sure that it's not set to |
| 1170 | * something other than NULL when we get here, which we can |
| 1171 | * do safely with only holding the trace_types_lock and not |
| 1172 | * having to take the max_lock. |
| 1173 | */ |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 1174 | if (tr->cond_snapshot) { |
| 1175 | ret = -EBUSY; |
| 1176 | goto fail_unlock; |
| 1177 | } |
| 1178 | |
| 1179 | arch_spin_lock(&tr->max_lock); |
| 1180 | tr->cond_snapshot = cond_snapshot; |
| 1181 | arch_spin_unlock(&tr->max_lock); |
| 1182 | |
| 1183 | mutex_unlock(&trace_types_lock); |
| 1184 | |
| 1185 | return ret; |
| 1186 | |
| 1187 | fail_unlock: |
| 1188 | mutex_unlock(&trace_types_lock); |
| 1189 | kfree(cond_snapshot); |
| 1190 | return ret; |
| 1191 | } |
| 1192 | EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable); |
| 1193 | |
| 1194 | /** |
| 1195 | * tracing_snapshot_cond_disable - disable conditional snapshot for an instance |
| 1196 | * @tr: The tracing instance |
| 1197 | * |
| 1198 | * Check whether the conditional snapshot for the given instance is |
| 1199 | * enabled; if so, free the cond_snapshot associated with it, |
| 1200 | * otherwise return -EINVAL. |
| 1201 | * |
| 1202 | * Returns 0 if successful, error otherwise. |
| 1203 | */ |
| 1204 | int tracing_snapshot_cond_disable(struct trace_array *tr) |
| 1205 | { |
| 1206 | int ret = 0; |
| 1207 | |
| 1208 | arch_spin_lock(&tr->max_lock); |
| 1209 | |
| 1210 | if (!tr->cond_snapshot) |
| 1211 | ret = -EINVAL; |
| 1212 | else { |
| 1213 | kfree(tr->cond_snapshot); |
| 1214 | tr->cond_snapshot = NULL; |
| 1215 | } |
| 1216 | |
| 1217 | arch_spin_unlock(&tr->max_lock); |
| 1218 | |
| 1219 | return ret; |
| 1220 | } |
| 1221 | EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable); |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1222 | #else |
| 1223 | void tracing_snapshot(void) |
| 1224 | { |
| 1225 | WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used"); |
| 1226 | } |
Steven Rostedt (Red Hat) | 1b22e38 | 2013-03-09 00:56:08 -0500 | [diff] [blame] | 1227 | EXPORT_SYMBOL_GPL(tracing_snapshot); |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 1228 | void tracing_snapshot_cond(struct trace_array *tr, void *cond_data) |
| 1229 | { |
| 1230 | WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used"); |
| 1231 | } |
| 1232 | EXPORT_SYMBOL_GPL(tracing_snapshot_cond); |
Tom Zanussi | 93e31ff | 2013-10-24 08:59:26 -0500 | [diff] [blame] | 1233 | int tracing_alloc_snapshot(void) |
| 1234 | { |
| 1235 | WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used"); |
| 1236 | return -ENODEV; |
| 1237 | } |
| 1238 | EXPORT_SYMBOL_GPL(tracing_alloc_snapshot); |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1239 | void tracing_snapshot_alloc(void) |
| 1240 | { |
| 1241 | /* Give warning */ |
| 1242 | tracing_snapshot(); |
| 1243 | } |
Steven Rostedt (Red Hat) | 1b22e38 | 2013-03-09 00:56:08 -0500 | [diff] [blame] | 1244 | EXPORT_SYMBOL_GPL(tracing_snapshot_alloc); |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 1245 | void *tracing_cond_snapshot_data(struct trace_array *tr) |
| 1246 | { |
| 1247 | return NULL; |
| 1248 | } |
| 1249 | EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data); |
| 1250 | int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update) |
| 1251 | { |
| 1252 | return -ENODEV; |
| 1253 | } |
| 1254 | EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable); |
| 1255 | int tracing_snapshot_cond_disable(struct trace_array *tr) |
| 1256 | { |
| 1257 | return false; |
| 1258 | } |
| 1259 | EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable); |
Steven Rostedt (Red Hat) | ad909e2 | 2013-03-06 21:45:37 -0500 | [diff] [blame] | 1260 | #endif /* CONFIG_TRACER_SNAPSHOT */ |
| 1261 | |
Steven Rostedt (VMware) | 2290f2c | 2017-04-20 11:46:03 -0400 | [diff] [blame] | 1262 | void tracer_tracing_off(struct trace_array *tr) |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 1263 | { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 1264 | if (tr->array_buffer.buffer) |
| 1265 | ring_buffer_record_off(tr->array_buffer.buffer); |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 1266 | /* |
| 1267 | * This flag is looked at when buffers haven't been allocated |
| 1268 | * yet, or by some tracers (like irqsoff), that just want to |
| 1269 | * know if the ring buffer has been disabled, but it can handle |
| 1270 | * races of where it gets disabled but we still do a record. |
| 1271 | * As the check is in the fast path of the tracers, it is more |
| 1272 | * important to be fast than accurate. |
| 1273 | */ |
| 1274 | tr->buffer_disabled = 1; |
| 1275 | /* Make the flag seen by readers */ |
| 1276 | smp_wmb(); |
| 1277 | } |
| 1278 | |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 1279 | /** |
| 1280 | * tracing_off - turn off tracing buffers |
| 1281 | * |
| 1282 | * This function stops the tracing buffers from recording data. |
| 1283 | * It does not disable any overhead the tracers themselves may |
| 1284 | * be causing. This function simply causes all recording to |
| 1285 | * the ring buffers to fail. |
| 1286 | */ |
| 1287 | void tracing_off(void) |
| 1288 | { |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 1289 | tracer_tracing_off(&global_trace); |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 1290 | } |
| 1291 | EXPORT_SYMBOL_GPL(tracing_off); |
| 1292 | |
Steven Rostedt (Red Hat) | de7edd3 | 2013-06-14 16:21:43 -0400 | [diff] [blame] | 1293 | void disable_trace_on_warning(void) |
| 1294 | { |
| 1295 | if (__disable_trace_on_warning) |
| 1296 | tracing_off(); |
| 1297 | } |
| 1298 | |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 1299 | /** |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 1300 | * tracer_tracing_is_on - show real state of ring buffer enabled |
| 1301 | * @tr : the trace array to know if ring buffer is enabled |
| 1302 | * |
| 1303 | * Shows real state of the ring buffer if it is enabled or not. |
| 1304 | */ |
Steven Rostedt (VMware) | ec57350 | 2018-08-01 16:08:57 -0400 | [diff] [blame] | 1305 | bool tracer_tracing_is_on(struct trace_array *tr) |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 1306 | { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 1307 | if (tr->array_buffer.buffer) |
| 1308 | return ring_buffer_record_is_on(tr->array_buffer.buffer); |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 1309 | return !tr->buffer_disabled; |
| 1310 | } |
| 1311 | |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 1312 | /** |
| 1313 | * tracing_is_on - show state of ring buffers enabled |
| 1314 | */ |
| 1315 | int tracing_is_on(void) |
| 1316 | { |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 1317 | return tracer_tracing_is_on(&global_trace); |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 1318 | } |
| 1319 | EXPORT_SYMBOL_GPL(tracing_is_on); |
| 1320 | |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1321 | static int __init set_buf_size(char *str) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1322 | { |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1323 | unsigned long buf_size; |
Steven Rostedt | c6caeeb | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1324 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1325 | if (!str) |
| 1326 | return 0; |
Li Zefan | 9d612be | 2009-06-24 17:33:15 +0800 | [diff] [blame] | 1327 | buf_size = memparse(str, &str); |
Steven Rostedt | c6caeeb | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1328 | /* nr_entries can not be zero */ |
Li Zefan | 9d612be | 2009-06-24 17:33:15 +0800 | [diff] [blame] | 1329 | if (buf_size == 0) |
Steven Rostedt | c6caeeb | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1330 | return 0; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1331 | trace_buf_size = buf_size; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1332 | return 1; |
| 1333 | } |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1334 | __setup("trace_buf_size=", set_buf_size); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1335 | |
Tim Bird | 0e95017 | 2010-02-25 15:36:43 -0800 | [diff] [blame] | 1336 | static int __init set_tracing_thresh(char *str) |
| 1337 | { |
Wang Tianhong | 87abb3b | 2012-08-02 14:02:00 +0800 | [diff] [blame] | 1338 | unsigned long threshold; |
Tim Bird | 0e95017 | 2010-02-25 15:36:43 -0800 | [diff] [blame] | 1339 | int ret; |
| 1340 | |
| 1341 | if (!str) |
| 1342 | return 0; |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 1343 | ret = kstrtoul(str, 0, &threshold); |
Tim Bird | 0e95017 | 2010-02-25 15:36:43 -0800 | [diff] [blame] | 1344 | if (ret < 0) |
| 1345 | return 0; |
Wang Tianhong | 87abb3b | 2012-08-02 14:02:00 +0800 | [diff] [blame] | 1346 | tracing_thresh = threshold * 1000; |
Tim Bird | 0e95017 | 2010-02-25 15:36:43 -0800 | [diff] [blame] | 1347 | return 1; |
| 1348 | } |
| 1349 | __setup("tracing_thresh=", set_tracing_thresh); |
| 1350 | |
Steven Rostedt | 57f50be | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 1351 | unsigned long nsecs_to_usecs(unsigned long nsecs) |
| 1352 | { |
| 1353 | return nsecs / 1000; |
| 1354 | } |
| 1355 | |
Steven Rostedt (Red Hat) | a3418a3 | 2015-09-29 09:43:30 -0400 | [diff] [blame] | 1356 | /* |
| 1357 | * TRACE_FLAGS is defined as a tuple matching bit masks with strings. |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 1358 | * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that |
Steven Rostedt (Red Hat) | a3418a3 | 2015-09-29 09:43:30 -0400 | [diff] [blame] | 1359 | * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 1360 | * of strings in the order that the evals (enum) were defined. |
Steven Rostedt (Red Hat) | a3418a3 | 2015-09-29 09:43:30 -0400 | [diff] [blame] | 1361 | */ |
| 1362 | #undef C |
| 1363 | #define C(a, b) b |
| 1364 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1365 | /* These must match the bit postions in trace_iterator_flags */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1366 | static const char *trace_options[] = { |
Steven Rostedt (Red Hat) | a3418a3 | 2015-09-29 09:43:30 -0400 | [diff] [blame] | 1367 | TRACE_FLAGS |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1368 | NULL |
| 1369 | }; |
| 1370 | |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 1371 | static struct { |
| 1372 | u64 (*func)(void); |
| 1373 | const char *name; |
David Sharp | 8be0709 | 2012-11-13 12:18:22 -0800 | [diff] [blame] | 1374 | int in_ns; /* is this clock in nanoseconds? */ |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 1375 | } trace_clocks[] = { |
Thomas Gleixner | 1b3e5c0 | 2014-07-16 21:05:25 +0000 | [diff] [blame] | 1376 | { trace_clock_local, "local", 1 }, |
| 1377 | { trace_clock_global, "global", 1 }, |
| 1378 | { trace_clock_counter, "counter", 0 }, |
Linus Torvalds | e7fda6c | 2014-08-05 17:46:42 -0700 | [diff] [blame] | 1379 | { trace_clock_jiffies, "uptime", 0 }, |
Thomas Gleixner | 1b3e5c0 | 2014-07-16 21:05:25 +0000 | [diff] [blame] | 1380 | { trace_clock, "perf", 1 }, |
| 1381 | { ktime_get_mono_fast_ns, "mono", 1 }, |
Drew Richardson | aabfa5f | 2015-05-08 07:30:39 -0700 | [diff] [blame] | 1382 | { ktime_get_raw_fast_ns, "mono_raw", 1 }, |
Thomas Gleixner | a3ed0e43 | 2018-04-25 15:33:38 +0200 | [diff] [blame] | 1383 | { ktime_get_boot_fast_ns, "boot", 1 }, |
David Sharp | 8cbd9cc | 2012-11-13 12:18:21 -0800 | [diff] [blame] | 1384 | ARCH_TRACE_CLOCKS |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 1385 | }; |
| 1386 | |
Tom Zanussi | 860f9f6 | 2018-01-15 20:51:48 -0600 | [diff] [blame] | 1387 | bool trace_clock_in_ns(struct trace_array *tr) |
| 1388 | { |
| 1389 | if (trace_clocks[tr->clock_id].in_ns) |
| 1390 | return true; |
| 1391 | |
| 1392 | return false; |
| 1393 | } |
| 1394 | |
jolsa@redhat.com | b63f39e | 2009-09-11 17:29:27 +0200 | [diff] [blame] | 1395 | /* |
| 1396 | * trace_parser_get_init - gets the buffer for trace parser |
| 1397 | */ |
| 1398 | int trace_parser_get_init(struct trace_parser *parser, int size) |
| 1399 | { |
| 1400 | memset(parser, 0, sizeof(*parser)); |
| 1401 | |
| 1402 | parser->buffer = kmalloc(size, GFP_KERNEL); |
| 1403 | if (!parser->buffer) |
| 1404 | return 1; |
| 1405 | |
| 1406 | parser->size = size; |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | /* |
| 1411 | * trace_parser_put - frees the buffer for trace parser |
| 1412 | */ |
| 1413 | void trace_parser_put(struct trace_parser *parser) |
| 1414 | { |
| 1415 | kfree(parser->buffer); |
Steven Rostedt (VMware) | 0e684b6 | 2017-02-02 17:58:18 -0500 | [diff] [blame] | 1416 | parser->buffer = NULL; |
jolsa@redhat.com | b63f39e | 2009-09-11 17:29:27 +0200 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | /* |
| 1420 | * trace_get_user - reads the user input string separated by space |
| 1421 | * (matched by isspace(ch)) |
| 1422 | * |
| 1423 | * For each string found the 'struct trace_parser' is updated, |
| 1424 | * and the function returns. |
| 1425 | * |
| 1426 | * Returns number of bytes read. |
| 1427 | * |
| 1428 | * See kernel/trace/trace.h for 'struct trace_parser' details. |
| 1429 | */ |
| 1430 | int trace_get_user(struct trace_parser *parser, const char __user *ubuf, |
| 1431 | size_t cnt, loff_t *ppos) |
| 1432 | { |
| 1433 | char ch; |
| 1434 | size_t read = 0; |
| 1435 | ssize_t ret; |
| 1436 | |
| 1437 | if (!*ppos) |
| 1438 | trace_parser_clear(parser); |
| 1439 | |
| 1440 | ret = get_user(ch, ubuf++); |
| 1441 | if (ret) |
| 1442 | goto out; |
| 1443 | |
| 1444 | read++; |
| 1445 | cnt--; |
| 1446 | |
| 1447 | /* |
| 1448 | * The parser is not finished with the last write, |
| 1449 | * continue reading the user input without skipping spaces. |
| 1450 | */ |
| 1451 | if (!parser->cont) { |
| 1452 | /* skip white space */ |
| 1453 | while (cnt && isspace(ch)) { |
| 1454 | ret = get_user(ch, ubuf++); |
| 1455 | if (ret) |
| 1456 | goto out; |
| 1457 | read++; |
| 1458 | cnt--; |
| 1459 | } |
| 1460 | |
Changbin Du | 76638d9 | 2018-01-16 17:02:29 +0800 | [diff] [blame] | 1461 | parser->idx = 0; |
| 1462 | |
jolsa@redhat.com | b63f39e | 2009-09-11 17:29:27 +0200 | [diff] [blame] | 1463 | /* only spaces were written */ |
Changbin Du | 921a7ac | 2018-01-16 17:02:28 +0800 | [diff] [blame] | 1464 | if (isspace(ch) || !ch) { |
jolsa@redhat.com | b63f39e | 2009-09-11 17:29:27 +0200 | [diff] [blame] | 1465 | *ppos += read; |
| 1466 | ret = read; |
| 1467 | goto out; |
| 1468 | } |
jolsa@redhat.com | b63f39e | 2009-09-11 17:29:27 +0200 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | /* read the non-space input */ |
Changbin Du | 921a7ac | 2018-01-16 17:02:28 +0800 | [diff] [blame] | 1472 | while (cnt && !isspace(ch) && ch) { |
Li Zefan | 3c235a3 | 2009-09-22 13:51:54 +0800 | [diff] [blame] | 1473 | if (parser->idx < parser->size - 1) |
jolsa@redhat.com | b63f39e | 2009-09-11 17:29:27 +0200 | [diff] [blame] | 1474 | parser->buffer[parser->idx++] = ch; |
| 1475 | else { |
| 1476 | ret = -EINVAL; |
| 1477 | goto out; |
| 1478 | } |
| 1479 | ret = get_user(ch, ubuf++); |
| 1480 | if (ret) |
| 1481 | goto out; |
| 1482 | read++; |
| 1483 | cnt--; |
| 1484 | } |
| 1485 | |
| 1486 | /* We either got finished input or we have to wait for another call. */ |
Changbin Du | 921a7ac | 2018-01-16 17:02:28 +0800 | [diff] [blame] | 1487 | if (isspace(ch) || !ch) { |
jolsa@redhat.com | b63f39e | 2009-09-11 17:29:27 +0200 | [diff] [blame] | 1488 | parser->buffer[parser->idx] = 0; |
| 1489 | parser->cont = false; |
Steven Rostedt | 057db84 | 2013-10-09 22:23:23 -0400 | [diff] [blame] | 1490 | } else if (parser->idx < parser->size - 1) { |
jolsa@redhat.com | b63f39e | 2009-09-11 17:29:27 +0200 | [diff] [blame] | 1491 | parser->cont = true; |
| 1492 | parser->buffer[parser->idx++] = ch; |
Changbin Du | f4d0706 | 2018-01-16 17:02:30 +0800 | [diff] [blame] | 1493 | /* Make sure the parsed string always terminates with '\0'. */ |
| 1494 | parser->buffer[parser->idx] = 0; |
Steven Rostedt | 057db84 | 2013-10-09 22:23:23 -0400 | [diff] [blame] | 1495 | } else { |
| 1496 | ret = -EINVAL; |
| 1497 | goto out; |
jolsa@redhat.com | b63f39e | 2009-09-11 17:29:27 +0200 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | *ppos += read; |
| 1501 | ret = read; |
| 1502 | |
| 1503 | out: |
| 1504 | return ret; |
| 1505 | } |
| 1506 | |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 1507 | /* TODO add a seq_buf_to_buffer() */ |
Dmitri Vorobiev | b8b9426 | 2009-03-22 19:11:11 +0200 | [diff] [blame] | 1508 | static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt) |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 1509 | { |
| 1510 | int len; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 1511 | |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 1512 | if (trace_seq_used(s) <= s->seq.readpos) |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 1513 | return -EBUSY; |
| 1514 | |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 1515 | len = trace_seq_used(s) - s->seq.readpos; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 1516 | if (cnt > len) |
| 1517 | cnt = len; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 1518 | memcpy(buf, s->buffer + s->seq.readpos, cnt); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 1519 | |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 1520 | s->seq.readpos += cnt; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 1521 | return cnt; |
| 1522 | } |
| 1523 | |
Tim Bird | 0e95017 | 2010-02-25 15:36:43 -0800 | [diff] [blame] | 1524 | unsigned long __read_mostly tracing_thresh; |
Viktor Rosendahl (BMW) | 91edde2 | 2019-10-09 00:08:21 +0200 | [diff] [blame] | 1525 | static const struct file_operations tracing_max_lat_fops; |
| 1526 | |
| 1527 | #if (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \ |
| 1528 | defined(CONFIG_FSNOTIFY) |
| 1529 | |
| 1530 | static struct workqueue_struct *fsnotify_wq; |
| 1531 | |
| 1532 | static void latency_fsnotify_workfn(struct work_struct *work) |
| 1533 | { |
| 1534 | struct trace_array *tr = container_of(work, struct trace_array, |
| 1535 | fsnotify_work); |
| 1536 | fsnotify(tr->d_max_latency->d_inode, FS_MODIFY, |
| 1537 | tr->d_max_latency->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
| 1538 | } |
| 1539 | |
| 1540 | static void latency_fsnotify_workfn_irq(struct irq_work *iwork) |
| 1541 | { |
| 1542 | struct trace_array *tr = container_of(iwork, struct trace_array, |
| 1543 | fsnotify_irqwork); |
| 1544 | queue_work(fsnotify_wq, &tr->fsnotify_work); |
| 1545 | } |
| 1546 | |
| 1547 | static void trace_create_maxlat_file(struct trace_array *tr, |
| 1548 | struct dentry *d_tracer) |
| 1549 | { |
| 1550 | INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn); |
| 1551 | init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq); |
| 1552 | tr->d_max_latency = trace_create_file("tracing_max_latency", 0644, |
| 1553 | d_tracer, &tr->max_latency, |
| 1554 | &tracing_max_lat_fops); |
| 1555 | } |
| 1556 | |
| 1557 | __init static int latency_fsnotify_init(void) |
| 1558 | { |
| 1559 | fsnotify_wq = alloc_workqueue("tr_max_lat_wq", |
| 1560 | WQ_UNBOUND | WQ_HIGHPRI, 0); |
| 1561 | if (!fsnotify_wq) { |
| 1562 | pr_err("Unable to allocate tr_max_lat_wq\n"); |
| 1563 | return -ENOMEM; |
| 1564 | } |
| 1565 | return 0; |
| 1566 | } |
| 1567 | |
| 1568 | late_initcall_sync(latency_fsnotify_init); |
| 1569 | |
| 1570 | void latency_fsnotify(struct trace_array *tr) |
| 1571 | { |
| 1572 | if (!fsnotify_wq) |
| 1573 | return; |
| 1574 | /* |
| 1575 | * We cannot call queue_work(&tr->fsnotify_work) from here because it's |
| 1576 | * possible that we are called from __schedule() or do_idle(), which |
| 1577 | * could cause a deadlock. |
| 1578 | */ |
| 1579 | irq_work_queue(&tr->fsnotify_irqwork); |
| 1580 | } |
| 1581 | |
| 1582 | /* |
| 1583 | * (defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)) && \ |
| 1584 | * defined(CONFIG_FSNOTIFY) |
| 1585 | */ |
| 1586 | #else |
| 1587 | |
| 1588 | #define trace_create_maxlat_file(tr, d_tracer) \ |
| 1589 | trace_create_file("tracing_max_latency", 0644, d_tracer, \ |
| 1590 | &tr->max_latency, &tracing_max_lat_fops) |
| 1591 | |
| 1592 | #endif |
Tim Bird | 0e95017 | 2010-02-25 15:36:43 -0800 | [diff] [blame] | 1593 | |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1594 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1595 | /* |
| 1596 | * Copy the new maximum trace into the separate maximum-trace |
| 1597 | * structure. (this way the maximum trace is permanently saved, |
Chunyu Hu | 5a93bae | 2017-10-19 14:32:33 +0800 | [diff] [blame] | 1598 | * for later retrieval via /sys/kernel/tracing/tracing_max_latency) |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1599 | */ |
| 1600 | static void |
| 1601 | __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) |
| 1602 | { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 1603 | struct array_buffer *trace_buf = &tr->array_buffer; |
| 1604 | struct array_buffer *max_buf = &tr->max_buffer; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1605 | struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu); |
| 1606 | struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu); |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1607 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1608 | max_buf->cpu = cpu; |
| 1609 | max_buf->time_start = data->preempt_timestamp; |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1610 | |
Steven Rostedt (Red Hat) | 6d9b3fa | 2014-01-14 11:28:38 -0500 | [diff] [blame] | 1611 | max_data->saved_latency = tr->max_latency; |
Steven Rostedt | 8248ac0 | 2009-09-02 12:27:41 -0400 | [diff] [blame] | 1612 | max_data->critical_start = data->critical_start; |
| 1613 | max_data->critical_end = data->critical_end; |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1614 | |
Tom Zanussi | 85f726a | 2019-03-05 10:12:00 -0600 | [diff] [blame] | 1615 | strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN); |
Steven Rostedt | 8248ac0 | 2009-09-02 12:27:41 -0400 | [diff] [blame] | 1616 | max_data->pid = tsk->pid; |
Steven Rostedt (Red Hat) | f17a519 | 2013-05-30 21:10:37 -0400 | [diff] [blame] | 1617 | /* |
| 1618 | * If tsk == current, then use current_uid(), as that does not use |
| 1619 | * RCU. The irq tracer can be called out of RCU scope. |
| 1620 | */ |
| 1621 | if (tsk == current) |
| 1622 | max_data->uid = current_uid(); |
| 1623 | else |
| 1624 | max_data->uid = task_uid(tsk); |
| 1625 | |
Steven Rostedt | 8248ac0 | 2009-09-02 12:27:41 -0400 | [diff] [blame] | 1626 | max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO; |
| 1627 | max_data->policy = tsk->policy; |
| 1628 | max_data->rt_priority = tsk->rt_priority; |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1629 | |
| 1630 | /* record this tasks comm */ |
| 1631 | tracing_record_cmdline(tsk); |
Viktor Rosendahl (BMW) | 91edde2 | 2019-10-09 00:08:21 +0200 | [diff] [blame] | 1632 | latency_fsnotify(tr); |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1633 | } |
| 1634 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1635 | /** |
| 1636 | * update_max_tr - snapshot all trace buffers from global_trace to max_tr |
| 1637 | * @tr: tracer |
| 1638 | * @tsk: the task with the latency |
| 1639 | * @cpu: The cpu that initiated the trace. |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 1640 | * @cond_data: User data associated with a conditional snapshot |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1641 | * |
| 1642 | * Flip the buffers between the @tr and the max_tr and record information |
| 1643 | * about which task was the cause of this latency. |
| 1644 | */ |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1645 | void |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 1646 | update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu, |
| 1647 | void *cond_data) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1648 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 1649 | if (tr->stop_count) |
Steven Rostedt | b8de7bd1 | 2009-08-31 22:32:27 -0400 | [diff] [blame] | 1650 | return; |
| 1651 | |
Steven Rostedt | 4c11d7a | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1652 | WARN_ON_ONCE(!irqs_disabled()); |
Steven Rostedt | 34600f0 | 2013-01-22 13:35:11 -0500 | [diff] [blame] | 1653 | |
Steven Rostedt (Red Hat) | 45ad21c | 2013-03-05 18:25:02 -0500 | [diff] [blame] | 1654 | if (!tr->allocated_snapshot) { |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 1655 | /* Only the nop tracer should hit this when disabling */ |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 1656 | WARN_ON_ONCE(tr->current_trace != &nop_trace); |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 1657 | return; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 1658 | } |
Steven Rostedt | 34600f0 | 2013-01-22 13:35:11 -0500 | [diff] [blame] | 1659 | |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 1660 | arch_spin_lock(&tr->max_lock); |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1661 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 1662 | /* Inherit the recordable setting from array_buffer */ |
| 1663 | if (ring_buffer_record_is_set_on(tr->array_buffer.buffer)) |
Masami Hiramatsu | 73c8d89 | 2018-07-14 01:28:15 +0900 | [diff] [blame] | 1664 | ring_buffer_record_on(tr->max_buffer.buffer); |
| 1665 | else |
| 1666 | ring_buffer_record_off(tr->max_buffer.buffer); |
| 1667 | |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 1668 | #ifdef CONFIG_TRACER_SNAPSHOT |
| 1669 | if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data)) |
| 1670 | goto out_unlock; |
| 1671 | #endif |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 1672 | swap(tr->array_buffer.buffer, tr->max_buffer.buffer); |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1673 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1674 | __update_max_tr(tr, tsk, cpu); |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 1675 | |
| 1676 | out_unlock: |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 1677 | arch_spin_unlock(&tr->max_lock); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | /** |
| 1681 | * update_max_tr_single - only copy one trace over, and reset the rest |
Jakub Kicinski | c68c9ec | 2019-08-27 22:25:47 -0700 | [diff] [blame] | 1682 | * @tr: tracer |
| 1683 | * @tsk: task with the latency |
| 1684 | * @cpu: the cpu of the buffer to copy. |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1685 | * |
| 1686 | * Flip the trace of a single CPU buffer between the @tr and the max_tr. |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1687 | */ |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 1688 | void |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1689 | update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu) |
| 1690 | { |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1691 | int ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1692 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 1693 | if (tr->stop_count) |
Steven Rostedt | b8de7bd1 | 2009-08-31 22:32:27 -0400 | [diff] [blame] | 1694 | return; |
| 1695 | |
Steven Rostedt | 4c11d7a | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1696 | WARN_ON_ONCE(!irqs_disabled()); |
Steven Rostedt | 6c24499 | 2013-04-29 20:08:14 -0400 | [diff] [blame] | 1697 | if (!tr->allocated_snapshot) { |
Steven Rostedt (Red Hat) | 2930e04 | 2013-03-26 17:33:00 -0400 | [diff] [blame] | 1698 | /* Only the nop tracer should hit this when disabling */ |
Linus Torvalds | 9e8529a | 2013-04-29 13:55:38 -0700 | [diff] [blame] | 1699 | WARN_ON_ONCE(tr->current_trace != &nop_trace); |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 1700 | return; |
Steven Rostedt (Red Hat) | 2930e04 | 2013-03-26 17:33:00 -0400 | [diff] [blame] | 1701 | } |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 1702 | |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 1703 | arch_spin_lock(&tr->max_lock); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1704 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 1705 | ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->array_buffer.buffer, cpu); |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 1706 | |
Steven Rostedt | e8165dbb | 2009-09-03 19:13:05 -0400 | [diff] [blame] | 1707 | if (ret == -EBUSY) { |
| 1708 | /* |
| 1709 | * We failed to swap the buffer due to a commit taking |
| 1710 | * place on this CPU. We fail to record, but we reset |
| 1711 | * the max trace buffer (no one writes directly to it) |
| 1712 | * and flag that it failed. |
| 1713 | */ |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 1714 | trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_, |
Steven Rostedt | e8165dbb | 2009-09-03 19:13:05 -0400 | [diff] [blame] | 1715 | "Failed to swap buffers due to commit in progress\n"); |
| 1716 | } |
| 1717 | |
Steven Rostedt | e8165dbb | 2009-09-03 19:13:05 -0400 | [diff] [blame] | 1718 | WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1719 | |
| 1720 | __update_max_tr(tr, tsk, cpu); |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 1721 | arch_spin_unlock(&tr->max_lock); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1722 | } |
Steven Rostedt | 5d4a9db | 2009-08-27 16:52:21 -0400 | [diff] [blame] | 1723 | #endif /* CONFIG_TRACER_MAX_TRACE */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1724 | |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 1725 | static int wait_on_pipe(struct trace_iterator *iter, int full) |
Steven Rostedt | 0d5c6e1 | 2012-11-01 20:54:21 -0400 | [diff] [blame] | 1726 | { |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 1727 | /* Iterators are static, they should be filled or empty */ |
| 1728 | if (trace_buffer_iter(iter, iter->cpu_file)) |
Steven Rostedt (Red Hat) | 8b8b368 | 2014-06-10 09:46:00 -0400 | [diff] [blame] | 1729 | return 0; |
Steven Rostedt | 0d5c6e1 | 2012-11-01 20:54:21 -0400 | [diff] [blame] | 1730 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 1731 | return ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file, |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 1732 | full); |
Steven Rostedt | 0d5c6e1 | 2012-11-01 20:54:21 -0400 | [diff] [blame] | 1733 | } |
| 1734 | |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1735 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
Steven Rostedt (VMware) | 9afecfb | 2017-03-24 17:59:10 -0400 | [diff] [blame] | 1736 | static bool selftests_can_run; |
| 1737 | |
| 1738 | struct trace_selftests { |
| 1739 | struct list_head list; |
| 1740 | struct tracer *type; |
| 1741 | }; |
| 1742 | |
| 1743 | static LIST_HEAD(postponed_selftests); |
| 1744 | |
| 1745 | static int save_selftest(struct tracer *type) |
| 1746 | { |
| 1747 | struct trace_selftests *selftest; |
| 1748 | |
| 1749 | selftest = kmalloc(sizeof(*selftest), GFP_KERNEL); |
| 1750 | if (!selftest) |
| 1751 | return -ENOMEM; |
| 1752 | |
| 1753 | selftest->type = type; |
| 1754 | list_add(&selftest->list, &postponed_selftests); |
| 1755 | return 0; |
| 1756 | } |
| 1757 | |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1758 | static int run_tracer_selftest(struct tracer *type) |
| 1759 | { |
| 1760 | struct trace_array *tr = &global_trace; |
| 1761 | struct tracer *saved_tracer = tr->current_trace; |
| 1762 | int ret; |
| 1763 | |
| 1764 | if (!type->selftest || tracing_selftest_disabled) |
| 1765 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1766 | |
| 1767 | /* |
Steven Rostedt (VMware) | 9afecfb | 2017-03-24 17:59:10 -0400 | [diff] [blame] | 1768 | * If a tracer registers early in boot up (before scheduling is |
| 1769 | * initialized and such), then do not run its selftests yet. |
| 1770 | * Instead, run it a little later in the boot process. |
| 1771 | */ |
| 1772 | if (!selftests_can_run) |
| 1773 | return save_selftest(type); |
| 1774 | |
| 1775 | /* |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1776 | * Run a selftest on this tracer. |
| 1777 | * Here we reset the trace buffer, and set the current |
| 1778 | * tracer to be this tracer. The tracer can then run some |
| 1779 | * internal tracing to verify that everything is in order. |
| 1780 | * If we fail, we do not register this tracer. |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1781 | */ |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 1782 | tracing_reset_online_cpus(&tr->array_buffer); |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1783 | |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1784 | tr->current_trace = type; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1785 | |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1786 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 1787 | if (type->use_max_tr) { |
| 1788 | /* If we expanded the buffers, make sure the max is expanded too */ |
| 1789 | if (ring_buffer_expanded) |
| 1790 | ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size, |
| 1791 | RING_BUFFER_ALL_CPUS); |
| 1792 | tr->allocated_snapshot = true; |
| 1793 | } |
| 1794 | #endif |
| 1795 | |
| 1796 | /* the test is responsible for initializing and enabling */ |
| 1797 | pr_info("Testing tracer %s: ", type->name); |
| 1798 | ret = type->selftest(type, tr); |
| 1799 | /* the test is responsible for resetting too */ |
| 1800 | tr->current_trace = saved_tracer; |
| 1801 | if (ret) { |
| 1802 | printk(KERN_CONT "FAILED!\n"); |
| 1803 | /* Add the warning after printing 'FAILED' */ |
| 1804 | WARN_ON(1); |
| 1805 | return -1; |
| 1806 | } |
| 1807 | /* Only reset on passing, to avoid touching corrupted buffers */ |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 1808 | tracing_reset_online_cpus(&tr->array_buffer); |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1809 | |
| 1810 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 1811 | if (type->use_max_tr) { |
| 1812 | tr->allocated_snapshot = false; |
| 1813 | |
| 1814 | /* Shrink the max buffer again */ |
| 1815 | if (ring_buffer_expanded) |
| 1816 | ring_buffer_resize(tr->max_buffer.buffer, 1, |
| 1817 | RING_BUFFER_ALL_CPUS); |
| 1818 | } |
| 1819 | #endif |
| 1820 | |
| 1821 | printk(KERN_CONT "PASSED\n"); |
| 1822 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1823 | } |
Steven Rostedt (VMware) | 9afecfb | 2017-03-24 17:59:10 -0400 | [diff] [blame] | 1824 | |
| 1825 | static __init int init_trace_selftests(void) |
| 1826 | { |
| 1827 | struct trace_selftests *p, *n; |
| 1828 | struct tracer *t, **last; |
| 1829 | int ret; |
| 1830 | |
| 1831 | selftests_can_run = true; |
| 1832 | |
| 1833 | mutex_lock(&trace_types_lock); |
| 1834 | |
| 1835 | if (list_empty(&postponed_selftests)) |
| 1836 | goto out; |
| 1837 | |
| 1838 | pr_info("Running postponed tracer tests:\n"); |
| 1839 | |
| 1840 | list_for_each_entry_safe(p, n, &postponed_selftests, list) { |
Anders Roxell | 6fc2171 | 2018-11-30 15:56:22 +0100 | [diff] [blame] | 1841 | /* This loop can take minutes when sanitizers are enabled, so |
| 1842 | * lets make sure we allow RCU processing. |
| 1843 | */ |
| 1844 | cond_resched(); |
Steven Rostedt (VMware) | 9afecfb | 2017-03-24 17:59:10 -0400 | [diff] [blame] | 1845 | ret = run_tracer_selftest(p->type); |
| 1846 | /* If the test fails, then warn and remove from available_tracers */ |
| 1847 | if (ret < 0) { |
| 1848 | WARN(1, "tracer: %s failed selftest, disabling\n", |
| 1849 | p->type->name); |
| 1850 | last = &trace_types; |
| 1851 | for (t = trace_types; t; t = t->next) { |
| 1852 | if (t == p->type) { |
| 1853 | *last = t->next; |
| 1854 | break; |
| 1855 | } |
| 1856 | last = &t->next; |
| 1857 | } |
| 1858 | } |
| 1859 | list_del(&p->list); |
| 1860 | kfree(p); |
| 1861 | } |
| 1862 | |
| 1863 | out: |
| 1864 | mutex_unlock(&trace_types_lock); |
| 1865 | |
| 1866 | return 0; |
| 1867 | } |
Steven Rostedt | b9ef032 | 2017-05-17 11:14:35 -0400 | [diff] [blame] | 1868 | core_initcall(init_trace_selftests); |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1869 | #else |
| 1870 | static inline int run_tracer_selftest(struct tracer *type) |
| 1871 | { |
| 1872 | return 0; |
| 1873 | } |
| 1874 | #endif /* CONFIG_FTRACE_STARTUP_TEST */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1875 | |
Steven Rostedt (Red Hat) | 41d9c0b | 2015-09-29 17:31:55 -0400 | [diff] [blame] | 1876 | static void add_tracer_options(struct trace_array *tr, struct tracer *t); |
| 1877 | |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 1878 | static void __init apply_trace_boot_options(void); |
| 1879 | |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1880 | /** |
| 1881 | * register_tracer - register a tracer with the ftrace system. |
Jakub Kicinski | c68c9ec | 2019-08-27 22:25:47 -0700 | [diff] [blame] | 1882 | * @type: the plugin for the tracer |
Steven Rostedt | 4fcdae8 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 1883 | * |
| 1884 | * Register a new plugin tracer. |
| 1885 | */ |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 1886 | int __init register_tracer(struct tracer *type) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1887 | { |
| 1888 | struct tracer *t; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1889 | int ret = 0; |
| 1890 | |
| 1891 | if (!type->name) { |
| 1892 | pr_info("Tracer must have a name\n"); |
| 1893 | return -1; |
| 1894 | } |
| 1895 | |
Dan Carpenter | 24a461d | 2010-07-10 12:06:44 +0200 | [diff] [blame] | 1896 | if (strlen(type->name) >= MAX_TRACER_SIZE) { |
Li Zefan | ee6c2c1 | 2009-09-18 14:06:47 +0800 | [diff] [blame] | 1897 | pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE); |
| 1898 | return -1; |
| 1899 | } |
| 1900 | |
Steven Rostedt (VMware) | a356646 | 2019-12-02 16:25:27 -0500 | [diff] [blame] | 1901 | if (security_locked_down(LOCKDOWN_TRACEFS)) { |
Stephen Rothwell | ee19545 | 2019-12-06 09:25:03 +1100 | [diff] [blame] | 1902 | pr_warn("Can not register tracer %s due to lockdown\n", |
Steven Rostedt (VMware) | a356646 | 2019-12-02 16:25:27 -0500 | [diff] [blame] | 1903 | type->name); |
| 1904 | return -EPERM; |
| 1905 | } |
| 1906 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1907 | mutex_lock(&trace_types_lock); |
Ingo Molnar | 86fa2f6 | 2008-11-19 10:00:15 +0100 | [diff] [blame] | 1908 | |
Frederic Weisbecker | 8e1b82e | 2008-12-06 03:41:33 +0100 | [diff] [blame] | 1909 | tracing_selftest_running = true; |
| 1910 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1911 | for (t = trace_types; t; t = t->next) { |
| 1912 | if (strcmp(type->name, t->name) == 0) { |
| 1913 | /* already found */ |
Li Zefan | ee6c2c1 | 2009-09-18 14:06:47 +0800 | [diff] [blame] | 1914 | pr_info("Tracer %s already registered\n", |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1915 | type->name); |
| 1916 | ret = -1; |
| 1917 | goto out; |
| 1918 | } |
| 1919 | } |
| 1920 | |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 1921 | if (!type->set_flag) |
| 1922 | type->set_flag = &dummy_set_flag; |
Chunyu Hu | d39cdd2 | 2016-03-08 21:37:01 +0800 | [diff] [blame] | 1923 | if (!type->flags) { |
| 1924 | /*allocate a dummy tracer_flags*/ |
| 1925 | type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL); |
Chunyu Hu | c8ca003 | 2016-03-14 20:35:41 +0800 | [diff] [blame] | 1926 | if (!type->flags) { |
| 1927 | ret = -ENOMEM; |
| 1928 | goto out; |
| 1929 | } |
Chunyu Hu | d39cdd2 | 2016-03-08 21:37:01 +0800 | [diff] [blame] | 1930 | type->flags->val = 0; |
| 1931 | type->flags->opts = dummy_tracer_opt; |
| 1932 | } else |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 1933 | if (!type->flags->opts) |
| 1934 | type->flags->opts = dummy_tracer_opt; |
Frederic Weisbecker | 6eaaa5d | 2009-02-11 02:25:00 +0100 | [diff] [blame] | 1935 | |
Chunyu Hu | d39cdd2 | 2016-03-08 21:37:01 +0800 | [diff] [blame] | 1936 | /* store the tracer for __set_tracer_option */ |
| 1937 | type->flags->trace = type; |
| 1938 | |
Steven Rostedt (Red Hat) | f4e781c | 2013-03-07 11:10:56 -0500 | [diff] [blame] | 1939 | ret = run_tracer_selftest(type); |
| 1940 | if (ret < 0) |
| 1941 | goto out; |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 1942 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1943 | type->next = trace_types; |
| 1944 | trace_types = type; |
Steven Rostedt (Red Hat) | 41d9c0b | 2015-09-29 17:31:55 -0400 | [diff] [blame] | 1945 | add_tracer_options(&global_trace, type); |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 1946 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1947 | out: |
Frederic Weisbecker | 8e1b82e | 2008-12-06 03:41:33 +0100 | [diff] [blame] | 1948 | tracing_selftest_running = false; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1949 | mutex_unlock(&trace_types_lock); |
| 1950 | |
Steven Rostedt | dac7494 | 2009-02-05 01:13:38 -0500 | [diff] [blame] | 1951 | if (ret || !default_bootup_tracer) |
| 1952 | goto out_unlock; |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 1953 | |
Li Zefan | ee6c2c1 | 2009-09-18 14:06:47 +0800 | [diff] [blame] | 1954 | if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE)) |
Steven Rostedt | dac7494 | 2009-02-05 01:13:38 -0500 | [diff] [blame] | 1955 | goto out_unlock; |
| 1956 | |
| 1957 | printk(KERN_INFO "Starting tracer '%s'\n", type->name); |
| 1958 | /* Do we want this tracer to start on bootup? */ |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 1959 | tracing_set_tracer(&global_trace, type->name); |
Steven Rostedt | dac7494 | 2009-02-05 01:13:38 -0500 | [diff] [blame] | 1960 | default_bootup_tracer = NULL; |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 1961 | |
| 1962 | apply_trace_boot_options(); |
| 1963 | |
Steven Rostedt | dac7494 | 2009-02-05 01:13:38 -0500 | [diff] [blame] | 1964 | /* disable other selftests, since this will break it. */ |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 1965 | tracing_selftest_disabled = true; |
Steven Rostedt | dac7494 | 2009-02-05 01:13:38 -0500 | [diff] [blame] | 1966 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
| 1967 | printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n", |
| 1968 | type->name); |
| 1969 | #endif |
| 1970 | |
| 1971 | out_unlock: |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1972 | return ret; |
| 1973 | } |
| 1974 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 1975 | static void tracing_reset_cpu(struct array_buffer *buf, int cpu) |
Steven Rostedt | f633903 | 2009-09-04 12:35:16 -0400 | [diff] [blame] | 1976 | { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1977 | struct trace_buffer *buffer = buf->buffer; |
Steven Rostedt | f633903 | 2009-09-04 12:35:16 -0400 | [diff] [blame] | 1978 | |
Hiraku Toyooka | a541641 | 2012-12-19 16:02:34 +0900 | [diff] [blame] | 1979 | if (!buffer) |
| 1980 | return; |
| 1981 | |
Steven Rostedt | f633903 | 2009-09-04 12:35:16 -0400 | [diff] [blame] | 1982 | ring_buffer_record_disable(buffer); |
| 1983 | |
| 1984 | /* Make sure all commits have finished */ |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 1985 | synchronize_rcu(); |
Steven Rostedt | 6817968 | 2012-05-08 20:57:53 -0400 | [diff] [blame] | 1986 | ring_buffer_reset_cpu(buffer, cpu); |
Steven Rostedt | f633903 | 2009-09-04 12:35:16 -0400 | [diff] [blame] | 1987 | |
| 1988 | ring_buffer_record_enable(buffer); |
| 1989 | } |
| 1990 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 1991 | void tracing_reset_online_cpus(struct array_buffer *buf) |
Pekka J Enberg | 213cc06 | 2008-12-19 12:08:39 +0200 | [diff] [blame] | 1992 | { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1993 | struct trace_buffer *buffer = buf->buffer; |
Pekka J Enberg | 213cc06 | 2008-12-19 12:08:39 +0200 | [diff] [blame] | 1994 | int cpu; |
| 1995 | |
Hiraku Toyooka | a541641 | 2012-12-19 16:02:34 +0900 | [diff] [blame] | 1996 | if (!buffer) |
| 1997 | return; |
| 1998 | |
Steven Rostedt | 621968c | 2009-09-04 12:02:35 -0400 | [diff] [blame] | 1999 | ring_buffer_record_disable(buffer); |
| 2000 | |
| 2001 | /* Make sure all commits have finished */ |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 2002 | synchronize_rcu(); |
Steven Rostedt | 621968c | 2009-09-04 12:02:35 -0400 | [diff] [blame] | 2003 | |
Alexander Z Lam | 9457158 | 2013-08-02 18:36:16 -0700 | [diff] [blame] | 2004 | buf->time_start = buffer_ftrace_now(buf, buf->cpu); |
Pekka J Enberg | 213cc06 | 2008-12-19 12:08:39 +0200 | [diff] [blame] | 2005 | |
| 2006 | for_each_online_cpu(cpu) |
Steven Rostedt | 6817968 | 2012-05-08 20:57:53 -0400 | [diff] [blame] | 2007 | ring_buffer_reset_cpu(buffer, cpu); |
Steven Rostedt | 621968c | 2009-09-04 12:02:35 -0400 | [diff] [blame] | 2008 | |
| 2009 | ring_buffer_record_enable(buffer); |
Pekka J Enberg | 213cc06 | 2008-12-19 12:08:39 +0200 | [diff] [blame] | 2010 | } |
| 2011 | |
Steven Rostedt (Red Hat) | 09d8091 | 2013-07-23 22:21:59 -0400 | [diff] [blame] | 2012 | /* Must have trace_types_lock held */ |
Steven Rostedt (Red Hat) | 873c642 | 2013-03-04 23:26:06 -0500 | [diff] [blame] | 2013 | void tracing_reset_all_online_cpus(void) |
Steven Rostedt | 9456f0f | 2009-05-06 21:54:09 -0400 | [diff] [blame] | 2014 | { |
Steven Rostedt (Red Hat) | 873c642 | 2013-03-04 23:26:06 -0500 | [diff] [blame] | 2015 | struct trace_array *tr; |
| 2016 | |
Steven Rostedt (Red Hat) | 873c642 | 2013-03-04 23:26:06 -0500 | [diff] [blame] | 2017 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
Steven Rostedt (VMware) | 065e63f | 2017-08-31 17:03:47 -0400 | [diff] [blame] | 2018 | if (!tr->clear_trace) |
| 2019 | continue; |
| 2020 | tr->clear_trace = false; |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 2021 | tracing_reset_online_cpus(&tr->array_buffer); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2022 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 2023 | tracing_reset_online_cpus(&tr->max_buffer); |
| 2024 | #endif |
Steven Rostedt (Red Hat) | 873c642 | 2013-03-04 23:26:06 -0500 | [diff] [blame] | 2025 | } |
Steven Rostedt | 9456f0f | 2009-05-06 21:54:09 -0400 | [diff] [blame] | 2026 | } |
| 2027 | |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 2028 | static int *tgid_map; |
| 2029 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2030 | #define SAVED_CMDLINES_DEFAULT 128 |
Thomas Gleixner | 2c7eea4 | 2009-03-18 09:03:19 +0100 | [diff] [blame] | 2031 | #define NO_CMDLINE_MAP UINT_MAX |
Thomas Gleixner | edc35bd | 2009-12-03 12:38:57 +0100 | [diff] [blame] | 2032 | static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED; |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2033 | struct saved_cmdlines_buffer { |
| 2034 | unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1]; |
| 2035 | unsigned *map_cmdline_to_pid; |
| 2036 | unsigned cmdline_num; |
| 2037 | int cmdline_idx; |
| 2038 | char *saved_cmdlines; |
| 2039 | }; |
| 2040 | static struct saved_cmdlines_buffer *savedcmd; |
Steven Rostedt | 25b0b44 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 2041 | |
Steven Rostedt | 25b0b44 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 2042 | /* temporary disable recording */ |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 2043 | static atomic_t trace_record_taskinfo_disabled __read_mostly; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2044 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2045 | static inline char *get_saved_cmdlines(int idx) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2046 | { |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2047 | return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN]; |
| 2048 | } |
| 2049 | |
| 2050 | static inline void set_cmdline(int idx, const char *cmdline) |
| 2051 | { |
Tom Zanussi | 85f726a | 2019-03-05 10:12:00 -0600 | [diff] [blame] | 2052 | strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN); |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | static int allocate_cmdlines_buffer(unsigned int val, |
| 2056 | struct saved_cmdlines_buffer *s) |
| 2057 | { |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 2058 | s->map_cmdline_to_pid = kmalloc_array(val, |
| 2059 | sizeof(*s->map_cmdline_to_pid), |
| 2060 | GFP_KERNEL); |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2061 | if (!s->map_cmdline_to_pid) |
| 2062 | return -ENOMEM; |
| 2063 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 2064 | s->saved_cmdlines = kmalloc_array(TASK_COMM_LEN, val, GFP_KERNEL); |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2065 | if (!s->saved_cmdlines) { |
| 2066 | kfree(s->map_cmdline_to_pid); |
| 2067 | return -ENOMEM; |
| 2068 | } |
| 2069 | |
| 2070 | s->cmdline_idx = 0; |
| 2071 | s->cmdline_num = val; |
| 2072 | memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP, |
| 2073 | sizeof(s->map_pid_to_cmdline)); |
| 2074 | memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP, |
| 2075 | val * sizeof(*s->map_cmdline_to_pid)); |
| 2076 | |
| 2077 | return 0; |
| 2078 | } |
| 2079 | |
| 2080 | static int trace_create_savedcmd(void) |
| 2081 | { |
| 2082 | int ret; |
| 2083 | |
Namhyung Kim | a6af8fb | 2014-06-10 16:11:35 +0900 | [diff] [blame] | 2084 | savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL); |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2085 | if (!savedcmd) |
| 2086 | return -ENOMEM; |
| 2087 | |
| 2088 | ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd); |
| 2089 | if (ret < 0) { |
| 2090 | kfree(savedcmd); |
| 2091 | savedcmd = NULL; |
| 2092 | return -ENOMEM; |
| 2093 | } |
| 2094 | |
| 2095 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2096 | } |
| 2097 | |
Carsten Emde | b5130b1 | 2009-09-13 01:43:07 +0200 | [diff] [blame] | 2098 | int is_tracing_stopped(void) |
| 2099 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 2100 | return global_trace.stop_count; |
Carsten Emde | b5130b1 | 2009-09-13 01:43:07 +0200 | [diff] [blame] | 2101 | } |
| 2102 | |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2103 | /** |
| 2104 | * tracing_start - quick start of the tracer |
| 2105 | * |
| 2106 | * If tracing is enabled but was stopped by tracing_stop, |
| 2107 | * this will start the tracer back up. |
| 2108 | */ |
| 2109 | void tracing_start(void) |
| 2110 | { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2111 | struct trace_buffer *buffer; |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2112 | unsigned long flags; |
| 2113 | |
| 2114 | if (tracing_disabled) |
| 2115 | return; |
| 2116 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 2117 | raw_spin_lock_irqsave(&global_trace.start_lock, flags); |
| 2118 | if (--global_trace.stop_count) { |
| 2119 | if (global_trace.stop_count < 0) { |
Steven Rostedt | b06a830 | 2009-01-22 14:26:15 -0500 | [diff] [blame] | 2120 | /* Someone screwed up their debugging */ |
| 2121 | WARN_ON_ONCE(1); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 2122 | global_trace.stop_count = 0; |
Steven Rostedt | b06a830 | 2009-01-22 14:26:15 -0500 | [diff] [blame] | 2123 | } |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2124 | goto out; |
| 2125 | } |
| 2126 | |
Steven Rostedt | a2f8071 | 2010-03-12 19:56:00 -0500 | [diff] [blame] | 2127 | /* Prevent the buffers from switching */ |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 2128 | arch_spin_lock(&global_trace.max_lock); |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2129 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 2130 | buffer = global_trace.array_buffer.buffer; |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2131 | if (buffer) |
| 2132 | ring_buffer_record_enable(buffer); |
| 2133 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2134 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 2135 | buffer = global_trace.max_buffer.buffer; |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2136 | if (buffer) |
| 2137 | ring_buffer_record_enable(buffer); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2138 | #endif |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2139 | |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 2140 | arch_spin_unlock(&global_trace.max_lock); |
Steven Rostedt | a2f8071 | 2010-03-12 19:56:00 -0500 | [diff] [blame] | 2141 | |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2142 | out: |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 2143 | raw_spin_unlock_irqrestore(&global_trace.start_lock, flags); |
| 2144 | } |
| 2145 | |
| 2146 | static void tracing_start_tr(struct trace_array *tr) |
| 2147 | { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2148 | struct trace_buffer *buffer; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 2149 | unsigned long flags; |
| 2150 | |
| 2151 | if (tracing_disabled) |
| 2152 | return; |
| 2153 | |
| 2154 | /* If global, we need to also start the max tracer */ |
| 2155 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL) |
| 2156 | return tracing_start(); |
| 2157 | |
| 2158 | raw_spin_lock_irqsave(&tr->start_lock, flags); |
| 2159 | |
| 2160 | if (--tr->stop_count) { |
| 2161 | if (tr->stop_count < 0) { |
| 2162 | /* Someone screwed up their debugging */ |
| 2163 | WARN_ON_ONCE(1); |
| 2164 | tr->stop_count = 0; |
| 2165 | } |
| 2166 | goto out; |
| 2167 | } |
| 2168 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 2169 | buffer = tr->array_buffer.buffer; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 2170 | if (buffer) |
| 2171 | ring_buffer_record_enable(buffer); |
| 2172 | |
| 2173 | out: |
| 2174 | raw_spin_unlock_irqrestore(&tr->start_lock, flags); |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | /** |
| 2178 | * tracing_stop - quick stop of the tracer |
| 2179 | * |
| 2180 | * Light weight way to stop tracing. Use in conjunction with |
| 2181 | * tracing_start. |
| 2182 | */ |
| 2183 | void tracing_stop(void) |
| 2184 | { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2185 | struct trace_buffer *buffer; |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2186 | unsigned long flags; |
| 2187 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 2188 | raw_spin_lock_irqsave(&global_trace.start_lock, flags); |
| 2189 | if (global_trace.stop_count++) |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2190 | goto out; |
| 2191 | |
Steven Rostedt | a2f8071 | 2010-03-12 19:56:00 -0500 | [diff] [blame] | 2192 | /* Prevent the buffers from switching */ |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 2193 | arch_spin_lock(&global_trace.max_lock); |
Steven Rostedt | a2f8071 | 2010-03-12 19:56:00 -0500 | [diff] [blame] | 2194 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 2195 | buffer = global_trace.array_buffer.buffer; |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2196 | if (buffer) |
| 2197 | ring_buffer_record_disable(buffer); |
| 2198 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2199 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 2200 | buffer = global_trace.max_buffer.buffer; |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2201 | if (buffer) |
| 2202 | ring_buffer_record_disable(buffer); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 2203 | #endif |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2204 | |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 2205 | arch_spin_unlock(&global_trace.max_lock); |
Steven Rostedt | a2f8071 | 2010-03-12 19:56:00 -0500 | [diff] [blame] | 2206 | |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2207 | out: |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 2208 | raw_spin_unlock_irqrestore(&global_trace.start_lock, flags); |
| 2209 | } |
| 2210 | |
| 2211 | static void tracing_stop_tr(struct trace_array *tr) |
| 2212 | { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2213 | struct trace_buffer *buffer; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 2214 | unsigned long flags; |
| 2215 | |
| 2216 | /* If global, we need to also stop the max tracer */ |
| 2217 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL) |
| 2218 | return tracing_stop(); |
| 2219 | |
| 2220 | raw_spin_lock_irqsave(&tr->start_lock, flags); |
| 2221 | if (tr->stop_count++) |
| 2222 | goto out; |
| 2223 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 2224 | buffer = tr->array_buffer.buffer; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 2225 | if (buffer) |
| 2226 | ring_buffer_record_disable(buffer); |
| 2227 | |
| 2228 | out: |
| 2229 | raw_spin_unlock_irqrestore(&tr->start_lock, flags); |
Steven Rostedt | 0f04870 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 2230 | } |
| 2231 | |
Steven Rostedt (Red Hat) | 379cfda | 2014-05-30 09:42:39 -0400 | [diff] [blame] | 2232 | static int trace_save_cmdline(struct task_struct *tsk) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2233 | { |
Carsten Emde | a635cf0 | 2009-03-18 09:00:41 +0100 | [diff] [blame] | 2234 | unsigned pid, idx; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2235 | |
Joel Fernandes | eaf260a | 2017-07-06 16:00:21 -0700 | [diff] [blame] | 2236 | /* treat recording of idle task as a success */ |
| 2237 | if (!tsk->pid) |
| 2238 | return 1; |
| 2239 | |
| 2240 | if (unlikely(tsk->pid > PID_MAX_DEFAULT)) |
Steven Rostedt (Red Hat) | 379cfda | 2014-05-30 09:42:39 -0400 | [diff] [blame] | 2241 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2242 | |
| 2243 | /* |
| 2244 | * It's not the end of the world if we don't get |
| 2245 | * the lock, but we also don't want to spin |
| 2246 | * nor do we want to disable interrupts, |
| 2247 | * so if we miss here, then better luck next time. |
| 2248 | */ |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 2249 | if (!arch_spin_trylock(&trace_cmdline_lock)) |
Steven Rostedt (Red Hat) | 379cfda | 2014-05-30 09:42:39 -0400 | [diff] [blame] | 2250 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2251 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2252 | idx = savedcmd->map_pid_to_cmdline[tsk->pid]; |
Thomas Gleixner | 2c7eea4 | 2009-03-18 09:03:19 +0100 | [diff] [blame] | 2253 | if (idx == NO_CMDLINE_MAP) { |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2254 | idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2255 | |
Carsten Emde | a635cf0 | 2009-03-18 09:00:41 +0100 | [diff] [blame] | 2256 | /* |
| 2257 | * Check whether the cmdline buffer at idx has a pid |
| 2258 | * mapped. We are going to overwrite that entry so we |
| 2259 | * need to clear the map_pid_to_cmdline. Otherwise we |
| 2260 | * would read the new comm for the old pid. |
| 2261 | */ |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2262 | pid = savedcmd->map_cmdline_to_pid[idx]; |
Carsten Emde | a635cf0 | 2009-03-18 09:00:41 +0100 | [diff] [blame] | 2263 | if (pid != NO_CMDLINE_MAP) |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2264 | savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2265 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2266 | savedcmd->map_cmdline_to_pid[idx] = tsk->pid; |
| 2267 | savedcmd->map_pid_to_cmdline[tsk->pid] = idx; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2268 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2269 | savedcmd->cmdline_idx = idx; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2270 | } |
| 2271 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2272 | set_cmdline(idx, tsk->comm); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2273 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 2274 | arch_spin_unlock(&trace_cmdline_lock); |
Steven Rostedt (Red Hat) | 379cfda | 2014-05-30 09:42:39 -0400 | [diff] [blame] | 2275 | |
| 2276 | return 1; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2277 | } |
| 2278 | |
Steven Rostedt (Red Hat) | 4c27e75 | 2014-05-30 10:49:46 -0400 | [diff] [blame] | 2279 | static void __trace_find_cmdline(int pid, char comm[]) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2280 | { |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2281 | unsigned map; |
| 2282 | |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 2283 | if (!pid) { |
| 2284 | strcpy(comm, "<idle>"); |
| 2285 | return; |
| 2286 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2287 | |
Steven Rostedt | 74bf407 | 2010-01-25 15:11:53 -0500 | [diff] [blame] | 2288 | if (WARN_ON_ONCE(pid < 0)) { |
| 2289 | strcpy(comm, "<XXX>"); |
| 2290 | return; |
| 2291 | } |
| 2292 | |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 2293 | if (pid > PID_MAX_DEFAULT) { |
| 2294 | strcpy(comm, "<...>"); |
| 2295 | return; |
| 2296 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2297 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 2298 | map = savedcmd->map_pid_to_cmdline[pid]; |
Thomas Gleixner | 50d8875 | 2009-03-18 08:58:44 +0100 | [diff] [blame] | 2299 | if (map != NO_CMDLINE_MAP) |
Amey Telawane | e09e286 | 2017-05-03 15:41:14 +0530 | [diff] [blame] | 2300 | strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN); |
Thomas Gleixner | 50d8875 | 2009-03-18 08:58:44 +0100 | [diff] [blame] | 2301 | else |
| 2302 | strcpy(comm, "<...>"); |
Steven Rostedt (Red Hat) | 4c27e75 | 2014-05-30 10:49:46 -0400 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | void trace_find_cmdline(int pid, char comm[]) |
| 2306 | { |
| 2307 | preempt_disable(); |
| 2308 | arch_spin_lock(&trace_cmdline_lock); |
| 2309 | |
| 2310 | __trace_find_cmdline(pid, comm); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2311 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 2312 | arch_spin_unlock(&trace_cmdline_lock); |
Heiko Carstens | 5b6045a | 2009-05-26 17:28:02 +0200 | [diff] [blame] | 2313 | preempt_enable(); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2314 | } |
| 2315 | |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 2316 | int trace_find_tgid(int pid) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2317 | { |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 2318 | if (unlikely(!tgid_map || !pid || pid > PID_MAX_DEFAULT)) |
| 2319 | return 0; |
| 2320 | |
| 2321 | return tgid_map[pid]; |
| 2322 | } |
| 2323 | |
| 2324 | static int trace_save_tgid(struct task_struct *tsk) |
| 2325 | { |
Joel Fernandes | bd45d34 | 2017-07-06 16:00:22 -0700 | [diff] [blame] | 2326 | /* treat recording of idle task as a success */ |
| 2327 | if (!tsk->pid) |
| 2328 | return 1; |
| 2329 | |
| 2330 | if (unlikely(!tgid_map || tsk->pid > PID_MAX_DEFAULT)) |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 2331 | return 0; |
| 2332 | |
| 2333 | tgid_map[tsk->pid] = tsk->tgid; |
| 2334 | return 1; |
| 2335 | } |
| 2336 | |
| 2337 | static bool tracing_record_taskinfo_skip(int flags) |
| 2338 | { |
| 2339 | if (unlikely(!(flags & (TRACE_RECORD_CMDLINE | TRACE_RECORD_TGID)))) |
| 2340 | return true; |
| 2341 | if (atomic_read(&trace_record_taskinfo_disabled) || !tracing_is_on()) |
| 2342 | return true; |
| 2343 | if (!__this_cpu_read(trace_taskinfo_save)) |
| 2344 | return true; |
| 2345 | return false; |
| 2346 | } |
| 2347 | |
| 2348 | /** |
| 2349 | * tracing_record_taskinfo - record the task info of a task |
| 2350 | * |
Jakub Kicinski | c68c9ec | 2019-08-27 22:25:47 -0700 | [diff] [blame] | 2351 | * @task: task to record |
| 2352 | * @flags: TRACE_RECORD_CMDLINE for recording comm |
| 2353 | * TRACE_RECORD_TGID for recording tgid |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 2354 | */ |
| 2355 | void tracing_record_taskinfo(struct task_struct *task, int flags) |
| 2356 | { |
Joel Fernandes | 29b1a8a | 2017-07-06 16:00:23 -0700 | [diff] [blame] | 2357 | bool done; |
| 2358 | |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 2359 | if (tracing_record_taskinfo_skip(flags)) |
| 2360 | return; |
Joel Fernandes | 29b1a8a | 2017-07-06 16:00:23 -0700 | [diff] [blame] | 2361 | |
| 2362 | /* |
| 2363 | * Record as much task information as possible. If some fail, continue |
| 2364 | * to try to record the others. |
| 2365 | */ |
| 2366 | done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(task); |
| 2367 | done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(task); |
| 2368 | |
| 2369 | /* If recording any information failed, retry again soon. */ |
| 2370 | if (!done) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2371 | return; |
| 2372 | |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 2373 | __this_cpu_write(trace_taskinfo_save, false); |
| 2374 | } |
| 2375 | |
| 2376 | /** |
| 2377 | * tracing_record_taskinfo_sched_switch - record task info for sched_switch |
| 2378 | * |
Jakub Kicinski | c68c9ec | 2019-08-27 22:25:47 -0700 | [diff] [blame] | 2379 | * @prev: previous task during sched_switch |
| 2380 | * @next: next task during sched_switch |
| 2381 | * @flags: TRACE_RECORD_CMDLINE for recording comm |
| 2382 | * TRACE_RECORD_TGID for recording tgid |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 2383 | */ |
| 2384 | void tracing_record_taskinfo_sched_switch(struct task_struct *prev, |
| 2385 | struct task_struct *next, int flags) |
| 2386 | { |
Joel Fernandes | 29b1a8a | 2017-07-06 16:00:23 -0700 | [diff] [blame] | 2387 | bool done; |
| 2388 | |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 2389 | if (tracing_record_taskinfo_skip(flags)) |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 2390 | return; |
| 2391 | |
Joel Fernandes | 29b1a8a | 2017-07-06 16:00:23 -0700 | [diff] [blame] | 2392 | /* |
| 2393 | * Record as much task information as possible. If some fail, continue |
| 2394 | * to try to record the others. |
| 2395 | */ |
| 2396 | done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(prev); |
| 2397 | done &= !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(next); |
| 2398 | done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(prev); |
| 2399 | done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(next); |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 2400 | |
Joel Fernandes | 29b1a8a | 2017-07-06 16:00:23 -0700 | [diff] [blame] | 2401 | /* If recording any information failed, retry again soon. */ |
| 2402 | if (!done) |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 2403 | return; |
| 2404 | |
| 2405 | __this_cpu_write(trace_taskinfo_save, false); |
| 2406 | } |
| 2407 | |
| 2408 | /* Helpers to record a specific task information */ |
| 2409 | void tracing_record_cmdline(struct task_struct *task) |
| 2410 | { |
| 2411 | tracing_record_taskinfo(task, TRACE_RECORD_CMDLINE); |
| 2412 | } |
| 2413 | |
| 2414 | void tracing_record_tgid(struct task_struct *task) |
| 2415 | { |
| 2416 | tracing_record_taskinfo(task, TRACE_RECORD_TGID); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2417 | } |
| 2418 | |
Steven Rostedt (VMware) | af0009f | 2017-03-16 11:01:06 -0400 | [diff] [blame] | 2419 | /* |
| 2420 | * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq |
| 2421 | * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function |
| 2422 | * simplifies those functions and keeps them in sync. |
| 2423 | */ |
| 2424 | enum print_line_t trace_handle_return(struct trace_seq *s) |
| 2425 | { |
| 2426 | return trace_seq_has_overflowed(s) ? |
| 2427 | TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED; |
| 2428 | } |
| 2429 | EXPORT_SYMBOL_GPL(trace_handle_return); |
| 2430 | |
Pekka Paalanen | 45dcd8b | 2008-09-16 21:56:41 +0300 | [diff] [blame] | 2431 | void |
Cong Wang | 46710f3 | 2019-05-25 09:57:59 -0700 | [diff] [blame] | 2432 | tracing_generic_entry_update(struct trace_entry *entry, unsigned short type, |
| 2433 | unsigned long flags, int pc) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2434 | { |
| 2435 | struct task_struct *tsk = current; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2436 | |
Steven Rostedt | 777e208 | 2008-09-29 23:02:42 -0400 | [diff] [blame] | 2437 | entry->preempt_count = pc & 0xff; |
| 2438 | entry->pid = (tsk) ? tsk->pid : 0; |
Cong Wang | 46710f3 | 2019-05-25 09:57:59 -0700 | [diff] [blame] | 2439 | entry->type = type; |
Steven Rostedt | 777e208 | 2008-09-29 23:02:42 -0400 | [diff] [blame] | 2440 | entry->flags = |
Steven Rostedt | 9244489 | 2008-10-24 09:42:59 -0400 | [diff] [blame] | 2441 | #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT |
Steven Rostedt | 2e2ca15 | 2008-08-01 12:26:40 -0400 | [diff] [blame] | 2442 | (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) | |
Steven Rostedt | 9244489 | 2008-10-24 09:42:59 -0400 | [diff] [blame] | 2443 | #else |
| 2444 | TRACE_FLAG_IRQS_NOSUPPORT | |
| 2445 | #endif |
Peter Zijlstra | 7e6867b | 2016-03-18 16:28:04 +0100 | [diff] [blame] | 2446 | ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2447 | ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) | |
Pavankumar Kondeti | c59f29c | 2016-12-09 21:50:17 +0530 | [diff] [blame] | 2448 | ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0) | |
Peter Zijlstra | e5137b5 | 2013-10-04 17:28:26 +0200 | [diff] [blame] | 2449 | (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) | |
| 2450 | (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2451 | } |
Frederic Weisbecker | f413cdb | 2009-08-07 01:25:54 +0200 | [diff] [blame] | 2452 | EXPORT_SYMBOL_GPL(tracing_generic_entry_update); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2453 | |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 2454 | struct ring_buffer_event * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2455 | trace_buffer_lock_reserve(struct trace_buffer *buffer, |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 2456 | int type, |
| 2457 | unsigned long len, |
| 2458 | unsigned long flags, int pc) |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 2459 | { |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 2460 | return __trace_buffer_lock_reserve(buffer, type, len, flags, pc); |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 2461 | } |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 2462 | |
Steven Rostedt (Red Hat) | 0fc1b09 | 2016-05-03 17:15:43 -0400 | [diff] [blame] | 2463 | DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event); |
| 2464 | DEFINE_PER_CPU(int, trace_buffered_event_cnt); |
| 2465 | static int trace_buffered_event_ref; |
| 2466 | |
| 2467 | /** |
| 2468 | * trace_buffered_event_enable - enable buffering events |
| 2469 | * |
| 2470 | * When events are being filtered, it is quicker to use a temporary |
| 2471 | * buffer to write the event data into if there's a likely chance |
| 2472 | * that it will not be committed. The discard of the ring buffer |
| 2473 | * is not as fast as committing, and is much slower than copying |
| 2474 | * a commit. |
| 2475 | * |
| 2476 | * When an event is to be filtered, allocate per cpu buffers to |
| 2477 | * write the event data into, and if the event is filtered and discarded |
| 2478 | * it is simply dropped, otherwise, the entire data is to be committed |
| 2479 | * in one shot. |
| 2480 | */ |
| 2481 | void trace_buffered_event_enable(void) |
| 2482 | { |
| 2483 | struct ring_buffer_event *event; |
| 2484 | struct page *page; |
| 2485 | int cpu; |
| 2486 | |
| 2487 | WARN_ON_ONCE(!mutex_is_locked(&event_mutex)); |
| 2488 | |
| 2489 | if (trace_buffered_event_ref++) |
| 2490 | return; |
| 2491 | |
| 2492 | for_each_tracing_cpu(cpu) { |
| 2493 | page = alloc_pages_node(cpu_to_node(cpu), |
| 2494 | GFP_KERNEL | __GFP_NORETRY, 0); |
| 2495 | if (!page) |
| 2496 | goto failed; |
| 2497 | |
| 2498 | event = page_address(page); |
| 2499 | memset(event, 0, sizeof(*event)); |
| 2500 | |
| 2501 | per_cpu(trace_buffered_event, cpu) = event; |
| 2502 | |
| 2503 | preempt_disable(); |
| 2504 | if (cpu == smp_processor_id() && |
| 2505 | this_cpu_read(trace_buffered_event) != |
| 2506 | per_cpu(trace_buffered_event, cpu)) |
| 2507 | WARN_ON_ONCE(1); |
| 2508 | preempt_enable(); |
| 2509 | } |
| 2510 | |
| 2511 | return; |
| 2512 | failed: |
| 2513 | trace_buffered_event_disable(); |
| 2514 | } |
| 2515 | |
| 2516 | static void enable_trace_buffered_event(void *data) |
| 2517 | { |
| 2518 | /* Probably not needed, but do it anyway */ |
| 2519 | smp_rmb(); |
| 2520 | this_cpu_dec(trace_buffered_event_cnt); |
| 2521 | } |
| 2522 | |
| 2523 | static void disable_trace_buffered_event(void *data) |
| 2524 | { |
| 2525 | this_cpu_inc(trace_buffered_event_cnt); |
| 2526 | } |
| 2527 | |
| 2528 | /** |
| 2529 | * trace_buffered_event_disable - disable buffering events |
| 2530 | * |
| 2531 | * When a filter is removed, it is faster to not use the buffered |
| 2532 | * events, and to commit directly into the ring buffer. Free up |
| 2533 | * the temp buffers when there are no more users. This requires |
| 2534 | * special synchronization with current events. |
| 2535 | */ |
| 2536 | void trace_buffered_event_disable(void) |
| 2537 | { |
| 2538 | int cpu; |
| 2539 | |
| 2540 | WARN_ON_ONCE(!mutex_is_locked(&event_mutex)); |
| 2541 | |
| 2542 | if (WARN_ON_ONCE(!trace_buffered_event_ref)) |
| 2543 | return; |
| 2544 | |
| 2545 | if (--trace_buffered_event_ref) |
| 2546 | return; |
| 2547 | |
| 2548 | preempt_disable(); |
| 2549 | /* For each CPU, set the buffer as used. */ |
| 2550 | smp_call_function_many(tracing_buffer_mask, |
| 2551 | disable_trace_buffered_event, NULL, 1); |
| 2552 | preempt_enable(); |
| 2553 | |
| 2554 | /* Wait for all current users to finish */ |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 2555 | synchronize_rcu(); |
Steven Rostedt (Red Hat) | 0fc1b09 | 2016-05-03 17:15:43 -0400 | [diff] [blame] | 2556 | |
| 2557 | for_each_tracing_cpu(cpu) { |
| 2558 | free_page((unsigned long)per_cpu(trace_buffered_event, cpu)); |
| 2559 | per_cpu(trace_buffered_event, cpu) = NULL; |
| 2560 | } |
| 2561 | /* |
| 2562 | * Make sure trace_buffered_event is NULL before clearing |
| 2563 | * trace_buffered_event_cnt. |
| 2564 | */ |
| 2565 | smp_wmb(); |
| 2566 | |
| 2567 | preempt_disable(); |
| 2568 | /* Do the work on each cpu */ |
| 2569 | smp_call_function_many(tracing_buffer_mask, |
| 2570 | enable_trace_buffered_event, NULL, 1); |
| 2571 | preempt_enable(); |
| 2572 | } |
| 2573 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2574 | static struct trace_buffer *temp_buffer; |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 2575 | |
Steven Rostedt | ef5580d | 2009-02-27 19:38:04 -0500 | [diff] [blame] | 2576 | struct ring_buffer_event * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2577 | trace_event_buffer_lock_reserve(struct trace_buffer **current_rb, |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 2578 | struct trace_event_file *trace_file, |
Steven Rostedt | ccb469a | 2012-08-02 10:32:10 -0400 | [diff] [blame] | 2579 | int type, unsigned long len, |
| 2580 | unsigned long flags, int pc) |
| 2581 | { |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 2582 | struct ring_buffer_event *entry; |
Steven Rostedt (Red Hat) | 0fc1b09 | 2016-05-03 17:15:43 -0400 | [diff] [blame] | 2583 | int val; |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 2584 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 2585 | *current_rb = trace_file->tr->array_buffer.buffer; |
Steven Rostedt (Red Hat) | 0fc1b09 | 2016-05-03 17:15:43 -0400 | [diff] [blame] | 2586 | |
Tom Zanussi | 00b4145 | 2018-01-15 20:51:39 -0600 | [diff] [blame] | 2587 | if (!ring_buffer_time_stamp_abs(*current_rb) && (trace_file->flags & |
Steven Rostedt (Red Hat) | 0fc1b09 | 2016-05-03 17:15:43 -0400 | [diff] [blame] | 2588 | (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) && |
| 2589 | (entry = this_cpu_read(trace_buffered_event))) { |
| 2590 | /* Try to use the per cpu buffer first */ |
| 2591 | val = this_cpu_inc_return(trace_buffered_event_cnt); |
| 2592 | if (val == 1) { |
| 2593 | trace_event_setup(entry, type, flags, pc); |
| 2594 | entry->array[0] = len; |
| 2595 | return entry; |
| 2596 | } |
| 2597 | this_cpu_dec(trace_buffered_event_cnt); |
| 2598 | } |
| 2599 | |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 2600 | entry = __trace_buffer_lock_reserve(*current_rb, |
| 2601 | type, len, flags, pc); |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 2602 | /* |
| 2603 | * If tracing is off, but we have triggers enabled |
| 2604 | * we still need to look at the event data. Use the temp_buffer |
| 2605 | * to store the trace event for the tigger to use. It's recusive |
| 2606 | * safe and will not be recorded anywhere. |
| 2607 | */ |
Steven Rostedt (Red Hat) | 5d6ad96 | 2015-05-13 15:12:33 -0400 | [diff] [blame] | 2608 | if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) { |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 2609 | *current_rb = temp_buffer; |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 2610 | entry = __trace_buffer_lock_reserve(*current_rb, |
| 2611 | type, len, flags, pc); |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 2612 | } |
| 2613 | return entry; |
Steven Rostedt | ccb469a | 2012-08-02 10:32:10 -0400 | [diff] [blame] | 2614 | } |
| 2615 | EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve); |
| 2616 | |
Steven Rostedt (Red Hat) | 42391745 | 2016-11-23 15:52:45 -0500 | [diff] [blame] | 2617 | static DEFINE_SPINLOCK(tracepoint_iter_lock); |
| 2618 | static DEFINE_MUTEX(tracepoint_printk_mutex); |
| 2619 | |
| 2620 | static void output_printk(struct trace_event_buffer *fbuffer) |
| 2621 | { |
| 2622 | struct trace_event_call *event_call; |
Masami Hiramatsu | d8d0c24 | 2020-01-11 01:05:18 +0900 | [diff] [blame] | 2623 | struct trace_event_file *file; |
Steven Rostedt (Red Hat) | 42391745 | 2016-11-23 15:52:45 -0500 | [diff] [blame] | 2624 | struct trace_event *event; |
| 2625 | unsigned long flags; |
| 2626 | struct trace_iterator *iter = tracepoint_print_iter; |
| 2627 | |
| 2628 | /* We should never get here if iter is NULL */ |
| 2629 | if (WARN_ON_ONCE(!iter)) |
| 2630 | return; |
| 2631 | |
| 2632 | event_call = fbuffer->trace_file->event_call; |
| 2633 | if (!event_call || !event_call->event.funcs || |
| 2634 | !event_call->event.funcs->trace) |
| 2635 | return; |
| 2636 | |
Masami Hiramatsu | d8d0c24 | 2020-01-11 01:05:18 +0900 | [diff] [blame] | 2637 | file = fbuffer->trace_file; |
| 2638 | if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) || |
| 2639 | (unlikely(file->flags & EVENT_FILE_FL_FILTERED) && |
| 2640 | !filter_match_preds(file->filter, fbuffer->entry))) |
| 2641 | return; |
| 2642 | |
Steven Rostedt (Red Hat) | 42391745 | 2016-11-23 15:52:45 -0500 | [diff] [blame] | 2643 | event = &fbuffer->trace_file->event_call->event; |
| 2644 | |
| 2645 | spin_lock_irqsave(&tracepoint_iter_lock, flags); |
| 2646 | trace_seq_init(&iter->seq); |
| 2647 | iter->ent = fbuffer->entry; |
| 2648 | event_call->event.funcs->trace(iter, 0, event); |
| 2649 | trace_seq_putc(&iter->seq, 0); |
| 2650 | printk("%s", iter->seq.buffer); |
| 2651 | |
| 2652 | spin_unlock_irqrestore(&tracepoint_iter_lock, flags); |
| 2653 | } |
| 2654 | |
| 2655 | int tracepoint_printk_sysctl(struct ctl_table *table, int write, |
| 2656 | void __user *buffer, size_t *lenp, |
| 2657 | loff_t *ppos) |
| 2658 | { |
| 2659 | int save_tracepoint_printk; |
| 2660 | int ret; |
| 2661 | |
| 2662 | mutex_lock(&tracepoint_printk_mutex); |
| 2663 | save_tracepoint_printk = tracepoint_printk; |
| 2664 | |
| 2665 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
| 2666 | |
| 2667 | /* |
| 2668 | * This will force exiting early, as tracepoint_printk |
| 2669 | * is always zero when tracepoint_printk_iter is not allocated |
| 2670 | */ |
| 2671 | if (!tracepoint_print_iter) |
| 2672 | tracepoint_printk = 0; |
| 2673 | |
| 2674 | if (save_tracepoint_printk == tracepoint_printk) |
| 2675 | goto out; |
| 2676 | |
| 2677 | if (tracepoint_printk) |
| 2678 | static_key_enable(&tracepoint_printk_key.key); |
| 2679 | else |
| 2680 | static_key_disable(&tracepoint_printk_key.key); |
| 2681 | |
| 2682 | out: |
| 2683 | mutex_unlock(&tracepoint_printk_mutex); |
| 2684 | |
| 2685 | return ret; |
| 2686 | } |
| 2687 | |
| 2688 | void trace_event_buffer_commit(struct trace_event_buffer *fbuffer) |
| 2689 | { |
| 2690 | if (static_key_false(&tracepoint_printk_key.key)) |
| 2691 | output_printk(fbuffer); |
| 2692 | |
Masami Hiramatsu | 8cfcf15 | 2020-01-11 01:05:31 +0900 | [diff] [blame] | 2693 | event_trigger_unlock_commit_regs(fbuffer->trace_file, fbuffer->buffer, |
Steven Rostedt (Red Hat) | 42391745 | 2016-11-23 15:52:45 -0500 | [diff] [blame] | 2694 | fbuffer->event, fbuffer->entry, |
Masami Hiramatsu | 8cfcf15 | 2020-01-11 01:05:31 +0900 | [diff] [blame] | 2695 | fbuffer->flags, fbuffer->pc, fbuffer->regs); |
Steven Rostedt (Red Hat) | 42391745 | 2016-11-23 15:52:45 -0500 | [diff] [blame] | 2696 | } |
| 2697 | EXPORT_SYMBOL_GPL(trace_event_buffer_commit); |
| 2698 | |
Steven Rostedt (VMware) | 2ee5b92 | 2018-01-23 13:25:04 -0500 | [diff] [blame] | 2699 | /* |
| 2700 | * Skip 3: |
| 2701 | * |
| 2702 | * trace_buffer_unlock_commit_regs() |
| 2703 | * trace_event_buffer_commit() |
| 2704 | * trace_event_raw_event_xxx() |
Rohit Visavalia | 13cf912 | 2018-01-29 15:11:26 +0530 | [diff] [blame] | 2705 | */ |
Steven Rostedt (VMware) | 2ee5b92 | 2018-01-23 13:25:04 -0500 | [diff] [blame] | 2706 | # define STACK_SKIP 3 |
| 2707 | |
Steven Rostedt (Red Hat) | b7f0c95 | 2015-09-25 17:38:44 -0400 | [diff] [blame] | 2708 | void trace_buffer_unlock_commit_regs(struct trace_array *tr, |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2709 | struct trace_buffer *buffer, |
Steven Rostedt | 0d5c6e1 | 2012-11-01 20:54:21 -0400 | [diff] [blame] | 2710 | struct ring_buffer_event *event, |
| 2711 | unsigned long flags, int pc, |
| 2712 | struct pt_regs *regs) |
Masami Hiramatsu | 1fd8df2 | 2011-06-08 16:09:34 +0900 | [diff] [blame] | 2713 | { |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 2714 | __buffer_unlock_commit(buffer, event); |
Masami Hiramatsu | 1fd8df2 | 2011-06-08 16:09:34 +0900 | [diff] [blame] | 2715 | |
Steven Rostedt (Red Hat) | be54f69 | 2016-06-23 14:03:47 -0400 | [diff] [blame] | 2716 | /* |
Steven Rostedt (VMware) | 2ee5b92 | 2018-01-23 13:25:04 -0500 | [diff] [blame] | 2717 | * If regs is not set, then skip the necessary functions. |
Steven Rostedt (Red Hat) | be54f69 | 2016-06-23 14:03:47 -0400 | [diff] [blame] | 2718 | * Note, we can still get here via blktrace, wakeup tracer |
| 2719 | * and mmiotrace, but that's ok if they lose a function or |
Steven Rostedt (VMware) | 2ee5b92 | 2018-01-23 13:25:04 -0500 | [diff] [blame] | 2720 | * two. They are not that meaningful. |
Steven Rostedt (Red Hat) | be54f69 | 2016-06-23 14:03:47 -0400 | [diff] [blame] | 2721 | */ |
Steven Rostedt (VMware) | 2ee5b92 | 2018-01-23 13:25:04 -0500 | [diff] [blame] | 2722 | ftrace_trace_stack(tr, buffer, flags, regs ? 0 : STACK_SKIP, pc, regs); |
Masami Hiramatsu | 1fd8df2 | 2011-06-08 16:09:34 +0900 | [diff] [blame] | 2723 | ftrace_trace_userstack(buffer, flags, pc); |
| 2724 | } |
Masami Hiramatsu | 1fd8df2 | 2011-06-08 16:09:34 +0900 | [diff] [blame] | 2725 | |
Steven Rostedt (Red Hat) | 52ffabe3 | 2016-11-23 20:28:38 -0500 | [diff] [blame] | 2726 | /* |
| 2727 | * Similar to trace_buffer_unlock_commit_regs() but do not dump stack. |
| 2728 | */ |
| 2729 | void |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2730 | trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer, |
Steven Rostedt (Red Hat) | 52ffabe3 | 2016-11-23 20:28:38 -0500 | [diff] [blame] | 2731 | struct ring_buffer_event *event) |
| 2732 | { |
| 2733 | __buffer_unlock_commit(buffer, event); |
| 2734 | } |
| 2735 | |
Chunyan Zhang | 478409d | 2016-11-21 15:57:18 +0800 | [diff] [blame] | 2736 | static void |
| 2737 | trace_process_export(struct trace_export *export, |
| 2738 | struct ring_buffer_event *event) |
| 2739 | { |
| 2740 | struct trace_entry *entry; |
| 2741 | unsigned int size = 0; |
| 2742 | |
| 2743 | entry = ring_buffer_event_data(event); |
| 2744 | size = ring_buffer_event_length(event); |
Felipe Balbi | a773d41 | 2017-06-02 13:20:25 +0300 | [diff] [blame] | 2745 | export->write(export, entry, size); |
Chunyan Zhang | 478409d | 2016-11-21 15:57:18 +0800 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | static DEFINE_MUTEX(ftrace_export_lock); |
| 2749 | |
| 2750 | static struct trace_export __rcu *ftrace_exports_list __read_mostly; |
| 2751 | |
| 2752 | static DEFINE_STATIC_KEY_FALSE(ftrace_exports_enabled); |
| 2753 | |
| 2754 | static inline void ftrace_exports_enable(void) |
| 2755 | { |
| 2756 | static_branch_enable(&ftrace_exports_enabled); |
| 2757 | } |
| 2758 | |
| 2759 | static inline void ftrace_exports_disable(void) |
| 2760 | { |
| 2761 | static_branch_disable(&ftrace_exports_enabled); |
| 2762 | } |
| 2763 | |
Mathieu Malaterre | 1cce377 | 2018-05-16 21:30:12 +0200 | [diff] [blame] | 2764 | static void ftrace_exports(struct ring_buffer_event *event) |
Chunyan Zhang | 478409d | 2016-11-21 15:57:18 +0800 | [diff] [blame] | 2765 | { |
| 2766 | struct trace_export *export; |
| 2767 | |
| 2768 | preempt_disable_notrace(); |
| 2769 | |
Joel Fernandes (Google) | 0a5b99f | 2019-07-11 16:45:41 -0400 | [diff] [blame] | 2770 | export = rcu_dereference_raw_check(ftrace_exports_list); |
Chunyan Zhang | 478409d | 2016-11-21 15:57:18 +0800 | [diff] [blame] | 2771 | while (export) { |
| 2772 | trace_process_export(export, event); |
Joel Fernandes (Google) | 0a5b99f | 2019-07-11 16:45:41 -0400 | [diff] [blame] | 2773 | export = rcu_dereference_raw_check(export->next); |
Chunyan Zhang | 478409d | 2016-11-21 15:57:18 +0800 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | preempt_enable_notrace(); |
| 2777 | } |
| 2778 | |
| 2779 | static inline void |
| 2780 | add_trace_export(struct trace_export **list, struct trace_export *export) |
| 2781 | { |
| 2782 | rcu_assign_pointer(export->next, *list); |
| 2783 | /* |
| 2784 | * We are entering export into the list but another |
| 2785 | * CPU might be walking that list. We need to make sure |
| 2786 | * the export->next pointer is valid before another CPU sees |
| 2787 | * the export pointer included into the list. |
| 2788 | */ |
| 2789 | rcu_assign_pointer(*list, export); |
| 2790 | } |
| 2791 | |
| 2792 | static inline int |
| 2793 | rm_trace_export(struct trace_export **list, struct trace_export *export) |
| 2794 | { |
| 2795 | struct trace_export **p; |
| 2796 | |
| 2797 | for (p = list; *p != NULL; p = &(*p)->next) |
| 2798 | if (*p == export) |
| 2799 | break; |
| 2800 | |
| 2801 | if (*p != export) |
| 2802 | return -1; |
| 2803 | |
| 2804 | rcu_assign_pointer(*p, (*p)->next); |
| 2805 | |
| 2806 | return 0; |
| 2807 | } |
| 2808 | |
| 2809 | static inline void |
| 2810 | add_ftrace_export(struct trace_export **list, struct trace_export *export) |
| 2811 | { |
| 2812 | if (*list == NULL) |
| 2813 | ftrace_exports_enable(); |
| 2814 | |
| 2815 | add_trace_export(list, export); |
| 2816 | } |
| 2817 | |
| 2818 | static inline int |
| 2819 | rm_ftrace_export(struct trace_export **list, struct trace_export *export) |
| 2820 | { |
| 2821 | int ret; |
| 2822 | |
| 2823 | ret = rm_trace_export(list, export); |
| 2824 | if (*list == NULL) |
| 2825 | ftrace_exports_disable(); |
| 2826 | |
| 2827 | return ret; |
| 2828 | } |
| 2829 | |
| 2830 | int register_ftrace_export(struct trace_export *export) |
| 2831 | { |
| 2832 | if (WARN_ON_ONCE(!export->write)) |
| 2833 | return -1; |
| 2834 | |
| 2835 | mutex_lock(&ftrace_export_lock); |
| 2836 | |
| 2837 | add_ftrace_export(&ftrace_exports_list, export); |
| 2838 | |
| 2839 | mutex_unlock(&ftrace_export_lock); |
| 2840 | |
| 2841 | return 0; |
| 2842 | } |
| 2843 | EXPORT_SYMBOL_GPL(register_ftrace_export); |
| 2844 | |
| 2845 | int unregister_ftrace_export(struct trace_export *export) |
| 2846 | { |
| 2847 | int ret; |
| 2848 | |
| 2849 | mutex_lock(&ftrace_export_lock); |
| 2850 | |
| 2851 | ret = rm_ftrace_export(&ftrace_exports_list, export); |
| 2852 | |
| 2853 | mutex_unlock(&ftrace_export_lock); |
| 2854 | |
| 2855 | return ret; |
| 2856 | } |
| 2857 | EXPORT_SYMBOL_GPL(unregister_ftrace_export); |
| 2858 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2859 | void |
Arnaldo Carvalho de Melo | 7be4215 | 2009-02-05 01:13:37 -0500 | [diff] [blame] | 2860 | trace_function(struct trace_array *tr, |
Steven Rostedt | 3869705 | 2008-10-01 13:14:09 -0400 | [diff] [blame] | 2861 | unsigned long ip, unsigned long parent_ip, unsigned long flags, |
| 2862 | int pc) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2863 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 2864 | struct trace_event_call *call = &event_function; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2865 | struct trace_buffer *buffer = tr->array_buffer.buffer; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 2866 | struct ring_buffer_event *event; |
Steven Rostedt | 777e208 | 2008-09-29 23:02:42 -0400 | [diff] [blame] | 2867 | struct ftrace_entry *entry; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2868 | |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 2869 | event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry), |
| 2870 | flags, pc); |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 2871 | if (!event) |
| 2872 | return; |
| 2873 | entry = ring_buffer_event_data(event); |
Steven Rostedt | 777e208 | 2008-09-29 23:02:42 -0400 | [diff] [blame] | 2874 | entry->ip = ip; |
| 2875 | entry->parent_ip = parent_ip; |
Tom Zanussi | e1112b4 | 2009-03-31 00:48:49 -0500 | [diff] [blame] | 2876 | |
Chunyan Zhang | 478409d | 2016-11-21 15:57:18 +0800 | [diff] [blame] | 2877 | if (!call_filter_check_discard(call, entry, buffer, event)) { |
| 2878 | if (static_branch_unlikely(&ftrace_exports_enabled)) |
| 2879 | ftrace_exports(event); |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 2880 | __buffer_unlock_commit(buffer, event); |
Chunyan Zhang | 478409d | 2016-11-21 15:57:18 +0800 | [diff] [blame] | 2881 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2882 | } |
| 2883 | |
Frederic Weisbecker | c0a0d0d | 2009-07-29 17:51:13 +0200 | [diff] [blame] | 2884 | #ifdef CONFIG_STACKTRACE |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2885 | |
Thomas Gleixner | 2a820bf | 2019-04-25 11:45:14 +0200 | [diff] [blame] | 2886 | /* Allow 4 levels of nesting: normal, softirq, irq, NMI */ |
| 2887 | #define FTRACE_KSTACK_NESTING 4 |
| 2888 | |
| 2889 | #define FTRACE_KSTACK_ENTRIES (PAGE_SIZE / FTRACE_KSTACK_NESTING) |
| 2890 | |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2891 | struct ftrace_stack { |
Thomas Gleixner | 2a820bf | 2019-04-25 11:45:14 +0200 | [diff] [blame] | 2892 | unsigned long calls[FTRACE_KSTACK_ENTRIES]; |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2893 | }; |
| 2894 | |
Thomas Gleixner | 2a820bf | 2019-04-25 11:45:14 +0200 | [diff] [blame] | 2895 | |
| 2896 | struct ftrace_stacks { |
| 2897 | struct ftrace_stack stacks[FTRACE_KSTACK_NESTING]; |
| 2898 | }; |
| 2899 | |
| 2900 | static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks); |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2901 | static DEFINE_PER_CPU(int, ftrace_stack_reserve); |
| 2902 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2903 | static void __ftrace_trace_stack(struct trace_buffer *buffer, |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 2904 | unsigned long flags, |
Masami Hiramatsu | 1fd8df2 | 2011-06-08 16:09:34 +0900 | [diff] [blame] | 2905 | int skip, int pc, struct pt_regs *regs) |
Ingo Molnar | 86387f7 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2906 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 2907 | struct trace_event_call *call = &event_kernel_stack; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 2908 | struct ring_buffer_event *event; |
Thomas Gleixner | ee6dd0d | 2019-04-25 11:45:16 +0200 | [diff] [blame] | 2909 | unsigned int size, nr_entries; |
Thomas Gleixner | 2a820bf | 2019-04-25 11:45:14 +0200 | [diff] [blame] | 2910 | struct ftrace_stack *fstack; |
Steven Rostedt | 777e208 | 2008-09-29 23:02:42 -0400 | [diff] [blame] | 2911 | struct stack_entry *entry; |
Thomas Gleixner | 2a820bf | 2019-04-25 11:45:14 +0200 | [diff] [blame] | 2912 | int stackidx; |
Ingo Molnar | 86387f7 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 2913 | |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2914 | /* |
Steven Rostedt (VMware) | 2ee5b92 | 2018-01-23 13:25:04 -0500 | [diff] [blame] | 2915 | * Add one, for this function and the call to save_stack_trace() |
Steven Rostedt (Red Hat) | be54f69 | 2016-06-23 14:03:47 -0400 | [diff] [blame] | 2916 | * If regs is set, then these functions will not be in the way. |
| 2917 | */ |
Steven Rostedt (VMware) | 2ee5b92 | 2018-01-23 13:25:04 -0500 | [diff] [blame] | 2918 | #ifndef CONFIG_UNWINDER_ORC |
Steven Rostedt (Red Hat) | be54f69 | 2016-06-23 14:03:47 -0400 | [diff] [blame] | 2919 | if (!regs) |
Thomas Gleixner | ee6dd0d | 2019-04-25 11:45:16 +0200 | [diff] [blame] | 2920 | skip++; |
Steven Rostedt (VMware) | 2ee5b92 | 2018-01-23 13:25:04 -0500 | [diff] [blame] | 2921 | #endif |
Steven Rostedt (Red Hat) | be54f69 | 2016-06-23 14:03:47 -0400 | [diff] [blame] | 2922 | |
| 2923 | /* |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2924 | * Since events can happen in NMIs there's no safe way to |
| 2925 | * use the per cpu ftrace_stacks. We reserve it and if an interrupt |
| 2926 | * or NMI comes in, it will just have to use the default |
| 2927 | * FTRACE_STACK_SIZE. |
| 2928 | */ |
| 2929 | preempt_disable_notrace(); |
| 2930 | |
Thomas Gleixner | 2a820bf | 2019-04-25 11:45:14 +0200 | [diff] [blame] | 2931 | stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1; |
| 2932 | |
| 2933 | /* This should never happen. If it does, yell once and skip */ |
| 2934 | if (WARN_ON_ONCE(stackidx > FTRACE_KSTACK_NESTING)) |
| 2935 | goto out; |
| 2936 | |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2937 | /* |
Thomas Gleixner | 2a820bf | 2019-04-25 11:45:14 +0200 | [diff] [blame] | 2938 | * The above __this_cpu_inc_return() is 'atomic' cpu local. An |
| 2939 | * interrupt will either see the value pre increment or post |
| 2940 | * increment. If the interrupt happens pre increment it will have |
| 2941 | * restored the counter when it returns. We just need a barrier to |
| 2942 | * keep gcc from moving things around. |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2943 | */ |
| 2944 | barrier(); |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2945 | |
Thomas Gleixner | 2a820bf | 2019-04-25 11:45:14 +0200 | [diff] [blame] | 2946 | fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx; |
Thomas Gleixner | ee6dd0d | 2019-04-25 11:45:16 +0200 | [diff] [blame] | 2947 | size = ARRAY_SIZE(fstack->calls); |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2948 | |
Thomas Gleixner | ee6dd0d | 2019-04-25 11:45:16 +0200 | [diff] [blame] | 2949 | if (regs) { |
| 2950 | nr_entries = stack_trace_save_regs(regs, fstack->calls, |
| 2951 | size, skip); |
| 2952 | } else { |
| 2953 | nr_entries = stack_trace_save(fstack->calls, size, skip); |
| 2954 | } |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2955 | |
Thomas Gleixner | ee6dd0d | 2019-04-25 11:45:16 +0200 | [diff] [blame] | 2956 | size = nr_entries * sizeof(unsigned long); |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 2957 | event = __trace_buffer_lock_reserve(buffer, TRACE_STACK, |
| 2958 | sizeof(*entry) + size, flags, pc); |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2959 | if (!event) |
| 2960 | goto out; |
| 2961 | entry = ring_buffer_event_data(event); |
| 2962 | |
Thomas Gleixner | ee6dd0d | 2019-04-25 11:45:16 +0200 | [diff] [blame] | 2963 | memcpy(&entry->caller, fstack->calls, size); |
| 2964 | entry->size = nr_entries; |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2965 | |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 2966 | if (!call_filter_check_discard(call, entry, buffer, event)) |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 2967 | __buffer_unlock_commit(buffer, event); |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2968 | |
| 2969 | out: |
| 2970 | /* Again, don't let gcc optimize things here */ |
| 2971 | barrier(); |
Shan Wei | 8214652 | 2012-11-19 13:21:01 +0800 | [diff] [blame] | 2972 | __this_cpu_dec(ftrace_stack_reserve); |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 2973 | preempt_enable_notrace(); |
| 2974 | |
Ingo Molnar | f0a920d | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 2975 | } |
| 2976 | |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 2977 | static inline void ftrace_trace_stack(struct trace_array *tr, |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2978 | struct trace_buffer *buffer, |
Steven Rostedt (Red Hat) | 73dddbb | 2015-09-29 15:38:55 -0400 | [diff] [blame] | 2979 | unsigned long flags, |
| 2980 | int skip, int pc, struct pt_regs *regs) |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 2981 | { |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 2982 | if (!(tr->trace_flags & TRACE_ITER_STACKTRACE)) |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 2983 | return; |
| 2984 | |
Steven Rostedt (Red Hat) | 73dddbb | 2015-09-29 15:38:55 -0400 | [diff] [blame] | 2985 | __ftrace_trace_stack(buffer, flags, skip, pc, regs); |
Steven Rostedt | 5361499 | 2009-01-15 19:12:40 -0500 | [diff] [blame] | 2986 | } |
| 2987 | |
Frederic Weisbecker | c0a0d0d | 2009-07-29 17:51:13 +0200 | [diff] [blame] | 2988 | void __trace_stack(struct trace_array *tr, unsigned long flags, int skip, |
| 2989 | int pc) |
Steven Rostedt | 3869705 | 2008-10-01 13:14:09 -0400 | [diff] [blame] | 2990 | { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2991 | struct trace_buffer *buffer = tr->array_buffer.buffer; |
Steven Rostedt (VMware) | a33d7d9 | 2017-05-12 13:15:45 -0400 | [diff] [blame] | 2992 | |
| 2993 | if (rcu_is_watching()) { |
| 2994 | __ftrace_trace_stack(buffer, flags, skip, pc, NULL); |
| 2995 | return; |
| 2996 | } |
| 2997 | |
| 2998 | /* |
| 2999 | * When an NMI triggers, RCU is enabled via rcu_nmi_enter(), |
| 3000 | * but if the above rcu_is_watching() failed, then the NMI |
| 3001 | * triggered someplace critical, and rcu_irq_enter() should |
| 3002 | * not be called from NMI. |
| 3003 | */ |
| 3004 | if (unlikely(in_nmi())) |
| 3005 | return; |
| 3006 | |
Steven Rostedt (VMware) | a33d7d9 | 2017-05-12 13:15:45 -0400 | [diff] [blame] | 3007 | rcu_irq_enter_irqson(); |
| 3008 | __ftrace_trace_stack(buffer, flags, skip, pc, NULL); |
| 3009 | rcu_irq_exit_irqson(); |
Steven Rostedt | 3869705 | 2008-10-01 13:14:09 -0400 | [diff] [blame] | 3010 | } |
| 3011 | |
Steven Rostedt | 0388938 | 2009-12-11 09:48:22 -0500 | [diff] [blame] | 3012 | /** |
| 3013 | * trace_dump_stack - record a stack back trace in the trace buffer |
Steven Rostedt (Red Hat) | c142be8 | 2013-03-13 09:55:57 -0400 | [diff] [blame] | 3014 | * @skip: Number of functions to skip (helper handlers) |
Steven Rostedt | 0388938 | 2009-12-11 09:48:22 -0500 | [diff] [blame] | 3015 | */ |
Steven Rostedt (Red Hat) | c142be8 | 2013-03-13 09:55:57 -0400 | [diff] [blame] | 3016 | void trace_dump_stack(int skip) |
Steven Rostedt | 0388938 | 2009-12-11 09:48:22 -0500 | [diff] [blame] | 3017 | { |
| 3018 | unsigned long flags; |
| 3019 | |
| 3020 | if (tracing_disabled || tracing_selftest_running) |
Steven Rostedt | e36c545 | 2009-12-14 15:58:33 -0500 | [diff] [blame] | 3021 | return; |
Steven Rostedt | 0388938 | 2009-12-11 09:48:22 -0500 | [diff] [blame] | 3022 | |
| 3023 | local_save_flags(flags); |
| 3024 | |
Steven Rostedt (VMware) | 2ee5b92 | 2018-01-23 13:25:04 -0500 | [diff] [blame] | 3025 | #ifndef CONFIG_UNWINDER_ORC |
| 3026 | /* Skip 1 to skip this function. */ |
| 3027 | skip++; |
| 3028 | #endif |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3029 | __ftrace_trace_stack(global_trace.array_buffer.buffer, |
Steven Rostedt (Red Hat) | c142be8 | 2013-03-13 09:55:57 -0400 | [diff] [blame] | 3030 | flags, skip, preempt_count(), NULL); |
Steven Rostedt | 0388938 | 2009-12-11 09:48:22 -0500 | [diff] [blame] | 3031 | } |
Nikolay Borisov | da387e5 | 2018-10-17 09:51:43 +0300 | [diff] [blame] | 3032 | EXPORT_SYMBOL_GPL(trace_dump_stack); |
Steven Rostedt | 0388938 | 2009-12-11 09:48:22 -0500 | [diff] [blame] | 3033 | |
Thomas Gleixner | c438f14 | 2019-04-25 11:45:15 +0200 | [diff] [blame] | 3034 | #ifdef CONFIG_USER_STACKTRACE_SUPPORT |
Steven Rostedt | 91e86e5 | 2010-11-10 12:56:12 +0100 | [diff] [blame] | 3035 | static DEFINE_PER_CPU(int, user_stack_count); |
| 3036 | |
Thomas Gleixner | c438f14 | 2019-04-25 11:45:15 +0200 | [diff] [blame] | 3037 | static void |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3038 | ftrace_trace_userstack(struct trace_buffer *buffer, unsigned long flags, int pc) |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 3039 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 3040 | struct trace_event_call *call = &event_user_stack; |
Török Edwin | 8d7c6a9 | 2008-11-23 12:39:06 +0200 | [diff] [blame] | 3041 | struct ring_buffer_event *event; |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 3042 | struct userstack_entry *entry; |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 3043 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3044 | if (!(global_trace.trace_flags & TRACE_ITER_USERSTACKTRACE)) |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 3045 | return; |
| 3046 | |
Steven Rostedt | b634587 | 2010-03-12 20:03:30 -0500 | [diff] [blame] | 3047 | /* |
| 3048 | * NMIs can not handle page faults, even with fix ups. |
| 3049 | * The save user stack can (and often does) fault. |
| 3050 | */ |
| 3051 | if (unlikely(in_nmi())) |
| 3052 | return; |
| 3053 | |
Steven Rostedt | 91e86e5 | 2010-11-10 12:56:12 +0100 | [diff] [blame] | 3054 | /* |
| 3055 | * prevent recursion, since the user stack tracing may |
| 3056 | * trigger other kernel events. |
| 3057 | */ |
| 3058 | preempt_disable(); |
| 3059 | if (__this_cpu_read(user_stack_count)) |
| 3060 | goto out; |
| 3061 | |
| 3062 | __this_cpu_inc(user_stack_count); |
| 3063 | |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 3064 | event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK, |
| 3065 | sizeof(*entry), flags, pc); |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 3066 | if (!event) |
Li Zefan | 1dbd195 | 2010-12-09 15:47:56 +0800 | [diff] [blame] | 3067 | goto out_drop_count; |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 3068 | entry = ring_buffer_event_data(event); |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 3069 | |
Steven Rostedt | 48659d3 | 2009-09-11 11:36:23 -0400 | [diff] [blame] | 3070 | entry->tgid = current->tgid; |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 3071 | memset(&entry->caller, 0, sizeof(entry->caller)); |
| 3072 | |
Thomas Gleixner | ee6dd0d | 2019-04-25 11:45:16 +0200 | [diff] [blame] | 3073 | stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES); |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 3074 | if (!call_filter_check_discard(call, entry, buffer, event)) |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 3075 | __buffer_unlock_commit(buffer, event); |
Steven Rostedt | 91e86e5 | 2010-11-10 12:56:12 +0100 | [diff] [blame] | 3076 | |
Li Zefan | 1dbd195 | 2010-12-09 15:47:56 +0800 | [diff] [blame] | 3077 | out_drop_count: |
Steven Rostedt | 91e86e5 | 2010-11-10 12:56:12 +0100 | [diff] [blame] | 3078 | __this_cpu_dec(user_stack_count); |
Steven Rostedt | 91e86e5 | 2010-11-10 12:56:12 +0100 | [diff] [blame] | 3079 | out: |
| 3080 | preempt_enable(); |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 3081 | } |
Thomas Gleixner | c438f14 | 2019-04-25 11:45:15 +0200 | [diff] [blame] | 3082 | #else /* CONFIG_USER_STACKTRACE_SUPPORT */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3083 | static void ftrace_trace_userstack(struct trace_buffer *buffer, |
Thomas Gleixner | c438f14 | 2019-04-25 11:45:15 +0200 | [diff] [blame] | 3084 | unsigned long flags, int pc) |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 3085 | { |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 3086 | } |
Thomas Gleixner | c438f14 | 2019-04-25 11:45:15 +0200 | [diff] [blame] | 3087 | #endif /* !CONFIG_USER_STACKTRACE_SUPPORT */ |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 3088 | |
Frederic Weisbecker | c0a0d0d | 2009-07-29 17:51:13 +0200 | [diff] [blame] | 3089 | #endif /* CONFIG_STACKTRACE */ |
| 3090 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3091 | /* created for use with alloc_percpu */ |
| 3092 | struct trace_buffer_struct { |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 3093 | int nesting; |
| 3094 | char buffer[4][TRACE_BUF_SIZE]; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3095 | }; |
| 3096 | |
| 3097 | static struct trace_buffer_struct *trace_percpu_buffer; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3098 | |
| 3099 | /* |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 3100 | * Thise allows for lockless recording. If we're nested too deeply, then |
| 3101 | * this returns NULL. |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3102 | */ |
| 3103 | static char *get_trace_buf(void) |
| 3104 | { |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 3105 | struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer); |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3106 | |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 3107 | if (!buffer || buffer->nesting >= 4) |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3108 | return NULL; |
| 3109 | |
Steven Rostedt (VMware) | 3d9622c | 2017-09-05 11:32:01 -0400 | [diff] [blame] | 3110 | buffer->nesting++; |
| 3111 | |
| 3112 | /* Interrupts must see nesting incremented before we use the buffer */ |
| 3113 | barrier(); |
| 3114 | return &buffer->buffer[buffer->nesting][0]; |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 3115 | } |
| 3116 | |
| 3117 | static void put_trace_buf(void) |
| 3118 | { |
Steven Rostedt (VMware) | 3d9622c | 2017-09-05 11:32:01 -0400 | [diff] [blame] | 3119 | /* Don't let the decrement of nesting leak before this */ |
| 3120 | barrier(); |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 3121 | this_cpu_dec(trace_percpu_buffer->nesting); |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | static int alloc_percpu_trace_buffer(void) |
| 3125 | { |
| 3126 | struct trace_buffer_struct *buffers; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3127 | |
| 3128 | buffers = alloc_percpu(struct trace_buffer_struct); |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 3129 | if (WARN(!buffers, "Could not allocate percpu trace_printk buffer")) |
| 3130 | return -ENOMEM; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3131 | |
| 3132 | trace_percpu_buffer = buffers; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3133 | return 0; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3134 | } |
| 3135 | |
Steven Rostedt | 8169883 | 2012-10-11 10:15:05 -0400 | [diff] [blame] | 3136 | static int buffers_allocated; |
| 3137 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3138 | void trace_printk_init_buffers(void) |
| 3139 | { |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3140 | if (buffers_allocated) |
| 3141 | return; |
| 3142 | |
| 3143 | if (alloc_percpu_trace_buffer()) |
| 3144 | return; |
| 3145 | |
Steven Rostedt | 2184db4 | 2014-05-28 13:14:40 -0400 | [diff] [blame] | 3146 | /* trace_printk() is for debug use only. Don't use it in production. */ |
| 3147 | |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 3148 | pr_warn("\n"); |
| 3149 | pr_warn("**********************************************************\n"); |
| 3150 | pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); |
| 3151 | pr_warn("** **\n"); |
| 3152 | pr_warn("** trace_printk() being used. Allocating extra memory. **\n"); |
| 3153 | pr_warn("** **\n"); |
| 3154 | pr_warn("** This means that this is a DEBUG kernel and it is **\n"); |
| 3155 | pr_warn("** unsafe for production use. **\n"); |
| 3156 | pr_warn("** **\n"); |
| 3157 | pr_warn("** If you see this message and you are not debugging **\n"); |
| 3158 | pr_warn("** the kernel, report this immediately to your vendor! **\n"); |
| 3159 | pr_warn("** **\n"); |
| 3160 | pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); |
| 3161 | pr_warn("**********************************************************\n"); |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3162 | |
Steven Rostedt | b382ede6 | 2012-10-10 21:44:34 -0400 | [diff] [blame] | 3163 | /* Expand the buffers to set size */ |
| 3164 | tracing_update_buffers(); |
| 3165 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3166 | buffers_allocated = 1; |
Steven Rostedt | 8169883 | 2012-10-11 10:15:05 -0400 | [diff] [blame] | 3167 | |
| 3168 | /* |
| 3169 | * trace_printk_init_buffers() can be called by modules. |
| 3170 | * If that happens, then we need to start cmdline recording |
| 3171 | * directly here. If the global_trace.buffer is already |
| 3172 | * allocated here, then this was called by module code. |
| 3173 | */ |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3174 | if (global_trace.array_buffer.buffer) |
Steven Rostedt | 8169883 | 2012-10-11 10:15:05 -0400 | [diff] [blame] | 3175 | tracing_start_cmdline_record(); |
| 3176 | } |
Divya Indi | f45d122 | 2019-03-20 11:28:51 -0700 | [diff] [blame] | 3177 | EXPORT_SYMBOL_GPL(trace_printk_init_buffers); |
Steven Rostedt | 8169883 | 2012-10-11 10:15:05 -0400 | [diff] [blame] | 3178 | |
| 3179 | void trace_printk_start_comm(void) |
| 3180 | { |
| 3181 | /* Start tracing comms if trace printk is set */ |
| 3182 | if (!buffers_allocated) |
| 3183 | return; |
| 3184 | tracing_start_cmdline_record(); |
| 3185 | } |
| 3186 | |
| 3187 | static void trace_printk_start_stop_comm(int enabled) |
| 3188 | { |
| 3189 | if (!buffers_allocated) |
| 3190 | return; |
| 3191 | |
| 3192 | if (enabled) |
| 3193 | tracing_start_cmdline_record(); |
| 3194 | else |
| 3195 | tracing_stop_cmdline_record(); |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3196 | } |
| 3197 | |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3198 | /** |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3199 | * trace_vbprintk - write binary msg to tracing buffer |
Jakub Kicinski | c68c9ec | 2019-08-27 22:25:47 -0700 | [diff] [blame] | 3200 | * @ip: The address of the caller |
| 3201 | * @fmt: The string format to write to the buffer |
| 3202 | * @args: Arguments for @fmt |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3203 | */ |
Steven Rostedt | 40ce74f | 2009-03-19 14:03:53 -0400 | [diff] [blame] | 3204 | int trace_vbprintk(unsigned long ip, const char *fmt, va_list args) |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3205 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 3206 | struct trace_event_call *call = &event_bprint; |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3207 | struct ring_buffer_event *event; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3208 | struct trace_buffer *buffer; |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3209 | struct trace_array *tr = &global_trace; |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3210 | struct bprint_entry *entry; |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3211 | unsigned long flags; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3212 | char *tbuffer; |
| 3213 | int len = 0, size, pc; |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3214 | |
| 3215 | if (unlikely(tracing_selftest_running || tracing_disabled)) |
| 3216 | return 0; |
| 3217 | |
| 3218 | /* Don't pollute graph traces with trace_vprintk internals */ |
| 3219 | pause_graph_tracing(); |
| 3220 | |
| 3221 | pc = preempt_count(); |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3222 | preempt_disable_notrace(); |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3223 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3224 | tbuffer = get_trace_buf(); |
| 3225 | if (!tbuffer) { |
| 3226 | len = 0; |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 3227 | goto out_nobuffer; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3228 | } |
| 3229 | |
| 3230 | len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args); |
| 3231 | |
| 3232 | if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0) |
Steven Rostedt (VMware) | 34423f2 | 2020-01-22 06:44:50 -0500 | [diff] [blame^] | 3233 | goto out_put; |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3234 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3235 | local_save_flags(flags); |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3236 | size = sizeof(*entry) + sizeof(u32) * len; |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3237 | buffer = tr->array_buffer.buffer; |
Steven Rostedt (VMware) | 82d1b81 | 2020-01-16 08:20:18 -0500 | [diff] [blame] | 3238 | ring_buffer_nest_start(buffer); |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 3239 | event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size, |
| 3240 | flags, pc); |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3241 | if (!event) |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3242 | goto out; |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3243 | entry = ring_buffer_event_data(event); |
| 3244 | entry->ip = ip; |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3245 | entry->fmt = fmt; |
| 3246 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3247 | memcpy(entry->buf, tbuffer, sizeof(u32) * len); |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 3248 | if (!call_filter_check_discard(call, entry, buffer, event)) { |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 3249 | __buffer_unlock_commit(buffer, event); |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 3250 | ftrace_trace_stack(tr, buffer, flags, 6, pc, NULL); |
Steven Rostedt | d931369 | 2010-01-06 17:27:11 -0500 | [diff] [blame] | 3251 | } |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3252 | |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3253 | out: |
Steven Rostedt (VMware) | 82d1b81 | 2020-01-16 08:20:18 -0500 | [diff] [blame] | 3254 | ring_buffer_nest_end(buffer); |
Steven Rostedt (VMware) | 34423f2 | 2020-01-22 06:44:50 -0500 | [diff] [blame^] | 3255 | out_put: |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 3256 | put_trace_buf(); |
| 3257 | |
| 3258 | out_nobuffer: |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3259 | preempt_enable_notrace(); |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3260 | unpause_graph_tracing(); |
| 3261 | |
| 3262 | return len; |
| 3263 | } |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3264 | EXPORT_SYMBOL_GPL(trace_vbprintk); |
| 3265 | |
Mathieu Malaterre | 26b68dd | 2018-03-08 21:58:43 +0100 | [diff] [blame] | 3266 | __printf(3, 0) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3267 | static int |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3268 | __trace_array_vprintk(struct trace_buffer *buffer, |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3269 | unsigned long ip, const char *fmt, va_list args) |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3270 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 3271 | struct trace_event_call *call = &event_print; |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3272 | struct ring_buffer_event *event; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3273 | int len = 0, size, pc; |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3274 | struct print_entry *entry; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3275 | unsigned long flags; |
| 3276 | char *tbuffer; |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3277 | |
| 3278 | if (tracing_disabled || tracing_selftest_running) |
| 3279 | return 0; |
| 3280 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3281 | /* Don't pollute graph traces with trace_vprintk internals */ |
| 3282 | pause_graph_tracing(); |
| 3283 | |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3284 | pc = preempt_count(); |
| 3285 | preempt_disable_notrace(); |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3286 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3287 | |
| 3288 | tbuffer = get_trace_buf(); |
| 3289 | if (!tbuffer) { |
| 3290 | len = 0; |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 3291 | goto out_nobuffer; |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3292 | } |
| 3293 | |
Dan Carpenter | 3558a5a | 2014-11-27 18:57:52 +0300 | [diff] [blame] | 3294 | len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args); |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3295 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3296 | local_save_flags(flags); |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3297 | size = sizeof(*entry) + len + 1; |
Steven Rostedt (VMware) | 82d1b81 | 2020-01-16 08:20:18 -0500 | [diff] [blame] | 3298 | ring_buffer_nest_start(buffer); |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 3299 | event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, |
| 3300 | flags, pc); |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3301 | if (!event) |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3302 | goto out; |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3303 | entry = ring_buffer_event_data(event); |
Carsten Emde | c13d2f7 | 2009-11-16 20:56:13 +0100 | [diff] [blame] | 3304 | entry->ip = ip; |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3305 | |
Dan Carpenter | 3558a5a | 2014-11-27 18:57:52 +0300 | [diff] [blame] | 3306 | memcpy(&entry->buf, tbuffer, len + 1); |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 3307 | if (!call_filter_check_discard(call, entry, buffer, event)) { |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 3308 | __buffer_unlock_commit(buffer, event); |
Steven Rostedt (Red Hat) | 2d34f48 | 2015-09-30 11:45:22 -0400 | [diff] [blame] | 3309 | ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL); |
Steven Rostedt | d931369 | 2010-01-06 17:27:11 -0500 | [diff] [blame] | 3310 | } |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 3311 | |
| 3312 | out: |
Steven Rostedt (VMware) | 82d1b81 | 2020-01-16 08:20:18 -0500 | [diff] [blame] | 3313 | ring_buffer_nest_end(buffer); |
Andy Lutomirski | e2ace00 | 2016-05-26 12:00:33 -0700 | [diff] [blame] | 3314 | put_trace_buf(); |
| 3315 | |
| 3316 | out_nobuffer: |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3317 | preempt_enable_notrace(); |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 3318 | unpause_graph_tracing(); |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3319 | |
| 3320 | return len; |
| 3321 | } |
Steven Rostedt | 659372d | 2009-09-03 19:11:07 -0400 | [diff] [blame] | 3322 | |
Mathieu Malaterre | 26b68dd | 2018-03-08 21:58:43 +0100 | [diff] [blame] | 3323 | __printf(3, 0) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3324 | int trace_array_vprintk(struct trace_array *tr, |
| 3325 | unsigned long ip, const char *fmt, va_list args) |
| 3326 | { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3327 | return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3328 | } |
| 3329 | |
Mathieu Malaterre | 26b68dd | 2018-03-08 21:58:43 +0100 | [diff] [blame] | 3330 | __printf(3, 0) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3331 | int trace_array_printk(struct trace_array *tr, |
| 3332 | unsigned long ip, const char *fmt, ...) |
| 3333 | { |
| 3334 | int ret; |
| 3335 | va_list ap; |
| 3336 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3337 | if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3338 | return 0; |
| 3339 | |
Divya Indi | 953ae45 | 2019-08-14 10:55:25 -0700 | [diff] [blame] | 3340 | if (!tr) |
| 3341 | return -ENOENT; |
| 3342 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3343 | va_start(ap, fmt); |
| 3344 | ret = trace_array_vprintk(tr, ip, fmt, ap); |
| 3345 | va_end(ap); |
| 3346 | return ret; |
| 3347 | } |
Divya Indi | f45d122 | 2019-03-20 11:28:51 -0700 | [diff] [blame] | 3348 | EXPORT_SYMBOL_GPL(trace_array_printk); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3349 | |
Mathieu Malaterre | 26b68dd | 2018-03-08 21:58:43 +0100 | [diff] [blame] | 3350 | __printf(3, 4) |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3351 | int trace_array_printk_buf(struct trace_buffer *buffer, |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3352 | unsigned long ip, const char *fmt, ...) |
| 3353 | { |
| 3354 | int ret; |
| 3355 | va_list ap; |
| 3356 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3357 | if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3358 | return 0; |
| 3359 | |
| 3360 | va_start(ap, fmt); |
| 3361 | ret = __trace_array_vprintk(buffer, ip, fmt, ap); |
| 3362 | va_end(ap); |
| 3363 | return ret; |
| 3364 | } |
| 3365 | |
Mathieu Malaterre | 26b68dd | 2018-03-08 21:58:43 +0100 | [diff] [blame] | 3366 | __printf(2, 0) |
Steven Rostedt | 659372d | 2009-09-03 19:11:07 -0400 | [diff] [blame] | 3367 | int trace_vprintk(unsigned long ip, const char *fmt, va_list args) |
| 3368 | { |
Steven Rostedt | a813a15 | 2009-10-09 01:41:35 -0400 | [diff] [blame] | 3369 | return trace_array_vprintk(&global_trace, ip, fmt, args); |
Steven Rostedt | 659372d | 2009-09-03 19:11:07 -0400 | [diff] [blame] | 3370 | } |
Frederic Weisbecker | 769b044 | 2009-03-06 17:21:49 +0100 | [diff] [blame] | 3371 | EXPORT_SYMBOL_GPL(trace_vprintk); |
| 3372 | |
Robert Richter | e2ac8ef | 2008-11-12 12:59:32 +0100 | [diff] [blame] | 3373 | static void trace_iterator_increment(struct trace_iterator *iter) |
Steven Rostedt | 5a90f57 | 2008-09-03 17:42:51 -0400 | [diff] [blame] | 3374 | { |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 3375 | struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu); |
| 3376 | |
Steven Rostedt | 5a90f57 | 2008-09-03 17:42:51 -0400 | [diff] [blame] | 3377 | iter->idx++; |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 3378 | if (buf_iter) |
| 3379 | ring_buffer_read(buf_iter, NULL); |
Steven Rostedt | 5a90f57 | 2008-09-03 17:42:51 -0400 | [diff] [blame] | 3380 | } |
| 3381 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3382 | static struct trace_entry * |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 3383 | peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts, |
| 3384 | unsigned long *lost_events) |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3385 | { |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3386 | struct ring_buffer_event *event; |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 3387 | struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu); |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3388 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3389 | if (buf_iter) |
| 3390 | event = ring_buffer_iter_peek(buf_iter, ts); |
| 3391 | else |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3392 | event = ring_buffer_peek(iter->array_buffer->buffer, cpu, ts, |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 3393 | lost_events); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3394 | |
Steven Rostedt | 4a9bd3f | 2011-07-14 16:36:53 -0400 | [diff] [blame] | 3395 | if (event) { |
| 3396 | iter->ent_size = ring_buffer_event_length(event); |
| 3397 | return ring_buffer_event_data(event); |
| 3398 | } |
| 3399 | iter->ent_size = 0; |
| 3400 | return NULL; |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3401 | } |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3402 | |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3403 | static struct trace_entry * |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 3404 | __find_next_entry(struct trace_iterator *iter, int *ent_cpu, |
| 3405 | unsigned long *missing_events, u64 *ent_ts) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3406 | { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3407 | struct trace_buffer *buffer = iter->array_buffer->buffer; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3408 | struct trace_entry *ent, *next = NULL; |
Lai Jiangshan | aa27497 | 2010-04-05 17:11:05 +0800 | [diff] [blame] | 3409 | unsigned long lost_events = 0, next_lost = 0; |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 3410 | int cpu_file = iter->cpu_file; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3411 | u64 next_ts = 0, ts; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3412 | int next_cpu = -1; |
Steven Rostedt | 12b5da3 | 2012-03-27 10:43:28 -0400 | [diff] [blame] | 3413 | int next_size = 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3414 | int cpu; |
| 3415 | |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 3416 | /* |
| 3417 | * If we are in a per_cpu trace file, don't bother by iterating over |
| 3418 | * all cpu and peek directly. |
| 3419 | */ |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 3420 | if (cpu_file > RING_BUFFER_ALL_CPUS) { |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 3421 | if (ring_buffer_empty_cpu(buffer, cpu_file)) |
| 3422 | return NULL; |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 3423 | ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 3424 | if (ent_cpu) |
| 3425 | *ent_cpu = cpu_file; |
| 3426 | |
| 3427 | return ent; |
| 3428 | } |
| 3429 | |
Steven Rostedt | ab46428 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 3430 | for_each_tracing_cpu(cpu) { |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3431 | |
| 3432 | if (ring_buffer_empty_cpu(buffer, cpu)) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3433 | continue; |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3434 | |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 3435 | ent = peek_next_entry(iter, cpu, &ts, &lost_events); |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3436 | |
Ingo Molnar | cdd31cd2 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3437 | /* |
| 3438 | * Pick the entry with the smallest timestamp: |
| 3439 | */ |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3440 | if (ent && (!next || ts < next_ts)) { |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3441 | next = ent; |
| 3442 | next_cpu = cpu; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3443 | next_ts = ts; |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 3444 | next_lost = lost_events; |
Steven Rostedt | 12b5da3 | 2012-03-27 10:43:28 -0400 | [diff] [blame] | 3445 | next_size = iter->ent_size; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3446 | } |
| 3447 | } |
| 3448 | |
Steven Rostedt | 12b5da3 | 2012-03-27 10:43:28 -0400 | [diff] [blame] | 3449 | iter->ent_size = next_size; |
| 3450 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3451 | if (ent_cpu) |
| 3452 | *ent_cpu = next_cpu; |
| 3453 | |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3454 | if (ent_ts) |
| 3455 | *ent_ts = next_ts; |
| 3456 | |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 3457 | if (missing_events) |
| 3458 | *missing_events = next_lost; |
| 3459 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3460 | return next; |
| 3461 | } |
| 3462 | |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3463 | /* Find the next real entry, without updating the iterator itself */ |
Frederic Weisbecker | c4a8e8b | 2009-02-02 20:29:21 -0200 | [diff] [blame] | 3464 | struct trace_entry *trace_find_next_entry(struct trace_iterator *iter, |
| 3465 | int *ent_cpu, u64 *ent_ts) |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3466 | { |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 3467 | return __find_next_entry(iter, ent_cpu, NULL, ent_ts); |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3468 | } |
Ingo Molnar | 8c523a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3469 | |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3470 | /* Find the next real entry, and increment the iterator to the next entry */ |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 3471 | void *trace_find_next_entry_inc(struct trace_iterator *iter) |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3472 | { |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 3473 | iter->ent = __find_next_entry(iter, &iter->cpu, |
| 3474 | &iter->lost_events, &iter->ts); |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3475 | |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3476 | if (iter->ent) |
Robert Richter | e2ac8ef | 2008-11-12 12:59:32 +0100 | [diff] [blame] | 3477 | trace_iterator_increment(iter); |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3478 | |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3479 | return iter->ent ? iter : NULL; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3480 | } |
| 3481 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3482 | static void trace_consume(struct trace_iterator *iter) |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3483 | { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3484 | ring_buffer_consume(iter->array_buffer->buffer, iter->cpu, &iter->ts, |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 3485 | &iter->lost_events); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3486 | } |
| 3487 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3488 | static void *s_next(struct seq_file *m, void *v, loff_t *pos) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3489 | { |
| 3490 | struct trace_iterator *iter = m->private; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3491 | int i = (int)*pos; |
Ingo Molnar | 4e3c333 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3492 | void *ent; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3493 | |
Steven Rostedt | a63ce5b | 2009-12-07 09:11:39 -0500 | [diff] [blame] | 3494 | WARN_ON_ONCE(iter->leftover); |
| 3495 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3496 | (*pos)++; |
| 3497 | |
| 3498 | /* can't go backwards */ |
| 3499 | if (iter->idx > i) |
| 3500 | return NULL; |
| 3501 | |
| 3502 | if (iter->idx < 0) |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 3503 | ent = trace_find_next_entry_inc(iter); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3504 | else |
| 3505 | ent = iter; |
| 3506 | |
| 3507 | while (ent && iter->idx < i) |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 3508 | ent = trace_find_next_entry_inc(iter); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3509 | |
| 3510 | iter->pos = *pos; |
| 3511 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3512 | return ent; |
| 3513 | } |
| 3514 | |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 3515 | void tracing_iter_reset(struct trace_iterator *iter, int cpu) |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3516 | { |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3517 | struct ring_buffer_event *event; |
| 3518 | struct ring_buffer_iter *buf_iter; |
| 3519 | unsigned long entries = 0; |
| 3520 | u64 ts; |
| 3521 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3522 | per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = 0; |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3523 | |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 3524 | buf_iter = trace_buffer_iter(iter, cpu); |
| 3525 | if (!buf_iter) |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3526 | return; |
| 3527 | |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3528 | ring_buffer_iter_reset(buf_iter); |
| 3529 | |
| 3530 | /* |
| 3531 | * We could have the case with the max latency tracers |
| 3532 | * that a reset never took place on a cpu. This is evident |
| 3533 | * by the timestamp being before the start of the buffer. |
| 3534 | */ |
| 3535 | while ((event = ring_buffer_iter_peek(buf_iter, &ts))) { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3536 | if (ts >= iter->array_buffer->time_start) |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3537 | break; |
| 3538 | entries++; |
| 3539 | ring_buffer_read(buf_iter, NULL); |
| 3540 | } |
| 3541 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3542 | per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = entries; |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3543 | } |
| 3544 | |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3545 | /* |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3546 | * The current tracer is copied to avoid a global locking |
| 3547 | * all around. |
| 3548 | */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3549 | static void *s_start(struct seq_file *m, loff_t *pos) |
| 3550 | { |
| 3551 | struct trace_iterator *iter = m->private; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 3552 | struct trace_array *tr = iter->tr; |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 3553 | int cpu_file = iter->cpu_file; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3554 | void *p = NULL; |
| 3555 | loff_t l = 0; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 3556 | int cpu; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3557 | |
Hiraku Toyooka | 2fd196e | 2012-12-26 11:52:52 +0900 | [diff] [blame] | 3558 | /* |
| 3559 | * copy the tracer to avoid using a global lock all around. |
| 3560 | * iter->trace is a copy of current_trace, the pointer to the |
| 3561 | * name may be used instead of a strcmp(), as iter->trace->name |
| 3562 | * will point to the same string as current_trace->name. |
| 3563 | */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3564 | mutex_lock(&trace_types_lock); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 3565 | if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name)) |
| 3566 | *iter->trace = *tr->current_trace; |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 3567 | mutex_unlock(&trace_types_lock); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3568 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3569 | #ifdef CONFIG_TRACER_MAX_TRACE |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 3570 | if (iter->snapshot && iter->trace->use_max_tr) |
| 3571 | return ERR_PTR(-EBUSY); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3572 | #endif |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 3573 | |
| 3574 | if (!iter->snapshot) |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 3575 | atomic_inc(&trace_record_taskinfo_disabled); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3576 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3577 | if (*pos != iter->pos) { |
| 3578 | iter->ent = NULL; |
| 3579 | iter->cpu = 0; |
| 3580 | iter->idx = -1; |
| 3581 | |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 3582 | if (cpu_file == RING_BUFFER_ALL_CPUS) { |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 3583 | for_each_tracing_cpu(cpu) |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3584 | tracing_iter_reset(iter, cpu); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 3585 | } else |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3586 | tracing_iter_reset(iter, cpu_file); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3587 | |
Lai Jiangshan | ac91d85 | 2010-03-02 17:54:50 +0800 | [diff] [blame] | 3588 | iter->leftover = 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3589 | for (p = iter; p && l < *pos; p = s_next(m, p, &l)) |
| 3590 | ; |
| 3591 | |
| 3592 | } else { |
Steven Rostedt | a63ce5b | 2009-12-07 09:11:39 -0500 | [diff] [blame] | 3593 | /* |
| 3594 | * If we overflowed the seq_file before, then we want |
| 3595 | * to just reuse the trace_seq buffer again. |
| 3596 | */ |
| 3597 | if (iter->leftover) |
| 3598 | p = iter; |
| 3599 | else { |
| 3600 | l = *pos - 1; |
| 3601 | p = s_next(m, p, &l); |
| 3602 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3603 | } |
| 3604 | |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 3605 | trace_event_read_lock(); |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 3606 | trace_access_lock(cpu_file); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3607 | return p; |
| 3608 | } |
| 3609 | |
| 3610 | static void s_stop(struct seq_file *m, void *p) |
| 3611 | { |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 3612 | struct trace_iterator *iter = m->private; |
| 3613 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3614 | #ifdef CONFIG_TRACER_MAX_TRACE |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 3615 | if (iter->snapshot && iter->trace->use_max_tr) |
| 3616 | return; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3617 | #endif |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 3618 | |
| 3619 | if (!iter->snapshot) |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 3620 | atomic_dec(&trace_record_taskinfo_disabled); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3621 | |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 3622 | trace_access_unlock(iter->cpu_file); |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 3623 | trace_event_read_unlock(); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3624 | } |
| 3625 | |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3626 | static void |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3627 | get_total_entries_cpu(struct array_buffer *buf, unsigned long *total, |
Douglas Anderson | ecffc8a | 2019-03-19 10:12:05 -0700 | [diff] [blame] | 3628 | unsigned long *entries, int cpu) |
| 3629 | { |
| 3630 | unsigned long count; |
| 3631 | |
| 3632 | count = ring_buffer_entries_cpu(buf->buffer, cpu); |
| 3633 | /* |
| 3634 | * If this buffer has skipped entries, then we hold all |
| 3635 | * entries for the trace and we need to ignore the |
| 3636 | * ones before the time stamp. |
| 3637 | */ |
| 3638 | if (per_cpu_ptr(buf->data, cpu)->skipped_entries) { |
| 3639 | count -= per_cpu_ptr(buf->data, cpu)->skipped_entries; |
| 3640 | /* total is the same as the entries */ |
| 3641 | *total = count; |
| 3642 | } else |
| 3643 | *total = count + |
| 3644 | ring_buffer_overrun_cpu(buf->buffer, cpu); |
| 3645 | *entries = count; |
| 3646 | } |
| 3647 | |
| 3648 | static void |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3649 | get_total_entries(struct array_buffer *buf, |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3650 | unsigned long *total, unsigned long *entries) |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3651 | { |
Douglas Anderson | ecffc8a | 2019-03-19 10:12:05 -0700 | [diff] [blame] | 3652 | unsigned long t, e; |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3653 | int cpu; |
| 3654 | |
| 3655 | *total = 0; |
| 3656 | *entries = 0; |
| 3657 | |
| 3658 | for_each_tracing_cpu(cpu) { |
Douglas Anderson | ecffc8a | 2019-03-19 10:12:05 -0700 | [diff] [blame] | 3659 | get_total_entries_cpu(buf, &t, &e, cpu); |
| 3660 | *total += t; |
| 3661 | *entries += e; |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3662 | } |
| 3663 | } |
| 3664 | |
Douglas Anderson | ecffc8a | 2019-03-19 10:12:05 -0700 | [diff] [blame] | 3665 | unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu) |
| 3666 | { |
| 3667 | unsigned long total, entries; |
| 3668 | |
| 3669 | if (!tr) |
| 3670 | tr = &global_trace; |
| 3671 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3672 | get_total_entries_cpu(&tr->array_buffer, &total, &entries, cpu); |
Douglas Anderson | ecffc8a | 2019-03-19 10:12:05 -0700 | [diff] [blame] | 3673 | |
| 3674 | return entries; |
| 3675 | } |
| 3676 | |
| 3677 | unsigned long trace_total_entries(struct trace_array *tr) |
| 3678 | { |
| 3679 | unsigned long total, entries; |
| 3680 | |
| 3681 | if (!tr) |
| 3682 | tr = &global_trace; |
| 3683 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3684 | get_total_entries(&tr->array_buffer, &total, &entries); |
Douglas Anderson | ecffc8a | 2019-03-19 10:12:05 -0700 | [diff] [blame] | 3685 | |
| 3686 | return entries; |
| 3687 | } |
| 3688 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 3689 | static void print_lat_help_header(struct seq_file *m) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3690 | { |
Rasmus Villemoes | d79ac28 | 2014-11-08 21:42:11 +0100 | [diff] [blame] | 3691 | seq_puts(m, "# _------=> CPU# \n" |
| 3692 | "# / _-----=> irqs-off \n" |
| 3693 | "# | / _----=> need-resched \n" |
| 3694 | "# || / _---=> hardirq/softirq \n" |
| 3695 | "# ||| / _--=> preempt-depth \n" |
| 3696 | "# |||| / delay \n" |
| 3697 | "# cmd pid ||||| time | caller \n" |
| 3698 | "# \\ / ||||| \\ | / \n"); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3699 | } |
| 3700 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3701 | static void print_event_info(struct array_buffer *buf, struct seq_file *m) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3702 | { |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3703 | unsigned long total; |
| 3704 | unsigned long entries; |
| 3705 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3706 | get_total_entries(buf, &total, &entries); |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3707 | seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n", |
| 3708 | entries, total, num_online_cpus()); |
| 3709 | seq_puts(m, "#\n"); |
| 3710 | } |
| 3711 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3712 | static void print_func_help_header(struct array_buffer *buf, struct seq_file *m, |
Joel Fernandes | 441dae8 | 2017-06-25 22:38:43 -0700 | [diff] [blame] | 3713 | unsigned int flags) |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3714 | { |
Joel Fernandes | 441dae8 | 2017-06-25 22:38:43 -0700 | [diff] [blame] | 3715 | bool tgid = flags & TRACE_ITER_RECORD_TGID; |
| 3716 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3717 | print_event_info(buf, m); |
Joel Fernandes | 441dae8 | 2017-06-25 22:38:43 -0700 | [diff] [blame] | 3718 | |
Joel Fernandes (Google) | f8494fa | 2018-06-25 17:08:22 -0700 | [diff] [blame] | 3719 | seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? "TGID " : ""); |
| 3720 | seq_printf(m, "# | | %s | | |\n", tgid ? " | " : ""); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3721 | } |
| 3722 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3723 | static void print_func_help_header_irq(struct array_buffer *buf, struct seq_file *m, |
Joel Fernandes | 441dae8 | 2017-06-25 22:38:43 -0700 | [diff] [blame] | 3724 | unsigned int flags) |
Steven Rostedt | 77271ce | 2011-11-17 09:34:33 -0500 | [diff] [blame] | 3725 | { |
Joel Fernandes | 441dae8 | 2017-06-25 22:38:43 -0700 | [diff] [blame] | 3726 | bool tgid = flags & TRACE_ITER_RECORD_TGID; |
Rasmus Villemoes | 0f5e5a3 | 2019-03-20 09:17:57 +0100 | [diff] [blame] | 3727 | const char *space = " "; |
| 3728 | int prec = tgid ? 10 : 2; |
Joel Fernandes | 441dae8 | 2017-06-25 22:38:43 -0700 | [diff] [blame] | 3729 | |
Quentin Perret | 9e73821 | 2019-02-14 15:29:50 +0000 | [diff] [blame] | 3730 | print_event_info(buf, m); |
| 3731 | |
Rasmus Villemoes | 0f5e5a3 | 2019-03-20 09:17:57 +0100 | [diff] [blame] | 3732 | seq_printf(m, "# %.*s _-----=> irqs-off\n", prec, space); |
| 3733 | seq_printf(m, "# %.*s / _----=> need-resched\n", prec, space); |
| 3734 | seq_printf(m, "# %.*s| / _---=> hardirq/softirq\n", prec, space); |
| 3735 | seq_printf(m, "# %.*s|| / _--=> preempt-depth\n", prec, space); |
| 3736 | seq_printf(m, "# %.*s||| / delay\n", prec, space); |
| 3737 | seq_printf(m, "# TASK-PID %.*sCPU# |||| TIMESTAMP FUNCTION\n", prec, " TGID "); |
| 3738 | seq_printf(m, "# | | %.*s | |||| | |\n", prec, " | "); |
Steven Rostedt | 77271ce | 2011-11-17 09:34:33 -0500 | [diff] [blame] | 3739 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3740 | |
Jiri Olsa | 62b915f | 2010-04-02 19:01:22 +0200 | [diff] [blame] | 3741 | void |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3742 | print_trace_header(struct seq_file *m, struct trace_iterator *iter) |
| 3743 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3744 | unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK); |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3745 | struct array_buffer *buf = iter->array_buffer; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3746 | struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 3747 | struct tracer *type = iter->trace; |
Steven Rostedt | 39eaf7e | 2011-11-17 10:35:16 -0500 | [diff] [blame] | 3748 | unsigned long entries; |
| 3749 | unsigned long total; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3750 | const char *name = "preemption"; |
| 3751 | |
Steven Rostedt (Red Hat) | d840f71 | 2013-02-01 18:38:47 -0500 | [diff] [blame] | 3752 | name = type->name; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3753 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3754 | get_total_entries(buf, &total, &entries); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3755 | |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3756 | seq_printf(m, "# %s latency trace v1.1.5 on %s\n", |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3757 | name, UTS_RELEASE); |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3758 | seq_puts(m, "# -----------------------------------" |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3759 | "---------------------------------\n"); |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3760 | seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |" |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3761 | " (M:%s VP:%d, KP:%d, SP:%d HP:%d", |
Steven Rostedt | 57f50be | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 3762 | nsecs_to_usecs(data->saved_latency), |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3763 | entries, |
Steven Rostedt | 4c11d7a | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 3764 | total, |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 3765 | buf->cpu, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3766 | #if defined(CONFIG_PREEMPT_NONE) |
| 3767 | "server", |
| 3768 | #elif defined(CONFIG_PREEMPT_VOLUNTARY) |
| 3769 | "desktop", |
Steven Rostedt | b5c21b4 | 2008-07-10 20:58:12 -0400 | [diff] [blame] | 3770 | #elif defined(CONFIG_PREEMPT) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3771 | "preempt", |
Sebastian Andrzej Siewior | 9c34fc4 | 2019-10-15 21:18:20 +0200 | [diff] [blame] | 3772 | #elif defined(CONFIG_PREEMPT_RT) |
| 3773 | "preempt_rt", |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3774 | #else |
| 3775 | "unknown", |
| 3776 | #endif |
| 3777 | /* These are reserved for later use */ |
| 3778 | 0, 0, 0, 0); |
| 3779 | #ifdef CONFIG_SMP |
| 3780 | seq_printf(m, " #P:%d)\n", num_online_cpus()); |
| 3781 | #else |
| 3782 | seq_puts(m, ")\n"); |
| 3783 | #endif |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3784 | seq_puts(m, "# -----------------\n"); |
| 3785 | seq_printf(m, "# | task: %.16s-%d " |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3786 | "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n", |
Eric W. Biederman | d20b92a | 2012-03-13 16:02:19 -0700 | [diff] [blame] | 3787 | data->comm, data->pid, |
| 3788 | from_kuid_munged(seq_user_ns(m), data->uid), data->nice, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3789 | data->policy, data->rt_priority); |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3790 | seq_puts(m, "# -----------------\n"); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3791 | |
| 3792 | if (data->critical_start) { |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3793 | seq_puts(m, "# => started at: "); |
Steven Rostedt | 214023c | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3794 | seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags); |
| 3795 | trace_print_seq(m, &iter->seq); |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3796 | seq_puts(m, "\n# => ended at: "); |
Steven Rostedt | 214023c | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3797 | seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags); |
| 3798 | trace_print_seq(m, &iter->seq); |
Steven Rostedt | 8248ac0 | 2009-09-02 12:27:41 -0400 | [diff] [blame] | 3799 | seq_puts(m, "\n#\n"); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3800 | } |
| 3801 | |
KOSAKI Motohiro | 888b55d | 2009-03-08 13:12:43 +0900 | [diff] [blame] | 3802 | seq_puts(m, "#\n"); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3803 | } |
| 3804 | |
Steven Rostedt | a309720 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 3805 | static void test_cpu_buff_start(struct trace_iterator *iter) |
| 3806 | { |
| 3807 | struct trace_seq *s = &iter->seq; |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3808 | struct trace_array *tr = iter->tr; |
Steven Rostedt | a309720 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 3809 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3810 | if (!(tr->trace_flags & TRACE_ITER_ANNOTATE)) |
Steven Rostedt | 12ef7d4 | 2008-11-12 17:52:38 -0500 | [diff] [blame] | 3811 | return; |
| 3812 | |
| 3813 | if (!(iter->iter_flags & TRACE_FILE_ANNOTATE)) |
| 3814 | return; |
| 3815 | |
Matthias Kaehlcke | 4dbbe2d | 2017-04-21 16:41:10 -0700 | [diff] [blame] | 3816 | if (cpumask_available(iter->started) && |
| 3817 | cpumask_test_cpu(iter->cpu, iter->started)) |
Steven Rostedt | a309720 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 3818 | return; |
| 3819 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3820 | if (per_cpu_ptr(iter->array_buffer->data, iter->cpu)->skipped_entries) |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 3821 | return; |
| 3822 | |
Matthias Kaehlcke | 4dbbe2d | 2017-04-21 16:41:10 -0700 | [diff] [blame] | 3823 | if (cpumask_available(iter->started)) |
Sasha Levin | 919cd97 | 2015-09-04 12:45:56 -0400 | [diff] [blame] | 3824 | cpumask_set_cpu(iter->cpu, iter->started); |
Frederic Weisbecker | b0dfa97 | 2009-04-01 22:53:08 +0200 | [diff] [blame] | 3825 | |
| 3826 | /* Don't print started cpu buffer for the first entry of the trace */ |
| 3827 | if (iter->idx > 1) |
| 3828 | trace_seq_printf(s, "##### CPU %u buffer started ####\n", |
| 3829 | iter->cpu); |
Steven Rostedt | a309720 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 3830 | } |
| 3831 | |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 3832 | static enum print_line_t print_trace_fmt(struct trace_iterator *iter) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3833 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3834 | struct trace_array *tr = iter->tr; |
Steven Rostedt | 214023c | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 3835 | struct trace_seq *s = &iter->seq; |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3836 | unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK); |
Ingo Molnar | 4e3c333 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3837 | struct trace_entry *entry; |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3838 | struct trace_event *event; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3839 | |
Ingo Molnar | 4e3c333 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 3840 | entry = iter->ent; |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3841 | |
Steven Rostedt | a309720 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 3842 | test_cpu_buff_start(iter); |
| 3843 | |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3844 | event = ftrace_find_event(entry->type); |
Frederic Weisbecker | c4a8e8b | 2009-02-02 20:29:21 -0200 | [diff] [blame] | 3845 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3846 | if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) { |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3847 | if (iter->iter_flags & TRACE_FILE_LAT_FMT) |
| 3848 | trace_print_lat_context(iter); |
| 3849 | else |
| 3850 | trace_print_context(iter); |
Frederic Weisbecker | c4a8e8b | 2009-02-02 20:29:21 -0200 | [diff] [blame] | 3851 | } |
| 3852 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3853 | if (trace_seq_has_overflowed(s)) |
| 3854 | return TRACE_TYPE_PARTIAL_LINE; |
| 3855 | |
Arnaldo Carvalho de Melo | 268ccda | 2009-02-04 20:16:39 -0200 | [diff] [blame] | 3856 | if (event) |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 3857 | return event->funcs->trace(iter, sym_flags, event); |
Arnaldo Carvalho de Melo | d9793bd | 2009-02-03 20:20:41 -0200 | [diff] [blame] | 3858 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3859 | trace_seq_printf(s, "Unknown type %d\n", entry->type); |
Steven Rostedt | 7104f30 | 2008-10-01 10:52:51 -0400 | [diff] [blame] | 3860 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3861 | return trace_handle_return(s); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3862 | } |
| 3863 | |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 3864 | static enum print_line_t print_raw_fmt(struct trace_iterator *iter) |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3865 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3866 | struct trace_array *tr = iter->tr; |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3867 | struct trace_seq *s = &iter->seq; |
| 3868 | struct trace_entry *entry; |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3869 | struct trace_event *event; |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3870 | |
| 3871 | entry = iter->ent; |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3872 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3873 | if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3874 | trace_seq_printf(s, "%d %d %llu ", |
| 3875 | entry->pid, iter->cpu, iter->ts); |
| 3876 | |
| 3877 | if (trace_seq_has_overflowed(s)) |
| 3878 | return TRACE_TYPE_PARTIAL_LINE; |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3879 | |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3880 | event = ftrace_find_event(entry->type); |
Arnaldo Carvalho de Melo | 268ccda | 2009-02-04 20:16:39 -0200 | [diff] [blame] | 3881 | if (event) |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 3882 | return event->funcs->raw(iter, 0, event); |
Arnaldo Carvalho de Melo | d9793bd | 2009-02-03 20:20:41 -0200 | [diff] [blame] | 3883 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3884 | trace_seq_printf(s, "%d ?\n", entry->type); |
Steven Rostedt | 7104f30 | 2008-10-01 10:52:51 -0400 | [diff] [blame] | 3885 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3886 | return trace_handle_return(s); |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3887 | } |
| 3888 | |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 3889 | static enum print_line_t print_hex_fmt(struct trace_iterator *iter) |
Ingo Molnar | 5e3ca0e | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 3890 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3891 | struct trace_array *tr = iter->tr; |
Ingo Molnar | 5e3ca0e | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 3892 | struct trace_seq *s = &iter->seq; |
| 3893 | unsigned char newline = '\n'; |
| 3894 | struct trace_entry *entry; |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3895 | struct trace_event *event; |
Ingo Molnar | 5e3ca0e | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 3896 | |
| 3897 | entry = iter->ent; |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3898 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3899 | if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) { |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3900 | SEQ_PUT_HEX_FIELD(s, entry->pid); |
| 3901 | SEQ_PUT_HEX_FIELD(s, iter->cpu); |
| 3902 | SEQ_PUT_HEX_FIELD(s, iter->ts); |
| 3903 | if (trace_seq_has_overflowed(s)) |
| 3904 | return TRACE_TYPE_PARTIAL_LINE; |
Frederic Weisbecker | c4a8e8b | 2009-02-02 20:29:21 -0200 | [diff] [blame] | 3905 | } |
Ingo Molnar | 5e3ca0e | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 3906 | |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3907 | event = ftrace_find_event(entry->type); |
Arnaldo Carvalho de Melo | 268ccda | 2009-02-04 20:16:39 -0200 | [diff] [blame] | 3908 | if (event) { |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 3909 | enum print_line_t ret = event->funcs->hex(iter, 0, event); |
Arnaldo Carvalho de Melo | d9793bd | 2009-02-03 20:20:41 -0200 | [diff] [blame] | 3910 | if (ret != TRACE_TYPE_HANDLED) |
| 3911 | return ret; |
| 3912 | } |
Steven Rostedt | 7104f30 | 2008-10-01 10:52:51 -0400 | [diff] [blame] | 3913 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3914 | SEQ_PUT_FIELD(s, newline); |
Ingo Molnar | 5e3ca0e | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 3915 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3916 | return trace_handle_return(s); |
Ingo Molnar | 5e3ca0e | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 3917 | } |
| 3918 | |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 3919 | static enum print_line_t print_bin_fmt(struct trace_iterator *iter) |
Ingo Molnar | cb0f12a | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3920 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3921 | struct trace_array *tr = iter->tr; |
Ingo Molnar | cb0f12a | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3922 | struct trace_seq *s = &iter->seq; |
| 3923 | struct trace_entry *entry; |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3924 | struct trace_event *event; |
Ingo Molnar | cb0f12a | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3925 | |
| 3926 | entry = iter->ent; |
Steven Rostedt | dd0e545 | 2008-08-01 12:26:41 -0400 | [diff] [blame] | 3927 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3928 | if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) { |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3929 | SEQ_PUT_FIELD(s, entry->pid); |
| 3930 | SEQ_PUT_FIELD(s, iter->cpu); |
| 3931 | SEQ_PUT_FIELD(s, iter->ts); |
| 3932 | if (trace_seq_has_overflowed(s)) |
| 3933 | return TRACE_TYPE_PARTIAL_LINE; |
Frederic Weisbecker | c4a8e8b | 2009-02-02 20:29:21 -0200 | [diff] [blame] | 3934 | } |
Ingo Molnar | cb0f12a | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3935 | |
Steven Rostedt | f633cef | 2008-12-23 23:24:13 -0500 | [diff] [blame] | 3936 | event = ftrace_find_event(entry->type); |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 3937 | return event ? event->funcs->binary(iter, 0, event) : |
| 3938 | TRACE_TYPE_HANDLED; |
Ingo Molnar | cb0f12a | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3939 | } |
| 3940 | |
Jiri Olsa | 62b915f | 2010-04-02 19:01:22 +0200 | [diff] [blame] | 3941 | int trace_empty(struct trace_iterator *iter) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3942 | { |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 3943 | struct ring_buffer_iter *buf_iter; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3944 | int cpu; |
| 3945 | |
Steven Rostedt | 9aba60f | 2009-03-11 19:52:30 -0400 | [diff] [blame] | 3946 | /* If we are looking at one CPU buffer, only check that one */ |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 3947 | if (iter->cpu_file != RING_BUFFER_ALL_CPUS) { |
Steven Rostedt | 9aba60f | 2009-03-11 19:52:30 -0400 | [diff] [blame] | 3948 | cpu = iter->cpu_file; |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 3949 | buf_iter = trace_buffer_iter(iter, cpu); |
| 3950 | if (buf_iter) { |
| 3951 | if (!ring_buffer_iter_empty(buf_iter)) |
Steven Rostedt | 9aba60f | 2009-03-11 19:52:30 -0400 | [diff] [blame] | 3952 | return 0; |
| 3953 | } else { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3954 | if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu)) |
Steven Rostedt | 9aba60f | 2009-03-11 19:52:30 -0400 | [diff] [blame] | 3955 | return 0; |
| 3956 | } |
| 3957 | return 1; |
| 3958 | } |
| 3959 | |
Steven Rostedt | ab46428 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 3960 | for_each_tracing_cpu(cpu) { |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 3961 | buf_iter = trace_buffer_iter(iter, cpu); |
| 3962 | if (buf_iter) { |
| 3963 | if (!ring_buffer_iter_empty(buf_iter)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3964 | return 0; |
| 3965 | } else { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 3966 | if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3967 | return 0; |
| 3968 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3969 | } |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3970 | |
Frederic Weisbecker | 797d371 | 2008-09-30 18:13:45 +0200 | [diff] [blame] | 3971 | return 1; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 3972 | } |
| 3973 | |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 3974 | /* Called with trace_event_read_lock() held. */ |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 3975 | enum print_line_t print_trace_line(struct trace_iterator *iter) |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 3976 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 3977 | struct trace_array *tr = iter->tr; |
| 3978 | unsigned long trace_flags = tr->trace_flags; |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 3979 | enum print_line_t ret; |
| 3980 | |
Steven Rostedt (Red Hat) | 19a7fe2 | 2014-11-12 10:29:54 -0500 | [diff] [blame] | 3981 | if (iter->lost_events) { |
| 3982 | trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n", |
| 3983 | iter->cpu, iter->lost_events); |
| 3984 | if (trace_seq_has_overflowed(&iter->seq)) |
| 3985 | return TRACE_TYPE_PARTIAL_LINE; |
| 3986 | } |
Steven Rostedt | bc21b47 | 2010-03-31 19:49:26 -0400 | [diff] [blame] | 3987 | |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 3988 | if (iter->trace && iter->trace->print_line) { |
| 3989 | ret = iter->trace->print_line(iter); |
| 3990 | if (ret != TRACE_TYPE_UNHANDLED) |
| 3991 | return ret; |
| 3992 | } |
Thomas Gleixner | 72829bc | 2008-05-23 21:37:28 +0200 | [diff] [blame] | 3993 | |
Steven Rostedt (Red Hat) | 09ae723 | 2013-03-08 21:02:34 -0500 | [diff] [blame] | 3994 | if (iter->ent->type == TRACE_BPUTS && |
| 3995 | trace_flags & TRACE_ITER_PRINTK && |
| 3996 | trace_flags & TRACE_ITER_PRINTK_MSGONLY) |
| 3997 | return trace_print_bputs_msg_only(iter); |
| 3998 | |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 3999 | if (iter->ent->type == TRACE_BPRINT && |
| 4000 | trace_flags & TRACE_ITER_PRINTK && |
| 4001 | trace_flags & TRACE_ITER_PRINTK_MSGONLY) |
Steven Rostedt | 5ef841f | 2009-03-19 12:20:38 -0400 | [diff] [blame] | 4002 | return trace_print_bprintk_msg_only(iter); |
Frederic Weisbecker | 48ead02 | 2009-03-12 18:24:49 +0100 | [diff] [blame] | 4003 | |
Frederic Weisbecker | 66896a8 | 2008-12-13 20:18:13 +0100 | [diff] [blame] | 4004 | if (iter->ent->type == TRACE_PRINT && |
| 4005 | trace_flags & TRACE_ITER_PRINTK && |
| 4006 | trace_flags & TRACE_ITER_PRINTK_MSGONLY) |
Steven Rostedt | 5ef841f | 2009-03-19 12:20:38 -0400 | [diff] [blame] | 4007 | return trace_print_printk_msg_only(iter); |
Frederic Weisbecker | 66896a8 | 2008-12-13 20:18:13 +0100 | [diff] [blame] | 4008 | |
Ingo Molnar | cb0f12a | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 4009 | if (trace_flags & TRACE_ITER_BIN) |
| 4010 | return print_bin_fmt(iter); |
| 4011 | |
Ingo Molnar | 5e3ca0e | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 4012 | if (trace_flags & TRACE_ITER_HEX) |
| 4013 | return print_hex_fmt(iter); |
| 4014 | |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 4015 | if (trace_flags & TRACE_ITER_RAW) |
| 4016 | return print_raw_fmt(iter); |
| 4017 | |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 4018 | return print_trace_fmt(iter); |
| 4019 | } |
| 4020 | |
Jiri Olsa | 7e9a49e | 2011-11-07 16:08:49 +0100 | [diff] [blame] | 4021 | void trace_latency_header(struct seq_file *m) |
| 4022 | { |
| 4023 | struct trace_iterator *iter = m->private; |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 4024 | struct trace_array *tr = iter->tr; |
Jiri Olsa | 7e9a49e | 2011-11-07 16:08:49 +0100 | [diff] [blame] | 4025 | |
| 4026 | /* print nothing if the buffers are empty */ |
| 4027 | if (trace_empty(iter)) |
| 4028 | return; |
| 4029 | |
| 4030 | if (iter->iter_flags & TRACE_FILE_LAT_FMT) |
| 4031 | print_trace_header(m, iter); |
| 4032 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 4033 | if (!(tr->trace_flags & TRACE_ITER_VERBOSE)) |
Jiri Olsa | 7e9a49e | 2011-11-07 16:08:49 +0100 | [diff] [blame] | 4034 | print_lat_help_header(m); |
| 4035 | } |
| 4036 | |
Jiri Olsa | 62b915f | 2010-04-02 19:01:22 +0200 | [diff] [blame] | 4037 | void trace_default_header(struct seq_file *m) |
| 4038 | { |
| 4039 | struct trace_iterator *iter = m->private; |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 4040 | struct trace_array *tr = iter->tr; |
| 4041 | unsigned long trace_flags = tr->trace_flags; |
Jiri Olsa | 62b915f | 2010-04-02 19:01:22 +0200 | [diff] [blame] | 4042 | |
Jiri Olsa | f56e7f8 | 2011-06-03 16:58:49 +0200 | [diff] [blame] | 4043 | if (!(trace_flags & TRACE_ITER_CONTEXT_INFO)) |
| 4044 | return; |
| 4045 | |
Jiri Olsa | 62b915f | 2010-04-02 19:01:22 +0200 | [diff] [blame] | 4046 | if (iter->iter_flags & TRACE_FILE_LAT_FMT) { |
| 4047 | /* print nothing if the buffers are empty */ |
| 4048 | if (trace_empty(iter)) |
| 4049 | return; |
| 4050 | print_trace_header(m, iter); |
| 4051 | if (!(trace_flags & TRACE_ITER_VERBOSE)) |
| 4052 | print_lat_help_header(m); |
| 4053 | } else { |
Steven Rostedt | 77271ce | 2011-11-17 09:34:33 -0500 | [diff] [blame] | 4054 | if (!(trace_flags & TRACE_ITER_VERBOSE)) { |
| 4055 | if (trace_flags & TRACE_ITER_IRQ_INFO) |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 4056 | print_func_help_header_irq(iter->array_buffer, |
Joel Fernandes | 441dae8 | 2017-06-25 22:38:43 -0700 | [diff] [blame] | 4057 | m, trace_flags); |
Steven Rostedt | 77271ce | 2011-11-17 09:34:33 -0500 | [diff] [blame] | 4058 | else |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 4059 | print_func_help_header(iter->array_buffer, m, |
Joel Fernandes | 441dae8 | 2017-06-25 22:38:43 -0700 | [diff] [blame] | 4060 | trace_flags); |
Steven Rostedt | 77271ce | 2011-11-17 09:34:33 -0500 | [diff] [blame] | 4061 | } |
Jiri Olsa | 62b915f | 2010-04-02 19:01:22 +0200 | [diff] [blame] | 4062 | } |
| 4063 | } |
| 4064 | |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 4065 | static void test_ftrace_alive(struct seq_file *m) |
| 4066 | { |
| 4067 | if (!ftrace_is_dead()) |
| 4068 | return; |
Rasmus Villemoes | d79ac28 | 2014-11-08 21:42:11 +0100 | [diff] [blame] | 4069 | seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n" |
| 4070 | "# MAY BE MISSING FUNCTION EVENTS\n"); |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 4071 | } |
| 4072 | |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 4073 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 4074 | static void show_snapshot_main_help(struct seq_file *m) |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 4075 | { |
Rasmus Villemoes | d79ac28 | 2014-11-08 21:42:11 +0100 | [diff] [blame] | 4076 | seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n" |
| 4077 | "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n" |
| 4078 | "# Takes a snapshot of the main buffer.\n" |
| 4079 | "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n" |
| 4080 | "# (Doesn't have to be '2' works with any number that\n" |
| 4081 | "# is not a '0' or '1')\n"); |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 4082 | } |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 4083 | |
| 4084 | static void show_snapshot_percpu_help(struct seq_file *m) |
| 4085 | { |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 4086 | seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n"); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 4087 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP |
Rasmus Villemoes | d79ac28 | 2014-11-08 21:42:11 +0100 | [diff] [blame] | 4088 | seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n" |
| 4089 | "# Takes a snapshot of the main buffer for this cpu.\n"); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 4090 | #else |
Rasmus Villemoes | d79ac28 | 2014-11-08 21:42:11 +0100 | [diff] [blame] | 4091 | seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n" |
| 4092 | "# Must use main snapshot file to allocate.\n"); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 4093 | #endif |
Rasmus Villemoes | d79ac28 | 2014-11-08 21:42:11 +0100 | [diff] [blame] | 4094 | seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n" |
| 4095 | "# (Doesn't have to be '2' works with any number that\n" |
| 4096 | "# is not a '0' or '1')\n"); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 4097 | } |
| 4098 | |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 4099 | static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) |
| 4100 | { |
Steven Rostedt (Red Hat) | 45ad21c | 2013-03-05 18:25:02 -0500 | [diff] [blame] | 4101 | if (iter->tr->allocated_snapshot) |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 4102 | seq_puts(m, "#\n# * Snapshot is allocated *\n#\n"); |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 4103 | else |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 4104 | seq_puts(m, "#\n# * Snapshot is freed *\n#\n"); |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 4105 | |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 4106 | seq_puts(m, "# Snapshot commands:\n"); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 4107 | if (iter->cpu_file == RING_BUFFER_ALL_CPUS) |
| 4108 | show_snapshot_main_help(m); |
| 4109 | else |
| 4110 | show_snapshot_percpu_help(m); |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 4111 | } |
| 4112 | #else |
| 4113 | /* Should never be called */ |
| 4114 | static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { } |
| 4115 | #endif |
| 4116 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4117 | static int s_show(struct seq_file *m, void *v) |
| 4118 | { |
| 4119 | struct trace_iterator *iter = v; |
Steven Rostedt | a63ce5b | 2009-12-07 09:11:39 -0500 | [diff] [blame] | 4120 | int ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4121 | |
| 4122 | if (iter->ent == NULL) { |
| 4123 | if (iter->tr) { |
| 4124 | seq_printf(m, "# tracer: %s\n", iter->trace->name); |
| 4125 | seq_puts(m, "#\n"); |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 4126 | test_ftrace_alive(m); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4127 | } |
Steven Rostedt (Red Hat) | d8741e2 | 2013-03-05 10:25:16 -0500 | [diff] [blame] | 4128 | if (iter->snapshot && trace_empty(iter)) |
| 4129 | print_snapshot_help(m, iter); |
| 4130 | else if (iter->trace && iter->trace->print_header) |
Markus Metzger | 8bba1bf | 2008-11-25 09:12:31 +0100 | [diff] [blame] | 4131 | iter->trace->print_header(m); |
Jiri Olsa | 62b915f | 2010-04-02 19:01:22 +0200 | [diff] [blame] | 4132 | else |
| 4133 | trace_default_header(m); |
| 4134 | |
Steven Rostedt | a63ce5b | 2009-12-07 09:11:39 -0500 | [diff] [blame] | 4135 | } else if (iter->leftover) { |
| 4136 | /* |
| 4137 | * If we filled the seq_file buffer earlier, we |
| 4138 | * want to just show it now. |
| 4139 | */ |
| 4140 | ret = trace_print_seq(m, &iter->seq); |
| 4141 | |
| 4142 | /* ret should this time be zero, but you never know */ |
| 4143 | iter->leftover = ret; |
| 4144 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4145 | } else { |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 4146 | print_trace_line(iter); |
Steven Rostedt | a63ce5b | 2009-12-07 09:11:39 -0500 | [diff] [blame] | 4147 | ret = trace_print_seq(m, &iter->seq); |
| 4148 | /* |
| 4149 | * If we overflow the seq_file buffer, then it will |
| 4150 | * ask us for this data again at start up. |
| 4151 | * Use that instead. |
| 4152 | * ret is 0 if seq_file write succeeded. |
| 4153 | * -1 otherwise. |
| 4154 | */ |
| 4155 | iter->leftover = ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | return 0; |
| 4159 | } |
| 4160 | |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 4161 | /* |
| 4162 | * Should be used after trace_array_get(), trace_types_lock |
| 4163 | * ensures that i_cdev was already initialized. |
| 4164 | */ |
| 4165 | static inline int tracing_get_cpu(struct inode *inode) |
| 4166 | { |
| 4167 | if (inode->i_cdev) /* See trace_create_cpu_file() */ |
| 4168 | return (long)inode->i_cdev - 1; |
| 4169 | return RING_BUFFER_ALL_CPUS; |
| 4170 | } |
| 4171 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 4172 | static const struct seq_operations tracer_seq_ops = { |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 4173 | .start = s_start, |
| 4174 | .next = s_next, |
| 4175 | .stop = s_stop, |
| 4176 | .show = s_show, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4177 | }; |
| 4178 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 4179 | static struct trace_iterator * |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 4180 | __tracing_open(struct inode *inode, struct file *file, bool snapshot) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4181 | { |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 4182 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4183 | struct trace_iterator *iter; |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 4184 | int cpu; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4185 | |
Steven Rostedt | 85a2f9b | 2009-02-27 00:12:38 -0500 | [diff] [blame] | 4186 | if (tracing_disabled) |
| 4187 | return ERR_PTR(-ENODEV); |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 4188 | |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 4189 | iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter)); |
Steven Rostedt | 85a2f9b | 2009-02-27 00:12:38 -0500 | [diff] [blame] | 4190 | if (!iter) |
| 4191 | return ERR_PTR(-ENOMEM); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4192 | |
Gil Fruchter | 7291723 | 2015-06-09 10:32:35 +0300 | [diff] [blame] | 4193 | iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter), |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 4194 | GFP_KERNEL); |
Dan Carpenter | 93574fc | 2012-07-11 09:35:08 +0300 | [diff] [blame] | 4195 | if (!iter->buffer_iter) |
| 4196 | goto release; |
| 4197 | |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 4198 | /* |
| 4199 | * We make a copy of the current tracer to avoid concurrent |
| 4200 | * changes on it while we are reading. |
| 4201 | */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4202 | mutex_lock(&trace_types_lock); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 4203 | iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL); |
Steven Rostedt | 85a2f9b | 2009-02-27 00:12:38 -0500 | [diff] [blame] | 4204 | if (!iter->trace) |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 4205 | goto fail; |
Steven Rostedt | 85a2f9b | 2009-02-27 00:12:38 -0500 | [diff] [blame] | 4206 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4207 | *iter->trace = *tr->current_trace; |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 4208 | |
Li Zefan | 79f5599 | 2009-06-15 14:58:26 +0800 | [diff] [blame] | 4209 | if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL)) |
Frederic Weisbecker | b0dfa97 | 2009-04-01 22:53:08 +0200 | [diff] [blame] | 4210 | goto fail; |
| 4211 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4212 | iter->tr = tr; |
| 4213 | |
| 4214 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4215 | /* Currently only the top directory has a snapshot */ |
| 4216 | if (tr->current_trace->print_max || snapshot) |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 4217 | iter->array_buffer = &tr->max_buffer; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4218 | else |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4219 | #endif |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 4220 | iter->array_buffer = &tr->array_buffer; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 4221 | iter->snapshot = snapshot; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4222 | iter->pos = -1; |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 4223 | iter->cpu_file = tracing_get_cpu(inode); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 4224 | mutex_init(&iter->mutex); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4225 | |
Markus Metzger | 8bba1bf | 2008-11-25 09:12:31 +0100 | [diff] [blame] | 4226 | /* Notify the tracer early; before we stop tracing. */ |
| 4227 | if (iter->trace && iter->trace->open) |
Markus Metzger | a93751c | 2008-12-11 13:53:26 +0100 | [diff] [blame] | 4228 | iter->trace->open(iter); |
Markus Metzger | 8bba1bf | 2008-11-25 09:12:31 +0100 | [diff] [blame] | 4229 | |
Steven Rostedt | 12ef7d4 | 2008-11-12 17:52:38 -0500 | [diff] [blame] | 4230 | /* Annotate start of buffers if we had overruns */ |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 4231 | if (ring_buffer_overruns(iter->array_buffer->buffer)) |
Steven Rostedt | 12ef7d4 | 2008-11-12 17:52:38 -0500 | [diff] [blame] | 4232 | iter->iter_flags |= TRACE_FILE_ANNOTATE; |
| 4233 | |
David Sharp | 8be0709 | 2012-11-13 12:18:22 -0800 | [diff] [blame] | 4234 | /* Output in nanoseconds only if we are using a clock in nanoseconds. */ |
Yoshihiro YUNOMAE | 58e8eed | 2013-04-23 10:32:39 +0900 | [diff] [blame] | 4235 | if (trace_clocks[tr->clock_id].in_ns) |
David Sharp | 8be0709 | 2012-11-13 12:18:22 -0800 | [diff] [blame] | 4236 | iter->iter_flags |= TRACE_FILE_TIME_IN_NS; |
| 4237 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 4238 | /* stop the trace while dumping if we are not opening "snapshot" */ |
| 4239 | if (!iter->snapshot) |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4240 | tracing_stop_tr(tr); |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 4241 | |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 4242 | if (iter->cpu_file == RING_BUFFER_ALL_CPUS) { |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 4243 | for_each_tracing_cpu(cpu) { |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 4244 | iter->buffer_iter[cpu] = |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 4245 | ring_buffer_read_prepare(iter->array_buffer->buffer, |
Douglas Anderson | 31b265b | 2019-03-08 11:32:04 -0800 | [diff] [blame] | 4246 | cpu, GFP_KERNEL); |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4247 | } |
| 4248 | ring_buffer_read_prepare_sync(); |
| 4249 | for_each_tracing_cpu(cpu) { |
| 4250 | ring_buffer_read_start(iter->buffer_iter[cpu]); |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 4251 | tracing_iter_reset(iter, cpu); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 4252 | } |
| 4253 | } else { |
| 4254 | cpu = iter->cpu_file; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 4255 | iter->buffer_iter[cpu] = |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 4256 | ring_buffer_read_prepare(iter->array_buffer->buffer, |
Douglas Anderson | 31b265b | 2019-03-08 11:32:04 -0800 | [diff] [blame] | 4257 | cpu, GFP_KERNEL); |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4258 | ring_buffer_read_prepare_sync(); |
| 4259 | ring_buffer_read_start(iter->buffer_iter[cpu]); |
Steven Rostedt | 2f26ebd | 2009-09-01 11:06:29 -0400 | [diff] [blame] | 4260 | tracing_iter_reset(iter, cpu); |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 4261 | } |
| 4262 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4263 | mutex_unlock(&trace_types_lock); |
| 4264 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4265 | return iter; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 4266 | |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 4267 | fail: |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 4268 | mutex_unlock(&trace_types_lock); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 4269 | kfree(iter->trace); |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 4270 | kfree(iter->buffer_iter); |
Dan Carpenter | 93574fc | 2012-07-11 09:35:08 +0300 | [diff] [blame] | 4271 | release: |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 4272 | seq_release_private(inode, file); |
| 4273 | return ERR_PTR(-ENOMEM); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4274 | } |
| 4275 | |
| 4276 | int tracing_open_generic(struct inode *inode, struct file *filp) |
| 4277 | { |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 4278 | int ret; |
| 4279 | |
| 4280 | ret = tracing_check_open_get_tr(NULL); |
| 4281 | if (ret) |
| 4282 | return ret; |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 4283 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4284 | filp->private_data = inode->i_private; |
| 4285 | return 0; |
| 4286 | } |
| 4287 | |
Geyslan G. Bem | 2e86421 | 2013-10-18 21:15:54 -0300 | [diff] [blame] | 4288 | bool tracing_is_disabled(void) |
| 4289 | { |
| 4290 | return (tracing_disabled) ? true: false; |
| 4291 | } |
| 4292 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4293 | /* |
| 4294 | * Open and update trace_array ref count. |
| 4295 | * Must have the current trace_array passed to it. |
| 4296 | */ |
Steven Rostedt (VMware) | aa07d71 | 2019-10-11 19:12:21 -0400 | [diff] [blame] | 4297 | int tracing_open_generic_tr(struct inode *inode, struct file *filp) |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4298 | { |
| 4299 | struct trace_array *tr = inode->i_private; |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 4300 | int ret; |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4301 | |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 4302 | ret = tracing_check_open_get_tr(tr); |
| 4303 | if (ret) |
| 4304 | return ret; |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4305 | |
| 4306 | filp->private_data = inode->i_private; |
| 4307 | |
| 4308 | return 0; |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4309 | } |
| 4310 | |
Hannes Eder | 4fd2735 | 2009-02-10 19:44:12 +0100 | [diff] [blame] | 4311 | static int tracing_release(struct inode *inode, struct file *file) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4312 | { |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 4313 | struct trace_array *tr = inode->i_private; |
matt mooney | 907f278 | 2010-09-27 19:04:53 -0700 | [diff] [blame] | 4314 | struct seq_file *m = file->private_data; |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 4315 | struct trace_iterator *iter; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 4316 | int cpu; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4317 | |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 4318 | if (!(file->f_mode & FMODE_READ)) { |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 4319 | trace_array_put(tr); |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 4320 | return 0; |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 4321 | } |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 4322 | |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 4323 | /* Writes do not use seq_file */ |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 4324 | iter = m->private; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4325 | mutex_lock(&trace_types_lock); |
Steven Rostedt (Red Hat) | a695cb5 | 2013-03-06 15:27:24 -0500 | [diff] [blame] | 4326 | |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 4327 | for_each_tracing_cpu(cpu) { |
| 4328 | if (iter->buffer_iter[cpu]) |
| 4329 | ring_buffer_read_finish(iter->buffer_iter[cpu]); |
| 4330 | } |
| 4331 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4332 | if (iter->trace && iter->trace->close) |
| 4333 | iter->trace->close(iter); |
| 4334 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 4335 | if (!iter->snapshot) |
| 4336 | /* reenable tracing if it was previously enabled */ |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4337 | tracing_start_tr(tr); |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 4338 | |
| 4339 | __trace_array_put(tr); |
| 4340 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4341 | mutex_unlock(&trace_types_lock); |
| 4342 | |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 4343 | mutex_destroy(&iter->mutex); |
Frederic Weisbecker | b0dfa97 | 2009-04-01 22:53:08 +0200 | [diff] [blame] | 4344 | free_cpumask_var(iter->started); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 4345 | kfree(iter->trace); |
Steven Rostedt | 6d158a8 | 2012-06-27 20:46:14 -0400 | [diff] [blame] | 4346 | kfree(iter->buffer_iter); |
Jiri Olsa | 50e18b9 | 2012-04-25 10:23:39 +0200 | [diff] [blame] | 4347 | seq_release_private(inode, file); |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 4348 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4349 | return 0; |
| 4350 | } |
| 4351 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4352 | static int tracing_release_generic_tr(struct inode *inode, struct file *file) |
| 4353 | { |
| 4354 | struct trace_array *tr = inode->i_private; |
| 4355 | |
| 4356 | trace_array_put(tr); |
| 4357 | return 0; |
| 4358 | } |
| 4359 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4360 | static int tracing_single_release_tr(struct inode *inode, struct file *file) |
| 4361 | { |
| 4362 | struct trace_array *tr = inode->i_private; |
| 4363 | |
| 4364 | trace_array_put(tr); |
| 4365 | |
| 4366 | return single_release(inode, file); |
| 4367 | } |
| 4368 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4369 | static int tracing_open(struct inode *inode, struct file *file) |
| 4370 | { |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 4371 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | 85a2f9b | 2009-02-27 00:12:38 -0500 | [diff] [blame] | 4372 | struct trace_iterator *iter; |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 4373 | int ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4374 | |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 4375 | ret = tracing_check_open_get_tr(tr); |
| 4376 | if (ret) |
| 4377 | return ret; |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 4378 | |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 4379 | /* If this file was open for write, then erase contents */ |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 4380 | if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { |
| 4381 | int cpu = tracing_get_cpu(inode); |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 4382 | struct array_buffer *trace_buf = &tr->array_buffer; |
Bo Yan | 8dd33bc | 2017-09-18 10:03:35 -0700 | [diff] [blame] | 4383 | |
| 4384 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 4385 | if (tr->current_trace->print_max) |
| 4386 | trace_buf = &tr->max_buffer; |
| 4387 | #endif |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 4388 | |
| 4389 | if (cpu == RING_BUFFER_ALL_CPUS) |
Bo Yan | 8dd33bc | 2017-09-18 10:03:35 -0700 | [diff] [blame] | 4390 | tracing_reset_online_cpus(trace_buf); |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 4391 | else |
Steven Rostedt (VMware) | a47b53e | 2019-08-13 12:14:35 -0400 | [diff] [blame] | 4392 | tracing_reset_cpu(trace_buf, cpu); |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | if (file->f_mode & FMODE_READ) { |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 4396 | iter = __tracing_open(inode, file, false); |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 4397 | if (IS_ERR(iter)) |
| 4398 | ret = PTR_ERR(iter); |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 4399 | else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 4400 | iter->iter_flags |= TRACE_FILE_LAT_FMT; |
| 4401 | } |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 4402 | |
| 4403 | if (ret < 0) |
| 4404 | trace_array_put(tr); |
| 4405 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4406 | return ret; |
| 4407 | } |
| 4408 | |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 4409 | /* |
| 4410 | * Some tracers are not suitable for instance buffers. |
| 4411 | * A tracer is always available for the global array (toplevel) |
| 4412 | * or if it explicitly states that it is. |
| 4413 | */ |
| 4414 | static bool |
| 4415 | trace_ok_for_array(struct tracer *t, struct trace_array *tr) |
| 4416 | { |
| 4417 | return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances; |
| 4418 | } |
| 4419 | |
| 4420 | /* Find the next tracer that this trace array may use */ |
| 4421 | static struct tracer * |
| 4422 | get_tracer_for_array(struct trace_array *tr, struct tracer *t) |
| 4423 | { |
| 4424 | while (t && !trace_ok_for_array(t, tr)) |
| 4425 | t = t->next; |
| 4426 | |
| 4427 | return t; |
| 4428 | } |
| 4429 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 4430 | static void * |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4431 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 4432 | { |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 4433 | struct trace_array *tr = m->private; |
Li Zefan | f129e96 | 2009-06-24 09:53:44 +0800 | [diff] [blame] | 4434 | struct tracer *t = v; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4435 | |
| 4436 | (*pos)++; |
| 4437 | |
| 4438 | if (t) |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 4439 | t = get_tracer_for_array(tr, t->next); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4440 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4441 | return t; |
| 4442 | } |
| 4443 | |
| 4444 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 4445 | { |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 4446 | struct trace_array *tr = m->private; |
Li Zefan | f129e96 | 2009-06-24 09:53:44 +0800 | [diff] [blame] | 4447 | struct tracer *t; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4448 | loff_t l = 0; |
| 4449 | |
| 4450 | mutex_lock(&trace_types_lock); |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 4451 | |
| 4452 | t = get_tracer_for_array(tr, trace_types); |
| 4453 | for (; t && l < *pos; t = t_next(m, t, &l)) |
| 4454 | ; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4455 | |
| 4456 | return t; |
| 4457 | } |
| 4458 | |
| 4459 | static void t_stop(struct seq_file *m, void *p) |
| 4460 | { |
| 4461 | mutex_unlock(&trace_types_lock); |
| 4462 | } |
| 4463 | |
| 4464 | static int t_show(struct seq_file *m, void *v) |
| 4465 | { |
| 4466 | struct tracer *t = v; |
| 4467 | |
| 4468 | if (!t) |
| 4469 | return 0; |
| 4470 | |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 4471 | seq_puts(m, t->name); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4472 | if (t->next) |
| 4473 | seq_putc(m, ' '); |
| 4474 | else |
| 4475 | seq_putc(m, '\n'); |
| 4476 | |
| 4477 | return 0; |
| 4478 | } |
| 4479 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 4480 | static const struct seq_operations show_traces_seq_ops = { |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 4481 | .start = t_start, |
| 4482 | .next = t_next, |
| 4483 | .stop = t_stop, |
| 4484 | .show = t_show, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4485 | }; |
| 4486 | |
| 4487 | static int show_traces_open(struct inode *inode, struct file *file) |
| 4488 | { |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 4489 | struct trace_array *tr = inode->i_private; |
| 4490 | struct seq_file *m; |
| 4491 | int ret; |
| 4492 | |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 4493 | ret = tracing_check_open_get_tr(tr); |
| 4494 | if (ret) |
| 4495 | return ret; |
Steven Rostedt (VMware) | 194c2c7 | 2019-10-11 18:19:17 -0400 | [diff] [blame] | 4496 | |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 4497 | ret = seq_open(file, &show_traces_seq_ops); |
Steven Rostedt (VMware) | 194c2c7 | 2019-10-11 18:19:17 -0400 | [diff] [blame] | 4498 | if (ret) { |
| 4499 | trace_array_put(tr); |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 4500 | return ret; |
Steven Rostedt (VMware) | 194c2c7 | 2019-10-11 18:19:17 -0400 | [diff] [blame] | 4501 | } |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 4502 | |
| 4503 | m = file->private_data; |
| 4504 | m->private = tr; |
| 4505 | |
| 4506 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4507 | } |
| 4508 | |
Steven Rostedt (VMware) | 194c2c7 | 2019-10-11 18:19:17 -0400 | [diff] [blame] | 4509 | static int show_traces_release(struct inode *inode, struct file *file) |
| 4510 | { |
| 4511 | struct trace_array *tr = inode->i_private; |
| 4512 | |
| 4513 | trace_array_put(tr); |
| 4514 | return seq_release(inode, file); |
| 4515 | } |
| 4516 | |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 4517 | static ssize_t |
| 4518 | tracing_write_stub(struct file *filp, const char __user *ubuf, |
| 4519 | size_t count, loff_t *ppos) |
| 4520 | { |
| 4521 | return count; |
| 4522 | } |
| 4523 | |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4524 | loff_t tracing_lseek(struct file *file, loff_t offset, int whence) |
Slava Pestov | 364829b | 2010-11-24 15:13:16 -0800 | [diff] [blame] | 4525 | { |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4526 | int ret; |
| 4527 | |
Slava Pestov | 364829b | 2010-11-24 15:13:16 -0800 | [diff] [blame] | 4528 | if (file->f_mode & FMODE_READ) |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4529 | ret = seq_lseek(file, offset, whence); |
Slava Pestov | 364829b | 2010-11-24 15:13:16 -0800 | [diff] [blame] | 4530 | else |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4531 | file->f_pos = ret = 0; |
| 4532 | |
| 4533 | return ret; |
Slava Pestov | 364829b | 2010-11-24 15:13:16 -0800 | [diff] [blame] | 4534 | } |
| 4535 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4536 | static const struct file_operations tracing_fops = { |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 4537 | .open = tracing_open, |
| 4538 | .read = seq_read, |
Steven Rostedt | 4acd4d0 | 2009-03-18 10:40:24 -0400 | [diff] [blame] | 4539 | .write = tracing_write_stub, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 4540 | .llseek = tracing_lseek, |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 4541 | .release = tracing_release, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4542 | }; |
| 4543 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4544 | static const struct file_operations show_traces_fops = { |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4545 | .open = show_traces_open, |
| 4546 | .read = seq_read, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 4547 | .llseek = seq_lseek, |
Steven Rostedt (VMware) | 194c2c7 | 2019-10-11 18:19:17 -0400 | [diff] [blame] | 4548 | .release = show_traces_release, |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4549 | }; |
| 4550 | |
| 4551 | static ssize_t |
| 4552 | tracing_cpumask_read(struct file *filp, char __user *ubuf, |
| 4553 | size_t count, loff_t *ppos) |
| 4554 | { |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 4555 | struct trace_array *tr = file_inode(filp)->i_private; |
Changbin Du | 90e406f | 2017-11-30 11:39:43 +0800 | [diff] [blame] | 4556 | char *mask_str; |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4557 | int len; |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4558 | |
Changbin Du | 90e406f | 2017-11-30 11:39:43 +0800 | [diff] [blame] | 4559 | len = snprintf(NULL, 0, "%*pb\n", |
| 4560 | cpumask_pr_args(tr->tracing_cpumask)) + 1; |
| 4561 | mask_str = kmalloc(len, GFP_KERNEL); |
| 4562 | if (!mask_str) |
| 4563 | return -ENOMEM; |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4564 | |
Changbin Du | 90e406f | 2017-11-30 11:39:43 +0800 | [diff] [blame] | 4565 | len = snprintf(mask_str, len, "%*pb\n", |
Tejun Heo | 1a40243 | 2015-02-13 14:37:39 -0800 | [diff] [blame] | 4566 | cpumask_pr_args(tr->tracing_cpumask)); |
| 4567 | if (len >= count) { |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4568 | count = -EINVAL; |
| 4569 | goto out_err; |
| 4570 | } |
Changbin Du | 90e406f | 2017-11-30 11:39:43 +0800 | [diff] [blame] | 4571 | count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len); |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4572 | |
| 4573 | out_err: |
Changbin Du | 90e406f | 2017-11-30 11:39:43 +0800 | [diff] [blame] | 4574 | kfree(mask_str); |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4575 | |
| 4576 | return count; |
| 4577 | } |
| 4578 | |
Masami Hiramatsu | 9d15dbb | 2020-01-11 01:07:16 +0900 | [diff] [blame] | 4579 | int tracing_set_cpumask(struct trace_array *tr, |
| 4580 | cpumask_var_t tracing_cpumask_new) |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4581 | { |
Masami Hiramatsu | 9d15dbb | 2020-01-11 01:07:16 +0900 | [diff] [blame] | 4582 | int cpu; |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 4583 | |
Masami Hiramatsu | 9d15dbb | 2020-01-11 01:07:16 +0900 | [diff] [blame] | 4584 | if (!tr) |
| 4585 | return -EINVAL; |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4586 | |
Steven Rostedt | a5e2588 | 2008-12-02 15:34:05 -0500 | [diff] [blame] | 4587 | local_irq_disable(); |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 4588 | arch_spin_lock(&tr->max_lock); |
Steven Rostedt | ab46428 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 4589 | for_each_tracing_cpu(cpu) { |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4590 | /* |
| 4591 | * Increase/decrease the disabled counter if we are |
| 4592 | * about to flip a bit in the cpumask: |
| 4593 | */ |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 4594 | if (cpumask_test_cpu(cpu, tr->tracing_cpumask) && |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 4595 | !cpumask_test_cpu(cpu, tracing_cpumask_new)) { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 4596 | atomic_inc(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled); |
| 4597 | ring_buffer_record_disable_cpu(tr->array_buffer.buffer, cpu); |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4598 | } |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 4599 | if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) && |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 4600 | cpumask_test_cpu(cpu, tracing_cpumask_new)) { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 4601 | atomic_dec(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled); |
| 4602 | ring_buffer_record_enable_cpu(tr->array_buffer.buffer, cpu); |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4603 | } |
| 4604 | } |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 4605 | arch_spin_unlock(&tr->max_lock); |
Steven Rostedt | a5e2588 | 2008-12-02 15:34:05 -0500 | [diff] [blame] | 4606 | local_irq_enable(); |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4607 | |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 4608 | cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new); |
Masami Hiramatsu | 9d15dbb | 2020-01-11 01:07:16 +0900 | [diff] [blame] | 4609 | |
| 4610 | return 0; |
| 4611 | } |
| 4612 | |
| 4613 | static ssize_t |
| 4614 | tracing_cpumask_write(struct file *filp, const char __user *ubuf, |
| 4615 | size_t count, loff_t *ppos) |
| 4616 | { |
| 4617 | struct trace_array *tr = file_inode(filp)->i_private; |
| 4618 | cpumask_var_t tracing_cpumask_new; |
| 4619 | int err; |
| 4620 | |
| 4621 | if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL)) |
| 4622 | return -ENOMEM; |
| 4623 | |
| 4624 | err = cpumask_parse_user(ubuf, count, tracing_cpumask_new); |
| 4625 | if (err) |
| 4626 | goto err_free; |
| 4627 | |
| 4628 | err = tracing_set_cpumask(tr, tracing_cpumask_new); |
| 4629 | if (err) |
| 4630 | goto err_free; |
| 4631 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 4632 | free_cpumask_var(tracing_cpumask_new); |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4633 | |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4634 | return count; |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4635 | |
Masami Hiramatsu | 9d15dbb | 2020-01-11 01:07:16 +0900 | [diff] [blame] | 4636 | err_free: |
Li Zefan | 215368e | 2009-06-15 10:56:42 +0800 | [diff] [blame] | 4637 | free_cpumask_var(tracing_cpumask_new); |
Ingo Molnar | 36dfe92 | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4638 | |
| 4639 | return err; |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4640 | } |
| 4641 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4642 | static const struct file_operations tracing_cpumask_fops = { |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 4643 | .open = tracing_open_generic_tr, |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 4644 | .read = tracing_cpumask_read, |
| 4645 | .write = tracing_cpumask_write, |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 4646 | .release = tracing_release_generic_tr, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 4647 | .llseek = generic_file_llseek, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4648 | }; |
| 4649 | |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4650 | static int tracing_trace_options_show(struct seq_file *m, void *v) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4651 | { |
Steven Rostedt | d8e83d2 | 2009-02-26 23:55:58 -0500 | [diff] [blame] | 4652 | struct tracer_opt *trace_opts; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4653 | struct trace_array *tr = m->private; |
Steven Rostedt | d8e83d2 | 2009-02-26 23:55:58 -0500 | [diff] [blame] | 4654 | u32 tracer_flags; |
Steven Rostedt | d8e83d2 | 2009-02-26 23:55:58 -0500 | [diff] [blame] | 4655 | int i; |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4656 | |
Steven Rostedt | d8e83d2 | 2009-02-26 23:55:58 -0500 | [diff] [blame] | 4657 | mutex_lock(&trace_types_lock); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4658 | tracer_flags = tr->current_trace->flags->val; |
| 4659 | trace_opts = tr->current_trace->flags->opts; |
Steven Rostedt | d8e83d2 | 2009-02-26 23:55:58 -0500 | [diff] [blame] | 4660 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4661 | for (i = 0; trace_options[i]; i++) { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 4662 | if (tr->trace_flags & (1 << i)) |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4663 | seq_printf(m, "%s\n", trace_options[i]); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4664 | else |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4665 | seq_printf(m, "no%s\n", trace_options[i]); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4666 | } |
| 4667 | |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4668 | for (i = 0; trace_opts[i].name; i++) { |
| 4669 | if (tracer_flags & trace_opts[i].bit) |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4670 | seq_printf(m, "%s\n", trace_opts[i].name); |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4671 | else |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4672 | seq_printf(m, "no%s\n", trace_opts[i].name); |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4673 | } |
Steven Rostedt | d8e83d2 | 2009-02-26 23:55:58 -0500 | [diff] [blame] | 4674 | mutex_unlock(&trace_types_lock); |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4675 | |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4676 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4677 | } |
| 4678 | |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 4679 | static int __set_tracer_option(struct trace_array *tr, |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4680 | struct tracer_flags *tracer_flags, |
| 4681 | struct tracer_opt *opts, int neg) |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4682 | { |
Chunyu Hu | d39cdd2 | 2016-03-08 21:37:01 +0800 | [diff] [blame] | 4683 | struct tracer *trace = tracer_flags->trace; |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4684 | int ret; |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4685 | |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 4686 | ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg); |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4687 | if (ret) |
| 4688 | return ret; |
| 4689 | |
| 4690 | if (neg) |
Zhaolei | 7770841 | 2009-08-07 18:53:21 +0800 | [diff] [blame] | 4691 | tracer_flags->val &= ~opts->bit; |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4692 | else |
Zhaolei | 7770841 | 2009-08-07 18:53:21 +0800 | [diff] [blame] | 4693 | tracer_flags->val |= opts->bit; |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4694 | return 0; |
| 4695 | } |
| 4696 | |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4697 | /* Try to assign a tracer specific option */ |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 4698 | static int set_tracer_option(struct trace_array *tr, char *cmp, int neg) |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4699 | { |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 4700 | struct tracer *trace = tr->current_trace; |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4701 | struct tracer_flags *tracer_flags = trace->flags; |
| 4702 | struct tracer_opt *opts = NULL; |
| 4703 | int i; |
| 4704 | |
| 4705 | for (i = 0; tracer_flags->opts[i].name; i++) { |
| 4706 | opts = &tracer_flags->opts[i]; |
| 4707 | |
| 4708 | if (strcmp(cmp, opts->name) == 0) |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 4709 | return __set_tracer_option(tr, trace->flags, opts, neg); |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | return -EINVAL; |
| 4713 | } |
| 4714 | |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 4715 | /* Some tracers require overwrite to stay enabled */ |
| 4716 | int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set) |
| 4717 | { |
| 4718 | if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set) |
| 4719 | return -1; |
| 4720 | |
| 4721 | return 0; |
| 4722 | } |
| 4723 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4724 | int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled) |
Steven Rostedt | af4617b | 2009-03-17 18:09:55 -0400 | [diff] [blame] | 4725 | { |
Prateek Sood | 3a53acf | 2019-12-10 09:15:16 +0000 | [diff] [blame] | 4726 | if ((mask == TRACE_ITER_RECORD_TGID) || |
| 4727 | (mask == TRACE_ITER_RECORD_CMD)) |
| 4728 | lockdep_assert_held(&event_mutex); |
| 4729 | |
Steven Rostedt | af4617b | 2009-03-17 18:09:55 -0400 | [diff] [blame] | 4730 | /* do nothing if flag is already set */ |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 4731 | if (!!(tr->trace_flags & mask) == !!enabled) |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 4732 | return 0; |
| 4733 | |
| 4734 | /* Give the tracer a chance to approve the change */ |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4735 | if (tr->current_trace->flag_changed) |
Steven Rostedt (Red Hat) | bf6065b | 2014-01-10 17:51:01 -0500 | [diff] [blame] | 4736 | if (tr->current_trace->flag_changed(tr, mask, !!enabled)) |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 4737 | return -EINVAL; |
Steven Rostedt | af4617b | 2009-03-17 18:09:55 -0400 | [diff] [blame] | 4738 | |
| 4739 | if (enabled) |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 4740 | tr->trace_flags |= mask; |
Steven Rostedt | af4617b | 2009-03-17 18:09:55 -0400 | [diff] [blame] | 4741 | else |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 4742 | tr->trace_flags &= ~mask; |
Li Zefan | e870e9a | 2010-07-02 11:07:32 +0800 | [diff] [blame] | 4743 | |
| 4744 | if (mask == TRACE_ITER_RECORD_CMD) |
| 4745 | trace_event_enable_cmd_record(enabled); |
David Sharp | 750912f | 2010-12-08 13:46:47 -0800 | [diff] [blame] | 4746 | |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 4747 | if (mask == TRACE_ITER_RECORD_TGID) { |
| 4748 | if (!tgid_map) |
Yuming Han | 6ee4051 | 2019-10-24 11:34:30 +0800 | [diff] [blame] | 4749 | tgid_map = kvcalloc(PID_MAX_DEFAULT + 1, |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 4750 | sizeof(*tgid_map), |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 4751 | GFP_KERNEL); |
| 4752 | if (!tgid_map) { |
| 4753 | tr->trace_flags &= ~TRACE_ITER_RECORD_TGID; |
| 4754 | return -ENOMEM; |
| 4755 | } |
| 4756 | |
| 4757 | trace_event_enable_tgid_record(enabled); |
| 4758 | } |
| 4759 | |
Steven Rostedt | c37775d | 2016-04-13 16:59:18 -0400 | [diff] [blame] | 4760 | if (mask == TRACE_ITER_EVENT_FORK) |
| 4761 | trace_event_follow_fork(tr, enabled); |
| 4762 | |
Namhyung Kim | 1e10486 | 2017-04-17 11:44:28 +0900 | [diff] [blame] | 4763 | if (mask == TRACE_ITER_FUNC_FORK) |
| 4764 | ftrace_pid_follow_fork(tr, enabled); |
| 4765 | |
Steven Rostedt (Red Hat) | 8090282 | 2013-03-14 14:20:54 -0400 | [diff] [blame] | 4766 | if (mask == TRACE_ITER_OVERWRITE) { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 4767 | ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled); |
Steven Rostedt (Red Hat) | 8090282 | 2013-03-14 14:20:54 -0400 | [diff] [blame] | 4768 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 4769 | ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled); |
Steven Rostedt (Red Hat) | 8090282 | 2013-03-14 14:20:54 -0400 | [diff] [blame] | 4770 | #endif |
| 4771 | } |
Steven Rostedt | 8169883 | 2012-10-11 10:15:05 -0400 | [diff] [blame] | 4772 | |
Steven Rostedt (Red Hat) | b9f9108 | 2015-09-29 18:21:35 -0400 | [diff] [blame] | 4773 | if (mask == TRACE_ITER_PRINTK) { |
Steven Rostedt | 8169883 | 2012-10-11 10:15:05 -0400 | [diff] [blame] | 4774 | trace_printk_start_stop_comm(enabled); |
Steven Rostedt (Red Hat) | b9f9108 | 2015-09-29 18:21:35 -0400 | [diff] [blame] | 4775 | trace_printk_control(enabled); |
| 4776 | } |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 4777 | |
| 4778 | return 0; |
Steven Rostedt | af4617b | 2009-03-17 18:09:55 -0400 | [diff] [blame] | 4779 | } |
| 4780 | |
Masami Hiramatsu | 9c5b9d3 | 2020-01-11 01:06:17 +0900 | [diff] [blame] | 4781 | int trace_set_options(struct trace_array *tr, char *option) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4782 | { |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 4783 | char *cmp; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4784 | int neg = 0; |
Yisheng Xie | 591a033 | 2018-05-17 16:36:03 +0800 | [diff] [blame] | 4785 | int ret; |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 4786 | size_t orig_len = strlen(option); |
Steven Rostedt (VMware) | 3d739c1 | 2018-12-21 23:10:26 -0500 | [diff] [blame] | 4787 | int len; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4788 | |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 4789 | cmp = strstrip(option); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4790 | |
Steven Rostedt (VMware) | 3d739c1 | 2018-12-21 23:10:26 -0500 | [diff] [blame] | 4791 | len = str_has_prefix(cmp, "no"); |
| 4792 | if (len) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4793 | neg = 1; |
Steven Rostedt (VMware) | 3d739c1 | 2018-12-21 23:10:26 -0500 | [diff] [blame] | 4794 | |
| 4795 | cmp += len; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4796 | |
Prateek Sood | 3a53acf | 2019-12-10 09:15:16 +0000 | [diff] [blame] | 4797 | mutex_lock(&event_mutex); |
Steven Rostedt (Red Hat) | 69d34da | 2013-03-14 13:50:56 -0400 | [diff] [blame] | 4798 | mutex_lock(&trace_types_lock); |
| 4799 | |
Yisheng Xie | 591a033 | 2018-05-17 16:36:03 +0800 | [diff] [blame] | 4800 | ret = match_string(trace_options, -1, cmp); |
Frederic Weisbecker | adf9f19 | 2008-11-17 19:23:42 +0100 | [diff] [blame] | 4801 | /* If no option could be set, test the specific tracer options */ |
Yisheng Xie | 591a033 | 2018-05-17 16:36:03 +0800 | [diff] [blame] | 4802 | if (ret < 0) |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 4803 | ret = set_tracer_option(tr, cmp, neg); |
Yisheng Xie | 591a033 | 2018-05-17 16:36:03 +0800 | [diff] [blame] | 4804 | else |
| 4805 | ret = set_tracer_flag(tr, 1 << ret, !neg); |
Steven Rostedt (Red Hat) | 69d34da | 2013-03-14 13:50:56 -0400 | [diff] [blame] | 4806 | |
| 4807 | mutex_unlock(&trace_types_lock); |
Prateek Sood | 3a53acf | 2019-12-10 09:15:16 +0000 | [diff] [blame] | 4808 | mutex_unlock(&event_mutex); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4809 | |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 4810 | /* |
| 4811 | * If the first trailing whitespace is replaced with '\0' by strstrip, |
| 4812 | * turn it back into a space. |
| 4813 | */ |
| 4814 | if (orig_len > strlen(option)) |
| 4815 | option[strlen(option)] = ' '; |
| 4816 | |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 4817 | return ret; |
| 4818 | } |
| 4819 | |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 4820 | static void __init apply_trace_boot_options(void) |
| 4821 | { |
| 4822 | char *buf = trace_boot_options_buf; |
| 4823 | char *option; |
| 4824 | |
| 4825 | while (true) { |
| 4826 | option = strsep(&buf, ","); |
| 4827 | |
| 4828 | if (!option) |
| 4829 | break; |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 4830 | |
Steven Rostedt (Red Hat) | 43ed384 | 2015-11-03 22:15:14 -0500 | [diff] [blame] | 4831 | if (*option) |
| 4832 | trace_set_options(&global_trace, option); |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 4833 | |
| 4834 | /* Put back the comma to allow this to be called again */ |
| 4835 | if (buf) |
| 4836 | *(buf - 1) = ','; |
| 4837 | } |
| 4838 | } |
| 4839 | |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 4840 | static ssize_t |
| 4841 | tracing_trace_options_write(struct file *filp, const char __user *ubuf, |
| 4842 | size_t cnt, loff_t *ppos) |
| 4843 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4844 | struct seq_file *m = filp->private_data; |
| 4845 | struct trace_array *tr = m->private; |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 4846 | char buf[64]; |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 4847 | int ret; |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 4848 | |
| 4849 | if (cnt >= sizeof(buf)) |
| 4850 | return -EINVAL; |
| 4851 | |
Wang Xiaoqiang | 4afe649 | 2016-04-18 15:23:29 +0800 | [diff] [blame] | 4852 | if (copy_from_user(buf, ubuf, cnt)) |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 4853 | return -EFAULT; |
| 4854 | |
Steven Rostedt | a8dd217 | 2013-01-09 20:54:17 -0500 | [diff] [blame] | 4855 | buf[cnt] = 0; |
| 4856 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 4857 | ret = trace_set_options(tr, buf); |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 4858 | if (ret < 0) |
| 4859 | return ret; |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 4860 | |
Jiri Olsa | cf8517c | 2009-10-23 19:36:16 -0400 | [diff] [blame] | 4861 | *ppos += cnt; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4862 | |
| 4863 | return cnt; |
| 4864 | } |
| 4865 | |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4866 | static int tracing_trace_options_open(struct inode *inode, struct file *file) |
| 4867 | { |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4868 | struct trace_array *tr = inode->i_private; |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 4869 | int ret; |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4870 | |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 4871 | ret = tracing_check_open_get_tr(tr); |
| 4872 | if (ret) |
| 4873 | return ret; |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4874 | |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 4875 | ret = single_open(file, tracing_trace_options_show, inode->i_private); |
| 4876 | if (ret < 0) |
| 4877 | trace_array_put(tr); |
| 4878 | |
| 4879 | return ret; |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4880 | } |
| 4881 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 4882 | static const struct file_operations tracing_iter_fops = { |
Li Zefan | fdb372e | 2009-12-08 11:15:59 +0800 | [diff] [blame] | 4883 | .open = tracing_trace_options_open, |
| 4884 | .read = seq_read, |
| 4885 | .llseek = seq_lseek, |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 4886 | .release = tracing_single_release_tr, |
Steven Rostedt | ee6bce5 | 2008-11-12 17:52:37 -0500 | [diff] [blame] | 4887 | .write = tracing_trace_options_write, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 4888 | }; |
| 4889 | |
Ingo Molnar | 7bd2f24 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 4890 | static const char readme_msg[] = |
| 4891 | "tracing mini-HOWTO:\n\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4892 | "# echo 0 > tracing_on : quick way to disable tracing\n" |
| 4893 | "# echo 1 > tracing_on : quick way to re-enable tracing\n\n" |
| 4894 | " Important files:\n" |
| 4895 | " trace\t\t\t- The static contents of the buffer\n" |
| 4896 | "\t\t\t To clear the buffer write into this file: echo > trace\n" |
| 4897 | " trace_pipe\t\t- A consuming read to see the contents of the buffer\n" |
| 4898 | " current_tracer\t- function and latency tracers\n" |
| 4899 | " available_tracers\t- list of configured tracers for current_tracer\n" |
Tom Zanussi | a8d6557 | 2019-03-31 18:48:25 -0500 | [diff] [blame] | 4900 | " error_log\t- error log for failed commands (that support it)\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4901 | " buffer_size_kb\t- view and modify size of per cpu buffer\n" |
| 4902 | " buffer_total_size_kb - view total size of all cpu buffers\n\n" |
| 4903 | " trace_clock\t\t-change the clock used to order events\n" |
| 4904 | " local: Per cpu clock but may not be synced across CPUs\n" |
| 4905 | " global: Synced across CPUs but slows tracing down.\n" |
| 4906 | " counter: Not a clock, but just an increment\n" |
| 4907 | " uptime: Jiffy counter from time of boot\n" |
| 4908 | " perf: Same clock that perf events use\n" |
| 4909 | #ifdef CONFIG_X86_64 |
| 4910 | " x86-tsc: TSC cycle counter\n" |
| 4911 | #endif |
Tom Zanussi | 2c1ea60 | 2018-01-15 20:51:41 -0600 | [diff] [blame] | 4912 | "\n timestamp_mode\t-view the mode used to timestamp events\n" |
| 4913 | " delta: Delta difference against a buffer-wide timestamp\n" |
| 4914 | " absolute: Absolute (standalone) timestamp\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4915 | "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n" |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 4916 | "\n trace_marker_raw\t\t- Writes into this file writes binary data into the kernel buffer\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4917 | " tracing_cpumask\t- Limit which CPUs to trace\n" |
| 4918 | " instances\t\t- Make sub-buffers with: mkdir instances/foo\n" |
| 4919 | "\t\t\t Remove sub-buffer with rmdir\n" |
| 4920 | " trace_options\t\t- Set format or modify how tracing happens\n" |
Srivatsa S. Bhat (VMware) | b941699 | 2019-01-28 17:55:53 -0800 | [diff] [blame] | 4921 | "\t\t\t Disable an option by prefixing 'no' to the\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4922 | "\t\t\t option name\n" |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 4923 | " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4924 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 4925 | "\n available_filter_functions - list of functions that can be filtered on\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4926 | " set_ftrace_filter\t- echo function name in here to only trace these\n" |
| 4927 | "\t\t\t functions\n" |
Masami Hiramatsu | 60f1d5e | 2016-10-05 20:58:15 +0900 | [diff] [blame] | 4928 | "\t accepts: func_full_name or glob-matching-pattern\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4929 | "\t modules: Can select a group via module\n" |
| 4930 | "\t Format: :mod:<module-name>\n" |
| 4931 | "\t example: echo :mod:ext3 > set_ftrace_filter\n" |
| 4932 | "\t triggers: a command to perform when function is hit\n" |
| 4933 | "\t Format: <function>:<trigger>[:count]\n" |
| 4934 | "\t trigger: traceon, traceoff\n" |
| 4935 | "\t\t enable_event:<system>:<event>\n" |
| 4936 | "\t\t disable_event:<system>:<event>\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4937 | #ifdef CONFIG_STACKTRACE |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4938 | "\t\t stacktrace\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4939 | #endif |
| 4940 | #ifdef CONFIG_TRACER_SNAPSHOT |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4941 | "\t\t snapshot\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4942 | #endif |
Steven Rostedt (Red Hat) | 17a280e | 2014-04-10 22:43:37 -0400 | [diff] [blame] | 4943 | "\t\t dump\n" |
| 4944 | "\t\t cpudump\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4945 | "\t example: echo do_fault:traceoff > set_ftrace_filter\n" |
| 4946 | "\t echo do_trap:traceoff:3 > set_ftrace_filter\n" |
| 4947 | "\t The first one will disable tracing every time do_fault is hit\n" |
| 4948 | "\t The second will disable tracing at most 3 times when do_trap is hit\n" |
| 4949 | "\t The first time do trap is hit and it disables tracing, the\n" |
| 4950 | "\t counter will decrement to 2. If tracing is already disabled,\n" |
| 4951 | "\t the counter will not decrement. It only decrements when the\n" |
| 4952 | "\t trigger did work\n" |
| 4953 | "\t To remove trigger without count:\n" |
| 4954 | "\t echo '!<function>:<trigger> > set_ftrace_filter\n" |
| 4955 | "\t To remove trigger with a count:\n" |
| 4956 | "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4957 | " set_ftrace_notrace\t- echo function name in here to never trace.\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4958 | "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n" |
| 4959 | "\t modules: Can select a group via module command :mod:\n" |
| 4960 | "\t Does not accept triggers\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4961 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 4962 | #ifdef CONFIG_FUNCTION_TRACER |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4963 | " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n" |
| 4964 | "\t\t (function)\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4965 | #endif |
| 4966 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 4967 | " set_graph_function\t- Trace the nested calls of a function (function_graph)\n" |
Namhyung Kim | d048a8c7 | 2014-06-13 01:23:53 +0900 | [diff] [blame] | 4968 | " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4969 | " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n" |
| 4970 | #endif |
| 4971 | #ifdef CONFIG_TRACER_SNAPSHOT |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4972 | "\n snapshot\t\t- Like 'trace' but shows the content of the static\n" |
| 4973 | "\t\t\t snapshot buffer. Read the contents for more\n" |
| 4974 | "\t\t\t information\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4975 | #endif |
zhangwei(Jovi) | 991821c | 2013-07-15 16:32:34 +0800 | [diff] [blame] | 4976 | #ifdef CONFIG_STACK_TRACER |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4977 | " stack_trace\t\t- Shows the max stack trace when active\n" |
| 4978 | " stack_max_size\t- Shows current max stack size that was traced\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4979 | "\t\t\t Write into this file to reset the max size (trigger a\n" |
| 4980 | "\t\t\t new trace)\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4981 | #ifdef CONFIG_DYNAMIC_FTRACE |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 4982 | " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n" |
| 4983 | "\t\t\t traces\n" |
Steven Rostedt (Red Hat) | 22f4564 | 2013-03-15 17:23:20 -0400 | [diff] [blame] | 4984 | #endif |
zhangwei(Jovi) | 991821c | 2013-07-15 16:32:34 +0800 | [diff] [blame] | 4985 | #endif /* CONFIG_STACK_TRACER */ |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 4986 | #ifdef CONFIG_DYNAMIC_EVENTS |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 4987 | " dynamic_events\t\t- Create/append/remove/show the generic dynamic events\n" |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 4988 | "\t\t\t Write into this file to define/undefine new trace events.\n" |
| 4989 | #endif |
Anton Blanchard | 6b0b755 | 2017-02-16 17:00:50 +1100 | [diff] [blame] | 4990 | #ifdef CONFIG_KPROBE_EVENTS |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 4991 | " kprobe_events\t\t- Create/append/remove/show the kernel dynamic events\n" |
Masami Hiramatsu | 8642562 | 2016-08-18 17:58:15 +0900 | [diff] [blame] | 4992 | "\t\t\t Write into this file to define/undefine new trace events.\n" |
| 4993 | #endif |
Anton Blanchard | 6b0b755 | 2017-02-16 17:00:50 +1100 | [diff] [blame] | 4994 | #ifdef CONFIG_UPROBE_EVENTS |
Masami Hiramatsu | 41af3cf | 2019-06-20 00:07:58 +0900 | [diff] [blame] | 4995 | " uprobe_events\t\t- Create/append/remove/show the userspace dynamic events\n" |
Masami Hiramatsu | 8642562 | 2016-08-18 17:58:15 +0900 | [diff] [blame] | 4996 | "\t\t\t Write into this file to define/undefine new trace events.\n" |
| 4997 | #endif |
Anton Blanchard | 6b0b755 | 2017-02-16 17:00:50 +1100 | [diff] [blame] | 4998 | #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS) |
Masami Hiramatsu | 8642562 | 2016-08-18 17:58:15 +0900 | [diff] [blame] | 4999 | "\t accepts: event-definitions (one definition per line)\n" |
Masami Hiramatsu | c3ca46e | 2017-05-23 15:05:50 +0900 | [diff] [blame] | 5000 | "\t Format: p[:[<group>/]<event>] <place> [<args>]\n" |
| 5001 | "\t r[maxactive][:[<group>/]<event>] <place> [<args>]\n" |
Masami Hiramatsu | 7bbab38 | 2018-11-05 18:03:33 +0900 | [diff] [blame] | 5002 | #ifdef CONFIG_HIST_TRIGGERS |
| 5003 | "\t s:[synthetic/]<event> <field> [<field>]\n" |
| 5004 | #endif |
Masami Hiramatsu | 8642562 | 2016-08-18 17:58:15 +0900 | [diff] [blame] | 5005 | "\t -:[<group>/]<event>\n" |
Anton Blanchard | 6b0b755 | 2017-02-16 17:00:50 +1100 | [diff] [blame] | 5006 | #ifdef CONFIG_KPROBE_EVENTS |
Masami Hiramatsu | 8642562 | 2016-08-18 17:58:15 +0900 | [diff] [blame] | 5007 | "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n" |
Naveen N. Rao | 35b6f55 | 2017-02-22 19:23:39 +0530 | [diff] [blame] | 5008 | "place (kretprobe): [<module>:]<symbol>[+<offset>]|<memaddr>\n" |
Masami Hiramatsu | 8642562 | 2016-08-18 17:58:15 +0900 | [diff] [blame] | 5009 | #endif |
Anton Blanchard | 6b0b755 | 2017-02-16 17:00:50 +1100 | [diff] [blame] | 5010 | #ifdef CONFIG_UPROBE_EVENTS |
Ravi Bangoria | 1cc3316 | 2018-08-20 10:12:47 +0530 | [diff] [blame] | 5011 | " place (uprobe): <path>:<offset>[(ref_ctr_offset)]\n" |
Masami Hiramatsu | 8642562 | 2016-08-18 17:58:15 +0900 | [diff] [blame] | 5012 | #endif |
| 5013 | "\t args: <name>=fetcharg[:type]\n" |
| 5014 | "\t fetcharg: %<register>, @<address>, @<symbol>[+|-<offset>],\n" |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 5015 | #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 5016 | "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n" |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 5017 | #else |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 5018 | "\t $stack<index>, $stack, $retval, $comm,\n" |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 5019 | #endif |
Masami Hiramatsu | a42e3c4 | 2019-06-20 00:08:37 +0900 | [diff] [blame] | 5020 | "\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n" |
Masami Hiramatsu | 60c2e0c | 2018-04-25 21:20:28 +0900 | [diff] [blame] | 5021 | "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n" |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 5022 | "\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n" |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 5023 | "\t <type>\\[<array-size>\\]\n" |
Masami Hiramatsu | 7bbab38 | 2018-11-05 18:03:33 +0900 | [diff] [blame] | 5024 | #ifdef CONFIG_HIST_TRIGGERS |
| 5025 | "\t field: <stype> <name>;\n" |
| 5026 | "\t stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n" |
| 5027 | "\t [unsigned] char/int/long\n" |
| 5028 | #endif |
Masami Hiramatsu | 8642562 | 2016-08-18 17:58:15 +0900 | [diff] [blame] | 5029 | #endif |
Tom Zanussi | 26f2556 | 2014-01-17 15:11:44 -0600 | [diff] [blame] | 5030 | " events/\t\t- Directory containing all trace event subsystems:\n" |
| 5031 | " enable\t\t- Write 0/1 to enable/disable tracing of all events\n" |
| 5032 | " events/<system>/\t- Directory containing all trace events for <system>:\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 5033 | " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n" |
| 5034 | "\t\t\t events\n" |
Tom Zanussi | 26f2556 | 2014-01-17 15:11:44 -0600 | [diff] [blame] | 5035 | " filter\t\t- If set, only events passing filter are traced\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 5036 | " events/<system>/<event>/\t- Directory containing control files for\n" |
| 5037 | "\t\t\t <event>:\n" |
Tom Zanussi | 26f2556 | 2014-01-17 15:11:44 -0600 | [diff] [blame] | 5038 | " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n" |
| 5039 | " filter\t\t- If set, only events passing filter are traced\n" |
| 5040 | " trigger\t\t- If set, a command to perform when event is hit\n" |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 5041 | "\t Format: <trigger>[:count][if <filter>]\n" |
| 5042 | "\t trigger: traceon, traceoff\n" |
| 5043 | "\t enable_event:<system>:<event>\n" |
| 5044 | "\t disable_event:<system>:<event>\n" |
Tom Zanussi | d0bad49 | 2016-03-03 12:54:55 -0600 | [diff] [blame] | 5045 | #ifdef CONFIG_HIST_TRIGGERS |
| 5046 | "\t enable_hist:<system>:<event>\n" |
| 5047 | "\t disable_hist:<system>:<event>\n" |
| 5048 | #endif |
Tom Zanussi | 26f2556 | 2014-01-17 15:11:44 -0600 | [diff] [blame] | 5049 | #ifdef CONFIG_STACKTRACE |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 5050 | "\t\t stacktrace\n" |
Tom Zanussi | 26f2556 | 2014-01-17 15:11:44 -0600 | [diff] [blame] | 5051 | #endif |
| 5052 | #ifdef CONFIG_TRACER_SNAPSHOT |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 5053 | "\t\t snapshot\n" |
Tom Zanussi | 26f2556 | 2014-01-17 15:11:44 -0600 | [diff] [blame] | 5054 | #endif |
Tom Zanussi | 7ef224d | 2016-03-03 12:54:42 -0600 | [diff] [blame] | 5055 | #ifdef CONFIG_HIST_TRIGGERS |
| 5056 | "\t\t hist (see below)\n" |
| 5057 | #endif |
Steven Rostedt (Red Hat) | 71485c4 | 2014-01-23 00:10:04 -0500 | [diff] [blame] | 5058 | "\t example: echo traceoff > events/block/block_unplug/trigger\n" |
| 5059 | "\t echo traceoff:3 > events/block/block_unplug/trigger\n" |
| 5060 | "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n" |
| 5061 | "\t events/block/block_unplug/trigger\n" |
| 5062 | "\t The first disables tracing every time block_unplug is hit.\n" |
| 5063 | "\t The second disables tracing the first 3 times block_unplug is hit.\n" |
| 5064 | "\t The third enables the kmalloc event the first 3 times block_unplug\n" |
| 5065 | "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n" |
| 5066 | "\t Like function triggers, the counter is only decremented if it\n" |
| 5067 | "\t enabled or disabled tracing.\n" |
| 5068 | "\t To remove a trigger without a count:\n" |
| 5069 | "\t echo '!<trigger> > <system>/<event>/trigger\n" |
| 5070 | "\t To remove a trigger with a count:\n" |
| 5071 | "\t echo '!<trigger>:0 > <system>/<event>/trigger\n" |
| 5072 | "\t Filters can be ignored when removing a trigger.\n" |
Tom Zanussi | 7ef224d | 2016-03-03 12:54:42 -0600 | [diff] [blame] | 5073 | #ifdef CONFIG_HIST_TRIGGERS |
| 5074 | " hist trigger\t- If set, event hits are aggregated into a hash table\n" |
Tom Zanussi | 76a3b0c | 2016-03-03 12:54:44 -0600 | [diff] [blame] | 5075 | "\t Format: hist:keys=<field1[,field2,...]>\n" |
Tom Zanussi | f260683 | 2016-03-03 12:54:43 -0600 | [diff] [blame] | 5076 | "\t [:values=<field1[,field2,...]>]\n" |
Tom Zanussi | e62347d | 2016-03-03 12:54:45 -0600 | [diff] [blame] | 5077 | "\t [:sort=<field1[,field2,...]>]\n" |
Tom Zanussi | 7ef224d | 2016-03-03 12:54:42 -0600 | [diff] [blame] | 5078 | "\t [:size=#entries]\n" |
Tom Zanussi | e86ae9b | 2016-03-03 12:54:47 -0600 | [diff] [blame] | 5079 | "\t [:pause][:continue][:clear]\n" |
Tom Zanussi | 5463bfd | 2016-03-03 12:54:59 -0600 | [diff] [blame] | 5080 | "\t [:name=histname1]\n" |
Tom Zanussi | c3e4950 | 2019-02-13 17:42:43 -0600 | [diff] [blame] | 5081 | "\t [:<handler>.<action>]\n" |
Tom Zanussi | 7ef224d | 2016-03-03 12:54:42 -0600 | [diff] [blame] | 5082 | "\t [if <filter>]\n\n" |
| 5083 | "\t When a matching event is hit, an entry is added to a hash\n" |
Tom Zanussi | f260683 | 2016-03-03 12:54:43 -0600 | [diff] [blame] | 5084 | "\t table using the key(s) and value(s) named, and the value of a\n" |
| 5085 | "\t sum called 'hitcount' is incremented. Keys and values\n" |
| 5086 | "\t correspond to fields in the event's format description. Keys\n" |
Tom Zanussi | 69a0200 | 2016-03-03 12:54:52 -0600 | [diff] [blame] | 5087 | "\t can be any field, or the special string 'stacktrace'.\n" |
| 5088 | "\t Compound keys consisting of up to two fields can be specified\n" |
| 5089 | "\t by the 'keys' keyword. Values must correspond to numeric\n" |
| 5090 | "\t fields. Sort keys consisting of up to two fields can be\n" |
| 5091 | "\t specified using the 'sort' keyword. The sort direction can\n" |
| 5092 | "\t be modified by appending '.descending' or '.ascending' to a\n" |
| 5093 | "\t sort field. The 'size' parameter can be used to specify more\n" |
Tom Zanussi | 5463bfd | 2016-03-03 12:54:59 -0600 | [diff] [blame] | 5094 | "\t or fewer than the default 2048 entries for the hashtable size.\n" |
| 5095 | "\t If a hist trigger is given a name using the 'name' parameter,\n" |
| 5096 | "\t its histogram data will be shared with other triggers of the\n" |
| 5097 | "\t same name, and trigger hits will update this common data.\n\n" |
Tom Zanussi | 7ef224d | 2016-03-03 12:54:42 -0600 | [diff] [blame] | 5098 | "\t Reading the 'hist' file for the event will dump the hash\n" |
Tom Zanussi | 52a7f16 | 2016-03-03 12:54:57 -0600 | [diff] [blame] | 5099 | "\t table in its entirety to stdout. If there are multiple hist\n" |
| 5100 | "\t triggers attached to an event, there will be a table for each\n" |
Tom Zanussi | 5463bfd | 2016-03-03 12:54:59 -0600 | [diff] [blame] | 5101 | "\t trigger in the output. The table displayed for a named\n" |
| 5102 | "\t trigger will be the same as any other instance having the\n" |
| 5103 | "\t same name. The default format used to display a given field\n" |
| 5104 | "\t can be modified by appending any of the following modifiers\n" |
| 5105 | "\t to the field name, as applicable:\n\n" |
Tom Zanussi | c6afad4 | 2016-03-03 12:54:49 -0600 | [diff] [blame] | 5106 | "\t .hex display a number as a hex value\n" |
| 5107 | "\t .sym display an address as a symbol\n" |
Tom Zanussi | 6b4827a | 2016-03-03 12:54:50 -0600 | [diff] [blame] | 5108 | "\t .sym-offset display an address as a symbol and offset\n" |
Tom Zanussi | 3169619 | 2016-03-03 12:54:51 -0600 | [diff] [blame] | 5109 | "\t .execname display a common_pid as a program name\n" |
Tom Zanussi | 860f9f6 | 2018-01-15 20:51:48 -0600 | [diff] [blame] | 5110 | "\t .syscall display a syscall id as a syscall name\n" |
| 5111 | "\t .log2 display log2 value rather than raw number\n" |
| 5112 | "\t .usecs display a common_timestamp in microseconds\n\n" |
Tom Zanussi | 83e9991 | 2016-03-03 12:54:46 -0600 | [diff] [blame] | 5113 | "\t The 'pause' parameter can be used to pause an existing hist\n" |
| 5114 | "\t trigger or to start a hist trigger but not log any events\n" |
| 5115 | "\t until told to do so. 'continue' can be used to start or\n" |
| 5116 | "\t restart a paused hist trigger.\n\n" |
Tom Zanussi | e86ae9b | 2016-03-03 12:54:47 -0600 | [diff] [blame] | 5117 | "\t The 'clear' parameter will clear the contents of a running\n" |
| 5118 | "\t hist trigger and leave its current paused/active state\n" |
| 5119 | "\t unchanged.\n\n" |
Tom Zanussi | d0bad49 | 2016-03-03 12:54:55 -0600 | [diff] [blame] | 5120 | "\t The enable_hist and disable_hist triggers can be used to\n" |
| 5121 | "\t have one event conditionally start and stop another event's\n" |
Colin Ian King | 9e5a36a | 2019-02-17 22:32:22 +0000 | [diff] [blame] | 5122 | "\t already-attached hist trigger. The syntax is analogous to\n" |
Tom Zanussi | c3e4950 | 2019-02-13 17:42:43 -0600 | [diff] [blame] | 5123 | "\t the enable_event and disable_event triggers.\n\n" |
| 5124 | "\t Hist trigger handlers and actions are executed whenever a\n" |
| 5125 | "\t a histogram entry is added or updated. They take the form:\n\n" |
| 5126 | "\t <handler>.<action>\n\n" |
| 5127 | "\t The available handlers are:\n\n" |
| 5128 | "\t onmatch(matching.event) - invoke on addition or update\n" |
Tom Zanussi | dff81f5 | 2019-02-13 17:42:48 -0600 | [diff] [blame] | 5129 | "\t onmax(var) - invoke if var exceeds current max\n" |
| 5130 | "\t onchange(var) - invoke action if var changes\n\n" |
Tom Zanussi | c3e4950 | 2019-02-13 17:42:43 -0600 | [diff] [blame] | 5131 | "\t The available actions are:\n\n" |
Tom Zanussi | e91eefd7 | 2019-02-13 17:42:50 -0600 | [diff] [blame] | 5132 | "\t trace(<synthetic_event>,param list) - generate synthetic event\n" |
Tom Zanussi | c3e4950 | 2019-02-13 17:42:43 -0600 | [diff] [blame] | 5133 | "\t save(field,...) - save current event fields\n" |
Tom Zanussi | a3785b7 | 2019-02-13 17:42:46 -0600 | [diff] [blame] | 5134 | #ifdef CONFIG_TRACER_SNAPSHOT |
| 5135 | "\t snapshot() - snapshot the trace buffer\n" |
| 5136 | #endif |
Tom Zanussi | 7ef224d | 2016-03-03 12:54:42 -0600 | [diff] [blame] | 5137 | #endif |
Ingo Molnar | 7bd2f24 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 5138 | ; |
| 5139 | |
| 5140 | static ssize_t |
| 5141 | tracing_readme_read(struct file *filp, char __user *ubuf, |
| 5142 | size_t cnt, loff_t *ppos) |
| 5143 | { |
| 5144 | return simple_read_from_buffer(ubuf, cnt, ppos, |
| 5145 | readme_msg, strlen(readme_msg)); |
| 5146 | } |
| 5147 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 5148 | static const struct file_operations tracing_readme_fops = { |
Ingo Molnar | c7078de | 2008-05-12 21:20:52 +0200 | [diff] [blame] | 5149 | .open = tracing_open_generic, |
| 5150 | .read = tracing_readme_read, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 5151 | .llseek = generic_file_llseek, |
Ingo Molnar | 7bd2f24 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 5152 | }; |
| 5153 | |
Michael Sartain | 99c621d | 2017-07-05 22:07:15 -0600 | [diff] [blame] | 5154 | static void *saved_tgids_next(struct seq_file *m, void *v, loff_t *pos) |
| 5155 | { |
| 5156 | int *ptr = v; |
| 5157 | |
| 5158 | if (*pos || m->count) |
| 5159 | ptr++; |
| 5160 | |
| 5161 | (*pos)++; |
| 5162 | |
| 5163 | for (; ptr <= &tgid_map[PID_MAX_DEFAULT]; ptr++) { |
| 5164 | if (trace_find_tgid(*ptr)) |
| 5165 | return ptr; |
| 5166 | } |
| 5167 | |
| 5168 | return NULL; |
| 5169 | } |
| 5170 | |
| 5171 | static void *saved_tgids_start(struct seq_file *m, loff_t *pos) |
| 5172 | { |
| 5173 | void *v; |
| 5174 | loff_t l = 0; |
| 5175 | |
| 5176 | if (!tgid_map) |
| 5177 | return NULL; |
| 5178 | |
| 5179 | v = &tgid_map[0]; |
| 5180 | while (l <= *pos) { |
| 5181 | v = saved_tgids_next(m, v, &l); |
| 5182 | if (!v) |
| 5183 | return NULL; |
| 5184 | } |
| 5185 | |
| 5186 | return v; |
| 5187 | } |
| 5188 | |
| 5189 | static void saved_tgids_stop(struct seq_file *m, void *v) |
| 5190 | { |
| 5191 | } |
| 5192 | |
| 5193 | static int saved_tgids_show(struct seq_file *m, void *v) |
| 5194 | { |
| 5195 | int pid = (int *)v - tgid_map; |
| 5196 | |
| 5197 | seq_printf(m, "%d %d\n", pid, trace_find_tgid(pid)); |
| 5198 | return 0; |
| 5199 | } |
| 5200 | |
| 5201 | static const struct seq_operations tracing_saved_tgids_seq_ops = { |
| 5202 | .start = saved_tgids_start, |
| 5203 | .stop = saved_tgids_stop, |
| 5204 | .next = saved_tgids_next, |
| 5205 | .show = saved_tgids_show, |
| 5206 | }; |
| 5207 | |
| 5208 | static int tracing_saved_tgids_open(struct inode *inode, struct file *filp) |
| 5209 | { |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 5210 | int ret; |
| 5211 | |
| 5212 | ret = tracing_check_open_get_tr(NULL); |
| 5213 | if (ret) |
| 5214 | return ret; |
Michael Sartain | 99c621d | 2017-07-05 22:07:15 -0600 | [diff] [blame] | 5215 | |
| 5216 | return seq_open(filp, &tracing_saved_tgids_seq_ops); |
| 5217 | } |
| 5218 | |
| 5219 | |
| 5220 | static const struct file_operations tracing_saved_tgids_fops = { |
| 5221 | .open = tracing_saved_tgids_open, |
| 5222 | .read = seq_read, |
| 5223 | .llseek = seq_lseek, |
| 5224 | .release = seq_release, |
| 5225 | }; |
| 5226 | |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 5227 | static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos) |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 5228 | { |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 5229 | unsigned int *ptr = v; |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 5230 | |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 5231 | if (*pos || m->count) |
| 5232 | ptr++; |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 5233 | |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 5234 | (*pos)++; |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 5235 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 5236 | for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num]; |
| 5237 | ptr++) { |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 5238 | if (*ptr == -1 || *ptr == NO_CMDLINE_MAP) |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 5239 | continue; |
| 5240 | |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 5241 | return ptr; |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 5242 | } |
| 5243 | |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 5244 | return NULL; |
| 5245 | } |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 5246 | |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 5247 | static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos) |
| 5248 | { |
| 5249 | void *v; |
| 5250 | loff_t l = 0; |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 5251 | |
Steven Rostedt (Red Hat) | 4c27e75 | 2014-05-30 10:49:46 -0400 | [diff] [blame] | 5252 | preempt_disable(); |
| 5253 | arch_spin_lock(&trace_cmdline_lock); |
| 5254 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 5255 | v = &savedcmd->map_cmdline_to_pid[0]; |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 5256 | while (l <= *pos) { |
| 5257 | v = saved_cmdlines_next(m, v, &l); |
| 5258 | if (!v) |
| 5259 | return NULL; |
| 5260 | } |
| 5261 | |
| 5262 | return v; |
| 5263 | } |
| 5264 | |
| 5265 | static void saved_cmdlines_stop(struct seq_file *m, void *v) |
| 5266 | { |
Steven Rostedt (Red Hat) | 4c27e75 | 2014-05-30 10:49:46 -0400 | [diff] [blame] | 5267 | arch_spin_unlock(&trace_cmdline_lock); |
| 5268 | preempt_enable(); |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 5269 | } |
| 5270 | |
| 5271 | static int saved_cmdlines_show(struct seq_file *m, void *v) |
| 5272 | { |
| 5273 | char buf[TASK_COMM_LEN]; |
| 5274 | unsigned int *pid = v; |
| 5275 | |
Steven Rostedt (Red Hat) | 4c27e75 | 2014-05-30 10:49:46 -0400 | [diff] [blame] | 5276 | __trace_find_cmdline(*pid, buf); |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 5277 | seq_printf(m, "%d %s\n", *pid, buf); |
| 5278 | return 0; |
| 5279 | } |
| 5280 | |
| 5281 | static const struct seq_operations tracing_saved_cmdlines_seq_ops = { |
| 5282 | .start = saved_cmdlines_start, |
| 5283 | .next = saved_cmdlines_next, |
| 5284 | .stop = saved_cmdlines_stop, |
| 5285 | .show = saved_cmdlines_show, |
| 5286 | }; |
| 5287 | |
| 5288 | static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp) |
| 5289 | { |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 5290 | int ret; |
| 5291 | |
| 5292 | ret = tracing_check_open_get_tr(NULL); |
| 5293 | if (ret) |
| 5294 | return ret; |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 5295 | |
| 5296 | return seq_open(filp, &tracing_saved_cmdlines_seq_ops); |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 5297 | } |
| 5298 | |
| 5299 | static const struct file_operations tracing_saved_cmdlines_fops = { |
Yoshihiro YUNOMAE | 42584c8 | 2014-02-20 17:44:31 +0900 | [diff] [blame] | 5300 | .open = tracing_saved_cmdlines_open, |
| 5301 | .read = seq_read, |
| 5302 | .llseek = seq_lseek, |
| 5303 | .release = seq_release, |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 5304 | }; |
| 5305 | |
| 5306 | static ssize_t |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 5307 | tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf, |
| 5308 | size_t cnt, loff_t *ppos) |
| 5309 | { |
| 5310 | char buf[64]; |
| 5311 | int r; |
| 5312 | |
| 5313 | arch_spin_lock(&trace_cmdline_lock); |
Namhyung Kim | a6af8fb | 2014-06-10 16:11:35 +0900 | [diff] [blame] | 5314 | r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num); |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 5315 | arch_spin_unlock(&trace_cmdline_lock); |
| 5316 | |
| 5317 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 5318 | } |
| 5319 | |
| 5320 | static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s) |
| 5321 | { |
| 5322 | kfree(s->saved_cmdlines); |
| 5323 | kfree(s->map_cmdline_to_pid); |
| 5324 | kfree(s); |
| 5325 | } |
| 5326 | |
| 5327 | static int tracing_resize_saved_cmdlines(unsigned int val) |
| 5328 | { |
| 5329 | struct saved_cmdlines_buffer *s, *savedcmd_temp; |
| 5330 | |
Namhyung Kim | a6af8fb | 2014-06-10 16:11:35 +0900 | [diff] [blame] | 5331 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 5332 | if (!s) |
| 5333 | return -ENOMEM; |
| 5334 | |
| 5335 | if (allocate_cmdlines_buffer(val, s) < 0) { |
| 5336 | kfree(s); |
| 5337 | return -ENOMEM; |
| 5338 | } |
| 5339 | |
| 5340 | arch_spin_lock(&trace_cmdline_lock); |
| 5341 | savedcmd_temp = savedcmd; |
| 5342 | savedcmd = s; |
| 5343 | arch_spin_unlock(&trace_cmdline_lock); |
| 5344 | free_saved_cmdlines_buffer(savedcmd_temp); |
| 5345 | |
| 5346 | return 0; |
| 5347 | } |
| 5348 | |
| 5349 | static ssize_t |
| 5350 | tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf, |
| 5351 | size_t cnt, loff_t *ppos) |
| 5352 | { |
| 5353 | unsigned long val; |
| 5354 | int ret; |
| 5355 | |
| 5356 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 5357 | if (ret) |
| 5358 | return ret; |
| 5359 | |
| 5360 | /* must have at least 1 entry or less than PID_MAX_DEFAULT */ |
| 5361 | if (!val || val > PID_MAX_DEFAULT) |
| 5362 | return -EINVAL; |
| 5363 | |
| 5364 | ret = tracing_resize_saved_cmdlines((unsigned int)val); |
| 5365 | if (ret < 0) |
| 5366 | return ret; |
| 5367 | |
| 5368 | *ppos += cnt; |
| 5369 | |
| 5370 | return cnt; |
| 5371 | } |
| 5372 | |
| 5373 | static const struct file_operations tracing_saved_cmdlines_size_fops = { |
| 5374 | .open = tracing_open_generic, |
| 5375 | .read = tracing_saved_cmdlines_size_read, |
| 5376 | .write = tracing_saved_cmdlines_size_write, |
| 5377 | }; |
| 5378 | |
Jeremy Linton | 681bec0 | 2017-05-31 16:56:53 -0500 | [diff] [blame] | 5379 | #ifdef CONFIG_TRACE_EVAL_MAP_FILE |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 5380 | static union trace_eval_map_item * |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5381 | update_eval_map(union trace_eval_map_item *ptr) |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5382 | { |
Jeremy Linton | 00f4b65 | 2017-05-31 16:56:43 -0500 | [diff] [blame] | 5383 | if (!ptr->map.eval_string) { |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5384 | if (ptr->tail.next) { |
| 5385 | ptr = ptr->tail.next; |
| 5386 | /* Set ptr to the next real item (skip head) */ |
| 5387 | ptr++; |
| 5388 | } else |
| 5389 | return NULL; |
| 5390 | } |
| 5391 | return ptr; |
| 5392 | } |
| 5393 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5394 | static void *eval_map_next(struct seq_file *m, void *v, loff_t *pos) |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5395 | { |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 5396 | union trace_eval_map_item *ptr = v; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5397 | |
| 5398 | /* |
| 5399 | * Paranoid! If ptr points to end, we don't want to increment past it. |
| 5400 | * This really should never happen. |
| 5401 | */ |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5402 | ptr = update_eval_map(ptr); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5403 | if (WARN_ON_ONCE(!ptr)) |
| 5404 | return NULL; |
| 5405 | |
| 5406 | ptr++; |
| 5407 | |
| 5408 | (*pos)++; |
| 5409 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5410 | ptr = update_eval_map(ptr); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5411 | |
| 5412 | return ptr; |
| 5413 | } |
| 5414 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5415 | static void *eval_map_start(struct seq_file *m, loff_t *pos) |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5416 | { |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 5417 | union trace_eval_map_item *v; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5418 | loff_t l = 0; |
| 5419 | |
Jeremy Linton | 1793ed9 | 2017-05-31 16:56:46 -0500 | [diff] [blame] | 5420 | mutex_lock(&trace_eval_mutex); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5421 | |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 5422 | v = trace_eval_maps; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5423 | if (v) |
| 5424 | v++; |
| 5425 | |
| 5426 | while (v && l < *pos) { |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5427 | v = eval_map_next(m, v, &l); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5428 | } |
| 5429 | |
| 5430 | return v; |
| 5431 | } |
| 5432 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5433 | static void eval_map_stop(struct seq_file *m, void *v) |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5434 | { |
Jeremy Linton | 1793ed9 | 2017-05-31 16:56:46 -0500 | [diff] [blame] | 5435 | mutex_unlock(&trace_eval_mutex); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5436 | } |
| 5437 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5438 | static int eval_map_show(struct seq_file *m, void *v) |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5439 | { |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 5440 | union trace_eval_map_item *ptr = v; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5441 | |
| 5442 | seq_printf(m, "%s %ld (%s)\n", |
Jeremy Linton | 00f4b65 | 2017-05-31 16:56:43 -0500 | [diff] [blame] | 5443 | ptr->map.eval_string, ptr->map.eval_value, |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5444 | ptr->map.system); |
| 5445 | |
| 5446 | return 0; |
| 5447 | } |
| 5448 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5449 | static const struct seq_operations tracing_eval_map_seq_ops = { |
| 5450 | .start = eval_map_start, |
| 5451 | .next = eval_map_next, |
| 5452 | .stop = eval_map_stop, |
| 5453 | .show = eval_map_show, |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5454 | }; |
| 5455 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5456 | static int tracing_eval_map_open(struct inode *inode, struct file *filp) |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5457 | { |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 5458 | int ret; |
| 5459 | |
| 5460 | ret = tracing_check_open_get_tr(NULL); |
| 5461 | if (ret) |
| 5462 | return ret; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5463 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5464 | return seq_open(filp, &tracing_eval_map_seq_ops); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5465 | } |
| 5466 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5467 | static const struct file_operations tracing_eval_map_fops = { |
| 5468 | .open = tracing_eval_map_open, |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5469 | .read = seq_read, |
| 5470 | .llseek = seq_lseek, |
| 5471 | .release = seq_release, |
| 5472 | }; |
| 5473 | |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 5474 | static inline union trace_eval_map_item * |
Jeremy Linton | 5f60b35 | 2017-05-31 16:56:47 -0500 | [diff] [blame] | 5475 | trace_eval_jmp_to_tail(union trace_eval_map_item *ptr) |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5476 | { |
| 5477 | /* Return tail of array given the head */ |
| 5478 | return ptr + ptr->head.length + 1; |
| 5479 | } |
| 5480 | |
| 5481 | static void |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5482 | trace_insert_eval_map_file(struct module *mod, struct trace_eval_map **start, |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5483 | int len) |
| 5484 | { |
Jeremy Linton | 00f4b65 | 2017-05-31 16:56:43 -0500 | [diff] [blame] | 5485 | struct trace_eval_map **stop; |
| 5486 | struct trace_eval_map **map; |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 5487 | union trace_eval_map_item *map_array; |
| 5488 | union trace_eval_map_item *ptr; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5489 | |
| 5490 | stop = start + len; |
| 5491 | |
| 5492 | /* |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 5493 | * The trace_eval_maps contains the map plus a head and tail item, |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5494 | * where the head holds the module and length of array, and the |
| 5495 | * tail holds a pointer to the next list. |
| 5496 | */ |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 5497 | map_array = kmalloc_array(len + 2, sizeof(*map_array), GFP_KERNEL); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5498 | if (!map_array) { |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5499 | pr_warn("Unable to allocate trace eval mapping\n"); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5500 | return; |
| 5501 | } |
| 5502 | |
Jeremy Linton | 1793ed9 | 2017-05-31 16:56:46 -0500 | [diff] [blame] | 5503 | mutex_lock(&trace_eval_mutex); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5504 | |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 5505 | if (!trace_eval_maps) |
| 5506 | trace_eval_maps = map_array; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5507 | else { |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 5508 | ptr = trace_eval_maps; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5509 | for (;;) { |
Jeremy Linton | 5f60b35 | 2017-05-31 16:56:47 -0500 | [diff] [blame] | 5510 | ptr = trace_eval_jmp_to_tail(ptr); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5511 | if (!ptr->tail.next) |
| 5512 | break; |
| 5513 | ptr = ptr->tail.next; |
| 5514 | |
| 5515 | } |
| 5516 | ptr->tail.next = map_array; |
| 5517 | } |
| 5518 | map_array->head.mod = mod; |
| 5519 | map_array->head.length = len; |
| 5520 | map_array++; |
| 5521 | |
| 5522 | for (map = start; (unsigned long)map < (unsigned long)stop; map++) { |
| 5523 | map_array->map = **map; |
| 5524 | map_array++; |
| 5525 | } |
| 5526 | memset(map_array, 0, sizeof(*map_array)); |
| 5527 | |
Jeremy Linton | 1793ed9 | 2017-05-31 16:56:46 -0500 | [diff] [blame] | 5528 | mutex_unlock(&trace_eval_mutex); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5529 | } |
| 5530 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5531 | static void trace_create_eval_file(struct dentry *d_tracer) |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5532 | { |
Jeremy Linton | 681bec0 | 2017-05-31 16:56:53 -0500 | [diff] [blame] | 5533 | trace_create_file("eval_map", 0444, d_tracer, |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5534 | NULL, &tracing_eval_map_fops); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5535 | } |
| 5536 | |
Jeremy Linton | 681bec0 | 2017-05-31 16:56:53 -0500 | [diff] [blame] | 5537 | #else /* CONFIG_TRACE_EVAL_MAP_FILE */ |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5538 | static inline void trace_create_eval_file(struct dentry *d_tracer) { } |
| 5539 | static inline void trace_insert_eval_map_file(struct module *mod, |
Jeremy Linton | 00f4b65 | 2017-05-31 16:56:43 -0500 | [diff] [blame] | 5540 | struct trace_eval_map **start, int len) { } |
Jeremy Linton | 681bec0 | 2017-05-31 16:56:53 -0500 | [diff] [blame] | 5541 | #endif /* !CONFIG_TRACE_EVAL_MAP_FILE */ |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5542 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5543 | static void trace_insert_eval_map(struct module *mod, |
Jeremy Linton | 00f4b65 | 2017-05-31 16:56:43 -0500 | [diff] [blame] | 5544 | struct trace_eval_map **start, int len) |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 5545 | { |
Jeremy Linton | 00f4b65 | 2017-05-31 16:56:43 -0500 | [diff] [blame] | 5546 | struct trace_eval_map **map; |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 5547 | |
| 5548 | if (len <= 0) |
| 5549 | return; |
| 5550 | |
| 5551 | map = start; |
| 5552 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5553 | trace_event_eval_update(map, len); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 5554 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 5555 | trace_insert_eval_map_file(mod, start, len); |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 5556 | } |
| 5557 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 5558 | static ssize_t |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5559 | tracing_set_trace_read(struct file *filp, char __user *ubuf, |
| 5560 | size_t cnt, loff_t *ppos) |
| 5561 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5562 | struct trace_array *tr = filp->private_data; |
Li Zefan | ee6c2c1 | 2009-09-18 14:06:47 +0800 | [diff] [blame] | 5563 | char buf[MAX_TRACER_SIZE+2]; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5564 | int r; |
| 5565 | |
| 5566 | mutex_lock(&trace_types_lock); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5567 | r = sprintf(buf, "%s\n", tr->current_trace->name); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5568 | mutex_unlock(&trace_types_lock); |
| 5569 | |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5570 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5571 | } |
| 5572 | |
Arnaldo Carvalho de Melo | b6f11df | 2009-02-05 18:02:00 -0200 | [diff] [blame] | 5573 | int tracer_init(struct tracer *t, struct trace_array *tr) |
| 5574 | { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 5575 | tracing_reset_online_cpus(&tr->array_buffer); |
Arnaldo Carvalho de Melo | b6f11df | 2009-02-05 18:02:00 -0200 | [diff] [blame] | 5576 | return t->init(tr); |
| 5577 | } |
| 5578 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 5579 | static void set_buffer_entries(struct array_buffer *buf, unsigned long val) |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5580 | { |
| 5581 | int cpu; |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 5582 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5583 | for_each_tracing_cpu(cpu) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5584 | per_cpu_ptr(buf->data, cpu)->entries = val; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5585 | } |
| 5586 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5587 | #ifdef CONFIG_TRACER_MAX_TRACE |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 5588 | /* resize @tr's buffer to the size of @size_tr's entries */ |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 5589 | static int resize_buffer_duplicate_size(struct array_buffer *trace_buf, |
| 5590 | struct array_buffer *size_buf, int cpu_id) |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 5591 | { |
| 5592 | int cpu, ret = 0; |
| 5593 | |
| 5594 | if (cpu_id == RING_BUFFER_ALL_CPUS) { |
| 5595 | for_each_tracing_cpu(cpu) { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5596 | ret = ring_buffer_resize(trace_buf->buffer, |
| 5597 | per_cpu_ptr(size_buf->data, cpu)->entries, cpu); |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 5598 | if (ret < 0) |
| 5599 | break; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5600 | per_cpu_ptr(trace_buf->data, cpu)->entries = |
| 5601 | per_cpu_ptr(size_buf->data, cpu)->entries; |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 5602 | } |
| 5603 | } else { |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5604 | ret = ring_buffer_resize(trace_buf->buffer, |
| 5605 | per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id); |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 5606 | if (ret == 0) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5607 | per_cpu_ptr(trace_buf->data, cpu_id)->entries = |
| 5608 | per_cpu_ptr(size_buf->data, cpu_id)->entries; |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 5609 | } |
| 5610 | |
| 5611 | return ret; |
| 5612 | } |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5613 | #endif /* CONFIG_TRACER_MAX_TRACE */ |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 5614 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5615 | static int __tracing_resize_ring_buffer(struct trace_array *tr, |
| 5616 | unsigned long size, int cpu) |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 5617 | { |
| 5618 | int ret; |
| 5619 | |
| 5620 | /* |
| 5621 | * If kernel or user changes the size of the ring buffer |
Steven Rostedt | a123c52 | 2009-03-12 11:21:08 -0400 | [diff] [blame] | 5622 | * we use the size that was given, and we can forget about |
| 5623 | * expanding it later. |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 5624 | */ |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 5625 | ring_buffer_expanded = true; |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 5626 | |
Steven Rostedt | b382ede6 | 2012-10-10 21:44:34 -0400 | [diff] [blame] | 5627 | /* May be called before buffers are initialized */ |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 5628 | if (!tr->array_buffer.buffer) |
Steven Rostedt | b382ede6 | 2012-10-10 21:44:34 -0400 | [diff] [blame] | 5629 | return 0; |
| 5630 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 5631 | ret = ring_buffer_resize(tr->array_buffer.buffer, size, cpu); |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 5632 | if (ret < 0) |
| 5633 | return ret; |
| 5634 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5635 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5636 | if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) || |
| 5637 | !tr->current_trace->use_max_tr) |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 5638 | goto out; |
| 5639 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5640 | ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu); |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 5641 | if (ret < 0) { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 5642 | int r = resize_buffer_duplicate_size(&tr->array_buffer, |
| 5643 | &tr->array_buffer, cpu); |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 5644 | if (r < 0) { |
Steven Rostedt | a123c52 | 2009-03-12 11:21:08 -0400 | [diff] [blame] | 5645 | /* |
| 5646 | * AARGH! We are left with different |
| 5647 | * size max buffer!!!! |
| 5648 | * The max buffer is our "snapshot" buffer. |
| 5649 | * When a tracer needs a snapshot (one of the |
| 5650 | * latency tracers), it swaps the max buffer |
| 5651 | * with the saved snap shot. We succeeded to |
| 5652 | * update the size of the main buffer, but failed to |
| 5653 | * update the size of the max buffer. But when we tried |
| 5654 | * to reset the main buffer to the original size, we |
| 5655 | * failed there too. This is very unlikely to |
| 5656 | * happen, but if it does, warn and kill all |
| 5657 | * tracing. |
| 5658 | */ |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 5659 | WARN_ON(1); |
| 5660 | tracing_disabled = 1; |
| 5661 | } |
| 5662 | return ret; |
| 5663 | } |
| 5664 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5665 | if (cpu == RING_BUFFER_ALL_CPUS) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5666 | set_buffer_entries(&tr->max_buffer, size); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5667 | else |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5668 | per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5669 | |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 5670 | out: |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5671 | #endif /* CONFIG_TRACER_MAX_TRACE */ |
| 5672 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5673 | if (cpu == RING_BUFFER_ALL_CPUS) |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 5674 | set_buffer_entries(&tr->array_buffer, size); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5675 | else |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 5676 | per_cpu_ptr(tr->array_buffer.data, cpu)->entries = size; |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 5677 | |
| 5678 | return ret; |
| 5679 | } |
| 5680 | |
Masami Hiramatsu | 9c5b9d3 | 2020-01-11 01:06:17 +0900 | [diff] [blame] | 5681 | ssize_t tracing_resize_ring_buffer(struct trace_array *tr, |
| 5682 | unsigned long size, int cpu_id) |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 5683 | { |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 5684 | int ret = size; |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 5685 | |
| 5686 | mutex_lock(&trace_types_lock); |
| 5687 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5688 | if (cpu_id != RING_BUFFER_ALL_CPUS) { |
| 5689 | /* make sure, this cpu is enabled in the mask */ |
| 5690 | if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) { |
| 5691 | ret = -EINVAL; |
| 5692 | goto out; |
| 5693 | } |
| 5694 | } |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 5695 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5696 | ret = __tracing_resize_ring_buffer(tr, size, cpu_id); |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 5697 | if (ret < 0) |
| 5698 | ret = -ENOMEM; |
| 5699 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5700 | out: |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 5701 | mutex_unlock(&trace_types_lock); |
| 5702 | |
| 5703 | return ret; |
| 5704 | } |
| 5705 | |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 5706 | |
Steven Rostedt | 1852fcc | 2009-03-11 14:33:00 -0400 | [diff] [blame] | 5707 | /** |
| 5708 | * tracing_update_buffers - used by tracing facility to expand ring buffers |
| 5709 | * |
| 5710 | * To save on memory when the tracing is never used on a system with it |
| 5711 | * configured in. The ring buffers are set to a minimum size. But once |
| 5712 | * a user starts to use the tracing facility, then they need to grow |
| 5713 | * to their default size. |
| 5714 | * |
| 5715 | * This function is to be called when a tracer is about to be used. |
| 5716 | */ |
| 5717 | int tracing_update_buffers(void) |
| 5718 | { |
| 5719 | int ret = 0; |
| 5720 | |
Steven Rostedt | 1027fcb | 2009-03-12 11:33:20 -0400 | [diff] [blame] | 5721 | mutex_lock(&trace_types_lock); |
Steven Rostedt | 1852fcc | 2009-03-11 14:33:00 -0400 | [diff] [blame] | 5722 | if (!ring_buffer_expanded) |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5723 | ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size, |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5724 | RING_BUFFER_ALL_CPUS); |
Steven Rostedt | 1027fcb | 2009-03-12 11:33:20 -0400 | [diff] [blame] | 5725 | mutex_unlock(&trace_types_lock); |
Steven Rostedt | 1852fcc | 2009-03-11 14:33:00 -0400 | [diff] [blame] | 5726 | |
| 5727 | return ret; |
| 5728 | } |
| 5729 | |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 5730 | struct trace_option_dentry; |
| 5731 | |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 5732 | static void |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5733 | create_trace_option_files(struct trace_array *tr, struct tracer *tracer); |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 5734 | |
Steven Rostedt (Red Hat) | 6b450d2 | 2014-01-14 08:43:01 -0500 | [diff] [blame] | 5735 | /* |
| 5736 | * Used to clear out the tracer before deletion of an instance. |
| 5737 | * Must have trace_types_lock held. |
| 5738 | */ |
| 5739 | static void tracing_set_nop(struct trace_array *tr) |
| 5740 | { |
| 5741 | if (tr->current_trace == &nop_trace) |
| 5742 | return; |
| 5743 | |
Steven Rostedt (Red Hat) | 50512ab | 2014-01-14 08:52:35 -0500 | [diff] [blame] | 5744 | tr->current_trace->enabled--; |
Steven Rostedt (Red Hat) | 6b450d2 | 2014-01-14 08:43:01 -0500 | [diff] [blame] | 5745 | |
| 5746 | if (tr->current_trace->reset) |
| 5747 | tr->current_trace->reset(tr); |
| 5748 | |
| 5749 | tr->current_trace = &nop_trace; |
| 5750 | } |
| 5751 | |
Steven Rostedt (Red Hat) | 41d9c0b | 2015-09-29 17:31:55 -0400 | [diff] [blame] | 5752 | static void add_tracer_options(struct trace_array *tr, struct tracer *t) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5753 | { |
Steven Rostedt (Red Hat) | 09d23a1 | 2015-02-03 12:45:53 -0500 | [diff] [blame] | 5754 | /* Only enable if the directory has been created already. */ |
| 5755 | if (!tr->dir) |
| 5756 | return; |
| 5757 | |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 5758 | create_trace_option_files(tr, t); |
Steven Rostedt (Red Hat) | 09d23a1 | 2015-02-03 12:45:53 -0500 | [diff] [blame] | 5759 | } |
| 5760 | |
Masami Hiramatsu | 9c5b9d3 | 2020-01-11 01:06:17 +0900 | [diff] [blame] | 5761 | int tracing_set_tracer(struct trace_array *tr, const char *buf) |
Steven Rostedt (Red Hat) | 09d23a1 | 2015-02-03 12:45:53 -0500 | [diff] [blame] | 5762 | { |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5763 | struct tracer *t; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5764 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt | 34600f0 | 2013-01-22 13:35:11 -0500 | [diff] [blame] | 5765 | bool had_max_tr; |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5766 | #endif |
Peter Zijlstra | d9e5407 | 2008-11-01 19:57:37 +0100 | [diff] [blame] | 5767 | int ret = 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5768 | |
Steven Rostedt | 1027fcb | 2009-03-12 11:33:20 -0400 | [diff] [blame] | 5769 | mutex_lock(&trace_types_lock); |
| 5770 | |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 5771 | if (!ring_buffer_expanded) { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5772 | ret = __tracing_resize_ring_buffer(tr, trace_buf_size, |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5773 | RING_BUFFER_ALL_CPUS); |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 5774 | if (ret < 0) |
Frederic Weisbecker | 59f586d | 2009-03-15 22:10:39 +0100 | [diff] [blame] | 5775 | goto out; |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 5776 | ret = 0; |
| 5777 | } |
| 5778 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5779 | for (t = trace_types; t; t = t->next) { |
| 5780 | if (strcmp(t->name, buf) == 0) |
| 5781 | break; |
| 5782 | } |
Frederic Weisbecker | c2931e0 | 2008-10-04 22:04:44 +0200 | [diff] [blame] | 5783 | if (!t) { |
| 5784 | ret = -EINVAL; |
| 5785 | goto out; |
| 5786 | } |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5787 | if (t == tr->current_trace) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5788 | goto out; |
| 5789 | |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 5790 | #ifdef CONFIG_TRACER_SNAPSHOT |
| 5791 | if (t->use_max_tr) { |
| 5792 | arch_spin_lock(&tr->max_lock); |
| 5793 | if (tr->cond_snapshot) |
| 5794 | ret = -EBUSY; |
| 5795 | arch_spin_unlock(&tr->max_lock); |
| 5796 | if (ret) |
| 5797 | goto out; |
| 5798 | } |
| 5799 | #endif |
Ziqian SUN (Zamir) | c7b3ae0 | 2017-09-11 14:26:35 +0800 | [diff] [blame] | 5800 | /* Some tracers won't work on kernel command line */ |
| 5801 | if (system_state < SYSTEM_RUNNING && t->noboot) { |
| 5802 | pr_warn("Tracer '%s' is not allowed on command line, ignored\n", |
| 5803 | t->name); |
| 5804 | goto out; |
| 5805 | } |
| 5806 | |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 5807 | /* Some tracers are only allowed for the top level buffer */ |
| 5808 | if (!trace_ok_for_array(t, tr)) { |
| 5809 | ret = -EINVAL; |
| 5810 | goto out; |
| 5811 | } |
| 5812 | |
Steven Rostedt (Red Hat) | cf6ab6d | 2014-12-15 20:13:31 -0500 | [diff] [blame] | 5813 | /* If trace pipe files are being read, we can't change the tracer */ |
| 5814 | if (tr->current_trace->ref) { |
| 5815 | ret = -EBUSY; |
| 5816 | goto out; |
| 5817 | } |
| 5818 | |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 5819 | trace_branch_disable(); |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 5820 | |
Steven Rostedt (Red Hat) | 50512ab | 2014-01-14 08:52:35 -0500 | [diff] [blame] | 5821 | tr->current_trace->enabled--; |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 5822 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5823 | if (tr->current_trace->reset) |
| 5824 | tr->current_trace->reset(tr); |
Steven Rostedt | 34600f0 | 2013-01-22 13:35:11 -0500 | [diff] [blame] | 5825 | |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 5826 | /* Current trace needs to be nop_trace before synchronize_rcu */ |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5827 | tr->current_trace = &nop_trace; |
Steven Rostedt | 34600f0 | 2013-01-22 13:35:11 -0500 | [diff] [blame] | 5828 | |
Steven Rostedt (Red Hat) | 45ad21c | 2013-03-05 18:25:02 -0500 | [diff] [blame] | 5829 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 5830 | had_max_tr = tr->allocated_snapshot; |
Steven Rostedt | 34600f0 | 2013-01-22 13:35:11 -0500 | [diff] [blame] | 5831 | |
| 5832 | if (had_max_tr && !t->use_max_tr) { |
| 5833 | /* |
| 5834 | * We need to make sure that the update_max_tr sees that |
| 5835 | * current_trace changed to nop_trace to keep it from |
| 5836 | * swapping the buffers after we resize it. |
| 5837 | * The update_max_tr is called from interrupts disabled |
| 5838 | * so a synchronized_sched() is sufficient. |
| 5839 | */ |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 5840 | synchronize_rcu(); |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 5841 | free_snapshot(tr); |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 5842 | } |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5843 | #endif |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5844 | |
| 5845 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt | 34600f0 | 2013-01-22 13:35:11 -0500 | [diff] [blame] | 5846 | if (t->use_max_tr && !had_max_tr) { |
Steven Rostedt (VMware) | 2824f50 | 2018-05-28 10:56:36 -0400 | [diff] [blame] | 5847 | ret = tracing_alloc_snapshot_instance(tr); |
Hiraku Toyooka | d60da50 | 2012-10-17 11:56:16 +0900 | [diff] [blame] | 5848 | if (ret < 0) |
| 5849 | goto out; |
KOSAKI Motohiro | ef710e1 | 2010-07-01 14:34:35 +0900 | [diff] [blame] | 5850 | } |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 5851 | #endif |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 5852 | |
Frederic Weisbecker | 1c80025 | 2008-11-16 05:57:26 +0100 | [diff] [blame] | 5853 | if (t->init) { |
Arnaldo Carvalho de Melo | b6f11df | 2009-02-05 18:02:00 -0200 | [diff] [blame] | 5854 | ret = tracer_init(t, tr); |
Frederic Weisbecker | 1c80025 | 2008-11-16 05:57:26 +0100 | [diff] [blame] | 5855 | if (ret) |
| 5856 | goto out; |
| 5857 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5858 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 5859 | tr->current_trace = t; |
Steven Rostedt (Red Hat) | 50512ab | 2014-01-14 08:52:35 -0500 | [diff] [blame] | 5860 | tr->current_trace->enabled++; |
Steven Rostedt | 9f029e8 | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 5861 | trace_branch_enable(tr); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5862 | out: |
| 5863 | mutex_unlock(&trace_types_lock); |
| 5864 | |
Peter Zijlstra | d9e5407 | 2008-11-01 19:57:37 +0100 | [diff] [blame] | 5865 | return ret; |
| 5866 | } |
| 5867 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5868 | static ssize_t |
| 5869 | tracing_set_trace_write(struct file *filp, const char __user *ubuf, |
| 5870 | size_t cnt, loff_t *ppos) |
| 5871 | { |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 5872 | struct trace_array *tr = filp->private_data; |
Li Zefan | ee6c2c1 | 2009-09-18 14:06:47 +0800 | [diff] [blame] | 5873 | char buf[MAX_TRACER_SIZE+1]; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5874 | int i; |
| 5875 | size_t ret; |
Frederic Weisbecker | e6e7a65 | 2008-11-16 05:53:19 +0100 | [diff] [blame] | 5876 | int err; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5877 | |
Steven Rostedt | 60063a6 | 2008-10-28 10:44:24 -0400 | [diff] [blame] | 5878 | ret = cnt; |
| 5879 | |
Li Zefan | ee6c2c1 | 2009-09-18 14:06:47 +0800 | [diff] [blame] | 5880 | if (cnt > MAX_TRACER_SIZE) |
| 5881 | cnt = MAX_TRACER_SIZE; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5882 | |
Wang Xiaoqiang | 4afe649 | 2016-04-18 15:23:29 +0800 | [diff] [blame] | 5883 | if (copy_from_user(buf, ubuf, cnt)) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5884 | return -EFAULT; |
| 5885 | |
| 5886 | buf[cnt] = 0; |
| 5887 | |
| 5888 | /* strip ending whitespace. */ |
| 5889 | for (i = cnt - 1; i > 0 && isspace(buf[i]); i--) |
| 5890 | buf[i] = 0; |
| 5891 | |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 5892 | err = tracing_set_tracer(tr, buf); |
Frederic Weisbecker | e6e7a65 | 2008-11-16 05:53:19 +0100 | [diff] [blame] | 5893 | if (err) |
| 5894 | return err; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5895 | |
Jiri Olsa | cf8517c | 2009-10-23 19:36:16 -0400 | [diff] [blame] | 5896 | *ppos += ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5897 | |
Frederic Weisbecker | c2931e0 | 2008-10-04 22:04:44 +0200 | [diff] [blame] | 5898 | return ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5899 | } |
| 5900 | |
| 5901 | static ssize_t |
Stanislav Fomichev | 6508fa7 | 2014-07-18 15:17:27 +0400 | [diff] [blame] | 5902 | tracing_nsecs_read(unsigned long *ptr, char __user *ubuf, |
| 5903 | size_t cnt, loff_t *ppos) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5904 | { |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5905 | char buf[64]; |
| 5906 | int r; |
| 5907 | |
Steven Rostedt | cffae43 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 5908 | r = snprintf(buf, sizeof(buf), "%ld\n", |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5909 | *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr)); |
Steven Rostedt | cffae43 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 5910 | if (r > sizeof(buf)) |
| 5911 | r = sizeof(buf); |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5912 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5913 | } |
| 5914 | |
| 5915 | static ssize_t |
Stanislav Fomichev | 6508fa7 | 2014-07-18 15:17:27 +0400 | [diff] [blame] | 5916 | tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf, |
| 5917 | size_t cnt, loff_t *ppos) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5918 | { |
Hannes Eder | 5e39841 | 2009-02-10 19:44:34 +0100 | [diff] [blame] | 5919 | unsigned long val; |
Steven Rostedt | c6caeeb | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 5920 | int ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5921 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 5922 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 5923 | if (ret) |
Steven Rostedt | c6caeeb | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 5924 | return ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 5925 | |
| 5926 | *ptr = val * 1000; |
| 5927 | |
| 5928 | return cnt; |
| 5929 | } |
| 5930 | |
Stanislav Fomichev | 6508fa7 | 2014-07-18 15:17:27 +0400 | [diff] [blame] | 5931 | static ssize_t |
| 5932 | tracing_thresh_read(struct file *filp, char __user *ubuf, |
| 5933 | size_t cnt, loff_t *ppos) |
| 5934 | { |
| 5935 | return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos); |
| 5936 | } |
| 5937 | |
| 5938 | static ssize_t |
| 5939 | tracing_thresh_write(struct file *filp, const char __user *ubuf, |
| 5940 | size_t cnt, loff_t *ppos) |
| 5941 | { |
| 5942 | struct trace_array *tr = filp->private_data; |
| 5943 | int ret; |
| 5944 | |
| 5945 | mutex_lock(&trace_types_lock); |
| 5946 | ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos); |
| 5947 | if (ret < 0) |
| 5948 | goto out; |
| 5949 | |
| 5950 | if (tr->current_trace->update_thresh) { |
| 5951 | ret = tr->current_trace->update_thresh(tr); |
| 5952 | if (ret < 0) |
| 5953 | goto out; |
| 5954 | } |
| 5955 | |
| 5956 | ret = cnt; |
| 5957 | out: |
| 5958 | mutex_unlock(&trace_types_lock); |
| 5959 | |
| 5960 | return ret; |
| 5961 | } |
| 5962 | |
Steven Rostedt (Red Hat) | f971cc9 | 2016-09-07 12:45:09 -0400 | [diff] [blame] | 5963 | #if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER) |
Chen Gang | e428abb | 2015-11-10 05:15:15 +0800 | [diff] [blame] | 5964 | |
Stanislav Fomichev | 6508fa7 | 2014-07-18 15:17:27 +0400 | [diff] [blame] | 5965 | static ssize_t |
| 5966 | tracing_max_lat_read(struct file *filp, char __user *ubuf, |
| 5967 | size_t cnt, loff_t *ppos) |
| 5968 | { |
| 5969 | return tracing_nsecs_read(filp->private_data, ubuf, cnt, ppos); |
| 5970 | } |
| 5971 | |
| 5972 | static ssize_t |
| 5973 | tracing_max_lat_write(struct file *filp, const char __user *ubuf, |
| 5974 | size_t cnt, loff_t *ppos) |
| 5975 | { |
| 5976 | return tracing_nsecs_write(filp->private_data, ubuf, cnt, ppos); |
| 5977 | } |
| 5978 | |
Chen Gang | e428abb | 2015-11-10 05:15:15 +0800 | [diff] [blame] | 5979 | #endif |
| 5980 | |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5981 | static int tracing_open_pipe(struct inode *inode, struct file *filp) |
| 5982 | { |
Oleg Nesterov | 1554420 | 2013-07-23 17:25:57 +0200 | [diff] [blame] | 5983 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5984 | struct trace_iterator *iter; |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 5985 | int ret; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5986 | |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 5987 | ret = tracing_check_open_get_tr(tr); |
| 5988 | if (ret) |
| 5989 | return ret; |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 5990 | |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 5991 | mutex_lock(&trace_types_lock); |
| 5992 | |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 5993 | /* create a buffer to store the information to pass to userspace */ |
| 5994 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 5995 | if (!iter) { |
| 5996 | ret = -ENOMEM; |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 5997 | __trace_array_put(tr); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 5998 | goto out; |
| 5999 | } |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6000 | |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 6001 | trace_seq_init(&iter->seq); |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 6002 | iter->trace = tr->current_trace; |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 6003 | |
| 6004 | if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) { |
| 6005 | ret = -ENOMEM; |
| 6006 | goto fail; |
Rusty Russell | 4462344 | 2009-01-01 10:12:23 +1030 | [diff] [blame] | 6007 | } |
| 6008 | |
Steven Rostedt | a309720 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 6009 | /* trace pipe does not show start of buffer */ |
Rusty Russell | 4462344 | 2009-01-01 10:12:23 +1030 | [diff] [blame] | 6010 | cpumask_setall(iter->started); |
Steven Rostedt | a309720 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 6011 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 6012 | if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) |
Steven Rostedt | 112f38a7 | 2009-06-01 15:16:05 -0400 | [diff] [blame] | 6013 | iter->iter_flags |= TRACE_FILE_LAT_FMT; |
| 6014 | |
David Sharp | 8be0709 | 2012-11-13 12:18:22 -0800 | [diff] [blame] | 6015 | /* Output in nanoseconds only if we are using a clock in nanoseconds. */ |
Yoshihiro YUNOMAE | 58e8eed | 2013-04-23 10:32:39 +0900 | [diff] [blame] | 6016 | if (trace_clocks[tr->clock_id].in_ns) |
David Sharp | 8be0709 | 2012-11-13 12:18:22 -0800 | [diff] [blame] | 6017 | iter->iter_flags |= TRACE_FILE_TIME_IN_NS; |
| 6018 | |
Oleg Nesterov | 1554420 | 2013-07-23 17:25:57 +0200 | [diff] [blame] | 6019 | iter->tr = tr; |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 6020 | iter->array_buffer = &tr->array_buffer; |
Oleg Nesterov | 1554420 | 2013-07-23 17:25:57 +0200 | [diff] [blame] | 6021 | iter->cpu_file = tracing_get_cpu(inode); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 6022 | mutex_init(&iter->mutex); |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6023 | filp->private_data = iter; |
| 6024 | |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 6025 | if (iter->trace->pipe_open) |
| 6026 | iter->trace->pipe_open(iter); |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 6027 | |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 6028 | nonseekable_open(inode, filp); |
Steven Rostedt (Red Hat) | cf6ab6d | 2014-12-15 20:13:31 -0500 | [diff] [blame] | 6029 | |
| 6030 | tr->current_trace->ref++; |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 6031 | out: |
| 6032 | mutex_unlock(&trace_types_lock); |
| 6033 | return ret; |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 6034 | |
| 6035 | fail: |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 6036 | kfree(iter); |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6037 | __trace_array_put(tr); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 6038 | mutex_unlock(&trace_types_lock); |
| 6039 | return ret; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6040 | } |
| 6041 | |
| 6042 | static int tracing_release_pipe(struct inode *inode, struct file *file) |
| 6043 | { |
| 6044 | struct trace_iterator *iter = file->private_data; |
Oleg Nesterov | 1554420 | 2013-07-23 17:25:57 +0200 | [diff] [blame] | 6045 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6046 | |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 6047 | mutex_lock(&trace_types_lock); |
| 6048 | |
Steven Rostedt (Red Hat) | cf6ab6d | 2014-12-15 20:13:31 -0500 | [diff] [blame] | 6049 | tr->current_trace->ref--; |
| 6050 | |
Steven Rostedt | 29bf4a5 | 2009-12-09 12:37:43 -0500 | [diff] [blame] | 6051 | if (iter->trace->pipe_close) |
Steven Rostedt | c521efd | 2009-12-07 09:06:24 -0500 | [diff] [blame] | 6052 | iter->trace->pipe_close(iter); |
| 6053 | |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 6054 | mutex_unlock(&trace_types_lock); |
| 6055 | |
Rusty Russell | 4462344 | 2009-01-01 10:12:23 +1030 | [diff] [blame] | 6056 | free_cpumask_var(iter->started); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 6057 | mutex_destroy(&iter->mutex); |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6058 | kfree(iter); |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6059 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6060 | trace_array_put(tr); |
| 6061 | |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6062 | return 0; |
| 6063 | } |
| 6064 | |
Al Viro | 9dd9574 | 2017-07-03 00:42:43 -0400 | [diff] [blame] | 6065 | static __poll_t |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6066 | trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table) |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 6067 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 6068 | struct trace_array *tr = iter->tr; |
| 6069 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 6070 | /* Iterators are static, they should be filled or empty */ |
| 6071 | if (trace_buffer_iter(iter, iter->cpu_file)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 6072 | return EPOLLIN | EPOLLRDNORM; |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 6073 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 6074 | if (tr->trace_flags & TRACE_ITER_BLOCK) |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 6075 | /* |
| 6076 | * Always select as readable when in blocking mode |
| 6077 | */ |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 6078 | return EPOLLIN | EPOLLRDNORM; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 6079 | else |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 6080 | return ring_buffer_poll_wait(iter->array_buffer->buffer, iter->cpu_file, |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 6081 | filp, poll_table); |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 6082 | } |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 6083 | |
Al Viro | 9dd9574 | 2017-07-03 00:42:43 -0400 | [diff] [blame] | 6084 | static __poll_t |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 6085 | tracing_poll_pipe(struct file *filp, poll_table *poll_table) |
| 6086 | { |
| 6087 | struct trace_iterator *iter = filp->private_data; |
| 6088 | |
| 6089 | return trace_poll(iter, filp, poll_table); |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 6090 | } |
| 6091 | |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 6092 | /* Must be called with iter->mutex held. */ |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 6093 | static int tracing_wait_pipe(struct file *filp) |
| 6094 | { |
| 6095 | struct trace_iterator *iter = filp->private_data; |
Steven Rostedt (Red Hat) | 8b8b368 | 2014-06-10 09:46:00 -0400 | [diff] [blame] | 6096 | int ret; |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 6097 | |
| 6098 | while (trace_empty(iter)) { |
| 6099 | |
| 6100 | if ((filp->f_flags & O_NONBLOCK)) { |
| 6101 | return -EAGAIN; |
| 6102 | } |
| 6103 | |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 6104 | /* |
Liu Bo | 250bfd3 | 2013-01-14 10:54:11 +0800 | [diff] [blame] | 6105 | * We block until we read something and tracing is disabled. |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 6106 | * We still block if tracing is disabled, but we have never |
| 6107 | * read anything. This allows a user to cat this file, and |
| 6108 | * then enable tracing. But after we have read something, |
| 6109 | * we give an EOF when tracing is again disabled. |
| 6110 | * |
| 6111 | * iter->pos will be 0 if we haven't read anything. |
| 6112 | */ |
Tahsin Erdogan | 75df6e6 | 2017-09-17 03:23:48 -0700 | [diff] [blame] | 6113 | if (!tracer_tracing_is_on(iter->tr) && iter->pos) |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 6114 | break; |
Steven Rostedt (Red Hat) | f487426 | 2014-04-29 16:07:28 -0400 | [diff] [blame] | 6115 | |
| 6116 | mutex_unlock(&iter->mutex); |
| 6117 | |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 6118 | ret = wait_on_pipe(iter, 0); |
Steven Rostedt (Red Hat) | f487426 | 2014-04-29 16:07:28 -0400 | [diff] [blame] | 6119 | |
| 6120 | mutex_lock(&iter->mutex); |
| 6121 | |
Steven Rostedt (Red Hat) | 8b8b368 | 2014-06-10 09:46:00 -0400 | [diff] [blame] | 6122 | if (ret) |
| 6123 | return ret; |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 6124 | } |
| 6125 | |
| 6126 | return 1; |
| 6127 | } |
| 6128 | |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6129 | /* |
| 6130 | * Consumer reader. |
| 6131 | */ |
| 6132 | static ssize_t |
| 6133 | tracing_read_pipe(struct file *filp, char __user *ubuf, |
| 6134 | size_t cnt, loff_t *ppos) |
| 6135 | { |
| 6136 | struct trace_iterator *iter = filp->private_data; |
Pekka Paalanen | 6c6c279 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 6137 | ssize_t sret; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6138 | |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 6139 | /* |
| 6140 | * Avoid more than one consumer on a single file descriptor |
| 6141 | * This is just a matter of traces coherency, the ring buffer itself |
| 6142 | * is protected. |
| 6143 | */ |
| 6144 | mutex_lock(&iter->mutex); |
Steven Rostedt (Red Hat) | 1245800 | 2016-09-23 22:57:13 -0400 | [diff] [blame] | 6145 | |
| 6146 | /* return any leftover data */ |
| 6147 | sret = trace_seq_to_user(&iter->seq, ubuf, cnt); |
| 6148 | if (sret != -EBUSY) |
| 6149 | goto out; |
| 6150 | |
| 6151 | trace_seq_init(&iter->seq); |
| 6152 | |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 6153 | if (iter->trace->read) { |
Pekka Paalanen | 6c6c279 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 6154 | sret = iter->trace->read(iter, filp, ubuf, cnt, ppos); |
| 6155 | if (sret) |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 6156 | goto out; |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 6157 | } |
| 6158 | |
Pekka Paalanen | 9ff4b97 | 2008-09-29 20:23:48 +0200 | [diff] [blame] | 6159 | waitagain: |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 6160 | sret = tracing_wait_pipe(filp); |
| 6161 | if (sret <= 0) |
| 6162 | goto out; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6163 | |
| 6164 | /* stop when tracing is finished */ |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 6165 | if (trace_empty(iter)) { |
| 6166 | sret = 0; |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 6167 | goto out; |
Eduard - Gabriel Munteanu | ff98781 | 2009-02-09 08:15:55 +0200 | [diff] [blame] | 6168 | } |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6169 | |
| 6170 | if (cnt >= PAGE_SIZE) |
| 6171 | cnt = PAGE_SIZE - 1; |
| 6172 | |
Steven Rostedt | 53d0aa7 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 6173 | /* reset all but tr, trace, and overruns */ |
Steven Rostedt | 53d0aa7 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 6174 | memset(&iter->seq, 0, |
| 6175 | sizeof(struct trace_iterator) - |
| 6176 | offsetof(struct trace_iterator, seq)); |
Andrew Vagin | ed5467d | 2013-08-02 21:16:43 +0400 | [diff] [blame] | 6177 | cpumask_clear(iter->started); |
Petr Mladek | d303de1 | 2019-10-11 16:21:34 +0200 | [diff] [blame] | 6178 | trace_seq_init(&iter->seq); |
Steven Rostedt | 4823ed7 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 6179 | iter->pos = -1; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6180 | |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 6181 | trace_event_read_lock(); |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 6182 | trace_access_lock(iter->cpu_file); |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 6183 | while (trace_find_next_entry_inc(iter) != NULL) { |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 6184 | enum print_line_t ret; |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 6185 | int save_len = iter->seq.seq.len; |
Steven Rostedt | 088b1e42 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 6186 | |
Ingo Molnar | f9896bf | 2008-05-12 21:20:47 +0200 | [diff] [blame] | 6187 | ret = print_trace_line(iter); |
Frederic Weisbecker | 2c4f035 | 2008-09-29 20:18:34 +0200 | [diff] [blame] | 6188 | if (ret == TRACE_TYPE_PARTIAL_LINE) { |
Steven Rostedt | 088b1e42 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 6189 | /* don't print partial lines */ |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 6190 | iter->seq.seq.len = save_len; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6191 | break; |
Steven Rostedt | 088b1e42 | 2008-05-12 21:20:48 +0200 | [diff] [blame] | 6192 | } |
Frederic Weisbecker | b91facc | 2009-02-06 18:30:44 +0100 | [diff] [blame] | 6193 | if (ret != TRACE_TYPE_NO_CONSUME) |
| 6194 | trace_consume(iter); |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6195 | |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 6196 | if (trace_seq_used(&iter->seq) >= cnt) |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6197 | break; |
Jiri Olsa | ee5e51f | 2011-03-25 12:05:18 +0100 | [diff] [blame] | 6198 | |
| 6199 | /* |
| 6200 | * Setting the full flag means we reached the trace_seq buffer |
| 6201 | * size and we should leave by partial output condition above. |
| 6202 | * One of the trace_seq_* functions is not used properly. |
| 6203 | */ |
| 6204 | WARN_ONCE(iter->seq.full, "full flag set for trace type %d", |
| 6205 | iter->ent->type); |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6206 | } |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 6207 | trace_access_unlock(iter->cpu_file); |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 6208 | trace_event_read_unlock(); |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6209 | |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6210 | /* Now copy what we have to the user */ |
Pekka Paalanen | 6c6c279 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 6211 | sret = trace_seq_to_user(&iter->seq, ubuf, cnt); |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 6212 | if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq)) |
Steven Rostedt | f952075 | 2009-03-02 14:04:40 -0500 | [diff] [blame] | 6213 | trace_seq_init(&iter->seq); |
Pekka Paalanen | 9ff4b97 | 2008-09-29 20:23:48 +0200 | [diff] [blame] | 6214 | |
| 6215 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6216 | * If there was nothing to send to user, in spite of consuming trace |
Pekka Paalanen | 9ff4b97 | 2008-09-29 20:23:48 +0200 | [diff] [blame] | 6217 | * entries, go back to wait for more entries. |
| 6218 | */ |
Pekka Paalanen | 6c6c279 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 6219 | if (sret == -EBUSY) |
Pekka Paalanen | 9ff4b97 | 2008-09-29 20:23:48 +0200 | [diff] [blame] | 6220 | goto waitagain; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6221 | |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 6222 | out: |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 6223 | mutex_unlock(&iter->mutex); |
Steven Rostedt | 107bad8 | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 6224 | |
Pekka Paalanen | 6c6c279 | 2008-05-12 21:21:02 +0200 | [diff] [blame] | 6225 | return sret; |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6226 | } |
| 6227 | |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6228 | static void tracing_spd_release_pipe(struct splice_pipe_desc *spd, |
| 6229 | unsigned int idx) |
| 6230 | { |
| 6231 | __free_page(spd->pages[idx]); |
| 6232 | } |
| 6233 | |
Alexey Dobriyan | 28dfef8 | 2009-12-15 16:46:48 -0800 | [diff] [blame] | 6234 | static const struct pipe_buf_operations tracing_pipe_buf_ops = { |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6235 | .confirm = generic_pipe_buf_confirm, |
Al Viro | 92fdd98 | 2014-01-17 07:53:39 -0500 | [diff] [blame] | 6236 | .release = generic_pipe_buf_release, |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6237 | .steal = generic_pipe_buf_steal, |
| 6238 | .get = generic_pipe_buf_get, |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6239 | }; |
| 6240 | |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6241 | static size_t |
Frederic Weisbecker | fa7c7f6 | 2009-02-11 02:51:30 +0100 | [diff] [blame] | 6242 | tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter) |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6243 | { |
| 6244 | size_t count; |
Steven Rostedt (Red Hat) | 74f06bb | 2014-11-17 13:12:22 -0500 | [diff] [blame] | 6245 | int save_len; |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6246 | int ret; |
| 6247 | |
| 6248 | /* Seq buffer is page-sized, exactly what we need. */ |
| 6249 | for (;;) { |
Steven Rostedt (Red Hat) | 74f06bb | 2014-11-17 13:12:22 -0500 | [diff] [blame] | 6250 | save_len = iter->seq.seq.len; |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6251 | ret = print_trace_line(iter); |
Steven Rostedt (Red Hat) | 74f06bb | 2014-11-17 13:12:22 -0500 | [diff] [blame] | 6252 | |
| 6253 | if (trace_seq_has_overflowed(&iter->seq)) { |
| 6254 | iter->seq.seq.len = save_len; |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6255 | break; |
| 6256 | } |
Steven Rostedt (Red Hat) | 74f06bb | 2014-11-17 13:12:22 -0500 | [diff] [blame] | 6257 | |
| 6258 | /* |
| 6259 | * This should not be hit, because it should only |
| 6260 | * be set if the iter->seq overflowed. But check it |
| 6261 | * anyway to be safe. |
| 6262 | */ |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6263 | if (ret == TRACE_TYPE_PARTIAL_LINE) { |
Steven Rostedt (Red Hat) | 74f06bb | 2014-11-17 13:12:22 -0500 | [diff] [blame] | 6264 | iter->seq.seq.len = save_len; |
| 6265 | break; |
| 6266 | } |
| 6267 | |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 6268 | count = trace_seq_used(&iter->seq) - save_len; |
Steven Rostedt (Red Hat) | 74f06bb | 2014-11-17 13:12:22 -0500 | [diff] [blame] | 6269 | if (rem < count) { |
| 6270 | rem = 0; |
| 6271 | iter->seq.seq.len = save_len; |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6272 | break; |
| 6273 | } |
| 6274 | |
Lai Jiangshan | 74e7ff8 | 2009-07-28 20:17:22 +0800 | [diff] [blame] | 6275 | if (ret != TRACE_TYPE_NO_CONSUME) |
| 6276 | trace_consume(iter); |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6277 | rem -= count; |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 6278 | if (!trace_find_next_entry_inc(iter)) { |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6279 | rem = 0; |
| 6280 | iter->ent = NULL; |
| 6281 | break; |
| 6282 | } |
| 6283 | } |
| 6284 | |
| 6285 | return rem; |
| 6286 | } |
| 6287 | |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6288 | static ssize_t tracing_splice_read_pipe(struct file *filp, |
| 6289 | loff_t *ppos, |
| 6290 | struct pipe_inode_info *pipe, |
| 6291 | size_t len, |
| 6292 | unsigned int flags) |
| 6293 | { |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 6294 | struct page *pages_def[PIPE_DEF_BUFFERS]; |
| 6295 | struct partial_page partial_def[PIPE_DEF_BUFFERS]; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6296 | struct trace_iterator *iter = filp->private_data; |
| 6297 | struct splice_pipe_desc spd = { |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 6298 | .pages = pages_def, |
| 6299 | .partial = partial_def, |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6300 | .nr_pages = 0, /* This gets updated below. */ |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 6301 | .nr_pages_max = PIPE_DEF_BUFFERS, |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6302 | .ops = &tracing_pipe_buf_ops, |
| 6303 | .spd_release = tracing_spd_release_pipe, |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6304 | }; |
| 6305 | ssize_t ret; |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6306 | size_t rem; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6307 | unsigned int i; |
| 6308 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 6309 | if (splice_grow_spd(pipe, &spd)) |
| 6310 | return -ENOMEM; |
| 6311 | |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 6312 | mutex_lock(&iter->mutex); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6313 | |
| 6314 | if (iter->trace->splice_read) { |
| 6315 | ret = iter->trace->splice_read(iter, filp, |
| 6316 | ppos, pipe, len, flags); |
| 6317 | if (ret) |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6318 | goto out_err; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6319 | } |
| 6320 | |
| 6321 | ret = tracing_wait_pipe(filp); |
| 6322 | if (ret <= 0) |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6323 | goto out_err; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6324 | |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 6325 | if (!iter->ent && !trace_find_next_entry_inc(iter)) { |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6326 | ret = -EFAULT; |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6327 | goto out_err; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6328 | } |
| 6329 | |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 6330 | trace_event_read_lock(); |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 6331 | trace_access_lock(iter->cpu_file); |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 6332 | |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6333 | /* Fill as many pages as possible. */ |
Al Viro | a786c06 | 2014-04-11 12:01:03 -0400 | [diff] [blame] | 6334 | for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) { |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 6335 | spd.pages[i] = alloc_page(GFP_KERNEL); |
| 6336 | if (!spd.pages[i]) |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6337 | break; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6338 | |
Frederic Weisbecker | fa7c7f6 | 2009-02-11 02:51:30 +0100 | [diff] [blame] | 6339 | rem = tracing_fill_pipe_page(rem, iter); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6340 | |
| 6341 | /* Copy the data into the page, so we can start over. */ |
| 6342 | ret = trace_seq_to_buffer(&iter->seq, |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 6343 | page_address(spd.pages[i]), |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 6344 | trace_seq_used(&iter->seq)); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6345 | if (ret < 0) { |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 6346 | __free_page(spd.pages[i]); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6347 | break; |
| 6348 | } |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 6349 | spd.partial[i].offset = 0; |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 6350 | spd.partial[i].len = trace_seq_used(&iter->seq); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6351 | |
Steven Rostedt | f952075 | 2009-03-02 14:04:40 -0500 | [diff] [blame] | 6352 | trace_seq_init(&iter->seq); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6353 | } |
| 6354 | |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 6355 | trace_access_unlock(iter->cpu_file); |
Lai Jiangshan | 4f53596 | 2009-05-18 19:35:34 +0800 | [diff] [blame] | 6356 | trace_event_read_unlock(); |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 6357 | mutex_unlock(&iter->mutex); |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6358 | |
| 6359 | spd.nr_pages = i; |
| 6360 | |
Steven Rostedt (Red Hat) | a29054d9 | 2016-03-18 15:46:48 -0400 | [diff] [blame] | 6361 | if (i) |
| 6362 | ret = splice_to_pipe(pipe, &spd); |
| 6363 | else |
| 6364 | ret = 0; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 6365 | out: |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 6366 | splice_shrink_spd(&spd); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 6367 | return ret; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6368 | |
Steven Rostedt | 34cd499 | 2009-02-09 12:06:29 -0500 | [diff] [blame] | 6369 | out_err: |
Frederic Weisbecker | d7350c3f | 2009-02-25 06:13:16 +0100 | [diff] [blame] | 6370 | mutex_unlock(&iter->mutex); |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 6371 | goto out; |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 6372 | } |
| 6373 | |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 6374 | static ssize_t |
| 6375 | tracing_entries_read(struct file *filp, char __user *ubuf, |
| 6376 | size_t cnt, loff_t *ppos) |
| 6377 | { |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 6378 | struct inode *inode = file_inode(filp); |
| 6379 | struct trace_array *tr = inode->i_private; |
| 6380 | int cpu = tracing_get_cpu(inode); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 6381 | char buf[64]; |
| 6382 | int r = 0; |
| 6383 | ssize_t ret; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 6384 | |
Steven Rostedt | db526ca | 2009-03-12 13:53:25 -0400 | [diff] [blame] | 6385 | mutex_lock(&trace_types_lock); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 6386 | |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 6387 | if (cpu == RING_BUFFER_ALL_CPUS) { |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 6388 | int cpu, buf_size_same; |
| 6389 | unsigned long size; |
| 6390 | |
| 6391 | size = 0; |
| 6392 | buf_size_same = 1; |
| 6393 | /* check if all cpu sizes are same */ |
| 6394 | for_each_tracing_cpu(cpu) { |
| 6395 | /* fill in the size from first enabled cpu */ |
| 6396 | if (size == 0) |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 6397 | size = per_cpu_ptr(tr->array_buffer.data, cpu)->entries; |
| 6398 | if (size != per_cpu_ptr(tr->array_buffer.data, cpu)->entries) { |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 6399 | buf_size_same = 0; |
| 6400 | break; |
| 6401 | } |
| 6402 | } |
| 6403 | |
| 6404 | if (buf_size_same) { |
| 6405 | if (!ring_buffer_expanded) |
| 6406 | r = sprintf(buf, "%lu (expanded: %lu)\n", |
| 6407 | size >> 10, |
| 6408 | trace_buf_size >> 10); |
| 6409 | else |
| 6410 | r = sprintf(buf, "%lu\n", size >> 10); |
| 6411 | } else |
| 6412 | r = sprintf(buf, "X\n"); |
| 6413 | } else |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 6414 | r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 6415 | |
Steven Rostedt | db526ca | 2009-03-12 13:53:25 -0400 | [diff] [blame] | 6416 | mutex_unlock(&trace_types_lock); |
| 6417 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 6418 | ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 6419 | return ret; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 6420 | } |
| 6421 | |
| 6422 | static ssize_t |
| 6423 | tracing_entries_write(struct file *filp, const char __user *ubuf, |
| 6424 | size_t cnt, loff_t *ppos) |
| 6425 | { |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 6426 | struct inode *inode = file_inode(filp); |
| 6427 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 6428 | unsigned long val; |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 6429 | int ret; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 6430 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 6431 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 6432 | if (ret) |
Steven Rostedt | c6caeeb | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 6433 | return ret; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 6434 | |
| 6435 | /* must have at least 1 entry */ |
| 6436 | if (!val) |
| 6437 | return -EINVAL; |
| 6438 | |
Steven Rostedt | 1696b2b | 2008-11-13 00:09:35 -0500 | [diff] [blame] | 6439 | /* value is in KB */ |
| 6440 | val <<= 10; |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 6441 | ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode)); |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 6442 | if (ret < 0) |
| 6443 | return ret; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 6444 | |
Jiri Olsa | cf8517c | 2009-10-23 19:36:16 -0400 | [diff] [blame] | 6445 | *ppos += cnt; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 6446 | |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 6447 | return cnt; |
| 6448 | } |
Steven Rostedt | bf5e651 | 2008-11-10 21:46:00 -0500 | [diff] [blame] | 6449 | |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 6450 | static ssize_t |
Vaibhav Nagarnaik | f81ab07 | 2011-08-16 14:46:15 -0700 | [diff] [blame] | 6451 | tracing_total_entries_read(struct file *filp, char __user *ubuf, |
| 6452 | size_t cnt, loff_t *ppos) |
| 6453 | { |
| 6454 | struct trace_array *tr = filp->private_data; |
| 6455 | char buf[64]; |
| 6456 | int r, cpu; |
| 6457 | unsigned long size = 0, expanded_size = 0; |
| 6458 | |
| 6459 | mutex_lock(&trace_types_lock); |
| 6460 | for_each_tracing_cpu(cpu) { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 6461 | size += per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10; |
Vaibhav Nagarnaik | f81ab07 | 2011-08-16 14:46:15 -0700 | [diff] [blame] | 6462 | if (!ring_buffer_expanded) |
| 6463 | expanded_size += trace_buf_size >> 10; |
| 6464 | } |
| 6465 | if (ring_buffer_expanded) |
| 6466 | r = sprintf(buf, "%lu\n", size); |
| 6467 | else |
| 6468 | r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size); |
| 6469 | mutex_unlock(&trace_types_lock); |
| 6470 | |
| 6471 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 6472 | } |
| 6473 | |
| 6474 | static ssize_t |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 6475 | tracing_free_buffer_write(struct file *filp, const char __user *ubuf, |
| 6476 | size_t cnt, loff_t *ppos) |
| 6477 | { |
| 6478 | /* |
| 6479 | * There is no need to read what the user has written, this function |
| 6480 | * is just to make sure that there is no error when "echo" is used |
| 6481 | */ |
| 6482 | |
| 6483 | *ppos += cnt; |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 6484 | |
| 6485 | return cnt; |
| 6486 | } |
| 6487 | |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 6488 | static int |
| 6489 | tracing_free_buffer_release(struct inode *inode, struct file *filp) |
| 6490 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6491 | struct trace_array *tr = inode->i_private; |
| 6492 | |
Steven Rostedt | cf30cf6 | 2011-06-14 22:44:07 -0400 | [diff] [blame] | 6493 | /* disable tracing ? */ |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 6494 | if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE) |
Alexander Z Lam | 711e124 | 2013-08-02 18:36:15 -0700 | [diff] [blame] | 6495 | tracer_tracing_off(tr); |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 6496 | /* resize the ring buffer to 0 */ |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6497 | tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS); |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 6498 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6499 | trace_array_put(tr); |
| 6500 | |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 6501 | return 0; |
| 6502 | } |
| 6503 | |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 6504 | static ssize_t |
| 6505 | tracing_mark_write(struct file *filp, const char __user *ubuf, |
| 6506 | size_t cnt, loff_t *fpos) |
| 6507 | { |
Alexander Z Lam | 2d71619 | 2013-07-01 15:31:24 -0700 | [diff] [blame] | 6508 | struct trace_array *tr = filp->private_data; |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 6509 | struct ring_buffer_event *event; |
Steven Rostedt (VMware) | 3dd8095 | 2018-05-09 14:17:48 -0400 | [diff] [blame] | 6510 | enum event_trigger_type tt = ETT_NONE; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 6511 | struct trace_buffer *buffer; |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 6512 | struct print_entry *entry; |
| 6513 | unsigned long irq_flags; |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 6514 | ssize_t written; |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 6515 | int size; |
| 6516 | int len; |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 6517 | |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6518 | /* Used in tracing_mark_raw_write() as well */ |
Rasmus Villemoes | 0f5e5a3 | 2019-03-20 09:17:57 +0100 | [diff] [blame] | 6519 | #define FAULTED_STR "<faulted>" |
| 6520 | #define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */ |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 6521 | |
Steven Rostedt | c76f069 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 6522 | if (tracing_disabled) |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 6523 | return -EINVAL; |
| 6524 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 6525 | if (!(tr->trace_flags & TRACE_ITER_MARKERS)) |
Mandeep Singh Baines | 5224c3a | 2012-09-07 18:12:19 -0700 | [diff] [blame] | 6526 | return -EINVAL; |
| 6527 | |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 6528 | if (cnt > TRACE_BUF_SIZE) |
| 6529 | cnt = TRACE_BUF_SIZE; |
| 6530 | |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 6531 | BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE); |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 6532 | |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 6533 | local_save_flags(irq_flags); |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6534 | size = sizeof(*entry) + cnt + 2; /* add '\0' and possible '\n' */ |
| 6535 | |
| 6536 | /* If less than "<faulted>", then make sure we can still add that */ |
| 6537 | if (cnt < FAULTED_SIZE) |
| 6538 | size += FAULTED_SIZE - cnt; |
| 6539 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 6540 | buffer = tr->array_buffer.buffer; |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 6541 | event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, |
| 6542 | irq_flags, preempt_count()); |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6543 | if (unlikely(!event)) |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 6544 | /* Ring buffer disabled, return as if not open for write */ |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6545 | return -EBADF; |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 6546 | |
| 6547 | entry = ring_buffer_event_data(event); |
| 6548 | entry->ip = _THIS_IP_; |
| 6549 | |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6550 | len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt); |
| 6551 | if (len) { |
Rasmus Villemoes | 0f5e5a3 | 2019-03-20 09:17:57 +0100 | [diff] [blame] | 6552 | memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE); |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6553 | cnt = FAULTED_SIZE; |
| 6554 | written = -EFAULT; |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 6555 | } else |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6556 | written = cnt; |
| 6557 | len = cnt; |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 6558 | |
Steven Rostedt (VMware) | 3dd8095 | 2018-05-09 14:17:48 -0400 | [diff] [blame] | 6559 | if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) { |
| 6560 | /* do not add \n before testing triggers, but add \0 */ |
| 6561 | entry->buf[cnt] = '\0'; |
| 6562 | tt = event_triggers_call(tr->trace_marker_file, entry, event); |
| 6563 | } |
| 6564 | |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 6565 | if (entry->buf[cnt - 1] != '\n') { |
| 6566 | entry->buf[cnt] = '\n'; |
| 6567 | entry->buf[cnt + 1] = '\0'; |
| 6568 | } else |
| 6569 | entry->buf[cnt] = '\0'; |
| 6570 | |
Steven Rostedt | 7ffbd48 | 2012-10-11 12:14:25 -0400 | [diff] [blame] | 6571 | __buffer_unlock_commit(buffer, event); |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 6572 | |
Steven Rostedt (VMware) | 3dd8095 | 2018-05-09 14:17:48 -0400 | [diff] [blame] | 6573 | if (tt) |
| 6574 | event_triggers_post_call(tr->trace_marker_file, tt); |
| 6575 | |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6576 | if (written > 0) |
| 6577 | *fpos += written; |
Steven Rostedt | d696b58 | 2011-09-22 11:50:27 -0400 | [diff] [blame] | 6578 | |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 6579 | return written; |
| 6580 | } |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 6581 | |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 6582 | /* Limit it for now to 3K (including tag) */ |
| 6583 | #define RAW_DATA_MAX_SIZE (1024*3) |
| 6584 | |
| 6585 | static ssize_t |
| 6586 | tracing_mark_raw_write(struct file *filp, const char __user *ubuf, |
| 6587 | size_t cnt, loff_t *fpos) |
| 6588 | { |
| 6589 | struct trace_array *tr = filp->private_data; |
| 6590 | struct ring_buffer_event *event; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 6591 | struct trace_buffer *buffer; |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 6592 | struct raw_data_entry *entry; |
| 6593 | unsigned long irq_flags; |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 6594 | ssize_t written; |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 6595 | int size; |
| 6596 | int len; |
| 6597 | |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6598 | #define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int)) |
| 6599 | |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 6600 | if (tracing_disabled) |
| 6601 | return -EINVAL; |
| 6602 | |
| 6603 | if (!(tr->trace_flags & TRACE_ITER_MARKERS)) |
| 6604 | return -EINVAL; |
| 6605 | |
| 6606 | /* The marker must at least have a tag id */ |
| 6607 | if (cnt < sizeof(unsigned int) || cnt > RAW_DATA_MAX_SIZE) |
| 6608 | return -EINVAL; |
| 6609 | |
| 6610 | if (cnt > TRACE_BUF_SIZE) |
| 6611 | cnt = TRACE_BUF_SIZE; |
| 6612 | |
| 6613 | BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE); |
| 6614 | |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 6615 | local_save_flags(irq_flags); |
| 6616 | size = sizeof(*entry) + cnt; |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6617 | if (cnt < FAULT_SIZE_ID) |
| 6618 | size += FAULT_SIZE_ID - cnt; |
| 6619 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 6620 | buffer = tr->array_buffer.buffer; |
Steven Rostedt (Red Hat) | 3e9a8aa | 2016-11-23 11:29:58 -0500 | [diff] [blame] | 6621 | event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size, |
| 6622 | irq_flags, preempt_count()); |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6623 | if (!event) |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 6624 | /* Ring buffer disabled, return as if not open for write */ |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6625 | return -EBADF; |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 6626 | |
| 6627 | entry = ring_buffer_event_data(event); |
| 6628 | |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6629 | len = __copy_from_user_inatomic(&entry->id, ubuf, cnt); |
| 6630 | if (len) { |
| 6631 | entry->id = -1; |
Rasmus Villemoes | 0f5e5a3 | 2019-03-20 09:17:57 +0100 | [diff] [blame] | 6632 | memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE); |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6633 | written = -EFAULT; |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 6634 | } else |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6635 | written = cnt; |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 6636 | |
| 6637 | __buffer_unlock_commit(buffer, event); |
| 6638 | |
Steven Rostedt (Red Hat) | 656c7f0 | 2016-12-08 12:40:18 -0500 | [diff] [blame] | 6639 | if (written > 0) |
| 6640 | *fpos += written; |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 6641 | |
Marcin Slusarz | 1aa54bc | 2010-07-28 01:18:01 +0200 | [diff] [blame] | 6642 | return written; |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 6643 | } |
| 6644 | |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 6645 | static int tracing_clock_show(struct seq_file *m, void *v) |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6646 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6647 | struct trace_array *tr = m->private; |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6648 | int i; |
| 6649 | |
| 6650 | for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 6651 | seq_printf(m, |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6652 | "%s%s%s%s", i ? " " : "", |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6653 | i == tr->clock_id ? "[" : "", trace_clocks[i].name, |
| 6654 | i == tr->clock_id ? "]" : ""); |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 6655 | seq_putc(m, '\n'); |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6656 | |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 6657 | return 0; |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6658 | } |
| 6659 | |
Tom Zanussi | d71bd34 | 2018-01-15 20:52:07 -0600 | [diff] [blame] | 6660 | int tracing_set_clock(struct trace_array *tr, const char *clockstr) |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6661 | { |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6662 | int i; |
| 6663 | |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6664 | for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) { |
| 6665 | if (strcmp(trace_clocks[i].name, clockstr) == 0) |
| 6666 | break; |
| 6667 | } |
| 6668 | if (i == ARRAY_SIZE(trace_clocks)) |
| 6669 | return -EINVAL; |
| 6670 | |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6671 | mutex_lock(&trace_types_lock); |
| 6672 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6673 | tr->clock_id = i; |
| 6674 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 6675 | ring_buffer_set_clock(tr->array_buffer.buffer, trace_clocks[i].func); |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6676 | |
David Sharp | 60303ed | 2012-10-11 16:27:52 -0700 | [diff] [blame] | 6677 | /* |
| 6678 | * New clock may not be consistent with the previous clock. |
| 6679 | * Reset the buffer so that it doesn't have incomparable timestamps. |
| 6680 | */ |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 6681 | tracing_reset_online_cpus(&tr->array_buffer); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6682 | |
| 6683 | #ifdef CONFIG_TRACER_MAX_TRACE |
Baohong Liu | 170b3b1 | 2017-09-05 16:57:19 -0500 | [diff] [blame] | 6684 | if (tr->max_buffer.buffer) |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6685 | ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func); |
Alexander Z Lam | 9457158 | 2013-08-02 18:36:16 -0700 | [diff] [blame] | 6686 | tracing_reset_online_cpus(&tr->max_buffer); |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 6687 | #endif |
David Sharp | 60303ed | 2012-10-11 16:27:52 -0700 | [diff] [blame] | 6688 | |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6689 | mutex_unlock(&trace_types_lock); |
| 6690 | |
Steven Rostedt | e1e232c | 2014-02-10 23:38:46 -0500 | [diff] [blame] | 6691 | return 0; |
| 6692 | } |
| 6693 | |
| 6694 | static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf, |
| 6695 | size_t cnt, loff_t *fpos) |
| 6696 | { |
| 6697 | struct seq_file *m = filp->private_data; |
| 6698 | struct trace_array *tr = m->private; |
| 6699 | char buf[64]; |
| 6700 | const char *clockstr; |
| 6701 | int ret; |
| 6702 | |
| 6703 | if (cnt >= sizeof(buf)) |
| 6704 | return -EINVAL; |
| 6705 | |
Wang Xiaoqiang | 4afe649 | 2016-04-18 15:23:29 +0800 | [diff] [blame] | 6706 | if (copy_from_user(buf, ubuf, cnt)) |
Steven Rostedt | e1e232c | 2014-02-10 23:38:46 -0500 | [diff] [blame] | 6707 | return -EFAULT; |
| 6708 | |
| 6709 | buf[cnt] = 0; |
| 6710 | |
| 6711 | clockstr = strstrip(buf); |
| 6712 | |
| 6713 | ret = tracing_set_clock(tr, clockstr); |
| 6714 | if (ret) |
| 6715 | return ret; |
| 6716 | |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 6717 | *fpos += cnt; |
| 6718 | |
| 6719 | return cnt; |
| 6720 | } |
| 6721 | |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 6722 | static int tracing_clock_open(struct inode *inode, struct file *file) |
| 6723 | { |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6724 | struct trace_array *tr = inode->i_private; |
| 6725 | int ret; |
| 6726 | |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 6727 | ret = tracing_check_open_get_tr(tr); |
| 6728 | if (ret) |
| 6729 | return ret; |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 6730 | |
| 6731 | ret = single_open(file, tracing_clock_show, inode->i_private); |
| 6732 | if (ret < 0) |
| 6733 | trace_array_put(tr); |
| 6734 | |
| 6735 | return ret; |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 6736 | } |
| 6737 | |
Tom Zanussi | 2c1ea60 | 2018-01-15 20:51:41 -0600 | [diff] [blame] | 6738 | static int tracing_time_stamp_mode_show(struct seq_file *m, void *v) |
| 6739 | { |
| 6740 | struct trace_array *tr = m->private; |
| 6741 | |
| 6742 | mutex_lock(&trace_types_lock); |
| 6743 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 6744 | if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer)) |
Tom Zanussi | 2c1ea60 | 2018-01-15 20:51:41 -0600 | [diff] [blame] | 6745 | seq_puts(m, "delta [absolute]\n"); |
| 6746 | else |
| 6747 | seq_puts(m, "[delta] absolute\n"); |
| 6748 | |
| 6749 | mutex_unlock(&trace_types_lock); |
| 6750 | |
| 6751 | return 0; |
| 6752 | } |
| 6753 | |
| 6754 | static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file) |
| 6755 | { |
| 6756 | struct trace_array *tr = inode->i_private; |
| 6757 | int ret; |
| 6758 | |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 6759 | ret = tracing_check_open_get_tr(tr); |
| 6760 | if (ret) |
| 6761 | return ret; |
Tom Zanussi | 2c1ea60 | 2018-01-15 20:51:41 -0600 | [diff] [blame] | 6762 | |
| 6763 | ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private); |
| 6764 | if (ret < 0) |
| 6765 | trace_array_put(tr); |
| 6766 | |
| 6767 | return ret; |
| 6768 | } |
| 6769 | |
Tom Zanussi | 00b4145 | 2018-01-15 20:51:39 -0600 | [diff] [blame] | 6770 | int tracing_set_time_stamp_abs(struct trace_array *tr, bool abs) |
| 6771 | { |
| 6772 | int ret = 0; |
| 6773 | |
| 6774 | mutex_lock(&trace_types_lock); |
| 6775 | |
| 6776 | if (abs && tr->time_stamp_abs_ref++) |
| 6777 | goto out; |
| 6778 | |
| 6779 | if (!abs) { |
| 6780 | if (WARN_ON_ONCE(!tr->time_stamp_abs_ref)) { |
| 6781 | ret = -EINVAL; |
| 6782 | goto out; |
| 6783 | } |
| 6784 | |
| 6785 | if (--tr->time_stamp_abs_ref) |
| 6786 | goto out; |
| 6787 | } |
| 6788 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 6789 | ring_buffer_set_time_stamp_abs(tr->array_buffer.buffer, abs); |
Tom Zanussi | 00b4145 | 2018-01-15 20:51:39 -0600 | [diff] [blame] | 6790 | |
| 6791 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 6792 | if (tr->max_buffer.buffer) |
| 6793 | ring_buffer_set_time_stamp_abs(tr->max_buffer.buffer, abs); |
| 6794 | #endif |
| 6795 | out: |
| 6796 | mutex_unlock(&trace_types_lock); |
| 6797 | |
| 6798 | return ret; |
| 6799 | } |
| 6800 | |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6801 | struct ftrace_buffer_info { |
| 6802 | struct trace_iterator iter; |
| 6803 | void *spare; |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 6804 | unsigned int spare_cpu; |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6805 | unsigned int read; |
| 6806 | }; |
| 6807 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6808 | #ifdef CONFIG_TRACER_SNAPSHOT |
| 6809 | static int tracing_snapshot_open(struct inode *inode, struct file *file) |
| 6810 | { |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 6811 | struct trace_array *tr = inode->i_private; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6812 | struct trace_iterator *iter; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6813 | struct seq_file *m; |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 6814 | int ret; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6815 | |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 6816 | ret = tracing_check_open_get_tr(tr); |
| 6817 | if (ret) |
| 6818 | return ret; |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 6819 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6820 | if (file->f_mode & FMODE_READ) { |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 6821 | iter = __tracing_open(inode, file, true); |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6822 | if (IS_ERR(iter)) |
| 6823 | ret = PTR_ERR(iter); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6824 | } else { |
| 6825 | /* Writes still need the seq_file to hold the private data */ |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 6826 | ret = -ENOMEM; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6827 | m = kzalloc(sizeof(*m), GFP_KERNEL); |
| 6828 | if (!m) |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 6829 | goto out; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6830 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 6831 | if (!iter) { |
| 6832 | kfree(m); |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 6833 | goto out; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6834 | } |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 6835 | ret = 0; |
| 6836 | |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 6837 | iter->tr = tr; |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 6838 | iter->array_buffer = &tr->max_buffer; |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 6839 | iter->cpu_file = tracing_get_cpu(inode); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6840 | m->private = iter; |
| 6841 | file->private_data = m; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6842 | } |
Alexander Z Lam | f77d09a | 2013-07-18 11:18:44 -0700 | [diff] [blame] | 6843 | out: |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 6844 | if (ret < 0) |
| 6845 | trace_array_put(tr); |
| 6846 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6847 | return ret; |
| 6848 | } |
| 6849 | |
| 6850 | static ssize_t |
| 6851 | tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 6852 | loff_t *ppos) |
| 6853 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6854 | struct seq_file *m = filp->private_data; |
| 6855 | struct trace_iterator *iter = m->private; |
| 6856 | struct trace_array *tr = iter->tr; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6857 | unsigned long val; |
| 6858 | int ret; |
| 6859 | |
| 6860 | ret = tracing_update_buffers(); |
| 6861 | if (ret < 0) |
| 6862 | return ret; |
| 6863 | |
| 6864 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 6865 | if (ret) |
| 6866 | return ret; |
| 6867 | |
| 6868 | mutex_lock(&trace_types_lock); |
| 6869 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6870 | if (tr->current_trace->use_max_tr) { |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6871 | ret = -EBUSY; |
| 6872 | goto out; |
| 6873 | } |
| 6874 | |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 6875 | arch_spin_lock(&tr->max_lock); |
| 6876 | if (tr->cond_snapshot) |
| 6877 | ret = -EBUSY; |
| 6878 | arch_spin_unlock(&tr->max_lock); |
| 6879 | if (ret) |
| 6880 | goto out; |
| 6881 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6882 | switch (val) { |
| 6883 | case 0: |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 6884 | if (iter->cpu_file != RING_BUFFER_ALL_CPUS) { |
| 6885 | ret = -EINVAL; |
| 6886 | break; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6887 | } |
Steven Rostedt (Red Hat) | 3209cff | 2013-03-12 11:17:54 -0400 | [diff] [blame] | 6888 | if (tr->allocated_snapshot) |
| 6889 | free_snapshot(tr); |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6890 | break; |
| 6891 | case 1: |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 6892 | /* Only allow per-cpu swap if the ring buffer supports it */ |
| 6893 | #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP |
| 6894 | if (iter->cpu_file != RING_BUFFER_ALL_CPUS) { |
| 6895 | ret = -EINVAL; |
| 6896 | break; |
| 6897 | } |
| 6898 | #endif |
Eiichi Tsukata | 46cc0b4 | 2019-06-25 10:29:10 +0900 | [diff] [blame] | 6899 | if (tr->allocated_snapshot) |
| 6900 | ret = resize_buffer_duplicate_size(&tr->max_buffer, |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 6901 | &tr->array_buffer, iter->cpu_file); |
Eiichi Tsukata | 46cc0b4 | 2019-06-25 10:29:10 +0900 | [diff] [blame] | 6902 | else |
Steven Rostedt (VMware) | 2824f50 | 2018-05-28 10:56:36 -0400 | [diff] [blame] | 6903 | ret = tracing_alloc_snapshot_instance(tr); |
Eiichi Tsukata | 46cc0b4 | 2019-06-25 10:29:10 +0900 | [diff] [blame] | 6904 | if (ret < 0) |
| 6905 | break; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6906 | local_irq_disable(); |
| 6907 | /* Now, we're going to swap */ |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 6908 | if (iter->cpu_file == RING_BUFFER_ALL_CPUS) |
Tom Zanussi | a35873a | 2019-02-13 17:42:45 -0600 | [diff] [blame] | 6909 | update_max_tr(tr, current, smp_processor_id(), NULL); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 6910 | else |
Steven Rostedt (Red Hat) | ce9bae5 | 2013-03-05 21:23:55 -0500 | [diff] [blame] | 6911 | update_max_tr_single(tr, current, iter->cpu_file); |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6912 | local_irq_enable(); |
| 6913 | break; |
| 6914 | default: |
Steven Rostedt (Red Hat) | 45ad21c | 2013-03-05 18:25:02 -0500 | [diff] [blame] | 6915 | if (tr->allocated_snapshot) { |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 6916 | if (iter->cpu_file == RING_BUFFER_ALL_CPUS) |
| 6917 | tracing_reset_online_cpus(&tr->max_buffer); |
| 6918 | else |
Steven Rostedt (VMware) | a47b53e | 2019-08-13 12:14:35 -0400 | [diff] [blame] | 6919 | tracing_reset_cpu(&tr->max_buffer, iter->cpu_file); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 6920 | } |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6921 | break; |
| 6922 | } |
| 6923 | |
| 6924 | if (ret >= 0) { |
| 6925 | *ppos += cnt; |
| 6926 | ret = cnt; |
| 6927 | } |
| 6928 | out: |
| 6929 | mutex_unlock(&trace_types_lock); |
| 6930 | return ret; |
| 6931 | } |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6932 | |
| 6933 | static int tracing_snapshot_release(struct inode *inode, struct file *file) |
| 6934 | { |
| 6935 | struct seq_file *m = file->private_data; |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 6936 | int ret; |
| 6937 | |
| 6938 | ret = tracing_release(inode, file); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6939 | |
| 6940 | if (file->f_mode & FMODE_READ) |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 6941 | return ret; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 6942 | |
| 6943 | /* If write only, the seq_file is just a stub */ |
| 6944 | if (m) |
| 6945 | kfree(m->private); |
| 6946 | kfree(m); |
| 6947 | |
| 6948 | return 0; |
| 6949 | } |
| 6950 | |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6951 | static int tracing_buffers_open(struct inode *inode, struct file *filp); |
| 6952 | static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf, |
| 6953 | size_t count, loff_t *ppos); |
| 6954 | static int tracing_buffers_release(struct inode *inode, struct file *file); |
| 6955 | static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos, |
| 6956 | struct pipe_inode_info *pipe, size_t len, unsigned int flags); |
| 6957 | |
| 6958 | static int snapshot_raw_open(struct inode *inode, struct file *filp) |
| 6959 | { |
| 6960 | struct ftrace_buffer_info *info; |
| 6961 | int ret; |
| 6962 | |
Steven Rostedt (VMware) | 17911ff | 2019-10-11 17:22:50 -0400 | [diff] [blame] | 6963 | /* The following checks for tracefs lockdown */ |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6964 | ret = tracing_buffers_open(inode, filp); |
| 6965 | if (ret < 0) |
| 6966 | return ret; |
| 6967 | |
| 6968 | info = filp->private_data; |
| 6969 | |
| 6970 | if (info->iter.trace->use_max_tr) { |
| 6971 | tracing_buffers_release(inode, filp); |
| 6972 | return -EBUSY; |
| 6973 | } |
| 6974 | |
| 6975 | info->iter.snapshot = true; |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 6976 | info->iter.array_buffer = &info->iter.tr->max_buffer; |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 6977 | |
| 6978 | return ret; |
| 6979 | } |
| 6980 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 6981 | #endif /* CONFIG_TRACER_SNAPSHOT */ |
| 6982 | |
| 6983 | |
Stanislav Fomichev | 6508fa7 | 2014-07-18 15:17:27 +0400 | [diff] [blame] | 6984 | static const struct file_operations tracing_thresh_fops = { |
| 6985 | .open = tracing_open_generic, |
| 6986 | .read = tracing_thresh_read, |
| 6987 | .write = tracing_thresh_write, |
| 6988 | .llseek = generic_file_llseek, |
| 6989 | }; |
| 6990 | |
Steven Rostedt (Red Hat) | f971cc9 | 2016-09-07 12:45:09 -0400 | [diff] [blame] | 6991 | #if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER) |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 6992 | static const struct file_operations tracing_max_lat_fops = { |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 6993 | .open = tracing_open_generic, |
| 6994 | .read = tracing_max_lat_read, |
| 6995 | .write = tracing_max_lat_write, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 6996 | .llseek = generic_file_llseek, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6997 | }; |
Chen Gang | e428abb | 2015-11-10 05:15:15 +0800 | [diff] [blame] | 6998 | #endif |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 6999 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 7000 | static const struct file_operations set_tracer_fops = { |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 7001 | .open = tracing_open_generic, |
| 7002 | .read = tracing_set_trace_read, |
| 7003 | .write = tracing_set_trace_write, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 7004 | .llseek = generic_file_llseek, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7005 | }; |
| 7006 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 7007 | static const struct file_operations tracing_pipe_fops = { |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 7008 | .open = tracing_open_pipe, |
Soeren Sandmann Pedersen | 2a2cc8f | 2008-05-12 21:20:49 +0200 | [diff] [blame] | 7009 | .poll = tracing_poll_pipe, |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 7010 | .read = tracing_read_pipe, |
Eduard - Gabriel Munteanu | 3c56819 | 2009-02-09 08:15:56 +0200 | [diff] [blame] | 7011 | .splice_read = tracing_splice_read_pipe, |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 7012 | .release = tracing_release_pipe, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 7013 | .llseek = no_llseek, |
Steven Rostedt | b3806b4 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 7014 | }; |
| 7015 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 7016 | static const struct file_operations tracing_entries_fops = { |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 7017 | .open = tracing_open_generic_tr, |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 7018 | .read = tracing_entries_read, |
| 7019 | .write = tracing_entries_write, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 7020 | .llseek = generic_file_llseek, |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 7021 | .release = tracing_release_generic_tr, |
Steven Rostedt | a98a3c3 | 2008-05-12 21:20:59 +0200 | [diff] [blame] | 7022 | }; |
| 7023 | |
Vaibhav Nagarnaik | f81ab07 | 2011-08-16 14:46:15 -0700 | [diff] [blame] | 7024 | static const struct file_operations tracing_total_entries_fops = { |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 7025 | .open = tracing_open_generic_tr, |
Vaibhav Nagarnaik | f81ab07 | 2011-08-16 14:46:15 -0700 | [diff] [blame] | 7026 | .read = tracing_total_entries_read, |
| 7027 | .llseek = generic_file_llseek, |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 7028 | .release = tracing_release_generic_tr, |
Vaibhav Nagarnaik | f81ab07 | 2011-08-16 14:46:15 -0700 | [diff] [blame] | 7029 | }; |
| 7030 | |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 7031 | static const struct file_operations tracing_free_buffer_fops = { |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 7032 | .open = tracing_open_generic_tr, |
Vaibhav Nagarnaik | 4f271a2 | 2011-06-13 17:51:57 -0700 | [diff] [blame] | 7033 | .write = tracing_free_buffer_write, |
| 7034 | .release = tracing_free_buffer_release, |
| 7035 | }; |
| 7036 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 7037 | static const struct file_operations tracing_mark_fops = { |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 7038 | .open = tracing_open_generic_tr, |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 7039 | .write = tracing_mark_write, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 7040 | .llseek = generic_file_llseek, |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 7041 | .release = tracing_release_generic_tr, |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 7042 | }; |
| 7043 | |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 7044 | static const struct file_operations tracing_mark_raw_fops = { |
| 7045 | .open = tracing_open_generic_tr, |
| 7046 | .write = tracing_mark_raw_write, |
| 7047 | .llseek = generic_file_llseek, |
| 7048 | .release = tracing_release_generic_tr, |
| 7049 | }; |
| 7050 | |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 7051 | static const struct file_operations trace_clock_fops = { |
Li Zefan | 13f16d2 | 2009-12-08 11:16:11 +0800 | [diff] [blame] | 7052 | .open = tracing_clock_open, |
| 7053 | .read = seq_read, |
| 7054 | .llseek = seq_lseek, |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 7055 | .release = tracing_single_release_tr, |
Zhaolei | 5079f32 | 2009-08-25 16:12:56 +0800 | [diff] [blame] | 7056 | .write = tracing_clock_write, |
| 7057 | }; |
| 7058 | |
Tom Zanussi | 2c1ea60 | 2018-01-15 20:51:41 -0600 | [diff] [blame] | 7059 | static const struct file_operations trace_time_stamp_mode_fops = { |
| 7060 | .open = tracing_time_stamp_mode_open, |
| 7061 | .read = seq_read, |
| 7062 | .llseek = seq_lseek, |
| 7063 | .release = tracing_single_release_tr, |
| 7064 | }; |
| 7065 | |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 7066 | #ifdef CONFIG_TRACER_SNAPSHOT |
| 7067 | static const struct file_operations snapshot_fops = { |
| 7068 | .open = tracing_snapshot_open, |
| 7069 | .read = seq_read, |
| 7070 | .write = tracing_snapshot_write, |
Steven Rostedt (Red Hat) | 098c879 | 2013-12-21 17:39:40 -0500 | [diff] [blame] | 7071 | .llseek = tracing_lseek, |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7072 | .release = tracing_snapshot_release, |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 7073 | }; |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 7074 | |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 7075 | static const struct file_operations snapshot_raw_fops = { |
| 7076 | .open = snapshot_raw_open, |
| 7077 | .read = tracing_buffers_read, |
| 7078 | .release = tracing_buffers_release, |
| 7079 | .splice_read = tracing_buffers_splice_read, |
| 7080 | .llseek = no_llseek, |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7081 | }; |
| 7082 | |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 7083 | #endif /* CONFIG_TRACER_SNAPSHOT */ |
| 7084 | |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7085 | #define TRACING_LOG_ERRS_MAX 8 |
| 7086 | #define TRACING_LOG_LOC_MAX 128 |
| 7087 | |
| 7088 | #define CMD_PREFIX " Command: " |
| 7089 | |
| 7090 | struct err_info { |
| 7091 | const char **errs; /* ptr to loc-specific array of err strings */ |
| 7092 | u8 type; /* index into errs -> specific err string */ |
| 7093 | u8 pos; /* MAX_FILTER_STR_VAL = 256 */ |
| 7094 | u64 ts; |
| 7095 | }; |
| 7096 | |
| 7097 | struct tracing_log_err { |
| 7098 | struct list_head list; |
| 7099 | struct err_info info; |
| 7100 | char loc[TRACING_LOG_LOC_MAX]; /* err location */ |
| 7101 | char cmd[MAX_FILTER_STR_VAL]; /* what caused err */ |
| 7102 | }; |
| 7103 | |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7104 | static DEFINE_MUTEX(tracing_err_log_lock); |
| 7105 | |
YueHaibing | ff585c5 | 2019-06-14 23:32:10 +0800 | [diff] [blame] | 7106 | static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr) |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7107 | { |
| 7108 | struct tracing_log_err *err; |
| 7109 | |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7110 | if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) { |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7111 | err = kzalloc(sizeof(*err), GFP_KERNEL); |
| 7112 | if (!err) |
| 7113 | err = ERR_PTR(-ENOMEM); |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7114 | tr->n_err_log_entries++; |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7115 | |
| 7116 | return err; |
| 7117 | } |
| 7118 | |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7119 | err = list_first_entry(&tr->err_log, struct tracing_log_err, list); |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7120 | list_del(&err->list); |
| 7121 | |
| 7122 | return err; |
| 7123 | } |
| 7124 | |
| 7125 | /** |
| 7126 | * err_pos - find the position of a string within a command for error careting |
| 7127 | * @cmd: The tracing command that caused the error |
| 7128 | * @str: The string to position the caret at within @cmd |
| 7129 | * |
| 7130 | * Finds the position of the first occurence of @str within @cmd. The |
| 7131 | * return value can be passed to tracing_log_err() for caret placement |
| 7132 | * within @cmd. |
| 7133 | * |
| 7134 | * Returns the index within @cmd of the first occurence of @str or 0 |
| 7135 | * if @str was not found. |
| 7136 | */ |
| 7137 | unsigned int err_pos(char *cmd, const char *str) |
| 7138 | { |
| 7139 | char *found; |
| 7140 | |
| 7141 | if (WARN_ON(!strlen(cmd))) |
| 7142 | return 0; |
| 7143 | |
| 7144 | found = strstr(cmd, str); |
| 7145 | if (found) |
| 7146 | return found - cmd; |
| 7147 | |
| 7148 | return 0; |
| 7149 | } |
| 7150 | |
| 7151 | /** |
| 7152 | * tracing_log_err - write an error to the tracing error log |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7153 | * @tr: The associated trace array for the error (NULL for top level array) |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7154 | * @loc: A string describing where the error occurred |
| 7155 | * @cmd: The tracing command that caused the error |
| 7156 | * @errs: The array of loc-specific static error strings |
| 7157 | * @type: The index into errs[], which produces the specific static err string |
| 7158 | * @pos: The position the caret should be placed in the cmd |
| 7159 | * |
| 7160 | * Writes an error into tracing/error_log of the form: |
| 7161 | * |
| 7162 | * <loc>: error: <text> |
| 7163 | * Command: <cmd> |
| 7164 | * ^ |
| 7165 | * |
| 7166 | * tracing/error_log is a small log file containing the last |
| 7167 | * TRACING_LOG_ERRS_MAX errors (8). Memory for errors isn't allocated |
| 7168 | * unless there has been a tracing error, and the error log can be |
| 7169 | * cleared and have its memory freed by writing the empty string in |
| 7170 | * truncation mode to it i.e. echo > tracing/error_log. |
| 7171 | * |
| 7172 | * NOTE: the @errs array along with the @type param are used to |
| 7173 | * produce a static error string - this string is not copied and saved |
| 7174 | * when the error is logged - only a pointer to it is saved. See |
| 7175 | * existing callers for examples of how static strings are typically |
| 7176 | * defined for use with tracing_log_err(). |
| 7177 | */ |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7178 | void tracing_log_err(struct trace_array *tr, |
| 7179 | const char *loc, const char *cmd, |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7180 | const char **errs, u8 type, u8 pos) |
| 7181 | { |
| 7182 | struct tracing_log_err *err; |
| 7183 | |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7184 | if (!tr) |
| 7185 | tr = &global_trace; |
| 7186 | |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7187 | mutex_lock(&tracing_err_log_lock); |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7188 | err = get_tracing_log_err(tr); |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7189 | if (PTR_ERR(err) == -ENOMEM) { |
| 7190 | mutex_unlock(&tracing_err_log_lock); |
| 7191 | return; |
| 7192 | } |
| 7193 | |
| 7194 | snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc); |
| 7195 | snprintf(err->cmd, MAX_FILTER_STR_VAL,"\n" CMD_PREFIX "%s\n", cmd); |
| 7196 | |
| 7197 | err->info.errs = errs; |
| 7198 | err->info.type = type; |
| 7199 | err->info.pos = pos; |
| 7200 | err->info.ts = local_clock(); |
| 7201 | |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7202 | list_add_tail(&err->list, &tr->err_log); |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7203 | mutex_unlock(&tracing_err_log_lock); |
| 7204 | } |
| 7205 | |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7206 | static void clear_tracing_err_log(struct trace_array *tr) |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7207 | { |
| 7208 | struct tracing_log_err *err, *next; |
| 7209 | |
| 7210 | mutex_lock(&tracing_err_log_lock); |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7211 | list_for_each_entry_safe(err, next, &tr->err_log, list) { |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7212 | list_del(&err->list); |
| 7213 | kfree(err); |
| 7214 | } |
| 7215 | |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7216 | tr->n_err_log_entries = 0; |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7217 | mutex_unlock(&tracing_err_log_lock); |
| 7218 | } |
| 7219 | |
| 7220 | static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos) |
| 7221 | { |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7222 | struct trace_array *tr = m->private; |
| 7223 | |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7224 | mutex_lock(&tracing_err_log_lock); |
| 7225 | |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7226 | return seq_list_start(&tr->err_log, *pos); |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7227 | } |
| 7228 | |
| 7229 | static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos) |
| 7230 | { |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7231 | struct trace_array *tr = m->private; |
| 7232 | |
| 7233 | return seq_list_next(v, &tr->err_log, pos); |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7234 | } |
| 7235 | |
| 7236 | static void tracing_err_log_seq_stop(struct seq_file *m, void *v) |
| 7237 | { |
| 7238 | mutex_unlock(&tracing_err_log_lock); |
| 7239 | } |
| 7240 | |
| 7241 | static void tracing_err_log_show_pos(struct seq_file *m, u8 pos) |
| 7242 | { |
| 7243 | u8 i; |
| 7244 | |
| 7245 | for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++) |
| 7246 | seq_putc(m, ' '); |
| 7247 | for (i = 0; i < pos; i++) |
| 7248 | seq_putc(m, ' '); |
| 7249 | seq_puts(m, "^\n"); |
| 7250 | } |
| 7251 | |
| 7252 | static int tracing_err_log_seq_show(struct seq_file *m, void *v) |
| 7253 | { |
| 7254 | struct tracing_log_err *err = v; |
| 7255 | |
| 7256 | if (err) { |
| 7257 | const char *err_text = err->info.errs[err->info.type]; |
| 7258 | u64 sec = err->info.ts; |
| 7259 | u32 nsec; |
| 7260 | |
| 7261 | nsec = do_div(sec, NSEC_PER_SEC); |
| 7262 | seq_printf(m, "[%5llu.%06u] %s%s", sec, nsec / 1000, |
| 7263 | err->loc, err_text); |
| 7264 | seq_printf(m, "%s", err->cmd); |
| 7265 | tracing_err_log_show_pos(m, err->info.pos); |
| 7266 | } |
| 7267 | |
| 7268 | return 0; |
| 7269 | } |
| 7270 | |
| 7271 | static const struct seq_operations tracing_err_log_seq_ops = { |
| 7272 | .start = tracing_err_log_seq_start, |
| 7273 | .next = tracing_err_log_seq_next, |
| 7274 | .stop = tracing_err_log_seq_stop, |
| 7275 | .show = tracing_err_log_seq_show |
| 7276 | }; |
| 7277 | |
| 7278 | static int tracing_err_log_open(struct inode *inode, struct file *file) |
| 7279 | { |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7280 | struct trace_array *tr = inode->i_private; |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7281 | int ret = 0; |
| 7282 | |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 7283 | ret = tracing_check_open_get_tr(tr); |
| 7284 | if (ret) |
| 7285 | return ret; |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7286 | |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7287 | /* If this file was opened for write, then erase contents */ |
| 7288 | if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7289 | clear_tracing_err_log(tr); |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7290 | |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7291 | if (file->f_mode & FMODE_READ) { |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7292 | ret = seq_open(file, &tracing_err_log_seq_ops); |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 7293 | if (!ret) { |
| 7294 | struct seq_file *m = file->private_data; |
| 7295 | m->private = tr; |
| 7296 | } else { |
| 7297 | trace_array_put(tr); |
| 7298 | } |
| 7299 | } |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7300 | return ret; |
| 7301 | } |
| 7302 | |
| 7303 | static ssize_t tracing_err_log_write(struct file *file, |
| 7304 | const char __user *buffer, |
| 7305 | size_t count, loff_t *ppos) |
| 7306 | { |
| 7307 | return count; |
| 7308 | } |
| 7309 | |
Takeshi Misawa | d122ed6 | 2019-06-28 19:56:40 +0900 | [diff] [blame] | 7310 | static int tracing_err_log_release(struct inode *inode, struct file *file) |
| 7311 | { |
| 7312 | struct trace_array *tr = inode->i_private; |
| 7313 | |
| 7314 | trace_array_put(tr); |
| 7315 | |
| 7316 | if (file->f_mode & FMODE_READ) |
| 7317 | seq_release(inode, file); |
| 7318 | |
| 7319 | return 0; |
| 7320 | } |
| 7321 | |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7322 | static const struct file_operations tracing_err_log_fops = { |
| 7323 | .open = tracing_err_log_open, |
| 7324 | .write = tracing_err_log_write, |
| 7325 | .read = seq_read, |
| 7326 | .llseek = seq_lseek, |
Takeshi Misawa | d122ed6 | 2019-06-28 19:56:40 +0900 | [diff] [blame] | 7327 | .release = tracing_err_log_release, |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 7328 | }; |
| 7329 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7330 | static int tracing_buffers_open(struct inode *inode, struct file *filp) |
| 7331 | { |
Oleg Nesterov | 46ef2be | 2013-07-23 17:26:00 +0200 | [diff] [blame] | 7332 | struct trace_array *tr = inode->i_private; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7333 | struct ftrace_buffer_info *info; |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 7334 | int ret; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7335 | |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 7336 | ret = tracing_check_open_get_tr(tr); |
| 7337 | if (ret) |
| 7338 | return ret; |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 7339 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7340 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 7341 | if (!info) { |
| 7342 | trace_array_put(tr); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7343 | return -ENOMEM; |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 7344 | } |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7345 | |
Steven Rostedt (Red Hat) | a695cb5 | 2013-03-06 15:27:24 -0500 | [diff] [blame] | 7346 | mutex_lock(&trace_types_lock); |
| 7347 | |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7348 | info->iter.tr = tr; |
Oleg Nesterov | 46ef2be | 2013-07-23 17:26:00 +0200 | [diff] [blame] | 7349 | info->iter.cpu_file = tracing_get_cpu(inode); |
Steven Rostedt | b627344 | 2013-02-28 13:44:11 -0500 | [diff] [blame] | 7350 | info->iter.trace = tr->current_trace; |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 7351 | info->iter.array_buffer = &tr->array_buffer; |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7352 | info->spare = NULL; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7353 | /* Force reading ring buffer for first read */ |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7354 | info->read = (unsigned int)-1; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7355 | |
| 7356 | filp->private_data = info; |
| 7357 | |
Steven Rostedt (Red Hat) | cf6ab6d | 2014-12-15 20:13:31 -0500 | [diff] [blame] | 7358 | tr->current_trace->ref++; |
| 7359 | |
Steven Rostedt (Red Hat) | a695cb5 | 2013-03-06 15:27:24 -0500 | [diff] [blame] | 7360 | mutex_unlock(&trace_types_lock); |
| 7361 | |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 7362 | ret = nonseekable_open(inode, filp); |
| 7363 | if (ret < 0) |
| 7364 | trace_array_put(tr); |
| 7365 | |
| 7366 | return ret; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7367 | } |
| 7368 | |
Al Viro | 9dd9574 | 2017-07-03 00:42:43 -0400 | [diff] [blame] | 7369 | static __poll_t |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7370 | tracing_buffers_poll(struct file *filp, poll_table *poll_table) |
| 7371 | { |
| 7372 | struct ftrace_buffer_info *info = filp->private_data; |
| 7373 | struct trace_iterator *iter = &info->iter; |
| 7374 | |
| 7375 | return trace_poll(iter, filp, poll_table); |
| 7376 | } |
| 7377 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7378 | static ssize_t |
| 7379 | tracing_buffers_read(struct file *filp, char __user *ubuf, |
| 7380 | size_t count, loff_t *ppos) |
| 7381 | { |
| 7382 | struct ftrace_buffer_info *info = filp->private_data; |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7383 | struct trace_iterator *iter = &info->iter; |
Steven Rostedt (VMware) | a7e52ad | 2017-08-02 14:20:54 -0400 | [diff] [blame] | 7384 | ssize_t ret = 0; |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 7385 | ssize_t size; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7386 | |
Steven Rostedt | 2dc5d12 | 2009-03-04 19:10:05 -0500 | [diff] [blame] | 7387 | if (!count) |
| 7388 | return 0; |
| 7389 | |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 7390 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 7391 | if (iter->snapshot && iter->tr->current_trace->use_max_tr) |
| 7392 | return -EBUSY; |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 7393 | #endif |
| 7394 | |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 7395 | if (!info->spare) { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 7396 | info->spare = ring_buffer_alloc_read_page(iter->array_buffer->buffer, |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 7397 | iter->cpu_file); |
Steven Rostedt (VMware) | a7e52ad | 2017-08-02 14:20:54 -0400 | [diff] [blame] | 7398 | if (IS_ERR(info->spare)) { |
| 7399 | ret = PTR_ERR(info->spare); |
| 7400 | info->spare = NULL; |
| 7401 | } else { |
| 7402 | info->spare_cpu = iter->cpu_file; |
| 7403 | } |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 7404 | } |
Lai Jiangshan | ddd538f | 2009-04-02 15:16:59 +0800 | [diff] [blame] | 7405 | if (!info->spare) |
Steven Rostedt (VMware) | a7e52ad | 2017-08-02 14:20:54 -0400 | [diff] [blame] | 7406 | return ret; |
Lai Jiangshan | ddd538f | 2009-04-02 15:16:59 +0800 | [diff] [blame] | 7407 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7408 | /* Do we have previous read data to read? */ |
| 7409 | if (info->read < PAGE_SIZE) |
| 7410 | goto read; |
| 7411 | |
Steven Rostedt | b627344 | 2013-02-28 13:44:11 -0500 | [diff] [blame] | 7412 | again: |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7413 | trace_access_lock(iter->cpu_file); |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 7414 | ret = ring_buffer_read_page(iter->array_buffer->buffer, |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7415 | &info->spare, |
| 7416 | count, |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7417 | iter->cpu_file, 0); |
| 7418 | trace_access_unlock(iter->cpu_file); |
Steven Rostedt | b627344 | 2013-02-28 13:44:11 -0500 | [diff] [blame] | 7419 | |
| 7420 | if (ret < 0) { |
| 7421 | if (trace_empty(iter)) { |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 7422 | if ((filp->f_flags & O_NONBLOCK)) |
| 7423 | return -EAGAIN; |
| 7424 | |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 7425 | ret = wait_on_pipe(iter, 0); |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 7426 | if (ret) |
| 7427 | return ret; |
| 7428 | |
Steven Rostedt | b627344 | 2013-02-28 13:44:11 -0500 | [diff] [blame] | 7429 | goto again; |
| 7430 | } |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 7431 | return 0; |
Steven Rostedt | b627344 | 2013-02-28 13:44:11 -0500 | [diff] [blame] | 7432 | } |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7433 | |
Steven Rostedt | 436fc28 | 2011-10-14 10:44:25 -0400 | [diff] [blame] | 7434 | info->read = 0; |
Steven Rostedt | b627344 | 2013-02-28 13:44:11 -0500 | [diff] [blame] | 7435 | read: |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7436 | size = PAGE_SIZE - info->read; |
| 7437 | if (size > count) |
| 7438 | size = count; |
| 7439 | |
| 7440 | ret = copy_to_user(ubuf, info->spare + info->read, size); |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 7441 | if (ret == size) |
| 7442 | return -EFAULT; |
| 7443 | |
Steven Rostedt | 2dc5d12 | 2009-03-04 19:10:05 -0500 | [diff] [blame] | 7444 | size -= ret; |
| 7445 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7446 | *ppos += size; |
| 7447 | info->read += size; |
| 7448 | |
| 7449 | return size; |
| 7450 | } |
| 7451 | |
| 7452 | static int tracing_buffers_release(struct inode *inode, struct file *file) |
| 7453 | { |
| 7454 | struct ftrace_buffer_info *info = file->private_data; |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7455 | struct trace_iterator *iter = &info->iter; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7456 | |
Steven Rostedt (Red Hat) | a695cb5 | 2013-03-06 15:27:24 -0500 | [diff] [blame] | 7457 | mutex_lock(&trace_types_lock); |
| 7458 | |
Steven Rostedt (Red Hat) | cf6ab6d | 2014-12-15 20:13:31 -0500 | [diff] [blame] | 7459 | iter->tr->current_trace->ref--; |
| 7460 | |
Steven Rostedt (Red Hat) | ff45196 | 2013-07-01 22:50:29 -0400 | [diff] [blame] | 7461 | __trace_array_put(iter->tr); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7462 | |
Lai Jiangshan | ddd538f | 2009-04-02 15:16:59 +0800 | [diff] [blame] | 7463 | if (info->spare) |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 7464 | ring_buffer_free_read_page(iter->array_buffer->buffer, |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 7465 | info->spare_cpu, info->spare); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7466 | kfree(info); |
| 7467 | |
Steven Rostedt (Red Hat) | a695cb5 | 2013-03-06 15:27:24 -0500 | [diff] [blame] | 7468 | mutex_unlock(&trace_types_lock); |
| 7469 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7470 | return 0; |
| 7471 | } |
| 7472 | |
| 7473 | struct buffer_ref { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 7474 | struct trace_buffer *buffer; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7475 | void *page; |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 7476 | int cpu; |
Jann Horn | b987222 | 2019-04-04 23:59:25 +0200 | [diff] [blame] | 7477 | refcount_t refcount; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7478 | }; |
| 7479 | |
Jann Horn | b987222 | 2019-04-04 23:59:25 +0200 | [diff] [blame] | 7480 | static void buffer_ref_release(struct buffer_ref *ref) |
| 7481 | { |
| 7482 | if (!refcount_dec_and_test(&ref->refcount)) |
| 7483 | return; |
| 7484 | ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page); |
| 7485 | kfree(ref); |
| 7486 | } |
| 7487 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7488 | static void buffer_pipe_buf_release(struct pipe_inode_info *pipe, |
| 7489 | struct pipe_buffer *buf) |
| 7490 | { |
| 7491 | struct buffer_ref *ref = (struct buffer_ref *)buf->private; |
| 7492 | |
Jann Horn | b987222 | 2019-04-04 23:59:25 +0200 | [diff] [blame] | 7493 | buffer_ref_release(ref); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7494 | buf->private = 0; |
| 7495 | } |
| 7496 | |
Matthew Wilcox | 15fab63 | 2019-04-05 14:02:10 -0700 | [diff] [blame] | 7497 | static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe, |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7498 | struct pipe_buffer *buf) |
| 7499 | { |
| 7500 | struct buffer_ref *ref = (struct buffer_ref *)buf->private; |
| 7501 | |
Linus Torvalds | e9e1a2e | 2019-04-26 11:09:55 -0700 | [diff] [blame] | 7502 | if (refcount_read(&ref->refcount) > INT_MAX/2) |
Matthew Wilcox | 15fab63 | 2019-04-05 14:02:10 -0700 | [diff] [blame] | 7503 | return false; |
| 7504 | |
Jann Horn | b987222 | 2019-04-04 23:59:25 +0200 | [diff] [blame] | 7505 | refcount_inc(&ref->refcount); |
Matthew Wilcox | 15fab63 | 2019-04-05 14:02:10 -0700 | [diff] [blame] | 7506 | return true; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7507 | } |
| 7508 | |
| 7509 | /* Pipe buffer operations for a buffer. */ |
Alexey Dobriyan | 28dfef8 | 2009-12-15 16:46:48 -0800 | [diff] [blame] | 7510 | static const struct pipe_buf_operations buffer_pipe_buf_ops = { |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7511 | .confirm = generic_pipe_buf_confirm, |
| 7512 | .release = buffer_pipe_buf_release, |
Jann Horn | b987222 | 2019-04-04 23:59:25 +0200 | [diff] [blame] | 7513 | .steal = generic_pipe_buf_nosteal, |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7514 | .get = buffer_pipe_buf_get, |
| 7515 | }; |
| 7516 | |
| 7517 | /* |
| 7518 | * Callback from splice_to_pipe(), if we need to release some pages |
| 7519 | * at the end of the spd in case we error'ed out in filling the pipe. |
| 7520 | */ |
| 7521 | static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i) |
| 7522 | { |
| 7523 | struct buffer_ref *ref = |
| 7524 | (struct buffer_ref *)spd->partial[i].private; |
| 7525 | |
Jann Horn | b987222 | 2019-04-04 23:59:25 +0200 | [diff] [blame] | 7526 | buffer_ref_release(ref); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7527 | spd->partial[i].private = 0; |
| 7528 | } |
| 7529 | |
| 7530 | static ssize_t |
| 7531 | tracing_buffers_splice_read(struct file *file, loff_t *ppos, |
| 7532 | struct pipe_inode_info *pipe, size_t len, |
| 7533 | unsigned int flags) |
| 7534 | { |
| 7535 | struct ftrace_buffer_info *info = file->private_data; |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7536 | struct trace_iterator *iter = &info->iter; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 7537 | struct partial_page partial_def[PIPE_DEF_BUFFERS]; |
| 7538 | struct page *pages_def[PIPE_DEF_BUFFERS]; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7539 | struct splice_pipe_desc spd = { |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 7540 | .pages = pages_def, |
| 7541 | .partial = partial_def, |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 7542 | .nr_pages_max = PIPE_DEF_BUFFERS, |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7543 | .ops = &buffer_pipe_buf_ops, |
| 7544 | .spd_release = buffer_spd_release, |
| 7545 | }; |
| 7546 | struct buffer_ref *ref; |
Steven Rostedt (VMware) | 6b7e633 | 2017-12-22 20:38:57 -0500 | [diff] [blame] | 7547 | int entries, i; |
Rabin Vincent | 07906da | 2014-11-06 22:26:07 +0100 | [diff] [blame] | 7548 | ssize_t ret = 0; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7549 | |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 7550 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 7551 | if (iter->snapshot && iter->tr->current_trace->use_max_tr) |
| 7552 | return -EBUSY; |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 7553 | #endif |
| 7554 | |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 7555 | if (*ppos & (PAGE_SIZE - 1)) |
| 7556 | return -EINVAL; |
Lai Jiangshan | 93cfb3c | 2009-04-02 15:17:08 +0800 | [diff] [blame] | 7557 | |
| 7558 | if (len & (PAGE_SIZE - 1)) { |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 7559 | if (len < PAGE_SIZE) |
| 7560 | return -EINVAL; |
Lai Jiangshan | 93cfb3c | 2009-04-02 15:17:08 +0800 | [diff] [blame] | 7561 | len &= PAGE_MASK; |
| 7562 | } |
| 7563 | |
Al Viro | 1ae2293 | 2016-09-17 18:31:46 -0400 | [diff] [blame] | 7564 | if (splice_grow_spd(pipe, &spd)) |
| 7565 | return -ENOMEM; |
| 7566 | |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7567 | again: |
| 7568 | trace_access_lock(iter->cpu_file); |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 7569 | entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file); |
Steven Rostedt | 93459c6 | 2009-04-29 00:23:13 -0400 | [diff] [blame] | 7570 | |
Al Viro | a786c06 | 2014-04-11 12:01:03 -0400 | [diff] [blame] | 7571 | for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) { |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7572 | struct page *page; |
| 7573 | int r; |
| 7574 | |
| 7575 | ref = kzalloc(sizeof(*ref), GFP_KERNEL); |
Rabin Vincent | 07906da | 2014-11-06 22:26:07 +0100 | [diff] [blame] | 7576 | if (!ref) { |
| 7577 | ret = -ENOMEM; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7578 | break; |
Rabin Vincent | 07906da | 2014-11-06 22:26:07 +0100 | [diff] [blame] | 7579 | } |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7580 | |
Jann Horn | b987222 | 2019-04-04 23:59:25 +0200 | [diff] [blame] | 7581 | refcount_set(&ref->refcount, 1); |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 7582 | ref->buffer = iter->array_buffer->buffer; |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7583 | ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file); |
Steven Rostedt (VMware) | a7e52ad | 2017-08-02 14:20:54 -0400 | [diff] [blame] | 7584 | if (IS_ERR(ref->page)) { |
| 7585 | ret = PTR_ERR(ref->page); |
| 7586 | ref->page = NULL; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7587 | kfree(ref); |
| 7588 | break; |
| 7589 | } |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 7590 | ref->cpu = iter->cpu_file; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7591 | |
| 7592 | r = ring_buffer_read_page(ref->buffer, &ref->page, |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7593 | len, iter->cpu_file, 1); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7594 | if (r < 0) { |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 7595 | ring_buffer_free_read_page(ref->buffer, ref->cpu, |
| 7596 | ref->page); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7597 | kfree(ref); |
| 7598 | break; |
| 7599 | } |
| 7600 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7601 | page = virt_to_page(ref->page); |
| 7602 | |
| 7603 | spd.pages[i] = page; |
| 7604 | spd.partial[i].len = PAGE_SIZE; |
| 7605 | spd.partial[i].offset = 0; |
| 7606 | spd.partial[i].private = (unsigned long)ref; |
| 7607 | spd.nr_pages++; |
Lai Jiangshan | 93cfb3c | 2009-04-02 15:17:08 +0800 | [diff] [blame] | 7608 | *ppos += PAGE_SIZE; |
Steven Rostedt | 93459c6 | 2009-04-29 00:23:13 -0400 | [diff] [blame] | 7609 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 7610 | entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7611 | } |
| 7612 | |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7613 | trace_access_unlock(iter->cpu_file); |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7614 | spd.nr_pages = i; |
| 7615 | |
| 7616 | /* did we read anything? */ |
| 7617 | if (!spd.nr_pages) { |
Rabin Vincent | 07906da | 2014-11-06 22:26:07 +0100 | [diff] [blame] | 7618 | if (ret) |
Al Viro | 1ae2293 | 2016-09-17 18:31:46 -0400 | [diff] [blame] | 7619 | goto out; |
Rabin Vincent | 07906da | 2014-11-06 22:26:07 +0100 | [diff] [blame] | 7620 | |
Al Viro | 1ae2293 | 2016-09-17 18:31:46 -0400 | [diff] [blame] | 7621 | ret = -EAGAIN; |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 7622 | if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) |
Al Viro | 1ae2293 | 2016-09-17 18:31:46 -0400 | [diff] [blame] | 7623 | goto out; |
Steven Rostedt (Red Hat) | d716ff7 | 2014-12-15 22:31:07 -0500 | [diff] [blame] | 7624 | |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 7625 | ret = wait_on_pipe(iter, iter->tr->buffer_percent); |
Steven Rostedt (Red Hat) | 8b8b368 | 2014-06-10 09:46:00 -0400 | [diff] [blame] | 7626 | if (ret) |
Al Viro | 1ae2293 | 2016-09-17 18:31:46 -0400 | [diff] [blame] | 7627 | goto out; |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 7628 | |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7629 | goto again; |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7630 | } |
| 7631 | |
| 7632 | ret = splice_to_pipe(pipe, &spd); |
Al Viro | 1ae2293 | 2016-09-17 18:31:46 -0400 | [diff] [blame] | 7633 | out: |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 7634 | splice_shrink_spd(&spd); |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 7635 | |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7636 | return ret; |
| 7637 | } |
| 7638 | |
| 7639 | static const struct file_operations tracing_buffers_fops = { |
| 7640 | .open = tracing_buffers_open, |
| 7641 | .read = tracing_buffers_read, |
Steven Rostedt | cc60cdc | 2013-02-28 09:17:16 -0500 | [diff] [blame] | 7642 | .poll = tracing_buffers_poll, |
Steven Rostedt | 2cadf91 | 2008-12-01 22:20:19 -0500 | [diff] [blame] | 7643 | .release = tracing_buffers_release, |
| 7644 | .splice_read = tracing_buffers_splice_read, |
| 7645 | .llseek = no_llseek, |
| 7646 | }; |
| 7647 | |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 7648 | static ssize_t |
| 7649 | tracing_stats_read(struct file *filp, char __user *ubuf, |
| 7650 | size_t count, loff_t *ppos) |
| 7651 | { |
Oleg Nesterov | 4d3435b | 2013-07-23 17:26:03 +0200 | [diff] [blame] | 7652 | struct inode *inode = file_inode(filp); |
| 7653 | struct trace_array *tr = inode->i_private; |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 7654 | struct array_buffer *trace_buf = &tr->array_buffer; |
Oleg Nesterov | 4d3435b | 2013-07-23 17:26:03 +0200 | [diff] [blame] | 7655 | int cpu = tracing_get_cpu(inode); |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 7656 | struct trace_seq *s; |
| 7657 | unsigned long cnt; |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 7658 | unsigned long long t; |
| 7659 | unsigned long usec_rem; |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 7660 | |
Li Zefan | e4f2d10 | 2009-06-15 10:57:28 +0800 | [diff] [blame] | 7661 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 7662 | if (!s) |
Roel Kluin | a646365 | 2009-11-11 22:26:35 +0100 | [diff] [blame] | 7663 | return -ENOMEM; |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 7664 | |
| 7665 | trace_seq_init(s); |
| 7666 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 7667 | cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu); |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 7668 | trace_seq_printf(s, "entries: %ld\n", cnt); |
| 7669 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 7670 | cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu); |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 7671 | trace_seq_printf(s, "overrun: %ld\n", cnt); |
| 7672 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 7673 | cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu); |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 7674 | trace_seq_printf(s, "commit overrun: %ld\n", cnt); |
| 7675 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 7676 | cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 7677 | trace_seq_printf(s, "bytes: %ld\n", cnt); |
| 7678 | |
Yoshihiro YUNOMAE | 58e8eed | 2013-04-23 10:32:39 +0900 | [diff] [blame] | 7679 | if (trace_clocks[tr->clock_id].in_ns) { |
Yoshihiro YUNOMAE | 11043d8b | 2012-11-13 12:18:23 -0800 | [diff] [blame] | 7680 | /* local or global for trace_clock */ |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 7681 | t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu)); |
Yoshihiro YUNOMAE | 11043d8b | 2012-11-13 12:18:23 -0800 | [diff] [blame] | 7682 | usec_rem = do_div(t, USEC_PER_SEC); |
| 7683 | trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", |
| 7684 | t, usec_rem); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 7685 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 7686 | t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu)); |
Yoshihiro YUNOMAE | 11043d8b | 2012-11-13 12:18:23 -0800 | [diff] [blame] | 7687 | usec_rem = do_div(t, USEC_PER_SEC); |
| 7688 | trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem); |
| 7689 | } else { |
| 7690 | /* counter or tsc mode for trace_clock */ |
| 7691 | trace_seq_printf(s, "oldest event ts: %llu\n", |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 7692 | ring_buffer_oldest_event_ts(trace_buf->buffer, cpu)); |
Yoshihiro YUNOMAE | 11043d8b | 2012-11-13 12:18:23 -0800 | [diff] [blame] | 7693 | |
| 7694 | trace_seq_printf(s, "now ts: %llu\n", |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 7695 | ring_buffer_time_stamp(trace_buf->buffer, cpu)); |
Yoshihiro YUNOMAE | 11043d8b | 2012-11-13 12:18:23 -0800 | [diff] [blame] | 7696 | } |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 7697 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 7698 | cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu); |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 7699 | trace_seq_printf(s, "dropped events: %ld\n", cnt); |
| 7700 | |
Steven Rostedt (Red Hat) | 12883ef | 2013-03-05 09:24:35 -0500 | [diff] [blame] | 7701 | cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu); |
Steven Rostedt (Red Hat) | ad96470 | 2013-01-29 17:45:49 -0500 | [diff] [blame] | 7702 | trace_seq_printf(s, "read events: %ld\n", cnt); |
| 7703 | |
Steven Rostedt (Red Hat) | 5ac48378 | 2014-11-14 15:49:41 -0500 | [diff] [blame] | 7704 | count = simple_read_from_buffer(ubuf, count, ppos, |
| 7705 | s->buffer, trace_seq_used(s)); |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 7706 | |
| 7707 | kfree(s); |
| 7708 | |
| 7709 | return count; |
| 7710 | } |
| 7711 | |
| 7712 | static const struct file_operations tracing_stats_fops = { |
Oleg Nesterov | 4d3435b | 2013-07-23 17:26:03 +0200 | [diff] [blame] | 7713 | .open = tracing_open_generic_tr, |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 7714 | .read = tracing_stats_read, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 7715 | .llseek = generic_file_llseek, |
Oleg Nesterov | 4d3435b | 2013-07-23 17:26:03 +0200 | [diff] [blame] | 7716 | .release = tracing_release_generic_tr, |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 7717 | }; |
| 7718 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7719 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 7720 | |
| 7721 | static ssize_t |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 7722 | tracing_read_dyn_info(struct file *filp, char __user *ubuf, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7723 | size_t cnt, loff_t *ppos) |
| 7724 | { |
Steven Rostedt (VMware) | da537f0 | 2019-10-01 14:38:07 -0400 | [diff] [blame] | 7725 | ssize_t ret; |
| 7726 | char *buf; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7727 | int r; |
| 7728 | |
Steven Rostedt (VMware) | da537f0 | 2019-10-01 14:38:07 -0400 | [diff] [blame] | 7729 | /* 256 should be plenty to hold the amount needed */ |
| 7730 | buf = kmalloc(256, GFP_KERNEL); |
| 7731 | if (!buf) |
| 7732 | return -ENOMEM; |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 7733 | |
Steven Rostedt (VMware) | da537f0 | 2019-10-01 14:38:07 -0400 | [diff] [blame] | 7734 | r = scnprintf(buf, 256, "%ld pages:%ld groups: %ld\n", |
| 7735 | ftrace_update_tot_cnt, |
| 7736 | ftrace_number_of_pages, |
| 7737 | ftrace_number_of_groups); |
| 7738 | |
| 7739 | ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 7740 | kfree(buf); |
| 7741 | return ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7742 | } |
| 7743 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 7744 | static const struct file_operations tracing_dyn_info_fops = { |
Ingo Molnar | 4bf39a9 | 2008-05-12 21:20:46 +0200 | [diff] [blame] | 7745 | .open = tracing_open_generic, |
Steven Rostedt | b807c3d | 2008-10-30 16:08:33 -0400 | [diff] [blame] | 7746 | .read = tracing_read_dyn_info, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 7747 | .llseek = generic_file_llseek, |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7748 | }; |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7749 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7750 | |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7751 | #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) |
| 7752 | static void |
Steven Rostedt (VMware) | bca6c8d | 2017-04-03 18:18:47 -0400 | [diff] [blame] | 7753 | ftrace_snapshot(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 7754 | struct trace_array *tr, struct ftrace_probe_ops *ops, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 7755 | void *data) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7756 | { |
Steven Rostedt (VMware) | cab5037 | 2017-04-20 11:34:06 -0400 | [diff] [blame] | 7757 | tracing_snapshot_instance(tr); |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7758 | } |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7759 | |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7760 | static void |
Steven Rostedt (VMware) | bca6c8d | 2017-04-03 18:18:47 -0400 | [diff] [blame] | 7761 | ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 7762 | struct trace_array *tr, struct ftrace_probe_ops *ops, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 7763 | void *data) |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7764 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 7765 | struct ftrace_func_mapper *mapper = data; |
Steven Rostedt (VMware) | 1a93f8b | 2017-04-03 22:09:43 -0400 | [diff] [blame] | 7766 | long *count = NULL; |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7767 | |
Steven Rostedt (VMware) | 1a93f8b | 2017-04-03 22:09:43 -0400 | [diff] [blame] | 7768 | if (mapper) |
| 7769 | count = (long *)ftrace_func_mapper_find_ip(mapper, ip); |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7770 | |
Steven Rostedt (VMware) | 1a93f8b | 2017-04-03 22:09:43 -0400 | [diff] [blame] | 7771 | if (count) { |
| 7772 | |
| 7773 | if (*count <= 0) |
| 7774 | return; |
| 7775 | |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7776 | (*count)--; |
Steven Rostedt (VMware) | 1a93f8b | 2017-04-03 22:09:43 -0400 | [diff] [blame] | 7777 | } |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7778 | |
Steven Rostedt (VMware) | cab5037 | 2017-04-20 11:34:06 -0400 | [diff] [blame] | 7779 | tracing_snapshot_instance(tr); |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7780 | } |
| 7781 | |
| 7782 | static int |
| 7783 | ftrace_snapshot_print(struct seq_file *m, unsigned long ip, |
| 7784 | struct ftrace_probe_ops *ops, void *data) |
| 7785 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 7786 | struct ftrace_func_mapper *mapper = data; |
Steven Rostedt (VMware) | 1a93f8b | 2017-04-03 22:09:43 -0400 | [diff] [blame] | 7787 | long *count = NULL; |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7788 | |
| 7789 | seq_printf(m, "%ps:", (void *)ip); |
| 7790 | |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 7791 | seq_puts(m, "snapshot"); |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7792 | |
Steven Rostedt (VMware) | 1a93f8b | 2017-04-03 22:09:43 -0400 | [diff] [blame] | 7793 | if (mapper) |
| 7794 | count = (long *)ftrace_func_mapper_find_ip(mapper, ip); |
| 7795 | |
| 7796 | if (count) |
| 7797 | seq_printf(m, ":count=%ld\n", *count); |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7798 | else |
Steven Rostedt (VMware) | 1a93f8b | 2017-04-03 22:09:43 -0400 | [diff] [blame] | 7799 | seq_puts(m, ":unlimited\n"); |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7800 | |
| 7801 | return 0; |
| 7802 | } |
| 7803 | |
Steven Rostedt (VMware) | 1a93f8b | 2017-04-03 22:09:43 -0400 | [diff] [blame] | 7804 | static int |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 7805 | ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 7806 | unsigned long ip, void *init_data, void **data) |
Steven Rostedt (VMware) | 1a93f8b | 2017-04-03 22:09:43 -0400 | [diff] [blame] | 7807 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 7808 | struct ftrace_func_mapper *mapper = *data; |
Steven Rostedt (VMware) | 1a93f8b | 2017-04-03 22:09:43 -0400 | [diff] [blame] | 7809 | |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 7810 | if (!mapper) { |
| 7811 | mapper = allocate_ftrace_func_mapper(); |
| 7812 | if (!mapper) |
| 7813 | return -ENOMEM; |
| 7814 | *data = mapper; |
| 7815 | } |
| 7816 | |
| 7817 | return ftrace_func_mapper_add_ip(mapper, ip, init_data); |
Steven Rostedt (VMware) | 1a93f8b | 2017-04-03 22:09:43 -0400 | [diff] [blame] | 7818 | } |
| 7819 | |
| 7820 | static void |
Steven Rostedt (VMware) | b5f081b | 2017-04-10 22:30:05 -0400 | [diff] [blame] | 7821 | ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr, |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 7822 | unsigned long ip, void *data) |
Steven Rostedt (VMware) | 1a93f8b | 2017-04-03 22:09:43 -0400 | [diff] [blame] | 7823 | { |
Steven Rostedt (VMware) | 6e44431 | 2017-04-19 22:39:44 -0400 | [diff] [blame] | 7824 | struct ftrace_func_mapper *mapper = data; |
| 7825 | |
| 7826 | if (!ip) { |
| 7827 | if (!mapper) |
| 7828 | return; |
| 7829 | free_ftrace_func_mapper(mapper, NULL); |
| 7830 | return; |
| 7831 | } |
Steven Rostedt (VMware) | 1a93f8b | 2017-04-03 22:09:43 -0400 | [diff] [blame] | 7832 | |
| 7833 | ftrace_func_mapper_remove_ip(mapper, ip); |
| 7834 | } |
| 7835 | |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7836 | static struct ftrace_probe_ops snapshot_probe_ops = { |
| 7837 | .func = ftrace_snapshot, |
| 7838 | .print = ftrace_snapshot_print, |
| 7839 | }; |
| 7840 | |
| 7841 | static struct ftrace_probe_ops snapshot_count_probe_ops = { |
| 7842 | .func = ftrace_count_snapshot, |
| 7843 | .print = ftrace_snapshot_print, |
Steven Rostedt (VMware) | 1a93f8b | 2017-04-03 22:09:43 -0400 | [diff] [blame] | 7844 | .init = ftrace_snapshot_init, |
| 7845 | .free = ftrace_snapshot_free, |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7846 | }; |
| 7847 | |
| 7848 | static int |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 7849 | ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash, |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7850 | char *glob, char *cmd, char *param, int enable) |
| 7851 | { |
| 7852 | struct ftrace_probe_ops *ops; |
| 7853 | void *count = (void *)-1; |
| 7854 | char *number; |
| 7855 | int ret; |
| 7856 | |
Steven Rostedt (VMware) | 0f17976 | 2017-06-29 10:05:45 -0400 | [diff] [blame] | 7857 | if (!tr) |
| 7858 | return -ENODEV; |
| 7859 | |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7860 | /* hash funcs only work with set_ftrace_filter */ |
| 7861 | if (!enable) |
| 7862 | return -EINVAL; |
| 7863 | |
| 7864 | ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops; |
| 7865 | |
Steven Rostedt (VMware) | d3d532d | 2017-04-04 16:44:43 -0400 | [diff] [blame] | 7866 | if (glob[0] == '!') |
Steven Rostedt (VMware) | 7b60f3d | 2017-04-18 14:50:39 -0400 | [diff] [blame] | 7867 | return unregister_ftrace_function_probe_func(glob+1, tr, ops); |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7868 | |
| 7869 | if (!param) |
| 7870 | goto out_reg; |
| 7871 | |
| 7872 | number = strsep(¶m, ":"); |
| 7873 | |
| 7874 | if (!strlen(number)) |
| 7875 | goto out_reg; |
| 7876 | |
| 7877 | /* |
| 7878 | * We use the callback data field (which is a pointer) |
| 7879 | * as our counter. |
| 7880 | */ |
| 7881 | ret = kstrtoul(number, 0, (unsigned long *)&count); |
| 7882 | if (ret) |
| 7883 | return ret; |
| 7884 | |
| 7885 | out_reg: |
Steven Rostedt (VMware) | 2824f50 | 2018-05-28 10:56:36 -0400 | [diff] [blame] | 7886 | ret = tracing_alloc_snapshot_instance(tr); |
Steven Rostedt (VMware) | df62db5 | 2017-04-19 12:07:08 -0400 | [diff] [blame] | 7887 | if (ret < 0) |
| 7888 | goto out; |
| 7889 | |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 7890 | ret = register_ftrace_function_probe(glob, tr, ops, count); |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7891 | |
Steven Rostedt (VMware) | df62db5 | 2017-04-19 12:07:08 -0400 | [diff] [blame] | 7892 | out: |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7893 | return ret < 0 ? ret : 0; |
| 7894 | } |
| 7895 | |
| 7896 | static struct ftrace_func_command ftrace_snapshot_cmd = { |
| 7897 | .name = "snapshot", |
| 7898 | .func = ftrace_trace_snapshot_callback, |
| 7899 | }; |
| 7900 | |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 7901 | static __init int register_snapshot_cmd(void) |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7902 | { |
| 7903 | return register_ftrace_command(&ftrace_snapshot_cmd); |
| 7904 | } |
| 7905 | #else |
Tom Zanussi | 38de93a | 2013-10-24 08:34:18 -0500 | [diff] [blame] | 7906 | static inline __init int register_snapshot_cmd(void) { return 0; } |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 7907 | #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */ |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7908 | |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 7909 | static struct dentry *tracing_get_dentry(struct trace_array *tr) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7910 | { |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7911 | if (WARN_ON(!tr->dir)) |
| 7912 | return ERR_PTR(-ENODEV); |
| 7913 | |
| 7914 | /* Top directory uses NULL as the parent */ |
| 7915 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL) |
| 7916 | return NULL; |
| 7917 | |
| 7918 | /* All sub buffers have a descriptor */ |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7919 | return tr->dir; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 7920 | } |
| 7921 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7922 | static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu) |
| 7923 | { |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 7924 | struct dentry *d_tracer; |
| 7925 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7926 | if (tr->percpu_dir) |
| 7927 | return tr->percpu_dir; |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 7928 | |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 7929 | d_tracer = tracing_get_dentry(tr); |
Steven Rostedt (Red Hat) | 14a5ae4 | 2015-01-20 11:14:16 -0500 | [diff] [blame] | 7930 | if (IS_ERR(d_tracer)) |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 7931 | return NULL; |
| 7932 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7933 | tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 7934 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7935 | WARN_ONCE(!tr->percpu_dir, |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7936 | "Could not create tracefs directory 'per_cpu/%d'\n", cpu); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 7937 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7938 | return tr->percpu_dir; |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 7939 | } |
| 7940 | |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 7941 | static struct dentry * |
| 7942 | trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent, |
| 7943 | void *data, long cpu, const struct file_operations *fops) |
| 7944 | { |
| 7945 | struct dentry *ret = trace_create_file(name, mode, parent, data, fops); |
| 7946 | |
| 7947 | if (ret) /* See tracing_get_cpu() */ |
David Howells | 7682c91 | 2015-03-17 22:26:16 +0000 | [diff] [blame] | 7948 | d_inode(ret)->i_cdev = (void *)(cpu + 1); |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 7949 | return ret; |
| 7950 | } |
| 7951 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7952 | static void |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7953 | tracing_init_tracefs_percpu(struct trace_array *tr, long cpu) |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 7954 | { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 7955 | struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu); |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 7956 | struct dentry *d_cpu; |
Steven Rostedt | dd49a38 | 2010-10-20 21:51:26 -0400 | [diff] [blame] | 7957 | char cpu_dir[30]; /* 30 characters should be more than enough */ |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 7958 | |
Namhyung Kim | 0a3d7ce | 2012-04-23 10:11:57 +0900 | [diff] [blame] | 7959 | if (!d_percpu) |
| 7960 | return; |
| 7961 | |
Steven Rostedt | dd49a38 | 2010-10-20 21:51:26 -0400 | [diff] [blame] | 7962 | snprintf(cpu_dir, 30, "cpu%ld", cpu); |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 7963 | d_cpu = tracefs_create_dir(cpu_dir, d_percpu); |
Frederic Weisbecker | 8656e7a | 2009-02-26 00:41:38 +0100 | [diff] [blame] | 7964 | if (!d_cpu) { |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 7965 | pr_warn("Could not create tracefs '%s' entry\n", cpu_dir); |
Frederic Weisbecker | 8656e7a | 2009-02-26 00:41:38 +0100 | [diff] [blame] | 7966 | return; |
| 7967 | } |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 7968 | |
Frederic Weisbecker | 8656e7a | 2009-02-26 00:41:38 +0100 | [diff] [blame] | 7969 | /* per cpu trace_pipe */ |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 7970 | trace_create_cpu_file("trace_pipe", 0444, d_cpu, |
Oleg Nesterov | 1554420 | 2013-07-23 17:25:57 +0200 | [diff] [blame] | 7971 | tr, cpu, &tracing_pipe_fops); |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 7972 | |
| 7973 | /* per cpu trace */ |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 7974 | trace_create_cpu_file("trace", 0644, d_cpu, |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 7975 | tr, cpu, &tracing_fops); |
Steven Rostedt | 7f96f93 | 2009-03-13 00:37:42 -0400 | [diff] [blame] | 7976 | |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 7977 | trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu, |
Oleg Nesterov | 46ef2be | 2013-07-23 17:26:00 +0200 | [diff] [blame] | 7978 | tr, cpu, &tracing_buffers_fops); |
Steven Rostedt | c8d7718 | 2009-04-29 18:03:45 -0400 | [diff] [blame] | 7979 | |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 7980 | trace_create_cpu_file("stats", 0444, d_cpu, |
Oleg Nesterov | 4d3435b | 2013-07-23 17:26:03 +0200 | [diff] [blame] | 7981 | tr, cpu, &tracing_stats_fops); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 7982 | |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 7983 | trace_create_cpu_file("buffer_size_kb", 0444, d_cpu, |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 7984 | tr, cpu, &tracing_entries_fops); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 7985 | |
| 7986 | #ifdef CONFIG_TRACER_SNAPSHOT |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 7987 | trace_create_cpu_file("snapshot", 0644, d_cpu, |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 7988 | tr, cpu, &snapshot_fops); |
Steven Rostedt (Red Hat) | 6de58e6 | 2013-03-05 16:18:16 -0500 | [diff] [blame] | 7989 | |
Oleg Nesterov | 649e9c70 | 2013-07-23 17:25:54 +0200 | [diff] [blame] | 7990 | trace_create_cpu_file("snapshot_raw", 0444, d_cpu, |
Oleg Nesterov | 46ef2be | 2013-07-23 17:26:00 +0200 | [diff] [blame] | 7991 | tr, cpu, &snapshot_raw_fops); |
Steven Rostedt (Red Hat) | f1affca | 2013-03-05 14:35:11 -0500 | [diff] [blame] | 7992 | #endif |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 7993 | } |
| 7994 | |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 7995 | #ifdef CONFIG_FTRACE_SELFTEST |
| 7996 | /* Let selftest have access to static functions in this file */ |
| 7997 | #include "trace_selftest.c" |
| 7998 | #endif |
| 7999 | |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8000 | static ssize_t |
| 8001 | trace_options_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 8002 | loff_t *ppos) |
| 8003 | { |
| 8004 | struct trace_option_dentry *topt = filp->private_data; |
| 8005 | char *buf; |
| 8006 | |
| 8007 | if (topt->flags->val & topt->opt->bit) |
| 8008 | buf = "1\n"; |
| 8009 | else |
| 8010 | buf = "0\n"; |
| 8011 | |
| 8012 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); |
| 8013 | } |
| 8014 | |
| 8015 | static ssize_t |
| 8016 | trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 8017 | loff_t *ppos) |
| 8018 | { |
| 8019 | struct trace_option_dentry *topt = filp->private_data; |
| 8020 | unsigned long val; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8021 | int ret; |
| 8022 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 8023 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 8024 | if (ret) |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8025 | return ret; |
| 8026 | |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 8027 | if (val != 0 && val != 1) |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8028 | return -EINVAL; |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 8029 | |
| 8030 | if (!!(topt->flags->val & topt->opt->bit) != val) { |
| 8031 | mutex_lock(&trace_types_lock); |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 8032 | ret = __set_tracer_option(topt->tr, topt->flags, |
Steven Rostedt | c757bea | 2009-12-21 22:35:16 -0500 | [diff] [blame] | 8033 | topt->opt, !val); |
Li Zefan | 8d18eaa | 2009-12-08 11:17:06 +0800 | [diff] [blame] | 8034 | mutex_unlock(&trace_types_lock); |
| 8035 | if (ret) |
| 8036 | return ret; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8037 | } |
| 8038 | |
| 8039 | *ppos += cnt; |
| 8040 | |
| 8041 | return cnt; |
| 8042 | } |
| 8043 | |
| 8044 | |
| 8045 | static const struct file_operations trace_options_fops = { |
| 8046 | .open = tracing_open_generic, |
| 8047 | .read = trace_options_read, |
| 8048 | .write = trace_options_write, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 8049 | .llseek = generic_file_llseek, |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8050 | }; |
| 8051 | |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 8052 | /* |
| 8053 | * In order to pass in both the trace_array descriptor as well as the index |
| 8054 | * to the flag that the trace option file represents, the trace_array |
| 8055 | * has a character array of trace_flags_index[], which holds the index |
| 8056 | * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc. |
| 8057 | * The address of this character array is passed to the flag option file |
| 8058 | * read/write callbacks. |
| 8059 | * |
| 8060 | * In order to extract both the index and the trace_array descriptor, |
| 8061 | * get_tr_index() uses the following algorithm. |
| 8062 | * |
| 8063 | * idx = *ptr; |
| 8064 | * |
| 8065 | * As the pointer itself contains the address of the index (remember |
| 8066 | * index[1] == 1). |
| 8067 | * |
| 8068 | * Then to get the trace_array descriptor, by subtracting that index |
| 8069 | * from the ptr, we get to the start of the index itself. |
| 8070 | * |
| 8071 | * ptr - idx == &index[0] |
| 8072 | * |
| 8073 | * Then a simple container_of() from that pointer gets us to the |
| 8074 | * trace_array descriptor. |
| 8075 | */ |
| 8076 | static void get_tr_index(void *data, struct trace_array **ptr, |
| 8077 | unsigned int *pindex) |
| 8078 | { |
| 8079 | *pindex = *(unsigned char *)data; |
| 8080 | |
| 8081 | *ptr = container_of(data - *pindex, struct trace_array, |
| 8082 | trace_flags_index); |
| 8083 | } |
| 8084 | |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8085 | static ssize_t |
| 8086 | trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 8087 | loff_t *ppos) |
| 8088 | { |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 8089 | void *tr_index = filp->private_data; |
| 8090 | struct trace_array *tr; |
| 8091 | unsigned int index; |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8092 | char *buf; |
| 8093 | |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 8094 | get_tr_index(tr_index, &tr, &index); |
| 8095 | |
| 8096 | if (tr->trace_flags & (1 << index)) |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8097 | buf = "1\n"; |
| 8098 | else |
| 8099 | buf = "0\n"; |
| 8100 | |
| 8101 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); |
| 8102 | } |
| 8103 | |
| 8104 | static ssize_t |
| 8105 | trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 8106 | loff_t *ppos) |
| 8107 | { |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 8108 | void *tr_index = filp->private_data; |
| 8109 | struct trace_array *tr; |
| 8110 | unsigned int index; |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8111 | unsigned long val; |
| 8112 | int ret; |
| 8113 | |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 8114 | get_tr_index(tr_index, &tr, &index); |
| 8115 | |
Peter Huewe | 22fe9b5 | 2011-06-07 21:58:27 +0200 | [diff] [blame] | 8116 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 8117 | if (ret) |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8118 | return ret; |
| 8119 | |
Zhaolei | f2d84b6 | 2009-08-07 18:55:48 +0800 | [diff] [blame] | 8120 | if (val != 0 && val != 1) |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8121 | return -EINVAL; |
Steven Rostedt (Red Hat) | 69d34da | 2013-03-14 13:50:56 -0400 | [diff] [blame] | 8122 | |
Prateek Sood | 3a53acf | 2019-12-10 09:15:16 +0000 | [diff] [blame] | 8123 | mutex_lock(&event_mutex); |
Steven Rostedt (Red Hat) | 69d34da | 2013-03-14 13:50:56 -0400 | [diff] [blame] | 8124 | mutex_lock(&trace_types_lock); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8125 | ret = set_tracer_flag(tr, 1 << index, val); |
Steven Rostedt (Red Hat) | 69d34da | 2013-03-14 13:50:56 -0400 | [diff] [blame] | 8126 | mutex_unlock(&trace_types_lock); |
Prateek Sood | 3a53acf | 2019-12-10 09:15:16 +0000 | [diff] [blame] | 8127 | mutex_unlock(&event_mutex); |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8128 | |
Steven Rostedt (Red Hat) | 613f04a | 2013-03-14 15:03:53 -0400 | [diff] [blame] | 8129 | if (ret < 0) |
| 8130 | return ret; |
| 8131 | |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8132 | *ppos += cnt; |
| 8133 | |
| 8134 | return cnt; |
| 8135 | } |
| 8136 | |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8137 | static const struct file_operations trace_options_core_fops = { |
| 8138 | .open = tracing_open_generic, |
| 8139 | .read = trace_options_core_read, |
| 8140 | .write = trace_options_core_write, |
Arnd Bergmann | b444786 | 2010-07-07 23:40:11 +0200 | [diff] [blame] | 8141 | .llseek = generic_file_llseek, |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8142 | }; |
| 8143 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 8144 | struct dentry *trace_create_file(const char *name, |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 8145 | umode_t mode, |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 8146 | struct dentry *parent, |
| 8147 | void *data, |
| 8148 | const struct file_operations *fops) |
| 8149 | { |
| 8150 | struct dentry *ret; |
| 8151 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 8152 | ret = tracefs_create_file(name, mode, parent, data, fops); |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 8153 | if (!ret) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 8154 | pr_warn("Could not create tracefs '%s' entry\n", name); |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 8155 | |
| 8156 | return ret; |
| 8157 | } |
| 8158 | |
| 8159 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8160 | static struct dentry *trace_options_init_dentry(struct trace_array *tr) |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8161 | { |
| 8162 | struct dentry *d_tracer; |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8163 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8164 | if (tr->options) |
| 8165 | return tr->options; |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8166 | |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 8167 | d_tracer = tracing_get_dentry(tr); |
Steven Rostedt (Red Hat) | 14a5ae4 | 2015-01-20 11:14:16 -0500 | [diff] [blame] | 8168 | if (IS_ERR(d_tracer)) |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8169 | return NULL; |
| 8170 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 8171 | tr->options = tracefs_create_dir("options", d_tracer); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8172 | if (!tr->options) { |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 8173 | pr_warn("Could not create tracefs directory 'options'\n"); |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8174 | return NULL; |
| 8175 | } |
| 8176 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8177 | return tr->options; |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8178 | } |
| 8179 | |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8180 | static void |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8181 | create_trace_option_file(struct trace_array *tr, |
| 8182 | struct trace_option_dentry *topt, |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8183 | struct tracer_flags *flags, |
| 8184 | struct tracer_opt *opt) |
| 8185 | { |
| 8186 | struct dentry *t_options; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8187 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8188 | t_options = trace_options_init_dentry(tr); |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8189 | if (!t_options) |
| 8190 | return; |
| 8191 | |
| 8192 | topt->flags = flags; |
| 8193 | topt->opt = opt; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8194 | topt->tr = tr; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8195 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 8196 | topt->entry = trace_create_file(opt->name, 0644, t_options, topt, |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8197 | &trace_options_fops); |
| 8198 | |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8199 | } |
| 8200 | |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 8201 | static void |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8202 | create_trace_option_files(struct trace_array *tr, struct tracer *tracer) |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8203 | { |
| 8204 | struct trace_option_dentry *topts; |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 8205 | struct trace_options *tr_topts; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8206 | struct tracer_flags *flags; |
| 8207 | struct tracer_opt *opts; |
| 8208 | int cnt; |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 8209 | int i; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8210 | |
| 8211 | if (!tracer) |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 8212 | return; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8213 | |
| 8214 | flags = tracer->flags; |
| 8215 | |
| 8216 | if (!flags || !flags->opts) |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 8217 | return; |
| 8218 | |
| 8219 | /* |
| 8220 | * If this is an instance, only create flags for tracers |
| 8221 | * the instance may have. |
| 8222 | */ |
| 8223 | if (!trace_ok_for_array(tracer, tr)) |
| 8224 | return; |
| 8225 | |
| 8226 | for (i = 0; i < tr->nr_topts; i++) { |
Chunyu Hu | d39cdd2 | 2016-03-08 21:37:01 +0800 | [diff] [blame] | 8227 | /* Make sure there's no duplicate flags. */ |
| 8228 | if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags)) |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 8229 | return; |
| 8230 | } |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8231 | |
| 8232 | opts = flags->opts; |
| 8233 | |
| 8234 | for (cnt = 0; opts[cnt].name; cnt++) |
| 8235 | ; |
| 8236 | |
Steven Rostedt | 0cfe824 | 2009-02-27 10:51:10 -0500 | [diff] [blame] | 8237 | topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL); |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8238 | if (!topts) |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 8239 | return; |
| 8240 | |
| 8241 | tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1), |
| 8242 | GFP_KERNEL); |
| 8243 | if (!tr_topts) { |
| 8244 | kfree(topts); |
| 8245 | return; |
| 8246 | } |
| 8247 | |
| 8248 | tr->topts = tr_topts; |
| 8249 | tr->topts[tr->nr_topts].tracer = tracer; |
| 8250 | tr->topts[tr->nr_topts].topts = topts; |
| 8251 | tr->nr_topts++; |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8252 | |
Steven Rostedt (Red Hat) | 41d9c0b | 2015-09-29 17:31:55 -0400 | [diff] [blame] | 8253 | for (cnt = 0; opts[cnt].name; cnt++) { |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8254 | create_trace_option_file(tr, &topts[cnt], flags, |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8255 | &opts[cnt]); |
Steven Rostedt (Red Hat) | 41d9c0b | 2015-09-29 17:31:55 -0400 | [diff] [blame] | 8256 | WARN_ONCE(topts[cnt].entry == NULL, |
| 8257 | "Failed to create trace option: %s", |
| 8258 | opts[cnt].name); |
| 8259 | } |
Steven Rostedt | 577b785 | 2009-02-26 23:43:05 -0500 | [diff] [blame] | 8260 | } |
| 8261 | |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8262 | static struct dentry * |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8263 | create_trace_option_core_file(struct trace_array *tr, |
| 8264 | const char *option, long index) |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8265 | { |
| 8266 | struct dentry *t_options; |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8267 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8268 | t_options = trace_options_init_dentry(tr); |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8269 | if (!t_options) |
| 8270 | return NULL; |
| 8271 | |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 8272 | return trace_create_file(option, 0644, t_options, |
| 8273 | (void *)&tr->trace_flags_index[index], |
| 8274 | &trace_options_core_fops); |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8275 | } |
| 8276 | |
Steven Rostedt (Red Hat) | 1627014 | 2015-09-30 12:30:06 -0400 | [diff] [blame] | 8277 | static void create_trace_options_dir(struct trace_array *tr) |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8278 | { |
| 8279 | struct dentry *t_options; |
Steven Rostedt (Red Hat) | 1627014 | 2015-09-30 12:30:06 -0400 | [diff] [blame] | 8280 | bool top_level = tr == &global_trace; |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8281 | int i; |
| 8282 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8283 | t_options = trace_options_init_dentry(tr); |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8284 | if (!t_options) |
| 8285 | return; |
| 8286 | |
Steven Rostedt (Red Hat) | 1627014 | 2015-09-30 12:30:06 -0400 | [diff] [blame] | 8287 | for (i = 0; trace_options[i]; i++) { |
| 8288 | if (top_level || |
| 8289 | !((1 << i) & TOP_LEVEL_TRACE_FLAGS)) |
| 8290 | create_trace_option_core_file(tr, trace_options[i], i); |
| 8291 | } |
Steven Rostedt | a825907 | 2009-02-26 22:19:12 -0500 | [diff] [blame] | 8292 | } |
| 8293 | |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 8294 | static ssize_t |
| 8295 | rb_simple_read(struct file *filp, char __user *ubuf, |
| 8296 | size_t cnt, loff_t *ppos) |
| 8297 | { |
Steven Rostedt | 348f0fc | 2012-04-16 15:41:28 -0400 | [diff] [blame] | 8298 | struct trace_array *tr = filp->private_data; |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 8299 | char buf[64]; |
| 8300 | int r; |
| 8301 | |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 8302 | r = tracer_tracing_is_on(tr); |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 8303 | r = sprintf(buf, "%d\n", r); |
| 8304 | |
| 8305 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 8306 | } |
| 8307 | |
| 8308 | static ssize_t |
| 8309 | rb_simple_write(struct file *filp, const char __user *ubuf, |
| 8310 | size_t cnt, loff_t *ppos) |
| 8311 | { |
Steven Rostedt | 348f0fc | 2012-04-16 15:41:28 -0400 | [diff] [blame] | 8312 | struct trace_array *tr = filp->private_data; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 8313 | struct trace_buffer *buffer = tr->array_buffer.buffer; |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 8314 | unsigned long val; |
| 8315 | int ret; |
| 8316 | |
| 8317 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 8318 | if (ret) |
| 8319 | return ret; |
| 8320 | |
| 8321 | if (buffer) { |
Steven Rostedt | 2df8f8a | 2013-01-11 16:14:10 -0500 | [diff] [blame] | 8322 | mutex_lock(&trace_types_lock); |
Steven Rostedt (VMware) | f143641 | 2018-08-01 15:40:57 -0400 | [diff] [blame] | 8323 | if (!!val == tracer_tracing_is_on(tr)) { |
| 8324 | val = 0; /* do nothing */ |
| 8325 | } else if (val) { |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 8326 | tracer_tracing_on(tr); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8327 | if (tr->current_trace->start) |
| 8328 | tr->current_trace->start(tr); |
Steven Rostedt | 2df8f8a | 2013-01-11 16:14:10 -0500 | [diff] [blame] | 8329 | } else { |
Steven Rostedt (Red Hat) | 10246fa | 2013-07-01 15:58:24 -0400 | [diff] [blame] | 8330 | tracer_tracing_off(tr); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8331 | if (tr->current_trace->stop) |
| 8332 | tr->current_trace->stop(tr); |
Steven Rostedt | 2df8f8a | 2013-01-11 16:14:10 -0500 | [diff] [blame] | 8333 | } |
| 8334 | mutex_unlock(&trace_types_lock); |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 8335 | } |
| 8336 | |
| 8337 | (*ppos)++; |
| 8338 | |
| 8339 | return cnt; |
| 8340 | } |
| 8341 | |
| 8342 | static const struct file_operations rb_simple_fops = { |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 8343 | .open = tracing_open_generic_tr, |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 8344 | .read = rb_simple_read, |
| 8345 | .write = rb_simple_write, |
Steven Rostedt (Red Hat) | 7b85af6 | 2013-07-01 23:34:22 -0400 | [diff] [blame] | 8346 | .release = tracing_release_generic_tr, |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 8347 | .llseek = default_llseek, |
| 8348 | }; |
| 8349 | |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 8350 | static ssize_t |
| 8351 | buffer_percent_read(struct file *filp, char __user *ubuf, |
| 8352 | size_t cnt, loff_t *ppos) |
| 8353 | { |
| 8354 | struct trace_array *tr = filp->private_data; |
| 8355 | char buf[64]; |
| 8356 | int r; |
| 8357 | |
| 8358 | r = tr->buffer_percent; |
| 8359 | r = sprintf(buf, "%d\n", r); |
| 8360 | |
| 8361 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 8362 | } |
| 8363 | |
| 8364 | static ssize_t |
| 8365 | buffer_percent_write(struct file *filp, const char __user *ubuf, |
| 8366 | size_t cnt, loff_t *ppos) |
| 8367 | { |
| 8368 | struct trace_array *tr = filp->private_data; |
| 8369 | unsigned long val; |
| 8370 | int ret; |
| 8371 | |
| 8372 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 8373 | if (ret) |
| 8374 | return ret; |
| 8375 | |
| 8376 | if (val > 100) |
| 8377 | return -EINVAL; |
| 8378 | |
| 8379 | if (!val) |
| 8380 | val = 1; |
| 8381 | |
| 8382 | tr->buffer_percent = val; |
| 8383 | |
| 8384 | (*ppos)++; |
| 8385 | |
| 8386 | return cnt; |
| 8387 | } |
| 8388 | |
| 8389 | static const struct file_operations buffer_percent_fops = { |
| 8390 | .open = tracing_open_generic_tr, |
| 8391 | .read = buffer_percent_read, |
| 8392 | .write = buffer_percent_write, |
| 8393 | .release = tracing_release_generic_tr, |
| 8394 | .llseek = default_llseek, |
| 8395 | }; |
| 8396 | |
YueHaibing | ff585c5 | 2019-06-14 23:32:10 +0800 | [diff] [blame] | 8397 | static struct dentry *trace_instance_dir; |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8398 | |
| 8399 | static void |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 8400 | init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer); |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8401 | |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 8402 | static int |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 8403 | allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size) |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8404 | { |
| 8405 | enum ring_buffer_flags rb_flags; |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 8406 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 8407 | rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0; |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 8408 | |
Steven Rostedt (Red Hat) | dced341 | 2014-01-14 10:19:46 -0500 | [diff] [blame] | 8409 | buf->tr = tr; |
| 8410 | |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 8411 | buf->buffer = ring_buffer_alloc(size, rb_flags); |
| 8412 | if (!buf->buffer) |
| 8413 | return -ENOMEM; |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 8414 | |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 8415 | buf->data = alloc_percpu(struct trace_array_cpu); |
| 8416 | if (!buf->data) { |
| 8417 | ring_buffer_free(buf->buffer); |
Steven Rostedt (VMware) | 4397f04 | 2017-12-26 20:07:34 -0500 | [diff] [blame] | 8418 | buf->buffer = NULL; |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 8419 | return -ENOMEM; |
| 8420 | } |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 8421 | |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 8422 | /* Allocate the first page for all buffers */ |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 8423 | set_buffer_entries(&tr->array_buffer, |
| 8424 | ring_buffer_size(tr->array_buffer.buffer, 0)); |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 8425 | |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 8426 | return 0; |
| 8427 | } |
| 8428 | |
| 8429 | static int allocate_trace_buffers(struct trace_array *tr, int size) |
| 8430 | { |
| 8431 | int ret; |
| 8432 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 8433 | ret = allocate_trace_buffer(tr, &tr->array_buffer, size); |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 8434 | if (ret) |
| 8435 | return ret; |
| 8436 | |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 8437 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 8438 | ret = allocate_trace_buffer(tr, &tr->max_buffer, |
| 8439 | allocate_snapshot ? size : 1); |
| 8440 | if (WARN_ON(ret)) { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 8441 | ring_buffer_free(tr->array_buffer.buffer); |
| 8442 | tr->array_buffer.buffer = NULL; |
| 8443 | free_percpu(tr->array_buffer.data); |
| 8444 | tr->array_buffer.data = NULL; |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 8445 | return -ENOMEM; |
| 8446 | } |
| 8447 | tr->allocated_snapshot = allocate_snapshot; |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 8448 | |
Steven Rostedt (Red Hat) | 55034cd | 2013-03-07 22:48:09 -0500 | [diff] [blame] | 8449 | /* |
| 8450 | * Only the top level trace array gets its snapshot allocated |
| 8451 | * from the kernel command line. |
| 8452 | */ |
| 8453 | allocate_snapshot = false; |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 8454 | #endif |
| 8455 | return 0; |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 8456 | } |
| 8457 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 8458 | static void free_trace_buffer(struct array_buffer *buf) |
Steven Rostedt (Red Hat) | f0b70cc | 2014-06-10 12:06:30 -0400 | [diff] [blame] | 8459 | { |
| 8460 | if (buf->buffer) { |
| 8461 | ring_buffer_free(buf->buffer); |
| 8462 | buf->buffer = NULL; |
| 8463 | free_percpu(buf->data); |
| 8464 | buf->data = NULL; |
| 8465 | } |
| 8466 | } |
| 8467 | |
Steven Rostedt (Red Hat) | 23aaa3c | 2014-06-06 00:01:46 -0400 | [diff] [blame] | 8468 | static void free_trace_buffers(struct trace_array *tr) |
| 8469 | { |
| 8470 | if (!tr) |
| 8471 | return; |
| 8472 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 8473 | free_trace_buffer(&tr->array_buffer); |
Steven Rostedt (Red Hat) | 23aaa3c | 2014-06-06 00:01:46 -0400 | [diff] [blame] | 8474 | |
| 8475 | #ifdef CONFIG_TRACER_MAX_TRACE |
Steven Rostedt (Red Hat) | f0b70cc | 2014-06-10 12:06:30 -0400 | [diff] [blame] | 8476 | free_trace_buffer(&tr->max_buffer); |
Steven Rostedt (Red Hat) | 23aaa3c | 2014-06-06 00:01:46 -0400 | [diff] [blame] | 8477 | #endif |
| 8478 | } |
| 8479 | |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 8480 | static void init_trace_flags_index(struct trace_array *tr) |
| 8481 | { |
| 8482 | int i; |
| 8483 | |
| 8484 | /* Used by the trace options files */ |
| 8485 | for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) |
| 8486 | tr->trace_flags_index[i] = i; |
| 8487 | } |
| 8488 | |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 8489 | static void __update_tracer_options(struct trace_array *tr) |
| 8490 | { |
| 8491 | struct tracer *t; |
| 8492 | |
| 8493 | for (t = trace_types; t; t = t->next) |
| 8494 | add_tracer_options(tr, t); |
| 8495 | } |
| 8496 | |
| 8497 | static void update_tracer_options(struct trace_array *tr) |
| 8498 | { |
| 8499 | mutex_lock(&trace_types_lock); |
| 8500 | __update_tracer_options(tr); |
| 8501 | mutex_unlock(&trace_types_lock); |
| 8502 | } |
| 8503 | |
Divya Indi | 2887978 | 2019-11-20 11:08:38 -0800 | [diff] [blame] | 8504 | static struct trace_array *trace_array_create(const char *name) |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 8505 | { |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8506 | struct trace_array *tr; |
| 8507 | int ret; |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8508 | |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8509 | ret = -ENOMEM; |
| 8510 | tr = kzalloc(sizeof(*tr), GFP_KERNEL); |
| 8511 | if (!tr) |
Divya Indi | 2887978 | 2019-11-20 11:08:38 -0800 | [diff] [blame] | 8512 | return ERR_PTR(ret); |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8513 | |
| 8514 | tr->name = kstrdup(name, GFP_KERNEL); |
| 8515 | if (!tr->name) |
| 8516 | goto out_free_tr; |
| 8517 | |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 8518 | if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL)) |
| 8519 | goto out_free_tr; |
| 8520 | |
Steven Rostedt (Red Hat) | 2055062 | 2016-04-25 22:40:12 -0400 | [diff] [blame] | 8521 | tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS; |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 8522 | |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 8523 | cpumask_copy(tr->tracing_cpumask, cpu_all_mask); |
| 8524 | |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8525 | raw_spin_lock_init(&tr->start_lock); |
| 8526 | |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 8527 | tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
| 8528 | |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8529 | tr->current_trace = &nop_trace; |
| 8530 | |
| 8531 | INIT_LIST_HEAD(&tr->systems); |
| 8532 | INIT_LIST_HEAD(&tr->events); |
Tom Zanussi | 067fe03 | 2018-01-15 20:51:56 -0600 | [diff] [blame] | 8533 | INIT_LIST_HEAD(&tr->hist_vars); |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 8534 | INIT_LIST_HEAD(&tr->err_log); |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8535 | |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 8536 | if (allocate_trace_buffers(tr, trace_buf_size) < 0) |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8537 | goto out_free_tr; |
| 8538 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 8539 | tr->dir = tracefs_create_dir(name, trace_instance_dir); |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8540 | if (!tr->dir) |
| 8541 | goto out_free_tr; |
| 8542 | |
| 8543 | ret = event_trace_add_tracer(tr->dir, tr); |
Alexander Z Lam | 609e85a | 2013-07-10 17:34:34 -0700 | [diff] [blame] | 8544 | if (ret) { |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 8545 | tracefs_remove_recursive(tr->dir); |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8546 | goto out_free_tr; |
Alexander Z Lam | 609e85a | 2013-07-10 17:34:34 -0700 | [diff] [blame] | 8547 | } |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8548 | |
Steven Rostedt (VMware) | 04ec7bb | 2017-04-05 13:12:55 -0400 | [diff] [blame] | 8549 | ftrace_init_trace_array(tr); |
| 8550 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 8551 | init_tracer_tracefs(tr, tr->dir); |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 8552 | init_trace_flags_index(tr); |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 8553 | __update_tracer_options(tr); |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8554 | |
| 8555 | list_add(&tr->list, &ftrace_trace_arrays); |
| 8556 | |
Divya Indi | 2887978 | 2019-11-20 11:08:38 -0800 | [diff] [blame] | 8557 | tr->ref++; |
| 8558 | |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8559 | |
Divya Indi | f45d122 | 2019-03-20 11:28:51 -0700 | [diff] [blame] | 8560 | return tr; |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8561 | |
| 8562 | out_free_tr: |
Steven Rostedt (Red Hat) | 23aaa3c | 2014-06-06 00:01:46 -0400 | [diff] [blame] | 8563 | free_trace_buffers(tr); |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 8564 | free_cpumask_var(tr->tracing_cpumask); |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8565 | kfree(tr->name); |
| 8566 | kfree(tr); |
| 8567 | |
Divya Indi | f45d122 | 2019-03-20 11:28:51 -0700 | [diff] [blame] | 8568 | return ERR_PTR(ret); |
| 8569 | } |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8570 | |
Divya Indi | f45d122 | 2019-03-20 11:28:51 -0700 | [diff] [blame] | 8571 | static int instance_mkdir(const char *name) |
| 8572 | { |
Divya Indi | 2887978 | 2019-11-20 11:08:38 -0800 | [diff] [blame] | 8573 | struct trace_array *tr; |
| 8574 | int ret; |
| 8575 | |
| 8576 | mutex_lock(&event_mutex); |
| 8577 | mutex_lock(&trace_types_lock); |
| 8578 | |
| 8579 | ret = -EEXIST; |
| 8580 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 8581 | if (tr->name && strcmp(tr->name, name) == 0) |
| 8582 | goto out_unlock; |
| 8583 | } |
| 8584 | |
| 8585 | tr = trace_array_create(name); |
| 8586 | |
| 8587 | ret = PTR_ERR_OR_ZERO(tr); |
| 8588 | |
| 8589 | out_unlock: |
| 8590 | mutex_unlock(&trace_types_lock); |
| 8591 | mutex_unlock(&event_mutex); |
| 8592 | return ret; |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8593 | } |
| 8594 | |
Divya Indi | 2887978 | 2019-11-20 11:08:38 -0800 | [diff] [blame] | 8595 | /** |
| 8596 | * trace_array_get_by_name - Create/Lookup a trace array, given its name. |
| 8597 | * @name: The name of the trace array to be looked up/created. |
| 8598 | * |
| 8599 | * Returns pointer to trace array with given name. |
| 8600 | * NULL, if it cannot be created. |
| 8601 | * |
| 8602 | * NOTE: This function increments the reference counter associated with the |
| 8603 | * trace array returned. This makes sure it cannot be freed while in use. |
| 8604 | * Use trace_array_put() once the trace array is no longer needed. |
| 8605 | * |
| 8606 | */ |
| 8607 | struct trace_array *trace_array_get_by_name(const char *name) |
| 8608 | { |
| 8609 | struct trace_array *tr; |
| 8610 | |
| 8611 | mutex_lock(&event_mutex); |
| 8612 | mutex_lock(&trace_types_lock); |
| 8613 | |
| 8614 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 8615 | if (tr->name && strcmp(tr->name, name) == 0) |
| 8616 | goto out_unlock; |
| 8617 | } |
| 8618 | |
| 8619 | tr = trace_array_create(name); |
| 8620 | |
| 8621 | if (IS_ERR(tr)) |
| 8622 | tr = NULL; |
| 8623 | out_unlock: |
| 8624 | if (tr) |
| 8625 | tr->ref++; |
| 8626 | |
| 8627 | mutex_unlock(&trace_types_lock); |
| 8628 | mutex_unlock(&event_mutex); |
| 8629 | return tr; |
| 8630 | } |
| 8631 | EXPORT_SYMBOL_GPL(trace_array_get_by_name); |
| 8632 | |
Divya Indi | f45d122 | 2019-03-20 11:28:51 -0700 | [diff] [blame] | 8633 | static int __remove_instance(struct trace_array *tr) |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 8634 | { |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 8635 | int i; |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 8636 | |
Divya Indi | 2887978 | 2019-11-20 11:08:38 -0800 | [diff] [blame] | 8637 | /* Reference counter for a newly created trace array = 1. */ |
| 8638 | if (tr->ref > 1 || (tr->current_trace && tr->current_trace->ref)) |
Divya Indi | f45d122 | 2019-03-20 11:28:51 -0700 | [diff] [blame] | 8639 | return -EBUSY; |
Steven Rostedt (Red Hat) | a695cb5 | 2013-03-06 15:27:24 -0500 | [diff] [blame] | 8640 | |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 8641 | list_del(&tr->list); |
| 8642 | |
Steven Rostedt (Red Hat) | 2055062 | 2016-04-25 22:40:12 -0400 | [diff] [blame] | 8643 | /* Disable all the flags that were enabled coming in */ |
| 8644 | for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) { |
| 8645 | if ((1 << i) & ZEROED_TRACE_FLAGS) |
| 8646 | set_tracer_flag(tr, 1 << i, 0); |
| 8647 | } |
| 8648 | |
Steven Rostedt (Red Hat) | 6b450d2 | 2014-01-14 08:43:01 -0500 | [diff] [blame] | 8649 | tracing_set_nop(tr); |
Naveen N. Rao | a0e6369 | 2017-05-16 23:21:26 +0530 | [diff] [blame] | 8650 | clear_ftrace_function_probes(tr); |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 8651 | event_trace_del_tracer(tr); |
Namhyung Kim | d879d0b | 2017-04-17 11:44:27 +0900 | [diff] [blame] | 8652 | ftrace_clear_pids(tr); |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 8653 | ftrace_destroy_function_files(tr); |
Jiaxing Wang | 681a4a2 | 2015-10-18 19:58:08 +0800 | [diff] [blame] | 8654 | tracefs_remove_recursive(tr->dir); |
Steven Rostedt (Red Hat) | a9fcaaa | 2014-06-06 23:17:28 -0400 | [diff] [blame] | 8655 | free_trace_buffers(tr); |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 8656 | |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 8657 | for (i = 0; i < tr->nr_topts; i++) { |
| 8658 | kfree(tr->topts[i].topts); |
| 8659 | } |
| 8660 | kfree(tr->topts); |
| 8661 | |
Chunyu Hu | db9108e0 | 2017-07-20 18:36:09 +0800 | [diff] [blame] | 8662 | free_cpumask_var(tr->tracing_cpumask); |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 8663 | kfree(tr->name); |
| 8664 | kfree(tr); |
Divya Indi | f45d122 | 2019-03-20 11:28:51 -0700 | [diff] [blame] | 8665 | tr = NULL; |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 8666 | |
Divya Indi | f45d122 | 2019-03-20 11:28:51 -0700 | [diff] [blame] | 8667 | return 0; |
| 8668 | } |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 8669 | |
Divya Indi | e585e64 | 2019-08-14 10:55:24 -0700 | [diff] [blame] | 8670 | int trace_array_destroy(struct trace_array *this_tr) |
Divya Indi | f45d122 | 2019-03-20 11:28:51 -0700 | [diff] [blame] | 8671 | { |
Divya Indi | e585e64 | 2019-08-14 10:55:24 -0700 | [diff] [blame] | 8672 | struct trace_array *tr; |
Divya Indi | f45d122 | 2019-03-20 11:28:51 -0700 | [diff] [blame] | 8673 | int ret; |
| 8674 | |
Divya Indi | e585e64 | 2019-08-14 10:55:24 -0700 | [diff] [blame] | 8675 | if (!this_tr) |
Divya Indi | f45d122 | 2019-03-20 11:28:51 -0700 | [diff] [blame] | 8676 | return -EINVAL; |
| 8677 | |
| 8678 | mutex_lock(&event_mutex); |
| 8679 | mutex_lock(&trace_types_lock); |
| 8680 | |
Divya Indi | e585e64 | 2019-08-14 10:55:24 -0700 | [diff] [blame] | 8681 | ret = -ENODEV; |
| 8682 | |
| 8683 | /* Making sure trace array exists before destroying it. */ |
| 8684 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 8685 | if (tr == this_tr) { |
| 8686 | ret = __remove_instance(tr); |
| 8687 | break; |
| 8688 | } |
| 8689 | } |
Divya Indi | f45d122 | 2019-03-20 11:28:51 -0700 | [diff] [blame] | 8690 | |
| 8691 | mutex_unlock(&trace_types_lock); |
| 8692 | mutex_unlock(&event_mutex); |
| 8693 | |
| 8694 | return ret; |
| 8695 | } |
| 8696 | EXPORT_SYMBOL_GPL(trace_array_destroy); |
| 8697 | |
| 8698 | static int instance_rmdir(const char *name) |
| 8699 | { |
| 8700 | struct trace_array *tr; |
| 8701 | int ret; |
| 8702 | |
| 8703 | mutex_lock(&event_mutex); |
| 8704 | mutex_lock(&trace_types_lock); |
| 8705 | |
| 8706 | ret = -ENODEV; |
| 8707 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 8708 | if (tr->name && strcmp(tr->name, name) == 0) { |
| 8709 | ret = __remove_instance(tr); |
| 8710 | break; |
| 8711 | } |
| 8712 | } |
| 8713 | |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 8714 | mutex_unlock(&trace_types_lock); |
Steven Rostedt (VMware) | 12ecef0 | 2017-09-21 16:22:49 -0400 | [diff] [blame] | 8715 | mutex_unlock(&event_mutex); |
Steven Rostedt | 0c8916c | 2012-08-07 16:14:16 -0400 | [diff] [blame] | 8716 | |
| 8717 | return ret; |
| 8718 | } |
| 8719 | |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8720 | static __init void create_trace_instances(struct dentry *d_tracer) |
| 8721 | { |
Steven Rostedt (Red Hat) | eae4735 | 2015-01-21 10:01:39 -0500 | [diff] [blame] | 8722 | trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer, |
| 8723 | instance_mkdir, |
| 8724 | instance_rmdir); |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8725 | if (WARN_ON(!trace_instance_dir)) |
| 8726 | return; |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8727 | } |
| 8728 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8729 | static void |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 8730 | init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer) |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8731 | { |
Steven Rostedt (VMware) | 3dd8095 | 2018-05-09 14:17:48 -0400 | [diff] [blame] | 8732 | struct trace_event_file *file; |
Steven Rostedt (Red Hat) | 121aaee | 2013-03-05 21:52:25 -0500 | [diff] [blame] | 8733 | int cpu; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8734 | |
Steven Rostedt (Red Hat) | 607e2ea | 2013-11-06 22:42:48 -0500 | [diff] [blame] | 8735 | trace_create_file("available_tracers", 0444, d_tracer, |
| 8736 | tr, &show_traces_fops); |
| 8737 | |
| 8738 | trace_create_file("current_tracer", 0644, d_tracer, |
| 8739 | tr, &set_tracer_fops); |
| 8740 | |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 8741 | trace_create_file("tracing_cpumask", 0644, d_tracer, |
| 8742 | tr, &tracing_cpumask_fops); |
| 8743 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8744 | trace_create_file("trace_options", 0644, d_tracer, |
| 8745 | tr, &tracing_iter_fops); |
| 8746 | |
| 8747 | trace_create_file("trace", 0644, d_tracer, |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 8748 | tr, &tracing_fops); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8749 | |
| 8750 | trace_create_file("trace_pipe", 0444, d_tracer, |
Oleg Nesterov | 1554420 | 2013-07-23 17:25:57 +0200 | [diff] [blame] | 8751 | tr, &tracing_pipe_fops); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8752 | |
| 8753 | trace_create_file("buffer_size_kb", 0644, d_tracer, |
Oleg Nesterov | 0bc392e | 2013-07-23 17:26:06 +0200 | [diff] [blame] | 8754 | tr, &tracing_entries_fops); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8755 | |
| 8756 | trace_create_file("buffer_total_size_kb", 0444, d_tracer, |
| 8757 | tr, &tracing_total_entries_fops); |
| 8758 | |
Wang YanQing | 238ae93 | 2013-05-26 16:52:01 +0800 | [diff] [blame] | 8759 | trace_create_file("free_buffer", 0200, d_tracer, |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8760 | tr, &tracing_free_buffer_fops); |
| 8761 | |
| 8762 | trace_create_file("trace_marker", 0220, d_tracer, |
| 8763 | tr, &tracing_mark_fops); |
| 8764 | |
Steven Rostedt (VMware) | 3dd8095 | 2018-05-09 14:17:48 -0400 | [diff] [blame] | 8765 | file = __find_event_file(tr, "ftrace", "print"); |
| 8766 | if (file && file->dir) |
| 8767 | trace_create_file("trigger", 0644, file->dir, file, |
| 8768 | &event_trigger_fops); |
| 8769 | tr->trace_marker_file = file; |
| 8770 | |
Steven Rostedt | fa32e85 | 2016-07-06 15:25:08 -0400 | [diff] [blame] | 8771 | trace_create_file("trace_marker_raw", 0220, d_tracer, |
| 8772 | tr, &tracing_mark_raw_fops); |
| 8773 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8774 | trace_create_file("trace_clock", 0644, d_tracer, tr, |
| 8775 | &trace_clock_fops); |
| 8776 | |
| 8777 | trace_create_file("tracing_on", 0644, d_tracer, |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 8778 | tr, &rb_simple_fops); |
Steven Rostedt (Red Hat) | ce9bae5 | 2013-03-05 21:23:55 -0500 | [diff] [blame] | 8779 | |
Tom Zanussi | 2c1ea60 | 2018-01-15 20:51:41 -0600 | [diff] [blame] | 8780 | trace_create_file("timestamp_mode", 0444, d_tracer, tr, |
| 8781 | &trace_time_stamp_mode_fops); |
| 8782 | |
Steven Rostedt (VMware) | a7b1d74 | 2018-11-29 22:36:47 -0500 | [diff] [blame] | 8783 | tr->buffer_percent = 50; |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 8784 | |
| 8785 | trace_create_file("buffer_percent", 0444, d_tracer, |
| 8786 | tr, &buffer_percent_fops); |
| 8787 | |
Steven Rostedt (Red Hat) | 1627014 | 2015-09-30 12:30:06 -0400 | [diff] [blame] | 8788 | create_trace_options_dir(tr); |
| 8789 | |
Steven Rostedt (Red Hat) | f971cc9 | 2016-09-07 12:45:09 -0400 | [diff] [blame] | 8790 | #if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER) |
Viktor Rosendahl (BMW) | 91edde2 | 2019-10-09 00:08:21 +0200 | [diff] [blame] | 8791 | trace_create_maxlat_file(tr, d_tracer); |
Steven Rostedt (Red Hat) | 6d9b3fa | 2014-01-14 11:28:38 -0500 | [diff] [blame] | 8792 | #endif |
| 8793 | |
Steven Rostedt (Red Hat) | 591dffd | 2014-01-10 16:17:45 -0500 | [diff] [blame] | 8794 | if (ftrace_create_function_files(tr, d_tracer)) |
| 8795 | WARN(1, "Could not allocate function filter files"); |
| 8796 | |
Steven Rostedt (Red Hat) | ce9bae5 | 2013-03-05 21:23:55 -0500 | [diff] [blame] | 8797 | #ifdef CONFIG_TRACER_SNAPSHOT |
| 8798 | trace_create_file("snapshot", 0644, d_tracer, |
Oleg Nesterov | 6484c71 | 2013-07-23 17:26:10 +0200 | [diff] [blame] | 8799 | tr, &snapshot_fops); |
Steven Rostedt (Red Hat) | ce9bae5 | 2013-03-05 21:23:55 -0500 | [diff] [blame] | 8800 | #endif |
Steven Rostedt (Red Hat) | 121aaee | 2013-03-05 21:52:25 -0500 | [diff] [blame] | 8801 | |
Tom Zanussi | 8a06290 | 2019-03-31 18:48:15 -0500 | [diff] [blame] | 8802 | trace_create_file("error_log", 0644, d_tracer, |
| 8803 | tr, &tracing_err_log_fops); |
| 8804 | |
Steven Rostedt (Red Hat) | 121aaee | 2013-03-05 21:52:25 -0500 | [diff] [blame] | 8805 | for_each_tracing_cpu(cpu) |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 8806 | tracing_init_tracefs_percpu(tr, cpu); |
Steven Rostedt (Red Hat) | 121aaee | 2013-03-05 21:52:25 -0500 | [diff] [blame] | 8807 | |
Steven Rostedt (Red Hat) | 345ddcc | 2016-04-22 18:11:33 -0400 | [diff] [blame] | 8808 | ftrace_init_tracefs(tr, d_tracer); |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 8809 | } |
| 8810 | |
Eric W. Biederman | 93faccbb | 2017-02-01 06:06:16 +1300 | [diff] [blame] | 8811 | static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore) |
Steven Rostedt (Red Hat) | f76180b | 2015-01-20 15:48:46 -0500 | [diff] [blame] | 8812 | { |
| 8813 | struct vfsmount *mnt; |
| 8814 | struct file_system_type *type; |
| 8815 | |
| 8816 | /* |
| 8817 | * To maintain backward compatibility for tools that mount |
| 8818 | * debugfs to get to the tracing facility, tracefs is automatically |
| 8819 | * mounted to the debugfs/tracing directory. |
| 8820 | */ |
| 8821 | type = get_fs_type("tracefs"); |
| 8822 | if (!type) |
| 8823 | return NULL; |
Eric W. Biederman | 93faccbb | 2017-02-01 06:06:16 +1300 | [diff] [blame] | 8824 | mnt = vfs_submount(mntpt, type, "tracefs", NULL); |
Steven Rostedt (Red Hat) | f76180b | 2015-01-20 15:48:46 -0500 | [diff] [blame] | 8825 | put_filesystem(type); |
| 8826 | if (IS_ERR(mnt)) |
| 8827 | return NULL; |
| 8828 | mntget(mnt); |
| 8829 | |
| 8830 | return mnt; |
| 8831 | } |
| 8832 | |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 8833 | /** |
| 8834 | * tracing_init_dentry - initialize top level trace array |
| 8835 | * |
| 8836 | * This is called when creating files or directories in the tracing |
| 8837 | * directory. It is called via fs_initcall() by any of the boot up code |
| 8838 | * and expects to return the dentry of the top level tracing directory. |
| 8839 | */ |
| 8840 | struct dentry *tracing_init_dentry(void) |
| 8841 | { |
| 8842 | struct trace_array *tr = &global_trace; |
| 8843 | |
Steven Rostedt (VMware) | a356646 | 2019-12-02 16:25:27 -0500 | [diff] [blame] | 8844 | if (security_locked_down(LOCKDOWN_TRACEFS)) { |
Stephen Rothwell | ee19545 | 2019-12-06 09:25:03 +1100 | [diff] [blame] | 8845 | pr_warn("Tracing disabled due to lockdown\n"); |
Steven Rostedt (VMware) | a356646 | 2019-12-02 16:25:27 -0500 | [diff] [blame] | 8846 | return ERR_PTR(-EPERM); |
| 8847 | } |
| 8848 | |
Steven Rostedt (Red Hat) | f76180b | 2015-01-20 15:48:46 -0500 | [diff] [blame] | 8849 | /* The top level trace array uses NULL as parent */ |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 8850 | if (tr->dir) |
Steven Rostedt (Red Hat) | f76180b | 2015-01-20 15:48:46 -0500 | [diff] [blame] | 8851 | return NULL; |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 8852 | |
Jiaxing Wang | 8b12919 | 2015-11-06 16:04:16 +0800 | [diff] [blame] | 8853 | if (WARN_ON(!tracefs_initialized()) || |
| 8854 | (IS_ENABLED(CONFIG_DEBUG_FS) && |
| 8855 | WARN_ON(!debugfs_initialized()))) |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 8856 | return ERR_PTR(-ENODEV); |
| 8857 | |
Steven Rostedt (Red Hat) | f76180b | 2015-01-20 15:48:46 -0500 | [diff] [blame] | 8858 | /* |
| 8859 | * As there may still be users that expect the tracing |
| 8860 | * files to exist in debugfs/tracing, we must automount |
| 8861 | * the tracefs file system there, so older tools still |
| 8862 | * work with the newer kerenl. |
| 8863 | */ |
| 8864 | tr->dir = debugfs_create_automount("tracing", NULL, |
| 8865 | trace_automount, NULL); |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 8866 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 8867 | return NULL; |
Steven Rostedt (Red Hat) | 7eeafbc | 2015-01-26 21:00:48 -0500 | [diff] [blame] | 8868 | } |
| 8869 | |
Jeremy Linton | 00f4b65 | 2017-05-31 16:56:43 -0500 | [diff] [blame] | 8870 | extern struct trace_eval_map *__start_ftrace_eval_maps[]; |
| 8871 | extern struct trace_eval_map *__stop_ftrace_eval_maps[]; |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 8872 | |
Jeremy Linton | 5f60b35 | 2017-05-31 16:56:47 -0500 | [diff] [blame] | 8873 | static void __init trace_eval_init(void) |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 8874 | { |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 8875 | int len; |
| 8876 | |
Jeremy Linton | 02fd7f6 | 2017-05-31 16:56:42 -0500 | [diff] [blame] | 8877 | len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps; |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 8878 | trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len); |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 8879 | } |
| 8880 | |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 8881 | #ifdef CONFIG_MODULES |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 8882 | static void trace_module_add_evals(struct module *mod) |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 8883 | { |
Jeremy Linton | 99be647 | 2017-05-31 16:56:44 -0500 | [diff] [blame] | 8884 | if (!mod->num_trace_evals) |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 8885 | return; |
| 8886 | |
| 8887 | /* |
| 8888 | * Modules with bad taint do not have events created, do |
| 8889 | * not bother with enums either. |
| 8890 | */ |
| 8891 | if (trace_module_has_bad_taint(mod)) |
| 8892 | return; |
| 8893 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 8894 | trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals); |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 8895 | } |
| 8896 | |
Jeremy Linton | 681bec0 | 2017-05-31 16:56:53 -0500 | [diff] [blame] | 8897 | #ifdef CONFIG_TRACE_EVAL_MAP_FILE |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 8898 | static void trace_module_remove_evals(struct module *mod) |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 8899 | { |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 8900 | union trace_eval_map_item *map; |
| 8901 | union trace_eval_map_item **last = &trace_eval_maps; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 8902 | |
Jeremy Linton | 99be647 | 2017-05-31 16:56:44 -0500 | [diff] [blame] | 8903 | if (!mod->num_trace_evals) |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 8904 | return; |
| 8905 | |
Jeremy Linton | 1793ed9 | 2017-05-31 16:56:46 -0500 | [diff] [blame] | 8906 | mutex_lock(&trace_eval_mutex); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 8907 | |
Jeremy Linton | 23bf8cb | 2017-05-31 16:56:45 -0500 | [diff] [blame] | 8908 | map = trace_eval_maps; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 8909 | |
| 8910 | while (map) { |
| 8911 | if (map->head.mod == mod) |
| 8912 | break; |
Jeremy Linton | 5f60b35 | 2017-05-31 16:56:47 -0500 | [diff] [blame] | 8913 | map = trace_eval_jmp_to_tail(map); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 8914 | last = &map->tail.next; |
| 8915 | map = map->tail.next; |
| 8916 | } |
| 8917 | if (!map) |
| 8918 | goto out; |
| 8919 | |
Jeremy Linton | 5f60b35 | 2017-05-31 16:56:47 -0500 | [diff] [blame] | 8920 | *last = trace_eval_jmp_to_tail(map)->tail.next; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 8921 | kfree(map); |
| 8922 | out: |
Jeremy Linton | 1793ed9 | 2017-05-31 16:56:46 -0500 | [diff] [blame] | 8923 | mutex_unlock(&trace_eval_mutex); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 8924 | } |
| 8925 | #else |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 8926 | static inline void trace_module_remove_evals(struct module *mod) { } |
Jeremy Linton | 681bec0 | 2017-05-31 16:56:53 -0500 | [diff] [blame] | 8927 | #endif /* CONFIG_TRACE_EVAL_MAP_FILE */ |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 8928 | |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 8929 | static int trace_module_notify(struct notifier_block *self, |
| 8930 | unsigned long val, void *data) |
| 8931 | { |
| 8932 | struct module *mod = data; |
| 8933 | |
| 8934 | switch (val) { |
| 8935 | case MODULE_STATE_COMING: |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 8936 | trace_module_add_evals(mod); |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 8937 | break; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 8938 | case MODULE_STATE_GOING: |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 8939 | trace_module_remove_evals(mod); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 8940 | break; |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 8941 | } |
| 8942 | |
| 8943 | return 0; |
| 8944 | } |
| 8945 | |
| 8946 | static struct notifier_block trace_module_nb = { |
| 8947 | .notifier_call = trace_module_notify, |
| 8948 | .priority = 0, |
| 8949 | }; |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 8950 | #endif /* CONFIG_MODULES */ |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 8951 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 8952 | static __init int tracer_init_tracefs(void) |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 8953 | { |
| 8954 | struct dentry *d_tracer; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 8955 | |
Lai Jiangshan | 7e53bd4 | 2010-01-06 20:08:50 +0800 | [diff] [blame] | 8956 | trace_access_lock_init(); |
| 8957 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 8958 | d_tracer = tracing_init_dentry(); |
Steven Rostedt (Red Hat) | 14a5ae4 | 2015-01-20 11:14:16 -0500 | [diff] [blame] | 8959 | if (IS_ERR(d_tracer)) |
Namhyung Kim | ed6f1c9 | 2013-04-10 09:18:12 +0900 | [diff] [blame] | 8960 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 8961 | |
Steven Rostedt (VMware) | 58b9254 | 2018-05-08 15:09:27 -0400 | [diff] [blame] | 8962 | event_trace_init(); |
| 8963 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 8964 | init_tracer_tracefs(&global_trace, d_tracer); |
Steven Rostedt (Red Hat) | 501c237 | 2016-07-05 10:04:34 -0400 | [diff] [blame] | 8965 | ftrace_init_tracefs_toplevel(&global_trace, d_tracer); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 8966 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 8967 | trace_create_file("tracing_thresh", 0644, d_tracer, |
Stanislav Fomichev | 6508fa7 | 2014-07-18 15:17:27 +0400 | [diff] [blame] | 8968 | &global_trace, &tracing_thresh_fops); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 8969 | |
Li Zefan | 339ae5d | 2009-04-17 10:34:30 +0800 | [diff] [blame] | 8970 | trace_create_file("README", 0444, d_tracer, |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 8971 | NULL, &tracing_readme_fops); |
Ingo Molnar | 7bd2f24 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 8972 | |
Avadh Patel | 69abe6a | 2009-04-10 16:04:48 -0400 | [diff] [blame] | 8973 | trace_create_file("saved_cmdlines", 0444, d_tracer, |
| 8974 | NULL, &tracing_saved_cmdlines_fops); |
Pekka Paalanen | 5bf9a1e | 2008-09-16 22:06:42 +0300 | [diff] [blame] | 8975 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 8976 | trace_create_file("saved_cmdlines_size", 0644, d_tracer, |
| 8977 | NULL, &tracing_saved_cmdlines_size_fops); |
| 8978 | |
Michael Sartain | 99c621d | 2017-07-05 22:07:15 -0600 | [diff] [blame] | 8979 | trace_create_file("saved_tgids", 0444, d_tracer, |
| 8980 | NULL, &tracing_saved_tgids_fops); |
| 8981 | |
Jeremy Linton | 5f60b35 | 2017-05-31 16:56:47 -0500 | [diff] [blame] | 8982 | trace_eval_init(); |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 8983 | |
Jeremy Linton | f57a414 | 2017-05-31 16:56:48 -0500 | [diff] [blame] | 8984 | trace_create_eval_file(d_tracer); |
Steven Rostedt (Red Hat) | 9828413 | 2015-03-31 17:23:45 -0400 | [diff] [blame] | 8985 | |
Steven Rostedt (Red Hat) | 3673b8e | 2015-03-25 15:44:21 -0400 | [diff] [blame] | 8986 | #ifdef CONFIG_MODULES |
| 8987 | register_module_notifier(&trace_module_nb); |
| 8988 | #endif |
| 8989 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 8990 | #ifdef CONFIG_DYNAMIC_FTRACE |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 8991 | trace_create_file("dyn_ftrace_total_info", 0444, d_tracer, |
Steven Rostedt (VMware) | da537f0 | 2019-10-01 14:38:07 -0400 | [diff] [blame] | 8992 | NULL, &tracing_dyn_info_fops); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 8993 | #endif |
Frederic Weisbecker | b04cc6b | 2009-02-25 03:22:28 +0100 | [diff] [blame] | 8994 | |
Steven Rostedt | 277ba04 | 2012-08-03 16:10:49 -0400 | [diff] [blame] | 8995 | create_trace_instances(d_tracer); |
Hiraku Toyooka | debdd57 | 2012-12-26 11:53:00 +0900 | [diff] [blame] | 8996 | |
Steven Rostedt (Red Hat) | 37aea98 | 2015-09-30 14:27:31 -0400 | [diff] [blame] | 8997 | update_tracer_options(&global_trace); |
Steven Rostedt (Red Hat) | 09d23a1 | 2015-02-03 12:45:53 -0500 | [diff] [blame] | 8998 | |
Frédéric Weisbecker | b5ad384 | 2008-09-23 11:34:32 +0100 | [diff] [blame] | 8999 | return 0; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 9000 | } |
| 9001 | |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9002 | static int trace_panic_handler(struct notifier_block *this, |
| 9003 | unsigned long event, void *unused) |
| 9004 | { |
Steven Rostedt | 944ac42 | 2008-10-23 19:26:08 -0400 | [diff] [blame] | 9005 | if (ftrace_dump_on_oops) |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 9006 | ftrace_dump(ftrace_dump_on_oops); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9007 | return NOTIFY_OK; |
| 9008 | } |
| 9009 | |
| 9010 | static struct notifier_block trace_panic_notifier = { |
| 9011 | .notifier_call = trace_panic_handler, |
| 9012 | .next = NULL, |
| 9013 | .priority = 150 /* priority: INT_MAX >= x >= 0 */ |
| 9014 | }; |
| 9015 | |
| 9016 | static int trace_die_handler(struct notifier_block *self, |
| 9017 | unsigned long val, |
| 9018 | void *data) |
| 9019 | { |
| 9020 | switch (val) { |
| 9021 | case DIE_OOPS: |
Steven Rostedt | 944ac42 | 2008-10-23 19:26:08 -0400 | [diff] [blame] | 9022 | if (ftrace_dump_on_oops) |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 9023 | ftrace_dump(ftrace_dump_on_oops); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9024 | break; |
| 9025 | default: |
| 9026 | break; |
| 9027 | } |
| 9028 | return NOTIFY_OK; |
| 9029 | } |
| 9030 | |
| 9031 | static struct notifier_block trace_die_notifier = { |
| 9032 | .notifier_call = trace_die_handler, |
| 9033 | .priority = 200 |
| 9034 | }; |
| 9035 | |
| 9036 | /* |
| 9037 | * printk is set to max of 1024, we really don't need it that big. |
| 9038 | * Nothing should be printing 1000 characters anyway. |
| 9039 | */ |
| 9040 | #define TRACE_MAX_PRINT 1000 |
| 9041 | |
| 9042 | /* |
| 9043 | * Define here KERN_TRACE so that we have one place to modify |
| 9044 | * it if we decide to change what log level the ftrace dump |
| 9045 | * should be at. |
| 9046 | */ |
Steven Rostedt | 428aee1 | 2009-01-14 12:24:42 -0500 | [diff] [blame] | 9047 | #define KERN_TRACE KERN_EMERG |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9048 | |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 9049 | void |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9050 | trace_printk_seq(struct trace_seq *s) |
| 9051 | { |
| 9052 | /* Probably should print a warning here. */ |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 9053 | if (s->seq.len >= TRACE_MAX_PRINT) |
| 9054 | s->seq.len = TRACE_MAX_PRINT; |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9055 | |
Steven Rostedt (Red Hat) | 820b75f | 2014-11-19 10:56:41 -0500 | [diff] [blame] | 9056 | /* |
| 9057 | * More paranoid code. Although the buffer size is set to |
| 9058 | * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just |
| 9059 | * an extra layer of protection. |
| 9060 | */ |
| 9061 | if (WARN_ON_ONCE(s->seq.len >= s->seq.size)) |
| 9062 | s->seq.len = s->seq.size - 1; |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9063 | |
| 9064 | /* should be zero ended, but we are paranoid. */ |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 9065 | s->buffer[s->seq.len] = 0; |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9066 | |
| 9067 | printk(KERN_TRACE "%s", s->buffer); |
| 9068 | |
Steven Rostedt | f952075 | 2009-03-02 14:04:40 -0500 | [diff] [blame] | 9069 | trace_seq_init(s); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9070 | } |
| 9071 | |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 9072 | void trace_init_global_iter(struct trace_iterator *iter) |
| 9073 | { |
| 9074 | iter->tr = &global_trace; |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 9075 | iter->trace = iter->tr->current_trace; |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 9076 | iter->cpu_file = RING_BUFFER_ALL_CPUS; |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 9077 | iter->array_buffer = &global_trace.array_buffer; |
Cody P Schafer | b2f974d | 2013-10-23 11:49:57 -0700 | [diff] [blame] | 9078 | |
| 9079 | if (iter->trace && iter->trace->open) |
| 9080 | iter->trace->open(iter); |
| 9081 | |
| 9082 | /* Annotate start of buffers if we had overruns */ |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 9083 | if (ring_buffer_overruns(iter->array_buffer->buffer)) |
Cody P Schafer | b2f974d | 2013-10-23 11:49:57 -0700 | [diff] [blame] | 9084 | iter->iter_flags |= TRACE_FILE_ANNOTATE; |
| 9085 | |
| 9086 | /* Output in nanoseconds only if we are using a clock in nanoseconds. */ |
| 9087 | if (trace_clocks[iter->tr->clock_id].in_ns) |
| 9088 | iter->iter_flags |= TRACE_FILE_TIME_IN_NS; |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 9089 | } |
| 9090 | |
Steven Rostedt (Red Hat) | 7fe70b5 | 2013-03-15 13:10:35 -0400 | [diff] [blame] | 9091 | void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9092 | { |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9093 | /* use static because iter can be a bit big for the stack */ |
| 9094 | static struct trace_iterator iter; |
Steven Rostedt (Red Hat) | 7fe70b5 | 2013-03-15 13:10:35 -0400 | [diff] [blame] | 9095 | static atomic_t dump_running; |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 9096 | struct trace_array *tr = &global_trace; |
Frederic Weisbecker | cf586b6 | 2009-03-22 05:04:35 +0100 | [diff] [blame] | 9097 | unsigned int old_userobj; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 9098 | unsigned long flags; |
| 9099 | int cnt = 0, cpu; |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9100 | |
Steven Rostedt (Red Hat) | 7fe70b5 | 2013-03-15 13:10:35 -0400 | [diff] [blame] | 9101 | /* Only allow one dump user at a time. */ |
| 9102 | if (atomic_inc_return(&dump_running) != 1) { |
| 9103 | atomic_dec(&dump_running); |
| 9104 | return; |
Steven Rostedt | e0a413f | 2011-09-29 21:26:16 -0400 | [diff] [blame] | 9105 | } |
| 9106 | |
Steven Rostedt (Red Hat) | 7fe70b5 | 2013-03-15 13:10:35 -0400 | [diff] [blame] | 9107 | /* |
| 9108 | * Always turn off tracing when we dump. |
| 9109 | * We don't need to show trace output of what happens |
| 9110 | * between multiple crashes. |
| 9111 | * |
| 9112 | * If the user does a sysrq-z, then they can re-enable |
| 9113 | * tracing with echo 1 > tracing_on. |
| 9114 | */ |
| 9115 | tracing_off(); |
| 9116 | |
| 9117 | local_irq_save(flags); |
Petr Mladek | 03fc7f9 | 2018-06-27 16:20:28 +0200 | [diff] [blame] | 9118 | printk_nmi_direct_enter(); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9119 | |
Jovi Zhang | 38dbe0b | 2013-01-25 18:03:07 +0800 | [diff] [blame] | 9120 | /* Simulate the iterator */ |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 9121 | trace_init_global_iter(&iter); |
| 9122 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 9123 | for_each_tracing_cpu(cpu) { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 9124 | atomic_inc(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 9125 | } |
| 9126 | |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 9127 | old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ; |
Frederic Weisbecker | cf586b6 | 2009-03-22 05:04:35 +0100 | [diff] [blame] | 9128 | |
Török Edwin | b54d3de | 2008-11-22 13:28:48 +0200 | [diff] [blame] | 9129 | /* don't look at user memory in panic mode */ |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 9130 | tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ; |
Török Edwin | b54d3de | 2008-11-22 13:28:48 +0200 | [diff] [blame] | 9131 | |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 9132 | switch (oops_dump_mode) { |
| 9133 | case DUMP_ALL: |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 9134 | iter.cpu_file = RING_BUFFER_ALL_CPUS; |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 9135 | break; |
| 9136 | case DUMP_ORIG: |
| 9137 | iter.cpu_file = raw_smp_processor_id(); |
| 9138 | break; |
| 9139 | case DUMP_NONE: |
| 9140 | goto out_enable; |
| 9141 | default: |
| 9142 | printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n"); |
Steven Rostedt | ae3b509 | 2013-01-23 15:22:59 -0500 | [diff] [blame] | 9143 | iter.cpu_file = RING_BUFFER_ALL_CPUS; |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 9144 | } |
| 9145 | |
| 9146 | printk(KERN_TRACE "Dumping ftrace buffer:\n"); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9147 | |
Steven Rostedt (Red Hat) | 7fe70b5 | 2013-03-15 13:10:35 -0400 | [diff] [blame] | 9148 | /* Did function tracer already get disabled? */ |
| 9149 | if (ftrace_is_dead()) { |
| 9150 | printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n"); |
| 9151 | printk("# MAY BE MISSING FUNCTION EVENTS\n"); |
| 9152 | } |
| 9153 | |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9154 | /* |
| 9155 | * We need to stop all tracing on all CPUS to read the |
| 9156 | * the next buffer. This is a bit expensive, but is |
| 9157 | * not done often. We fill all what we can read, |
| 9158 | * and then release the locks again. |
| 9159 | */ |
| 9160 | |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9161 | while (!trace_empty(&iter)) { |
| 9162 | |
| 9163 | if (!cnt) |
| 9164 | printk(KERN_TRACE "---------------------------------\n"); |
| 9165 | |
| 9166 | cnt++; |
| 9167 | |
Miguel Ojeda | 0c97bf8 | 2019-05-23 14:45:35 +0200 | [diff] [blame] | 9168 | trace_iterator_reset(&iter); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9169 | iter.iter_flags |= TRACE_FILE_LAT_FMT; |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9170 | |
Jason Wessel | 955b61e | 2010-08-05 09:22:23 -0500 | [diff] [blame] | 9171 | if (trace_find_next_entry_inc(&iter) != NULL) { |
Lai Jiangshan | 74e7ff8 | 2009-07-28 20:17:22 +0800 | [diff] [blame] | 9172 | int ret; |
| 9173 | |
| 9174 | ret = print_trace_line(&iter); |
| 9175 | if (ret != TRACE_TYPE_NO_CONSUME) |
| 9176 | trace_consume(&iter); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9177 | } |
Steven Rostedt | b892e5c | 2012-03-01 22:06:48 -0500 | [diff] [blame] | 9178 | touch_nmi_watchdog(); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9179 | |
| 9180 | trace_printk_seq(&iter.seq); |
| 9181 | } |
| 9182 | |
| 9183 | if (!cnt) |
| 9184 | printk(KERN_TRACE " (ftrace buffer empty)\n"); |
| 9185 | else |
| 9186 | printk(KERN_TRACE "---------------------------------\n"); |
| 9187 | |
Frederic Weisbecker | cecbca9 | 2010-04-18 19:08:41 +0200 | [diff] [blame] | 9188 | out_enable: |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 9189 | tr->trace_flags |= old_userobj; |
Frederic Weisbecker | cf586b6 | 2009-03-22 05:04:35 +0100 | [diff] [blame] | 9190 | |
Steven Rostedt (Red Hat) | 7fe70b5 | 2013-03-15 13:10:35 -0400 | [diff] [blame] | 9191 | for_each_tracing_cpu(cpu) { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 9192 | atomic_dec(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled); |
Frederic Weisbecker | cf586b6 | 2009-03-22 05:04:35 +0100 | [diff] [blame] | 9193 | } |
Petr Mladek | 03fc7f9 | 2018-06-27 16:20:28 +0200 | [diff] [blame] | 9194 | atomic_dec(&dump_running); |
| 9195 | printk_nmi_direct_exit(); |
Steven Rostedt | cd891ae | 2009-04-28 11:39:34 -0400 | [diff] [blame] | 9196 | local_irq_restore(flags); |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9197 | } |
Paul E. McKenney | a8eecf2 | 2011-10-02 11:01:15 -0700 | [diff] [blame] | 9198 | EXPORT_SYMBOL_GPL(ftrace_dump); |
Frederic Weisbecker | cf586b6 | 2009-03-22 05:04:35 +0100 | [diff] [blame] | 9199 | |
Tom Zanussi | 7e465ba | 2017-09-22 14:58:20 -0500 | [diff] [blame] | 9200 | int trace_run_command(const char *buf, int (*createfn)(int, char **)) |
| 9201 | { |
| 9202 | char **argv; |
| 9203 | int argc, ret; |
| 9204 | |
| 9205 | argc = 0; |
| 9206 | ret = 0; |
| 9207 | argv = argv_split(GFP_KERNEL, buf, &argc); |
| 9208 | if (!argv) |
| 9209 | return -ENOMEM; |
| 9210 | |
| 9211 | if (argc) |
| 9212 | ret = createfn(argc, argv); |
| 9213 | |
| 9214 | argv_free(argv); |
| 9215 | |
| 9216 | return ret; |
| 9217 | } |
| 9218 | |
| 9219 | #define WRITE_BUFSIZE 4096 |
| 9220 | |
| 9221 | ssize_t trace_parse_run_command(struct file *file, const char __user *buffer, |
| 9222 | size_t count, loff_t *ppos, |
| 9223 | int (*createfn)(int, char **)) |
| 9224 | { |
| 9225 | char *kbuf, *buf, *tmp; |
| 9226 | int ret = 0; |
| 9227 | size_t done = 0; |
| 9228 | size_t size; |
| 9229 | |
| 9230 | kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL); |
| 9231 | if (!kbuf) |
| 9232 | return -ENOMEM; |
| 9233 | |
| 9234 | while (done < count) { |
| 9235 | size = count - done; |
| 9236 | |
| 9237 | if (size >= WRITE_BUFSIZE) |
| 9238 | size = WRITE_BUFSIZE - 1; |
| 9239 | |
| 9240 | if (copy_from_user(kbuf, buffer + done, size)) { |
| 9241 | ret = -EFAULT; |
| 9242 | goto out; |
| 9243 | } |
| 9244 | kbuf[size] = '\0'; |
| 9245 | buf = kbuf; |
| 9246 | do { |
| 9247 | tmp = strchr(buf, '\n'); |
| 9248 | if (tmp) { |
| 9249 | *tmp = '\0'; |
| 9250 | size = tmp - buf + 1; |
| 9251 | } else { |
| 9252 | size = strlen(buf); |
| 9253 | if (done + size < count) { |
| 9254 | if (buf != kbuf) |
| 9255 | break; |
| 9256 | /* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */ |
| 9257 | pr_warn("Line length is too long: Should be less than %d\n", |
| 9258 | WRITE_BUFSIZE - 2); |
| 9259 | ret = -EINVAL; |
| 9260 | goto out; |
| 9261 | } |
| 9262 | } |
| 9263 | done += size; |
| 9264 | |
| 9265 | /* Remove comments */ |
| 9266 | tmp = strchr(buf, '#'); |
| 9267 | |
| 9268 | if (tmp) |
| 9269 | *tmp = '\0'; |
| 9270 | |
| 9271 | ret = trace_run_command(buf, createfn); |
| 9272 | if (ret) |
| 9273 | goto out; |
| 9274 | buf += size; |
| 9275 | |
| 9276 | } while (done < count); |
| 9277 | } |
| 9278 | ret = done; |
| 9279 | |
| 9280 | out: |
| 9281 | kfree(kbuf); |
| 9282 | |
| 9283 | return ret; |
| 9284 | } |
| 9285 | |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 9286 | __init static int tracer_alloc_buffers(void) |
| 9287 | { |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 9288 | int ring_buf_size; |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 9289 | int ret = -ENOMEM; |
| 9290 | |
Steven Rostedt (VMware) | a356646 | 2019-12-02 16:25:27 -0500 | [diff] [blame] | 9291 | |
| 9292 | if (security_locked_down(LOCKDOWN_TRACEFS)) { |
Stephen Rothwell | ee19545 | 2019-12-06 09:25:03 +1100 | [diff] [blame] | 9293 | pr_warn("Tracing disabled due to lockdown\n"); |
Steven Rostedt (VMware) | a356646 | 2019-12-02 16:25:27 -0500 | [diff] [blame] | 9294 | return -EPERM; |
| 9295 | } |
| 9296 | |
Steven Rostedt (Red Hat) | b5e87c0 | 2015-09-29 18:13:33 -0400 | [diff] [blame] | 9297 | /* |
| 9298 | * Make sure we don't accidently add more trace options |
| 9299 | * than we have bits for. |
| 9300 | */ |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 9301 | BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE); |
Steven Rostedt (Red Hat) | b5e87c0 | 2015-09-29 18:13:33 -0400 | [diff] [blame] | 9302 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 9303 | if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL)) |
| 9304 | goto out; |
| 9305 | |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 9306 | if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL)) |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 9307 | goto out_free_buffer_mask; |
| 9308 | |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 9309 | /* Only allocate trace_printk buffers if a trace_printk exists */ |
| 9310 | if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt) |
Steven Rostedt | 8169883 | 2012-10-11 10:15:05 -0400 | [diff] [blame] | 9311 | /* Must be called before global_trace.buffer is allocated */ |
Steven Rostedt | 07d777f | 2011-09-22 14:01:55 -0400 | [diff] [blame] | 9312 | trace_printk_init_buffers(); |
| 9313 | |
Steven Rostedt | 73c5162 | 2009-03-11 13:42:01 -0400 | [diff] [blame] | 9314 | /* To save memory, keep the ring buffer size to its minimum */ |
| 9315 | if (ring_buffer_expanded) |
| 9316 | ring_buf_size = trace_buf_size; |
| 9317 | else |
| 9318 | ring_buf_size = 1; |
| 9319 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 9320 | cpumask_copy(tracing_buffer_mask, cpu_possible_mask); |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 9321 | cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask); |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 9322 | |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 9323 | raw_spin_lock_init(&global_trace.start_lock); |
| 9324 | |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 9325 | /* |
| 9326 | * The prepare callbacks allocates some memory for the ring buffer. We |
| 9327 | * don't free the buffer if the if the CPU goes down. If we were to free |
| 9328 | * the buffer, then the user would lose any trace that was in the |
| 9329 | * buffer. The memory will be removed once the "instance" is removed. |
| 9330 | */ |
| 9331 | ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE, |
| 9332 | "trace/RB:preapre", trace_rb_cpu_prepare, |
| 9333 | NULL); |
| 9334 | if (ret < 0) |
| 9335 | goto out_free_cpumask; |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 9336 | /* Used for event triggers */ |
Dan Carpenter | 147d88e0 | 2017-08-01 14:02:01 +0300 | [diff] [blame] | 9337 | ret = -ENOMEM; |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 9338 | temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE); |
| 9339 | if (!temp_buffer) |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 9340 | goto out_rm_hp_state; |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 9341 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 9342 | if (trace_create_savedcmd() < 0) |
| 9343 | goto out_free_temp_buffer; |
| 9344 | |
Steven Rostedt | ab46428 | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 9345 | /* TODO: make the number of buffers hot pluggable with CPUS */ |
Steven Rostedt (Red Hat) | 737223f | 2013-03-05 21:13:47 -0500 | [diff] [blame] | 9346 | if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) { |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 9347 | printk(KERN_ERR "tracer: failed to allocate ring buffer!\n"); |
| 9348 | WARN_ON(1); |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 9349 | goto out_free_savedcmd; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 9350 | } |
Steven Rostedt | a7603ff | 2012-08-06 16:24:11 -0400 | [diff] [blame] | 9351 | |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 9352 | if (global_trace.buffer_disabled) |
| 9353 | tracing_off(); |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 9354 | |
Steven Rostedt | e1e232c | 2014-02-10 23:38:46 -0500 | [diff] [blame] | 9355 | if (trace_boot_clock) { |
| 9356 | ret = tracing_set_clock(&global_trace, trace_boot_clock); |
| 9357 | if (ret < 0) |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 9358 | pr_warn("Trace clock %s not defined, going back to default\n", |
| 9359 | trace_boot_clock); |
Steven Rostedt | e1e232c | 2014-02-10 23:38:46 -0500 | [diff] [blame] | 9360 | } |
| 9361 | |
Steven Rostedt (Red Hat) | ca16431 | 2013-05-23 11:51:10 -0400 | [diff] [blame] | 9362 | /* |
| 9363 | * register_tracer() might reference current_trace, so it |
| 9364 | * needs to be set before we register anything. This is |
| 9365 | * just a bootstrap of current_trace anyway. |
| 9366 | */ |
Steven Rostedt | 2b6080f | 2012-05-11 13:29:49 -0400 | [diff] [blame] | 9367 | global_trace.current_trace = &nop_trace; |
| 9368 | |
Steven Rostedt (Red Hat) | 0b9b12c | 2014-01-14 10:04:59 -0500 | [diff] [blame] | 9369 | global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
| 9370 | |
Steven Rostedt (Red Hat) | 4104d32 | 2014-01-10 17:01:58 -0500 | [diff] [blame] | 9371 | ftrace_init_global_array_ops(&global_trace); |
| 9372 | |
Steven Rostedt (Red Hat) | 9a38a88 | 2015-09-30 11:11:15 -0400 | [diff] [blame] | 9373 | init_trace_flags_index(&global_trace); |
| 9374 | |
Steven Rostedt (Red Hat) | ca16431 | 2013-05-23 11:51:10 -0400 | [diff] [blame] | 9375 | register_tracer(&nop_trace); |
| 9376 | |
Steven Rostedt (VMware) | dbeafd0 | 2017-03-03 13:48:42 -0500 | [diff] [blame] | 9377 | /* Function tracing may start here (via kernel command line) */ |
| 9378 | init_function_trace(); |
| 9379 | |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 9380 | /* All seems OK, enable tracing */ |
| 9381 | tracing_disabled = 0; |
Steven Rostedt | 3928a8a | 2008-09-29 23:02:41 -0400 | [diff] [blame] | 9382 | |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9383 | atomic_notifier_chain_register(&panic_notifier_list, |
| 9384 | &trace_panic_notifier); |
| 9385 | |
| 9386 | register_die_notifier(&trace_die_notifier); |
Frederic Weisbecker | 2fc1dfb | 2009-03-16 01:45:03 +0100 | [diff] [blame] | 9387 | |
Steven Rostedt | ae63b31e | 2012-05-03 23:09:03 -0400 | [diff] [blame] | 9388 | global_trace.flags = TRACE_ARRAY_FL_GLOBAL; |
| 9389 | |
| 9390 | INIT_LIST_HEAD(&global_trace.systems); |
| 9391 | INIT_LIST_HEAD(&global_trace.events); |
Tom Zanussi | 067fe03 | 2018-01-15 20:51:56 -0600 | [diff] [blame] | 9392 | INIT_LIST_HEAD(&global_trace.hist_vars); |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 9393 | INIT_LIST_HEAD(&global_trace.err_log); |
Steven Rostedt | ae63b31e | 2012-05-03 23:09:03 -0400 | [diff] [blame] | 9394 | list_add(&global_trace.list, &ftrace_trace_arrays); |
| 9395 | |
Jiaxing Wang | a4d1e68 | 2015-11-04 09:14:29 +0800 | [diff] [blame] | 9396 | apply_trace_boot_options(); |
Steven Rostedt | 7bcfaf5 | 2012-11-01 22:56:07 -0400 | [diff] [blame] | 9397 | |
Steven Rostedt (Red Hat) | 77fd5c1 | 2013-03-12 11:49:18 -0400 | [diff] [blame] | 9398 | register_snapshot_cmd(); |
| 9399 | |
Frederic Weisbecker | 2fc1dfb | 2009-03-16 01:45:03 +0100 | [diff] [blame] | 9400 | return 0; |
Steven Rostedt | 3f5a54e | 2008-07-30 22:36:46 -0400 | [diff] [blame] | 9401 | |
Yoshihiro YUNOMAE | 939c7a4 | 2014-06-05 10:24:27 +0900 | [diff] [blame] | 9402 | out_free_savedcmd: |
| 9403 | free_saved_cmdlines_buffer(savedcmd); |
Steven Rostedt (Red Hat) | 2c4a33a | 2014-03-25 23:39:41 -0400 | [diff] [blame] | 9404 | out_free_temp_buffer: |
| 9405 | ring_buffer_free(temp_buffer); |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 9406 | out_rm_hp_state: |
| 9407 | cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE); |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 9408 | out_free_cpumask: |
Alexander Z Lam | ccfe9e4 | 2013-08-08 09:47:45 -0700 | [diff] [blame] | 9409 | free_cpumask_var(global_trace.tracing_cpumask); |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 9410 | out_free_buffer_mask: |
| 9411 | free_cpumask_var(tracing_buffer_mask); |
| 9412 | out: |
| 9413 | return ret; |
Steven Rostedt | bc0c38d | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 9414 | } |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 9415 | |
Steven Rostedt (VMware) | e725c73 | 2017-03-03 13:37:33 -0500 | [diff] [blame] | 9416 | void __init early_trace_init(void) |
Steven Rostedt (Red Hat) | 5f893b2 | 2014-12-12 20:05:10 -0500 | [diff] [blame] | 9417 | { |
Steven Rostedt (Red Hat) | 0daa2302 | 2014-12-12 22:27:10 -0500 | [diff] [blame] | 9418 | if (tracepoint_printk) { |
| 9419 | tracepoint_print_iter = |
| 9420 | kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL); |
| 9421 | if (WARN_ON(!tracepoint_print_iter)) |
| 9422 | tracepoint_printk = 0; |
Steven Rostedt (Red Hat) | 42391745 | 2016-11-23 15:52:45 -0500 | [diff] [blame] | 9423 | else |
| 9424 | static_key_enable(&tracepoint_printk_key.key); |
Steven Rostedt (Red Hat) | 0daa2302 | 2014-12-12 22:27:10 -0500 | [diff] [blame] | 9425 | } |
Steven Rostedt (Red Hat) | 5f893b2 | 2014-12-12 20:05:10 -0500 | [diff] [blame] | 9426 | tracer_alloc_buffers(); |
Steven Rostedt (VMware) | e725c73 | 2017-03-03 13:37:33 -0500 | [diff] [blame] | 9427 | } |
| 9428 | |
| 9429 | void __init trace_init(void) |
| 9430 | { |
Steven Rostedt (Red Hat) | 0c564a5 | 2015-03-24 17:58:09 -0400 | [diff] [blame] | 9431 | trace_event_init(); |
Steven Rostedt (Red Hat) | 5f893b2 | 2014-12-12 20:05:10 -0500 | [diff] [blame] | 9432 | } |
| 9433 | |
Steven Rostedt | b2821ae | 2009-02-02 21:38:32 -0500 | [diff] [blame] | 9434 | __init static int clear_boot_tracer(void) |
| 9435 | { |
| 9436 | /* |
| 9437 | * The default tracer at boot buffer is an init section. |
| 9438 | * This function is called in lateinit. If we did not |
| 9439 | * find the boot tracer, then clear it out, to prevent |
| 9440 | * later registration from accessing the buffer that is |
| 9441 | * about to be freed. |
| 9442 | */ |
| 9443 | if (!default_bootup_tracer) |
| 9444 | return 0; |
| 9445 | |
| 9446 | printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n", |
| 9447 | default_bootup_tracer); |
| 9448 | default_bootup_tracer = NULL; |
| 9449 | |
| 9450 | return 0; |
| 9451 | } |
| 9452 | |
Steven Rostedt (Red Hat) | 8434dc9 | 2015-01-20 12:13:40 -0500 | [diff] [blame] | 9453 | fs_initcall(tracer_init_tracefs); |
Steven Rostedt (VMware) | 4bb0f0e | 2017-08-01 12:01:52 -0400 | [diff] [blame] | 9454 | late_initcall_sync(clear_boot_tracer); |
Chris Wilson | 3fd49c9 | 2018-03-30 16:01:31 +0100 | [diff] [blame] | 9455 | |
| 9456 | #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK |
| 9457 | __init static int tracing_set_default_clock(void) |
| 9458 | { |
| 9459 | /* sched_clock_stable() is determined in late_initcall */ |
Chris Wilson | 5125eee | 2018-04-04 22:24:50 +0100 | [diff] [blame] | 9460 | if (!trace_boot_clock && !sched_clock_stable()) { |
Chris Wilson | 3fd49c9 | 2018-03-30 16:01:31 +0100 | [diff] [blame] | 9461 | printk(KERN_WARNING |
| 9462 | "Unstable clock detected, switching default tracing clock to \"global\"\n" |
| 9463 | "If you want to keep using the local clock, then add:\n" |
| 9464 | " \"trace_clock=local\"\n" |
| 9465 | "on the kernel command line\n"); |
| 9466 | tracing_set_clock(&global_trace, "global"); |
| 9467 | } |
| 9468 | |
| 9469 | return 0; |
| 9470 | } |
| 9471 | late_initcall_sync(tracing_set_default_clock); |
| 9472 | #endif |