blob: 1060b044603213c688b94ab8cc1d0a7d3456a4ba [file] [log] [blame]
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +09001// 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
Masami Hiramatsu76a598e2020-01-29 18:36:35 +09009#include <linux/bootconfig.h>
10#include <linux/cpumask.h>
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +090011#include <linux/ftrace.h>
12#include <linux/init.h>
Masami Hiramatsu76a598e2020-01-29 18:36:35 +090013#include <linux/kernel.h>
14#include <linux/mutex.h>
15#include <linux/string.h>
16#include <linux/slab.h>
17#include <linux/trace.h>
18#include <linux/trace_events.h>
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +090019
20#include "trace.h"
21
22#define MAX_BUF_LEN 256
23
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +090024static void __init
Masami Hiramatsu4f712a42020-01-11 01:07:04 +090025trace_boot_set_instance_options(struct trace_array *tr, struct xbc_node *node)
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +090026{
27 struct xbc_node *anode;
28 const char *p;
29 char buf[MAX_BUF_LEN];
30 unsigned long v = 0;
31
32 /* Common ftrace options */
33 xbc_node_for_each_array_value(node, "options", anode, p) {
34 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) {
35 pr_err("String is too long: %s\n", p);
36 continue;
37 }
38
39 if (trace_set_options(tr, buf) < 0)
40 pr_err("Failed to set option: %s\n", buf);
41 }
42
Masami Hiramatsu8490db02020-09-10 17:55:15 +090043 p = xbc_node_find_value(node, "tracing_on", NULL);
44 if (p && *p != '\0') {
45 if (kstrtoul(p, 10, &v))
46 pr_err("Failed to set tracing on: %s\n", p);
47 if (v)
48 tracer_tracing_on(tr);
49 else
50 tracer_tracing_off(tr);
51 }
52
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +090053 p = xbc_node_find_value(node, "trace_clock", NULL);
54 if (p && *p != '\0') {
55 if (tracing_set_clock(tr, p) < 0)
56 pr_err("Failed to set trace clock: %s\n", p);
57 }
58
59 p = xbc_node_find_value(node, "buffer_size", NULL);
60 if (p && *p != '\0') {
61 v = memparse(p, NULL);
62 if (v < PAGE_SIZE)
63 pr_err("Buffer size is too small: %s\n", p);
64 if (tracing_resize_ring_buffer(tr, v, RING_BUFFER_ALL_CPUS) < 0)
65 pr_err("Failed to resize trace buffer to %s\n", p);
66 }
Masami Hiramatsu9d15dbb2020-01-11 01:07:16 +090067
68 p = xbc_node_find_value(node, "cpumask", NULL);
69 if (p && *p != '\0') {
70 cpumask_var_t new_mask;
71
72 if (alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
73 if (cpumask_parse(p, new_mask) < 0 ||
74 tracing_set_cpumask(tr, new_mask) < 0)
75 pr_err("Failed to set new CPU mask %s\n", p);
76 free_cpumask_var(new_mask);
77 }
78 }
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +090079}
80
81#ifdef CONFIG_EVENT_TRACING
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +090082static void __init
83trace_boot_enable_events(struct trace_array *tr, struct xbc_node *node)
84{
85 struct xbc_node *anode;
86 char buf[MAX_BUF_LEN];
87 const char *p;
88
89 xbc_node_for_each_array_value(node, "events", anode, p) {
90 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) {
91 pr_err("String is too long: %s\n", p);
92 continue;
93 }
94
95 if (ftrace_set_clr_event(tr, buf, 1) < 0)
96 pr_err("Failed to enable event: %s\n", p);
97 }
98}
Masami Hiramatsu81a59552020-01-11 01:06:29 +090099
Masami Hiramatsu4d655282020-01-11 01:06:41 +0900100#ifdef CONFIG_KPROBE_EVENTS
Masami Hiramatsu4d655282020-01-11 01:06:41 +0900101static int __init
102trace_boot_add_kprobe_event(struct xbc_node *node, const char *event)
103{
Tom Zanussi29a15482020-01-29 12:59:30 -0600104 struct dynevent_cmd cmd;
Masami Hiramatsu4d655282020-01-11 01:06:41 +0900105 struct xbc_node *anode;
106 char buf[MAX_BUF_LEN];
107 const char *val;
Masami Hiramatsuda0f1f42020-04-25 14:49:17 +0900108 int ret = 0;
Masami Hiramatsu4d655282020-01-11 01:06:41 +0900109
110 xbc_node_for_each_array_value(node, "probes", anode, val) {
Masami Hiramatsuda0f1f42020-04-25 14:49:17 +0900111 kprobe_event_cmd_init(&cmd, buf, MAX_BUF_LEN);
Masami Hiramatsu4d655282020-01-11 01:06:41 +0900112
Masami Hiramatsuda0f1f42020-04-25 14:49:17 +0900113 ret = kprobe_event_gen_cmd_start(&cmd, event, val);
Sascha Ortmann20dc3842020-06-18 18:33:01 +0200114 if (ret) {
115 pr_err("Failed to generate probe: %s\n", buf);
Masami Hiramatsuda0f1f42020-04-25 14:49:17 +0900116 break;
Sascha Ortmann20dc3842020-06-18 18:33:01 +0200117 }
Masami Hiramatsuda0f1f42020-04-25 14:49:17 +0900118
119 ret = kprobe_event_gen_cmd_end(&cmd);
Sascha Ortmann20dc3842020-06-18 18:33:01 +0200120 if (ret) {
Masami Hiramatsuda0f1f42020-04-25 14:49:17 +0900121 pr_err("Failed to add probe: %s\n", buf);
Sascha Ortmann20dc3842020-06-18 18:33:01 +0200122 break;
123 }
Masami Hiramatsuda0f1f42020-04-25 14:49:17 +0900124 }
Tom Zanussi29a15482020-01-29 12:59:30 -0600125
126 return ret;
Masami Hiramatsu4d655282020-01-11 01:06:41 +0900127}
128#else
129static inline int __init
130trace_boot_add_kprobe_event(struct xbc_node *node, const char *event)
131{
132 pr_err("Kprobe event is not supported.\n");
133 return -ENOTSUPP;
134}
135#endif
136
Masami Hiramatsu6c955032020-06-20 12:45:54 +0900137#ifdef CONFIG_SYNTH_EVENTS
Masami Hiramatsu3fbe2d62020-01-11 01:06:52 +0900138static int __init
139trace_boot_add_synth_event(struct xbc_node *node, const char *event)
140{
Tom Zanussifdeb1ac2020-01-29 12:59:26 -0600141 struct dynevent_cmd cmd;
Masami Hiramatsu3fbe2d62020-01-11 01:06:52 +0900142 struct xbc_node *anode;
Tom Zanussifdeb1ac2020-01-29 12:59:26 -0600143 char buf[MAX_BUF_LEN];
Masami Hiramatsu3fbe2d62020-01-11 01:06:52 +0900144 const char *p;
Tom Zanussifdeb1ac2020-01-29 12:59:26 -0600145 int ret;
Masami Hiramatsu3fbe2d62020-01-11 01:06:52 +0900146
Tom Zanussifdeb1ac2020-01-29 12:59:26 -0600147 synth_event_cmd_init(&cmd, buf, MAX_BUF_LEN);
148
149 ret = synth_event_gen_cmd_start(&cmd, event, NULL);
150 if (ret)
151 return ret;
Masami Hiramatsu3fbe2d62020-01-11 01:06:52 +0900152
153 xbc_node_for_each_array_value(node, "fields", anode, p) {
Tom Zanussifdeb1ac2020-01-29 12:59:26 -0600154 ret = synth_event_add_field_str(&cmd, p);
155 if (ret)
156 return ret;
Masami Hiramatsu3fbe2d62020-01-11 01:06:52 +0900157 }
158
Tom Zanussifdeb1ac2020-01-29 12:59:26 -0600159 ret = synth_event_gen_cmd_end(&cmd);
Masami Hiramatsu3fbe2d62020-01-11 01:06:52 +0900160 if (ret < 0)
161 pr_err("Failed to add synthetic event: %s\n", buf);
162
Masami Hiramatsu3fbe2d62020-01-11 01:06:52 +0900163 return ret;
164}
165#else
166static inline int __init
167trace_boot_add_synth_event(struct xbc_node *node, const char *event)
168{
169 pr_err("Synthetic event is not supported.\n");
170 return -ENOTSUPP;
171}
172#endif
173
Masami Hiramatsue66ed862021-08-10 11:07:21 +0900174#ifdef CONFIG_HIST_TRIGGERS
175static int __init __printf(3, 4)
176append_printf(char **bufp, char *end, const char *fmt, ...)
177{
178 va_list args;
179 int ret;
180
181 if (*bufp == end)
182 return -ENOSPC;
183
184 va_start(args, fmt);
185 ret = vsnprintf(*bufp, end - *bufp, fmt, args);
186 if (ret < end - *bufp) {
187 *bufp += ret;
188 } else {
189 *bufp = end;
190 ret = -ERANGE;
191 }
192 va_end(args);
193
194 return ret;
195}
196
197static int __init
198append_str_nospace(char **bufp, char *end, const char *str)
199{
200 char *p = *bufp;
201 int len;
202
203 while (p < end - 1 && *str != '\0') {
204 if (!isspace(*str))
205 *(p++) = *str;
206 str++;
207 }
208 *p = '\0';
209 if (p == end - 1) {
210 *bufp = end;
211 return -ENOSPC;
212 }
213 len = p - *bufp;
214 *bufp = p;
215 return (int)len;
216}
217
218static int __init
219trace_boot_hist_add_array(struct xbc_node *hnode, char **bufp,
220 char *end, const char *key)
221{
222 struct xbc_node *knode, *anode;
223 const char *p;
224 char sep;
225
226 knode = xbc_node_find_child(hnode, key);
227 if (knode) {
228 anode = xbc_node_get_child(knode);
229 if (!anode) {
230 pr_err("hist.%s requires value(s).\n", key);
231 return -EINVAL;
232 }
233
234 append_printf(bufp, end, ":%s", key);
235 sep = '=';
236 xbc_array_for_each_value(anode, p) {
237 append_printf(bufp, end, "%c%s", sep, p);
238 if (sep == '=')
239 sep = ',';
240 }
241 } else
242 return -ENOENT;
243
244 return 0;
245}
246
247static int __init
Masami Hiramatsu89936652021-08-10 11:07:29 +0900248trace_boot_hist_add_one_handler(struct xbc_node *hnode, char **bufp,
249 char *end, const char *handler,
250 const char *param)
Masami Hiramatsue66ed862021-08-10 11:07:21 +0900251{
252 struct xbc_node *knode, *anode;
253 const char *p;
254 char sep;
255
256 /* Compose 'handler' parameter */
257 p = xbc_node_find_value(hnode, param, NULL);
258 if (!p) {
259 pr_err("hist.%s requires '%s' option.\n",
260 xbc_node_get_data(hnode), param);
261 return -EINVAL;
262 }
Masami Hiramatsu89936652021-08-10 11:07:29 +0900263 append_printf(bufp, end, ":%s(%s)", handler, p);
Masami Hiramatsue66ed862021-08-10 11:07:21 +0900264
265 /* Compose 'action' parameter */
266 knode = xbc_node_find_child(hnode, "trace");
267 if (!knode)
268 knode = xbc_node_find_child(hnode, "save");
269
270 if (knode) {
271 anode = xbc_node_get_child(knode);
272 if (!anode || !xbc_node_is_value(anode)) {
273 pr_err("hist.%s.%s requires value(s).\n",
274 xbc_node_get_data(hnode),
275 xbc_node_get_data(knode));
276 return -EINVAL;
277 }
278
279 append_printf(bufp, end, ".%s", xbc_node_get_data(knode));
280 sep = '(';
281 xbc_array_for_each_value(anode, p) {
282 append_printf(bufp, end, "%c%s", sep, p);
283 if (sep == '(')
284 sep = ',';
285 }
286 append_printf(bufp, end, ")");
287 } else if (xbc_node_find_child(hnode, "snapshot")) {
288 append_printf(bufp, end, ".snapshot()");
289 } else {
290 pr_err("hist.%s requires an action.\n",
291 xbc_node_get_data(hnode));
292 return -EINVAL;
293 }
294
295 return 0;
296}
297
Masami Hiramatsu89936652021-08-10 11:07:29 +0900298static int __init
299trace_boot_hist_add_handlers(struct xbc_node *hnode, char **bufp,
300 char *end, const char *param)
301{
302 struct xbc_node *node;
303 const char *p, *handler;
304 int ret;
305
306 handler = xbc_node_get_data(hnode);
307
308 xbc_node_for_each_subkey(hnode, node) {
309 p = xbc_node_get_data(node);
310 if (!isdigit(p[0]))
311 continue;
312 /* All digit started node should be instances. */
313 ret = trace_boot_hist_add_one_handler(node, bufp, end, handler, param);
314 if (ret < 0)
315 break;
316 }
317
318 if (xbc_node_find_child(hnode, param))
319 ret = trace_boot_hist_add_one_handler(hnode, bufp, end, handler, param);
320
321 return ret;
322}
323
Masami Hiramatsue66ed862021-08-10 11:07:21 +0900324/*
325 * Histogram boottime tracing syntax.
326 *
Masami Hiramatsu17abd7c2021-08-10 11:07:36 +0900327 * ftrace.[instance.INSTANCE.]event.GROUP.EVENT.hist[.N] {
Masami Hiramatsue66ed862021-08-10 11:07:21 +0900328 * keys = <KEY>[,...]
329 * values = <VAL>[,...]
330 * sort = <SORT-KEY>[,...]
331 * size = <ENTRIES>
332 * name = <HISTNAME>
333 * var { <VAR> = <EXPR> ... }
334 * pause|continue|clear
Masami Hiramatsu89936652021-08-10 11:07:29 +0900335 * onmax|onchange[.N] { var = <VAR>; <ACTION> [= <PARAM>] }
336 * onmatch[.N] { event = <EVENT>; <ACTION> [= <PARAM>] }
Masami Hiramatsue66ed862021-08-10 11:07:21 +0900337 * filter = <FILTER>
338 * }
339 *
340 * Where <ACTION> are;
341 *
342 * trace = <EVENT>, <ARG1>[, ...]
343 * save = <ARG1>[, ...]
344 * snapshot
345 */
346static int __init
347trace_boot_compose_hist_cmd(struct xbc_node *hnode, char *buf, size_t size)
348{
349 struct xbc_node *node, *knode;
350 char *end = buf + size;
351 const char *p;
352 int ret = 0;
353
354 append_printf(&buf, end, "hist");
355
356 ret = trace_boot_hist_add_array(hnode, &buf, end, "keys");
357 if (ret < 0) {
358 if (ret == -ENOENT)
359 pr_err("hist requires keys.\n");
360 return -EINVAL;
361 }
362
363 ret = trace_boot_hist_add_array(hnode, &buf, end, "values");
364 if (ret == -EINVAL)
365 return ret;
366 ret = trace_boot_hist_add_array(hnode, &buf, end, "sort");
367 if (ret == -EINVAL)
368 return ret;
369
370 p = xbc_node_find_value(hnode, "size", NULL);
371 if (p)
372 append_printf(&buf, end, ":size=%s", p);
373
374 p = xbc_node_find_value(hnode, "name", NULL);
375 if (p)
376 append_printf(&buf, end, ":name=%s", p);
377
378 node = xbc_node_find_child(hnode, "var");
379 if (node) {
380 xbc_node_for_each_key_value(node, knode, p) {
381 /* Expression must not include spaces. */
382 append_printf(&buf, end, ":%s=",
383 xbc_node_get_data(knode));
384 append_str_nospace(&buf, end, p);
385 }
386 }
387
388 /* Histogram control attributes (mutual exclusive) */
389 if (xbc_node_find_child(hnode, "pause"))
390 append_printf(&buf, end, ":pause");
391 else if (xbc_node_find_child(hnode, "continue"))
392 append_printf(&buf, end, ":continue");
393 else if (xbc_node_find_child(hnode, "clear"))
394 append_printf(&buf, end, ":clear");
395
396 /* Histogram handler and actions */
397 node = xbc_node_find_child(hnode, "onmax");
Masami Hiramatsu89936652021-08-10 11:07:29 +0900398 if (node && trace_boot_hist_add_handlers(node, &buf, end, "var") < 0)
Masami Hiramatsue66ed862021-08-10 11:07:21 +0900399 return -EINVAL;
400 node = xbc_node_find_child(hnode, "onchange");
Masami Hiramatsu89936652021-08-10 11:07:29 +0900401 if (node && trace_boot_hist_add_handlers(node, &buf, end, "var") < 0)
Masami Hiramatsue66ed862021-08-10 11:07:21 +0900402 return -EINVAL;
403 node = xbc_node_find_child(hnode, "onmatch");
Masami Hiramatsu89936652021-08-10 11:07:29 +0900404 if (node && trace_boot_hist_add_handlers(node, &buf, end, "event") < 0)
Masami Hiramatsue66ed862021-08-10 11:07:21 +0900405 return -EINVAL;
406
407 p = xbc_node_find_value(hnode, "filter", NULL);
408 if (p)
409 append_printf(&buf, end, " if %s", p);
410
411 if (buf == end) {
412 pr_err("hist exceeds the max command length.\n");
413 return -E2BIG;
414 }
415
416 return 0;
417}
Masami Hiramatsu17abd7c2021-08-10 11:07:36 +0900418
419static void __init
420trace_boot_init_histograms(struct trace_event_file *file,
421 struct xbc_node *hnode, char *buf, size_t size)
Masami Hiramatsue66ed862021-08-10 11:07:21 +0900422{
Masami Hiramatsu17abd7c2021-08-10 11:07:36 +0900423 struct xbc_node *node;
424 const char *p;
Masami Hiramatsu64dc7f62021-08-10 11:07:44 +0900425 char *tmp;
Masami Hiramatsu17abd7c2021-08-10 11:07:36 +0900426
427 xbc_node_for_each_subkey(hnode, node) {
428 p = xbc_node_get_data(node);
429 if (!isdigit(p[0]))
430 continue;
431 /* All digit started node should be instances. */
432 if (trace_boot_compose_hist_cmd(node, buf, size) == 0) {
Masami Hiramatsu64dc7f62021-08-10 11:07:44 +0900433 tmp = kstrdup(buf, GFP_KERNEL);
Masami Hiramatsu17abd7c2021-08-10 11:07:36 +0900434 if (trigger_process_regex(file, buf) < 0)
Masami Hiramatsu64dc7f62021-08-10 11:07:44 +0900435 pr_err("Failed to apply hist trigger: %s\n", tmp);
436 kfree(tmp);
Masami Hiramatsu17abd7c2021-08-10 11:07:36 +0900437 }
438 }
439
440 if (xbc_node_find_child(hnode, "keys")) {
Masami Hiramatsu64dc7f62021-08-10 11:07:44 +0900441 if (trace_boot_compose_hist_cmd(hnode, buf, size) == 0) {
442 tmp = kstrdup(buf, GFP_KERNEL);
Masami Hiramatsu17abd7c2021-08-10 11:07:36 +0900443 if (trigger_process_regex(file, buf) < 0)
Masami Hiramatsu64dc7f62021-08-10 11:07:44 +0900444 pr_err("Failed to apply hist trigger: %s\n", tmp);
445 kfree(tmp);
446 }
Masami Hiramatsu17abd7c2021-08-10 11:07:36 +0900447 }
448}
449#else
450static void __init
451trace_boot_init_histograms(struct trace_event_file *file,
452 struct xbc_node *hnode, char *buf, size_t size)
453{
454 /* do nothing */
Masami Hiramatsue66ed862021-08-10 11:07:21 +0900455}
456#endif
457
Masami Hiramatsu81a59552020-01-11 01:06:29 +0900458static void __init
459trace_boot_init_one_event(struct trace_array *tr, struct xbc_node *gnode,
460 struct xbc_node *enode)
461{
462 struct trace_event_file *file;
463 struct xbc_node *anode;
464 char buf[MAX_BUF_LEN];
465 const char *p, *group, *event;
466
467 group = xbc_node_get_data(gnode);
468 event = xbc_node_get_data(enode);
469
Masami Hiramatsu4d655282020-01-11 01:06:41 +0900470 if (!strcmp(group, "kprobes"))
471 if (trace_boot_add_kprobe_event(enode, event) < 0)
472 return;
Masami Hiramatsu3fbe2d62020-01-11 01:06:52 +0900473 if (!strcmp(group, "synthetic"))
474 if (trace_boot_add_synth_event(enode, event) < 0)
475 return;
Masami Hiramatsu4d655282020-01-11 01:06:41 +0900476
Masami Hiramatsu81a59552020-01-11 01:06:29 +0900477 mutex_lock(&event_mutex);
478 file = find_event_file(tr, group, event);
479 if (!file) {
480 pr_err("Failed to find event: %s:%s\n", group, event);
481 goto out;
482 }
483
484 p = xbc_node_find_value(enode, "filter", NULL);
485 if (p && *p != '\0') {
486 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
487 pr_err("filter string is too long: %s\n", p);
488 else if (apply_event_filter(file, buf) < 0)
489 pr_err("Failed to apply filter: %s\n", buf);
490 }
491
Masami Hiramatsu6fe7c742021-08-10 11:07:14 +0900492 if (IS_ENABLED(CONFIG_HIST_TRIGGERS)) {
493 xbc_node_for_each_array_value(enode, "actions", anode, p) {
494 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
495 pr_err("action string is too long: %s\n", p);
496 else if (trigger_process_regex(file, buf) < 0)
Masami Hiramatsu64dc7f62021-08-10 11:07:44 +0900497 pr_err("Failed to apply an action: %s\n", p);
Masami Hiramatsu6fe7c742021-08-10 11:07:14 +0900498 }
Masami Hiramatsue66ed862021-08-10 11:07:21 +0900499 anode = xbc_node_find_child(enode, "hist");
Masami Hiramatsu17abd7c2021-08-10 11:07:36 +0900500 if (anode)
501 trace_boot_init_histograms(file, anode, buf, ARRAY_SIZE(buf));
Masami Hiramatsu6fe7c742021-08-10 11:07:14 +0900502 } else if (xbc_node_find_value(enode, "actions", NULL))
503 pr_err("Failed to apply event actions because CONFIG_HIST_TRIGGERS is not set.\n");
Masami Hiramatsu81a59552020-01-11 01:06:29 +0900504
505 if (xbc_node_find_value(enode, "enable", NULL)) {
506 if (trace_event_enable_disable(file, 1, 0) < 0)
507 pr_err("Failed to enable event node: %s:%s\n",
508 group, event);
509 }
510out:
511 mutex_unlock(&event_mutex);
512}
513
514static void __init
515trace_boot_init_events(struct trace_array *tr, struct xbc_node *node)
516{
517 struct xbc_node *gnode, *enode;
Masami Hiramatsu4f99f842021-06-02 23:33:00 +0900518 bool enable, enable_all = false;
519 const char *data;
Masami Hiramatsu81a59552020-01-11 01:06:29 +0900520
521 node = xbc_node_find_child(node, "event");
522 if (!node)
523 return;
524 /* per-event key starts with "event.GROUP.EVENT" */
Masami Hiramatsu4f99f842021-06-02 23:33:00 +0900525 xbc_node_for_each_child(node, gnode) {
526 data = xbc_node_get_data(gnode);
527 if (!strcmp(data, "enable")) {
528 enable_all = true;
529 continue;
530 }
531 enable = false;
532 xbc_node_for_each_child(gnode, enode) {
533 data = xbc_node_get_data(enode);
534 if (!strcmp(data, "enable")) {
535 enable = true;
536 continue;
537 }
Masami Hiramatsu81a59552020-01-11 01:06:29 +0900538 trace_boot_init_one_event(tr, gnode, enode);
Masami Hiramatsu4f99f842021-06-02 23:33:00 +0900539 }
540 /* Event enablement must be done after event settings */
541 if (enable) {
542 data = xbc_node_get_data(gnode);
543 trace_array_set_clr_event(tr, data, NULL, true);
544 }
545 }
546 /* Ditto */
547 if (enable_all)
548 trace_array_set_clr_event(tr, NULL, NULL, true);
Masami Hiramatsu81a59552020-01-11 01:06:29 +0900549}
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +0900550#else
551#define trace_boot_enable_events(tr, node) do {} while (0)
Masami Hiramatsu81a59552020-01-11 01:06:29 +0900552#define trace_boot_init_events(tr, node) do {} while (0)
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +0900553#endif
554
Masami Hiramatsufe1efe92020-01-11 01:07:28 +0900555#ifdef CONFIG_DYNAMIC_FTRACE
Masami Hiramatsufe1efe92020-01-11 01:07:28 +0900556static void __init
557trace_boot_set_ftrace_filter(struct trace_array *tr, struct xbc_node *node)
558{
559 struct xbc_node *anode;
560 const char *p;
561 char *q;
562
563 xbc_node_for_each_array_value(node, "ftrace.filters", anode, p) {
564 q = kstrdup(p, GFP_KERNEL);
565 if (!q)
566 return;
567 if (ftrace_set_filter(tr->ops, q, strlen(q), 0) < 0)
568 pr_err("Failed to add %s to ftrace filter\n", p);
569 else
570 ftrace_filter_param = true;
571 kfree(q);
572 }
573 xbc_node_for_each_array_value(node, "ftrace.notraces", anode, p) {
574 q = kstrdup(p, GFP_KERNEL);
575 if (!q)
576 return;
577 if (ftrace_set_notrace(tr->ops, q, strlen(q), 0) < 0)
578 pr_err("Failed to add %s to ftrace filter\n", p);
579 else
580 ftrace_filter_param = true;
581 kfree(q);
582 }
583}
584#else
585#define trace_boot_set_ftrace_filter(tr, node) do {} while (0)
586#endif
587
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +0900588static void __init
589trace_boot_enable_tracer(struct trace_array *tr, struct xbc_node *node)
590{
591 const char *p;
592
Masami Hiramatsufe1efe92020-01-11 01:07:28 +0900593 trace_boot_set_ftrace_filter(tr, node);
594
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +0900595 p = xbc_node_find_value(node, "tracer", NULL);
596 if (p && *p != '\0') {
597 if (tracing_set_tracer(tr, p) < 0)
598 pr_err("Failed to set given tracer: %s\n", p);
599 }
Masami Hiramatsuc163409712020-10-11 00:28:09 +0900600
601 /* Since tracer can free snapshot buffer, allocate snapshot here.*/
602 if (xbc_node_find_value(node, "alloc_snapshot", NULL)) {
603 if (tracing_alloc_snapshot_instance(tr) < 0)
604 pr_err("Failed to allocate snapshot buffer\n");
605 }
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +0900606}
607
Masami Hiramatsu4f712a42020-01-11 01:07:04 +0900608static void __init
609trace_boot_init_one_instance(struct trace_array *tr, struct xbc_node *node)
610{
611 trace_boot_set_instance_options(tr, node);
612 trace_boot_init_events(tr, node);
613 trace_boot_enable_events(tr, node);
614 trace_boot_enable_tracer(tr, node);
615}
616
617static void __init
618trace_boot_init_instances(struct xbc_node *node)
619{
620 struct xbc_node *inode;
621 struct trace_array *tr;
622 const char *p;
623
624 node = xbc_node_find_child(node, "instance");
625 if (!node)
626 return;
627
628 xbc_node_for_each_child(node, inode) {
629 p = xbc_node_get_data(inode);
630 if (!p || *p == '\0')
631 continue;
632
633 tr = trace_array_get_by_name(p);
Dan Carpenter532f49a2020-01-17 08:30:07 +0300634 if (!tr) {
Masami Hiramatsu4f712a42020-01-11 01:07:04 +0900635 pr_err("Failed to get trace instance %s\n", p);
636 continue;
637 }
638 trace_boot_init_one_instance(tr, inode);
Steven Rostedt (VMware)28394da2020-01-24 20:47:46 -0500639 trace_array_put(tr);
Masami Hiramatsu4f712a42020-01-11 01:07:04 +0900640 }
641}
642
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +0900643static int __init trace_boot_init(void)
644{
645 struct xbc_node *trace_node;
646 struct trace_array *tr;
647
648 trace_node = xbc_find_node("ftrace");
649 if (!trace_node)
650 return 0;
651
652 tr = top_trace_array();
653 if (!tr)
654 return 0;
655
Masami Hiramatsu4f712a42020-01-11 01:07:04 +0900656 /* Global trace array is also one instance */
657 trace_boot_init_one_instance(tr, trace_node);
658 trace_boot_init_instances(trace_node);
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +0900659
Masami Hiramatsu60efe212020-12-08 17:54:09 +0900660 disable_tracing_selftest("running boot-time tracing");
661
Masami Hiramatsu9c5b9d32020-01-11 01:06:17 +0900662 return 0;
663}
Masami Hiramatsuba0fbfb2020-09-10 21:39:17 +0900664/*
665 * Start tracing at the end of core-initcall, so that it starts tracing
666 * from the beginning of postcore_initcall.
667 */
668core_initcall_sync(trace_boot_init);