]>
Commit | Line | Data |
---|---|---|
bcea3f96 | 1 | // SPDX-License-Identifier: GPL-2.0 |
8ab83f56 SD |
2 | /* |
3 | * Common code for probe-based Dynamic events. | |
4 | * | |
8ab83f56 SD |
5 | * This code was copied from kernel/trace/trace_kprobe.c written by |
6 | * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> | |
7 | * | |
8 | * Updates to make this generic: | |
9 | * Copyright (C) IBM Corporation, 2010-2011 | |
10 | * Author: Srikar Dronamraju | |
11 | */ | |
72576341 | 12 | #define pr_fmt(fmt) "trace_probe: " fmt |
8ab83f56 SD |
13 | |
14 | #include "trace_probe.h" | |
15 | ||
ab105a4f MH |
16 | #undef C |
17 | #define C(a, b) b | |
18 | ||
19 | static const char *trace_probe_err_text[] = { ERRORS }; | |
20 | ||
08416252 | 21 | static const char *reserved_field_names[] = { |
8ab83f56 SD |
22 | "common_type", |
23 | "common_flags", | |
24 | "common_preempt_count", | |
25 | "common_pid", | |
26 | "common_tgid", | |
27 | FIELD_STRING_IP, | |
28 | FIELD_STRING_RETIP, | |
29 | FIELD_STRING_FUNC, | |
30 | }; | |
31 | ||
8ab83f56 | 32 | /* Printing in basic type function template */ |
17ce3dc7 | 33 | #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \ |
56de7630 | 34 | int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\ |
8ab83f56 | 35 | { \ |
56de7630 | 36 | trace_seq_printf(s, fmt, *(type *)data); \ |
d2b0191a | 37 | return !trace_seq_has_overflowed(s); \ |
8ab83f56 | 38 | } \ |
7bfbc63e | 39 | const char PRINT_TYPE_FMT_NAME(tname)[] = fmt; |
8ab83f56 | 40 | |
bdca79c2 MH |
41 | DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u") |
42 | DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u") | |
43 | DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u") | |
44 | DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu") | |
17ce3dc7 MH |
45 | DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d") |
46 | DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d") | |
47 | DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d") | |
48 | DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld") | |
49 | DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x") | |
50 | DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x") | |
51 | DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x") | |
52 | DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx") | |
8ab83f56 | 53 | |
60c2e0ce MH |
54 | int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent) |
55 | { | |
56 | trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data); | |
57 | return !trace_seq_has_overflowed(s); | |
58 | } | |
59 | const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS"; | |
60 | ||
8ab83f56 | 61 | /* Print type function for string type */ |
56de7630 | 62 | int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent) |
8ab83f56 SD |
63 | { |
64 | int len = *(u32 *)data >> 16; | |
65 | ||
66 | if (!len) | |
56de7630 | 67 | trace_seq_puts(s, "(fault)"); |
8ab83f56 | 68 | else |
56de7630 | 69 | trace_seq_printf(s, "\"%s\"", |
d2b0191a SRRH |
70 | (const char *)get_loc_data(data, ent)); |
71 | return !trace_seq_has_overflowed(s); | |
8ab83f56 SD |
72 | } |
73 | ||
b26c74e1 | 74 | const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\""; |
8ab83f56 | 75 | |
f451bc89 MH |
76 | /* Fetch type information table */ |
77 | static const struct fetch_type probe_fetch_types[] = { | |
78 | /* Special types */ | |
79 | __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, | |
80 | "__data_loc char[]"), | |
88903c46 MH |
81 | __ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, |
82 | "__data_loc char[]"), | |
f451bc89 MH |
83 | /* Basic types */ |
84 | ASSIGN_FETCH_TYPE(u8, u8, 0), | |
85 | ASSIGN_FETCH_TYPE(u16, u16, 0), | |
86 | ASSIGN_FETCH_TYPE(u32, u32, 0), | |
87 | ASSIGN_FETCH_TYPE(u64, u64, 0), | |
88 | ASSIGN_FETCH_TYPE(s8, u8, 1), | |
89 | ASSIGN_FETCH_TYPE(s16, u16, 1), | |
90 | ASSIGN_FETCH_TYPE(s32, u32, 1), | |
91 | ASSIGN_FETCH_TYPE(s64, u64, 1), | |
92 | ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0), | |
93 | ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0), | |
94 | ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0), | |
95 | ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0), | |
60c2e0ce | 96 | ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0), |
f451bc89 MH |
97 | |
98 | ASSIGN_FETCH_TYPE_END | |
99 | }; | |
100 | ||
101 | static const struct fetch_type *find_fetch_type(const char *type) | |
8ab83f56 SD |
102 | { |
103 | int i; | |
104 | ||
105 | if (!type) | |
106 | type = DEFAULT_FETCH_TYPE_STR; | |
107 | ||
108 | /* Special case: bitfield */ | |
109 | if (*type == 'b') { | |
110 | unsigned long bs; | |
111 | ||
112 | type = strchr(type, '/'); | |
113 | if (!type) | |
114 | goto fail; | |
115 | ||
116 | type++; | |
bcd83ea6 | 117 | if (kstrtoul(type, 0, &bs)) |
8ab83f56 SD |
118 | goto fail; |
119 | ||
120 | switch (bs) { | |
121 | case 8: | |
f451bc89 | 122 | return find_fetch_type("u8"); |
8ab83f56 | 123 | case 16: |
f451bc89 | 124 | return find_fetch_type("u16"); |
8ab83f56 | 125 | case 32: |
f451bc89 | 126 | return find_fetch_type("u32"); |
8ab83f56 | 127 | case 64: |
f451bc89 | 128 | return find_fetch_type("u64"); |
8ab83f56 SD |
129 | default: |
130 | goto fail; | |
131 | } | |
132 | } | |
133 | ||
f451bc89 MH |
134 | for (i = 0; probe_fetch_types[i].name; i++) { |
135 | if (strcmp(type, probe_fetch_types[i].name) == 0) | |
136 | return &probe_fetch_types[i]; | |
34fee3a1 | 137 | } |
8ab83f56 SD |
138 | |
139 | fail: | |
140 | return NULL; | |
141 | } | |
142 | ||
ab105a4f MH |
143 | static struct trace_probe_log trace_probe_log; |
144 | ||
145 | void trace_probe_log_init(const char *subsystem, int argc, const char **argv) | |
146 | { | |
147 | trace_probe_log.subsystem = subsystem; | |
148 | trace_probe_log.argc = argc; | |
149 | trace_probe_log.argv = argv; | |
150 | trace_probe_log.index = 0; | |
151 | } | |
152 | ||
153 | void trace_probe_log_clear(void) | |
154 | { | |
155 | memset(&trace_probe_log, 0, sizeof(trace_probe_log)); | |
156 | } | |
157 | ||
158 | void trace_probe_log_set_index(int index) | |
159 | { | |
160 | trace_probe_log.index = index; | |
161 | } | |
162 | ||
163 | void __trace_probe_log_err(int offset, int err_type) | |
164 | { | |
165 | char *command, *p; | |
166 | int i, len = 0, pos = 0; | |
167 | ||
168 | if (!trace_probe_log.argv) | |
169 | return; | |
170 | ||
f2cc020d | 171 | /* Recalculate the length and allocate buffer */ |
ab105a4f MH |
172 | for (i = 0; i < trace_probe_log.argc; i++) { |
173 | if (i == trace_probe_log.index) | |
174 | pos = len; | |
175 | len += strlen(trace_probe_log.argv[i]) + 1; | |
176 | } | |
177 | command = kzalloc(len, GFP_KERNEL); | |
178 | if (!command) | |
179 | return; | |
180 | ||
d2aea95a MH |
181 | if (trace_probe_log.index >= trace_probe_log.argc) { |
182 | /** | |
183 | * Set the error position is next to the last arg + space. | |
184 | * Note that len includes the terminal null and the cursor | |
f2cc020d | 185 | * appears at pos + 1. |
d2aea95a MH |
186 | */ |
187 | pos = len; | |
188 | offset = 0; | |
189 | } | |
190 | ||
ab105a4f MH |
191 | /* And make a command string from argv array */ |
192 | p = command; | |
193 | for (i = 0; i < trace_probe_log.argc; i++) { | |
194 | len = strlen(trace_probe_log.argv[i]); | |
195 | strcpy(p, trace_probe_log.argv[i]); | |
196 | p[len] = ' '; | |
197 | p += len + 1; | |
198 | } | |
199 | *(p - 1) = '\0'; | |
200 | ||
2f754e77 | 201 | tracing_log_err(NULL, trace_probe_log.subsystem, command, |
ab105a4f MH |
202 | trace_probe_err_text, err_type, pos + offset); |
203 | ||
204 | kfree(command); | |
205 | } | |
206 | ||
8ab83f56 | 207 | /* Split symbol and offset. */ |
c5d343b6 | 208 | int traceprobe_split_symbol_offset(char *symbol, long *offset) |
8ab83f56 SD |
209 | { |
210 | char *tmp; | |
211 | int ret; | |
212 | ||
213 | if (!offset) | |
214 | return -EINVAL; | |
215 | ||
c5d343b6 | 216 | tmp = strpbrk(symbol, "+-"); |
8ab83f56 | 217 | if (tmp) { |
c5d343b6 | 218 | ret = kstrtol(tmp, 0, offset); |
8ab83f56 SD |
219 | if (ret) |
220 | return ret; | |
8ab83f56 SD |
221 | *tmp = '\0'; |
222 | } else | |
223 | *offset = 0; | |
224 | ||
225 | return 0; | |
226 | } | |
227 | ||
6212dd29 MH |
228 | /* @buf must has MAX_EVENT_NAME_LEN size */ |
229 | int traceprobe_parse_event_name(const char **pevent, const char **pgroup, | |
ab105a4f | 230 | char *buf, int offset) |
6212dd29 MH |
231 | { |
232 | const char *slash, *event = *pevent; | |
dec65d79 | 233 | int len; |
6212dd29 MH |
234 | |
235 | slash = strchr(event, '/'); | |
bc1b9734 SRV |
236 | if (!slash) |
237 | slash = strchr(event, '.'); | |
238 | ||
6212dd29 MH |
239 | if (slash) { |
240 | if (slash == event) { | |
ab105a4f | 241 | trace_probe_log_err(offset, NO_GROUP_NAME); |
6212dd29 MH |
242 | return -EINVAL; |
243 | } | |
244 | if (slash - event + 1 > MAX_EVENT_NAME_LEN) { | |
ab105a4f MH |
245 | trace_probe_log_err(offset, GROUP_TOO_LONG); |
246 | return -EINVAL; | |
6212dd29 MH |
247 | } |
248 | strlcpy(buf, event, slash - event + 1); | |
5b7a9622 | 249 | if (!is_good_name(buf)) { |
ab105a4f | 250 | trace_probe_log_err(offset, BAD_GROUP_NAME); |
5b7a9622 MH |
251 | return -EINVAL; |
252 | } | |
6212dd29 MH |
253 | *pgroup = buf; |
254 | *pevent = slash + 1; | |
ab105a4f | 255 | offset += slash - event + 1; |
dec65d79 | 256 | event = *pevent; |
6212dd29 | 257 | } |
dec65d79 MH |
258 | len = strlen(event); |
259 | if (len == 0) { | |
ab105a4f | 260 | trace_probe_log_err(offset, NO_EVENT_NAME); |
6212dd29 | 261 | return -EINVAL; |
dec65d79 | 262 | } else if (len > MAX_EVENT_NAME_LEN) { |
ab105a4f MH |
263 | trace_probe_log_err(offset, EVENT_TOO_LONG); |
264 | return -EINVAL; | |
6212dd29 | 265 | } |
5b7a9622 | 266 | if (!is_good_name(event)) { |
ab105a4f | 267 | trace_probe_log_err(offset, BAD_EVENT_NAME); |
5b7a9622 MH |
268 | return -EINVAL; |
269 | } | |
6212dd29 MH |
270 | return 0; |
271 | } | |
272 | ||
8ab83f56 SD |
273 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) |
274 | ||
275 | static int parse_probe_vars(char *arg, const struct fetch_type *t, | |
ab105a4f | 276 | struct fetch_insn *code, unsigned int flags, int offs) |
8ab83f56 | 277 | { |
8ab83f56 | 278 | unsigned long param; |
3d739c1f SRV |
279 | int ret = 0; |
280 | int len; | |
8ab83f56 SD |
281 | |
282 | if (strcmp(arg, "retval") == 0) { | |
ab105a4f | 283 | if (flags & TPARG_FL_RETURN) { |
53305928 | 284 | code->op = FETCH_OP_RETVAL; |
ab105a4f MH |
285 | } else { |
286 | trace_probe_log_err(offs, RETVAL_ON_PROBE); | |
8ab83f56 | 287 | ret = -EINVAL; |
ab105a4f | 288 | } |
3d739c1f SRV |
289 | } else if ((len = str_has_prefix(arg, "stack"))) { |
290 | if (arg[len] == '\0') { | |
53305928 | 291 | code->op = FETCH_OP_STACKP; |
3d739c1f SRV |
292 | } else if (isdigit(arg[len])) { |
293 | ret = kstrtoul(arg + len, 10, ¶m); | |
ab105a4f MH |
294 | if (ret) { |
295 | goto inval_var; | |
296 | } else if ((flags & TPARG_FL_KERNEL) && | |
297 | param > PARAM_MAX_STACK) { | |
298 | trace_probe_log_err(offs, BAD_STACK_NUM); | |
8ab83f56 | 299 | ret = -EINVAL; |
ab105a4f | 300 | } else { |
53305928 MH |
301 | code->op = FETCH_OP_STACK; |
302 | code->param = (unsigned int)param; | |
8ab83f56 SD |
303 | } |
304 | } else | |
ab105a4f | 305 | goto inval_var; |
35abb67d | 306 | } else if (strcmp(arg, "comm") == 0) { |
53305928 | 307 | code->op = FETCH_OP_COMM; |
a1303af5 MH |
308 | #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API |
309 | } else if (((flags & TPARG_FL_MASK) == | |
310 | (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) && | |
3d739c1f | 311 | (len = str_has_prefix(arg, "arg"))) { |
3d739c1f | 312 | ret = kstrtoul(arg + len, 10, ¶m); |
ab105a4f MH |
313 | if (ret) { |
314 | goto inval_var; | |
315 | } else if (!param || param > PARAM_MAX_STACK) { | |
316 | trace_probe_log_err(offs, BAD_ARG_NUM); | |
a1303af5 | 317 | return -EINVAL; |
ab105a4f | 318 | } |
a1303af5 MH |
319 | code->op = FETCH_OP_ARG; |
320 | code->param = (unsigned int)param - 1; | |
321 | #endif | |
7491e2c4 TSV |
322 | } else if (flags & TPARG_FL_TPOINT) { |
323 | if (code->data) | |
324 | return -EFAULT; | |
325 | code->data = kstrdup(arg, GFP_KERNEL); | |
326 | if (!code->data) | |
327 | return -ENOMEM; | |
328 | code->op = FETCH_OP_TP_ARG; | |
8ab83f56 | 329 | } else |
ab105a4f | 330 | goto inval_var; |
8ab83f56 SD |
331 | |
332 | return ret; | |
ab105a4f MH |
333 | |
334 | inval_var: | |
335 | trace_probe_log_err(offs, BAD_VAR); | |
336 | return -EINVAL; | |
8ab83f56 SD |
337 | } |
338 | ||
6218bf9f MH |
339 | static int str_to_immediate(char *str, unsigned long *imm) |
340 | { | |
341 | if (isdigit(str[0])) | |
342 | return kstrtoul(str, 0, imm); | |
343 | else if (str[0] == '-') | |
344 | return kstrtol(str, 0, (long *)imm); | |
345 | else if (str[0] == '+') | |
346 | return kstrtol(str + 1, 0, (long *)imm); | |
347 | return -EINVAL; | |
348 | } | |
349 | ||
a42e3c4d MH |
350 | static int __parse_imm_string(char *str, char **pbuf, int offs) |
351 | { | |
352 | size_t len = strlen(str); | |
353 | ||
354 | if (str[len - 1] != '"') { | |
355 | trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE); | |
356 | return -EINVAL; | |
357 | } | |
358 | *pbuf = kstrndup(str, len - 1, GFP_KERNEL); | |
359 | return 0; | |
360 | } | |
361 | ||
8ab83f56 | 362 | /* Recursive argument parser */ |
53305928 MH |
363 | static int |
364 | parse_probe_arg(char *arg, const struct fetch_type *type, | |
365 | struct fetch_insn **pcode, struct fetch_insn *end, | |
ab105a4f | 366 | unsigned int flags, int offs) |
8ab83f56 | 367 | { |
53305928 | 368 | struct fetch_insn *code = *pcode; |
8ab83f56 | 369 | unsigned long param; |
e65f7ae7 | 370 | int deref = FETCH_OP_DEREF; |
bf173ca9 | 371 | long offset = 0; |
8ab83f56 | 372 | char *tmp; |
34fee3a1 | 373 | int ret = 0; |
8ab83f56 | 374 | |
8ab83f56 SD |
375 | switch (arg[0]) { |
376 | case '$': | |
ab105a4f | 377 | ret = parse_probe_vars(arg + 1, type, code, flags, offs); |
8ab83f56 SD |
378 | break; |
379 | ||
380 | case '%': /* named register */ | |
381 | ret = regs_query_register_offset(arg + 1); | |
382 | if (ret >= 0) { | |
53305928 MH |
383 | code->op = FETCH_OP_REG; |
384 | code->param = (unsigned int)ret; | |
8ab83f56 | 385 | ret = 0; |
ab105a4f MH |
386 | } else |
387 | trace_probe_log_err(offs, BAD_REG_NAME); | |
8ab83f56 SD |
388 | break; |
389 | ||
b7e0bf34 | 390 | case '@': /* memory, file-offset or symbol */ |
8ab83f56 | 391 | if (isdigit(arg[1])) { |
bcd83ea6 | 392 | ret = kstrtoul(arg + 1, 0, ¶m); |
ab105a4f MH |
393 | if (ret) { |
394 | trace_probe_log_err(offs, BAD_MEM_ADDR); | |
8ab83f56 | 395 | break; |
ab105a4f | 396 | } |
53305928 MH |
397 | /* load address */ |
398 | code->op = FETCH_OP_IMM; | |
399 | code->immediate = param; | |
b7e0bf34 NK |
400 | } else if (arg[1] == '+') { |
401 | /* kprobes don't support file offsets */ | |
ab105a4f MH |
402 | if (flags & TPARG_FL_KERNEL) { |
403 | trace_probe_log_err(offs, FILE_ON_KPROBE); | |
b7e0bf34 | 404 | return -EINVAL; |
ab105a4f | 405 | } |
b7e0bf34 | 406 | ret = kstrtol(arg + 2, 0, &offset); |
ab105a4f MH |
407 | if (ret) { |
408 | trace_probe_log_err(offs, BAD_FILE_OFFS); | |
b7e0bf34 | 409 | break; |
ab105a4f | 410 | } |
b7e0bf34 | 411 | |
53305928 MH |
412 | code->op = FETCH_OP_FOFFS; |
413 | code->immediate = (unsigned long)offset; // imm64? | |
8ab83f56 | 414 | } else { |
b079d374 | 415 | /* uprobes don't support symbols */ |
ab105a4f MH |
416 | if (!(flags & TPARG_FL_KERNEL)) { |
417 | trace_probe_log_err(offs, SYM_ON_UPROBE); | |
b079d374 | 418 | return -EINVAL; |
ab105a4f | 419 | } |
a6682814 MH |
420 | /* Preserve symbol for updating */ |
421 | code->op = FETCH_NOP_SYMBOL; | |
422 | code->data = kstrdup(arg + 1, GFP_KERNEL); | |
423 | if (!code->data) | |
424 | return -ENOMEM; | |
ab105a4f MH |
425 | if (++code == end) { |
426 | trace_probe_log_err(offs, TOO_MANY_OPS); | |
427 | return -EINVAL; | |
428 | } | |
53305928 | 429 | code->op = FETCH_OP_IMM; |
a6682814 | 430 | code->immediate = 0; |
8ab83f56 | 431 | } |
53305928 | 432 | /* These are fetching from memory */ |
ab105a4f MH |
433 | if (++code == end) { |
434 | trace_probe_log_err(offs, TOO_MANY_OPS); | |
435 | return -EINVAL; | |
436 | } | |
53305928 MH |
437 | *pcode = code; |
438 | code->op = FETCH_OP_DEREF; | |
439 | code->offset = offset; | |
8ab83f56 SD |
440 | break; |
441 | ||
442 | case '+': /* deref memory */ | |
8ab83f56 | 443 | case '-': |
e65f7ae7 MH |
444 | if (arg[1] == 'u') { |
445 | deref = FETCH_OP_UDEREF; | |
446 | arg[1] = arg[0]; | |
447 | arg++; | |
448 | } | |
449 | if (arg[0] == '+') | |
450 | arg++; /* Skip '+', because kstrtol() rejects it. */ | |
8ab83f56 | 451 | tmp = strchr(arg, '('); |
ab105a4f MH |
452 | if (!tmp) { |
453 | trace_probe_log_err(offs, DEREF_NEED_BRACE); | |
53305928 | 454 | return -EINVAL; |
ab105a4f | 455 | } |
8ab83f56 | 456 | *tmp = '\0'; |
bcd83ea6 | 457 | ret = kstrtol(arg, 0, &offset); |
ab105a4f MH |
458 | if (ret) { |
459 | trace_probe_log_err(offs, BAD_DEREF_OFFS); | |
8ab83f56 | 460 | break; |
ab105a4f MH |
461 | } |
462 | offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0); | |
8ab83f56 SD |
463 | arg = tmp + 1; |
464 | tmp = strrchr(arg, ')'); | |
ab105a4f MH |
465 | if (!tmp) { |
466 | trace_probe_log_err(offs + strlen(arg), | |
467 | DEREF_OPEN_BRACE); | |
468 | return -EINVAL; | |
469 | } else { | |
f451bc89 | 470 | const struct fetch_type *t2 = find_fetch_type(NULL); |
8ab83f56 | 471 | |
8ab83f56 | 472 | *tmp = '\0'; |
ab105a4f | 473 | ret = parse_probe_arg(arg, t2, &code, end, flags, offs); |
8ab83f56 | 474 | if (ret) |
53305928 | 475 | break; |
a42e3c4d MH |
476 | if (code->op == FETCH_OP_COMM || |
477 | code->op == FETCH_OP_DATA) { | |
ab105a4f MH |
478 | trace_probe_log_err(offs, COMM_CANT_DEREF); |
479 | return -EINVAL; | |
480 | } | |
481 | if (++code == end) { | |
482 | trace_probe_log_err(offs, TOO_MANY_OPS); | |
53305928 | 483 | return -EINVAL; |
ab105a4f | 484 | } |
53305928 MH |
485 | *pcode = code; |
486 | ||
e65f7ae7 | 487 | code->op = deref; |
53305928 | 488 | code->offset = offset; |
8ab83f56 SD |
489 | } |
490 | break; | |
6218bf9f | 491 | case '\\': /* Immediate value */ |
a42e3c4d MH |
492 | if (arg[1] == '"') { /* Immediate string */ |
493 | ret = __parse_imm_string(arg + 2, &tmp, offs + 2); | |
494 | if (ret) | |
495 | break; | |
496 | code->op = FETCH_OP_DATA; | |
497 | code->data = tmp; | |
498 | } else { | |
499 | ret = str_to_immediate(arg + 1, &code->immediate); | |
500 | if (ret) | |
501 | trace_probe_log_err(offs + 1, BAD_IMM); | |
502 | else | |
503 | code->op = FETCH_OP_IMM; | |
504 | } | |
6218bf9f | 505 | break; |
8ab83f56 | 506 | } |
53305928 MH |
507 | if (!ret && code->op == FETCH_OP_NOP) { |
508 | /* Parsed, but do not find fetch method */ | |
ab105a4f | 509 | trace_probe_log_err(offs, BAD_FETCH_ARG); |
8ab83f56 SD |
510 | ret = -EINVAL; |
511 | } | |
8ab83f56 SD |
512 | return ret; |
513 | } | |
514 | ||
515 | #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long)) | |
516 | ||
517 | /* Bitfield type needs to be parsed into a fetch function */ | |
518 | static int __parse_bitfield_probe_arg(const char *bf, | |
519 | const struct fetch_type *t, | |
53305928 | 520 | struct fetch_insn **pcode) |
8ab83f56 | 521 | { |
53305928 | 522 | struct fetch_insn *code = *pcode; |
8ab83f56 SD |
523 | unsigned long bw, bo; |
524 | char *tail; | |
525 | ||
526 | if (*bf != 'b') | |
527 | return 0; | |
528 | ||
8ab83f56 SD |
529 | bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */ |
530 | ||
531 | if (bw == 0 || *tail != '@') | |
532 | return -EINVAL; | |
533 | ||
534 | bf = tail + 1; | |
535 | bo = simple_strtoul(bf, &tail, 0); | |
536 | ||
537 | if (tail == bf || *tail != '/') | |
538 | return -EINVAL; | |
53305928 MH |
539 | code++; |
540 | if (code->op != FETCH_OP_NOP) | |
ab105a4f | 541 | return -EINVAL; |
53305928 | 542 | *pcode = code; |
8ab83f56 | 543 | |
53305928 MH |
544 | code->op = FETCH_OP_MOD_BF; |
545 | code->lshift = BYTES_TO_BITS(t->size) - (bw + bo); | |
546 | code->rshift = BYTES_TO_BITS(t->size) - bw; | |
547 | code->basesize = t->size; | |
8ab83f56 SD |
548 | |
549 | return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0; | |
550 | } | |
551 | ||
552 | /* String length checking wrapper */ | |
fcd9db51 | 553 | static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size, |
ab105a4f | 554 | struct probe_arg *parg, unsigned int flags, int offset) |
8ab83f56 | 555 | { |
40b53b77 | 556 | struct fetch_insn *code, *scode, *tmp = NULL; |
ab105a4f | 557 | char *t, *t2, *t3; |
fcd9db51 | 558 | char *arg; |
40b53b77 | 559 | int ret, len; |
8ab83f56 | 560 | |
fcd9db51 SRV |
561 | arg = kstrdup(argv, GFP_KERNEL); |
562 | if (!arg) | |
563 | return -ENOMEM; | |
564 | ||
565 | ret = -EINVAL; | |
ab105a4f MH |
566 | len = strlen(arg); |
567 | if (len > MAX_ARGSTR_LEN) { | |
568 | trace_probe_log_err(offset, ARG_TOO_LONG); | |
fcd9db51 | 569 | goto out; |
ab105a4f MH |
570 | } else if (len == 0) { |
571 | trace_probe_log_err(offset, NO_ARG_BODY); | |
fcd9db51 | 572 | goto out; |
8ab83f56 | 573 | } |
ab105a4f | 574 | |
fcd9db51 | 575 | ret = -ENOMEM; |
8ab83f56 | 576 | parg->comm = kstrdup(arg, GFP_KERNEL); |
ab105a4f | 577 | if (!parg->comm) |
fcd9db51 | 578 | goto out; |
ab105a4f | 579 | |
fcd9db51 | 580 | ret = -EINVAL; |
40b53b77 | 581 | t = strchr(arg, ':'); |
8ab83f56 | 582 | if (t) { |
40b53b77 MH |
583 | *t = '\0'; |
584 | t2 = strchr(++t, '['); | |
585 | if (t2) { | |
ab105a4f MH |
586 | *t2++ = '\0'; |
587 | t3 = strchr(t2, ']'); | |
588 | if (!t3) { | |
589 | offset += t2 + strlen(t2) - arg; | |
590 | trace_probe_log_err(offset, | |
591 | ARRAY_NO_CLOSE); | |
fcd9db51 | 592 | goto out; |
ab105a4f MH |
593 | } else if (t3[1] != '\0') { |
594 | trace_probe_log_err(offset + t3 + 1 - arg, | |
595 | BAD_ARRAY_SUFFIX); | |
fcd9db51 | 596 | goto out; |
ab105a4f MH |
597 | } |
598 | *t3 = '\0'; | |
599 | if (kstrtouint(t2, 0, &parg->count) || !parg->count) { | |
600 | trace_probe_log_err(offset + t2 - arg, | |
601 | BAD_ARRAY_NUM); | |
fcd9db51 | 602 | goto out; |
ab105a4f MH |
603 | } |
604 | if (parg->count > MAX_ARRAY_LEN) { | |
605 | trace_probe_log_err(offset + t2 - arg, | |
606 | ARRAY_TOO_BIG); | |
fcd9db51 | 607 | goto out; |
ab105a4f | 608 | } |
40b53b77 | 609 | } |
8ab83f56 | 610 | } |
3dd1f7f2 | 611 | |
a42e3c4d | 612 | /* |
f2cc020d | 613 | * Since $comm and immediate string can not be dereferenced, |
a42e3c4d MH |
614 | * we can find those by strcmp. |
615 | */ | |
616 | if (strcmp(arg, "$comm") == 0 || strncmp(arg, "\\\"", 2) == 0) { | |
3dd1f7f2 MH |
617 | /* The type of $comm must be "string", and not an array. */ |
618 | if (parg->count || (t && strcmp(t, "string"))) | |
fcd9db51 | 619 | goto out; |
40b53b77 | 620 | parg->type = find_fetch_type("string"); |
3dd1f7f2 | 621 | } else |
40b53b77 | 622 | parg->type = find_fetch_type(t); |
8ab83f56 | 623 | if (!parg->type) { |
ab105a4f | 624 | trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE); |
fcd9db51 | 625 | goto out; |
8ab83f56 SD |
626 | } |
627 | parg->offset = *size; | |
40b53b77 MH |
628 | *size += parg->type->size * (parg->count ?: 1); |
629 | ||
fcd9db51 | 630 | ret = -ENOMEM; |
40b53b77 MH |
631 | if (parg->count) { |
632 | len = strlen(parg->type->fmttype) + 6; | |
633 | parg->fmt = kmalloc(len, GFP_KERNEL); | |
634 | if (!parg->fmt) | |
fcd9db51 | 635 | goto out; |
40b53b77 MH |
636 | snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype, |
637 | parg->count); | |
638 | } | |
8ab83f56 | 639 | |
8623b006 | 640 | code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL); |
53305928 | 641 | if (!code) |
fcd9db51 | 642 | goto out; |
53305928 MH |
643 | code[FETCH_INSN_MAX - 1].op = FETCH_OP_END; |
644 | ||
645 | ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1], | |
ab105a4f | 646 | flags, offset); |
53305928 MH |
647 | if (ret) |
648 | goto fail; | |
649 | ||
fcd9db51 | 650 | ret = -EINVAL; |
53305928 | 651 | /* Store operation */ |
88903c46 MH |
652 | if (!strcmp(parg->type->name, "string") || |
653 | !strcmp(parg->type->name, "ustring")) { | |
e65f7ae7 | 654 | if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF && |
a42e3c4d | 655 | code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM && |
7491e2c4 | 656 | code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) { |
ab105a4f MH |
657 | trace_probe_log_err(offset + (t ? (t - arg) : 0), |
658 | BAD_STRING); | |
53305928 MH |
659 | goto fail; |
660 | } | |
3aa8fdc3 | 661 | if ((code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM || |
7491e2c4 TSV |
662 | code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG || |
663 | parg->count) { | |
40b53b77 | 664 | /* |
a42e3c4d MH |
665 | * IMM, DATA and COMM is pointing actual address, those |
666 | * must be kept, and if parg->count != 0, this is an | |
667 | * array of string pointers instead of string address | |
668 | * itself. | |
40b53b77 | 669 | */ |
53305928 | 670 | code++; |
40b53b77 | 671 | if (code->op != FETCH_OP_NOP) { |
ab105a4f | 672 | trace_probe_log_err(offset, TOO_MANY_OPS); |
40b53b77 MH |
673 | goto fail; |
674 | } | |
675 | } | |
88903c46 | 676 | /* If op == DEREF, replace it with STRING */ |
e65f7ae7 MH |
677 | if (!strcmp(parg->type->name, "ustring") || |
678 | code->op == FETCH_OP_UDEREF) | |
88903c46 MH |
679 | code->op = FETCH_OP_ST_USTRING; |
680 | else | |
681 | code->op = FETCH_OP_ST_STRING; | |
40b53b77 | 682 | code->size = parg->type->size; |
53305928 MH |
683 | parg->dynamic = true; |
684 | } else if (code->op == FETCH_OP_DEREF) { | |
685 | code->op = FETCH_OP_ST_MEM; | |
686 | code->size = parg->type->size; | |
e65f7ae7 MH |
687 | } else if (code->op == FETCH_OP_UDEREF) { |
688 | code->op = FETCH_OP_ST_UMEM; | |
689 | code->size = parg->type->size; | |
53305928 MH |
690 | } else { |
691 | code++; | |
692 | if (code->op != FETCH_OP_NOP) { | |
ab105a4f | 693 | trace_probe_log_err(offset, TOO_MANY_OPS); |
53305928 MH |
694 | goto fail; |
695 | } | |
696 | code->op = FETCH_OP_ST_RAW; | |
697 | code->size = parg->type->size; | |
698 | } | |
40b53b77 | 699 | scode = code; |
53305928 MH |
700 | /* Modify operation */ |
701 | if (t != NULL) { | |
702 | ret = __parse_bitfield_probe_arg(t, parg->type, &code); | |
ab105a4f MH |
703 | if (ret) { |
704 | trace_probe_log_err(offset + t - arg, BAD_BITFIELD); | |
53305928 | 705 | goto fail; |
ab105a4f | 706 | } |
8ab83f56 | 707 | } |
fcd9db51 | 708 | ret = -EINVAL; |
40b53b77 MH |
709 | /* Loop(Array) operation */ |
710 | if (parg->count) { | |
711 | if (scode->op != FETCH_OP_ST_MEM && | |
88903c46 MH |
712 | scode->op != FETCH_OP_ST_STRING && |
713 | scode->op != FETCH_OP_ST_USTRING) { | |
ab105a4f MH |
714 | trace_probe_log_err(offset + (t ? (t - arg) : 0), |
715 | BAD_STRING); | |
40b53b77 MH |
716 | goto fail; |
717 | } | |
718 | code++; | |
719 | if (code->op != FETCH_OP_NOP) { | |
ab105a4f | 720 | trace_probe_log_err(offset, TOO_MANY_OPS); |
40b53b77 MH |
721 | goto fail; |
722 | } | |
723 | code->op = FETCH_OP_LP_ARRAY; | |
724 | code->param = parg->count; | |
725 | } | |
53305928 MH |
726 | code++; |
727 | code->op = FETCH_OP_END; | |
728 | ||
fcd9db51 | 729 | ret = 0; |
53305928 | 730 | /* Shrink down the code buffer */ |
8623b006 | 731 | parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL); |
53305928 MH |
732 | if (!parg->code) |
733 | ret = -ENOMEM; | |
734 | else | |
735 | memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1)); | |
736 | ||
737 | fail: | |
a6682814 MH |
738 | if (ret) { |
739 | for (code = tmp; code < tmp + FETCH_INSN_MAX; code++) | |
a42e3c4d MH |
740 | if (code->op == FETCH_NOP_SYMBOL || |
741 | code->op == FETCH_OP_DATA) | |
a6682814 MH |
742 | kfree(code->data); |
743 | } | |
53305928 | 744 | kfree(tmp); |
fcd9db51 SRV |
745 | out: |
746 | kfree(arg); | |
8ab83f56 SD |
747 | |
748 | return ret; | |
749 | } | |
750 | ||
751 | /* Return 1 if name is reserved or already used by another argument */ | |
d00bbea9 MH |
752 | static int traceprobe_conflict_field_name(const char *name, |
753 | struct probe_arg *args, int narg) | |
8ab83f56 SD |
754 | { |
755 | int i; | |
756 | ||
757 | for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) | |
758 | if (strcmp(reserved_field_names[i], name) == 0) | |
759 | return 1; | |
760 | ||
761 | for (i = 0; i < narg; i++) | |
762 | if (strcmp(args[i].name, name) == 0) | |
763 | return 1; | |
764 | ||
765 | return 0; | |
766 | } | |
767 | ||
fcd9db51 | 768 | int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg, |
d00bbea9 MH |
769 | unsigned int flags) |
770 | { | |
771 | struct probe_arg *parg = &tp->args[i]; | |
fcd9db51 | 772 | const char *body; |
d00bbea9 MH |
773 | |
774 | /* Increment count for freeing args in error case */ | |
775 | tp->nr_args++; | |
776 | ||
777 | body = strchr(arg, '='); | |
778 | if (body) { | |
ab105a4f MH |
779 | if (body - arg > MAX_ARG_NAME_LEN) { |
780 | trace_probe_log_err(0, ARG_NAME_TOO_LONG); | |
781 | return -EINVAL; | |
782 | } else if (body == arg) { | |
783 | trace_probe_log_err(0, NO_ARG_NAME); | |
b4443c17 | 784 | return -EINVAL; |
ab105a4f | 785 | } |
d00bbea9 MH |
786 | parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL); |
787 | body++; | |
788 | } else { | |
789 | /* If argument name is omitted, set "argN" */ | |
790 | parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1); | |
791 | body = arg; | |
792 | } | |
793 | if (!parg->name) | |
794 | return -ENOMEM; | |
795 | ||
796 | if (!is_good_name(parg->name)) { | |
ab105a4f | 797 | trace_probe_log_err(0, BAD_ARG_NAME); |
d00bbea9 MH |
798 | return -EINVAL; |
799 | } | |
d00bbea9 | 800 | if (traceprobe_conflict_field_name(parg->name, tp->args, i)) { |
ab105a4f | 801 | trace_probe_log_err(0, USED_ARG_NAME); |
d00bbea9 MH |
802 | return -EINVAL; |
803 | } | |
d00bbea9 | 804 | /* Parse fetch argument */ |
ab105a4f MH |
805 | return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags, |
806 | body - arg); | |
d00bbea9 MH |
807 | } |
808 | ||
8ab83f56 SD |
809 | void traceprobe_free_probe_arg(struct probe_arg *arg) |
810 | { | |
a6682814 MH |
811 | struct fetch_insn *code = arg->code; |
812 | ||
813 | while (code && code->op != FETCH_OP_END) { | |
a42e3c4d MH |
814 | if (code->op == FETCH_NOP_SYMBOL || |
815 | code->op == FETCH_OP_DATA) | |
a6682814 MH |
816 | kfree(code->data); |
817 | code++; | |
818 | } | |
53305928 | 819 | kfree(arg->code); |
8ab83f56 SD |
820 | kfree(arg->name); |
821 | kfree(arg->comm); | |
40b53b77 | 822 | kfree(arg->fmt); |
8ab83f56 SD |
823 | } |
824 | ||
a6682814 MH |
825 | int traceprobe_update_arg(struct probe_arg *arg) |
826 | { | |
827 | struct fetch_insn *code = arg->code; | |
828 | long offset; | |
829 | char *tmp; | |
830 | char c; | |
831 | int ret = 0; | |
832 | ||
833 | while (code && code->op != FETCH_OP_END) { | |
834 | if (code->op == FETCH_NOP_SYMBOL) { | |
835 | if (code[1].op != FETCH_OP_IMM) | |
836 | return -EINVAL; | |
837 | ||
ee474b81 | 838 | tmp = strpbrk(code->data, "+-"); |
a6682814 MH |
839 | if (tmp) |
840 | c = *tmp; | |
841 | ret = traceprobe_split_symbol_offset(code->data, | |
842 | &offset); | |
843 | if (ret) | |
844 | return ret; | |
845 | ||
846 | code[1].immediate = | |
847 | (unsigned long)kallsyms_lookup_name(code->data); | |
848 | if (tmp) | |
849 | *tmp = c; | |
850 | if (!code[1].immediate) | |
851 | return -ENOENT; | |
852 | code[1].immediate += offset; | |
853 | } | |
854 | code++; | |
855 | } | |
856 | return 0; | |
857 | } | |
858 | ||
40b53b77 MH |
859 | /* When len=0, we just calculate the needed length */ |
860 | #define LEN_OR_ZERO (len ? len - pos : 0) | |
5bf652aa | 861 | static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, |
007517a0 | 862 | enum probe_print_type ptype) |
5bf652aa | 863 | { |
40b53b77 MH |
864 | struct probe_arg *parg; |
865 | int i, j; | |
5bf652aa | 866 | int pos = 0; |
5bf652aa NK |
867 | const char *fmt, *arg; |
868 | ||
007517a0 SRV |
869 | switch (ptype) { |
870 | case PROBE_PRINT_NORMAL: | |
5bf652aa NK |
871 | fmt = "(%lx)"; |
872 | arg = "REC->" FIELD_STRING_IP; | |
007517a0 SRV |
873 | break; |
874 | case PROBE_PRINT_RETURN: | |
5bf652aa NK |
875 | fmt = "(%lx <- %lx)"; |
876 | arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; | |
007517a0 | 877 | break; |
7491e2c4 TSV |
878 | case PROBE_PRINT_EVENT: |
879 | fmt = "(%u)"; | |
880 | arg = "REC->" FIELD_STRING_TYPE; | |
881 | break; | |
007517a0 SRV |
882 | default: |
883 | WARN_ON_ONCE(1); | |
884 | return 0; | |
5bf652aa NK |
885 | } |
886 | ||
5bf652aa NK |
887 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); |
888 | ||
889 | for (i = 0; i < tp->nr_args; i++) { | |
40b53b77 MH |
890 | parg = tp->args + i; |
891 | pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name); | |
892 | if (parg->count) { | |
893 | pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s", | |
894 | parg->type->fmt); | |
895 | for (j = 1; j < parg->count; j++) | |
896 | pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s", | |
897 | parg->type->fmt); | |
898 | pos += snprintf(buf + pos, LEN_OR_ZERO, "}"); | |
899 | } else | |
900 | pos += snprintf(buf + pos, LEN_OR_ZERO, "%s", | |
901 | parg->type->fmt); | |
5bf652aa NK |
902 | } |
903 | ||
904 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); | |
905 | ||
906 | for (i = 0; i < tp->nr_args; i++) { | |
40b53b77 MH |
907 | parg = tp->args + i; |
908 | if (parg->count) { | |
20279420 SRV |
909 | if ((strcmp(parg->type->name, "string") == 0) || |
910 | (strcmp(parg->type->name, "ustring") == 0)) | |
40b53b77 MH |
911 | fmt = ", __get_str(%s[%d])"; |
912 | else | |
913 | fmt = ", REC->%s[%d]"; | |
914 | for (j = 0; j < parg->count; j++) | |
915 | pos += snprintf(buf + pos, LEN_OR_ZERO, | |
916 | fmt, parg->name, j); | |
917 | } else { | |
20279420 SRV |
918 | if ((strcmp(parg->type->name, "string") == 0) || |
919 | (strcmp(parg->type->name, "ustring") == 0)) | |
40b53b77 MH |
920 | fmt = ", __get_str(%s)"; |
921 | else | |
922 | fmt = ", REC->%s"; | |
5bf652aa | 923 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
40b53b77 MH |
924 | fmt, parg->name); |
925 | } | |
5bf652aa NK |
926 | } |
927 | ||
5bf652aa NK |
928 | /* return the length of print_fmt */ |
929 | return pos; | |
930 | } | |
40b53b77 | 931 | #undef LEN_OR_ZERO |
5bf652aa | 932 | |
007517a0 | 933 | int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype) |
5bf652aa | 934 | { |
e3dc9f89 | 935 | struct trace_event_call *call = trace_probe_event_call(tp); |
5bf652aa NK |
936 | int len; |
937 | char *print_fmt; | |
938 | ||
939 | /* First: called with 0 length to calculate the needed length */ | |
007517a0 | 940 | len = __set_print_fmt(tp, NULL, 0, ptype); |
5bf652aa NK |
941 | print_fmt = kmalloc(len + 1, GFP_KERNEL); |
942 | if (!print_fmt) | |
943 | return -ENOMEM; | |
944 | ||
945 | /* Second: actually write the @print_fmt */ | |
007517a0 | 946 | __set_print_fmt(tp, print_fmt, len + 1, ptype); |
e3dc9f89 | 947 | call->print_fmt = print_fmt; |
5bf652aa NK |
948 | |
949 | return 0; | |
950 | } | |
eeb07b06 MH |
951 | |
952 | int traceprobe_define_arg_fields(struct trace_event_call *event_call, | |
953 | size_t offset, struct trace_probe *tp) | |
954 | { | |
955 | int ret, i; | |
956 | ||
957 | /* Set argument names as fields */ | |
958 | for (i = 0; i < tp->nr_args; i++) { | |
959 | struct probe_arg *parg = &tp->args[i]; | |
40b53b77 MH |
960 | const char *fmt = parg->type->fmttype; |
961 | int size = parg->type->size; | |
962 | ||
963 | if (parg->fmt) | |
964 | fmt = parg->fmt; | |
965 | if (parg->count) | |
966 | size *= parg->count; | |
967 | ret = trace_define_field(event_call, fmt, parg->name, | |
968 | offset + parg->offset, size, | |
eeb07b06 MH |
969 | parg->type->is_signed, |
970 | FILTER_OTHER); | |
971 | if (ret) | |
972 | return ret; | |
973 | } | |
974 | return 0; | |
975 | } | |
455b2899 | 976 | |
ca89bc07 MH |
977 | static void trace_probe_event_free(struct trace_probe_event *tpe) |
978 | { | |
979 | kfree(tpe->class.system); | |
980 | kfree(tpe->call.name); | |
981 | kfree(tpe->call.print_fmt); | |
982 | kfree(tpe); | |
983 | } | |
984 | ||
985 | int trace_probe_append(struct trace_probe *tp, struct trace_probe *to) | |
986 | { | |
987 | if (trace_probe_has_sibling(tp)) | |
988 | return -EBUSY; | |
989 | ||
990 | list_del_init(&tp->list); | |
991 | trace_probe_event_free(tp->event); | |
992 | ||
993 | tp->event = to->event; | |
994 | list_add_tail(&tp->list, trace_probe_probe_list(to)); | |
995 | ||
996 | return 0; | |
997 | } | |
998 | ||
999 | void trace_probe_unlink(struct trace_probe *tp) | |
1000 | { | |
1001 | list_del_init(&tp->list); | |
1002 | if (list_empty(trace_probe_probe_list(tp))) | |
1003 | trace_probe_event_free(tp->event); | |
1004 | tp->event = NULL; | |
1005 | } | |
455b2899 MH |
1006 | |
1007 | void trace_probe_cleanup(struct trace_probe *tp) | |
1008 | { | |
1009 | int i; | |
1010 | ||
1011 | for (i = 0; i < tp->nr_args; i++) | |
1012 | traceprobe_free_probe_arg(&tp->args[i]); | |
1013 | ||
ca89bc07 MH |
1014 | if (tp->event) |
1015 | trace_probe_unlink(tp); | |
455b2899 MH |
1016 | } |
1017 | ||
1018 | int trace_probe_init(struct trace_probe *tp, const char *event, | |
b61387cb | 1019 | const char *group, bool alloc_filter) |
455b2899 | 1020 | { |
60d53e2c | 1021 | struct trace_event_call *call; |
b61387cb | 1022 | size_t size = sizeof(struct trace_probe_event); |
60d53e2c | 1023 | int ret = 0; |
e3dc9f89 | 1024 | |
455b2899 MH |
1025 | if (!event || !group) |
1026 | return -EINVAL; | |
1027 | ||
b61387cb MH |
1028 | if (alloc_filter) |
1029 | size += sizeof(struct trace_uprobe_filter); | |
1030 | ||
1031 | tp->event = kzalloc(size, GFP_KERNEL); | |
60d53e2c | 1032 | if (!tp->event) |
455b2899 MH |
1033 | return -ENOMEM; |
1034 | ||
d59fae6f MH |
1035 | INIT_LIST_HEAD(&tp->event->files); |
1036 | INIT_LIST_HEAD(&tp->event->class.fields); | |
1037 | INIT_LIST_HEAD(&tp->event->probes); | |
1038 | INIT_LIST_HEAD(&tp->list); | |
fc9d276f | 1039 | list_add(&tp->list, &tp->event->probes); |
d59fae6f | 1040 | |
60d53e2c MH |
1041 | call = trace_probe_event_call(tp); |
1042 | call->class = &tp->event->class; | |
1043 | call->name = kstrdup(event, GFP_KERNEL); | |
1044 | if (!call->name) { | |
1045 | ret = -ENOMEM; | |
1046 | goto error; | |
1047 | } | |
1048 | ||
1049 | tp->event->class.system = kstrdup(group, GFP_KERNEL); | |
1050 | if (!tp->event->class.system) { | |
1051 | ret = -ENOMEM; | |
1052 | goto error; | |
455b2899 | 1053 | } |
455b2899 MH |
1054 | |
1055 | return 0; | |
60d53e2c MH |
1056 | |
1057 | error: | |
1058 | trace_probe_cleanup(tp); | |
1059 | return ret; | |
455b2899 | 1060 | } |
46e5376d | 1061 | |
8e242060 MH |
1062 | static struct trace_event_call * |
1063 | find_trace_event_call(const char *system, const char *event_name) | |
1064 | { | |
1065 | struct trace_event_call *tp_event; | |
1066 | const char *name; | |
1067 | ||
1068 | list_for_each_entry(tp_event, &ftrace_events, list) { | |
1069 | if (!tp_event->class->system || | |
1070 | strcmp(system, tp_event->class->system)) | |
1071 | continue; | |
1072 | name = trace_event_name(tp_event); | |
1073 | if (!name || strcmp(event_name, name)) | |
1074 | continue; | |
1075 | return tp_event; | |
1076 | } | |
1077 | ||
1078 | return NULL; | |
1079 | } | |
1080 | ||
46e5376d MH |
1081 | int trace_probe_register_event_call(struct trace_probe *tp) |
1082 | { | |
e3dc9f89 | 1083 | struct trace_event_call *call = trace_probe_event_call(tp); |
46e5376d MH |
1084 | int ret; |
1085 | ||
8e242060 MH |
1086 | lockdep_assert_held(&event_mutex); |
1087 | ||
1088 | if (find_trace_event_call(trace_probe_group_name(tp), | |
1089 | trace_probe_name(tp))) | |
1090 | return -EEXIST; | |
1091 | ||
46e5376d MH |
1092 | ret = register_trace_event(&call->event); |
1093 | if (!ret) | |
1094 | return -ENODEV; | |
1095 | ||
1096 | ret = trace_add_event_call(call); | |
1097 | if (ret) | |
1098 | unregister_trace_event(&call->event); | |
1099 | ||
1100 | return ret; | |
1101 | } | |
b5f935ee MH |
1102 | |
1103 | int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file) | |
1104 | { | |
1105 | struct event_file_link *link; | |
1106 | ||
1107 | link = kmalloc(sizeof(*link), GFP_KERNEL); | |
1108 | if (!link) | |
1109 | return -ENOMEM; | |
1110 | ||
1111 | link->file = file; | |
1112 | INIT_LIST_HEAD(&link->list); | |
60d53e2c | 1113 | list_add_tail_rcu(&link->list, &tp->event->files); |
747774d6 | 1114 | trace_probe_set_flag(tp, TP_FLAG_TRACE); |
b5f935ee MH |
1115 | return 0; |
1116 | } | |
1117 | ||
1118 | struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp, | |
1119 | struct trace_event_file *file) | |
1120 | { | |
1121 | struct event_file_link *link; | |
1122 | ||
1123 | trace_probe_for_each_link(link, tp) { | |
1124 | if (link->file == file) | |
1125 | return link; | |
1126 | } | |
1127 | ||
1128 | return NULL; | |
1129 | } | |
1130 | ||
1131 | int trace_probe_remove_file(struct trace_probe *tp, | |
1132 | struct trace_event_file *file) | |
1133 | { | |
1134 | struct event_file_link *link; | |
1135 | ||
1136 | link = trace_probe_get_file_link(tp, file); | |
1137 | if (!link) | |
1138 | return -ENOENT; | |
1139 | ||
1140 | list_del_rcu(&link->list); | |
1141 | synchronize_rcu(); | |
1142 | kfree(link); | |
1143 | ||
60d53e2c | 1144 | if (list_empty(&tp->event->files)) |
747774d6 | 1145 | trace_probe_clear_flag(tp, TP_FLAG_TRACE); |
b5f935ee MH |
1146 | |
1147 | return 0; | |
1148 | } | |
ca89bc07 MH |
1149 | |
1150 | /* | |
1151 | * Return the smallest index of different type argument (start from 1). | |
1152 | * If all argument types and name are same, return 0. | |
1153 | */ | |
1154 | int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b) | |
1155 | { | |
1156 | int i; | |
1157 | ||
d2aea95a MH |
1158 | /* In case of more arguments */ |
1159 | if (a->nr_args < b->nr_args) | |
1160 | return a->nr_args + 1; | |
1161 | if (a->nr_args > b->nr_args) | |
1162 | return b->nr_args + 1; | |
1163 | ||
ca89bc07 MH |
1164 | for (i = 0; i < a->nr_args; i++) { |
1165 | if ((b->nr_args <= i) || | |
1166 | ((a->args[i].type != b->args[i].type) || | |
1167 | (a->args[i].count != b->args[i].count) || | |
1168 | strcmp(a->args[i].name, b->args[i].name))) | |
1169 | return i + 1; | |
1170 | } | |
1171 | ||
1172 | return 0; | |
1173 | } | |
eb5bf813 MH |
1174 | |
1175 | bool trace_probe_match_command_args(struct trace_probe *tp, | |
1176 | int argc, const char **argv) | |
1177 | { | |
1178 | char buf[MAX_ARGSTR_LEN + 1]; | |
1179 | int i; | |
1180 | ||
1181 | if (tp->nr_args < argc) | |
1182 | return false; | |
1183 | ||
1184 | for (i = 0; i < argc; i++) { | |
1185 | snprintf(buf, sizeof(buf), "%s=%s", | |
1186 | tp->args[i].name, tp->args[i].comm); | |
1187 | if (strcmp(buf, argv[i])) | |
1188 | return false; | |
1189 | } | |
1190 | return true; | |
1191 | } | |
d262271d MH |
1192 | |
1193 | int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **)) | |
1194 | { | |
1195 | int argc = 0, ret = 0; | |
1196 | char **argv; | |
1197 | ||
1198 | argv = argv_split(GFP_KERNEL, raw_command, &argc); | |
1199 | if (!argv) | |
1200 | return -ENOMEM; | |
1201 | ||
1202 | if (argc) | |
1203 | ret = createfn(argc, (const char **)argv); | |
1204 | ||
1205 | argv_free(argv); | |
1206 | ||
1207 | return ret; | |
1208 | } |