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, '/'); | |
236 | if (slash) { | |
237 | if (slash == event) { | |
ab105a4f | 238 | trace_probe_log_err(offset, NO_GROUP_NAME); |
6212dd29 MH |
239 | return -EINVAL; |
240 | } | |
241 | if (slash - event + 1 > MAX_EVENT_NAME_LEN) { | |
ab105a4f MH |
242 | trace_probe_log_err(offset, GROUP_TOO_LONG); |
243 | return -EINVAL; | |
6212dd29 MH |
244 | } |
245 | strlcpy(buf, event, slash - event + 1); | |
5b7a9622 | 246 | if (!is_good_name(buf)) { |
ab105a4f | 247 | trace_probe_log_err(offset, BAD_GROUP_NAME); |
5b7a9622 MH |
248 | return -EINVAL; |
249 | } | |
6212dd29 MH |
250 | *pgroup = buf; |
251 | *pevent = slash + 1; | |
ab105a4f | 252 | offset += slash - event + 1; |
dec65d79 | 253 | event = *pevent; |
6212dd29 | 254 | } |
dec65d79 MH |
255 | len = strlen(event); |
256 | if (len == 0) { | |
ab105a4f | 257 | trace_probe_log_err(offset, NO_EVENT_NAME); |
6212dd29 | 258 | return -EINVAL; |
dec65d79 | 259 | } else if (len > MAX_EVENT_NAME_LEN) { |
ab105a4f MH |
260 | trace_probe_log_err(offset, EVENT_TOO_LONG); |
261 | return -EINVAL; | |
6212dd29 | 262 | } |
5b7a9622 | 263 | if (!is_good_name(event)) { |
ab105a4f | 264 | trace_probe_log_err(offset, BAD_EVENT_NAME); |
5b7a9622 MH |
265 | return -EINVAL; |
266 | } | |
6212dd29 MH |
267 | return 0; |
268 | } | |
269 | ||
8ab83f56 SD |
270 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) |
271 | ||
272 | static int parse_probe_vars(char *arg, const struct fetch_type *t, | |
ab105a4f | 273 | struct fetch_insn *code, unsigned int flags, int offs) |
8ab83f56 | 274 | { |
8ab83f56 | 275 | unsigned long param; |
3d739c1f SRV |
276 | int ret = 0; |
277 | int len; | |
8ab83f56 SD |
278 | |
279 | if (strcmp(arg, "retval") == 0) { | |
ab105a4f | 280 | if (flags & TPARG_FL_RETURN) { |
53305928 | 281 | code->op = FETCH_OP_RETVAL; |
ab105a4f MH |
282 | } else { |
283 | trace_probe_log_err(offs, RETVAL_ON_PROBE); | |
8ab83f56 | 284 | ret = -EINVAL; |
ab105a4f | 285 | } |
3d739c1f SRV |
286 | } else if ((len = str_has_prefix(arg, "stack"))) { |
287 | if (arg[len] == '\0') { | |
53305928 | 288 | code->op = FETCH_OP_STACKP; |
3d739c1f SRV |
289 | } else if (isdigit(arg[len])) { |
290 | ret = kstrtoul(arg + len, 10, ¶m); | |
ab105a4f MH |
291 | if (ret) { |
292 | goto inval_var; | |
293 | } else if ((flags & TPARG_FL_KERNEL) && | |
294 | param > PARAM_MAX_STACK) { | |
295 | trace_probe_log_err(offs, BAD_STACK_NUM); | |
8ab83f56 | 296 | ret = -EINVAL; |
ab105a4f | 297 | } else { |
53305928 MH |
298 | code->op = FETCH_OP_STACK; |
299 | code->param = (unsigned int)param; | |
8ab83f56 SD |
300 | } |
301 | } else | |
ab105a4f | 302 | goto inval_var; |
35abb67d | 303 | } else if (strcmp(arg, "comm") == 0) { |
53305928 | 304 | code->op = FETCH_OP_COMM; |
a1303af5 MH |
305 | #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API |
306 | } else if (((flags & TPARG_FL_MASK) == | |
307 | (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) && | |
3d739c1f | 308 | (len = str_has_prefix(arg, "arg"))) { |
3d739c1f | 309 | ret = kstrtoul(arg + len, 10, ¶m); |
ab105a4f MH |
310 | if (ret) { |
311 | goto inval_var; | |
312 | } else if (!param || param > PARAM_MAX_STACK) { | |
313 | trace_probe_log_err(offs, BAD_ARG_NUM); | |
a1303af5 | 314 | return -EINVAL; |
ab105a4f | 315 | } |
a1303af5 MH |
316 | code->op = FETCH_OP_ARG; |
317 | code->param = (unsigned int)param - 1; | |
318 | #endif | |
8ab83f56 | 319 | } else |
ab105a4f | 320 | goto inval_var; |
8ab83f56 SD |
321 | |
322 | return ret; | |
ab105a4f MH |
323 | |
324 | inval_var: | |
325 | trace_probe_log_err(offs, BAD_VAR); | |
326 | return -EINVAL; | |
8ab83f56 SD |
327 | } |
328 | ||
6218bf9f MH |
329 | static int str_to_immediate(char *str, unsigned long *imm) |
330 | { | |
331 | if (isdigit(str[0])) | |
332 | return kstrtoul(str, 0, imm); | |
333 | else if (str[0] == '-') | |
334 | return kstrtol(str, 0, (long *)imm); | |
335 | else if (str[0] == '+') | |
336 | return kstrtol(str + 1, 0, (long *)imm); | |
337 | return -EINVAL; | |
338 | } | |
339 | ||
a42e3c4d MH |
340 | static int __parse_imm_string(char *str, char **pbuf, int offs) |
341 | { | |
342 | size_t len = strlen(str); | |
343 | ||
344 | if (str[len - 1] != '"') { | |
345 | trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE); | |
346 | return -EINVAL; | |
347 | } | |
348 | *pbuf = kstrndup(str, len - 1, GFP_KERNEL); | |
349 | return 0; | |
350 | } | |
351 | ||
8ab83f56 | 352 | /* Recursive argument parser */ |
53305928 MH |
353 | static int |
354 | parse_probe_arg(char *arg, const struct fetch_type *type, | |
355 | struct fetch_insn **pcode, struct fetch_insn *end, | |
ab105a4f | 356 | unsigned int flags, int offs) |
8ab83f56 | 357 | { |
53305928 | 358 | struct fetch_insn *code = *pcode; |
8ab83f56 | 359 | unsigned long param; |
e65f7ae7 | 360 | int deref = FETCH_OP_DEREF; |
bf173ca9 | 361 | long offset = 0; |
8ab83f56 | 362 | char *tmp; |
34fee3a1 | 363 | int ret = 0; |
8ab83f56 | 364 | |
8ab83f56 SD |
365 | switch (arg[0]) { |
366 | case '$': | |
ab105a4f | 367 | ret = parse_probe_vars(arg + 1, type, code, flags, offs); |
8ab83f56 SD |
368 | break; |
369 | ||
370 | case '%': /* named register */ | |
371 | ret = regs_query_register_offset(arg + 1); | |
372 | if (ret >= 0) { | |
53305928 MH |
373 | code->op = FETCH_OP_REG; |
374 | code->param = (unsigned int)ret; | |
8ab83f56 | 375 | ret = 0; |
ab105a4f MH |
376 | } else |
377 | trace_probe_log_err(offs, BAD_REG_NAME); | |
8ab83f56 SD |
378 | break; |
379 | ||
b7e0bf34 | 380 | case '@': /* memory, file-offset or symbol */ |
8ab83f56 | 381 | if (isdigit(arg[1])) { |
bcd83ea6 | 382 | ret = kstrtoul(arg + 1, 0, ¶m); |
ab105a4f MH |
383 | if (ret) { |
384 | trace_probe_log_err(offs, BAD_MEM_ADDR); | |
8ab83f56 | 385 | break; |
ab105a4f | 386 | } |
53305928 MH |
387 | /* load address */ |
388 | code->op = FETCH_OP_IMM; | |
389 | code->immediate = param; | |
b7e0bf34 NK |
390 | } else if (arg[1] == '+') { |
391 | /* kprobes don't support file offsets */ | |
ab105a4f MH |
392 | if (flags & TPARG_FL_KERNEL) { |
393 | trace_probe_log_err(offs, FILE_ON_KPROBE); | |
b7e0bf34 | 394 | return -EINVAL; |
ab105a4f | 395 | } |
b7e0bf34 | 396 | ret = kstrtol(arg + 2, 0, &offset); |
ab105a4f MH |
397 | if (ret) { |
398 | trace_probe_log_err(offs, BAD_FILE_OFFS); | |
b7e0bf34 | 399 | break; |
ab105a4f | 400 | } |
b7e0bf34 | 401 | |
53305928 MH |
402 | code->op = FETCH_OP_FOFFS; |
403 | code->immediate = (unsigned long)offset; // imm64? | |
8ab83f56 | 404 | } else { |
b079d374 | 405 | /* uprobes don't support symbols */ |
ab105a4f MH |
406 | if (!(flags & TPARG_FL_KERNEL)) { |
407 | trace_probe_log_err(offs, SYM_ON_UPROBE); | |
b079d374 | 408 | return -EINVAL; |
ab105a4f | 409 | } |
a6682814 MH |
410 | /* Preserve symbol for updating */ |
411 | code->op = FETCH_NOP_SYMBOL; | |
412 | code->data = kstrdup(arg + 1, GFP_KERNEL); | |
413 | if (!code->data) | |
414 | return -ENOMEM; | |
ab105a4f MH |
415 | if (++code == end) { |
416 | trace_probe_log_err(offs, TOO_MANY_OPS); | |
417 | return -EINVAL; | |
418 | } | |
53305928 | 419 | code->op = FETCH_OP_IMM; |
a6682814 | 420 | code->immediate = 0; |
8ab83f56 | 421 | } |
53305928 | 422 | /* These are fetching from memory */ |
ab105a4f MH |
423 | if (++code == end) { |
424 | trace_probe_log_err(offs, TOO_MANY_OPS); | |
425 | return -EINVAL; | |
426 | } | |
53305928 MH |
427 | *pcode = code; |
428 | code->op = FETCH_OP_DEREF; | |
429 | code->offset = offset; | |
8ab83f56 SD |
430 | break; |
431 | ||
432 | case '+': /* deref memory */ | |
8ab83f56 | 433 | case '-': |
e65f7ae7 MH |
434 | if (arg[1] == 'u') { |
435 | deref = FETCH_OP_UDEREF; | |
436 | arg[1] = arg[0]; | |
437 | arg++; | |
438 | } | |
439 | if (arg[0] == '+') | |
440 | arg++; /* Skip '+', because kstrtol() rejects it. */ | |
8ab83f56 | 441 | tmp = strchr(arg, '('); |
ab105a4f MH |
442 | if (!tmp) { |
443 | trace_probe_log_err(offs, DEREF_NEED_BRACE); | |
53305928 | 444 | return -EINVAL; |
ab105a4f | 445 | } |
8ab83f56 | 446 | *tmp = '\0'; |
bcd83ea6 | 447 | ret = kstrtol(arg, 0, &offset); |
ab105a4f MH |
448 | if (ret) { |
449 | trace_probe_log_err(offs, BAD_DEREF_OFFS); | |
8ab83f56 | 450 | break; |
ab105a4f MH |
451 | } |
452 | offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0); | |
8ab83f56 SD |
453 | arg = tmp + 1; |
454 | tmp = strrchr(arg, ')'); | |
ab105a4f MH |
455 | if (!tmp) { |
456 | trace_probe_log_err(offs + strlen(arg), | |
457 | DEREF_OPEN_BRACE); | |
458 | return -EINVAL; | |
459 | } else { | |
f451bc89 | 460 | const struct fetch_type *t2 = find_fetch_type(NULL); |
8ab83f56 | 461 | |
8ab83f56 | 462 | *tmp = '\0'; |
ab105a4f | 463 | ret = parse_probe_arg(arg, t2, &code, end, flags, offs); |
8ab83f56 | 464 | if (ret) |
53305928 | 465 | break; |
a42e3c4d MH |
466 | if (code->op == FETCH_OP_COMM || |
467 | code->op == FETCH_OP_DATA) { | |
ab105a4f MH |
468 | trace_probe_log_err(offs, COMM_CANT_DEREF); |
469 | return -EINVAL; | |
470 | } | |
471 | if (++code == end) { | |
472 | trace_probe_log_err(offs, TOO_MANY_OPS); | |
53305928 | 473 | return -EINVAL; |
ab105a4f | 474 | } |
53305928 MH |
475 | *pcode = code; |
476 | ||
e65f7ae7 | 477 | code->op = deref; |
53305928 | 478 | code->offset = offset; |
8ab83f56 SD |
479 | } |
480 | break; | |
6218bf9f | 481 | case '\\': /* Immediate value */ |
a42e3c4d MH |
482 | if (arg[1] == '"') { /* Immediate string */ |
483 | ret = __parse_imm_string(arg + 2, &tmp, offs + 2); | |
484 | if (ret) | |
485 | break; | |
486 | code->op = FETCH_OP_DATA; | |
487 | code->data = tmp; | |
488 | } else { | |
489 | ret = str_to_immediate(arg + 1, &code->immediate); | |
490 | if (ret) | |
491 | trace_probe_log_err(offs + 1, BAD_IMM); | |
492 | else | |
493 | code->op = FETCH_OP_IMM; | |
494 | } | |
6218bf9f | 495 | break; |
8ab83f56 | 496 | } |
53305928 MH |
497 | if (!ret && code->op == FETCH_OP_NOP) { |
498 | /* Parsed, but do not find fetch method */ | |
ab105a4f | 499 | trace_probe_log_err(offs, BAD_FETCH_ARG); |
8ab83f56 SD |
500 | ret = -EINVAL; |
501 | } | |
8ab83f56 SD |
502 | return ret; |
503 | } | |
504 | ||
505 | #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long)) | |
506 | ||
507 | /* Bitfield type needs to be parsed into a fetch function */ | |
508 | static int __parse_bitfield_probe_arg(const char *bf, | |
509 | const struct fetch_type *t, | |
53305928 | 510 | struct fetch_insn **pcode) |
8ab83f56 | 511 | { |
53305928 | 512 | struct fetch_insn *code = *pcode; |
8ab83f56 SD |
513 | unsigned long bw, bo; |
514 | char *tail; | |
515 | ||
516 | if (*bf != 'b') | |
517 | return 0; | |
518 | ||
8ab83f56 SD |
519 | bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */ |
520 | ||
521 | if (bw == 0 || *tail != '@') | |
522 | return -EINVAL; | |
523 | ||
524 | bf = tail + 1; | |
525 | bo = simple_strtoul(bf, &tail, 0); | |
526 | ||
527 | if (tail == bf || *tail != '/') | |
528 | return -EINVAL; | |
53305928 MH |
529 | code++; |
530 | if (code->op != FETCH_OP_NOP) | |
ab105a4f | 531 | return -EINVAL; |
53305928 | 532 | *pcode = code; |
8ab83f56 | 533 | |
53305928 MH |
534 | code->op = FETCH_OP_MOD_BF; |
535 | code->lshift = BYTES_TO_BITS(t->size) - (bw + bo); | |
536 | code->rshift = BYTES_TO_BITS(t->size) - bw; | |
537 | code->basesize = t->size; | |
8ab83f56 SD |
538 | |
539 | return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0; | |
540 | } | |
541 | ||
542 | /* String length checking wrapper */ | |
fcd9db51 | 543 | static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size, |
ab105a4f | 544 | struct probe_arg *parg, unsigned int flags, int offset) |
8ab83f56 | 545 | { |
40b53b77 | 546 | struct fetch_insn *code, *scode, *tmp = NULL; |
ab105a4f | 547 | char *t, *t2, *t3; |
fcd9db51 | 548 | char *arg; |
40b53b77 | 549 | int ret, len; |
8ab83f56 | 550 | |
fcd9db51 SRV |
551 | arg = kstrdup(argv, GFP_KERNEL); |
552 | if (!arg) | |
553 | return -ENOMEM; | |
554 | ||
555 | ret = -EINVAL; | |
ab105a4f MH |
556 | len = strlen(arg); |
557 | if (len > MAX_ARGSTR_LEN) { | |
558 | trace_probe_log_err(offset, ARG_TOO_LONG); | |
fcd9db51 | 559 | goto out; |
ab105a4f MH |
560 | } else if (len == 0) { |
561 | trace_probe_log_err(offset, NO_ARG_BODY); | |
fcd9db51 | 562 | goto out; |
8ab83f56 | 563 | } |
ab105a4f | 564 | |
fcd9db51 | 565 | ret = -ENOMEM; |
8ab83f56 | 566 | parg->comm = kstrdup(arg, GFP_KERNEL); |
ab105a4f | 567 | if (!parg->comm) |
fcd9db51 | 568 | goto out; |
ab105a4f | 569 | |
fcd9db51 | 570 | ret = -EINVAL; |
40b53b77 | 571 | t = strchr(arg, ':'); |
8ab83f56 | 572 | if (t) { |
40b53b77 MH |
573 | *t = '\0'; |
574 | t2 = strchr(++t, '['); | |
575 | if (t2) { | |
ab105a4f MH |
576 | *t2++ = '\0'; |
577 | t3 = strchr(t2, ']'); | |
578 | if (!t3) { | |
579 | offset += t2 + strlen(t2) - arg; | |
580 | trace_probe_log_err(offset, | |
581 | ARRAY_NO_CLOSE); | |
fcd9db51 | 582 | goto out; |
ab105a4f MH |
583 | } else if (t3[1] != '\0') { |
584 | trace_probe_log_err(offset + t3 + 1 - arg, | |
585 | BAD_ARRAY_SUFFIX); | |
fcd9db51 | 586 | goto out; |
ab105a4f MH |
587 | } |
588 | *t3 = '\0'; | |
589 | if (kstrtouint(t2, 0, &parg->count) || !parg->count) { | |
590 | trace_probe_log_err(offset + t2 - arg, | |
591 | BAD_ARRAY_NUM); | |
fcd9db51 | 592 | goto out; |
ab105a4f MH |
593 | } |
594 | if (parg->count > MAX_ARRAY_LEN) { | |
595 | trace_probe_log_err(offset + t2 - arg, | |
596 | ARRAY_TOO_BIG); | |
fcd9db51 | 597 | goto out; |
ab105a4f | 598 | } |
40b53b77 | 599 | } |
8ab83f56 | 600 | } |
3dd1f7f2 | 601 | |
a42e3c4d | 602 | /* |
f2cc020d | 603 | * Since $comm and immediate string can not be dereferenced, |
a42e3c4d MH |
604 | * we can find those by strcmp. |
605 | */ | |
606 | if (strcmp(arg, "$comm") == 0 || strncmp(arg, "\\\"", 2) == 0) { | |
3dd1f7f2 MH |
607 | /* The type of $comm must be "string", and not an array. */ |
608 | if (parg->count || (t && strcmp(t, "string"))) | |
fcd9db51 | 609 | goto out; |
40b53b77 | 610 | parg->type = find_fetch_type("string"); |
3dd1f7f2 | 611 | } else |
40b53b77 | 612 | parg->type = find_fetch_type(t); |
8ab83f56 | 613 | if (!parg->type) { |
ab105a4f | 614 | trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE); |
fcd9db51 | 615 | goto out; |
8ab83f56 SD |
616 | } |
617 | parg->offset = *size; | |
40b53b77 MH |
618 | *size += parg->type->size * (parg->count ?: 1); |
619 | ||
fcd9db51 | 620 | ret = -ENOMEM; |
40b53b77 MH |
621 | if (parg->count) { |
622 | len = strlen(parg->type->fmttype) + 6; | |
623 | parg->fmt = kmalloc(len, GFP_KERNEL); | |
624 | if (!parg->fmt) | |
fcd9db51 | 625 | goto out; |
40b53b77 MH |
626 | snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype, |
627 | parg->count); | |
628 | } | |
8ab83f56 | 629 | |
8623b006 | 630 | code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL); |
53305928 | 631 | if (!code) |
fcd9db51 | 632 | goto out; |
53305928 MH |
633 | code[FETCH_INSN_MAX - 1].op = FETCH_OP_END; |
634 | ||
635 | ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1], | |
ab105a4f | 636 | flags, offset); |
53305928 MH |
637 | if (ret) |
638 | goto fail; | |
639 | ||
fcd9db51 | 640 | ret = -EINVAL; |
53305928 | 641 | /* Store operation */ |
88903c46 MH |
642 | if (!strcmp(parg->type->name, "string") || |
643 | !strcmp(parg->type->name, "ustring")) { | |
e65f7ae7 | 644 | if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF && |
a42e3c4d MH |
645 | code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM && |
646 | code->op != FETCH_OP_DATA) { | |
ab105a4f MH |
647 | trace_probe_log_err(offset + (t ? (t - arg) : 0), |
648 | BAD_STRING); | |
53305928 MH |
649 | goto fail; |
650 | } | |
3aa8fdc3 VS |
651 | if ((code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM || |
652 | code->op == FETCH_OP_DATA) || parg->count) { | |
40b53b77 | 653 | /* |
a42e3c4d MH |
654 | * IMM, DATA and COMM is pointing actual address, those |
655 | * must be kept, and if parg->count != 0, this is an | |
656 | * array of string pointers instead of string address | |
657 | * itself. | |
40b53b77 | 658 | */ |
53305928 | 659 | code++; |
40b53b77 | 660 | if (code->op != FETCH_OP_NOP) { |
ab105a4f | 661 | trace_probe_log_err(offset, TOO_MANY_OPS); |
40b53b77 MH |
662 | goto fail; |
663 | } | |
664 | } | |
88903c46 | 665 | /* If op == DEREF, replace it with STRING */ |
e65f7ae7 MH |
666 | if (!strcmp(parg->type->name, "ustring") || |
667 | code->op == FETCH_OP_UDEREF) | |
88903c46 MH |
668 | code->op = FETCH_OP_ST_USTRING; |
669 | else | |
670 | code->op = FETCH_OP_ST_STRING; | |
40b53b77 | 671 | code->size = parg->type->size; |
53305928 MH |
672 | parg->dynamic = true; |
673 | } else if (code->op == FETCH_OP_DEREF) { | |
674 | code->op = FETCH_OP_ST_MEM; | |
675 | code->size = parg->type->size; | |
e65f7ae7 MH |
676 | } else if (code->op == FETCH_OP_UDEREF) { |
677 | code->op = FETCH_OP_ST_UMEM; | |
678 | code->size = parg->type->size; | |
53305928 MH |
679 | } else { |
680 | code++; | |
681 | if (code->op != FETCH_OP_NOP) { | |
ab105a4f | 682 | trace_probe_log_err(offset, TOO_MANY_OPS); |
53305928 MH |
683 | goto fail; |
684 | } | |
685 | code->op = FETCH_OP_ST_RAW; | |
686 | code->size = parg->type->size; | |
687 | } | |
40b53b77 | 688 | scode = code; |
53305928 MH |
689 | /* Modify operation */ |
690 | if (t != NULL) { | |
691 | ret = __parse_bitfield_probe_arg(t, parg->type, &code); | |
ab105a4f MH |
692 | if (ret) { |
693 | trace_probe_log_err(offset + t - arg, BAD_BITFIELD); | |
53305928 | 694 | goto fail; |
ab105a4f | 695 | } |
8ab83f56 | 696 | } |
fcd9db51 | 697 | ret = -EINVAL; |
40b53b77 MH |
698 | /* Loop(Array) operation */ |
699 | if (parg->count) { | |
700 | if (scode->op != FETCH_OP_ST_MEM && | |
88903c46 MH |
701 | scode->op != FETCH_OP_ST_STRING && |
702 | scode->op != FETCH_OP_ST_USTRING) { | |
ab105a4f MH |
703 | trace_probe_log_err(offset + (t ? (t - arg) : 0), |
704 | BAD_STRING); | |
40b53b77 MH |
705 | goto fail; |
706 | } | |
707 | code++; | |
708 | if (code->op != FETCH_OP_NOP) { | |
ab105a4f | 709 | trace_probe_log_err(offset, TOO_MANY_OPS); |
40b53b77 MH |
710 | goto fail; |
711 | } | |
712 | code->op = FETCH_OP_LP_ARRAY; | |
713 | code->param = parg->count; | |
714 | } | |
53305928 MH |
715 | code++; |
716 | code->op = FETCH_OP_END; | |
717 | ||
fcd9db51 | 718 | ret = 0; |
53305928 | 719 | /* Shrink down the code buffer */ |
8623b006 | 720 | parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL); |
53305928 MH |
721 | if (!parg->code) |
722 | ret = -ENOMEM; | |
723 | else | |
724 | memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1)); | |
725 | ||
726 | fail: | |
a6682814 MH |
727 | if (ret) { |
728 | for (code = tmp; code < tmp + FETCH_INSN_MAX; code++) | |
a42e3c4d MH |
729 | if (code->op == FETCH_NOP_SYMBOL || |
730 | code->op == FETCH_OP_DATA) | |
a6682814 MH |
731 | kfree(code->data); |
732 | } | |
53305928 | 733 | kfree(tmp); |
fcd9db51 SRV |
734 | out: |
735 | kfree(arg); | |
8ab83f56 SD |
736 | |
737 | return ret; | |
738 | } | |
739 | ||
740 | /* Return 1 if name is reserved or already used by another argument */ | |
d00bbea9 MH |
741 | static int traceprobe_conflict_field_name(const char *name, |
742 | struct probe_arg *args, int narg) | |
8ab83f56 SD |
743 | { |
744 | int i; | |
745 | ||
746 | for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) | |
747 | if (strcmp(reserved_field_names[i], name) == 0) | |
748 | return 1; | |
749 | ||
750 | for (i = 0; i < narg; i++) | |
751 | if (strcmp(args[i].name, name) == 0) | |
752 | return 1; | |
753 | ||
754 | return 0; | |
755 | } | |
756 | ||
fcd9db51 | 757 | int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg, |
d00bbea9 MH |
758 | unsigned int flags) |
759 | { | |
760 | struct probe_arg *parg = &tp->args[i]; | |
fcd9db51 | 761 | const char *body; |
d00bbea9 MH |
762 | |
763 | /* Increment count for freeing args in error case */ | |
764 | tp->nr_args++; | |
765 | ||
766 | body = strchr(arg, '='); | |
767 | if (body) { | |
ab105a4f MH |
768 | if (body - arg > MAX_ARG_NAME_LEN) { |
769 | trace_probe_log_err(0, ARG_NAME_TOO_LONG); | |
770 | return -EINVAL; | |
771 | } else if (body == arg) { | |
772 | trace_probe_log_err(0, NO_ARG_NAME); | |
b4443c17 | 773 | return -EINVAL; |
ab105a4f | 774 | } |
d00bbea9 MH |
775 | parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL); |
776 | body++; | |
777 | } else { | |
778 | /* If argument name is omitted, set "argN" */ | |
779 | parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1); | |
780 | body = arg; | |
781 | } | |
782 | if (!parg->name) | |
783 | return -ENOMEM; | |
784 | ||
785 | if (!is_good_name(parg->name)) { | |
ab105a4f | 786 | trace_probe_log_err(0, BAD_ARG_NAME); |
d00bbea9 MH |
787 | return -EINVAL; |
788 | } | |
d00bbea9 | 789 | if (traceprobe_conflict_field_name(parg->name, tp->args, i)) { |
ab105a4f | 790 | trace_probe_log_err(0, USED_ARG_NAME); |
d00bbea9 MH |
791 | return -EINVAL; |
792 | } | |
d00bbea9 | 793 | /* Parse fetch argument */ |
ab105a4f MH |
794 | return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags, |
795 | body - arg); | |
d00bbea9 MH |
796 | } |
797 | ||
8ab83f56 SD |
798 | void traceprobe_free_probe_arg(struct probe_arg *arg) |
799 | { | |
a6682814 MH |
800 | struct fetch_insn *code = arg->code; |
801 | ||
802 | while (code && code->op != FETCH_OP_END) { | |
a42e3c4d MH |
803 | if (code->op == FETCH_NOP_SYMBOL || |
804 | code->op == FETCH_OP_DATA) | |
a6682814 MH |
805 | kfree(code->data); |
806 | code++; | |
807 | } | |
53305928 | 808 | kfree(arg->code); |
8ab83f56 SD |
809 | kfree(arg->name); |
810 | kfree(arg->comm); | |
40b53b77 | 811 | kfree(arg->fmt); |
8ab83f56 SD |
812 | } |
813 | ||
a6682814 MH |
814 | int traceprobe_update_arg(struct probe_arg *arg) |
815 | { | |
816 | struct fetch_insn *code = arg->code; | |
817 | long offset; | |
818 | char *tmp; | |
819 | char c; | |
820 | int ret = 0; | |
821 | ||
822 | while (code && code->op != FETCH_OP_END) { | |
823 | if (code->op == FETCH_NOP_SYMBOL) { | |
824 | if (code[1].op != FETCH_OP_IMM) | |
825 | return -EINVAL; | |
826 | ||
ee474b81 | 827 | tmp = strpbrk(code->data, "+-"); |
a6682814 MH |
828 | if (tmp) |
829 | c = *tmp; | |
830 | ret = traceprobe_split_symbol_offset(code->data, | |
831 | &offset); | |
832 | if (ret) | |
833 | return ret; | |
834 | ||
835 | code[1].immediate = | |
836 | (unsigned long)kallsyms_lookup_name(code->data); | |
837 | if (tmp) | |
838 | *tmp = c; | |
839 | if (!code[1].immediate) | |
840 | return -ENOENT; | |
841 | code[1].immediate += offset; | |
842 | } | |
843 | code++; | |
844 | } | |
845 | return 0; | |
846 | } | |
847 | ||
40b53b77 MH |
848 | /* When len=0, we just calculate the needed length */ |
849 | #define LEN_OR_ZERO (len ? len - pos : 0) | |
5bf652aa NK |
850 | static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, |
851 | bool is_return) | |
852 | { | |
40b53b77 MH |
853 | struct probe_arg *parg; |
854 | int i, j; | |
5bf652aa | 855 | int pos = 0; |
5bf652aa NK |
856 | const char *fmt, *arg; |
857 | ||
858 | if (!is_return) { | |
859 | fmt = "(%lx)"; | |
860 | arg = "REC->" FIELD_STRING_IP; | |
861 | } else { | |
862 | fmt = "(%lx <- %lx)"; | |
863 | arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; | |
864 | } | |
865 | ||
5bf652aa NK |
866 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); |
867 | ||
868 | for (i = 0; i < tp->nr_args; i++) { | |
40b53b77 MH |
869 | parg = tp->args + i; |
870 | pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name); | |
871 | if (parg->count) { | |
872 | pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s", | |
873 | parg->type->fmt); | |
874 | for (j = 1; j < parg->count; j++) | |
875 | pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s", | |
876 | parg->type->fmt); | |
877 | pos += snprintf(buf + pos, LEN_OR_ZERO, "}"); | |
878 | } else | |
879 | pos += snprintf(buf + pos, LEN_OR_ZERO, "%s", | |
880 | parg->type->fmt); | |
5bf652aa NK |
881 | } |
882 | ||
883 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); | |
884 | ||
885 | for (i = 0; i < tp->nr_args; i++) { | |
40b53b77 MH |
886 | parg = tp->args + i; |
887 | if (parg->count) { | |
20279420 SRV |
888 | if ((strcmp(parg->type->name, "string") == 0) || |
889 | (strcmp(parg->type->name, "ustring") == 0)) | |
40b53b77 MH |
890 | fmt = ", __get_str(%s[%d])"; |
891 | else | |
892 | fmt = ", REC->%s[%d]"; | |
893 | for (j = 0; j < parg->count; j++) | |
894 | pos += snprintf(buf + pos, LEN_OR_ZERO, | |
895 | fmt, parg->name, j); | |
896 | } else { | |
20279420 SRV |
897 | if ((strcmp(parg->type->name, "string") == 0) || |
898 | (strcmp(parg->type->name, "ustring") == 0)) | |
40b53b77 MH |
899 | fmt = ", __get_str(%s)"; |
900 | else | |
901 | fmt = ", REC->%s"; | |
5bf652aa | 902 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
40b53b77 MH |
903 | fmt, parg->name); |
904 | } | |
5bf652aa NK |
905 | } |
906 | ||
5bf652aa NK |
907 | /* return the length of print_fmt */ |
908 | return pos; | |
909 | } | |
40b53b77 | 910 | #undef LEN_OR_ZERO |
5bf652aa | 911 | |
0a46c854 | 912 | int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return) |
5bf652aa | 913 | { |
e3dc9f89 | 914 | struct trace_event_call *call = trace_probe_event_call(tp); |
5bf652aa NK |
915 | int len; |
916 | char *print_fmt; | |
917 | ||
918 | /* First: called with 0 length to calculate the needed length */ | |
919 | len = __set_print_fmt(tp, NULL, 0, is_return); | |
920 | print_fmt = kmalloc(len + 1, GFP_KERNEL); | |
921 | if (!print_fmt) | |
922 | return -ENOMEM; | |
923 | ||
924 | /* Second: actually write the @print_fmt */ | |
925 | __set_print_fmt(tp, print_fmt, len + 1, is_return); | |
e3dc9f89 | 926 | call->print_fmt = print_fmt; |
5bf652aa NK |
927 | |
928 | return 0; | |
929 | } | |
eeb07b06 MH |
930 | |
931 | int traceprobe_define_arg_fields(struct trace_event_call *event_call, | |
932 | size_t offset, struct trace_probe *tp) | |
933 | { | |
934 | int ret, i; | |
935 | ||
936 | /* Set argument names as fields */ | |
937 | for (i = 0; i < tp->nr_args; i++) { | |
938 | struct probe_arg *parg = &tp->args[i]; | |
40b53b77 MH |
939 | const char *fmt = parg->type->fmttype; |
940 | int size = parg->type->size; | |
941 | ||
942 | if (parg->fmt) | |
943 | fmt = parg->fmt; | |
944 | if (parg->count) | |
945 | size *= parg->count; | |
946 | ret = trace_define_field(event_call, fmt, parg->name, | |
947 | offset + parg->offset, size, | |
eeb07b06 MH |
948 | parg->type->is_signed, |
949 | FILTER_OTHER); | |
950 | if (ret) | |
951 | return ret; | |
952 | } | |
953 | return 0; | |
954 | } | |
455b2899 | 955 | |
ca89bc07 MH |
956 | static void trace_probe_event_free(struct trace_probe_event *tpe) |
957 | { | |
958 | kfree(tpe->class.system); | |
959 | kfree(tpe->call.name); | |
960 | kfree(tpe->call.print_fmt); | |
961 | kfree(tpe); | |
962 | } | |
963 | ||
964 | int trace_probe_append(struct trace_probe *tp, struct trace_probe *to) | |
965 | { | |
966 | if (trace_probe_has_sibling(tp)) | |
967 | return -EBUSY; | |
968 | ||
969 | list_del_init(&tp->list); | |
970 | trace_probe_event_free(tp->event); | |
971 | ||
972 | tp->event = to->event; | |
973 | list_add_tail(&tp->list, trace_probe_probe_list(to)); | |
974 | ||
975 | return 0; | |
976 | } | |
977 | ||
978 | void trace_probe_unlink(struct trace_probe *tp) | |
979 | { | |
980 | list_del_init(&tp->list); | |
981 | if (list_empty(trace_probe_probe_list(tp))) | |
982 | trace_probe_event_free(tp->event); | |
983 | tp->event = NULL; | |
984 | } | |
455b2899 MH |
985 | |
986 | void trace_probe_cleanup(struct trace_probe *tp) | |
987 | { | |
988 | int i; | |
989 | ||
990 | for (i = 0; i < tp->nr_args; i++) | |
991 | traceprobe_free_probe_arg(&tp->args[i]); | |
992 | ||
ca89bc07 MH |
993 | if (tp->event) |
994 | trace_probe_unlink(tp); | |
455b2899 MH |
995 | } |
996 | ||
997 | int trace_probe_init(struct trace_probe *tp, const char *event, | |
b61387cb | 998 | const char *group, bool alloc_filter) |
455b2899 | 999 | { |
60d53e2c | 1000 | struct trace_event_call *call; |
b61387cb | 1001 | size_t size = sizeof(struct trace_probe_event); |
60d53e2c | 1002 | int ret = 0; |
e3dc9f89 | 1003 | |
455b2899 MH |
1004 | if (!event || !group) |
1005 | return -EINVAL; | |
1006 | ||
b61387cb MH |
1007 | if (alloc_filter) |
1008 | size += sizeof(struct trace_uprobe_filter); | |
1009 | ||
1010 | tp->event = kzalloc(size, GFP_KERNEL); | |
60d53e2c | 1011 | if (!tp->event) |
455b2899 MH |
1012 | return -ENOMEM; |
1013 | ||
d59fae6f MH |
1014 | INIT_LIST_HEAD(&tp->event->files); |
1015 | INIT_LIST_HEAD(&tp->event->class.fields); | |
1016 | INIT_LIST_HEAD(&tp->event->probes); | |
1017 | INIT_LIST_HEAD(&tp->list); | |
fc9d276f | 1018 | list_add(&tp->list, &tp->event->probes); |
d59fae6f | 1019 | |
60d53e2c MH |
1020 | call = trace_probe_event_call(tp); |
1021 | call->class = &tp->event->class; | |
1022 | call->name = kstrdup(event, GFP_KERNEL); | |
1023 | if (!call->name) { | |
1024 | ret = -ENOMEM; | |
1025 | goto error; | |
1026 | } | |
1027 | ||
1028 | tp->event->class.system = kstrdup(group, GFP_KERNEL); | |
1029 | if (!tp->event->class.system) { | |
1030 | ret = -ENOMEM; | |
1031 | goto error; | |
455b2899 | 1032 | } |
455b2899 MH |
1033 | |
1034 | return 0; | |
60d53e2c MH |
1035 | |
1036 | error: | |
1037 | trace_probe_cleanup(tp); | |
1038 | return ret; | |
455b2899 | 1039 | } |
46e5376d MH |
1040 | |
1041 | int trace_probe_register_event_call(struct trace_probe *tp) | |
1042 | { | |
e3dc9f89 | 1043 | struct trace_event_call *call = trace_probe_event_call(tp); |
46e5376d MH |
1044 | int ret; |
1045 | ||
1046 | ret = register_trace_event(&call->event); | |
1047 | if (!ret) | |
1048 | return -ENODEV; | |
1049 | ||
1050 | ret = trace_add_event_call(call); | |
1051 | if (ret) | |
1052 | unregister_trace_event(&call->event); | |
1053 | ||
1054 | return ret; | |
1055 | } | |
b5f935ee MH |
1056 | |
1057 | int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file) | |
1058 | { | |
1059 | struct event_file_link *link; | |
1060 | ||
1061 | link = kmalloc(sizeof(*link), GFP_KERNEL); | |
1062 | if (!link) | |
1063 | return -ENOMEM; | |
1064 | ||
1065 | link->file = file; | |
1066 | INIT_LIST_HEAD(&link->list); | |
60d53e2c | 1067 | list_add_tail_rcu(&link->list, &tp->event->files); |
747774d6 | 1068 | trace_probe_set_flag(tp, TP_FLAG_TRACE); |
b5f935ee MH |
1069 | return 0; |
1070 | } | |
1071 | ||
1072 | struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp, | |
1073 | struct trace_event_file *file) | |
1074 | { | |
1075 | struct event_file_link *link; | |
1076 | ||
1077 | trace_probe_for_each_link(link, tp) { | |
1078 | if (link->file == file) | |
1079 | return link; | |
1080 | } | |
1081 | ||
1082 | return NULL; | |
1083 | } | |
1084 | ||
1085 | int trace_probe_remove_file(struct trace_probe *tp, | |
1086 | struct trace_event_file *file) | |
1087 | { | |
1088 | struct event_file_link *link; | |
1089 | ||
1090 | link = trace_probe_get_file_link(tp, file); | |
1091 | if (!link) | |
1092 | return -ENOENT; | |
1093 | ||
1094 | list_del_rcu(&link->list); | |
1095 | synchronize_rcu(); | |
1096 | kfree(link); | |
1097 | ||
60d53e2c | 1098 | if (list_empty(&tp->event->files)) |
747774d6 | 1099 | trace_probe_clear_flag(tp, TP_FLAG_TRACE); |
b5f935ee MH |
1100 | |
1101 | return 0; | |
1102 | } | |
ca89bc07 MH |
1103 | |
1104 | /* | |
1105 | * Return the smallest index of different type argument (start from 1). | |
1106 | * If all argument types and name are same, return 0. | |
1107 | */ | |
1108 | int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b) | |
1109 | { | |
1110 | int i; | |
1111 | ||
d2aea95a MH |
1112 | /* In case of more arguments */ |
1113 | if (a->nr_args < b->nr_args) | |
1114 | return a->nr_args + 1; | |
1115 | if (a->nr_args > b->nr_args) | |
1116 | return b->nr_args + 1; | |
1117 | ||
ca89bc07 MH |
1118 | for (i = 0; i < a->nr_args; i++) { |
1119 | if ((b->nr_args <= i) || | |
1120 | ((a->args[i].type != b->args[i].type) || | |
1121 | (a->args[i].count != b->args[i].count) || | |
1122 | strcmp(a->args[i].name, b->args[i].name))) | |
1123 | return i + 1; | |
1124 | } | |
1125 | ||
1126 | return 0; | |
1127 | } | |
eb5bf813 MH |
1128 | |
1129 | bool trace_probe_match_command_args(struct trace_probe *tp, | |
1130 | int argc, const char **argv) | |
1131 | { | |
1132 | char buf[MAX_ARGSTR_LEN + 1]; | |
1133 | int i; | |
1134 | ||
1135 | if (tp->nr_args < argc) | |
1136 | return false; | |
1137 | ||
1138 | for (i = 0; i < argc; i++) { | |
1139 | snprintf(buf, sizeof(buf), "%s=%s", | |
1140 | tp->args[i].name, tp->args[i].comm); | |
1141 | if (strcmp(buf, argv[i])) | |
1142 | return false; | |
1143 | } | |
1144 | return true; | |
1145 | } | |
d262271d MH |
1146 | |
1147 | int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **)) | |
1148 | { | |
1149 | int argc = 0, ret = 0; | |
1150 | char **argv; | |
1151 | ||
1152 | argv = argv_split(GFP_KERNEL, raw_command, &argc); | |
1153 | if (!argv) | |
1154 | return -ENOMEM; | |
1155 | ||
1156 | if (argc) | |
1157 | ret = createfn(argc, (const char **)argv); | |
1158 | ||
1159 | argv_free(argv); | |
1160 | ||
1161 | return ret; | |
1162 | } |