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
, "trace_clock", NULL
);
44 if (p
&& *p
!= '\0') {
45 if (tracing_set_clock(tr
, p
) < 0)
46 pr_err("Failed to set trace clock: %s\n", p
);
49 p
= xbc_node_find_value(node
, "buffer_size", NULL
);
50 if (p
&& *p
!= '\0') {
51 v
= memparse(p
, NULL
);
53 pr_err("Buffer size is too small: %s\n", p
);
54 if (tracing_resize_ring_buffer(tr
, v
, RING_BUFFER_ALL_CPUS
) < 0)
55 pr_err("Failed to resize trace buffer to %s\n", p
);
58 p
= xbc_node_find_value(node
, "cpumask", NULL
);
59 if (p
&& *p
!= '\0') {
60 cpumask_var_t new_mask
;
62 if (alloc_cpumask_var(&new_mask
, GFP_KERNEL
)) {
63 if (cpumask_parse(p
, new_mask
) < 0 ||
64 tracing_set_cpumask(tr
, new_mask
) < 0)
65 pr_err("Failed to set new CPU mask %s\n", p
);
66 free_cpumask_var(new_mask
);
71 #ifdef CONFIG_EVENT_TRACING
73 trace_boot_enable_events(struct trace_array
*tr
, struct xbc_node
*node
)
75 struct xbc_node
*anode
;
76 char buf
[MAX_BUF_LEN
];
79 xbc_node_for_each_array_value(node
, "events", anode
, p
) {
80 if (strlcpy(buf
, p
, ARRAY_SIZE(buf
)) >= ARRAY_SIZE(buf
)) {
81 pr_err("String is too long: %s\n", p
);
85 if (ftrace_set_clr_event(tr
, buf
, 1) < 0)
86 pr_err("Failed to enable event: %s\n", p
);
90 #ifdef CONFIG_KPROBE_EVENTS
92 trace_boot_add_kprobe_event(struct xbc_node
*node
, const char *event
)
94 struct dynevent_cmd cmd
;
95 struct xbc_node
*anode
;
96 char buf
[MAX_BUF_LEN
];
100 kprobe_event_cmd_init(&cmd
, buf
, MAX_BUF_LEN
);
102 ret
= kprobe_event_gen_cmd_start(&cmd
, event
, NULL
);
106 xbc_node_for_each_array_value(node
, "probes", anode
, val
) {
107 ret
= kprobe_event_add_field(&cmd
, val
);
112 ret
= kprobe_event_gen_cmd_end(&cmd
);
114 pr_err("Failed to add probe: %s\n", buf
);
119 static inline int __init
120 trace_boot_add_kprobe_event(struct xbc_node
*node
, const char *event
)
122 pr_err("Kprobe event is not supported.\n");
127 #ifdef CONFIG_HIST_TRIGGERS
129 trace_boot_add_synth_event(struct xbc_node
*node
, const char *event
)
131 struct dynevent_cmd cmd
;
132 struct xbc_node
*anode
;
133 char buf
[MAX_BUF_LEN
];
137 synth_event_cmd_init(&cmd
, buf
, MAX_BUF_LEN
);
139 ret
= synth_event_gen_cmd_start(&cmd
, event
, NULL
);
143 xbc_node_for_each_array_value(node
, "fields", anode
, p
) {
144 ret
= synth_event_add_field_str(&cmd
, p
);
149 ret
= synth_event_gen_cmd_end(&cmd
);
151 pr_err("Failed to add synthetic event: %s\n", buf
);
156 static inline int __init
157 trace_boot_add_synth_event(struct xbc_node
*node
, const char *event
)
159 pr_err("Synthetic event is not supported.\n");
165 trace_boot_init_one_event(struct trace_array
*tr
, struct xbc_node
*gnode
,
166 struct xbc_node
*enode
)
168 struct trace_event_file
*file
;
169 struct xbc_node
*anode
;
170 char buf
[MAX_BUF_LEN
];
171 const char *p
, *group
, *event
;
173 group
= xbc_node_get_data(gnode
);
174 event
= xbc_node_get_data(enode
);
176 if (!strcmp(group
, "kprobes"))
177 if (trace_boot_add_kprobe_event(enode
, event
) < 0)
179 if (!strcmp(group
, "synthetic"))
180 if (trace_boot_add_synth_event(enode
, event
) < 0)
183 mutex_lock(&event_mutex
);
184 file
= find_event_file(tr
, group
, event
);
186 pr_err("Failed to find event: %s:%s\n", group
, event
);
190 p
= xbc_node_find_value(enode
, "filter", NULL
);
191 if (p
&& *p
!= '\0') {
192 if (strlcpy(buf
, p
, ARRAY_SIZE(buf
)) >= ARRAY_SIZE(buf
))
193 pr_err("filter string is too long: %s\n", p
);
194 else if (apply_event_filter(file
, buf
) < 0)
195 pr_err("Failed to apply filter: %s\n", buf
);
198 xbc_node_for_each_array_value(enode
, "actions", anode
, p
) {
199 if (strlcpy(buf
, p
, ARRAY_SIZE(buf
)) >= ARRAY_SIZE(buf
))
200 pr_err("action string is too long: %s\n", p
);
201 else if (trigger_process_regex(file
, buf
) < 0)
202 pr_err("Failed to apply an action: %s\n", buf
);
205 if (xbc_node_find_value(enode
, "enable", NULL
)) {
206 if (trace_event_enable_disable(file
, 1, 0) < 0)
207 pr_err("Failed to enable event node: %s:%s\n",
211 mutex_unlock(&event_mutex
);
215 trace_boot_init_events(struct trace_array
*tr
, struct xbc_node
*node
)
217 struct xbc_node
*gnode
, *enode
;
219 node
= xbc_node_find_child(node
, "event");
222 /* per-event key starts with "event.GROUP.EVENT" */
223 xbc_node_for_each_child(node
, gnode
)
224 xbc_node_for_each_child(gnode
, enode
)
225 trace_boot_init_one_event(tr
, gnode
, enode
);
228 #define trace_boot_enable_events(tr, node) do {} while (0)
229 #define trace_boot_init_events(tr, node) do {} while (0)
232 #ifdef CONFIG_DYNAMIC_FTRACE
234 trace_boot_set_ftrace_filter(struct trace_array
*tr
, struct xbc_node
*node
)
236 struct xbc_node
*anode
;
240 xbc_node_for_each_array_value(node
, "ftrace.filters", anode
, p
) {
241 q
= kstrdup(p
, GFP_KERNEL
);
244 if (ftrace_set_filter(tr
->ops
, q
, strlen(q
), 0) < 0)
245 pr_err("Failed to add %s to ftrace filter\n", p
);
247 ftrace_filter_param
= true;
250 xbc_node_for_each_array_value(node
, "ftrace.notraces", anode
, p
) {
251 q
= kstrdup(p
, GFP_KERNEL
);
254 if (ftrace_set_notrace(tr
->ops
, q
, strlen(q
), 0) < 0)
255 pr_err("Failed to add %s to ftrace filter\n", p
);
257 ftrace_filter_param
= true;
262 #define trace_boot_set_ftrace_filter(tr, node) do {} while (0)
266 trace_boot_enable_tracer(struct trace_array
*tr
, struct xbc_node
*node
)
270 trace_boot_set_ftrace_filter(tr
, node
);
272 p
= xbc_node_find_value(node
, "tracer", NULL
);
273 if (p
&& *p
!= '\0') {
274 if (tracing_set_tracer(tr
, p
) < 0)
275 pr_err("Failed to set given tracer: %s\n", p
);
280 trace_boot_init_one_instance(struct trace_array
*tr
, struct xbc_node
*node
)
282 trace_boot_set_instance_options(tr
, node
);
283 trace_boot_init_events(tr
, node
);
284 trace_boot_enable_events(tr
, node
);
285 trace_boot_enable_tracer(tr
, node
);
289 trace_boot_init_instances(struct xbc_node
*node
)
291 struct xbc_node
*inode
;
292 struct trace_array
*tr
;
295 node
= xbc_node_find_child(node
, "instance");
299 xbc_node_for_each_child(node
, inode
) {
300 p
= xbc_node_get_data(inode
);
301 if (!p
|| *p
== '\0')
304 tr
= trace_array_get_by_name(p
);
306 pr_err("Failed to get trace instance %s\n", p
);
309 trace_boot_init_one_instance(tr
, inode
);
314 static int __init
trace_boot_init(void)
316 struct xbc_node
*trace_node
;
317 struct trace_array
*tr
;
319 trace_node
= xbc_find_node("ftrace");
323 tr
= top_trace_array();
327 /* Global trace array is also one instance */
328 trace_boot_init_one_instance(tr
, trace_node
);
329 trace_boot_init_instances(trace_node
);
334 fs_initcall(trace_boot_init
);