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