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 *anode;
226 p = xbc_node_find_value(hnode, key, &anode);
229 pr_err("hist.%s requires value(s).\n", key);
233 append_printf(bufp, end, ":%s", key);
235 xbc_array_for_each_value(anode, p) {
236 append_printf(bufp, end, "%c%s", sep, p);
247 trace_boot_hist_add_one_handler(struct xbc_node *hnode, char **bufp,
248 char *end, const char *handler,
251 struct xbc_node *knode, *anode;
255 /* Compose 'handler' parameter */
256 p = xbc_node_find_value(hnode, param, NULL);
258 pr_err("hist.%s requires '%s' option.\n",
259 xbc_node_get_data(hnode), param);
262 append_printf(bufp, end, ":%s(%s)", handler, p);
264 /* Compose 'action' parameter */
265 knode = xbc_node_find_subkey(hnode, "trace");
267 knode = xbc_node_find_subkey(hnode, "save");
270 anode = xbc_node_get_child(knode);
271 if (!anode || !xbc_node_is_value(anode)) {
272 pr_err("hist.%s.%s requires value(s).\n",
273 xbc_node_get_data(hnode),
274 xbc_node_get_data(knode));
278 append_printf(bufp, end, ".%s", xbc_node_get_data(knode));
280 xbc_array_for_each_value(anode, p) {
281 append_printf(bufp, end, "%c%s", sep, p);
285 append_printf(bufp, end, ")");
286 } else if (xbc_node_find_subkey(hnode, "snapshot")) {
287 append_printf(bufp, end, ".snapshot()");
289 pr_err("hist.%s requires an action.\n",
290 xbc_node_get_data(hnode));
298 trace_boot_hist_add_handlers(struct xbc_node *hnode, char **bufp,
299 char *end, const char *param)
301 struct xbc_node *node;
302 const char *p, *handler;
305 handler = xbc_node_get_data(hnode);
307 xbc_node_for_each_subkey(hnode, node) {
308 p = xbc_node_get_data(node);
311 /* All digit started node should be instances. */
312 ret = trace_boot_hist_add_one_handler(node, bufp, end, handler, param);
317 if (xbc_node_find_subkey(hnode, param))
318 ret = trace_boot_hist_add_one_handler(hnode, bufp, end, handler, param);
324 * Histogram boottime tracing syntax.
326 * ftrace.[instance.INSTANCE.]event.GROUP.EVENT.hist[.N] {
328 * values = <VAL>[,...]
329 * sort = <SORT-KEY>[,...]
332 * var { <VAR> = <EXPR> ... }
333 * pause|continue|clear
334 * onmax|onchange[.N] { var = <VAR>; <ACTION> [= <PARAM>] }
335 * onmatch[.N] { event = <EVENT>; <ACTION> [= <PARAM>] }
339 * Where <ACTION> are;
341 * trace = <EVENT>, <ARG1>[, ...]
342 * save = <ARG1>[, ...]
346 trace_boot_compose_hist_cmd(struct xbc_node *hnode, char *buf, size_t size)
348 struct xbc_node *node, *knode;
349 char *end = buf + size;
353 append_printf(&buf, end, "hist");
355 ret = trace_boot_hist_add_array(hnode, &buf, end, "keys");
358 pr_err("hist requires keys.\n");
362 ret = trace_boot_hist_add_array(hnode, &buf, end, "values");
365 ret = trace_boot_hist_add_array(hnode, &buf, end, "sort");
369 p = xbc_node_find_value(hnode, "size", NULL);
371 append_printf(&buf, end, ":size=%s", p);
373 p = xbc_node_find_value(hnode, "name", NULL);
375 append_printf(&buf, end, ":name=%s", p);
377 node = xbc_node_find_subkey(hnode, "var");
379 xbc_node_for_each_key_value(node, knode, p) {
380 /* Expression must not include spaces. */
381 append_printf(&buf, end, ":%s=",
382 xbc_node_get_data(knode));
383 append_str_nospace(&buf, end, p);
387 /* Histogram control attributes (mutual exclusive) */
388 if (xbc_node_find_value(hnode, "pause", NULL))
389 append_printf(&buf, end, ":pause");
390 else if (xbc_node_find_value(hnode, "continue", NULL))
391 append_printf(&buf, end, ":continue");
392 else if (xbc_node_find_value(hnode, "clear", NULL))
393 append_printf(&buf, end, ":clear");
395 /* Histogram handler and actions */
396 node = xbc_node_find_subkey(hnode, "onmax");
397 if (node && trace_boot_hist_add_handlers(node, &buf, end, "var") < 0)
399 node = xbc_node_find_subkey(hnode, "onchange");
400 if (node && trace_boot_hist_add_handlers(node, &buf, end, "var") < 0)
402 node = xbc_node_find_subkey(hnode, "onmatch");
403 if (node && trace_boot_hist_add_handlers(node, &buf, end, "event") < 0)
406 p = xbc_node_find_value(hnode, "filter", NULL);
408 append_printf(&buf, end, " if %s", p);
411 pr_err("hist exceeds the max command length.\n");
419 trace_boot_init_histograms(struct trace_event_file *file,
420 struct xbc_node *hnode, char *buf, size_t size)
422 struct xbc_node *node;
426 xbc_node_for_each_subkey(hnode, node) {
427 p = xbc_node_get_data(node);
430 /* All digit started node should be instances. */
431 if (trace_boot_compose_hist_cmd(node, buf, size) == 0) {
432 tmp = kstrdup(buf, GFP_KERNEL);
435 if (trigger_process_regex(file, buf) < 0)
436 pr_err("Failed to apply hist trigger: %s\n", tmp);
441 if (xbc_node_find_subkey(hnode, "keys")) {
442 if (trace_boot_compose_hist_cmd(hnode, buf, size) == 0) {
443 tmp = kstrdup(buf, GFP_KERNEL);
446 if (trigger_process_regex(file, buf) < 0)
447 pr_err("Failed to apply hist trigger: %s\n", tmp);
454 trace_boot_init_histograms(struct trace_event_file *file,
455 struct xbc_node *hnode, char *buf, size_t size)
462 trace_boot_init_one_event(struct trace_array *tr, struct xbc_node *gnode,
463 struct xbc_node *enode)
465 struct trace_event_file *file;
466 struct xbc_node *anode;
467 char buf[MAX_BUF_LEN];
468 const char *p, *group, *event;
470 group = xbc_node_get_data(gnode);
471 event = xbc_node_get_data(enode);
473 if (!strcmp(group, "kprobes"))
474 if (trace_boot_add_kprobe_event(enode, event) < 0)
476 if (!strcmp(group, "synthetic"))
477 if (trace_boot_add_synth_event(enode, event) < 0)
480 mutex_lock(&event_mutex);
481 file = find_event_file(tr, group, event);
483 pr_err("Failed to find event: %s:%s\n", group, event);
487 p = xbc_node_find_value(enode, "filter", NULL);
488 if (p && *p != '\0') {
489 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
490 pr_err("filter string is too long: %s\n", p);
491 else if (apply_event_filter(file, buf) < 0)
492 pr_err("Failed to apply filter: %s\n", buf);
495 if (IS_ENABLED(CONFIG_HIST_TRIGGERS)) {
496 xbc_node_for_each_array_value(enode, "actions", anode, p) {
497 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
498 pr_err("action string is too long: %s\n", p);
499 else if (trigger_process_regex(file, buf) < 0)
500 pr_err("Failed to apply an action: %s\n", p);
502 anode = xbc_node_find_subkey(enode, "hist");
504 trace_boot_init_histograms(file, anode, buf, ARRAY_SIZE(buf));
505 } else if (xbc_node_find_value(enode, "actions", NULL))
506 pr_err("Failed to apply event actions because CONFIG_HIST_TRIGGERS is not set.\n");
508 if (xbc_node_find_value(enode, "enable", NULL)) {
509 if (trace_event_enable_disable(file, 1, 0) < 0)
510 pr_err("Failed to enable event node: %s:%s\n",
514 mutex_unlock(&event_mutex);
518 trace_boot_init_events(struct trace_array *tr, struct xbc_node *node)
520 struct xbc_node *gnode, *enode;
521 bool enable, enable_all = false;
524 node = xbc_node_find_subkey(node, "event");
527 /* per-event key starts with "event.GROUP.EVENT" */
528 xbc_node_for_each_subkey(node, gnode) {
529 data = xbc_node_get_data(gnode);
530 if (!strcmp(data, "enable")) {
535 xbc_node_for_each_subkey(gnode, enode) {
536 data = xbc_node_get_data(enode);
537 if (!strcmp(data, "enable")) {
541 trace_boot_init_one_event(tr, gnode, enode);
543 /* Event enablement must be done after event settings */
545 data = xbc_node_get_data(gnode);
546 trace_array_set_clr_event(tr, data, NULL, true);
551 trace_array_set_clr_event(tr, NULL, NULL, true);
554 #define trace_boot_enable_events(tr, node) do {} while (0)
555 #define trace_boot_init_events(tr, node) do {} while (0)
558 #ifdef CONFIG_DYNAMIC_FTRACE
560 trace_boot_set_ftrace_filter(struct trace_array *tr, struct xbc_node *node)
562 struct xbc_node *anode;
566 xbc_node_for_each_array_value(node, "ftrace.filters", anode, p) {
567 q = kstrdup(p, GFP_KERNEL);
570 if (ftrace_set_filter(tr->ops, q, strlen(q), 0) < 0)
571 pr_err("Failed to add %s to ftrace filter\n", p);
573 ftrace_filter_param = true;
576 xbc_node_for_each_array_value(node, "ftrace.notraces", anode, p) {
577 q = kstrdup(p, GFP_KERNEL);
580 if (ftrace_set_notrace(tr->ops, q, strlen(q), 0) < 0)
581 pr_err("Failed to add %s to ftrace filter\n", p);
583 ftrace_filter_param = true;
588 #define trace_boot_set_ftrace_filter(tr, node) do {} while (0)
592 trace_boot_enable_tracer(struct trace_array *tr, struct xbc_node *node)
596 trace_boot_set_ftrace_filter(tr, node);
598 p = xbc_node_find_value(node, "tracer", NULL);
599 if (p && *p != '\0') {
600 if (tracing_set_tracer(tr, p) < 0)
601 pr_err("Failed to set given tracer: %s\n", p);
604 /* Since tracer can free snapshot buffer, allocate snapshot here.*/
605 if (xbc_node_find_value(node, "alloc_snapshot", NULL)) {
606 if (tracing_alloc_snapshot_instance(tr) < 0)
607 pr_err("Failed to allocate snapshot buffer\n");
612 trace_boot_init_one_instance(struct trace_array *tr, struct xbc_node *node)
614 trace_boot_set_instance_options(tr, node);
615 trace_boot_init_events(tr, node);
616 trace_boot_enable_events(tr, node);
617 trace_boot_enable_tracer(tr, node);
621 trace_boot_init_instances(struct xbc_node *node)
623 struct xbc_node *inode;
624 struct trace_array *tr;
627 node = xbc_node_find_subkey(node, "instance");
631 xbc_node_for_each_subkey(node, inode) {
632 p = xbc_node_get_data(inode);
633 if (!p || *p == '\0')
636 tr = trace_array_get_by_name(p);
638 pr_err("Failed to get trace instance %s\n", p);
641 trace_boot_init_one_instance(tr, inode);
646 static int __init trace_boot_init(void)
648 struct xbc_node *trace_node;
649 struct trace_array *tr;
651 trace_node = xbc_find_node("ftrace");
655 tr = top_trace_array();
659 /* Global trace array is also one instance */
660 trace_boot_init_one_instance(tr, trace_node);
661 trace_boot_init_instances(trace_node);
663 disable_tracing_selftest("running boot-time tracing");
668 * Start tracing at the end of core-initcall, so that it starts tracing
669 * from the beginning of postcore_initcall.
671 core_initcall_sync(trace_boot_init);