Commit | Line | Data |
---|---|---|
50656eec | 1 | /* |
0e60836b | 2 | * probe-event.c : perf-probe definition to probe_events format converter |
50656eec MH |
3 | * |
4 | * Written by Masami Hiramatsu <mhiramat@redhat.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
19 | * | |
20 | */ | |
21 | ||
50656eec MH |
22 | #include <sys/utsname.h> |
23 | #include <sys/types.h> | |
24 | #include <sys/stat.h> | |
25 | #include <fcntl.h> | |
26 | #include <errno.h> | |
27 | #include <stdio.h> | |
28 | #include <unistd.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
4de189fe MH |
31 | #include <stdarg.h> |
32 | #include <limits.h> | |
e80711ca | 33 | #include <elf.h> |
50656eec | 34 | |
31facc5f | 35 | #include "util.h" |
50656eec | 36 | #include "event.h" |
4de189fe | 37 | #include "strlist.h" |
50656eec | 38 | #include "debug.h" |
72041334 | 39 | #include "cache.h" |
631c9def | 40 | #include "color.h" |
e0faa8d3 MH |
41 | #include "symbol.h" |
42 | #include "thread.h" | |
553873e1 | 43 | #include <api/fs/debugfs.h> |
23773ca1 | 44 | #include <api/fs/tracefs.h> |
1d037ca1 | 45 | #include "trace-event.h" /* For __maybe_unused */ |
50656eec | 46 | #include "probe-event.h" |
4235b045 | 47 | #include "probe-finder.h" |
225466f1 | 48 | #include "session.h" |
50656eec MH |
49 | |
50 | #define MAX_CMDLEN 256 | |
50656eec MH |
51 | #define PERFPROBE_GROUP "probe" |
52 | ||
f4d7da49 MH |
53 | bool probe_event_dry_run; /* Dry run flag */ |
54 | ||
146a1439 | 55 | #define semantic_error(msg ...) pr_err("Semantic error :" msg) |
50656eec | 56 | |
4de189fe | 57 | /* If there is no space to write, returns -E2BIG. */ |
84988450 MH |
58 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
59 | __attribute__((format(printf, 3, 4))); | |
60 | ||
4de189fe MH |
61 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
62 | { | |
63 | int ret; | |
64 | va_list ap; | |
65 | va_start(ap, format); | |
66 | ret = vsnprintf(str, size, format, ap); | |
67 | va_end(ap); | |
68 | if (ret >= (int)size) | |
69 | ret = -E2BIG; | |
70 | return ret; | |
71 | } | |
72 | ||
4b4da7f7 | 73 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp); |
981d05ad | 74 | static void clear_probe_trace_event(struct probe_trace_event *tev); |
ee45b6c2 | 75 | static struct machine *host_machine; |
e0faa8d3 | 76 | |
469b9b88 | 77 | /* Initialize symbol maps and path of vmlinux/modules */ |
ee45b6c2 | 78 | static int init_symbol_maps(bool user_only) |
e0faa8d3 | 79 | { |
146a1439 MH |
80 | int ret; |
81 | ||
e0faa8d3 | 82 | symbol_conf.sort_by_name = true; |
680d926a | 83 | symbol_conf.allow_aliases = true; |
0a7e6d1b | 84 | ret = symbol__init(NULL); |
146a1439 MH |
85 | if (ret < 0) { |
86 | pr_debug("Failed to init symbol map.\n"); | |
87 | goto out; | |
88 | } | |
e0faa8d3 | 89 | |
ee45b6c2 MH |
90 | if (host_machine || user_only) /* already initialized */ |
91 | return 0; | |
d28c6223 | 92 | |
ee45b6c2 MH |
93 | if (symbol_conf.vmlinux_name) |
94 | pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); | |
95 | ||
96 | host_machine = machine__new_host(); | |
97 | if (!host_machine) { | |
98 | pr_debug("machine__new_host() failed.\n"); | |
99 | symbol__exit(); | |
100 | ret = -1; | |
469b9b88 | 101 | } |
146a1439 MH |
102 | out: |
103 | if (ret < 0) | |
104 | pr_warning("Failed to init vmlinux path.\n"); | |
105 | return ret; | |
e0faa8d3 MH |
106 | } |
107 | ||
ee45b6c2 MH |
108 | static void exit_symbol_maps(void) |
109 | { | |
110 | if (host_machine) { | |
111 | machine__delete(host_machine); | |
112 | host_machine = NULL; | |
113 | } | |
114 | symbol__exit(); | |
115 | } | |
116 | ||
469b9b88 MH |
117 | static struct symbol *__find_kernel_function_by_name(const char *name, |
118 | struct map **mapp) | |
119 | { | |
ee45b6c2 | 120 | return machine__find_kernel_function_by_name(host_machine, name, mapp, |
469b9b88 MH |
121 | NULL); |
122 | } | |
123 | ||
8f33f7de MH |
124 | static struct symbol *__find_kernel_function(u64 addr, struct map **mapp) |
125 | { | |
126 | return machine__find_kernel_function(host_machine, addr, mapp, NULL); | |
127 | } | |
128 | ||
129 | static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void) | |
130 | { | |
131 | /* kmap->ref_reloc_sym should be set if host_machine is initialized */ | |
132 | struct kmap *kmap; | |
133 | ||
134 | if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0) | |
135 | return NULL; | |
136 | ||
137 | kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]); | |
ba92732e WN |
138 | if (!kmap) |
139 | return NULL; | |
8f33f7de MH |
140 | return kmap->ref_reloc_sym; |
141 | } | |
142 | ||
143 | static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc) | |
144 | { | |
145 | struct ref_reloc_sym *reloc_sym; | |
146 | struct symbol *sym; | |
147 | struct map *map; | |
148 | ||
149 | /* ref_reloc_sym is just a label. Need a special fix*/ | |
150 | reloc_sym = kernel_get_ref_reloc_sym(); | |
151 | if (reloc_sym && strcmp(name, reloc_sym->name) == 0) | |
152 | return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr; | |
153 | else { | |
154 | sym = __find_kernel_function_by_name(name, &map); | |
155 | if (sym) | |
156 | return map->unmap_ip(map, sym->start) - | |
f56847c2 | 157 | ((reloc) ? 0 : map->reloc); |
8f33f7de MH |
158 | } |
159 | return 0; | |
160 | } | |
161 | ||
e80711ca MH |
162 | static struct map *kernel_get_module_map(const char *module) |
163 | { | |
164 | struct rb_node *nd; | |
ee45b6c2 | 165 | struct map_groups *grp = &host_machine->kmaps; |
e80711ca | 166 | |
14a8fd7c MH |
167 | /* A file path -- this is an offline module */ |
168 | if (module && strchr(module, '/')) | |
ee45b6c2 | 169 | return machine__new_module(host_machine, 0, module); |
14a8fd7c | 170 | |
e80711ca MH |
171 | if (!module) |
172 | module = "kernel"; | |
173 | ||
174 | for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) { | |
175 | struct map *pos = rb_entry(nd, struct map, rb_node); | |
176 | if (strncmp(pos->dso->short_name + 1, module, | |
177 | pos->dso->short_name_len - 2) == 0) { | |
178 | return pos; | |
179 | } | |
180 | } | |
181 | return NULL; | |
182 | } | |
183 | ||
9b118aca MH |
184 | static struct map *get_target_map(const char *target, bool user) |
185 | { | |
186 | /* Init maps of given executable or kernel */ | |
187 | if (user) | |
188 | return dso__new_map(target); | |
189 | else | |
190 | return kernel_get_module_map(target); | |
191 | } | |
192 | ||
193 | static void put_target_map(struct map *map, bool user) | |
194 | { | |
195 | if (map && user) { | |
196 | /* Only the user map needs to be released */ | |
197 | dso__delete(map->dso); | |
198 | map__delete(map); | |
199 | } | |
200 | } | |
201 | ||
202 | ||
e80711ca | 203 | static struct dso *kernel_get_module_dso(const char *module) |
469b9b88 MH |
204 | { |
205 | struct dso *dso; | |
fd930ff9 FBH |
206 | struct map *map; |
207 | const char *vmlinux_name; | |
469b9b88 MH |
208 | |
209 | if (module) { | |
8fa7d87f WL |
210 | list_for_each_entry(dso, &host_machine->kernel_dsos.head, |
211 | node) { | |
469b9b88 MH |
212 | if (strncmp(dso->short_name + 1, module, |
213 | dso->short_name_len - 2) == 0) | |
214 | goto found; | |
215 | } | |
216 | pr_debug("Failed to find module %s.\n", module); | |
217 | return NULL; | |
fd930ff9 FBH |
218 | } |
219 | ||
ee45b6c2 | 220 | map = host_machine->vmlinux_maps[MAP__FUNCTION]; |
fd930ff9 FBH |
221 | dso = map->dso; |
222 | ||
223 | vmlinux_name = symbol_conf.vmlinux_name; | |
224 | if (vmlinux_name) { | |
5230fb7d | 225 | if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0) |
fd930ff9 | 226 | return NULL; |
469b9b88 | 227 | } else { |
c3a34e06 | 228 | if (dso__load_vmlinux_path(dso, map, NULL) <= 0) { |
469b9b88 MH |
229 | pr_debug("Failed to load kernel map.\n"); |
230 | return NULL; | |
231 | } | |
232 | } | |
233 | found: | |
e80711ca MH |
234 | return dso; |
235 | } | |
236 | ||
237 | const char *kernel_get_module_path(const char *module) | |
238 | { | |
239 | struct dso *dso = kernel_get_module_dso(module); | |
240 | return (dso) ? dso->long_name : NULL; | |
469b9b88 MH |
241 | } |
242 | ||
fb7345bb MH |
243 | static int convert_exec_to_group(const char *exec, char **result) |
244 | { | |
245 | char *ptr1, *ptr2, *exec_copy; | |
246 | char buf[64]; | |
247 | int ret; | |
248 | ||
249 | exec_copy = strdup(exec); | |
250 | if (!exec_copy) | |
251 | return -ENOMEM; | |
252 | ||
253 | ptr1 = basename(exec_copy); | |
254 | if (!ptr1) { | |
255 | ret = -EINVAL; | |
256 | goto out; | |
257 | } | |
258 | ||
259 | ptr2 = strpbrk(ptr1, "-._"); | |
260 | if (ptr2) | |
261 | *ptr2 = '\0'; | |
262 | ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1); | |
263 | if (ret < 0) | |
264 | goto out; | |
265 | ||
266 | *result = strdup(buf); | |
267 | ret = *result ? 0 : -ENOMEM; | |
268 | ||
269 | out: | |
270 | free(exec_copy); | |
271 | return ret; | |
272 | } | |
273 | ||
9b118aca MH |
274 | static void clear_perf_probe_point(struct perf_probe_point *pp) |
275 | { | |
276 | free(pp->file); | |
277 | free(pp->function); | |
278 | free(pp->lazy_line); | |
279 | } | |
280 | ||
eb948e50 MH |
281 | static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs) |
282 | { | |
283 | int i; | |
284 | ||
285 | for (i = 0; i < ntevs; i++) | |
286 | clear_probe_trace_event(tevs + i); | |
287 | } | |
288 | ||
89fe808a | 289 | #ifdef HAVE_DWARF_SUPPORT |
9b118aca MH |
290 | /* |
291 | * Some binaries like glibc have special symbols which are on the symbol | |
292 | * table, but not in the debuginfo. If we can find the address of the | |
293 | * symbol from map, we can translate the address back to the probe point. | |
294 | */ | |
295 | static int find_alternative_probe_point(struct debuginfo *dinfo, | |
296 | struct perf_probe_point *pp, | |
297 | struct perf_probe_point *result, | |
298 | const char *target, bool uprobes) | |
299 | { | |
300 | struct map *map = NULL; | |
301 | struct symbol *sym; | |
302 | u64 address = 0; | |
303 | int ret = -ENOENT; | |
304 | ||
305 | /* This can work only for function-name based one */ | |
306 | if (!pp->function || pp->file) | |
307 | return -ENOTSUP; | |
308 | ||
309 | map = get_target_map(target, uprobes); | |
310 | if (!map) | |
311 | return -EINVAL; | |
312 | ||
313 | /* Find the address of given function */ | |
314 | map__for_each_symbol_by_name(map, pp->function, sym) { | |
e6d7c91c MH |
315 | if (uprobes) |
316 | address = sym->start; | |
317 | else | |
318 | address = map->unmap_ip(map, sym->start); | |
e578da3b | 319 | break; |
9b118aca MH |
320 | } |
321 | if (!address) { | |
322 | ret = -ENOENT; | |
323 | goto out; | |
324 | } | |
f6c15621 WN |
325 | pr_debug("Symbol %s address found : %" PRIx64 "\n", |
326 | pp->function, address); | |
9b118aca MH |
327 | |
328 | ret = debuginfo__find_probe_point(dinfo, (unsigned long)address, | |
329 | result); | |
330 | if (ret <= 0) | |
331 | ret = (!ret) ? -ENOENT : ret; | |
332 | else { | |
333 | result->offset += pp->offset; | |
334 | result->line += pp->line; | |
335 | ret = 0; | |
336 | } | |
337 | ||
338 | out: | |
339 | put_target_map(map, uprobes); | |
340 | return ret; | |
341 | ||
342 | } | |
343 | ||
344 | static int get_alternative_probe_event(struct debuginfo *dinfo, | |
345 | struct perf_probe_event *pev, | |
346 | struct perf_probe_point *tmp, | |
347 | const char *target) | |
348 | { | |
349 | int ret; | |
350 | ||
351 | memcpy(tmp, &pev->point, sizeof(*tmp)); | |
352 | memset(&pev->point, 0, sizeof(pev->point)); | |
353 | ret = find_alternative_probe_point(dinfo, tmp, &pev->point, | |
354 | target, pev->uprobes); | |
355 | if (ret < 0) | |
356 | memcpy(&pev->point, tmp, sizeof(*tmp)); | |
357 | ||
358 | return ret; | |
359 | } | |
a15ad2f5 | 360 | |
811dd2ae MH |
361 | static int get_alternative_line_range(struct debuginfo *dinfo, |
362 | struct line_range *lr, | |
363 | const char *target, bool user) | |
364 | { | |
6d4a4896 DA |
365 | struct perf_probe_point pp = { .function = lr->function, |
366 | .file = lr->file, | |
367 | .line = lr->start }; | |
368 | struct perf_probe_point result; | |
811dd2ae MH |
369 | int ret, len = 0; |
370 | ||
6d4a4896 DA |
371 | memset(&result, 0, sizeof(result)); |
372 | ||
811dd2ae MH |
373 | if (lr->end != INT_MAX) |
374 | len = lr->end - lr->start; | |
375 | ret = find_alternative_probe_point(dinfo, &pp, &result, | |
376 | target, user); | |
377 | if (!ret) { | |
378 | lr->function = result.function; | |
379 | lr->file = result.file; | |
380 | lr->start = result.line; | |
381 | if (lr->end != INT_MAX) | |
382 | lr->end = lr->start + len; | |
383 | clear_perf_probe_point(&pp); | |
384 | } | |
385 | return ret; | |
386 | } | |
387 | ||
ff741783 | 388 | /* Open new debuginfo of given module */ |
92561cb7 | 389 | static struct debuginfo *open_debuginfo(const char *module, bool silent) |
e0faa8d3 | 390 | { |
a15ad2f5 | 391 | const char *path = module; |
92561cb7 | 392 | struct debuginfo *ret; |
ff741783 | 393 | |
a15ad2f5 | 394 | if (!module || !strchr(module, '/')) { |
14a8fd7c | 395 | path = kernel_get_module_path(module); |
14a8fd7c | 396 | if (!path) { |
92561cb7 MH |
397 | if (!silent) |
398 | pr_err("Failed to find path of %s module.\n", | |
399 | module ?: "kernel"); | |
14a8fd7c MH |
400 | return NULL; |
401 | } | |
e0faa8d3 | 402 | } |
92561cb7 MH |
403 | ret = debuginfo__new(path); |
404 | if (!ret && !silent) { | |
405 | pr_warning("The %s file has no debug information.\n", path); | |
406 | if (!module || !strtailcmp(path, ".ko")) | |
407 | pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, "); | |
408 | else | |
409 | pr_warning("Rebuild with -g, "); | |
410 | pr_warning("or install an appropriate debuginfo package.\n"); | |
411 | } | |
412 | return ret; | |
e0faa8d3 | 413 | } |
4b4da7f7 | 414 | |
92561cb7 | 415 | |
99ca4233 MH |
416 | static int get_text_start_address(const char *exec, unsigned long *address) |
417 | { | |
418 | Elf *elf; | |
419 | GElf_Ehdr ehdr; | |
420 | GElf_Shdr shdr; | |
421 | int fd, ret = -ENOENT; | |
422 | ||
423 | fd = open(exec, O_RDONLY); | |
424 | if (fd < 0) | |
425 | return -errno; | |
426 | ||
427 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); | |
428 | if (elf == NULL) | |
429 | return -EINVAL; | |
430 | ||
431 | if (gelf_getehdr(elf, &ehdr) == NULL) | |
432 | goto out; | |
433 | ||
434 | if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL)) | |
435 | goto out; | |
436 | ||
437 | *address = shdr.sh_addr - shdr.sh_offset; | |
438 | ret = 0; | |
439 | out: | |
440 | elf_end(elf); | |
441 | return ret; | |
442 | } | |
443 | ||
5a6f6314 MH |
444 | /* |
445 | * Convert trace point to probe point with debuginfo | |
446 | */ | |
447 | static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp, | |
448 | struct perf_probe_point *pp, | |
449 | bool is_kprobe) | |
450 | { | |
451 | struct debuginfo *dinfo = NULL; | |
452 | unsigned long stext = 0; | |
453 | u64 addr = tp->address; | |
454 | int ret = -ENOENT; | |
455 | ||
456 | /* convert the address to dwarf address */ | |
457 | if (!is_kprobe) { | |
458 | if (!addr) { | |
459 | ret = -EINVAL; | |
460 | goto error; | |
461 | } | |
462 | ret = get_text_start_address(tp->module, &stext); | |
463 | if (ret < 0) | |
464 | goto error; | |
465 | addr += stext; | |
466 | } else { | |
467 | addr = kernel_get_symbol_address_by_name(tp->symbol, false); | |
468 | if (addr == 0) | |
469 | goto error; | |
470 | addr += tp->offset; | |
471 | } | |
472 | ||
473 | pr_debug("try to find information at %" PRIx64 " in %s\n", addr, | |
474 | tp->module ? : "kernel"); | |
475 | ||
92561cb7 | 476 | dinfo = open_debuginfo(tp->module, verbose == 0); |
5a6f6314 MH |
477 | if (dinfo) { |
478 | ret = debuginfo__find_probe_point(dinfo, | |
479 | (unsigned long)addr, pp); | |
480 | debuginfo__delete(dinfo); | |
92561cb7 | 481 | } else |
5a6f6314 | 482 | ret = -ENOENT; |
5a6f6314 MH |
483 | |
484 | if (ret > 0) { | |
485 | pp->retprobe = tp->retprobe; | |
486 | return 0; | |
487 | } | |
488 | error: | |
489 | pr_debug("Failed to find corresponding probes from debuginfo.\n"); | |
490 | return ret ? : -ENOENT; | |
491 | } | |
492 | ||
fb7345bb MH |
493 | static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs, |
494 | int ntevs, const char *exec) | |
495 | { | |
496 | int i, ret = 0; | |
eb948e50 | 497 | unsigned long stext = 0; |
fb7345bb MH |
498 | |
499 | if (!exec) | |
500 | return 0; | |
501 | ||
502 | ret = get_text_start_address(exec, &stext); | |
503 | if (ret < 0) | |
504 | return ret; | |
505 | ||
506 | for (i = 0; i < ntevs && ret >= 0; i++) { | |
981a2379 | 507 | /* point.address is the addres of point.symbol + point.offset */ |
eb948e50 | 508 | tevs[i].point.address -= stext; |
fb7345bb | 509 | tevs[i].point.module = strdup(exec); |
eb948e50 | 510 | if (!tevs[i].point.module) { |
fb7345bb MH |
511 | ret = -ENOMEM; |
512 | break; | |
513 | } | |
514 | tevs[i].uprobes = true; | |
515 | } | |
516 | ||
517 | return ret; | |
518 | } | |
519 | ||
190b57fc MH |
520 | static int add_module_to_probe_trace_events(struct probe_trace_event *tevs, |
521 | int ntevs, const char *module) | |
522 | { | |
14a8fd7c MH |
523 | int i, ret = 0; |
524 | char *tmp; | |
525 | ||
526 | if (!module) | |
527 | return 0; | |
528 | ||
529 | tmp = strrchr(module, '/'); | |
530 | if (tmp) { | |
531 | /* This is a module path -- get the module name */ | |
532 | module = strdup(tmp + 1); | |
533 | if (!module) | |
534 | return -ENOMEM; | |
535 | tmp = strchr(module, '.'); | |
536 | if (tmp) | |
537 | *tmp = '\0'; | |
538 | tmp = (char *)module; /* For free() */ | |
539 | } | |
540 | ||
190b57fc MH |
541 | for (i = 0; i < ntevs; i++) { |
542 | tevs[i].point.module = strdup(module); | |
14a8fd7c MH |
543 | if (!tevs[i].point.module) { |
544 | ret = -ENOMEM; | |
545 | break; | |
546 | } | |
190b57fc | 547 | } |
14a8fd7c | 548 | |
f5385650 | 549 | free(tmp); |
14a8fd7c | 550 | return ret; |
190b57fc MH |
551 | } |
552 | ||
dfef99cd MH |
553 | /* Post processing the probe events */ |
554 | static int post_process_probe_trace_events(struct probe_trace_event *tevs, | |
555 | int ntevs, const char *module, | |
556 | bool uprobe) | |
557 | { | |
558 | struct ref_reloc_sym *reloc_sym; | |
559 | char *tmp; | |
560 | int i; | |
561 | ||
562 | if (uprobe) | |
563 | return add_exec_to_probe_trace_events(tevs, ntevs, module); | |
564 | ||
565 | /* Note that currently ref_reloc_sym based probe is not for drivers */ | |
566 | if (module) | |
567 | return add_module_to_probe_trace_events(tevs, ntevs, module); | |
568 | ||
8f33f7de | 569 | reloc_sym = kernel_get_ref_reloc_sym(); |
dfef99cd MH |
570 | if (!reloc_sym) { |
571 | pr_warning("Relocated base symbol is not found!\n"); | |
572 | return -EINVAL; | |
573 | } | |
574 | ||
575 | for (i = 0; i < ntevs; i++) { | |
25dd9171 | 576 | if (tevs[i].point.address && !tevs[i].point.retprobe) { |
dfef99cd MH |
577 | tmp = strdup(reloc_sym->name); |
578 | if (!tmp) | |
579 | return -ENOMEM; | |
580 | free(tevs[i].point.symbol); | |
581 | tevs[i].point.symbol = tmp; | |
582 | tevs[i].point.offset = tevs[i].point.address - | |
583 | reloc_sym->unrelocated_addr; | |
584 | } | |
585 | } | |
586 | return 0; | |
587 | } | |
588 | ||
4b4da7f7 | 589 | /* Try to find perf_probe_event with debuginfo */ |
0e60836b | 590 | static int try_to_find_probe_trace_events(struct perf_probe_event *pev, |
190b57fc | 591 | struct probe_trace_event **tevs, |
4eced234 | 592 | int max_tevs, const char *target) |
4b4da7f7 MH |
593 | { |
594 | bool need_dwarf = perf_probe_event_need_dwarf(pev); | |
9b118aca | 595 | struct perf_probe_point tmp; |
225466f1 | 596 | struct debuginfo *dinfo; |
190b57fc | 597 | int ntevs, ret = 0; |
4b4da7f7 | 598 | |
92561cb7 | 599 | dinfo = open_debuginfo(target, !need_dwarf); |
225466f1 | 600 | |
ff741783 | 601 | if (!dinfo) { |
92561cb7 | 602 | if (need_dwarf) |
ff741783 | 603 | return -ENOENT; |
ff741783 | 604 | pr_debug("Could not open debuginfo. Try to use symbols.\n"); |
4b4da7f7 MH |
605 | return 0; |
606 | } | |
607 | ||
dfef99cd | 608 | pr_debug("Try to find probe point from debuginfo.\n"); |
ff741783 MH |
609 | /* Searching trace events corresponding to a probe event */ |
610 | ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs); | |
611 | ||
9b118aca MH |
612 | if (ntevs == 0) { /* Not found, retry with an alternative */ |
613 | ret = get_alternative_probe_event(dinfo, pev, &tmp, target); | |
614 | if (!ret) { | |
615 | ntevs = debuginfo__find_trace_events(dinfo, pev, | |
616 | tevs, max_tevs); | |
617 | /* | |
618 | * Write back to the original probe_event for | |
619 | * setting appropriate (user given) event name | |
620 | */ | |
621 | clear_perf_probe_point(&pev->point); | |
622 | memcpy(&pev->point, &tmp, sizeof(tmp)); | |
623 | } | |
624 | } | |
625 | ||
ff741783 | 626 | debuginfo__delete(dinfo); |
4b4da7f7 | 627 | |
146a1439 | 628 | if (ntevs > 0) { /* Succeeded to find trace events */ |
dfef99cd MH |
629 | pr_debug("Found %d probe_trace_events.\n", ntevs); |
630 | ret = post_process_probe_trace_events(*tevs, ntevs, | |
631 | target, pev->uprobes); | |
981d05ad MH |
632 | if (ret < 0) { |
633 | clear_probe_trace_events(*tevs, ntevs); | |
634 | zfree(tevs); | |
635 | } | |
190b57fc | 636 | return ret < 0 ? ret : ntevs; |
146a1439 | 637 | } |
4b4da7f7 | 638 | |
146a1439 | 639 | if (ntevs == 0) { /* No error but failed to find probe point. */ |
0687eba7 | 640 | pr_warning("Probe point '%s' not found.\n", |
146a1439 | 641 | synthesize_perf_probe_point(&pev->point)); |
0687eba7 | 642 | return -ENOENT; |
146a1439 MH |
643 | } |
644 | /* Error path : ntevs < 0 */ | |
15eca306 MH |
645 | pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs); |
646 | if (ntevs == -EBADF) { | |
647 | pr_warning("Warning: No dwarf info found in the vmlinux - " | |
648 | "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n"); | |
649 | if (!need_dwarf) { | |
0e43e5d2 | 650 | pr_debug("Trying to use symbols.\n"); |
15eca306 MH |
651 | return 0; |
652 | } | |
4b4da7f7 | 653 | } |
15eca306 | 654 | return ntevs; |
4b4da7f7 MH |
655 | } |
656 | ||
7cf0b79e MH |
657 | /* |
658 | * Find a src file from a DWARF tag path. Prepend optional source path prefix | |
659 | * and chop off leading directories that do not exist. Result is passed back as | |
660 | * a newly allocated path on success. | |
661 | * Return 0 if file was found and readable, -errno otherwise. | |
662 | */ | |
6a330a3c MH |
663 | static int get_real_path(const char *raw_path, const char *comp_dir, |
664 | char **new_path) | |
7cf0b79e | 665 | { |
6a330a3c MH |
666 | const char *prefix = symbol_conf.source_prefix; |
667 | ||
668 | if (!prefix) { | |
669 | if (raw_path[0] != '/' && comp_dir) | |
670 | /* If not an absolute path, try to use comp_dir */ | |
671 | prefix = comp_dir; | |
672 | else { | |
673 | if (access(raw_path, R_OK) == 0) { | |
674 | *new_path = strdup(raw_path); | |
38ae502b | 675 | return *new_path ? 0 : -ENOMEM; |
6a330a3c MH |
676 | } else |
677 | return -errno; | |
678 | } | |
7cf0b79e MH |
679 | } |
680 | ||
6a330a3c | 681 | *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2)); |
7cf0b79e MH |
682 | if (!*new_path) |
683 | return -ENOMEM; | |
684 | ||
685 | for (;;) { | |
6a330a3c | 686 | sprintf(*new_path, "%s/%s", prefix, raw_path); |
7cf0b79e MH |
687 | |
688 | if (access(*new_path, R_OK) == 0) | |
689 | return 0; | |
690 | ||
eb47cb2e | 691 | if (!symbol_conf.source_prefix) { |
6a330a3c | 692 | /* In case of searching comp_dir, don't retry */ |
eb47cb2e | 693 | zfree(new_path); |
6a330a3c | 694 | return -errno; |
eb47cb2e | 695 | } |
6a330a3c | 696 | |
7cf0b79e MH |
697 | switch (errno) { |
698 | case ENAMETOOLONG: | |
699 | case ENOENT: | |
700 | case EROFS: | |
701 | case EFAULT: | |
702 | raw_path = strchr(++raw_path, '/'); | |
703 | if (!raw_path) { | |
04662523 | 704 | zfree(new_path); |
7cf0b79e MH |
705 | return -ENOENT; |
706 | } | |
707 | continue; | |
708 | ||
709 | default: | |
04662523 | 710 | zfree(new_path); |
7cf0b79e MH |
711 | return -errno; |
712 | } | |
713 | } | |
714 | } | |
715 | ||
4b4da7f7 MH |
716 | #define LINEBUF_SIZE 256 |
717 | #define NR_ADDITIONAL_LINES 2 | |
718 | ||
fde52dbd | 719 | static int __show_one_line(FILE *fp, int l, bool skip, bool show_num) |
4b4da7f7 | 720 | { |
5f03cba4 | 721 | char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE]; |
befe3414 FBH |
722 | const char *color = show_num ? "" : PERF_COLOR_BLUE; |
723 | const char *prefix = NULL; | |
4b4da7f7 | 724 | |
befe3414 | 725 | do { |
4b4da7f7 MH |
726 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
727 | goto error; | |
befe3414 FBH |
728 | if (skip) |
729 | continue; | |
730 | if (!prefix) { | |
731 | prefix = show_num ? "%7d " : " "; | |
732 | color_fprintf(stdout, color, prefix, l); | |
4b4da7f7 | 733 | } |
befe3414 FBH |
734 | color_fprintf(stdout, color, "%s", buf); |
735 | ||
736 | } while (strchr(buf, '\n') == NULL); | |
146a1439 | 737 | |
fde52dbd | 738 | return 1; |
4b4da7f7 | 739 | error: |
fde52dbd | 740 | if (ferror(fp)) { |
5f03cba4 MH |
741 | pr_warning("File read error: %s\n", |
742 | strerror_r(errno, sbuf, sizeof(sbuf))); | |
fde52dbd FBH |
743 | return -1; |
744 | } | |
745 | return 0; | |
746 | } | |
146a1439 | 747 | |
fde52dbd FBH |
748 | static int _show_one_line(FILE *fp, int l, bool skip, bool show_num) |
749 | { | |
750 | int rv = __show_one_line(fp, l, skip, show_num); | |
751 | if (rv == 0) { | |
752 | pr_warning("Source file is shorter than expected.\n"); | |
753 | rv = -1; | |
754 | } | |
755 | return rv; | |
4b4da7f7 MH |
756 | } |
757 | ||
fde52dbd FBH |
758 | #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true) |
759 | #define show_one_line(f,l) _show_one_line(f,l,false,false) | |
760 | #define skip_one_line(f,l) _show_one_line(f,l,true,false) | |
761 | #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false) | |
762 | ||
4b4da7f7 MH |
763 | /* |
764 | * Show line-range always requires debuginfo to find source file and | |
765 | * line number. | |
766 | */ | |
811dd2ae MH |
767 | static int __show_line_range(struct line_range *lr, const char *module, |
768 | bool user) | |
4b4da7f7 | 769 | { |
d3b63d7a | 770 | int l = 1; |
5a62257a | 771 | struct int_node *ln; |
ff741783 | 772 | struct debuginfo *dinfo; |
4b4da7f7 | 773 | FILE *fp; |
ff741783 | 774 | int ret; |
7cf0b79e | 775 | char *tmp; |
5f03cba4 | 776 | char sbuf[STRERR_BUFSIZE]; |
4b4da7f7 MH |
777 | |
778 | /* Search a line range */ | |
92561cb7 MH |
779 | dinfo = open_debuginfo(module, false); |
780 | if (!dinfo) | |
ff741783 | 781 | return -ENOENT; |
146a1439 | 782 | |
ff741783 | 783 | ret = debuginfo__find_line_range(dinfo, lr); |
811dd2ae MH |
784 | if (!ret) { /* Not found, retry with an alternative */ |
785 | ret = get_alternative_line_range(dinfo, lr, module, user); | |
786 | if (!ret) | |
787 | ret = debuginfo__find_line_range(dinfo, lr); | |
788 | } | |
ff741783 | 789 | debuginfo__delete(dinfo); |
5ee05b88 | 790 | if (ret == 0 || ret == -ENOENT) { |
146a1439 MH |
791 | pr_warning("Specified source line is not found.\n"); |
792 | return -ENOENT; | |
793 | } else if (ret < 0) { | |
5ee05b88 | 794 | pr_warning("Debuginfo analysis failed.\n"); |
146a1439 MH |
795 | return ret; |
796 | } | |
4b4da7f7 | 797 | |
7cf0b79e MH |
798 | /* Convert source file path */ |
799 | tmp = lr->path; | |
6a330a3c | 800 | ret = get_real_path(tmp, lr->comp_dir, &lr->path); |
a78604de HK |
801 | |
802 | /* Free old path when new path is assigned */ | |
803 | if (tmp != lr->path) | |
804 | free(tmp); | |
805 | ||
7cf0b79e | 806 | if (ret < 0) { |
5ee05b88 | 807 | pr_warning("Failed to find source file path.\n"); |
7cf0b79e MH |
808 | return ret; |
809 | } | |
810 | ||
4b4da7f7 MH |
811 | setup_pager(); |
812 | ||
813 | if (lr->function) | |
8737ebde | 814 | fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path, |
4b4da7f7 MH |
815 | lr->start - lr->offset); |
816 | else | |
62c15fc4 | 817 | fprintf(stdout, "<%s:%d>\n", lr->path, lr->start); |
4b4da7f7 MH |
818 | |
819 | fp = fopen(lr->path, "r"); | |
146a1439 MH |
820 | if (fp == NULL) { |
821 | pr_warning("Failed to open %s: %s\n", lr->path, | |
5f03cba4 | 822 | strerror_r(errno, sbuf, sizeof(sbuf))); |
146a1439 MH |
823 | return -errno; |
824 | } | |
4b4da7f7 | 825 | /* Skip to starting line number */ |
44b81e92 | 826 | while (l < lr->start) { |
fde52dbd | 827 | ret = skip_one_line(fp, l++); |
44b81e92 FBH |
828 | if (ret < 0) |
829 | goto end; | |
830 | } | |
4b4da7f7 | 831 | |
5a62257a MH |
832 | intlist__for_each(ln, lr->line_list) { |
833 | for (; ln->i > l; l++) { | |
fde52dbd | 834 | ret = show_one_line(fp, l - lr->offset); |
44b81e92 FBH |
835 | if (ret < 0) |
836 | goto end; | |
837 | } | |
fde52dbd | 838 | ret = show_one_line_with_num(fp, l++ - lr->offset); |
146a1439 MH |
839 | if (ret < 0) |
840 | goto end; | |
4b4da7f7 MH |
841 | } |
842 | ||
843 | if (lr->end == INT_MAX) | |
844 | lr->end = l + NR_ADDITIONAL_LINES; | |
fde52dbd FBH |
845 | while (l <= lr->end) { |
846 | ret = show_one_line_or_eof(fp, l++ - lr->offset); | |
847 | if (ret <= 0) | |
44b81e92 FBH |
848 | break; |
849 | } | |
146a1439 | 850 | end: |
4b4da7f7 | 851 | fclose(fp); |
146a1439 | 852 | return ret; |
4b4da7f7 MH |
853 | } |
854 | ||
2b394bc4 | 855 | int show_line_range(struct line_range *lr, const char *module, bool user) |
ee45b6c2 MH |
856 | { |
857 | int ret; | |
858 | ||
2b394bc4 | 859 | ret = init_symbol_maps(user); |
ee45b6c2 MH |
860 | if (ret < 0) |
861 | return ret; | |
811dd2ae | 862 | ret = __show_line_range(lr, module, user); |
ee45b6c2 MH |
863 | exit_symbol_maps(); |
864 | ||
865 | return ret; | |
866 | } | |
867 | ||
ff741783 MH |
868 | static int show_available_vars_at(struct debuginfo *dinfo, |
869 | struct perf_probe_event *pev, | |
bd09d7b5 | 870 | int max_vls, struct strfilter *_filter, |
9b118aca | 871 | bool externs, const char *target) |
cf6eb489 MH |
872 | { |
873 | char *buf; | |
bd09d7b5 | 874 | int ret, i, nvars; |
cf6eb489 MH |
875 | struct str_node *node; |
876 | struct variable_list *vls = NULL, *vl; | |
9b118aca | 877 | struct perf_probe_point tmp; |
bd09d7b5 | 878 | const char *var; |
cf6eb489 MH |
879 | |
880 | buf = synthesize_perf_probe_point(&pev->point); | |
881 | if (!buf) | |
882 | return -EINVAL; | |
883 | pr_debug("Searching variables at %s\n", buf); | |
884 | ||
ff741783 MH |
885 | ret = debuginfo__find_available_vars_at(dinfo, pev, &vls, |
886 | max_vls, externs); | |
9b118aca MH |
887 | if (!ret) { /* Not found, retry with an alternative */ |
888 | ret = get_alternative_probe_event(dinfo, pev, &tmp, target); | |
889 | if (!ret) { | |
890 | ret = debuginfo__find_available_vars_at(dinfo, pev, | |
891 | &vls, max_vls, externs); | |
892 | /* Release the old probe_point */ | |
893 | clear_perf_probe_point(&tmp); | |
894 | } | |
895 | } | |
bd09d7b5 | 896 | if (ret <= 0) { |
69e96eaa MH |
897 | if (ret == 0 || ret == -ENOENT) { |
898 | pr_err("Failed to find the address of %s\n", buf); | |
899 | ret = -ENOENT; | |
900 | } else | |
901 | pr_warning("Debuginfo analysis failed.\n"); | |
bd09d7b5 MH |
902 | goto end; |
903 | } | |
69e96eaa | 904 | |
bd09d7b5 MH |
905 | /* Some variables are found */ |
906 | fprintf(stdout, "Available variables at %s\n", buf); | |
907 | for (i = 0; i < ret; i++) { | |
908 | vl = &vls[i]; | |
909 | /* | |
910 | * A probe point might be converted to | |
911 | * several trace points. | |
912 | */ | |
913 | fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol, | |
914 | vl->point.offset); | |
74cf249d | 915 | zfree(&vl->point.symbol); |
bd09d7b5 MH |
916 | nvars = 0; |
917 | if (vl->vars) { | |
918 | strlist__for_each(node, vl->vars) { | |
919 | var = strchr(node->s, '\t') + 1; | |
920 | if (strfilter__compare(_filter, var)) { | |
cf6eb489 | 921 | fprintf(stdout, "\t\t%s\n", node->s); |
bd09d7b5 MH |
922 | nvars++; |
923 | } | |
924 | } | |
925 | strlist__delete(vl->vars); | |
cf6eb489 | 926 | } |
bd09d7b5 MH |
927 | if (nvars == 0) |
928 | fprintf(stdout, "\t\t(No matched variables)\n"); | |
929 | } | |
930 | free(vls); | |
931 | end: | |
cf6eb489 MH |
932 | free(buf); |
933 | return ret; | |
934 | } | |
935 | ||
936 | /* Show available variables on given probe point */ | |
937 | int show_available_vars(struct perf_probe_event *pevs, int npevs, | |
bd09d7b5 MH |
938 | int max_vls, const char *module, |
939 | struct strfilter *_filter, bool externs) | |
cf6eb489 | 940 | { |
ff741783 MH |
941 | int i, ret = 0; |
942 | struct debuginfo *dinfo; | |
cf6eb489 | 943 | |
2b394bc4 | 944 | ret = init_symbol_maps(pevs->uprobes); |
cf6eb489 MH |
945 | if (ret < 0) |
946 | return ret; | |
947 | ||
92561cb7 | 948 | dinfo = open_debuginfo(module, false); |
ff741783 | 949 | if (!dinfo) { |
ee45b6c2 MH |
950 | ret = -ENOENT; |
951 | goto out; | |
ff741783 MH |
952 | } |
953 | ||
cf6eb489 MH |
954 | setup_pager(); |
955 | ||
ff741783 MH |
956 | for (i = 0; i < npevs && ret >= 0; i++) |
957 | ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter, | |
9b118aca | 958 | externs, module); |
ff741783 MH |
959 | |
960 | debuginfo__delete(dinfo); | |
ee45b6c2 MH |
961 | out: |
962 | exit_symbol_maps(); | |
cf6eb489 MH |
963 | return ret; |
964 | } | |
965 | ||
89fe808a | 966 | #else /* !HAVE_DWARF_SUPPORT */ |
4b4da7f7 | 967 | |
5a6f6314 MH |
968 | static int |
969 | find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused, | |
970 | struct perf_probe_point *pp __maybe_unused, | |
971 | bool is_kprobe __maybe_unused) | |
4b4da7f7 | 972 | { |
5a6f6314 | 973 | return -ENOSYS; |
4b4da7f7 MH |
974 | } |
975 | ||
0e60836b | 976 | static int try_to_find_probe_trace_events(struct perf_probe_event *pev, |
1d037ca1 | 977 | struct probe_trace_event **tevs __maybe_unused, |
1d027ee9 ACM |
978 | int max_tevs __maybe_unused, |
979 | const char *target __maybe_unused) | |
4b4da7f7 | 980 | { |
146a1439 MH |
981 | if (perf_probe_event_need_dwarf(pev)) { |
982 | pr_warning("Debuginfo-analysis is not supported.\n"); | |
983 | return -ENOSYS; | |
984 | } | |
225466f1 | 985 | |
4b4da7f7 MH |
986 | return 0; |
987 | } | |
988 | ||
1d037ca1 | 989 | int show_line_range(struct line_range *lr __maybe_unused, |
2b394bc4 MH |
990 | const char *module __maybe_unused, |
991 | bool user __maybe_unused) | |
4b4da7f7 | 992 | { |
146a1439 MH |
993 | pr_warning("Debuginfo-analysis is not supported.\n"); |
994 | return -ENOSYS; | |
4b4da7f7 MH |
995 | } |
996 | ||
1d037ca1 IT |
997 | int show_available_vars(struct perf_probe_event *pevs __maybe_unused, |
998 | int npevs __maybe_unused, int max_vls __maybe_unused, | |
999 | const char *module __maybe_unused, | |
1000 | struct strfilter *filter __maybe_unused, | |
1001 | bool externs __maybe_unused) | |
cf6eb489 MH |
1002 | { |
1003 | pr_warning("Debuginfo-analysis is not supported.\n"); | |
1004 | return -ENOSYS; | |
1005 | } | |
e0faa8d3 MH |
1006 | #endif |
1007 | ||
e53b00d3 MH |
1008 | void line_range__clear(struct line_range *lr) |
1009 | { | |
e53b00d3 MH |
1010 | free(lr->function); |
1011 | free(lr->file); | |
1012 | free(lr->path); | |
1013 | free(lr->comp_dir); | |
5a62257a | 1014 | intlist__delete(lr->line_list); |
e53b00d3 MH |
1015 | memset(lr, 0, sizeof(*lr)); |
1016 | } | |
1017 | ||
5a62257a | 1018 | int line_range__init(struct line_range *lr) |
e53b00d3 MH |
1019 | { |
1020 | memset(lr, 0, sizeof(*lr)); | |
5a62257a MH |
1021 | lr->line_list = intlist__new(NULL); |
1022 | if (!lr->line_list) | |
1023 | return -ENOMEM; | |
1024 | else | |
1025 | return 0; | |
e53b00d3 MH |
1026 | } |
1027 | ||
21dd9ae5 FBH |
1028 | static int parse_line_num(char **ptr, int *val, const char *what) |
1029 | { | |
1030 | const char *start = *ptr; | |
1031 | ||
1032 | errno = 0; | |
1033 | *val = strtol(*ptr, ptr, 0); | |
1034 | if (errno || *ptr == start) { | |
1035 | semantic_error("'%s' is not a valid number.\n", what); | |
1036 | return -EINVAL; | |
1037 | } | |
1038 | return 0; | |
1039 | } | |
1040 | ||
9d95b580 FBH |
1041 | /* |
1042 | * Stuff 'lr' according to the line range described by 'arg'. | |
1043 | * The line range syntax is described by: | |
1044 | * | |
1045 | * SRC[:SLN[+NUM|-ELN]] | |
e116dfa1 | 1046 | * FNC[@SRC][:SLN[+NUM|-ELN]] |
9d95b580 | 1047 | */ |
146a1439 | 1048 | int parse_line_range_desc(const char *arg, struct line_range *lr) |
631c9def | 1049 | { |
e116dfa1 | 1050 | char *range, *file, *name = strdup(arg); |
21dd9ae5 FBH |
1051 | int err; |
1052 | ||
1053 | if (!name) | |
1054 | return -ENOMEM; | |
1055 | ||
1056 | lr->start = 0; | |
1057 | lr->end = INT_MAX; | |
1058 | ||
1059 | range = strchr(name, ':'); | |
1060 | if (range) { | |
1061 | *range++ = '\0'; | |
1062 | ||
1063 | err = parse_line_num(&range, &lr->start, "start line"); | |
1064 | if (err) | |
1065 | goto err; | |
1066 | ||
1067 | if (*range == '+' || *range == '-') { | |
1068 | const char c = *range++; | |
1069 | ||
1070 | err = parse_line_num(&range, &lr->end, "end line"); | |
1071 | if (err) | |
1072 | goto err; | |
1073 | ||
1074 | if (c == '+') { | |
1075 | lr->end += lr->start; | |
1076 | /* | |
1077 | * Adjust the number of lines here. | |
1078 | * If the number of lines == 1, the | |
1079 | * the end of line should be equal to | |
1080 | * the start of line. | |
1081 | */ | |
1082 | lr->end--; | |
1083 | } | |
1084 | } | |
9d95b580 | 1085 | |
d3b63d7a | 1086 | pr_debug("Line range is %d to %d\n", lr->start, lr->end); |
21dd9ae5 FBH |
1087 | |
1088 | err = -EINVAL; | |
d3b63d7a | 1089 | if (lr->start > lr->end) { |
631c9def | 1090 | semantic_error("Start line must be smaller" |
146a1439 | 1091 | " than end line.\n"); |
21dd9ae5 | 1092 | goto err; |
146a1439 | 1093 | } |
21dd9ae5 FBH |
1094 | if (*range != '\0') { |
1095 | semantic_error("Tailing with invalid str '%s'.\n", range); | |
1096 | goto err; | |
146a1439 | 1097 | } |
d3b63d7a | 1098 | } |
02b95dad | 1099 | |
e116dfa1 MH |
1100 | file = strchr(name, '@'); |
1101 | if (file) { | |
1102 | *file = '\0'; | |
1103 | lr->file = strdup(++file); | |
1104 | if (lr->file == NULL) { | |
1105 | err = -ENOMEM; | |
1106 | goto err; | |
1107 | } | |
1108 | lr->function = name; | |
1109 | } else if (strchr(name, '.')) | |
21dd9ae5 | 1110 | lr->file = name; |
631c9def | 1111 | else |
21dd9ae5 | 1112 | lr->function = name; |
146a1439 MH |
1113 | |
1114 | return 0; | |
21dd9ae5 FBH |
1115 | err: |
1116 | free(name); | |
1117 | return err; | |
631c9def MH |
1118 | } |
1119 | ||
b7702a21 MH |
1120 | /* Check the name is good for event/group */ |
1121 | static bool check_event_name(const char *name) | |
1122 | { | |
1123 | if (!isalpha(*name) && *name != '_') | |
1124 | return false; | |
1125 | while (*++name != '\0') { | |
1126 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') | |
1127 | return false; | |
1128 | } | |
1129 | return true; | |
1130 | } | |
1131 | ||
50656eec | 1132 | /* Parse probepoint definition. */ |
146a1439 | 1133 | static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev) |
50656eec | 1134 | { |
4235b045 | 1135 | struct perf_probe_point *pp = &pev->point; |
50656eec MH |
1136 | char *ptr, *tmp; |
1137 | char c, nc = 0; | |
1138 | /* | |
1139 | * <Syntax> | |
2a9c8c36 MH |
1140 | * perf probe [EVENT=]SRC[:LN|;PTN] |
1141 | * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] | |
af663d75 MH |
1142 | * |
1143 | * TODO:Group name support | |
50656eec MH |
1144 | */ |
1145 | ||
2a9c8c36 MH |
1146 | ptr = strpbrk(arg, ";=@+%"); |
1147 | if (ptr && *ptr == '=') { /* Event name */ | |
af663d75 MH |
1148 | *ptr = '\0'; |
1149 | tmp = ptr + 1; | |
146a1439 MH |
1150 | if (strchr(arg, ':')) { |
1151 | semantic_error("Group name is not supported yet.\n"); | |
1152 | return -ENOTSUP; | |
1153 | } | |
1154 | if (!check_event_name(arg)) { | |
b7702a21 | 1155 | semantic_error("%s is bad for event name -it must " |
146a1439 MH |
1156 | "follow C symbol-naming rule.\n", arg); |
1157 | return -EINVAL; | |
1158 | } | |
02b95dad MH |
1159 | pev->event = strdup(arg); |
1160 | if (pev->event == NULL) | |
1161 | return -ENOMEM; | |
4235b045 | 1162 | pev->group = NULL; |
af663d75 MH |
1163 | arg = tmp; |
1164 | } | |
1165 | ||
2a9c8c36 | 1166 | ptr = strpbrk(arg, ";:+@%"); |
50656eec MH |
1167 | if (ptr) { |
1168 | nc = *ptr; | |
1169 | *ptr++ = '\0'; | |
1170 | } | |
1171 | ||
02b95dad MH |
1172 | tmp = strdup(arg); |
1173 | if (tmp == NULL) | |
1174 | return -ENOMEM; | |
1175 | ||
50656eec | 1176 | /* Check arg is function or file and copy it */ |
02b95dad MH |
1177 | if (strchr(tmp, '.')) /* File */ |
1178 | pp->file = tmp; | |
50656eec | 1179 | else /* Function */ |
02b95dad | 1180 | pp->function = tmp; |
50656eec MH |
1181 | |
1182 | /* Parse other options */ | |
1183 | while (ptr) { | |
1184 | arg = ptr; | |
1185 | c = nc; | |
2a9c8c36 | 1186 | if (c == ';') { /* Lazy pattern must be the last part */ |
02b95dad MH |
1187 | pp->lazy_line = strdup(arg); |
1188 | if (pp->lazy_line == NULL) | |
1189 | return -ENOMEM; | |
2a9c8c36 MH |
1190 | break; |
1191 | } | |
1192 | ptr = strpbrk(arg, ";:+@%"); | |
50656eec MH |
1193 | if (ptr) { |
1194 | nc = *ptr; | |
1195 | *ptr++ = '\0'; | |
1196 | } | |
1197 | switch (c) { | |
1198 | case ':': /* Line number */ | |
1199 | pp->line = strtoul(arg, &tmp, 0); | |
146a1439 | 1200 | if (*tmp != '\0') { |
2a9c8c36 | 1201 | semantic_error("There is non-digit char" |
146a1439 MH |
1202 | " in line number.\n"); |
1203 | return -EINVAL; | |
1204 | } | |
50656eec MH |
1205 | break; |
1206 | case '+': /* Byte offset from a symbol */ | |
1207 | pp->offset = strtoul(arg, &tmp, 0); | |
146a1439 | 1208 | if (*tmp != '\0') { |
2a9c8c36 | 1209 | semantic_error("There is non-digit character" |
146a1439 MH |
1210 | " in offset.\n"); |
1211 | return -EINVAL; | |
1212 | } | |
50656eec MH |
1213 | break; |
1214 | case '@': /* File name */ | |
146a1439 MH |
1215 | if (pp->file) { |
1216 | semantic_error("SRC@SRC is not allowed.\n"); | |
1217 | return -EINVAL; | |
1218 | } | |
02b95dad MH |
1219 | pp->file = strdup(arg); |
1220 | if (pp->file == NULL) | |
1221 | return -ENOMEM; | |
50656eec MH |
1222 | break; |
1223 | case '%': /* Probe places */ | |
1224 | if (strcmp(arg, "return") == 0) { | |
1225 | pp->retprobe = 1; | |
146a1439 MH |
1226 | } else { /* Others not supported yet */ |
1227 | semantic_error("%%%s is not supported.\n", arg); | |
1228 | return -ENOTSUP; | |
1229 | } | |
50656eec | 1230 | break; |
146a1439 MH |
1231 | default: /* Buggy case */ |
1232 | pr_err("This program has a bug at %s:%d.\n", | |
1233 | __FILE__, __LINE__); | |
1234 | return -ENOTSUP; | |
50656eec MH |
1235 | break; |
1236 | } | |
1237 | } | |
1238 | ||
1239 | /* Exclusion check */ | |
146a1439 | 1240 | if (pp->lazy_line && pp->line) { |
0e43e5d2 MH |
1241 | semantic_error("Lazy pattern can't be used with" |
1242 | " line number.\n"); | |
146a1439 MH |
1243 | return -EINVAL; |
1244 | } | |
2a9c8c36 | 1245 | |
146a1439 | 1246 | if (pp->lazy_line && pp->offset) { |
0e43e5d2 | 1247 | semantic_error("Lazy pattern can't be used with offset.\n"); |
146a1439 MH |
1248 | return -EINVAL; |
1249 | } | |
2a9c8c36 | 1250 | |
146a1439 | 1251 | if (pp->line && pp->offset) { |
0e43e5d2 | 1252 | semantic_error("Offset can't be used with line number.\n"); |
146a1439 MH |
1253 | return -EINVAL; |
1254 | } | |
50656eec | 1255 | |
146a1439 | 1256 | if (!pp->line && !pp->lazy_line && pp->file && !pp->function) { |
2a9c8c36 | 1257 | semantic_error("File always requires line number or " |
0e43e5d2 | 1258 | "lazy pattern.\n"); |
146a1439 MH |
1259 | return -EINVAL; |
1260 | } | |
50656eec | 1261 | |
146a1439 | 1262 | if (pp->offset && !pp->function) { |
0e43e5d2 | 1263 | semantic_error("Offset requires an entry function.\n"); |
146a1439 MH |
1264 | return -EINVAL; |
1265 | } | |
50656eec | 1266 | |
146a1439 | 1267 | if (pp->retprobe && !pp->function) { |
0e43e5d2 | 1268 | semantic_error("Return probe requires an entry function.\n"); |
146a1439 MH |
1269 | return -EINVAL; |
1270 | } | |
50656eec | 1271 | |
146a1439 | 1272 | if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) { |
2a9c8c36 | 1273 | semantic_error("Offset/Line/Lazy pattern can't be used with " |
0e43e5d2 | 1274 | "return probe.\n"); |
146a1439 MH |
1275 | return -EINVAL; |
1276 | } | |
50656eec | 1277 | |
4235b045 | 1278 | pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n", |
2a9c8c36 MH |
1279 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe, |
1280 | pp->lazy_line); | |
146a1439 | 1281 | return 0; |
50656eec MH |
1282 | } |
1283 | ||
7df2f329 | 1284 | /* Parse perf-probe event argument */ |
146a1439 | 1285 | static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg) |
7df2f329 | 1286 | { |
b2a3c12b | 1287 | char *tmp, *goodname; |
7df2f329 MH |
1288 | struct perf_probe_arg_field **fieldp; |
1289 | ||
1290 | pr_debug("parsing arg: %s into ", str); | |
1291 | ||
48481938 MH |
1292 | tmp = strchr(str, '='); |
1293 | if (tmp) { | |
02b95dad MH |
1294 | arg->name = strndup(str, tmp - str); |
1295 | if (arg->name == NULL) | |
1296 | return -ENOMEM; | |
11a1ca35 | 1297 | pr_debug("name:%s ", arg->name); |
48481938 MH |
1298 | str = tmp + 1; |
1299 | } | |
1300 | ||
11a1ca35 MH |
1301 | tmp = strchr(str, ':'); |
1302 | if (tmp) { /* Type setting */ | |
1303 | *tmp = '\0'; | |
02b95dad MH |
1304 | arg->type = strdup(tmp + 1); |
1305 | if (arg->type == NULL) | |
1306 | return -ENOMEM; | |
11a1ca35 MH |
1307 | pr_debug("type:%s ", arg->type); |
1308 | } | |
1309 | ||
b2a3c12b | 1310 | tmp = strpbrk(str, "-.["); |
7df2f329 MH |
1311 | if (!is_c_varname(str) || !tmp) { |
1312 | /* A variable, register, symbol or special value */ | |
02b95dad MH |
1313 | arg->var = strdup(str); |
1314 | if (arg->var == NULL) | |
1315 | return -ENOMEM; | |
48481938 | 1316 | pr_debug("%s\n", arg->var); |
146a1439 | 1317 | return 0; |
7df2f329 MH |
1318 | } |
1319 | ||
b2a3c12b | 1320 | /* Structure fields or array element */ |
02b95dad MH |
1321 | arg->var = strndup(str, tmp - str); |
1322 | if (arg->var == NULL) | |
1323 | return -ENOMEM; | |
b2a3c12b | 1324 | goodname = arg->var; |
48481938 | 1325 | pr_debug("%s, ", arg->var); |
7df2f329 MH |
1326 | fieldp = &arg->field; |
1327 | ||
1328 | do { | |
e334016f MH |
1329 | *fieldp = zalloc(sizeof(struct perf_probe_arg_field)); |
1330 | if (*fieldp == NULL) | |
1331 | return -ENOMEM; | |
b2a3c12b MH |
1332 | if (*tmp == '[') { /* Array */ |
1333 | str = tmp; | |
1334 | (*fieldp)->index = strtol(str + 1, &tmp, 0); | |
7df2f329 | 1335 | (*fieldp)->ref = true; |
b2a3c12b MH |
1336 | if (*tmp != ']' || tmp == str + 1) { |
1337 | semantic_error("Array index must be a" | |
1338 | " number.\n"); | |
1339 | return -EINVAL; | |
1340 | } | |
1341 | tmp++; | |
1342 | if (*tmp == '\0') | |
1343 | tmp = NULL; | |
1344 | } else { /* Structure */ | |
1345 | if (*tmp == '.') { | |
1346 | str = tmp + 1; | |
1347 | (*fieldp)->ref = false; | |
1348 | } else if (tmp[1] == '>') { | |
1349 | str = tmp + 2; | |
1350 | (*fieldp)->ref = true; | |
1351 | } else { | |
1352 | semantic_error("Argument parse error: %s\n", | |
1353 | str); | |
1354 | return -EINVAL; | |
1355 | } | |
1356 | tmp = strpbrk(str, "-.["); | |
146a1439 | 1357 | } |
7df2f329 | 1358 | if (tmp) { |
02b95dad MH |
1359 | (*fieldp)->name = strndup(str, tmp - str); |
1360 | if ((*fieldp)->name == NULL) | |
1361 | return -ENOMEM; | |
b2a3c12b MH |
1362 | if (*str != '[') |
1363 | goodname = (*fieldp)->name; | |
7df2f329 MH |
1364 | pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); |
1365 | fieldp = &(*fieldp)->next; | |
1366 | } | |
1367 | } while (tmp); | |
02b95dad MH |
1368 | (*fieldp)->name = strdup(str); |
1369 | if ((*fieldp)->name == NULL) | |
1370 | return -ENOMEM; | |
b2a3c12b MH |
1371 | if (*str != '[') |
1372 | goodname = (*fieldp)->name; | |
7df2f329 | 1373 | pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); |
df0faf4b | 1374 | |
b2a3c12b | 1375 | /* If no name is specified, set the last field name (not array index)*/ |
02b95dad | 1376 | if (!arg->name) { |
b2a3c12b | 1377 | arg->name = strdup(goodname); |
02b95dad MH |
1378 | if (arg->name == NULL) |
1379 | return -ENOMEM; | |
1380 | } | |
146a1439 | 1381 | return 0; |
7df2f329 MH |
1382 | } |
1383 | ||
4235b045 | 1384 | /* Parse perf-probe event command */ |
146a1439 | 1385 | int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) |
50656eec | 1386 | { |
e1c01d61 | 1387 | char **argv; |
146a1439 | 1388 | int argc, i, ret = 0; |
fac13fd5 | 1389 | |
4235b045 | 1390 | argv = argv_split(cmd, &argc); |
146a1439 MH |
1391 | if (!argv) { |
1392 | pr_debug("Failed to split arguments.\n"); | |
1393 | return -ENOMEM; | |
1394 | } | |
1395 | if (argc - 1 > MAX_PROBE_ARGS) { | |
1396 | semantic_error("Too many probe arguments (%d).\n", argc - 1); | |
1397 | ret = -ERANGE; | |
1398 | goto out; | |
1399 | } | |
50656eec | 1400 | /* Parse probe point */ |
146a1439 MH |
1401 | ret = parse_perf_probe_point(argv[0], pev); |
1402 | if (ret < 0) | |
1403 | goto out; | |
50656eec | 1404 | |
e1c01d61 | 1405 | /* Copy arguments and ensure return probe has no C argument */ |
4235b045 | 1406 | pev->nargs = argc - 1; |
e334016f MH |
1407 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
1408 | if (pev->args == NULL) { | |
1409 | ret = -ENOMEM; | |
1410 | goto out; | |
1411 | } | |
146a1439 MH |
1412 | for (i = 0; i < pev->nargs && ret >= 0; i++) { |
1413 | ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); | |
1414 | if (ret >= 0 && | |
1415 | is_c_varname(pev->args[i].var) && pev->point.retprobe) { | |
4235b045 | 1416 | semantic_error("You can't specify local variable for" |
146a1439 MH |
1417 | " kretprobe.\n"); |
1418 | ret = -EINVAL; | |
1419 | } | |
e1c01d61 | 1420 | } |
146a1439 | 1421 | out: |
e1c01d61 | 1422 | argv_free(argv); |
146a1439 MH |
1423 | |
1424 | return ret; | |
50656eec MH |
1425 | } |
1426 | ||
4235b045 MH |
1427 | /* Return true if this perf_probe_event requires debuginfo */ |
1428 | bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) | |
1429 | { | |
1430 | int i; | |
1431 | ||
1432 | if (pev->point.file || pev->point.line || pev->point.lazy_line) | |
1433 | return true; | |
1434 | ||
1435 | for (i = 0; i < pev->nargs; i++) | |
48481938 | 1436 | if (is_c_varname(pev->args[i].var)) |
4235b045 MH |
1437 | return true; |
1438 | ||
1439 | return false; | |
1440 | } | |
1441 | ||
0e60836b SD |
1442 | /* Parse probe_events event into struct probe_point */ |
1443 | static int parse_probe_trace_command(const char *cmd, | |
190b57fc | 1444 | struct probe_trace_event *tev) |
4de189fe | 1445 | { |
0e60836b | 1446 | struct probe_trace_point *tp = &tev->point; |
4de189fe MH |
1447 | char pr; |
1448 | char *p; | |
bcbd0040 | 1449 | char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str; |
4de189fe MH |
1450 | int ret, i, argc; |
1451 | char **argv; | |
1452 | ||
0e60836b | 1453 | pr_debug("Parsing probe_events: %s\n", cmd); |
4235b045 | 1454 | argv = argv_split(cmd, &argc); |
146a1439 MH |
1455 | if (!argv) { |
1456 | pr_debug("Failed to split arguments.\n"); | |
1457 | return -ENOMEM; | |
1458 | } | |
1459 | if (argc < 2) { | |
1460 | semantic_error("Too few probe arguments.\n"); | |
1461 | ret = -ERANGE; | |
1462 | goto out; | |
1463 | } | |
4de189fe MH |
1464 | |
1465 | /* Scan event and group name. */ | |
bcbd0040 IT |
1466 | argv0_str = strdup(argv[0]); |
1467 | if (argv0_str == NULL) { | |
1468 | ret = -ENOMEM; | |
1469 | goto out; | |
1470 | } | |
1471 | fmt1_str = strtok_r(argv0_str, ":", &fmt); | |
1472 | fmt2_str = strtok_r(NULL, "/", &fmt); | |
1473 | fmt3_str = strtok_r(NULL, " \t", &fmt); | |
1474 | if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL | |
1475 | || fmt3_str == NULL) { | |
146a1439 MH |
1476 | semantic_error("Failed to parse event name: %s\n", argv[0]); |
1477 | ret = -EINVAL; | |
1478 | goto out; | |
1479 | } | |
bcbd0040 IT |
1480 | pr = fmt1_str[0]; |
1481 | tev->group = strdup(fmt2_str); | |
1482 | tev->event = strdup(fmt3_str); | |
1483 | if (tev->group == NULL || tev->event == NULL) { | |
1484 | ret = -ENOMEM; | |
1485 | goto out; | |
1486 | } | |
4235b045 | 1487 | pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr); |
4de189fe | 1488 | |
4235b045 | 1489 | tp->retprobe = (pr == 'r'); |
4de189fe | 1490 | |
190b57fc MH |
1491 | /* Scan module name(if there), function name and offset */ |
1492 | p = strchr(argv[1], ':'); | |
1493 | if (p) { | |
1494 | tp->module = strndup(argv[1], p - argv[1]); | |
1495 | p++; | |
1496 | } else | |
1497 | p = argv[1]; | |
bcbd0040 | 1498 | fmt1_str = strtok_r(p, "+", &fmt); |
5a6f6314 MH |
1499 | if (fmt1_str[0] == '0') /* only the address started with 0x */ |
1500 | tp->address = strtoul(fmt1_str, NULL, 0); | |
1501 | else { | |
1502 | /* Only the symbol-based probe has offset */ | |
1503 | tp->symbol = strdup(fmt1_str); | |
1504 | if (tp->symbol == NULL) { | |
1505 | ret = -ENOMEM; | |
1506 | goto out; | |
1507 | } | |
1508 | fmt2_str = strtok_r(NULL, "", &fmt); | |
1509 | if (fmt2_str == NULL) | |
1510 | tp->offset = 0; | |
1511 | else | |
1512 | tp->offset = strtoul(fmt2_str, NULL, 10); | |
bcbd0040 | 1513 | } |
4de189fe | 1514 | |
4235b045 | 1515 | tev->nargs = argc - 2; |
0e60836b | 1516 | tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); |
e334016f MH |
1517 | if (tev->args == NULL) { |
1518 | ret = -ENOMEM; | |
1519 | goto out; | |
1520 | } | |
4235b045 | 1521 | for (i = 0; i < tev->nargs; i++) { |
4de189fe MH |
1522 | p = strchr(argv[i + 2], '='); |
1523 | if (p) /* We don't need which register is assigned. */ | |
4235b045 MH |
1524 | *p++ = '\0'; |
1525 | else | |
1526 | p = argv[i + 2]; | |
02b95dad | 1527 | tev->args[i].name = strdup(argv[i + 2]); |
4235b045 | 1528 | /* TODO: parse regs and offset */ |
02b95dad MH |
1529 | tev->args[i].value = strdup(p); |
1530 | if (tev->args[i].name == NULL || tev->args[i].value == NULL) { | |
1531 | ret = -ENOMEM; | |
1532 | goto out; | |
1533 | } | |
4de189fe | 1534 | } |
146a1439 MH |
1535 | ret = 0; |
1536 | out: | |
bcbd0040 | 1537 | free(argv0_str); |
4de189fe | 1538 | argv_free(argv); |
146a1439 | 1539 | return ret; |
4de189fe MH |
1540 | } |
1541 | ||
7df2f329 MH |
1542 | /* Compose only probe arg */ |
1543 | int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len) | |
1544 | { | |
1545 | struct perf_probe_arg_field *field = pa->field; | |
1546 | int ret; | |
1547 | char *tmp = buf; | |
1548 | ||
48481938 MH |
1549 | if (pa->name && pa->var) |
1550 | ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var); | |
1551 | else | |
1552 | ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var); | |
7df2f329 MH |
1553 | if (ret <= 0) |
1554 | goto error; | |
1555 | tmp += ret; | |
1556 | len -= ret; | |
1557 | ||
1558 | while (field) { | |
b2a3c12b MH |
1559 | if (field->name[0] == '[') |
1560 | ret = e_snprintf(tmp, len, "%s", field->name); | |
1561 | else | |
1562 | ret = e_snprintf(tmp, len, "%s%s", | |
1563 | field->ref ? "->" : ".", field->name); | |
7df2f329 MH |
1564 | if (ret <= 0) |
1565 | goto error; | |
1566 | tmp += ret; | |
1567 | len -= ret; | |
1568 | field = field->next; | |
1569 | } | |
11a1ca35 MH |
1570 | |
1571 | if (pa->type) { | |
1572 | ret = e_snprintf(tmp, len, ":%s", pa->type); | |
1573 | if (ret <= 0) | |
1574 | goto error; | |
1575 | tmp += ret; | |
1576 | len -= ret; | |
1577 | } | |
1578 | ||
7df2f329 MH |
1579 | return tmp - buf; |
1580 | error: | |
5f03cba4 | 1581 | pr_debug("Failed to synthesize perf probe argument: %d\n", ret); |
146a1439 | 1582 | return ret; |
7df2f329 MH |
1583 | } |
1584 | ||
4235b045 MH |
1585 | /* Compose only probe point (not argument) */ |
1586 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp) | |
4de189fe | 1587 | { |
fb1587d8 MH |
1588 | char *buf, *tmp; |
1589 | char offs[32] = "", line[32] = "", file[32] = ""; | |
1590 | int ret, len; | |
4de189fe | 1591 | |
e334016f MH |
1592 | buf = zalloc(MAX_CMDLEN); |
1593 | if (buf == NULL) { | |
1594 | ret = -ENOMEM; | |
1595 | goto error; | |
1596 | } | |
4de189fe | 1597 | if (pp->offset) { |
fb1587d8 | 1598 | ret = e_snprintf(offs, 32, "+%lu", pp->offset); |
4de189fe MH |
1599 | if (ret <= 0) |
1600 | goto error; | |
1601 | } | |
1602 | if (pp->line) { | |
fb1587d8 MH |
1603 | ret = e_snprintf(line, 32, ":%d", pp->line); |
1604 | if (ret <= 0) | |
1605 | goto error; | |
1606 | } | |
1607 | if (pp->file) { | |
32ae2ade FBH |
1608 | tmp = pp->file; |
1609 | len = strlen(tmp); | |
1610 | if (len > 30) { | |
1611 | tmp = strchr(pp->file + len - 30, '/'); | |
1612 | tmp = tmp ? tmp + 1 : pp->file + len - 30; | |
1613 | } | |
1614 | ret = e_snprintf(file, 32, "@%s", tmp); | |
4de189fe MH |
1615 | if (ret <= 0) |
1616 | goto error; | |
1617 | } | |
1618 | ||
1619 | if (pp->function) | |
fb1587d8 MH |
1620 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function, |
1621 | offs, pp->retprobe ? "%return" : "", line, | |
1622 | file); | |
4de189fe | 1623 | else |
fb1587d8 | 1624 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line); |
4235b045 MH |
1625 | if (ret <= 0) |
1626 | goto error; | |
1627 | ||
1628 | return buf; | |
7ef17aaf | 1629 | error: |
5f03cba4 | 1630 | pr_debug("Failed to synthesize perf probe point: %d\n", ret); |
f5385650 | 1631 | free(buf); |
146a1439 | 1632 | return NULL; |
7ef17aaf MH |
1633 | } |
1634 | ||
4235b045 MH |
1635 | #if 0 |
1636 | char *synthesize_perf_probe_command(struct perf_probe_event *pev) | |
7ef17aaf MH |
1637 | { |
1638 | char *buf; | |
1639 | int i, len, ret; | |
1640 | ||
4235b045 MH |
1641 | buf = synthesize_perf_probe_point(&pev->point); |
1642 | if (!buf) | |
1643 | return NULL; | |
4de189fe | 1644 | |
4235b045 MH |
1645 | len = strlen(buf); |
1646 | for (i = 0; i < pev->nargs; i++) { | |
4de189fe | 1647 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
4235b045 MH |
1648 | pev->args[i].name); |
1649 | if (ret <= 0) { | |
1650 | free(buf); | |
1651 | return NULL; | |
1652 | } | |
4de189fe MH |
1653 | len += ret; |
1654 | } | |
4de189fe | 1655 | |
4235b045 MH |
1656 | return buf; |
1657 | } | |
1658 | #endif | |
1659 | ||
0e60836b | 1660 | static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref, |
4235b045 MH |
1661 | char **buf, size_t *buflen, |
1662 | int depth) | |
1663 | { | |
1664 | int ret; | |
1665 | if (ref->next) { | |
0e60836b | 1666 | depth = __synthesize_probe_trace_arg_ref(ref->next, buf, |
4235b045 MH |
1667 | buflen, depth + 1); |
1668 | if (depth < 0) | |
1669 | goto out; | |
1670 | } | |
1671 | ||
1672 | ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset); | |
1673 | if (ret < 0) | |
1674 | depth = ret; | |
1675 | else { | |
1676 | *buf += ret; | |
1677 | *buflen -= ret; | |
1678 | } | |
1679 | out: | |
1680 | return depth; | |
4de189fe | 1681 | |
4de189fe MH |
1682 | } |
1683 | ||
0e60836b | 1684 | static int synthesize_probe_trace_arg(struct probe_trace_arg *arg, |
4235b045 | 1685 | char *buf, size_t buflen) |
50656eec | 1686 | { |
0e60836b | 1687 | struct probe_trace_arg_ref *ref = arg->ref; |
4235b045 MH |
1688 | int ret, depth = 0; |
1689 | char *tmp = buf; | |
1690 | ||
1691 | /* Argument name or separator */ | |
1692 | if (arg->name) | |
1693 | ret = e_snprintf(buf, buflen, " %s=", arg->name); | |
1694 | else | |
1695 | ret = e_snprintf(buf, buflen, " "); | |
1696 | if (ret < 0) | |
1697 | return ret; | |
1698 | buf += ret; | |
1699 | buflen -= ret; | |
1700 | ||
b7dcb857 MH |
1701 | /* Special case: @XXX */ |
1702 | if (arg->value[0] == '@' && arg->ref) | |
1703 | ref = ref->next; | |
1704 | ||
4235b045 | 1705 | /* Dereferencing arguments */ |
b7dcb857 | 1706 | if (ref) { |
0e60836b | 1707 | depth = __synthesize_probe_trace_arg_ref(ref, &buf, |
4235b045 MH |
1708 | &buflen, 1); |
1709 | if (depth < 0) | |
1710 | return depth; | |
1711 | } | |
1712 | ||
1713 | /* Print argument value */ | |
b7dcb857 MH |
1714 | if (arg->value[0] == '@' && arg->ref) |
1715 | ret = e_snprintf(buf, buflen, "%s%+ld", arg->value, | |
1716 | arg->ref->offset); | |
1717 | else | |
1718 | ret = e_snprintf(buf, buflen, "%s", arg->value); | |
4235b045 MH |
1719 | if (ret < 0) |
1720 | return ret; | |
1721 | buf += ret; | |
1722 | buflen -= ret; | |
1723 | ||
1724 | /* Closing */ | |
1725 | while (depth--) { | |
1726 | ret = e_snprintf(buf, buflen, ")"); | |
1727 | if (ret < 0) | |
1728 | return ret; | |
1729 | buf += ret; | |
1730 | buflen -= ret; | |
1731 | } | |
4984912e MH |
1732 | /* Print argument type */ |
1733 | if (arg->type) { | |
1734 | ret = e_snprintf(buf, buflen, ":%s", arg->type); | |
1735 | if (ret <= 0) | |
1736 | return ret; | |
1737 | buf += ret; | |
1738 | } | |
4235b045 MH |
1739 | |
1740 | return buf - tmp; | |
1741 | } | |
1742 | ||
0e60836b | 1743 | char *synthesize_probe_trace_command(struct probe_trace_event *tev) |
4235b045 | 1744 | { |
0e60836b | 1745 | struct probe_trace_point *tp = &tev->point; |
50656eec MH |
1746 | char *buf; |
1747 | int i, len, ret; | |
1748 | ||
e334016f MH |
1749 | buf = zalloc(MAX_CMDLEN); |
1750 | if (buf == NULL) | |
1751 | return NULL; | |
1752 | ||
eb948e50 MH |
1753 | len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p', |
1754 | tev->group, tev->event); | |
1755 | if (len <= 0) | |
1756 | goto error; | |
1757 | ||
1758 | /* Uprobes must have tp->address and tp->module */ | |
1759 | if (tev->uprobes && (!tp->address || !tp->module)) | |
1760 | goto error; | |
1761 | ||
1762 | /* Use the tp->address for uprobes */ | |
225466f1 | 1763 | if (tev->uprobes) |
eb948e50 MH |
1764 | ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx", |
1765 | tp->module, tp->address); | |
225466f1 | 1766 | else |
eb948e50 | 1767 | ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu", |
225466f1 SD |
1768 | tp->module ?: "", tp->module ? ":" : "", |
1769 | tp->symbol, tp->offset); | |
1770 | ||
eb948e50 | 1771 | if (ret <= 0) |
50656eec | 1772 | goto error; |
eb948e50 | 1773 | len += ret; |
50656eec | 1774 | |
4235b045 | 1775 | for (i = 0; i < tev->nargs; i++) { |
0e60836b | 1776 | ret = synthesize_probe_trace_arg(&tev->args[i], buf + len, |
4235b045 | 1777 | MAX_CMDLEN - len); |
4de189fe | 1778 | if (ret <= 0) |
50656eec MH |
1779 | goto error; |
1780 | len += ret; | |
1781 | } | |
50656eec | 1782 | |
4235b045 | 1783 | return buf; |
50656eec | 1784 | error: |
4235b045 MH |
1785 | free(buf); |
1786 | return NULL; | |
1787 | } | |
50656eec | 1788 | |
5a6f6314 MH |
1789 | static int find_perf_probe_point_from_map(struct probe_trace_point *tp, |
1790 | struct perf_probe_point *pp, | |
1791 | bool is_kprobe) | |
1792 | { | |
1793 | struct symbol *sym = NULL; | |
1794 | struct map *map; | |
1795 | u64 addr; | |
1796 | int ret = -ENOENT; | |
1797 | ||
1798 | if (!is_kprobe) { | |
1799 | map = dso__new_map(tp->module); | |
1800 | if (!map) | |
1801 | goto out; | |
1802 | addr = tp->address; | |
1803 | sym = map__find_symbol(map, addr, NULL); | |
1804 | } else { | |
1805 | addr = kernel_get_symbol_address_by_name(tp->symbol, true); | |
1806 | if (addr) { | |
1807 | addr += tp->offset; | |
1808 | sym = __find_kernel_function(addr, &map); | |
1809 | } | |
1810 | } | |
1811 | if (!sym) | |
1812 | goto out; | |
1813 | ||
1814 | pp->retprobe = tp->retprobe; | |
1815 | pp->offset = addr - map->unmap_ip(map, sym->start); | |
1816 | pp->function = strdup(sym->name); | |
1817 | ret = pp->function ? 0 : -ENOMEM; | |
1818 | ||
1819 | out: | |
1820 | if (map && !is_kprobe) { | |
1821 | dso__delete(map->dso); | |
1822 | map__delete(map); | |
1823 | } | |
1824 | ||
1825 | return ret; | |
1826 | } | |
1827 | ||
1828 | static int convert_to_perf_probe_point(struct probe_trace_point *tp, | |
1829 | struct perf_probe_point *pp, | |
1830 | bool is_kprobe) | |
1831 | { | |
1832 | char buf[128]; | |
1833 | int ret; | |
1834 | ||
1835 | ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe); | |
1836 | if (!ret) | |
1837 | return 0; | |
1838 | ret = find_perf_probe_point_from_map(tp, pp, is_kprobe); | |
1839 | if (!ret) | |
1840 | return 0; | |
1841 | ||
1842 | pr_debug("Failed to find probe point from both of dwarf and map.\n"); | |
1843 | ||
1844 | if (tp->symbol) { | |
1845 | pp->function = strdup(tp->symbol); | |
1846 | pp->offset = tp->offset; | |
1847 | } else if (!tp->module && !is_kprobe) { | |
1848 | ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address); | |
1849 | if (ret < 0) | |
1850 | return ret; | |
1851 | pp->function = strdup(buf); | |
1852 | pp->offset = 0; | |
1853 | } | |
1854 | if (pp->function == NULL) | |
1855 | return -ENOMEM; | |
1856 | ||
1857 | pp->retprobe = tp->retprobe; | |
1858 | ||
1859 | return 0; | |
1860 | } | |
1861 | ||
0e60836b | 1862 | static int convert_to_perf_probe_event(struct probe_trace_event *tev, |
225466f1 | 1863 | struct perf_probe_event *pev, bool is_kprobe) |
4235b045 | 1864 | { |
02b95dad | 1865 | char buf[64] = ""; |
146a1439 | 1866 | int i, ret; |
4235b045 | 1867 | |
4b4da7f7 | 1868 | /* Convert event/group name */ |
02b95dad MH |
1869 | pev->event = strdup(tev->event); |
1870 | pev->group = strdup(tev->group); | |
1871 | if (pev->event == NULL || pev->group == NULL) | |
1872 | return -ENOMEM; | |
fb1587d8 | 1873 | |
4b4da7f7 | 1874 | /* Convert trace_point to probe_point */ |
5a6f6314 | 1875 | ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe); |
146a1439 MH |
1876 | if (ret < 0) |
1877 | return ret; | |
4b4da7f7 | 1878 | |
4235b045 MH |
1879 | /* Convert trace_arg to probe_arg */ |
1880 | pev->nargs = tev->nargs; | |
e334016f MH |
1881 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
1882 | if (pev->args == NULL) | |
1883 | return -ENOMEM; | |
02b95dad | 1884 | for (i = 0; i < tev->nargs && ret >= 0; i++) { |
4235b045 | 1885 | if (tev->args[i].name) |
02b95dad | 1886 | pev->args[i].name = strdup(tev->args[i].name); |
4235b045 | 1887 | else { |
0e60836b | 1888 | ret = synthesize_probe_trace_arg(&tev->args[i], |
146a1439 | 1889 | buf, 64); |
02b95dad | 1890 | pev->args[i].name = strdup(buf); |
4235b045 | 1891 | } |
02b95dad MH |
1892 | if (pev->args[i].name == NULL && ret >= 0) |
1893 | ret = -ENOMEM; | |
1894 | } | |
146a1439 MH |
1895 | |
1896 | if (ret < 0) | |
1897 | clear_perf_probe_event(pev); | |
1898 | ||
1899 | return ret; | |
4235b045 MH |
1900 | } |
1901 | ||
1902 | void clear_perf_probe_event(struct perf_probe_event *pev) | |
1903 | { | |
7df2f329 | 1904 | struct perf_probe_arg_field *field, *next; |
4235b045 MH |
1905 | int i; |
1906 | ||
f5385650 ACM |
1907 | free(pev->event); |
1908 | free(pev->group); | |
7afb3fab | 1909 | free(pev->target); |
9b118aca | 1910 | clear_perf_probe_point(&pev->point); |
f5385650 | 1911 | |
7df2f329 | 1912 | for (i = 0; i < pev->nargs; i++) { |
f5385650 ACM |
1913 | free(pev->args[i].name); |
1914 | free(pev->args[i].var); | |
1915 | free(pev->args[i].type); | |
7df2f329 MH |
1916 | field = pev->args[i].field; |
1917 | while (field) { | |
1918 | next = field->next; | |
74cf249d | 1919 | zfree(&field->name); |
7df2f329 MH |
1920 | free(field); |
1921 | field = next; | |
1922 | } | |
1923 | } | |
f5385650 | 1924 | free(pev->args); |
4235b045 MH |
1925 | memset(pev, 0, sizeof(*pev)); |
1926 | } | |
1927 | ||
0e60836b | 1928 | static void clear_probe_trace_event(struct probe_trace_event *tev) |
4235b045 | 1929 | { |
0e60836b | 1930 | struct probe_trace_arg_ref *ref, *next; |
4235b045 MH |
1931 | int i; |
1932 | ||
f5385650 ACM |
1933 | free(tev->event); |
1934 | free(tev->group); | |
1935 | free(tev->point.symbol); | |
1936 | free(tev->point.module); | |
4235b045 | 1937 | for (i = 0; i < tev->nargs; i++) { |
f5385650 ACM |
1938 | free(tev->args[i].name); |
1939 | free(tev->args[i].value); | |
1940 | free(tev->args[i].type); | |
4235b045 MH |
1941 | ref = tev->args[i].ref; |
1942 | while (ref) { | |
1943 | next = ref->next; | |
1944 | free(ref); | |
1945 | ref = next; | |
1946 | } | |
1947 | } | |
f5385650 | 1948 | free(tev->args); |
4235b045 | 1949 | memset(tev, 0, sizeof(*tev)); |
50656eec MH |
1950 | } |
1951 | ||
5e45187c | 1952 | static void print_open_warning(int err, bool is_kprobe) |
225466f1 | 1953 | { |
5f03cba4 | 1954 | char sbuf[STRERR_BUFSIZE]; |
225466f1 | 1955 | |
5e45187c | 1956 | if (err == -ENOENT) { |
225466f1 SD |
1957 | const char *config; |
1958 | ||
1959 | if (!is_kprobe) | |
1960 | config = "CONFIG_UPROBE_EVENTS"; | |
1961 | else | |
1962 | config = "CONFIG_KPROBE_EVENTS"; | |
1963 | ||
5e45187c MH |
1964 | pr_warning("%cprobe_events file does not exist" |
1965 | " - please rebuild kernel with %s.\n", | |
1966 | is_kprobe ? 'k' : 'u', config); | |
1967 | } else if (err == -ENOTSUP) | |
23773ca1 | 1968 | pr_warning("Tracefs or debugfs is not mounted.\n"); |
5e45187c MH |
1969 | else |
1970 | pr_warning("Failed to open %cprobe_events: %s\n", | |
1971 | is_kprobe ? 'k' : 'u', | |
1972 | strerror_r(-err, sbuf, sizeof(sbuf))); | |
225466f1 SD |
1973 | } |
1974 | ||
467ec085 MH |
1975 | static void print_both_open_warning(int kerr, int uerr) |
1976 | { | |
1977 | /* Both kprobes and uprobes are disabled, warn it. */ | |
1978 | if (kerr == -ENOTSUP && uerr == -ENOTSUP) | |
23773ca1 | 1979 | pr_warning("Tracefs or debugfs is not mounted.\n"); |
467ec085 MH |
1980 | else if (kerr == -ENOENT && uerr == -ENOENT) |
1981 | pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS " | |
1982 | "or/and CONFIG_UPROBE_EVENTS.\n"); | |
1983 | else { | |
5f03cba4 | 1984 | char sbuf[STRERR_BUFSIZE]; |
467ec085 MH |
1985 | pr_warning("Failed to open kprobe events: %s.\n", |
1986 | strerror_r(-kerr, sbuf, sizeof(sbuf))); | |
1987 | pr_warning("Failed to open uprobe events: %s.\n", | |
1988 | strerror_r(-uerr, sbuf, sizeof(sbuf))); | |
1989 | } | |
1990 | } | |
1991 | ||
5e45187c | 1992 | static int open_probe_events(const char *trace_file, bool readwrite) |
4de189fe MH |
1993 | { |
1994 | char buf[PATH_MAX]; | |
7ca5989d | 1995 | const char *__debugfs; |
23773ca1 | 1996 | const char *tracing_dir = ""; |
4de189fe MH |
1997 | int ret; |
1998 | ||
23773ca1 SRRH |
1999 | __debugfs = tracefs_find_mountpoint(); |
2000 | if (__debugfs == NULL) { | |
2001 | tracing_dir = "tracing/"; | |
7ca5989d | 2002 | |
23773ca1 SRRH |
2003 | __debugfs = debugfs_find_mountpoint(); |
2004 | if (__debugfs == NULL) | |
2005 | return -ENOTSUP; | |
2006 | } | |
2007 | ||
2008 | ret = e_snprintf(buf, PATH_MAX, "%s/%s%s", | |
2009 | __debugfs, tracing_dir, trace_file); | |
146a1439 | 2010 | if (ret >= 0) { |
7ca5989d | 2011 | pr_debug("Opening %s write=%d\n", buf, readwrite); |
146a1439 MH |
2012 | if (readwrite && !probe_event_dry_run) |
2013 | ret = open(buf, O_RDWR, O_APPEND); | |
2014 | else | |
2015 | ret = open(buf, O_RDONLY, 0); | |
f4d7da49 | 2016 | |
225466f1 | 2017 | if (ret < 0) |
5e45187c | 2018 | ret = -errno; |
4de189fe MH |
2019 | } |
2020 | return ret; | |
2021 | } | |
2022 | ||
225466f1 SD |
2023 | static int open_kprobe_events(bool readwrite) |
2024 | { | |
23773ca1 | 2025 | return open_probe_events("kprobe_events", readwrite); |
225466f1 SD |
2026 | } |
2027 | ||
2028 | static int open_uprobe_events(bool readwrite) | |
2029 | { | |
23773ca1 | 2030 | return open_probe_events("uprobe_events", readwrite); |
225466f1 SD |
2031 | } |
2032 | ||
2033 | /* Get raw string list of current kprobe_events or uprobe_events */ | |
0e60836b | 2034 | static struct strlist *get_probe_trace_command_rawlist(int fd) |
4de189fe MH |
2035 | { |
2036 | int ret, idx; | |
2037 | FILE *fp; | |
2038 | char buf[MAX_CMDLEN]; | |
2039 | char *p; | |
2040 | struct strlist *sl; | |
2041 | ||
2042 | sl = strlist__new(true, NULL); | |
2043 | ||
2044 | fp = fdopen(dup(fd), "r"); | |
2045 | while (!feof(fp)) { | |
2046 | p = fgets(buf, MAX_CMDLEN, fp); | |
2047 | if (!p) | |
2048 | break; | |
2049 | ||
2050 | idx = strlen(p) - 1; | |
2051 | if (p[idx] == '\n') | |
2052 | p[idx] = '\0'; | |
2053 | ret = strlist__add(sl, buf); | |
146a1439 | 2054 | if (ret < 0) { |
6eb08660 | 2055 | pr_debug("strlist__add failed (%d)\n", ret); |
146a1439 MH |
2056 | strlist__delete(sl); |
2057 | return NULL; | |
2058 | } | |
4de189fe MH |
2059 | } |
2060 | fclose(fp); | |
2061 | ||
2062 | return sl; | |
2063 | } | |
2064 | ||
9aaf5a5f MH |
2065 | struct kprobe_blacklist_node { |
2066 | struct list_head list; | |
2067 | unsigned long start; | |
2068 | unsigned long end; | |
2069 | char *symbol; | |
2070 | }; | |
2071 | ||
2072 | static void kprobe_blacklist__delete(struct list_head *blacklist) | |
2073 | { | |
2074 | struct kprobe_blacklist_node *node; | |
2075 | ||
2076 | while (!list_empty(blacklist)) { | |
2077 | node = list_first_entry(blacklist, | |
2078 | struct kprobe_blacklist_node, list); | |
2079 | list_del(&node->list); | |
2080 | free(node->symbol); | |
2081 | free(node); | |
2082 | } | |
2083 | } | |
2084 | ||
2085 | static int kprobe_blacklist__load(struct list_head *blacklist) | |
2086 | { | |
2087 | struct kprobe_blacklist_node *node; | |
2088 | const char *__debugfs = debugfs_find_mountpoint(); | |
2089 | char buf[PATH_MAX], *p; | |
2090 | FILE *fp; | |
2091 | int ret; | |
2092 | ||
2093 | if (__debugfs == NULL) | |
2094 | return -ENOTSUP; | |
2095 | ||
2096 | ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs); | |
2097 | if (ret < 0) | |
2098 | return ret; | |
2099 | ||
2100 | fp = fopen(buf, "r"); | |
2101 | if (!fp) | |
2102 | return -errno; | |
2103 | ||
2104 | ret = 0; | |
2105 | while (fgets(buf, PATH_MAX, fp)) { | |
2106 | node = zalloc(sizeof(*node)); | |
2107 | if (!node) { | |
2108 | ret = -ENOMEM; | |
2109 | break; | |
2110 | } | |
2111 | INIT_LIST_HEAD(&node->list); | |
2112 | list_add_tail(&node->list, blacklist); | |
2113 | if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) { | |
2114 | ret = -EINVAL; | |
2115 | break; | |
2116 | } | |
2117 | p = strchr(buf, '\t'); | |
2118 | if (p) { | |
2119 | p++; | |
2120 | if (p[strlen(p) - 1] == '\n') | |
2121 | p[strlen(p) - 1] = '\0'; | |
2122 | } else | |
2123 | p = (char *)"unknown"; | |
2124 | node->symbol = strdup(p); | |
2125 | if (!node->symbol) { | |
2126 | ret = -ENOMEM; | |
2127 | break; | |
2128 | } | |
2129 | pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n", | |
2130 | node->start, node->end, node->symbol); | |
2131 | ret++; | |
2132 | } | |
2133 | if (ret < 0) | |
2134 | kprobe_blacklist__delete(blacklist); | |
2135 | fclose(fp); | |
2136 | ||
2137 | return ret; | |
2138 | } | |
2139 | ||
2140 | static struct kprobe_blacklist_node * | |
2141 | kprobe_blacklist__find_by_address(struct list_head *blacklist, | |
2142 | unsigned long address) | |
2143 | { | |
2144 | struct kprobe_blacklist_node *node; | |
2145 | ||
2146 | list_for_each_entry(node, blacklist, list) { | |
2147 | if (node->start <= address && address <= node->end) | |
2148 | return node; | |
2149 | } | |
2150 | ||
2151 | return NULL; | |
2152 | } | |
2153 | ||
278498d4 | 2154 | /* Show an event */ |
fb226ccd MH |
2155 | static int show_perf_probe_event(struct perf_probe_event *pev, |
2156 | const char *module) | |
278498d4 | 2157 | { |
7e990a51 | 2158 | int i, ret; |
278498d4 | 2159 | char buf[128]; |
4235b045 | 2160 | char *place; |
278498d4 | 2161 | |
4235b045 MH |
2162 | /* Synthesize only event probe point */ |
2163 | place = synthesize_perf_probe_point(&pev->point); | |
146a1439 MH |
2164 | if (!place) |
2165 | return -EINVAL; | |
4235b045 MH |
2166 | |
2167 | ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event); | |
7e990a51 | 2168 | if (ret < 0) |
146a1439 MH |
2169 | return ret; |
2170 | ||
5e17b28f | 2171 | pr_info(" %-20s (on %s", buf, place); |
fb226ccd | 2172 | if (module) |
5e17b28f | 2173 | pr_info(" in %s", module); |
278498d4 | 2174 | |
4235b045 | 2175 | if (pev->nargs > 0) { |
5e17b28f | 2176 | pr_info(" with"); |
7df2f329 | 2177 | for (i = 0; i < pev->nargs; i++) { |
146a1439 MH |
2178 | ret = synthesize_perf_probe_arg(&pev->args[i], |
2179 | buf, 128); | |
2180 | if (ret < 0) | |
2181 | break; | |
5e17b28f | 2182 | pr_info(" %s", buf); |
7df2f329 | 2183 | } |
278498d4 | 2184 | } |
5e17b28f | 2185 | pr_info(")\n"); |
4235b045 | 2186 | free(place); |
146a1439 | 2187 | return ret; |
278498d4 MH |
2188 | } |
2189 | ||
225466f1 | 2190 | static int __show_perf_probe_events(int fd, bool is_kprobe) |
4de189fe | 2191 | { |
225466f1 | 2192 | int ret = 0; |
0e60836b | 2193 | struct probe_trace_event tev; |
4235b045 | 2194 | struct perf_probe_event pev; |
4de189fe MH |
2195 | struct strlist *rawlist; |
2196 | struct str_node *ent; | |
2197 | ||
4235b045 MH |
2198 | memset(&tev, 0, sizeof(tev)); |
2199 | memset(&pev, 0, sizeof(pev)); | |
72041334 | 2200 | |
0e60836b | 2201 | rawlist = get_probe_trace_command_rawlist(fd); |
146a1439 | 2202 | if (!rawlist) |
6eb08660 | 2203 | return -ENOMEM; |
4de189fe | 2204 | |
adf365f4 | 2205 | strlist__for_each(ent, rawlist) { |
0e60836b | 2206 | ret = parse_probe_trace_command(ent->s, &tev); |
146a1439 | 2207 | if (ret >= 0) { |
225466f1 SD |
2208 | ret = convert_to_perf_probe_event(&tev, &pev, |
2209 | is_kprobe); | |
146a1439 | 2210 | if (ret >= 0) |
fb226ccd MH |
2211 | ret = show_perf_probe_event(&pev, |
2212 | tev.point.module); | |
146a1439 | 2213 | } |
4235b045 | 2214 | clear_perf_probe_event(&pev); |
0e60836b | 2215 | clear_probe_trace_event(&tev); |
146a1439 MH |
2216 | if (ret < 0) |
2217 | break; | |
4de189fe | 2218 | } |
4de189fe | 2219 | strlist__delete(rawlist); |
146a1439 MH |
2220 | |
2221 | return ret; | |
4de189fe MH |
2222 | } |
2223 | ||
225466f1 SD |
2224 | /* List up current perf-probe events */ |
2225 | int show_perf_probe_events(void) | |
2226 | { | |
5e45187c | 2227 | int kp_fd, up_fd, ret; |
225466f1 SD |
2228 | |
2229 | setup_pager(); | |
225466f1 | 2230 | |
ee45b6c2 | 2231 | ret = init_symbol_maps(false); |
225466f1 SD |
2232 | if (ret < 0) |
2233 | return ret; | |
2234 | ||
5e45187c MH |
2235 | kp_fd = open_kprobe_events(false); |
2236 | if (kp_fd >= 0) { | |
2237 | ret = __show_perf_probe_events(kp_fd, true); | |
2238 | close(kp_fd); | |
2239 | if (ret < 0) | |
2240 | goto out; | |
2241 | } | |
225466f1 | 2242 | |
5e45187c MH |
2243 | up_fd = open_uprobe_events(false); |
2244 | if (kp_fd < 0 && up_fd < 0) { | |
467ec085 | 2245 | print_both_open_warning(kp_fd, up_fd); |
5e45187c MH |
2246 | ret = kp_fd; |
2247 | goto out; | |
225466f1 SD |
2248 | } |
2249 | ||
5e45187c MH |
2250 | if (up_fd >= 0) { |
2251 | ret = __show_perf_probe_events(up_fd, false); | |
2252 | close(up_fd); | |
2253 | } | |
2254 | out: | |
ee45b6c2 | 2255 | exit_symbol_maps(); |
225466f1 SD |
2256 | return ret; |
2257 | } | |
2258 | ||
b498ce1f | 2259 | /* Get current perf-probe event names */ |
0e60836b | 2260 | static struct strlist *get_probe_trace_event_names(int fd, bool include_group) |
b498ce1f | 2261 | { |
fa28244d | 2262 | char buf[128]; |
b498ce1f MH |
2263 | struct strlist *sl, *rawlist; |
2264 | struct str_node *ent; | |
0e60836b | 2265 | struct probe_trace_event tev; |
146a1439 | 2266 | int ret = 0; |
b498ce1f | 2267 | |
4235b045 | 2268 | memset(&tev, 0, sizeof(tev)); |
0e60836b | 2269 | rawlist = get_probe_trace_command_rawlist(fd); |
6eb08660 MH |
2270 | if (!rawlist) |
2271 | return NULL; | |
e1d2017b | 2272 | sl = strlist__new(true, NULL); |
adf365f4 | 2273 | strlist__for_each(ent, rawlist) { |
0e60836b | 2274 | ret = parse_probe_trace_command(ent->s, &tev); |
146a1439 MH |
2275 | if (ret < 0) |
2276 | break; | |
fa28244d | 2277 | if (include_group) { |
146a1439 MH |
2278 | ret = e_snprintf(buf, 128, "%s:%s", tev.group, |
2279 | tev.event); | |
2280 | if (ret >= 0) | |
2281 | ret = strlist__add(sl, buf); | |
fa28244d | 2282 | } else |
146a1439 | 2283 | ret = strlist__add(sl, tev.event); |
0e60836b | 2284 | clear_probe_trace_event(&tev); |
146a1439 MH |
2285 | if (ret < 0) |
2286 | break; | |
b498ce1f | 2287 | } |
b498ce1f MH |
2288 | strlist__delete(rawlist); |
2289 | ||
146a1439 MH |
2290 | if (ret < 0) { |
2291 | strlist__delete(sl); | |
2292 | return NULL; | |
2293 | } | |
b498ce1f MH |
2294 | return sl; |
2295 | } | |
2296 | ||
0e60836b | 2297 | static int write_probe_trace_event(int fd, struct probe_trace_event *tev) |
50656eec | 2298 | { |
6eca8cc3 | 2299 | int ret = 0; |
0e60836b | 2300 | char *buf = synthesize_probe_trace_command(tev); |
5f03cba4 | 2301 | char sbuf[STRERR_BUFSIZE]; |
50656eec | 2302 | |
146a1439 | 2303 | if (!buf) { |
0e60836b | 2304 | pr_debug("Failed to synthesize probe trace event.\n"); |
146a1439 MH |
2305 | return -EINVAL; |
2306 | } | |
2307 | ||
fa28244d | 2308 | pr_debug("Writing event: %s\n", buf); |
f4d7da49 MH |
2309 | if (!probe_event_dry_run) { |
2310 | ret = write(fd, buf, strlen(buf)); | |
7949ba1f NK |
2311 | if (ret <= 0) { |
2312 | ret = -errno; | |
146a1439 | 2313 | pr_warning("Failed to write event: %s\n", |
5f03cba4 | 2314 | strerror_r(errno, sbuf, sizeof(sbuf))); |
7949ba1f | 2315 | } |
f4d7da49 | 2316 | } |
4235b045 | 2317 | free(buf); |
146a1439 | 2318 | return ret; |
50656eec MH |
2319 | } |
2320 | ||
146a1439 MH |
2321 | static int get_new_event_name(char *buf, size_t len, const char *base, |
2322 | struct strlist *namelist, bool allow_suffix) | |
b498ce1f MH |
2323 | { |
2324 | int i, ret; | |
17f88fcd MH |
2325 | |
2326 | /* Try no suffix */ | |
2327 | ret = e_snprintf(buf, len, "%s", base); | |
146a1439 | 2328 | if (ret < 0) { |
5f03cba4 | 2329 | pr_debug("snprintf() failed: %d\n", ret); |
146a1439 MH |
2330 | return ret; |
2331 | } | |
17f88fcd | 2332 | if (!strlist__has_entry(namelist, buf)) |
146a1439 | 2333 | return 0; |
17f88fcd | 2334 | |
d761b08b MH |
2335 | if (!allow_suffix) { |
2336 | pr_warning("Error: event \"%s\" already exists. " | |
2337 | "(Use -f to force duplicates.)\n", base); | |
146a1439 | 2338 | return -EEXIST; |
d761b08b MH |
2339 | } |
2340 | ||
17f88fcd MH |
2341 | /* Try to add suffix */ |
2342 | for (i = 1; i < MAX_EVENT_INDEX; i++) { | |
b498ce1f | 2343 | ret = e_snprintf(buf, len, "%s_%d", base, i); |
146a1439 | 2344 | if (ret < 0) { |
5f03cba4 | 2345 | pr_debug("snprintf() failed: %d\n", ret); |
146a1439 MH |
2346 | return ret; |
2347 | } | |
b498ce1f MH |
2348 | if (!strlist__has_entry(namelist, buf)) |
2349 | break; | |
2350 | } | |
146a1439 MH |
2351 | if (i == MAX_EVENT_INDEX) { |
2352 | pr_warning("Too many events are on the same function.\n"); | |
2353 | ret = -ERANGE; | |
2354 | } | |
2355 | ||
2356 | return ret; | |
b498ce1f MH |
2357 | } |
2358 | ||
79702f61 MH |
2359 | /* Warn if the current kernel's uprobe implementation is old */ |
2360 | static void warn_uprobe_event_compat(struct probe_trace_event *tev) | |
2361 | { | |
2362 | int i; | |
2363 | char *buf = synthesize_probe_trace_command(tev); | |
2364 | ||
2365 | /* Old uprobe event doesn't support memory dereference */ | |
2366 | if (!tev->uprobes || tev->nargs == 0 || !buf) | |
2367 | goto out; | |
2368 | ||
2369 | for (i = 0; i < tev->nargs; i++) | |
2370 | if (strglobmatch(tev->args[i].value, "[$@+-]*")) { | |
2371 | pr_warning("Please upgrade your kernel to at least " | |
2372 | "3.14 to have access to feature %s\n", | |
2373 | tev->args[i].value); | |
2374 | break; | |
2375 | } | |
2376 | out: | |
2377 | free(buf); | |
2378 | } | |
2379 | ||
0e60836b SD |
2380 | static int __add_probe_trace_events(struct perf_probe_event *pev, |
2381 | struct probe_trace_event *tevs, | |
146a1439 | 2382 | int ntevs, bool allow_suffix) |
50656eec | 2383 | { |
146a1439 | 2384 | int i, fd, ret; |
0e60836b | 2385 | struct probe_trace_event *tev = NULL; |
4235b045 MH |
2386 | char buf[64]; |
2387 | const char *event, *group; | |
b498ce1f | 2388 | struct strlist *namelist; |
9aaf5a5f MH |
2389 | LIST_HEAD(blacklist); |
2390 | struct kprobe_blacklist_node *node; | |
50656eec | 2391 | |
225466f1 SD |
2392 | if (pev->uprobes) |
2393 | fd = open_uprobe_events(true); | |
2394 | else | |
2395 | fd = open_kprobe_events(true); | |
2396 | ||
5e45187c MH |
2397 | if (fd < 0) { |
2398 | print_open_warning(fd, !pev->uprobes); | |
146a1439 | 2399 | return fd; |
5e45187c MH |
2400 | } |
2401 | ||
b498ce1f | 2402 | /* Get current event names */ |
0e60836b | 2403 | namelist = get_probe_trace_event_names(fd, false); |
146a1439 MH |
2404 | if (!namelist) { |
2405 | pr_debug("Failed to get current event list.\n"); | |
2406 | return -EIO; | |
2407 | } | |
9aaf5a5f MH |
2408 | /* Get kprobe blacklist if exists */ |
2409 | if (!pev->uprobes) { | |
2410 | ret = kprobe_blacklist__load(&blacklist); | |
2411 | if (ret < 0) | |
2412 | pr_debug("No kprobe blacklist support, ignored\n"); | |
2413 | } | |
4235b045 | 2414 | |
146a1439 | 2415 | ret = 0; |
5e17b28f | 2416 | pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":"); |
02b95dad | 2417 | for (i = 0; i < ntevs; i++) { |
4235b045 | 2418 | tev = &tevs[i]; |
9aaf5a5f MH |
2419 | /* Ensure that the address is NOT blacklisted */ |
2420 | node = kprobe_blacklist__find_by_address(&blacklist, | |
2421 | tev->point.address); | |
2422 | if (node) { | |
2423 | pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol); | |
2424 | continue; | |
2425 | } | |
2426 | ||
4235b045 MH |
2427 | if (pev->event) |
2428 | event = pev->event; | |
2429 | else | |
2430 | if (pev->point.function) | |
2431 | event = pev->point.function; | |
2432 | else | |
2433 | event = tev->point.symbol; | |
2434 | if (pev->group) | |
2435 | group = pev->group; | |
2436 | else | |
2437 | group = PERFPROBE_GROUP; | |
2438 | ||
2439 | /* Get an unused new event name */ | |
146a1439 MH |
2440 | ret = get_new_event_name(buf, 64, event, |
2441 | namelist, allow_suffix); | |
2442 | if (ret < 0) | |
2443 | break; | |
4235b045 MH |
2444 | event = buf; |
2445 | ||
02b95dad MH |
2446 | tev->event = strdup(event); |
2447 | tev->group = strdup(group); | |
2448 | if (tev->event == NULL || tev->group == NULL) { | |
2449 | ret = -ENOMEM; | |
2450 | break; | |
2451 | } | |
0e60836b | 2452 | ret = write_probe_trace_event(fd, tev); |
146a1439 MH |
2453 | if (ret < 0) |
2454 | break; | |
4235b045 MH |
2455 | /* Add added event name to namelist */ |
2456 | strlist__add(namelist, event); | |
2457 | ||
2458 | /* Trick here - save current event/group */ | |
2459 | event = pev->event; | |
2460 | group = pev->group; | |
2461 | pev->event = tev->event; | |
2462 | pev->group = tev->group; | |
fb226ccd | 2463 | show_perf_probe_event(pev, tev->point.module); |
4235b045 MH |
2464 | /* Trick here - restore current event/group */ |
2465 | pev->event = (char *)event; | |
2466 | pev->group = (char *)group; | |
2467 | ||
2468 | /* | |
2469 | * Probes after the first probe which comes from same | |
2470 | * user input are always allowed to add suffix, because | |
2471 | * there might be several addresses corresponding to | |
2472 | * one code line. | |
2473 | */ | |
2474 | allow_suffix = true; | |
50656eec | 2475 | } |
79702f61 MH |
2476 | if (ret == -EINVAL && pev->uprobes) |
2477 | warn_uprobe_event_compat(tev); | |
146a1439 | 2478 | |
9aaf5a5f MH |
2479 | /* Note that it is possible to skip all events because of blacklist */ |
2480 | if (ret >= 0 && tev->event) { | |
146a1439 | 2481 | /* Show how to use the event. */ |
5e17b28f MH |
2482 | pr_info("\nYou can now use it in all perf tools, such as:\n\n"); |
2483 | pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group, | |
146a1439 MH |
2484 | tev->event); |
2485 | } | |
a9b495b0 | 2486 | |
9aaf5a5f | 2487 | kprobe_blacklist__delete(&blacklist); |
e1d2017b | 2488 | strlist__delete(namelist); |
50656eec | 2489 | close(fd); |
146a1439 | 2490 | return ret; |
50656eec | 2491 | } |
fa28244d | 2492 | |
564c62a4 | 2493 | static int find_probe_functions(struct map *map, char *name) |
eb948e50 | 2494 | { |
564c62a4 | 2495 | int found = 0; |
0a3873a8 | 2496 | struct symbol *sym; |
564c62a4 | 2497 | |
0a3873a8 | 2498 | map__for_each_symbol_by_name(map, name, sym) { |
e578da3b | 2499 | found++; |
eb948e50 | 2500 | } |
564c62a4 NK |
2501 | |
2502 | return found; | |
eb948e50 MH |
2503 | } |
2504 | ||
2505 | #define strdup_or_goto(str, label) \ | |
2506 | ({ char *__p = strdup(str); if (!__p) goto label; __p; }) | |
2507 | ||
2508 | /* | |
2509 | * Find probe function addresses from map. | |
2510 | * Return an error or the number of found probe_trace_event | |
2511 | */ | |
2512 | static int find_probe_trace_events_from_map(struct perf_probe_event *pev, | |
2513 | struct probe_trace_event **tevs, | |
2514 | int max_tevs, const char *target) | |
e0faa8d3 | 2515 | { |
eb948e50 | 2516 | struct map *map = NULL; |
eb948e50 | 2517 | struct ref_reloc_sym *reloc_sym = NULL; |
e0faa8d3 | 2518 | struct symbol *sym; |
0e60836b | 2519 | struct probe_trace_event *tev; |
eb948e50 MH |
2520 | struct perf_probe_point *pp = &pev->point; |
2521 | struct probe_trace_point *tp; | |
564c62a4 | 2522 | int num_matched_functions; |
eb948e50 | 2523 | int ret, i; |
4235b045 | 2524 | |
9b118aca | 2525 | map = get_target_map(target, pev->uprobes); |
eb948e50 MH |
2526 | if (!map) { |
2527 | ret = -EINVAL; | |
2528 | goto out; | |
fb7345bb MH |
2529 | } |
2530 | ||
eb948e50 MH |
2531 | /* |
2532 | * Load matched symbols: Since the different local symbols may have | |
2533 | * same name but different addresses, this lists all the symbols. | |
2534 | */ | |
564c62a4 NK |
2535 | num_matched_functions = find_probe_functions(map, pp->function); |
2536 | if (num_matched_functions == 0) { | |
eb948e50 MH |
2537 | pr_err("Failed to find symbol %s in %s\n", pp->function, |
2538 | target ? : "kernel"); | |
2539 | ret = -ENOENT; | |
2540 | goto out; | |
2541 | } else if (num_matched_functions > max_tevs) { | |
2542 | pr_err("Too many functions matched in %s\n", | |
2543 | target ? : "kernel"); | |
2544 | ret = -E2BIG; | |
2545 | goto out; | |
fb7345bb MH |
2546 | } |
2547 | ||
25dd9171 | 2548 | if (!pev->uprobes && !pp->retprobe) { |
0560a0c4 | 2549 | reloc_sym = kernel_get_ref_reloc_sym(); |
eb948e50 MH |
2550 | if (!reloc_sym) { |
2551 | pr_warning("Relocated base symbol is not found!\n"); | |
2552 | ret = -EINVAL; | |
2553 | goto out; | |
2554 | } | |
2555 | } | |
4235b045 | 2556 | |
eb948e50 MH |
2557 | /* Setup result trace-probe-events */ |
2558 | *tevs = zalloc(sizeof(*tev) * num_matched_functions); | |
2559 | if (!*tevs) { | |
02b95dad | 2560 | ret = -ENOMEM; |
eb948e50 | 2561 | goto out; |
02b95dad | 2562 | } |
ce27a443 | 2563 | |
eb948e50 | 2564 | ret = 0; |
564c62a4 | 2565 | |
0a3873a8 | 2566 | map__for_each_symbol_by_name(map, pp->function, sym) { |
eb948e50 MH |
2567 | tev = (*tevs) + ret; |
2568 | tp = &tev->point; | |
2569 | if (ret == num_matched_functions) { | |
2570 | pr_warning("Too many symbols are listed. Skip it.\n"); | |
2571 | break; | |
ce27a443 | 2572 | } |
eb948e50 | 2573 | ret++; |
ce27a443 | 2574 | |
eb948e50 MH |
2575 | if (pp->offset > sym->end - sym->start) { |
2576 | pr_warning("Offset %ld is bigger than the size of %s\n", | |
2577 | pp->offset, sym->name); | |
2578 | ret = -ENOENT; | |
2579 | goto err_out; | |
2580 | } | |
2581 | /* Add one probe point */ | |
2582 | tp->address = map->unmap_ip(map, sym->start) + pp->offset; | |
2583 | if (reloc_sym) { | |
2584 | tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out); | |
2585 | tp->offset = tp->address - reloc_sym->addr; | |
2586 | } else { | |
2587 | tp->symbol = strdup_or_goto(sym->name, nomem_out); | |
2588 | tp->offset = pp->offset; | |
2589 | } | |
2590 | tp->retprobe = pp->retprobe; | |
2591 | if (target) | |
2592 | tev->point.module = strdup_or_goto(target, nomem_out); | |
2593 | tev->uprobes = pev->uprobes; | |
2594 | tev->nargs = pev->nargs; | |
2595 | if (tev->nargs) { | |
2596 | tev->args = zalloc(sizeof(struct probe_trace_arg) * | |
2597 | tev->nargs); | |
2598 | if (tev->args == NULL) | |
2599 | goto nomem_out; | |
e334016f | 2600 | } |
48481938 | 2601 | for (i = 0; i < tev->nargs; i++) { |
eb948e50 MH |
2602 | if (pev->args[i].name) |
2603 | tev->args[i].name = | |
2604 | strdup_or_goto(pev->args[i].name, | |
2605 | nomem_out); | |
2606 | ||
2607 | tev->args[i].value = strdup_or_goto(pev->args[i].var, | |
2608 | nomem_out); | |
2609 | if (pev->args[i].type) | |
2610 | tev->args[i].type = | |
2611 | strdup_or_goto(pev->args[i].type, | |
2612 | nomem_out); | |
48481938 | 2613 | } |
4235b045 MH |
2614 | } |
2615 | ||
eb948e50 | 2616 | out: |
9b118aca | 2617 | put_target_map(map, pev->uprobes); |
eb948e50 | 2618 | return ret; |
225466f1 | 2619 | |
eb948e50 MH |
2620 | nomem_out: |
2621 | ret = -ENOMEM; | |
2622 | err_out: | |
2623 | clear_probe_trace_events(*tevs, num_matched_functions); | |
2624 | zfree(tevs); | |
2625 | goto out; | |
2626 | } | |
1c1bc922 | 2627 | |
eb948e50 MH |
2628 | static int convert_to_probe_trace_events(struct perf_probe_event *pev, |
2629 | struct probe_trace_event **tevs, | |
2630 | int max_tevs, const char *target) | |
2631 | { | |
2632 | int ret; | |
2633 | ||
2634 | if (pev->uprobes && !pev->group) { | |
2635 | /* Replace group name if not given */ | |
2636 | ret = convert_exec_to_group(target, &pev->group); | |
2637 | if (ret != 0) { | |
2638 | pr_warning("Failed to make a group name.\n"); | |
2639 | return ret; | |
2640 | } | |
02b95dad | 2641 | } |
e334016f | 2642 | |
eb948e50 MH |
2643 | /* Convert perf_probe_event with debuginfo */ |
2644 | ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target); | |
2645 | if (ret != 0) | |
2646 | return ret; /* Found in debuginfo or got an error */ | |
2647 | ||
2648 | return find_probe_trace_events_from_map(pev, tevs, max_tevs, target); | |
4235b045 MH |
2649 | } |
2650 | ||
2651 | struct __event_package { | |
2652 | struct perf_probe_event *pev; | |
0e60836b | 2653 | struct probe_trace_event *tevs; |
4235b045 MH |
2654 | int ntevs; |
2655 | }; | |
2656 | ||
146a1439 | 2657 | int add_perf_probe_events(struct perf_probe_event *pevs, int npevs, |
7afb3fab | 2658 | int max_tevs, bool force_add) |
4235b045 | 2659 | { |
146a1439 | 2660 | int i, j, ret; |
4235b045 MH |
2661 | struct __event_package *pkgs; |
2662 | ||
225466f1 | 2663 | ret = 0; |
e334016f | 2664 | pkgs = zalloc(sizeof(struct __event_package) * npevs); |
225466f1 | 2665 | |
e334016f MH |
2666 | if (pkgs == NULL) |
2667 | return -ENOMEM; | |
4235b045 | 2668 | |
ee45b6c2 | 2669 | ret = init_symbol_maps(pevs->uprobes); |
449e5b24 MH |
2670 | if (ret < 0) { |
2671 | free(pkgs); | |
146a1439 | 2672 | return ret; |
449e5b24 | 2673 | } |
4235b045 MH |
2674 | |
2675 | /* Loop 1: convert all events */ | |
2676 | for (i = 0; i < npevs; i++) { | |
2677 | pkgs[i].pev = &pevs[i]; | |
2678 | /* Convert with or without debuginfo */ | |
0e60836b | 2679 | ret = convert_to_probe_trace_events(pkgs[i].pev, |
469b9b88 MH |
2680 | &pkgs[i].tevs, |
2681 | max_tevs, | |
7afb3fab | 2682 | pkgs[i].pev->target); |
146a1439 MH |
2683 | if (ret < 0) |
2684 | goto end; | |
2685 | pkgs[i].ntevs = ret; | |
e0faa8d3 MH |
2686 | } |
2687 | ||
4235b045 | 2688 | /* Loop 2: add all events */ |
8635bf6e | 2689 | for (i = 0; i < npevs; i++) { |
0e60836b | 2690 | ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs, |
146a1439 | 2691 | pkgs[i].ntevs, force_add); |
fbee632d ACM |
2692 | if (ret < 0) |
2693 | break; | |
2694 | } | |
146a1439 | 2695 | end: |
449e5b24 MH |
2696 | /* Loop 3: cleanup and free trace events */ |
2697 | for (i = 0; i < npevs; i++) { | |
146a1439 | 2698 | for (j = 0; j < pkgs[i].ntevs; j++) |
0e60836b | 2699 | clear_probe_trace_event(&pkgs[i].tevs[j]); |
74cf249d | 2700 | zfree(&pkgs[i].tevs); |
449e5b24 MH |
2701 | } |
2702 | free(pkgs); | |
ee45b6c2 | 2703 | exit_symbol_maps(); |
146a1439 MH |
2704 | |
2705 | return ret; | |
e0faa8d3 MH |
2706 | } |
2707 | ||
0e60836b | 2708 | static int __del_trace_probe_event(int fd, struct str_node *ent) |
bbbb521b MH |
2709 | { |
2710 | char *p; | |
2711 | char buf[128]; | |
4235b045 | 2712 | int ret; |
bbbb521b | 2713 | |
0e60836b | 2714 | /* Convert from perf-probe event to trace-probe event */ |
146a1439 MH |
2715 | ret = e_snprintf(buf, 128, "-:%s", ent->s); |
2716 | if (ret < 0) | |
2717 | goto error; | |
2718 | ||
bbbb521b | 2719 | p = strchr(buf + 2, ':'); |
146a1439 MH |
2720 | if (!p) { |
2721 | pr_debug("Internal error: %s should have ':' but not.\n", | |
2722 | ent->s); | |
2723 | ret = -ENOTSUP; | |
2724 | goto error; | |
2725 | } | |
bbbb521b MH |
2726 | *p = '/'; |
2727 | ||
4235b045 MH |
2728 | pr_debug("Writing event: %s\n", buf); |
2729 | ret = write(fd, buf, strlen(buf)); | |
44a56040 MH |
2730 | if (ret < 0) { |
2731 | ret = -errno; | |
146a1439 | 2732 | goto error; |
44a56040 | 2733 | } |
146a1439 | 2734 | |
5e17b28f | 2735 | pr_info("Removed event: %s\n", ent->s); |
146a1439 MH |
2736 | return 0; |
2737 | error: | |
5f03cba4 MH |
2738 | pr_warning("Failed to delete event: %s\n", |
2739 | strerror_r(-ret, buf, sizeof(buf))); | |
146a1439 | 2740 | return ret; |
bbbb521b MH |
2741 | } |
2742 | ||
225466f1 SD |
2743 | static int del_trace_probe_event(int fd, const char *buf, |
2744 | struct strlist *namelist) | |
fa28244d | 2745 | { |
bbbb521b | 2746 | struct str_node *ent, *n; |
225466f1 | 2747 | int ret = -1; |
fa28244d | 2748 | |
bbbb521b MH |
2749 | if (strpbrk(buf, "*?")) { /* Glob-exp */ |
2750 | strlist__for_each_safe(ent, n, namelist) | |
2751 | if (strglobmatch(ent->s, buf)) { | |
0e60836b | 2752 | ret = __del_trace_probe_event(fd, ent); |
146a1439 MH |
2753 | if (ret < 0) |
2754 | break; | |
bbbb521b MH |
2755 | strlist__remove(namelist, ent); |
2756 | } | |
2757 | } else { | |
2758 | ent = strlist__find(namelist, buf); | |
2759 | if (ent) { | |
0e60836b | 2760 | ret = __del_trace_probe_event(fd, ent); |
146a1439 MH |
2761 | if (ret >= 0) |
2762 | strlist__remove(namelist, ent); | |
bbbb521b MH |
2763 | } |
2764 | } | |
146a1439 MH |
2765 | |
2766 | return ret; | |
fa28244d MH |
2767 | } |
2768 | ||
146a1439 | 2769 | int del_perf_probe_events(struct strlist *dellist) |
fa28244d | 2770 | { |
225466f1 SD |
2771 | int ret = -1, ufd = -1, kfd = -1; |
2772 | char buf[128]; | |
fa28244d MH |
2773 | const char *group, *event; |
2774 | char *p, *str; | |
2775 | struct str_node *ent; | |
225466f1 | 2776 | struct strlist *namelist = NULL, *unamelist = NULL; |
146a1439 | 2777 | |
fa28244d | 2778 | /* Get current event names */ |
225466f1 | 2779 | kfd = open_kprobe_events(true); |
467ec085 MH |
2780 | if (kfd >= 0) |
2781 | namelist = get_probe_trace_event_names(kfd, true); | |
225466f1 | 2782 | |
225466f1 | 2783 | ufd = open_uprobe_events(true); |
467ec085 | 2784 | if (ufd >= 0) |
225466f1 SD |
2785 | unamelist = get_probe_trace_event_names(ufd, true); |
2786 | ||
467ec085 MH |
2787 | if (kfd < 0 && ufd < 0) { |
2788 | print_both_open_warning(kfd, ufd); | |
2789 | goto error; | |
2790 | } | |
2791 | ||
225466f1 SD |
2792 | if (namelist == NULL && unamelist == NULL) |
2793 | goto error; | |
fa28244d | 2794 | |
adf365f4 | 2795 | strlist__for_each(ent, dellist) { |
02b95dad MH |
2796 | str = strdup(ent->s); |
2797 | if (str == NULL) { | |
2798 | ret = -ENOMEM; | |
225466f1 | 2799 | goto error; |
02b95dad | 2800 | } |
bbbb521b | 2801 | pr_debug("Parsing: %s\n", str); |
fa28244d MH |
2802 | p = strchr(str, ':'); |
2803 | if (p) { | |
2804 | group = str; | |
2805 | *p = '\0'; | |
2806 | event = p + 1; | |
2807 | } else { | |
bbbb521b | 2808 | group = "*"; |
fa28244d MH |
2809 | event = str; |
2810 | } | |
225466f1 SD |
2811 | |
2812 | ret = e_snprintf(buf, 128, "%s:%s", group, event); | |
2813 | if (ret < 0) { | |
2814 | pr_err("Failed to copy event."); | |
2815 | free(str); | |
2816 | goto error; | |
2817 | } | |
2818 | ||
bbbb521b | 2819 | pr_debug("Group: %s, Event: %s\n", group, event); |
225466f1 SD |
2820 | |
2821 | if (namelist) | |
2822 | ret = del_trace_probe_event(kfd, buf, namelist); | |
2823 | ||
2824 | if (unamelist && ret != 0) | |
2825 | ret = del_trace_probe_event(ufd, buf, unamelist); | |
2826 | ||
2827 | if (ret != 0) | |
2828 | pr_info("Info: Event \"%s\" does not exist.\n", buf); | |
2829 | ||
fa28244d MH |
2830 | free(str); |
2831 | } | |
225466f1 SD |
2832 | |
2833 | error: | |
2834 | if (kfd >= 0) { | |
a23c4dc4 | 2835 | strlist__delete(namelist); |
225466f1 SD |
2836 | close(kfd); |
2837 | } | |
2838 | ||
2839 | if (ufd >= 0) { | |
a23c4dc4 | 2840 | strlist__delete(unamelist); |
225466f1 SD |
2841 | close(ufd); |
2842 | } | |
146a1439 MH |
2843 | |
2844 | return ret; | |
fa28244d | 2845 | } |
225466f1 | 2846 | |
3c42258c MH |
2847 | /* TODO: don't use a global variable for filter ... */ |
2848 | static struct strfilter *available_func_filter; | |
fa28244d | 2849 | |
e80711ca | 2850 | /* |
3c42258c MH |
2851 | * If a symbol corresponds to a function with global binding and |
2852 | * matches filter return 0. For all others return 1. | |
e80711ca | 2853 | */ |
1d037ca1 | 2854 | static int filter_available_functions(struct map *map __maybe_unused, |
3c42258c | 2855 | struct symbol *sym) |
e80711ca | 2856 | { |
e578da3b | 2857 | if (strfilter__compare(available_func_filter, sym->name)) |
3c42258c MH |
2858 | return 0; |
2859 | return 1; | |
e80711ca MH |
2860 | } |
2861 | ||
2df58634 MH |
2862 | int show_available_funcs(const char *target, struct strfilter *_filter, |
2863 | bool user) | |
e80711ca MH |
2864 | { |
2865 | struct map *map; | |
2866 | int ret; | |
2867 | ||
2df58634 | 2868 | ret = init_symbol_maps(user); |
e80711ca MH |
2869 | if (ret < 0) |
2870 | return ret; | |
2871 | ||
2df58634 MH |
2872 | /* Get a symbol map */ |
2873 | if (user) | |
2874 | map = dso__new_map(target); | |
2875 | else | |
2876 | map = kernel_get_module_map(target); | |
e80711ca | 2877 | if (!map) { |
2df58634 | 2878 | pr_err("Failed to get a map for %s\n", (target) ? : "kernel"); |
e80711ca MH |
2879 | return -EINVAL; |
2880 | } | |
225466f1 | 2881 | |
2df58634 | 2882 | /* Load symbols with given filter */ |
3c42258c | 2883 | available_func_filter = _filter; |
2df58634 MH |
2884 | if (map__load(map, filter_available_functions)) { |
2885 | pr_err("Failed to load symbols in %s\n", (target) ? : "kernel"); | |
2886 | goto end; | |
2887 | } | |
2888 | if (!dso__sorted_by_name(map->dso, map->type)) | |
2889 | dso__sort_by_name(map->dso, map->type); | |
225466f1 | 2890 | |
2df58634 MH |
2891 | /* Show all (filtered) symbols */ |
2892 | setup_pager(); | |
2893 | dso__fprintf_symbols_by_name(map->dso, map->type, stdout); | |
2894 | end: | |
2895 | if (user) { | |
2896 | dso__delete(map->dso); | |
2897 | map__delete(map); | |
2898 | } | |
2899 | exit_symbol_maps(); | |
225466f1 | 2900 | |
2df58634 | 2901 | return ret; |
225466f1 SD |
2902 | } |
2903 |