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