1 // SPDX-License-Identifier: GPL-2.0
4 * Tracing kernel boot-time
7 #define pr_fmt(fmt) "trace_boot: " fmt
9 #include <linux/bootconfig.h>
10 #include <linux/cpumask.h>
11 #include <linux/ftrace.h>
12 #include <linux/init.h>
13 #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>
22 #define MAX_BUF_LEN 256
25 trace_boot_set_instance_options(struct trace_array *tr, struct xbc_node *node)
27 struct xbc_node *anode;
29 char buf[MAX_BUF_LEN];
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);
39 if (trace_set_options(tr, buf) < 0)
40 pr_err("Failed to set option: %s\n", buf);
43 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);
48 tracer_tracing_on(tr);
50 tracer_tracing_off(tr);
53 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);
59 p = xbc_node_find_value(node, "buffer_size", NULL);
60 if (p && *p != '\0') {
61 v = memparse(p, NULL);
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);
68 p = xbc_node_find_value(node, "cpumask", NULL);
69 if (p && *p != '\0') {
70 cpumask_var_t new_mask;
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);
81 #ifdef CONFIG_EVENT_TRACING
83 trace_boot_enable_events(struct trace_array *tr, struct xbc_node *node)
85 struct xbc_node *anode;
86 char buf[MAX_BUF_LEN];
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);
95 if (ftrace_set_clr_event(tr, buf, 1) < 0)
96 pr_err("Failed to enable event: %s\n", p);
100 #ifdef CONFIG_KPROBE_EVENTS
102 trace_boot_add_kprobe_event(struct xbc_node *node, const char *event)
104 struct dynevent_cmd cmd;
105 struct xbc_node *anode;
106 char buf[MAX_BUF_LEN];
110 xbc_node_for_each_array_value(node, "probes", anode, val) {
111 kprobe_event_cmd_init(&cmd, buf, MAX_BUF_LEN);
113 ret = kprobe_event_gen_cmd_start(&cmd, event, val);
115 pr_err("Failed to generate probe: %s\n", buf);
119 ret = kprobe_event_gen_cmd_end(&cmd);
121 pr_err("Failed to add probe: %s\n", buf);
129 static inline int __init
130 trace_boot_add_kprobe_event(struct xbc_node *node, const char *event)
132 pr_err("Kprobe event is not supported.\n");
137 #ifdef CONFIG_SYNTH_EVENTS
139 trace_boot_add_synth_event(struct xbc_node *node, const char *event)
141 struct dynevent_cmd cmd;
142 struct xbc_node *anode;
143 char buf[MAX_BUF_LEN];
147 synth_event_cmd_init(&cmd, buf, MAX_BUF_LEN);
149 ret = synth_event_gen_cmd_start(&cmd, event, NULL);
153 xbc_node_for_each_array_value(node, "fields", anode, p) {
154 ret = synth_event_add_field_str(&cmd, p);
159 ret = synth_event_gen_cmd_end(&cmd);
161 pr_err("Failed to add synthetic event: %s\n", buf);
166 static inline int __init
167 trace_boot_add_synth_event(struct xbc_node *node, const char *event)
169 pr_err("Synthetic event is not supported.\n");
174 #ifdef CONFIG_HIST_TRIGGERS
175 static int __init __printf(3, 4)
176 append_printf(char **bufp, char *end, const char *fmt, ...)
185 ret = vsnprintf(*bufp, end - *bufp, fmt, args);
186 if (ret < end - *bufp) {
198 append_str_nospace(char **bufp, char *end, const char *str)
203 while (p < end - 1 && *str != '\0') {
219 trace_boot_hist_add_array(struct xbc_node *hnode, char **bufp,
220 char *end, const char *key)
222 struct xbc_node *knode, *anode;
226 knode = xbc_node_find_child(hnode, key);
228 anode = xbc_node_get_child(knode);
230 pr_err("hist.%s requires value(s).\n", key);
234 append_printf(bufp, end, ":%s", key);
236 xbc_array_for_each_value(anode, p) {
237 append_printf(bufp, end, "%c%s", sep, p);
248 trace_boot_hist_add_one_handler(struct xbc_node *hnode, char **bufp,
249 char *end, const char *handler,
252 struct xbc_node *knode, *anode;
256 /* Compose 'handler' parameter */
257 p = xbc_node_find_value(hnode, param, NULL);
259 pr_err("hist.%s requires '%s' option.\n",
260 xbc_node_get_data(hnode), param);
263 append_printf(bufp, end, ":%s(%s)", handler, p);
265 /* Compose 'action' parameter */
266 knode = xbc_node_find_child(hnode, "trace");
268 knode = xbc_node_find_child(hnode, "save");
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));
279 append_printf(bufp, end, ".%s", xbc_node_get_data(knode));
281 xbc_array_for_each_value(anode, p) {
282 append_printf(bufp, end, "%c%s", sep, p);
286 append_printf(bufp, end, ")");
287 } else if (xbc_node_find_child(hnode, "snapshot")) {
288 append_printf(bufp, end, ".snapshot()");
290 pr_err("hist.%s requires an action.\n",
291 xbc_node_get_data(hnode));
299 trace_boot_hist_add_handlers(struct xbc_node *hnode, char **bufp,
300 char *end, const char *param)
302 struct xbc_node *node;
303 const char *p, *handler;
306 handler = xbc_node_get_data(hnode);
308 xbc_node_for_each_subkey(hnode, node) {
309 p = xbc_node_get_data(node);
312 /* All digit started node should be instances. */
313 ret = trace_boot_hist_add_one_handler(node, bufp, end, handler, param);
318 if (xbc_node_find_child(hnode, param))
319 ret = trace_boot_hist_add_one_handler(hnode, bufp, end, handler, param);
325 * Histogram boottime tracing syntax.
327 * ftrace.[instance.INSTANCE.]event.GROUP.EVENT.hist {
329 * values = <VAL>[,...]
330 * sort = <SORT-KEY>[,...]
333 * var { <VAR> = <EXPR> ... }
334 * pause|continue|clear
335 * onmax|onchange[.N] { var = <VAR>; <ACTION> [= <PARAM>] }
336 * onmatch[.N] { event = <EVENT>; <ACTION> [= <PARAM>] }
340 * Where <ACTION> are;
342 * trace = <EVENT>, <ARG1>[, ...]
343 * save = <ARG1>[, ...]
347 trace_boot_compose_hist_cmd(struct xbc_node *hnode, char *buf, size_t size)
349 struct xbc_node *node, *knode;
350 char *end = buf + size;
354 append_printf(&buf, end, "hist");
356 ret = trace_boot_hist_add_array(hnode, &buf, end, "keys");
359 pr_err("hist requires keys.\n");
363 ret = trace_boot_hist_add_array(hnode, &buf, end, "values");
366 ret = trace_boot_hist_add_array(hnode, &buf, end, "sort");
370 p = xbc_node_find_value(hnode, "size", NULL);
372 append_printf(&buf, end, ":size=%s", p);
374 p = xbc_node_find_value(hnode, "name", NULL);
376 append_printf(&buf, end, ":name=%s", p);
378 node = xbc_node_find_child(hnode, "var");
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);
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");
396 /* Histogram handler and actions */
397 node = xbc_node_find_child(hnode, "onmax");
398 if (node && trace_boot_hist_add_handlers(node, &buf, end, "var") < 0)
400 node = xbc_node_find_child(hnode, "onchange");
401 if (node && trace_boot_hist_add_handlers(node, &buf, end, "var") < 0)
403 node = xbc_node_find_child(hnode, "onmatch");
404 if (node && trace_boot_hist_add_handlers(node, &buf, end, "event") < 0)
407 p = xbc_node_find_value(hnode, "filter", NULL);
409 append_printf(&buf, end, " if %s", p);
412 pr_err("hist exceeds the max command length.\n");
420 trace_boot_compose_hist_cmd(struct xbc_node *hnode, char *buf, size_t size)
427 trace_boot_init_one_event(struct trace_array *tr, struct xbc_node *gnode,
428 struct xbc_node *enode)
430 struct trace_event_file *file;
431 struct xbc_node *anode;
432 char buf[MAX_BUF_LEN];
433 const char *p, *group, *event;
435 group = xbc_node_get_data(gnode);
436 event = xbc_node_get_data(enode);
438 if (!strcmp(group, "kprobes"))
439 if (trace_boot_add_kprobe_event(enode, event) < 0)
441 if (!strcmp(group, "synthetic"))
442 if (trace_boot_add_synth_event(enode, event) < 0)
445 mutex_lock(&event_mutex);
446 file = find_event_file(tr, group, event);
448 pr_err("Failed to find event: %s:%s\n", group, event);
452 p = xbc_node_find_value(enode, "filter", NULL);
453 if (p && *p != '\0') {
454 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
455 pr_err("filter string is too long: %s\n", p);
456 else if (apply_event_filter(file, buf) < 0)
457 pr_err("Failed to apply filter: %s\n", buf);
460 if (IS_ENABLED(CONFIG_HIST_TRIGGERS)) {
461 xbc_node_for_each_array_value(enode, "actions", anode, p) {
462 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
463 pr_err("action string is too long: %s\n", p);
464 else if (trigger_process_regex(file, buf) < 0)
465 pr_err("Failed to apply an action: %s\n", buf);
467 anode = xbc_node_find_child(enode, "hist");
469 trace_boot_compose_hist_cmd(anode, buf, ARRAY_SIZE(buf)) == 0) {
470 if (trigger_process_regex(file, buf) < 0)
471 pr_err("Failed to apply hist trigger: %s\n", buf);
473 } else if (xbc_node_find_value(enode, "actions", NULL))
474 pr_err("Failed to apply event actions because CONFIG_HIST_TRIGGERS is not set.\n");
476 if (xbc_node_find_value(enode, "enable", NULL)) {
477 if (trace_event_enable_disable(file, 1, 0) < 0)
478 pr_err("Failed to enable event node: %s:%s\n",
482 mutex_unlock(&event_mutex);
486 trace_boot_init_events(struct trace_array *tr, struct xbc_node *node)
488 struct xbc_node *gnode, *enode;
489 bool enable, enable_all = false;
492 node = xbc_node_find_child(node, "event");
495 /* per-event key starts with "event.GROUP.EVENT" */
496 xbc_node_for_each_child(node, gnode) {
497 data = xbc_node_get_data(gnode);
498 if (!strcmp(data, "enable")) {
503 xbc_node_for_each_child(gnode, enode) {
504 data = xbc_node_get_data(enode);
505 if (!strcmp(data, "enable")) {
509 trace_boot_init_one_event(tr, gnode, enode);
511 /* Event enablement must be done after event settings */
513 data = xbc_node_get_data(gnode);
514 trace_array_set_clr_event(tr, data, NULL, true);
519 trace_array_set_clr_event(tr, NULL, NULL, true);
522 #define trace_boot_enable_events(tr, node) do {} while (0)
523 #define trace_boot_init_events(tr, node) do {} while (0)
526 #ifdef CONFIG_DYNAMIC_FTRACE
528 trace_boot_set_ftrace_filter(struct trace_array *tr, struct xbc_node *node)
530 struct xbc_node *anode;
534 xbc_node_for_each_array_value(node, "ftrace.filters", anode, p) {
535 q = kstrdup(p, GFP_KERNEL);
538 if (ftrace_set_filter(tr->ops, q, strlen(q), 0) < 0)
539 pr_err("Failed to add %s to ftrace filter\n", p);
541 ftrace_filter_param = true;
544 xbc_node_for_each_array_value(node, "ftrace.notraces", anode, p) {
545 q = kstrdup(p, GFP_KERNEL);
548 if (ftrace_set_notrace(tr->ops, q, strlen(q), 0) < 0)
549 pr_err("Failed to add %s to ftrace filter\n", p);
551 ftrace_filter_param = true;
556 #define trace_boot_set_ftrace_filter(tr, node) do {} while (0)
560 trace_boot_enable_tracer(struct trace_array *tr, struct xbc_node *node)
564 trace_boot_set_ftrace_filter(tr, node);
566 p = xbc_node_find_value(node, "tracer", NULL);
567 if (p && *p != '\0') {
568 if (tracing_set_tracer(tr, p) < 0)
569 pr_err("Failed to set given tracer: %s\n", p);
572 /* Since tracer can free snapshot buffer, allocate snapshot here.*/
573 if (xbc_node_find_value(node, "alloc_snapshot", NULL)) {
574 if (tracing_alloc_snapshot_instance(tr) < 0)
575 pr_err("Failed to allocate snapshot buffer\n");
580 trace_boot_init_one_instance(struct trace_array *tr, struct xbc_node *node)
582 trace_boot_set_instance_options(tr, node);
583 trace_boot_init_events(tr, node);
584 trace_boot_enable_events(tr, node);
585 trace_boot_enable_tracer(tr, node);
589 trace_boot_init_instances(struct xbc_node *node)
591 struct xbc_node *inode;
592 struct trace_array *tr;
595 node = xbc_node_find_child(node, "instance");
599 xbc_node_for_each_child(node, inode) {
600 p = xbc_node_get_data(inode);
601 if (!p || *p == '\0')
604 tr = trace_array_get_by_name(p);
606 pr_err("Failed to get trace instance %s\n", p);
609 trace_boot_init_one_instance(tr, inode);
614 static int __init trace_boot_init(void)
616 struct xbc_node *trace_node;
617 struct trace_array *tr;
619 trace_node = xbc_find_node("ftrace");
623 tr = top_trace_array();
627 /* Global trace array is also one instance */
628 trace_boot_init_one_instance(tr, trace_node);
629 trace_boot_init_instances(trace_node);
631 disable_tracing_selftest("running boot-time tracing");
636 * Start tracing at the end of core-initcall, so that it starts tracing
637 * from the beginning of postcore_initcall.
639 core_initcall_sync(trace_boot_init);