]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - kernel/trace/trace_boot.c
f5db30d25b0b37b3ab29414e5c1c79137ed95564
1 // SPDX-License-Identifier: GPL-2.0
4 * Tracing kernel boot-time
7 #define pr_fmt(fmt) "trace_boot: " fmt
9 #include <linux/ftrace.h>
10 #include <linux/init.h>
11 #include <linux/bootconfig.h>
15 #define MAX_BUF_LEN 256
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
);
23 trace_boot_set_instance_options(struct trace_array
*tr
, struct xbc_node
*node
)
25 struct xbc_node
*anode
;
27 char buf
[MAX_BUF_LEN
];
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
);
37 if (trace_set_options(tr
, buf
) < 0)
38 pr_err("Failed to set option: %s\n", buf
);
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
);
47 p
= xbc_node_find_value(node
, "buffer_size", NULL
);
48 if (p
&& *p
!= '\0') {
49 v
= memparse(p
, NULL
);
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
);
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
);
62 trace_boot_enable_events(struct trace_array
*tr
, struct xbc_node
*node
)
64 struct xbc_node
*anode
;
65 char buf
[MAX_BUF_LEN
];
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
);
74 if (ftrace_set_clr_event(tr
, buf
, 1) < 0)
75 pr_err("Failed to enable event: %s\n", p
);
79 #ifdef CONFIG_KPROBE_EVENTS
80 extern int trace_kprobe_run_command(const char *command
);
83 trace_boot_add_kprobe_event(struct xbc_node
*node
, const char *event
)
85 struct xbc_node
*anode
;
86 char buf
[MAX_BUF_LEN
];
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
);
97 len
= ARRAY_SIZE(buf
) - len
;
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
);
104 if (trace_kprobe_run_command(buf
) < 0) {
105 pr_err("Failed to add probe: %s\n", buf
);
113 static inline int __init
114 trace_boot_add_kprobe_event(struct xbc_node
*node
, const char *event
)
116 pr_err("Kprobe event is not supported.\n");
121 #ifdef CONFIG_HIST_TRIGGERS
122 extern int synth_event_run_command(const char *command
);
125 trace_boot_add_synth_event(struct xbc_node
*node
, const char *event
)
127 struct xbc_node
*anode
;
128 char buf
[MAX_BUF_LEN
], *q
;
132 len
= ARRAY_SIZE(buf
);
133 delta
= snprintf(buf
, len
, "%s", event
);
135 pr_err("Event name is too long: %s\n", event
);
138 len
-= delta
; q
= buf
+ delta
;
140 xbc_node_for_each_array_value(node
, "fields", anode
, p
) {
141 delta
= snprintf(q
, len
, " %s;", p
);
143 pr_err("fields string is too long: %s\n", p
);
146 len
-= delta
; q
+= delta
;
149 ret
= synth_event_run_command(buf
);
151 pr_err("Failed to add synthetic event: %s\n", buf
);
157 static inline int __init
158 trace_boot_add_synth_event(struct xbc_node
*node
, const char *event
)
160 pr_err("Synthetic event is not supported.\n");
166 trace_boot_init_one_event(struct trace_array
*tr
, struct xbc_node
*gnode
,
167 struct xbc_node
*enode
)
169 struct trace_event_file
*file
;
170 struct xbc_node
*anode
;
171 char buf
[MAX_BUF_LEN
];
172 const char *p
, *group
, *event
;
174 group
= xbc_node_get_data(gnode
);
175 event
= xbc_node_get_data(enode
);
177 if (!strcmp(group
, "kprobes"))
178 if (trace_boot_add_kprobe_event(enode
, event
) < 0)
180 if (!strcmp(group
, "synthetic"))
181 if (trace_boot_add_synth_event(enode
, event
) < 0)
184 mutex_lock(&event_mutex
);
185 file
= find_event_file(tr
, group
, event
);
187 pr_err("Failed to find event: %s:%s\n", group
, event
);
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
);
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
);
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",
212 mutex_unlock(&event_mutex
);
216 trace_boot_init_events(struct trace_array
*tr
, struct xbc_node
*node
)
218 struct xbc_node
*gnode
, *enode
;
220 node
= xbc_node_find_child(node
, "event");
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
);
229 #define trace_boot_enable_events(tr, node) do {} while (0)
230 #define trace_boot_init_events(tr, node) do {} while (0)
234 trace_boot_enable_tracer(struct trace_array
*tr
, struct xbc_node
*node
)
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
);
246 trace_boot_init_one_instance(struct trace_array
*tr
, struct xbc_node
*node
)
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
);
255 trace_boot_init_instances(struct xbc_node
*node
)
257 struct xbc_node
*inode
;
258 struct trace_array
*tr
;
261 node
= xbc_node_find_child(node
, "instance");
265 xbc_node_for_each_child(node
, inode
) {
266 p
= xbc_node_get_data(inode
);
267 if (!p
|| *p
== '\0')
270 tr
= trace_array_get_by_name(p
);
272 pr_err("Failed to get trace instance %s\n", p
);
275 trace_boot_init_one_instance(tr
, inode
);
279 static int __init
trace_boot_init(void)
281 struct xbc_node
*trace_node
;
282 struct trace_array
*tr
;
284 trace_node
= xbc_find_node("ftrace");
288 tr
= top_trace_array();
292 /* Global trace array is also one instance */
293 trace_boot_init_one_instance(tr
, trace_node
);
294 trace_boot_init_instances(trace_node
);
299 fs_initcall(trace_boot_init
);