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
24 extern int trace_set_options(struct trace_array *tr, char *option);
25 extern int tracing_set_tracer(struct trace_array *tr, const char *buf);
26 extern ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
27 unsigned long size, int cpu_id);
28 extern int tracing_set_cpumask(struct trace_array *tr,
29 cpumask_var_t tracing_cpumask_new);
32 trace_boot_set_instance_options(struct trace_array *tr, struct xbc_node *node)
34 struct xbc_node *anode;
36 char buf[MAX_BUF_LEN];
39 /* Common ftrace options */
40 xbc_node_for_each_array_value(node, "options", anode, p) {
41 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) {
42 pr_err("String is too long: %s\n", p);
46 if (trace_set_options(tr, buf) < 0)
47 pr_err("Failed to set option: %s\n", buf);
50 p = xbc_node_find_value(node, "trace_clock", NULL);
51 if (p && *p != '\0') {
52 if (tracing_set_clock(tr, p) < 0)
53 pr_err("Failed to set trace clock: %s\n", p);
56 p = xbc_node_find_value(node, "buffer_size", NULL);
57 if (p && *p != '\0') {
58 v = memparse(p, NULL);
60 pr_err("Buffer size is too small: %s\n", p);
61 if (tracing_resize_ring_buffer(tr, v, RING_BUFFER_ALL_CPUS) < 0)
62 pr_err("Failed to resize trace buffer to %s\n", p);
65 p = xbc_node_find_value(node, "cpumask", NULL);
66 if (p && *p != '\0') {
67 cpumask_var_t new_mask;
69 if (alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
70 if (cpumask_parse(p, new_mask) < 0 ||
71 tracing_set_cpumask(tr, new_mask) < 0)
72 pr_err("Failed to set new CPU mask %s\n", p);
73 free_cpumask_var(new_mask);
78 #ifdef CONFIG_EVENT_TRACING
79 extern int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set);
80 extern int trigger_process_regex(struct trace_event_file *file, char *buff);
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
101 extern int trace_kprobe_run_command(const char *command);
104 trace_boot_add_kprobe_event(struct xbc_node *node, const char *event)
106 struct xbc_node *anode;
107 char buf[MAX_BUF_LEN];
112 len = snprintf(buf, ARRAY_SIZE(buf) - 1, "p:kprobes/%s ", event);
113 if (len >= ARRAY_SIZE(buf)) {
114 pr_err("Event name is too long: %s\n", event);
118 len = ARRAY_SIZE(buf) - len;
120 xbc_node_for_each_array_value(node, "probes", anode, val) {
121 if (strlcpy(p, val, len) >= len) {
122 pr_err("Probe definition is too long: %s\n", val);
125 if (trace_kprobe_run_command(buf) < 0) {
126 pr_err("Failed to add probe: %s\n", buf);
134 static inline int __init
135 trace_boot_add_kprobe_event(struct xbc_node *node, const char *event)
137 pr_err("Kprobe event is not supported.\n");
142 #ifdef CONFIG_HIST_TRIGGERS
143 extern int synth_event_run_command(const char *command);
146 trace_boot_add_synth_event(struct xbc_node *node, const char *event)
148 struct xbc_node *anode;
149 char buf[MAX_BUF_LEN], *q;
153 len = ARRAY_SIZE(buf);
154 delta = snprintf(buf, len, "%s", event);
156 pr_err("Event name is too long: %s\n", event);
159 len -= delta; q = buf + delta;
161 xbc_node_for_each_array_value(node, "fields", anode, p) {
162 delta = snprintf(q, len, " %s;", p);
164 pr_err("fields string is too long: %s\n", p);
167 len -= delta; q += delta;
170 ret = synth_event_run_command(buf);
172 pr_err("Failed to add synthetic event: %s\n", buf);
178 static inline int __init
179 trace_boot_add_synth_event(struct xbc_node *node, const char *event)
181 pr_err("Synthetic event is not supported.\n");
187 trace_boot_init_one_event(struct trace_array *tr, struct xbc_node *gnode,
188 struct xbc_node *enode)
190 struct trace_event_file *file;
191 struct xbc_node *anode;
192 char buf[MAX_BUF_LEN];
193 const char *p, *group, *event;
195 group = xbc_node_get_data(gnode);
196 event = xbc_node_get_data(enode);
198 if (!strcmp(group, "kprobes"))
199 if (trace_boot_add_kprobe_event(enode, event) < 0)
201 if (!strcmp(group, "synthetic"))
202 if (trace_boot_add_synth_event(enode, event) < 0)
205 mutex_lock(&event_mutex);
206 file = find_event_file(tr, group, event);
208 pr_err("Failed to find event: %s:%s\n", group, event);
212 p = xbc_node_find_value(enode, "filter", NULL);
213 if (p && *p != '\0') {
214 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
215 pr_err("filter string is too long: %s\n", p);
216 else if (apply_event_filter(file, buf) < 0)
217 pr_err("Failed to apply filter: %s\n", buf);
220 xbc_node_for_each_array_value(enode, "actions", anode, p) {
221 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
222 pr_err("action string is too long: %s\n", p);
223 else if (trigger_process_regex(file, buf) < 0)
224 pr_err("Failed to apply an action: %s\n", buf);
227 if (xbc_node_find_value(enode, "enable", NULL)) {
228 if (trace_event_enable_disable(file, 1, 0) < 0)
229 pr_err("Failed to enable event node: %s:%s\n",
233 mutex_unlock(&event_mutex);
237 trace_boot_init_events(struct trace_array *tr, struct xbc_node *node)
239 struct xbc_node *gnode, *enode;
241 node = xbc_node_find_child(node, "event");
244 /* per-event key starts with "event.GROUP.EVENT" */
245 xbc_node_for_each_child(node, gnode)
246 xbc_node_for_each_child(gnode, enode)
247 trace_boot_init_one_event(tr, gnode, enode);
250 #define trace_boot_enable_events(tr, node) do {} while (0)
251 #define trace_boot_init_events(tr, node) do {} while (0)
254 #ifdef CONFIG_DYNAMIC_FTRACE
255 extern bool ftrace_filter_param __initdata;
256 extern int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
258 extern int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
261 trace_boot_set_ftrace_filter(struct trace_array *tr, struct xbc_node *node)
263 struct xbc_node *anode;
267 xbc_node_for_each_array_value(node, "ftrace.filters", anode, p) {
268 q = kstrdup(p, GFP_KERNEL);
271 if (ftrace_set_filter(tr->ops, q, strlen(q), 0) < 0)
272 pr_err("Failed to add %s to ftrace filter\n", p);
274 ftrace_filter_param = true;
277 xbc_node_for_each_array_value(node, "ftrace.notraces", anode, p) {
278 q = kstrdup(p, GFP_KERNEL);
281 if (ftrace_set_notrace(tr->ops, q, strlen(q), 0) < 0)
282 pr_err("Failed to add %s to ftrace filter\n", p);
284 ftrace_filter_param = true;
289 #define trace_boot_set_ftrace_filter(tr, node) do {} while (0)
293 trace_boot_enable_tracer(struct trace_array *tr, struct xbc_node *node)
297 trace_boot_set_ftrace_filter(tr, node);
299 p = xbc_node_find_value(node, "tracer", NULL);
300 if (p && *p != '\0') {
301 if (tracing_set_tracer(tr, p) < 0)
302 pr_err("Failed to set given tracer: %s\n", p);
307 trace_boot_init_one_instance(struct trace_array *tr, struct xbc_node *node)
309 trace_boot_set_instance_options(tr, node);
310 trace_boot_init_events(tr, node);
311 trace_boot_enable_events(tr, node);
312 trace_boot_enable_tracer(tr, node);
316 trace_boot_init_instances(struct xbc_node *node)
318 struct xbc_node *inode;
319 struct trace_array *tr;
322 node = xbc_node_find_child(node, "instance");
326 xbc_node_for_each_child(node, inode) {
327 p = xbc_node_get_data(inode);
328 if (!p || *p == '\0')
331 tr = trace_array_get_by_name(p);
333 pr_err("Failed to get trace instance %s\n", p);
336 trace_boot_init_one_instance(tr, inode);
341 static int __init trace_boot_init(void)
343 struct xbc_node *trace_node;
344 struct trace_array *tr;
346 trace_node = xbc_find_node("ftrace");
350 tr = top_trace_array();
354 /* Global trace array is also one instance */
355 trace_boot_init_one_instance(tr, trace_node);
356 trace_boot_init_instances(trace_node);
361 fs_initcall(trace_boot_init);