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[]"), | |
81 | /* Basic types */ | |
82 | ASSIGN_FETCH_TYPE(u8, u8, 0), | |
83 | ASSIGN_FETCH_TYPE(u16, u16, 0), | |
84 | ASSIGN_FETCH_TYPE(u32, u32, 0), | |
85 | ASSIGN_FETCH_TYPE(u64, u64, 0), | |
86 | ASSIGN_FETCH_TYPE(s8, u8, 1), | |
87 | ASSIGN_FETCH_TYPE(s16, u16, 1), | |
88 | ASSIGN_FETCH_TYPE(s32, u32, 1), | |
89 | ASSIGN_FETCH_TYPE(s64, u64, 1), | |
90 | ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0), | |
91 | ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0), | |
92 | ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0), | |
93 | ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0), | |
60c2e0ce | 94 | ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0), |
f451bc89 MH |
95 | |
96 | ASSIGN_FETCH_TYPE_END | |
97 | }; | |
98 | ||
99 | static const struct fetch_type *find_fetch_type(const char *type) | |
8ab83f56 SD |
100 | { |
101 | int i; | |
102 | ||
103 | if (!type) | |
104 | type = DEFAULT_FETCH_TYPE_STR; | |
105 | ||
106 | /* Special case: bitfield */ | |
107 | if (*type == 'b') { | |
108 | unsigned long bs; | |
109 | ||
110 | type = strchr(type, '/'); | |
111 | if (!type) | |
112 | goto fail; | |
113 | ||
114 | type++; | |
bcd83ea6 | 115 | if (kstrtoul(type, 0, &bs)) |
8ab83f56 SD |
116 | goto fail; |
117 | ||
118 | switch (bs) { | |
119 | case 8: | |
f451bc89 | 120 | return find_fetch_type("u8"); |
8ab83f56 | 121 | case 16: |
f451bc89 | 122 | return find_fetch_type("u16"); |
8ab83f56 | 123 | case 32: |
f451bc89 | 124 | return find_fetch_type("u32"); |
8ab83f56 | 125 | case 64: |
f451bc89 | 126 | return find_fetch_type("u64"); |
8ab83f56 SD |
127 | default: |
128 | goto fail; | |
129 | } | |
130 | } | |
131 | ||
f451bc89 MH |
132 | for (i = 0; probe_fetch_types[i].name; i++) { |
133 | if (strcmp(type, probe_fetch_types[i].name) == 0) | |
134 | return &probe_fetch_types[i]; | |
34fee3a1 | 135 | } |
8ab83f56 SD |
136 | |
137 | fail: | |
138 | return NULL; | |
139 | } | |
140 | ||
ab105a4f MH |
141 | static struct trace_probe_log trace_probe_log; |
142 | ||
143 | void trace_probe_log_init(const char *subsystem, int argc, const char **argv) | |
144 | { | |
145 | trace_probe_log.subsystem = subsystem; | |
146 | trace_probe_log.argc = argc; | |
147 | trace_probe_log.argv = argv; | |
148 | trace_probe_log.index = 0; | |
149 | } | |
150 | ||
151 | void trace_probe_log_clear(void) | |
152 | { | |
153 | memset(&trace_probe_log, 0, sizeof(trace_probe_log)); | |
154 | } | |
155 | ||
156 | void trace_probe_log_set_index(int index) | |
157 | { | |
158 | trace_probe_log.index = index; | |
159 | } | |
160 | ||
161 | void __trace_probe_log_err(int offset, int err_type) | |
162 | { | |
163 | char *command, *p; | |
164 | int i, len = 0, pos = 0; | |
165 | ||
166 | if (!trace_probe_log.argv) | |
167 | return; | |
168 | ||
169 | /* Recalcurate the length and allocate buffer */ | |
170 | for (i = 0; i < trace_probe_log.argc; i++) { | |
171 | if (i == trace_probe_log.index) | |
172 | pos = len; | |
173 | len += strlen(trace_probe_log.argv[i]) + 1; | |
174 | } | |
175 | command = kzalloc(len, GFP_KERNEL); | |
176 | if (!command) | |
177 | return; | |
178 | ||
179 | /* And make a command string from argv array */ | |
180 | p = command; | |
181 | for (i = 0; i < trace_probe_log.argc; i++) { | |
182 | len = strlen(trace_probe_log.argv[i]); | |
183 | strcpy(p, trace_probe_log.argv[i]); | |
184 | p[len] = ' '; | |
185 | p += len + 1; | |
186 | } | |
187 | *(p - 1) = '\0'; | |
188 | ||
2f754e77 | 189 | tracing_log_err(NULL, trace_probe_log.subsystem, command, |
ab105a4f MH |
190 | trace_probe_err_text, err_type, pos + offset); |
191 | ||
192 | kfree(command); | |
193 | } | |
194 | ||
8ab83f56 | 195 | /* Split symbol and offset. */ |
c5d343b6 | 196 | int traceprobe_split_symbol_offset(char *symbol, long *offset) |
8ab83f56 SD |
197 | { |
198 | char *tmp; | |
199 | int ret; | |
200 | ||
201 | if (!offset) | |
202 | return -EINVAL; | |
203 | ||
c5d343b6 | 204 | tmp = strpbrk(symbol, "+-"); |
8ab83f56 | 205 | if (tmp) { |
c5d343b6 | 206 | ret = kstrtol(tmp, 0, offset); |
8ab83f56 SD |
207 | if (ret) |
208 | return ret; | |
8ab83f56 SD |
209 | *tmp = '\0'; |
210 | } else | |
211 | *offset = 0; | |
212 | ||
213 | return 0; | |
214 | } | |
215 | ||
6212dd29 MH |
216 | /* @buf must has MAX_EVENT_NAME_LEN size */ |
217 | int traceprobe_parse_event_name(const char **pevent, const char **pgroup, | |
ab105a4f | 218 | char *buf, int offset) |
6212dd29 MH |
219 | { |
220 | const char *slash, *event = *pevent; | |
dec65d79 | 221 | int len; |
6212dd29 MH |
222 | |
223 | slash = strchr(event, '/'); | |
224 | if (slash) { | |
225 | if (slash == event) { | |
ab105a4f | 226 | trace_probe_log_err(offset, NO_GROUP_NAME); |
6212dd29 MH |
227 | return -EINVAL; |
228 | } | |
229 | if (slash - event + 1 > MAX_EVENT_NAME_LEN) { | |
ab105a4f MH |
230 | trace_probe_log_err(offset, GROUP_TOO_LONG); |
231 | return -EINVAL; | |
6212dd29 MH |
232 | } |
233 | strlcpy(buf, event, slash - event + 1); | |
5b7a9622 | 234 | if (!is_good_name(buf)) { |
ab105a4f | 235 | trace_probe_log_err(offset, BAD_GROUP_NAME); |
5b7a9622 MH |
236 | return -EINVAL; |
237 | } | |
6212dd29 MH |
238 | *pgroup = buf; |
239 | *pevent = slash + 1; | |
ab105a4f | 240 | offset += slash - event + 1; |
dec65d79 | 241 | event = *pevent; |
6212dd29 | 242 | } |
dec65d79 MH |
243 | len = strlen(event); |
244 | if (len == 0) { | |
ab105a4f | 245 | trace_probe_log_err(offset, NO_EVENT_NAME); |
6212dd29 | 246 | return -EINVAL; |
dec65d79 | 247 | } else if (len > MAX_EVENT_NAME_LEN) { |
ab105a4f MH |
248 | trace_probe_log_err(offset, EVENT_TOO_LONG); |
249 | return -EINVAL; | |
6212dd29 | 250 | } |
5b7a9622 | 251 | if (!is_good_name(event)) { |
ab105a4f | 252 | trace_probe_log_err(offset, BAD_EVENT_NAME); |
5b7a9622 MH |
253 | return -EINVAL; |
254 | } | |
6212dd29 MH |
255 | return 0; |
256 | } | |
257 | ||
8ab83f56 SD |
258 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) |
259 | ||
260 | static int parse_probe_vars(char *arg, const struct fetch_type *t, | |
ab105a4f | 261 | struct fetch_insn *code, unsigned int flags, int offs) |
8ab83f56 | 262 | { |
8ab83f56 | 263 | unsigned long param; |
3d739c1f SRV |
264 | int ret = 0; |
265 | int len; | |
8ab83f56 SD |
266 | |
267 | if (strcmp(arg, "retval") == 0) { | |
ab105a4f | 268 | if (flags & TPARG_FL_RETURN) { |
53305928 | 269 | code->op = FETCH_OP_RETVAL; |
ab105a4f MH |
270 | } else { |
271 | trace_probe_log_err(offs, RETVAL_ON_PROBE); | |
8ab83f56 | 272 | ret = -EINVAL; |
ab105a4f | 273 | } |
3d739c1f SRV |
274 | } else if ((len = str_has_prefix(arg, "stack"))) { |
275 | if (arg[len] == '\0') { | |
53305928 | 276 | code->op = FETCH_OP_STACKP; |
3d739c1f SRV |
277 | } else if (isdigit(arg[len])) { |
278 | ret = kstrtoul(arg + len, 10, ¶m); | |
ab105a4f MH |
279 | if (ret) { |
280 | goto inval_var; | |
281 | } else if ((flags & TPARG_FL_KERNEL) && | |
282 | param > PARAM_MAX_STACK) { | |
283 | trace_probe_log_err(offs, BAD_STACK_NUM); | |
8ab83f56 | 284 | ret = -EINVAL; |
ab105a4f | 285 | } else { |
53305928 MH |
286 | code->op = FETCH_OP_STACK; |
287 | code->param = (unsigned int)param; | |
8ab83f56 SD |
288 | } |
289 | } else | |
ab105a4f | 290 | goto inval_var; |
35abb67d | 291 | } else if (strcmp(arg, "comm") == 0) { |
53305928 | 292 | code->op = FETCH_OP_COMM; |
a1303af5 MH |
293 | #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API |
294 | } else if (((flags & TPARG_FL_MASK) == | |
295 | (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) && | |
3d739c1f | 296 | (len = str_has_prefix(arg, "arg"))) { |
3d739c1f | 297 | ret = kstrtoul(arg + len, 10, ¶m); |
ab105a4f MH |
298 | if (ret) { |
299 | goto inval_var; | |
300 | } else if (!param || param > PARAM_MAX_STACK) { | |
301 | trace_probe_log_err(offs, BAD_ARG_NUM); | |
a1303af5 | 302 | return -EINVAL; |
ab105a4f | 303 | } |
a1303af5 MH |
304 | code->op = FETCH_OP_ARG; |
305 | code->param = (unsigned int)param - 1; | |
306 | #endif | |
8ab83f56 | 307 | } else |
ab105a4f | 308 | goto inval_var; |
8ab83f56 SD |
309 | |
310 | return ret; | |
ab105a4f MH |
311 | |
312 | inval_var: | |
313 | trace_probe_log_err(offs, BAD_VAR); | |
314 | return -EINVAL; | |
8ab83f56 SD |
315 | } |
316 | ||
317 | /* Recursive argument parser */ | |
53305928 MH |
318 | static int |
319 | parse_probe_arg(char *arg, const struct fetch_type *type, | |
320 | struct fetch_insn **pcode, struct fetch_insn *end, | |
ab105a4f | 321 | unsigned int flags, int offs) |
8ab83f56 | 322 | { |
53305928 | 323 | struct fetch_insn *code = *pcode; |
8ab83f56 | 324 | unsigned long param; |
bf173ca9 | 325 | long offset = 0; |
8ab83f56 | 326 | char *tmp; |
34fee3a1 | 327 | int ret = 0; |
8ab83f56 | 328 | |
8ab83f56 SD |
329 | switch (arg[0]) { |
330 | case '$': | |
ab105a4f | 331 | ret = parse_probe_vars(arg + 1, type, code, flags, offs); |
8ab83f56 SD |
332 | break; |
333 | ||
334 | case '%': /* named register */ | |
335 | ret = regs_query_register_offset(arg + 1); | |
336 | if (ret >= 0) { | |
53305928 MH |
337 | code->op = FETCH_OP_REG; |
338 | code->param = (unsigned int)ret; | |
8ab83f56 | 339 | ret = 0; |
ab105a4f MH |
340 | } else |
341 | trace_probe_log_err(offs, BAD_REG_NAME); | |
8ab83f56 SD |
342 | break; |
343 | ||
b7e0bf34 | 344 | case '@': /* memory, file-offset or symbol */ |
8ab83f56 | 345 | if (isdigit(arg[1])) { |
bcd83ea6 | 346 | ret = kstrtoul(arg + 1, 0, ¶m); |
ab105a4f MH |
347 | if (ret) { |
348 | trace_probe_log_err(offs, BAD_MEM_ADDR); | |
8ab83f56 | 349 | break; |
ab105a4f | 350 | } |
53305928 MH |
351 | /* load address */ |
352 | code->op = FETCH_OP_IMM; | |
353 | code->immediate = param; | |
b7e0bf34 NK |
354 | } else if (arg[1] == '+') { |
355 | /* kprobes don't support file offsets */ | |
ab105a4f MH |
356 | if (flags & TPARG_FL_KERNEL) { |
357 | trace_probe_log_err(offs, FILE_ON_KPROBE); | |
b7e0bf34 | 358 | return -EINVAL; |
ab105a4f | 359 | } |
b7e0bf34 | 360 | ret = kstrtol(arg + 2, 0, &offset); |
ab105a4f MH |
361 | if (ret) { |
362 | trace_probe_log_err(offs, BAD_FILE_OFFS); | |
b7e0bf34 | 363 | break; |
ab105a4f | 364 | } |
b7e0bf34 | 365 | |
53305928 MH |
366 | code->op = FETCH_OP_FOFFS; |
367 | code->immediate = (unsigned long)offset; // imm64? | |
8ab83f56 | 368 | } else { |
b079d374 | 369 | /* uprobes don't support symbols */ |
ab105a4f MH |
370 | if (!(flags & TPARG_FL_KERNEL)) { |
371 | trace_probe_log_err(offs, SYM_ON_UPROBE); | |
b079d374 | 372 | return -EINVAL; |
ab105a4f | 373 | } |
a6682814 MH |
374 | /* Preserve symbol for updating */ |
375 | code->op = FETCH_NOP_SYMBOL; | |
376 | code->data = kstrdup(arg + 1, GFP_KERNEL); | |
377 | if (!code->data) | |
378 | return -ENOMEM; | |
ab105a4f MH |
379 | if (++code == end) { |
380 | trace_probe_log_err(offs, TOO_MANY_OPS); | |
381 | return -EINVAL; | |
382 | } | |
53305928 | 383 | code->op = FETCH_OP_IMM; |
a6682814 | 384 | code->immediate = 0; |
8ab83f56 | 385 | } |
53305928 | 386 | /* These are fetching from memory */ |
ab105a4f MH |
387 | if (++code == end) { |
388 | trace_probe_log_err(offs, TOO_MANY_OPS); | |
389 | return -EINVAL; | |
390 | } | |
53305928 MH |
391 | *pcode = code; |
392 | code->op = FETCH_OP_DEREF; | |
393 | code->offset = offset; | |
8ab83f56 SD |
394 | break; |
395 | ||
396 | case '+': /* deref memory */ | |
bcd83ea6 | 397 | arg++; /* Skip '+', because kstrtol() rejects it. */ |
91457c01 | 398 | /* fall through */ |
8ab83f56 SD |
399 | case '-': |
400 | tmp = strchr(arg, '('); | |
ab105a4f MH |
401 | if (!tmp) { |
402 | trace_probe_log_err(offs, DEREF_NEED_BRACE); | |
53305928 | 403 | return -EINVAL; |
ab105a4f | 404 | } |
8ab83f56 | 405 | *tmp = '\0'; |
bcd83ea6 | 406 | ret = kstrtol(arg, 0, &offset); |
ab105a4f MH |
407 | if (ret) { |
408 | trace_probe_log_err(offs, BAD_DEREF_OFFS); | |
8ab83f56 | 409 | break; |
ab105a4f MH |
410 | } |
411 | offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0); | |
8ab83f56 SD |
412 | arg = tmp + 1; |
413 | tmp = strrchr(arg, ')'); | |
ab105a4f MH |
414 | if (!tmp) { |
415 | trace_probe_log_err(offs + strlen(arg), | |
416 | DEREF_OPEN_BRACE); | |
417 | return -EINVAL; | |
418 | } else { | |
f451bc89 | 419 | const struct fetch_type *t2 = find_fetch_type(NULL); |
8ab83f56 | 420 | |
8ab83f56 | 421 | *tmp = '\0'; |
ab105a4f | 422 | ret = parse_probe_arg(arg, t2, &code, end, flags, offs); |
8ab83f56 | 423 | if (ret) |
53305928 | 424 | break; |
ab105a4f MH |
425 | if (code->op == FETCH_OP_COMM) { |
426 | trace_probe_log_err(offs, COMM_CANT_DEREF); | |
427 | return -EINVAL; | |
428 | } | |
429 | if (++code == end) { | |
430 | trace_probe_log_err(offs, TOO_MANY_OPS); | |
53305928 | 431 | return -EINVAL; |
ab105a4f | 432 | } |
53305928 MH |
433 | *pcode = code; |
434 | ||
435 | code->op = FETCH_OP_DEREF; | |
436 | code->offset = offset; | |
8ab83f56 SD |
437 | } |
438 | break; | |
439 | } | |
53305928 MH |
440 | if (!ret && code->op == FETCH_OP_NOP) { |
441 | /* Parsed, but do not find fetch method */ | |
ab105a4f | 442 | trace_probe_log_err(offs, BAD_FETCH_ARG); |
8ab83f56 SD |
443 | ret = -EINVAL; |
444 | } | |
8ab83f56 SD |
445 | return ret; |
446 | } | |
447 | ||
448 | #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long)) | |
449 | ||
450 | /* Bitfield type needs to be parsed into a fetch function */ | |
451 | static int __parse_bitfield_probe_arg(const char *bf, | |
452 | const struct fetch_type *t, | |
53305928 | 453 | struct fetch_insn **pcode) |
8ab83f56 | 454 | { |
53305928 | 455 | struct fetch_insn *code = *pcode; |
8ab83f56 SD |
456 | unsigned long bw, bo; |
457 | char *tail; | |
458 | ||
459 | if (*bf != 'b') | |
460 | return 0; | |
461 | ||
8ab83f56 SD |
462 | bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */ |
463 | ||
464 | if (bw == 0 || *tail != '@') | |
465 | return -EINVAL; | |
466 | ||
467 | bf = tail + 1; | |
468 | bo = simple_strtoul(bf, &tail, 0); | |
469 | ||
470 | if (tail == bf || *tail != '/') | |
471 | return -EINVAL; | |
53305928 MH |
472 | code++; |
473 | if (code->op != FETCH_OP_NOP) | |
ab105a4f | 474 | return -EINVAL; |
53305928 | 475 | *pcode = code; |
8ab83f56 | 476 | |
53305928 MH |
477 | code->op = FETCH_OP_MOD_BF; |
478 | code->lshift = BYTES_TO_BITS(t->size) - (bw + bo); | |
479 | code->rshift = BYTES_TO_BITS(t->size) - bw; | |
480 | code->basesize = t->size; | |
8ab83f56 SD |
481 | |
482 | return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0; | |
483 | } | |
484 | ||
485 | /* String length checking wrapper */ | |
d00bbea9 | 486 | static int traceprobe_parse_probe_arg_body(char *arg, ssize_t *size, |
ab105a4f | 487 | struct probe_arg *parg, unsigned int flags, int offset) |
8ab83f56 | 488 | { |
40b53b77 | 489 | struct fetch_insn *code, *scode, *tmp = NULL; |
ab105a4f | 490 | char *t, *t2, *t3; |
40b53b77 | 491 | int ret, len; |
8ab83f56 | 492 | |
ab105a4f MH |
493 | len = strlen(arg); |
494 | if (len > MAX_ARGSTR_LEN) { | |
495 | trace_probe_log_err(offset, ARG_TOO_LONG); | |
496 | return -EINVAL; | |
497 | } else if (len == 0) { | |
498 | trace_probe_log_err(offset, NO_ARG_BODY); | |
499 | return -EINVAL; | |
8ab83f56 | 500 | } |
ab105a4f | 501 | |
8ab83f56 | 502 | parg->comm = kstrdup(arg, GFP_KERNEL); |
ab105a4f | 503 | if (!parg->comm) |
8ab83f56 | 504 | return -ENOMEM; |
ab105a4f | 505 | |
40b53b77 | 506 | t = strchr(arg, ':'); |
8ab83f56 | 507 | if (t) { |
40b53b77 MH |
508 | *t = '\0'; |
509 | t2 = strchr(++t, '['); | |
510 | if (t2) { | |
ab105a4f MH |
511 | *t2++ = '\0'; |
512 | t3 = strchr(t2, ']'); | |
513 | if (!t3) { | |
514 | offset += t2 + strlen(t2) - arg; | |
515 | trace_probe_log_err(offset, | |
516 | ARRAY_NO_CLOSE); | |
517 | return -EINVAL; | |
518 | } else if (t3[1] != '\0') { | |
519 | trace_probe_log_err(offset + t3 + 1 - arg, | |
520 | BAD_ARRAY_SUFFIX); | |
521 | return -EINVAL; | |
522 | } | |
523 | *t3 = '\0'; | |
524 | if (kstrtouint(t2, 0, &parg->count) || !parg->count) { | |
525 | trace_probe_log_err(offset + t2 - arg, | |
526 | BAD_ARRAY_NUM); | |
40b53b77 | 527 | return -EINVAL; |
ab105a4f MH |
528 | } |
529 | if (parg->count > MAX_ARRAY_LEN) { | |
530 | trace_probe_log_err(offset + t2 - arg, | |
531 | ARRAY_TOO_BIG); | |
532 | return -EINVAL; | |
533 | } | |
40b53b77 | 534 | } |
8ab83f56 | 535 | } |
3dd1f7f2 MH |
536 | |
537 | /* Since $comm can not be dereferred, we can find $comm by strcmp */ | |
538 | if (strcmp(arg, "$comm") == 0) { | |
539 | /* The type of $comm must be "string", and not an array. */ | |
540 | if (parg->count || (t && strcmp(t, "string"))) | |
541 | return -EINVAL; | |
40b53b77 | 542 | parg->type = find_fetch_type("string"); |
3dd1f7f2 | 543 | } else |
40b53b77 | 544 | parg->type = find_fetch_type(t); |
8ab83f56 | 545 | if (!parg->type) { |
ab105a4f | 546 | trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE); |
8ab83f56 SD |
547 | return -EINVAL; |
548 | } | |
549 | parg->offset = *size; | |
40b53b77 MH |
550 | *size += parg->type->size * (parg->count ?: 1); |
551 | ||
552 | if (parg->count) { | |
553 | len = strlen(parg->type->fmttype) + 6; | |
554 | parg->fmt = kmalloc(len, GFP_KERNEL); | |
555 | if (!parg->fmt) | |
556 | return -ENOMEM; | |
557 | snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype, | |
558 | parg->count); | |
559 | } | |
8ab83f56 | 560 | |
53305928 MH |
561 | code = tmp = kzalloc(sizeof(*code) * FETCH_INSN_MAX, GFP_KERNEL); |
562 | if (!code) | |
563 | return -ENOMEM; | |
564 | code[FETCH_INSN_MAX - 1].op = FETCH_OP_END; | |
565 | ||
566 | ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1], | |
ab105a4f | 567 | flags, offset); |
53305928 MH |
568 | if (ret) |
569 | goto fail; | |
570 | ||
571 | /* Store operation */ | |
572 | if (!strcmp(parg->type->name, "string")) { | |
573 | if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_IMM && | |
574 | code->op != FETCH_OP_COMM) { | |
ab105a4f MH |
575 | trace_probe_log_err(offset + (t ? (t - arg) : 0), |
576 | BAD_STRING); | |
53305928 MH |
577 | ret = -EINVAL; |
578 | goto fail; | |
579 | } | |
40b53b77 MH |
580 | if (code->op != FETCH_OP_DEREF || parg->count) { |
581 | /* | |
582 | * IMM and COMM is pointing actual address, those must | |
583 | * be kept, and if parg->count != 0, this is an array | |
584 | * of string pointers instead of string address itself. | |
585 | */ | |
53305928 | 586 | code++; |
40b53b77 | 587 | if (code->op != FETCH_OP_NOP) { |
ab105a4f MH |
588 | trace_probe_log_err(offset, TOO_MANY_OPS); |
589 | ret = -EINVAL; | |
40b53b77 MH |
590 | goto fail; |
591 | } | |
592 | } | |
53305928 | 593 | code->op = FETCH_OP_ST_STRING; /* In DEREF case, replace it */ |
40b53b77 | 594 | code->size = parg->type->size; |
53305928 MH |
595 | parg->dynamic = true; |
596 | } else if (code->op == FETCH_OP_DEREF) { | |
597 | code->op = FETCH_OP_ST_MEM; | |
598 | code->size = parg->type->size; | |
599 | } else { | |
600 | code++; | |
601 | if (code->op != FETCH_OP_NOP) { | |
ab105a4f MH |
602 | trace_probe_log_err(offset, TOO_MANY_OPS); |
603 | ret = -EINVAL; | |
53305928 MH |
604 | goto fail; |
605 | } | |
606 | code->op = FETCH_OP_ST_RAW; | |
607 | code->size = parg->type->size; | |
608 | } | |
40b53b77 | 609 | scode = code; |
53305928 MH |
610 | /* Modify operation */ |
611 | if (t != NULL) { | |
612 | ret = __parse_bitfield_probe_arg(t, parg->type, &code); | |
ab105a4f MH |
613 | if (ret) { |
614 | trace_probe_log_err(offset + t - arg, BAD_BITFIELD); | |
53305928 | 615 | goto fail; |
ab105a4f | 616 | } |
8ab83f56 | 617 | } |
40b53b77 MH |
618 | /* Loop(Array) operation */ |
619 | if (parg->count) { | |
620 | if (scode->op != FETCH_OP_ST_MEM && | |
621 | scode->op != FETCH_OP_ST_STRING) { | |
ab105a4f MH |
622 | trace_probe_log_err(offset + (t ? (t - arg) : 0), |
623 | BAD_STRING); | |
40b53b77 MH |
624 | ret = -EINVAL; |
625 | goto fail; | |
626 | } | |
627 | code++; | |
628 | if (code->op != FETCH_OP_NOP) { | |
ab105a4f MH |
629 | trace_probe_log_err(offset, TOO_MANY_OPS); |
630 | ret = -EINVAL; | |
40b53b77 MH |
631 | goto fail; |
632 | } | |
633 | code->op = FETCH_OP_LP_ARRAY; | |
634 | code->param = parg->count; | |
635 | } | |
53305928 MH |
636 | code++; |
637 | code->op = FETCH_OP_END; | |
638 | ||
639 | /* Shrink down the code buffer */ | |
640 | parg->code = kzalloc(sizeof(*code) * (code - tmp + 1), GFP_KERNEL); | |
641 | if (!parg->code) | |
642 | ret = -ENOMEM; | |
643 | else | |
644 | memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1)); | |
645 | ||
646 | fail: | |
a6682814 MH |
647 | if (ret) { |
648 | for (code = tmp; code < tmp + FETCH_INSN_MAX; code++) | |
649 | if (code->op == FETCH_NOP_SYMBOL) | |
650 | kfree(code->data); | |
651 | } | |
53305928 | 652 | kfree(tmp); |
8ab83f56 SD |
653 | |
654 | return ret; | |
655 | } | |
656 | ||
657 | /* Return 1 if name is reserved or already used by another argument */ | |
d00bbea9 MH |
658 | static int traceprobe_conflict_field_name(const char *name, |
659 | struct probe_arg *args, int narg) | |
8ab83f56 SD |
660 | { |
661 | int i; | |
662 | ||
663 | for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) | |
664 | if (strcmp(reserved_field_names[i], name) == 0) | |
665 | return 1; | |
666 | ||
667 | for (i = 0; i < narg; i++) | |
668 | if (strcmp(args[i].name, name) == 0) | |
669 | return 1; | |
670 | ||
671 | return 0; | |
672 | } | |
673 | ||
d00bbea9 MH |
674 | int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, char *arg, |
675 | unsigned int flags) | |
676 | { | |
677 | struct probe_arg *parg = &tp->args[i]; | |
678 | char *body; | |
d00bbea9 MH |
679 | |
680 | /* Increment count for freeing args in error case */ | |
681 | tp->nr_args++; | |
682 | ||
683 | body = strchr(arg, '='); | |
684 | if (body) { | |
ab105a4f MH |
685 | if (body - arg > MAX_ARG_NAME_LEN) { |
686 | trace_probe_log_err(0, ARG_NAME_TOO_LONG); | |
687 | return -EINVAL; | |
688 | } else if (body == arg) { | |
689 | trace_probe_log_err(0, NO_ARG_NAME); | |
b4443c17 | 690 | return -EINVAL; |
ab105a4f | 691 | } |
d00bbea9 MH |
692 | parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL); |
693 | body++; | |
694 | } else { | |
695 | /* If argument name is omitted, set "argN" */ | |
696 | parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1); | |
697 | body = arg; | |
698 | } | |
699 | if (!parg->name) | |
700 | return -ENOMEM; | |
701 | ||
702 | if (!is_good_name(parg->name)) { | |
ab105a4f | 703 | trace_probe_log_err(0, BAD_ARG_NAME); |
d00bbea9 MH |
704 | return -EINVAL; |
705 | } | |
d00bbea9 | 706 | if (traceprobe_conflict_field_name(parg->name, tp->args, i)) { |
ab105a4f | 707 | trace_probe_log_err(0, USED_ARG_NAME); |
d00bbea9 MH |
708 | return -EINVAL; |
709 | } | |
d00bbea9 | 710 | /* Parse fetch argument */ |
ab105a4f MH |
711 | return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags, |
712 | body - arg); | |
d00bbea9 MH |
713 | } |
714 | ||
8ab83f56 SD |
715 | void traceprobe_free_probe_arg(struct probe_arg *arg) |
716 | { | |
a6682814 MH |
717 | struct fetch_insn *code = arg->code; |
718 | ||
719 | while (code && code->op != FETCH_OP_END) { | |
720 | if (code->op == FETCH_NOP_SYMBOL) | |
721 | kfree(code->data); | |
722 | code++; | |
723 | } | |
53305928 | 724 | kfree(arg->code); |
8ab83f56 SD |
725 | kfree(arg->name); |
726 | kfree(arg->comm); | |
40b53b77 | 727 | kfree(arg->fmt); |
8ab83f56 SD |
728 | } |
729 | ||
a6682814 MH |
730 | int traceprobe_update_arg(struct probe_arg *arg) |
731 | { | |
732 | struct fetch_insn *code = arg->code; | |
733 | long offset; | |
734 | char *tmp; | |
735 | char c; | |
736 | int ret = 0; | |
737 | ||
738 | while (code && code->op != FETCH_OP_END) { | |
739 | if (code->op == FETCH_NOP_SYMBOL) { | |
740 | if (code[1].op != FETCH_OP_IMM) | |
741 | return -EINVAL; | |
742 | ||
ee474b81 | 743 | tmp = strpbrk(code->data, "+-"); |
a6682814 MH |
744 | if (tmp) |
745 | c = *tmp; | |
746 | ret = traceprobe_split_symbol_offset(code->data, | |
747 | &offset); | |
748 | if (ret) | |
749 | return ret; | |
750 | ||
751 | code[1].immediate = | |
752 | (unsigned long)kallsyms_lookup_name(code->data); | |
753 | if (tmp) | |
754 | *tmp = c; | |
755 | if (!code[1].immediate) | |
756 | return -ENOENT; | |
757 | code[1].immediate += offset; | |
758 | } | |
759 | code++; | |
760 | } | |
761 | return 0; | |
762 | } | |
763 | ||
40b53b77 MH |
764 | /* When len=0, we just calculate the needed length */ |
765 | #define LEN_OR_ZERO (len ? len - pos : 0) | |
5bf652aa NK |
766 | static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, |
767 | bool is_return) | |
768 | { | |
40b53b77 MH |
769 | struct probe_arg *parg; |
770 | int i, j; | |
5bf652aa | 771 | int pos = 0; |
5bf652aa NK |
772 | const char *fmt, *arg; |
773 | ||
774 | if (!is_return) { | |
775 | fmt = "(%lx)"; | |
776 | arg = "REC->" FIELD_STRING_IP; | |
777 | } else { | |
778 | fmt = "(%lx <- %lx)"; | |
779 | arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; | |
780 | } | |
781 | ||
5bf652aa NK |
782 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); |
783 | ||
784 | for (i = 0; i < tp->nr_args; i++) { | |
40b53b77 MH |
785 | parg = tp->args + i; |
786 | pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name); | |
787 | if (parg->count) { | |
788 | pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s", | |
789 | parg->type->fmt); | |
790 | for (j = 1; j < parg->count; j++) | |
791 | pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s", | |
792 | parg->type->fmt); | |
793 | pos += snprintf(buf + pos, LEN_OR_ZERO, "}"); | |
794 | } else | |
795 | pos += snprintf(buf + pos, LEN_OR_ZERO, "%s", | |
796 | parg->type->fmt); | |
5bf652aa NK |
797 | } |
798 | ||
799 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); | |
800 | ||
801 | for (i = 0; i < tp->nr_args; i++) { | |
40b53b77 MH |
802 | parg = tp->args + i; |
803 | if (parg->count) { | |
804 | if (strcmp(parg->type->name, "string") == 0) | |
805 | fmt = ", __get_str(%s[%d])"; | |
806 | else | |
807 | fmt = ", REC->%s[%d]"; | |
808 | for (j = 0; j < parg->count; j++) | |
809 | pos += snprintf(buf + pos, LEN_OR_ZERO, | |
810 | fmt, parg->name, j); | |
811 | } else { | |
812 | if (strcmp(parg->type->name, "string") == 0) | |
813 | fmt = ", __get_str(%s)"; | |
814 | else | |
815 | fmt = ", REC->%s"; | |
5bf652aa | 816 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
40b53b77 MH |
817 | fmt, parg->name); |
818 | } | |
5bf652aa NK |
819 | } |
820 | ||
5bf652aa NK |
821 | /* return the length of print_fmt */ |
822 | return pos; | |
823 | } | |
40b53b77 | 824 | #undef LEN_OR_ZERO |
5bf652aa | 825 | |
0a46c854 | 826 | int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return) |
5bf652aa NK |
827 | { |
828 | int len; | |
829 | char *print_fmt; | |
830 | ||
831 | /* First: called with 0 length to calculate the needed length */ | |
832 | len = __set_print_fmt(tp, NULL, 0, is_return); | |
833 | print_fmt = kmalloc(len + 1, GFP_KERNEL); | |
834 | if (!print_fmt) | |
835 | return -ENOMEM; | |
836 | ||
837 | /* Second: actually write the @print_fmt */ | |
838 | __set_print_fmt(tp, print_fmt, len + 1, is_return); | |
839 | tp->call.print_fmt = print_fmt; | |
840 | ||
841 | return 0; | |
842 | } | |
eeb07b06 MH |
843 | |
844 | int traceprobe_define_arg_fields(struct trace_event_call *event_call, | |
845 | size_t offset, struct trace_probe *tp) | |
846 | { | |
847 | int ret, i; | |
848 | ||
849 | /* Set argument names as fields */ | |
850 | for (i = 0; i < tp->nr_args; i++) { | |
851 | struct probe_arg *parg = &tp->args[i]; | |
40b53b77 MH |
852 | const char *fmt = parg->type->fmttype; |
853 | int size = parg->type->size; | |
854 | ||
855 | if (parg->fmt) | |
856 | fmt = parg->fmt; | |
857 | if (parg->count) | |
858 | size *= parg->count; | |
859 | ret = trace_define_field(event_call, fmt, parg->name, | |
860 | offset + parg->offset, size, | |
eeb07b06 MH |
861 | parg->type->is_signed, |
862 | FILTER_OTHER); | |
863 | if (ret) | |
864 | return ret; | |
865 | } | |
866 | return 0; | |
867 | } |