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