]> asedeno.scripts.mit.edu Git - linux.git/blob - kernel/trace/trace_boot.c
tracing/boot: Include required headers and sort it alphabetically
[linux.git] / kernel / trace / trace_boot.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * trace_boot.c
4  * Tracing kernel boot-time
5  */
6
7 #define pr_fmt(fmt)     "trace_boot: " fmt
8
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>
19
20 #include "trace.h"
21
22 #define MAX_BUF_LEN 256
23
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);
30
31 static void __init
32 trace_boot_set_instance_options(struct trace_array *tr, struct xbc_node *node)
33 {
34         struct xbc_node *anode;
35         const char *p;
36         char buf[MAX_BUF_LEN];
37         unsigned long v = 0;
38
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);
43                         continue;
44                 }
45
46                 if (trace_set_options(tr, buf) < 0)
47                         pr_err("Failed to set option: %s\n", buf);
48         }
49
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);
54         }
55
56         p = xbc_node_find_value(node, "buffer_size", NULL);
57         if (p && *p != '\0') {
58                 v = memparse(p, NULL);
59                 if (v < PAGE_SIZE)
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);
63         }
64
65         p = xbc_node_find_value(node, "cpumask", NULL);
66         if (p && *p != '\0') {
67                 cpumask_var_t new_mask;
68
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);
74                 }
75         }
76 }
77
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);
81
82 static void __init
83 trace_boot_enable_events(struct trace_array *tr, struct xbc_node *node)
84 {
85         struct xbc_node *anode;
86         char buf[MAX_BUF_LEN];
87         const char *p;
88
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);
92                         continue;
93                 }
94
95                 if (ftrace_set_clr_event(tr, buf, 1) < 0)
96                         pr_err("Failed to enable event: %s\n", p);
97         }
98 }
99
100 #ifdef CONFIG_KPROBE_EVENTS
101 extern int trace_kprobe_run_command(const char *command);
102
103 static int __init
104 trace_boot_add_kprobe_event(struct xbc_node *node, const char *event)
105 {
106         struct xbc_node *anode;
107         char buf[MAX_BUF_LEN];
108         const char *val;
109         char *p;
110         int len;
111
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);
115                 return -E2BIG;
116         }
117         p = buf + len;
118         len = ARRAY_SIZE(buf) - len;
119
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);
123                         return -E2BIG;
124                 }
125                 if (trace_kprobe_run_command(buf) < 0) {
126                         pr_err("Failed to add probe: %s\n", buf);
127                         return -EINVAL;
128                 }
129         }
130
131         return 0;
132 }
133 #else
134 static inline int __init
135 trace_boot_add_kprobe_event(struct xbc_node *node, const char *event)
136 {
137         pr_err("Kprobe event is not supported.\n");
138         return -ENOTSUPP;
139 }
140 #endif
141
142 #ifdef CONFIG_HIST_TRIGGERS
143 extern int synth_event_run_command(const char *command);
144
145 static int __init
146 trace_boot_add_synth_event(struct xbc_node *node, const char *event)
147 {
148         struct xbc_node *anode;
149         char buf[MAX_BUF_LEN], *q;
150         const char *p;
151         int len, delta, ret;
152
153         len = ARRAY_SIZE(buf);
154         delta = snprintf(buf, len, "%s", event);
155         if (delta >= len) {
156                 pr_err("Event name is too long: %s\n", event);
157                 return -E2BIG;
158         }
159         len -= delta; q = buf + delta;
160
161         xbc_node_for_each_array_value(node, "fields", anode, p) {
162                 delta = snprintf(q, len, " %s;", p);
163                 if (delta >= len) {
164                         pr_err("fields string is too long: %s\n", p);
165                         return -E2BIG;
166                 }
167                 len -= delta; q += delta;
168         }
169
170         ret = synth_event_run_command(buf);
171         if (ret < 0)
172                 pr_err("Failed to add synthetic event: %s\n", buf);
173
174
175         return ret;
176 }
177 #else
178 static inline int __init
179 trace_boot_add_synth_event(struct xbc_node *node, const char *event)
180 {
181         pr_err("Synthetic event is not supported.\n");
182         return -ENOTSUPP;
183 }
184 #endif
185
186 static void __init
187 trace_boot_init_one_event(struct trace_array *tr, struct xbc_node *gnode,
188                           struct xbc_node *enode)
189 {
190         struct trace_event_file *file;
191         struct xbc_node *anode;
192         char buf[MAX_BUF_LEN];
193         const char *p, *group, *event;
194
195         group = xbc_node_get_data(gnode);
196         event = xbc_node_get_data(enode);
197
198         if (!strcmp(group, "kprobes"))
199                 if (trace_boot_add_kprobe_event(enode, event) < 0)
200                         return;
201         if (!strcmp(group, "synthetic"))
202                 if (trace_boot_add_synth_event(enode, event) < 0)
203                         return;
204
205         mutex_lock(&event_mutex);
206         file = find_event_file(tr, group, event);
207         if (!file) {
208                 pr_err("Failed to find event: %s:%s\n", group, event);
209                 goto out;
210         }
211
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);
218         }
219
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);
225         }
226
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",
230                                 group, event);
231         }
232 out:
233         mutex_unlock(&event_mutex);
234 }
235
236 static void __init
237 trace_boot_init_events(struct trace_array *tr, struct xbc_node *node)
238 {
239         struct xbc_node *gnode, *enode;
240
241         node = xbc_node_find_child(node, "event");
242         if (!node)
243                 return;
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);
248 }
249 #else
250 #define trace_boot_enable_events(tr, node) do {} while (0)
251 #define trace_boot_init_events(tr, node) do {} while (0)
252 #endif
253
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,
257                              int len, int reset);
258 extern int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
259                               int len, int reset);
260 static void __init
261 trace_boot_set_ftrace_filter(struct trace_array *tr, struct xbc_node *node)
262 {
263         struct xbc_node *anode;
264         const char *p;
265         char *q;
266
267         xbc_node_for_each_array_value(node, "ftrace.filters", anode, p) {
268                 q = kstrdup(p, GFP_KERNEL);
269                 if (!q)
270                         return;
271                 if (ftrace_set_filter(tr->ops, q, strlen(q), 0) < 0)
272                         pr_err("Failed to add %s to ftrace filter\n", p);
273                 else
274                         ftrace_filter_param = true;
275                 kfree(q);
276         }
277         xbc_node_for_each_array_value(node, "ftrace.notraces", anode, p) {
278                 q = kstrdup(p, GFP_KERNEL);
279                 if (!q)
280                         return;
281                 if (ftrace_set_notrace(tr->ops, q, strlen(q), 0) < 0)
282                         pr_err("Failed to add %s to ftrace filter\n", p);
283                 else
284                         ftrace_filter_param = true;
285                 kfree(q);
286         }
287 }
288 #else
289 #define trace_boot_set_ftrace_filter(tr, node) do {} while (0)
290 #endif
291
292 static void __init
293 trace_boot_enable_tracer(struct trace_array *tr, struct xbc_node *node)
294 {
295         const char *p;
296
297         trace_boot_set_ftrace_filter(tr, node);
298
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);
303         }
304 }
305
306 static void __init
307 trace_boot_init_one_instance(struct trace_array *tr, struct xbc_node *node)
308 {
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);
313 }
314
315 static void __init
316 trace_boot_init_instances(struct xbc_node *node)
317 {
318         struct xbc_node *inode;
319         struct trace_array *tr;
320         const char *p;
321
322         node = xbc_node_find_child(node, "instance");
323         if (!node)
324                 return;
325
326         xbc_node_for_each_child(node, inode) {
327                 p = xbc_node_get_data(inode);
328                 if (!p || *p == '\0')
329                         continue;
330
331                 tr = trace_array_get_by_name(p);
332                 if (!tr) {
333                         pr_err("Failed to get trace instance %s\n", p);
334                         continue;
335                 }
336                 trace_boot_init_one_instance(tr, inode);
337                 trace_array_put(tr);
338         }
339 }
340
341 static int __init trace_boot_init(void)
342 {
343         struct xbc_node *trace_node;
344         struct trace_array *tr;
345
346         trace_node = xbc_find_node("ftrace");
347         if (!trace_node)
348                 return 0;
349
350         tr = top_trace_array();
351         if (!tr)
352                 return 0;
353
354         /* Global trace array is also one instance */
355         trace_boot_init_one_instance(tr, trace_node);
356         trace_boot_init_instances(trace_node);
357
358         return 0;
359 }
360
361 fs_initcall(trace_boot_init);