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