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