]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - kernel/trace/trace_boot.c
f5db30d25b0b37b3ab29414e5c1c79137ed95564
[mirror_ubuntu-jammy-kernel.git] / kernel / trace / trace_boot.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * trace_boot.c
4 * Tracing kernel boot-time
5 */
6
7 #define pr_fmt(fmt) "trace_boot: " fmt
8
9 #include <linux/ftrace.h>
10 #include <linux/init.h>
11 #include <linux/bootconfig.h>
12
13 #include "trace.h"
14
15 #define MAX_BUF_LEN 256
16
17 extern int trace_set_options(struct trace_array *tr, char *option);
18 extern int tracing_set_tracer(struct trace_array *tr, const char *buf);
19 extern ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
20 unsigned long size, int cpu_id);
21
22 static void __init
23 trace_boot_set_instance_options(struct trace_array *tr, struct xbc_node *node)
24 {
25 struct xbc_node *anode;
26 const char *p;
27 char buf[MAX_BUF_LEN];
28 unsigned long v = 0;
29
30 /* Common ftrace options */
31 xbc_node_for_each_array_value(node, "options", anode, p) {
32 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) {
33 pr_err("String is too long: %s\n", p);
34 continue;
35 }
36
37 if (trace_set_options(tr, buf) < 0)
38 pr_err("Failed to set option: %s\n", buf);
39 }
40
41 p = xbc_node_find_value(node, "trace_clock", NULL);
42 if (p && *p != '\0') {
43 if (tracing_set_clock(tr, p) < 0)
44 pr_err("Failed to set trace clock: %s\n", p);
45 }
46
47 p = xbc_node_find_value(node, "buffer_size", NULL);
48 if (p && *p != '\0') {
49 v = memparse(p, NULL);
50 if (v < PAGE_SIZE)
51 pr_err("Buffer size is too small: %s\n", p);
52 if (tracing_resize_ring_buffer(tr, v, RING_BUFFER_ALL_CPUS) < 0)
53 pr_err("Failed to resize trace buffer to %s\n", p);
54 }
55 }
56
57 #ifdef CONFIG_EVENT_TRACING
58 extern int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set);
59 extern int trigger_process_regex(struct trace_event_file *file, char *buff);
60
61 static void __init
62 trace_boot_enable_events(struct trace_array *tr, struct xbc_node *node)
63 {
64 struct xbc_node *anode;
65 char buf[MAX_BUF_LEN];
66 const char *p;
67
68 xbc_node_for_each_array_value(node, "events", anode, p) {
69 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) {
70 pr_err("String is too long: %s\n", p);
71 continue;
72 }
73
74 if (ftrace_set_clr_event(tr, buf, 1) < 0)
75 pr_err("Failed to enable event: %s\n", p);
76 }
77 }
78
79 #ifdef CONFIG_KPROBE_EVENTS
80 extern int trace_kprobe_run_command(const char *command);
81
82 static int __init
83 trace_boot_add_kprobe_event(struct xbc_node *node, const char *event)
84 {
85 struct xbc_node *anode;
86 char buf[MAX_BUF_LEN];
87 const char *val;
88 char *p;
89 int len;
90
91 len = snprintf(buf, ARRAY_SIZE(buf) - 1, "p:kprobes/%s ", event);
92 if (len >= ARRAY_SIZE(buf)) {
93 pr_err("Event name is too long: %s\n", event);
94 return -E2BIG;
95 }
96 p = buf + len;
97 len = ARRAY_SIZE(buf) - len;
98
99 xbc_node_for_each_array_value(node, "probes", anode, val) {
100 if (strlcpy(p, val, len) >= len) {
101 pr_err("Probe definition is too long: %s\n", val);
102 return -E2BIG;
103 }
104 if (trace_kprobe_run_command(buf) < 0) {
105 pr_err("Failed to add probe: %s\n", buf);
106 return -EINVAL;
107 }
108 }
109
110 return 0;
111 }
112 #else
113 static inline int __init
114 trace_boot_add_kprobe_event(struct xbc_node *node, const char *event)
115 {
116 pr_err("Kprobe event is not supported.\n");
117 return -ENOTSUPP;
118 }
119 #endif
120
121 #ifdef CONFIG_HIST_TRIGGERS
122 extern int synth_event_run_command(const char *command);
123
124 static int __init
125 trace_boot_add_synth_event(struct xbc_node *node, const char *event)
126 {
127 struct xbc_node *anode;
128 char buf[MAX_BUF_LEN], *q;
129 const char *p;
130 int len, delta, ret;
131
132 len = ARRAY_SIZE(buf);
133 delta = snprintf(buf, len, "%s", event);
134 if (delta >= len) {
135 pr_err("Event name is too long: %s\n", event);
136 return -E2BIG;
137 }
138 len -= delta; q = buf + delta;
139
140 xbc_node_for_each_array_value(node, "fields", anode, p) {
141 delta = snprintf(q, len, " %s;", p);
142 if (delta >= len) {
143 pr_err("fields string is too long: %s\n", p);
144 return -E2BIG;
145 }
146 len -= delta; q += delta;
147 }
148
149 ret = synth_event_run_command(buf);
150 if (ret < 0)
151 pr_err("Failed to add synthetic event: %s\n", buf);
152
153
154 return ret;
155 }
156 #else
157 static inline int __init
158 trace_boot_add_synth_event(struct xbc_node *node, const char *event)
159 {
160 pr_err("Synthetic event is not supported.\n");
161 return -ENOTSUPP;
162 }
163 #endif
164
165 static void __init
166 trace_boot_init_one_event(struct trace_array *tr, struct xbc_node *gnode,
167 struct xbc_node *enode)
168 {
169 struct trace_event_file *file;
170 struct xbc_node *anode;
171 char buf[MAX_BUF_LEN];
172 const char *p, *group, *event;
173
174 group = xbc_node_get_data(gnode);
175 event = xbc_node_get_data(enode);
176
177 if (!strcmp(group, "kprobes"))
178 if (trace_boot_add_kprobe_event(enode, event) < 0)
179 return;
180 if (!strcmp(group, "synthetic"))
181 if (trace_boot_add_synth_event(enode, event) < 0)
182 return;
183
184 mutex_lock(&event_mutex);
185 file = find_event_file(tr, group, event);
186 if (!file) {
187 pr_err("Failed to find event: %s:%s\n", group, event);
188 goto out;
189 }
190
191 p = xbc_node_find_value(enode, "filter", NULL);
192 if (p && *p != '\0') {
193 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
194 pr_err("filter string is too long: %s\n", p);
195 else if (apply_event_filter(file, buf) < 0)
196 pr_err("Failed to apply filter: %s\n", buf);
197 }
198
199 xbc_node_for_each_array_value(enode, "actions", anode, p) {
200 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
201 pr_err("action string is too long: %s\n", p);
202 else if (trigger_process_regex(file, buf) < 0)
203 pr_err("Failed to apply an action: %s\n", buf);
204 }
205
206 if (xbc_node_find_value(enode, "enable", NULL)) {
207 if (trace_event_enable_disable(file, 1, 0) < 0)
208 pr_err("Failed to enable event node: %s:%s\n",
209 group, event);
210 }
211 out:
212 mutex_unlock(&event_mutex);
213 }
214
215 static void __init
216 trace_boot_init_events(struct trace_array *tr, struct xbc_node *node)
217 {
218 struct xbc_node *gnode, *enode;
219
220 node = xbc_node_find_child(node, "event");
221 if (!node)
222 return;
223 /* per-event key starts with "event.GROUP.EVENT" */
224 xbc_node_for_each_child(node, gnode)
225 xbc_node_for_each_child(gnode, enode)
226 trace_boot_init_one_event(tr, gnode, enode);
227 }
228 #else
229 #define trace_boot_enable_events(tr, node) do {} while (0)
230 #define trace_boot_init_events(tr, node) do {} while (0)
231 #endif
232
233 static void __init
234 trace_boot_enable_tracer(struct trace_array *tr, struct xbc_node *node)
235 {
236 const char *p;
237
238 p = xbc_node_find_value(node, "tracer", NULL);
239 if (p && *p != '\0') {
240 if (tracing_set_tracer(tr, p) < 0)
241 pr_err("Failed to set given tracer: %s\n", p);
242 }
243 }
244
245 static void __init
246 trace_boot_init_one_instance(struct trace_array *tr, struct xbc_node *node)
247 {
248 trace_boot_set_instance_options(tr, node);
249 trace_boot_init_events(tr, node);
250 trace_boot_enable_events(tr, node);
251 trace_boot_enable_tracer(tr, node);
252 }
253
254 static void __init
255 trace_boot_init_instances(struct xbc_node *node)
256 {
257 struct xbc_node *inode;
258 struct trace_array *tr;
259 const char *p;
260
261 node = xbc_node_find_child(node, "instance");
262 if (!node)
263 return;
264
265 xbc_node_for_each_child(node, inode) {
266 p = xbc_node_get_data(inode);
267 if (!p || *p == '\0')
268 continue;
269
270 tr = trace_array_get_by_name(p);
271 if (IS_ERR(tr)) {
272 pr_err("Failed to get trace instance %s\n", p);
273 continue;
274 }
275 trace_boot_init_one_instance(tr, inode);
276 }
277 }
278
279 static int __init trace_boot_init(void)
280 {
281 struct xbc_node *trace_node;
282 struct trace_array *tr;
283
284 trace_node = xbc_find_node("ftrace");
285 if (!trace_node)
286 return 0;
287
288 tr = top_trace_array();
289 if (!tr)
290 return 0;
291
292 /* Global trace array is also one instance */
293 trace_boot_init_one_instance(tr, trace_node);
294 trace_boot_init_instances(trace_node);
295
296 return 0;
297 }
298
299 fs_initcall(trace_boot_init);