]>
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 | ||
fd20e811 | 22 | #include <inttypes.h> |
50656eec MH |
23 | #include <sys/utsname.h> |
24 | #include <sys/types.h> | |
25 | #include <sys/stat.h> | |
26 | #include <fcntl.h> | |
27 | #include <errno.h> | |
28 | #include <stdio.h> | |
29 | #include <unistd.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
4de189fe MH |
32 | #include <stdarg.h> |
33 | #include <limits.h> | |
e80711ca | 34 | #include <elf.h> |
50656eec | 35 | |
31facc5f | 36 | #include "util.h" |
50656eec | 37 | #include "event.h" |
4de189fe | 38 | #include "strlist.h" |
50656eec | 39 | #include "debug.h" |
72041334 | 40 | #include "cache.h" |
631c9def | 41 | #include "color.h" |
e0faa8d3 MH |
42 | #include "symbol.h" |
43 | #include "thread.h" | |
4605eab3 | 44 | #include <api/fs/fs.h> |
1d037ca1 | 45 | #include "trace-event.h" /* For __maybe_unused */ |
50656eec | 46 | #include "probe-event.h" |
4235b045 | 47 | #include "probe-finder.h" |
92f6c72e | 48 | #include "probe-file.h" |
225466f1 | 49 | #include "session.h" |
a067558e | 50 | #include "string2.h" |
50656eec | 51 | |
3d689ed6 ACM |
52 | #include "sane_ctype.h" |
53 | ||
50656eec MH |
54 | #define PERFPROBE_GROUP "probe" |
55 | ||
f4d7da49 | 56 | bool probe_event_dry_run; /* Dry run flag */ |
ddb2f58f | 57 | struct probe_conf probe_conf; |
f4d7da49 | 58 | |
146a1439 | 59 | #define semantic_error(msg ...) pr_err("Semantic error :" msg) |
50656eec | 60 | |
92f6c72e | 61 | int e_snprintf(char *str, size_t size, const char *format, ...) |
4de189fe MH |
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 | ||
ee45b6c2 | 73 | static struct machine *host_machine; |
e0faa8d3 | 74 | |
469b9b88 | 75 | /* Initialize symbol maps and path of vmlinux/modules */ |
9bae1e8c | 76 | int init_probe_symbol_maps(bool user_only) |
e0faa8d3 | 77 | { |
146a1439 MH |
78 | int ret; |
79 | ||
e0faa8d3 | 80 | symbol_conf.sort_by_name = true; |
680d926a | 81 | symbol_conf.allow_aliases = true; |
0a7e6d1b | 82 | ret = symbol__init(NULL); |
146a1439 MH |
83 | if (ret < 0) { |
84 | pr_debug("Failed to init symbol map.\n"); | |
85 | goto out; | |
86 | } | |
e0faa8d3 | 87 | |
ee45b6c2 MH |
88 | if (host_machine || user_only) /* already initialized */ |
89 | return 0; | |
d28c6223 | 90 | |
ee45b6c2 MH |
91 | if (symbol_conf.vmlinux_name) |
92 | pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); | |
93 | ||
94 | host_machine = machine__new_host(); | |
95 | if (!host_machine) { | |
96 | pr_debug("machine__new_host() failed.\n"); | |
97 | symbol__exit(); | |
98 | ret = -1; | |
469b9b88 | 99 | } |
146a1439 MH |
100 | out: |
101 | if (ret < 0) | |
102 | pr_warning("Failed to init vmlinux path.\n"); | |
103 | return ret; | |
e0faa8d3 MH |
104 | } |
105 | ||
9bae1e8c | 106 | void exit_probe_symbol_maps(void) |
ee45b6c2 | 107 | { |
32ca678d ACM |
108 | machine__delete(host_machine); |
109 | host_machine = NULL; | |
ee45b6c2 MH |
110 | symbol__exit(); |
111 | } | |
112 | ||
469b9b88 MH |
113 | static struct symbol *__find_kernel_function_by_name(const char *name, |
114 | struct map **mapp) | |
115 | { | |
be39db9f | 116 | return machine__find_kernel_function_by_name(host_machine, name, mapp); |
469b9b88 MH |
117 | } |
118 | ||
8f33f7de MH |
119 | static struct symbol *__find_kernel_function(u64 addr, struct map **mapp) |
120 | { | |
be39db9f | 121 | return machine__find_kernel_function(host_machine, addr, mapp); |
8f33f7de MH |
122 | } |
123 | ||
124 | static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void) | |
125 | { | |
126 | /* kmap->ref_reloc_sym should be set if host_machine is initialized */ | |
127 | struct kmap *kmap; | |
a5e813c6 | 128 | struct map *map = machine__kernel_map(host_machine); |
8f33f7de | 129 | |
be39db9f | 130 | if (map__load(map) < 0) |
8f33f7de MH |
131 | return NULL; |
132 | ||
77e65977 | 133 | kmap = map__kmap(map); |
ba92732e WN |
134 | if (!kmap) |
135 | return NULL; | |
8f33f7de MH |
136 | return kmap->ref_reloc_sym; |
137 | } | |
138 | ||
9b239a12 MH |
139 | static int kernel_get_symbol_address_by_name(const char *name, u64 *addr, |
140 | bool reloc, bool reladdr) | |
8f33f7de MH |
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) | |
9b239a12 | 149 | *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr; |
8f33f7de MH |
150 | else { |
151 | sym = __find_kernel_function_by_name(name, &map); | |
9b239a12 MH |
152 | if (!sym) |
153 | return -ENOENT; | |
154 | *addr = map->unmap_ip(map, sym->start) - | |
155 | ((reloc) ? 0 : map->reloc) - | |
156 | ((reladdr) ? map->start : 0); | |
8f33f7de MH |
157 | } |
158 | return 0; | |
159 | } | |
160 | ||
e80711ca MH |
161 | static struct map *kernel_get_module_map(const char *module) |
162 | { | |
ee45b6c2 | 163 | struct map_groups *grp = &host_machine->kmaps; |
1eee78ae | 164 | struct maps *maps = &grp->maps[MAP__FUNCTION]; |
4bb7123d | 165 | struct map *pos; |
e80711ca | 166 | |
14a8fd7c MH |
167 | /* A file path -- this is an offline module */ |
168 | if (module && strchr(module, '/')) | |
eebc509b | 169 | return dso__new_map(module); |
14a8fd7c | 170 | |
e80711ca MH |
171 | if (!module) |
172 | module = "kernel"; | |
173 | ||
4bb7123d | 174 | for (pos = maps__first(maps); pos; pos = map__next(pos)) { |
cb3f3378 | 175 | /* short_name is "[module]" */ |
e80711ca | 176 | if (strncmp(pos->dso->short_name + 1, module, |
cb3f3378 KK |
177 | pos->dso->short_name_len - 2) == 0 && |
178 | module[pos->dso->short_name_len - 2] == '\0') { | |
eebc509b | 179 | map__get(pos); |
e80711ca MH |
180 | return pos; |
181 | } | |
182 | } | |
183 | return NULL; | |
184 | } | |
185 | ||
99e608b5 | 186 | struct map *get_target_map(const char *target, bool user) |
9b118aca MH |
187 | { |
188 | /* Init maps of given executable or kernel */ | |
189 | if (user) | |
190 | return dso__new_map(target); | |
191 | else | |
192 | return kernel_get_module_map(target); | |
193 | } | |
194 | ||
fb7345bb MH |
195 | static int convert_exec_to_group(const char *exec, char **result) |
196 | { | |
197 | char *ptr1, *ptr2, *exec_copy; | |
198 | char buf[64]; | |
199 | int ret; | |
200 | ||
201 | exec_copy = strdup(exec); | |
202 | if (!exec_copy) | |
203 | return -ENOMEM; | |
204 | ||
205 | ptr1 = basename(exec_copy); | |
206 | if (!ptr1) { | |
207 | ret = -EINVAL; | |
208 | goto out; | |
209 | } | |
210 | ||
ead1a574 | 211 | for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) { |
35726d3a MH |
212 | if (!isalnum(*ptr2) && *ptr2 != '_') { |
213 | *ptr2 = '\0'; | |
214 | break; | |
215 | } | |
216 | } | |
217 | ||
fb7345bb MH |
218 | ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1); |
219 | if (ret < 0) | |
220 | goto out; | |
221 | ||
222 | *result = strdup(buf); | |
223 | ret = *result ? 0 : -ENOMEM; | |
224 | ||
225 | out: | |
226 | free(exec_copy); | |
227 | return ret; | |
228 | } | |
229 | ||
9b118aca MH |
230 | static void clear_perf_probe_point(struct perf_probe_point *pp) |
231 | { | |
232 | free(pp->file); | |
233 | free(pp->function); | |
234 | free(pp->lazy_line); | |
235 | } | |
236 | ||
eb948e50 MH |
237 | static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs) |
238 | { | |
239 | int i; | |
240 | ||
241 | for (i = 0; i < ntevs; i++) | |
242 | clear_probe_trace_event(tevs + i); | |
243 | } | |
244 | ||
b031220d MH |
245 | static bool kprobe_blacklist__listed(unsigned long address); |
246 | static bool kprobe_warn_out_range(const char *symbol, unsigned long address) | |
247 | { | |
9b239a12 MH |
248 | u64 etext_addr = 0; |
249 | int ret; | |
7c31bb8c | 250 | |
b031220d | 251 | /* Get the address of _etext for checking non-probable text symbol */ |
9b239a12 MH |
252 | ret = kernel_get_symbol_address_by_name("_etext", &etext_addr, |
253 | false, false); | |
7c31bb8c | 254 | |
9b239a12 | 255 | if (ret == 0 && etext_addr < address) |
b031220d MH |
256 | pr_warning("%s is out of .text, skip it.\n", symbol); |
257 | else if (kprobe_blacklist__listed(address)) | |
258 | pr_warning("%s is blacklisted function, skip it.\n", symbol); | |
259 | else | |
260 | return false; | |
261 | ||
262 | return true; | |
263 | } | |
264 | ||
c61fb959 RB |
265 | /* |
266 | * @module can be module name of module file path. In case of path, | |
267 | * inspect elf and find out what is actual module name. | |
268 | * Caller has to free mod_name after using it. | |
269 | */ | |
270 | static char *find_module_name(const char *module) | |
271 | { | |
272 | int fd; | |
273 | Elf *elf; | |
274 | GElf_Ehdr ehdr; | |
275 | GElf_Shdr shdr; | |
276 | Elf_Data *data; | |
277 | Elf_Scn *sec; | |
278 | char *mod_name = NULL; | |
1f2ed153 | 279 | int name_offset; |
c61fb959 RB |
280 | |
281 | fd = open(module, O_RDONLY); | |
282 | if (fd < 0) | |
283 | return NULL; | |
284 | ||
285 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); | |
286 | if (elf == NULL) | |
287 | goto elf_err; | |
288 | ||
289 | if (gelf_getehdr(elf, &ehdr) == NULL) | |
290 | goto ret_err; | |
291 | ||
292 | sec = elf_section_by_name(elf, &ehdr, &shdr, | |
293 | ".gnu.linkonce.this_module", NULL); | |
294 | if (!sec) | |
295 | goto ret_err; | |
296 | ||
297 | data = elf_getdata(sec, NULL); | |
298 | if (!data || !data->d_buf) | |
299 | goto ret_err; | |
300 | ||
1f2ed153 MH |
301 | /* |
302 | * NOTE: | |
303 | * '.gnu.linkonce.this_module' section of kernel module elf directly | |
304 | * maps to 'struct module' from linux/module.h. This section contains | |
305 | * actual module name which will be used by kernel after loading it. | |
306 | * But, we cannot use 'struct module' here since linux/module.h is not | |
307 | * exposed to user-space. Offset of 'name' has remained same from long | |
308 | * time, so hardcoding it here. | |
309 | */ | |
310 | if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) | |
311 | name_offset = 12; | |
312 | else /* expect ELFCLASS64 by default */ | |
313 | name_offset = 24; | |
314 | ||
315 | mod_name = strdup((char *)data->d_buf + name_offset); | |
c61fb959 RB |
316 | |
317 | ret_err: | |
318 | elf_end(elf); | |
319 | elf_err: | |
320 | close(fd); | |
321 | return mod_name; | |
322 | } | |
323 | ||
89fe808a | 324 | #ifdef HAVE_DWARF_SUPPORT |
60fb7742 WN |
325 | |
326 | static int kernel_get_module_dso(const char *module, struct dso **pdso) | |
327 | { | |
328 | struct dso *dso; | |
329 | struct map *map; | |
330 | const char *vmlinux_name; | |
331 | int ret = 0; | |
332 | ||
333 | if (module) { | |
266fa2b2 ACM |
334 | char module_name[128]; |
335 | ||
336 | snprintf(module_name, sizeof(module_name), "[%s]", module); | |
337 | map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name); | |
338 | if (map) { | |
339 | dso = map->dso; | |
340 | goto found; | |
60fb7742 WN |
341 | } |
342 | pr_debug("Failed to find module %s.\n", module); | |
343 | return -ENOENT; | |
344 | } | |
345 | ||
a5e813c6 | 346 | map = machine__kernel_map(host_machine); |
60fb7742 WN |
347 | dso = map->dso; |
348 | ||
349 | vmlinux_name = symbol_conf.vmlinux_name; | |
350 | dso->load_errno = 0; | |
351 | if (vmlinux_name) | |
be39db9f | 352 | ret = dso__load_vmlinux(dso, map, vmlinux_name, false); |
60fb7742 | 353 | else |
be39db9f | 354 | ret = dso__load_vmlinux_path(dso, map); |
60fb7742 WN |
355 | found: |
356 | *pdso = dso; | |
357 | return ret; | |
358 | } | |
359 | ||
9b118aca MH |
360 | /* |
361 | * Some binaries like glibc have special symbols which are on the symbol | |
362 | * table, but not in the debuginfo. If we can find the address of the | |
363 | * symbol from map, we can translate the address back to the probe point. | |
364 | */ | |
365 | static int find_alternative_probe_point(struct debuginfo *dinfo, | |
366 | struct perf_probe_point *pp, | |
367 | struct perf_probe_point *result, | |
368 | const char *target, bool uprobes) | |
369 | { | |
370 | struct map *map = NULL; | |
371 | struct symbol *sym; | |
372 | u64 address = 0; | |
373 | int ret = -ENOENT; | |
374 | ||
375 | /* This can work only for function-name based one */ | |
376 | if (!pp->function || pp->file) | |
377 | return -ENOTSUP; | |
378 | ||
379 | map = get_target_map(target, uprobes); | |
380 | if (!map) | |
381 | return -EINVAL; | |
382 | ||
383 | /* Find the address of given function */ | |
384 | map__for_each_symbol_by_name(map, pp->function, sym) { | |
e6d7c91c MH |
385 | if (uprobes) |
386 | address = sym->start; | |
387 | else | |
8e34189b | 388 | address = map->unmap_ip(map, sym->start) - map->reloc; |
e578da3b | 389 | break; |
9b118aca MH |
390 | } |
391 | if (!address) { | |
392 | ret = -ENOENT; | |
393 | goto out; | |
394 | } | |
f6c15621 WN |
395 | pr_debug("Symbol %s address found : %" PRIx64 "\n", |
396 | pp->function, address); | |
9b118aca MH |
397 | |
398 | ret = debuginfo__find_probe_point(dinfo, (unsigned long)address, | |
399 | result); | |
400 | if (ret <= 0) | |
401 | ret = (!ret) ? -ENOENT : ret; | |
402 | else { | |
403 | result->offset += pp->offset; | |
404 | result->line += pp->line; | |
9d7b45c5 | 405 | result->retprobe = pp->retprobe; |
9b118aca MH |
406 | ret = 0; |
407 | } | |
408 | ||
409 | out: | |
eebc509b | 410 | map__put(map); |
9b118aca MH |
411 | return ret; |
412 | ||
413 | } | |
414 | ||
415 | static int get_alternative_probe_event(struct debuginfo *dinfo, | |
416 | struct perf_probe_event *pev, | |
44225521 | 417 | struct perf_probe_point *tmp) |
9b118aca MH |
418 | { |
419 | int ret; | |
420 | ||
421 | memcpy(tmp, &pev->point, sizeof(*tmp)); | |
422 | memset(&pev->point, 0, sizeof(pev->point)); | |
423 | ret = find_alternative_probe_point(dinfo, tmp, &pev->point, | |
44225521 | 424 | pev->target, pev->uprobes); |
9b118aca MH |
425 | if (ret < 0) |
426 | memcpy(&pev->point, tmp, sizeof(*tmp)); | |
427 | ||
428 | return ret; | |
429 | } | |
a15ad2f5 | 430 | |
811dd2ae MH |
431 | static int get_alternative_line_range(struct debuginfo *dinfo, |
432 | struct line_range *lr, | |
433 | const char *target, bool user) | |
434 | { | |
6d4a4896 DA |
435 | struct perf_probe_point pp = { .function = lr->function, |
436 | .file = lr->file, | |
437 | .line = lr->start }; | |
438 | struct perf_probe_point result; | |
811dd2ae MH |
439 | int ret, len = 0; |
440 | ||
6d4a4896 DA |
441 | memset(&result, 0, sizeof(result)); |
442 | ||
811dd2ae MH |
443 | if (lr->end != INT_MAX) |
444 | len = lr->end - lr->start; | |
445 | ret = find_alternative_probe_point(dinfo, &pp, &result, | |
446 | target, user); | |
447 | if (!ret) { | |
448 | lr->function = result.function; | |
449 | lr->file = result.file; | |
450 | lr->start = result.line; | |
451 | if (lr->end != INT_MAX) | |
452 | lr->end = lr->start + len; | |
453 | clear_perf_probe_point(&pp); | |
454 | } | |
455 | return ret; | |
456 | } | |
457 | ||
ff741783 | 458 | /* Open new debuginfo of given module */ |
92561cb7 | 459 | static struct debuginfo *open_debuginfo(const char *module, bool silent) |
e0faa8d3 | 460 | { |
a15ad2f5 | 461 | const char *path = module; |
419e8738 MH |
462 | char reason[STRERR_BUFSIZE]; |
463 | struct debuginfo *ret = NULL; | |
464 | struct dso *dso = NULL; | |
465 | int err; | |
ff741783 | 466 | |
a15ad2f5 | 467 | if (!module || !strchr(module, '/')) { |
419e8738 MH |
468 | err = kernel_get_module_dso(module, &dso); |
469 | if (err < 0) { | |
470 | if (!dso || dso->load_errno == 0) { | |
c8b5f2c9 | 471 | if (!str_error_r(-err, reason, STRERR_BUFSIZE)) |
419e8738 MH |
472 | strcpy(reason, "(unknown)"); |
473 | } else | |
474 | dso__strerror_load(dso, reason, STRERR_BUFSIZE); | |
92561cb7 | 475 | if (!silent) |
419e8738 MH |
476 | pr_err("Failed to find the path for %s: %s\n", |
477 | module ?: "kernel", reason); | |
14a8fd7c MH |
478 | return NULL; |
479 | } | |
419e8738 | 480 | path = dso->long_name; |
e0faa8d3 | 481 | } |
92561cb7 MH |
482 | ret = debuginfo__new(path); |
483 | if (!ret && !silent) { | |
484 | pr_warning("The %s file has no debug information.\n", path); | |
485 | if (!module || !strtailcmp(path, ".ko")) | |
486 | pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, "); | |
487 | else | |
488 | pr_warning("Rebuild with -g, "); | |
489 | pr_warning("or install an appropriate debuginfo package.\n"); | |
490 | } | |
491 | return ret; | |
e0faa8d3 | 492 | } |
4b4da7f7 | 493 | |
7737af01 MH |
494 | /* For caching the last debuginfo */ |
495 | static struct debuginfo *debuginfo_cache; | |
496 | static char *debuginfo_cache_path; | |
497 | ||
498 | static struct debuginfo *debuginfo_cache__open(const char *module, bool silent) | |
499 | { | |
20f49859 MH |
500 | const char *path = module; |
501 | ||
502 | /* If the module is NULL, it should be the kernel. */ | |
503 | if (!module) | |
504 | path = "kernel"; | |
505 | ||
506 | if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path)) | |
7737af01 MH |
507 | goto out; |
508 | ||
509 | /* Copy module path */ | |
510 | free(debuginfo_cache_path); | |
20f49859 MH |
511 | debuginfo_cache_path = strdup(path); |
512 | if (!debuginfo_cache_path) { | |
513 | debuginfo__delete(debuginfo_cache); | |
514 | debuginfo_cache = NULL; | |
515 | goto out; | |
7737af01 MH |
516 | } |
517 | ||
518 | debuginfo_cache = open_debuginfo(module, silent); | |
519 | if (!debuginfo_cache) | |
520 | zfree(&debuginfo_cache_path); | |
521 | out: | |
522 | return debuginfo_cache; | |
523 | } | |
524 | ||
525 | static void debuginfo_cache__exit(void) | |
526 | { | |
527 | debuginfo__delete(debuginfo_cache); | |
528 | debuginfo_cache = NULL; | |
529 | zfree(&debuginfo_cache_path); | |
530 | } | |
531 | ||
92561cb7 | 532 | |
99ca4233 MH |
533 | static int get_text_start_address(const char *exec, unsigned long *address) |
534 | { | |
535 | Elf *elf; | |
536 | GElf_Ehdr ehdr; | |
537 | GElf_Shdr shdr; | |
538 | int fd, ret = -ENOENT; | |
539 | ||
540 | fd = open(exec, O_RDONLY); | |
541 | if (fd < 0) | |
542 | return -errno; | |
543 | ||
544 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); | |
062d6c2a MH |
545 | if (elf == NULL) { |
546 | ret = -EINVAL; | |
547 | goto out_close; | |
548 | } | |
99ca4233 MH |
549 | |
550 | if (gelf_getehdr(elf, &ehdr) == NULL) | |
551 | goto out; | |
552 | ||
553 | if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL)) | |
554 | goto out; | |
555 | ||
556 | *address = shdr.sh_addr - shdr.sh_offset; | |
557 | ret = 0; | |
558 | out: | |
559 | elf_end(elf); | |
062d6c2a MH |
560 | out_close: |
561 | close(fd); | |
562 | ||
99ca4233 MH |
563 | return ret; |
564 | } | |
565 | ||
5a6f6314 MH |
566 | /* |
567 | * Convert trace point to probe point with debuginfo | |
568 | */ | |
569 | static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp, | |
570 | struct perf_probe_point *pp, | |
571 | bool is_kprobe) | |
572 | { | |
573 | struct debuginfo *dinfo = NULL; | |
574 | unsigned long stext = 0; | |
575 | u64 addr = tp->address; | |
576 | int ret = -ENOENT; | |
577 | ||
578 | /* convert the address to dwarf address */ | |
579 | if (!is_kprobe) { | |
580 | if (!addr) { | |
581 | ret = -EINVAL; | |
582 | goto error; | |
583 | } | |
584 | ret = get_text_start_address(tp->module, &stext); | |
585 | if (ret < 0) | |
586 | goto error; | |
587 | addr += stext; | |
e486367f | 588 | } else if (tp->symbol) { |
9b239a12 MH |
589 | /* If the module is given, this returns relative address */ |
590 | ret = kernel_get_symbol_address_by_name(tp->symbol, &addr, | |
591 | false, !!tp->module); | |
592 | if (ret != 0) | |
5a6f6314 MH |
593 | goto error; |
594 | addr += tp->offset; | |
595 | } | |
596 | ||
597 | pr_debug("try to find information at %" PRIx64 " in %s\n", addr, | |
598 | tp->module ? : "kernel"); | |
599 | ||
bb963e16 | 600 | dinfo = debuginfo_cache__open(tp->module, verbose <= 0); |
7737af01 | 601 | if (dinfo) |
5a6f6314 MH |
602 | ret = debuginfo__find_probe_point(dinfo, |
603 | (unsigned long)addr, pp); | |
7737af01 | 604 | else |
5a6f6314 | 605 | ret = -ENOENT; |
5a6f6314 MH |
606 | |
607 | if (ret > 0) { | |
608 | pp->retprobe = tp->retprobe; | |
609 | return 0; | |
610 | } | |
611 | error: | |
612 | pr_debug("Failed to find corresponding probes from debuginfo.\n"); | |
613 | return ret ? : -ENOENT; | |
614 | } | |
615 | ||
3e96dac7 MH |
616 | /* Adjust symbol name and address */ |
617 | static int post_process_probe_trace_point(struct probe_trace_point *tp, | |
618 | struct map *map, unsigned long offs) | |
619 | { | |
620 | struct symbol *sym; | |
621 | u64 addr = tp->address + tp->offset - offs; | |
622 | ||
623 | sym = map__find_symbol(map, addr); | |
624 | if (!sym) | |
625 | return -ENOENT; | |
626 | ||
627 | if (strcmp(sym->name, tp->symbol)) { | |
628 | /* If we have no realname, use symbol for it */ | |
629 | if (!tp->realname) | |
630 | tp->realname = tp->symbol; | |
631 | else | |
632 | free(tp->symbol); | |
633 | tp->symbol = strdup(sym->name); | |
634 | if (!tp->symbol) | |
635 | return -ENOMEM; | |
636 | } | |
637 | tp->offset = addr - sym->start; | |
638 | tp->address -= offs; | |
639 | ||
640 | return 0; | |
641 | } | |
642 | ||
8a937a25 MH |
643 | /* |
644 | * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions | |
645 | * and generate new symbols with suffixes such as .constprop.N or .isra.N | |
646 | * etc. Since those symbols are not recorded in DWARF, we have to find | |
647 | * correct generated symbols from offline ELF binary. | |
648 | * For online kernel or uprobes we don't need this because those are | |
649 | * rebased on _text, or already a section relative address. | |
650 | */ | |
651 | static int | |
652 | post_process_offline_probe_trace_events(struct probe_trace_event *tevs, | |
653 | int ntevs, const char *pathname) | |
654 | { | |
8a937a25 MH |
655 | struct map *map; |
656 | unsigned long stext = 0; | |
3e96dac7 | 657 | int i, ret = 0; |
8a937a25 MH |
658 | |
659 | /* Prepare a map for offline binary */ | |
660 | map = dso__new_map(pathname); | |
661 | if (!map || get_text_start_address(pathname, &stext) < 0) { | |
662 | pr_warning("Failed to get ELF symbols for %s\n", pathname); | |
663 | return -EINVAL; | |
664 | } | |
665 | ||
666 | for (i = 0; i < ntevs; i++) { | |
3e96dac7 MH |
667 | ret = post_process_probe_trace_point(&tevs[i].point, |
668 | map, stext); | |
669 | if (ret < 0) | |
670 | break; | |
8a937a25 MH |
671 | } |
672 | map__put(map); | |
673 | ||
3e96dac7 | 674 | return ret; |
8a937a25 MH |
675 | } |
676 | ||
fb7345bb MH |
677 | static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs, |
678 | int ntevs, const char *exec) | |
679 | { | |
680 | int i, ret = 0; | |
eb948e50 | 681 | unsigned long stext = 0; |
fb7345bb MH |
682 | |
683 | if (!exec) | |
684 | return 0; | |
685 | ||
686 | ret = get_text_start_address(exec, &stext); | |
687 | if (ret < 0) | |
688 | return ret; | |
689 | ||
690 | for (i = 0; i < ntevs && ret >= 0; i++) { | |
981a2379 | 691 | /* point.address is the addres of point.symbol + point.offset */ |
eb948e50 | 692 | tevs[i].point.address -= stext; |
fb7345bb | 693 | tevs[i].point.module = strdup(exec); |
eb948e50 | 694 | if (!tevs[i].point.module) { |
fb7345bb MH |
695 | ret = -ENOMEM; |
696 | break; | |
697 | } | |
698 | tevs[i].uprobes = true; | |
699 | } | |
700 | ||
701 | return ret; | |
702 | } | |
703 | ||
613f050d MH |
704 | static int |
705 | post_process_module_probe_trace_events(struct probe_trace_event *tevs, | |
706 | int ntevs, const char *module, | |
707 | struct debuginfo *dinfo) | |
190b57fc | 708 | { |
613f050d | 709 | Dwarf_Addr text_offs = 0; |
14a8fd7c | 710 | int i, ret = 0; |
63a29613 | 711 | char *mod_name = NULL; |
613f050d | 712 | struct map *map; |
14a8fd7c MH |
713 | |
714 | if (!module) | |
715 | return 0; | |
716 | ||
613f050d MH |
717 | map = get_target_map(module, false); |
718 | if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) { | |
719 | pr_warning("Failed to get ELF symbols for %s\n", module); | |
720 | return -EINVAL; | |
721 | } | |
14a8fd7c | 722 | |
613f050d | 723 | mod_name = find_module_name(module); |
190b57fc | 724 | for (i = 0; i < ntevs; i++) { |
613f050d MH |
725 | ret = post_process_probe_trace_point(&tevs[i].point, |
726 | map, (unsigned long)text_offs); | |
727 | if (ret < 0) | |
728 | break; | |
63a29613 RB |
729 | tevs[i].point.module = |
730 | strdup(mod_name ? mod_name : module); | |
14a8fd7c MH |
731 | if (!tevs[i].point.module) { |
732 | ret = -ENOMEM; | |
733 | break; | |
734 | } | |
190b57fc | 735 | } |
14a8fd7c | 736 | |
63a29613 | 737 | free(mod_name); |
613f050d MH |
738 | map__put(map); |
739 | ||
14a8fd7c | 740 | return ret; |
190b57fc MH |
741 | } |
742 | ||
d820456d RB |
743 | static int |
744 | post_process_kernel_probe_trace_events(struct probe_trace_event *tevs, | |
745 | int ntevs) | |
dfef99cd MH |
746 | { |
747 | struct ref_reloc_sym *reloc_sym; | |
748 | char *tmp; | |
5a51fcd1 | 749 | int i, skipped = 0; |
dfef99cd | 750 | |
428aff82 MH |
751 | /* Skip post process if the target is an offline kernel */ |
752 | if (symbol_conf.ignore_vmlinux_buildid) | |
8a937a25 MH |
753 | return post_process_offline_probe_trace_events(tevs, ntevs, |
754 | symbol_conf.vmlinux_name); | |
428aff82 | 755 | |
8f33f7de | 756 | reloc_sym = kernel_get_ref_reloc_sym(); |
dfef99cd MH |
757 | if (!reloc_sym) { |
758 | pr_warning("Relocated base symbol is not found!\n"); | |
759 | return -EINVAL; | |
760 | } | |
761 | ||
762 | for (i = 0; i < ntevs; i++) { | |
7ab31d94 NR |
763 | if (!tevs[i].point.address) |
764 | continue; | |
765 | if (tevs[i].point.retprobe && !kretprobe_offset_is_supported()) | |
b031220d MH |
766 | continue; |
767 | /* If we found a wrong one, mark it by NULL symbol */ | |
768 | if (kprobe_warn_out_range(tevs[i].point.symbol, | |
769 | tevs[i].point.address)) { | |
770 | tmp = NULL; | |
771 | skipped++; | |
772 | } else { | |
773 | tmp = strdup(reloc_sym->name); | |
774 | if (!tmp) | |
775 | return -ENOMEM; | |
dfef99cd | 776 | } |
b031220d MH |
777 | /* If we have no realname, use symbol for it */ |
778 | if (!tevs[i].point.realname) | |
779 | tevs[i].point.realname = tevs[i].point.symbol; | |
780 | else | |
781 | free(tevs[i].point.symbol); | |
782 | tevs[i].point.symbol = tmp; | |
783 | tevs[i].point.offset = tevs[i].point.address - | |
784 | reloc_sym->unrelocated_addr; | |
dfef99cd | 785 | } |
5a51fcd1 | 786 | return skipped; |
dfef99cd MH |
787 | } |
788 | ||
99e608b5 RB |
789 | void __weak |
790 | arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused, | |
791 | int ntevs __maybe_unused) | |
792 | { | |
793 | } | |
794 | ||
d820456d | 795 | /* Post processing the probe events */ |
99e608b5 RB |
796 | static int post_process_probe_trace_events(struct perf_probe_event *pev, |
797 | struct probe_trace_event *tevs, | |
d820456d | 798 | int ntevs, const char *module, |
613f050d | 799 | bool uprobe, struct debuginfo *dinfo) |
d820456d | 800 | { |
99e608b5 | 801 | int ret; |
d820456d | 802 | |
99e608b5 RB |
803 | if (uprobe) |
804 | ret = add_exec_to_probe_trace_events(tevs, ntevs, module); | |
805 | else if (module) | |
d820456d | 806 | /* Currently ref_reloc_sym based probe is not for drivers */ |
613f050d MH |
807 | ret = post_process_module_probe_trace_events(tevs, ntevs, |
808 | module, dinfo); | |
99e608b5 RB |
809 | else |
810 | ret = post_process_kernel_probe_trace_events(tevs, ntevs); | |
d820456d | 811 | |
99e608b5 RB |
812 | if (ret >= 0) |
813 | arch__post_process_probe_trace_events(pev, ntevs); | |
814 | ||
815 | return ret; | |
d820456d RB |
816 | } |
817 | ||
4b4da7f7 | 818 | /* Try to find perf_probe_event with debuginfo */ |
0e60836b | 819 | static int try_to_find_probe_trace_events(struct perf_probe_event *pev, |
ddb2f58f | 820 | struct probe_trace_event **tevs) |
4b4da7f7 MH |
821 | { |
822 | bool need_dwarf = perf_probe_event_need_dwarf(pev); | |
9b118aca | 823 | struct perf_probe_point tmp; |
225466f1 | 824 | struct debuginfo *dinfo; |
190b57fc | 825 | int ntevs, ret = 0; |
4b4da7f7 | 826 | |
44225521 | 827 | dinfo = open_debuginfo(pev->target, !need_dwarf); |
ff741783 | 828 | if (!dinfo) { |
92561cb7 | 829 | if (need_dwarf) |
ff741783 | 830 | return -ENOENT; |
ff741783 | 831 | pr_debug("Could not open debuginfo. Try to use symbols.\n"); |
4b4da7f7 MH |
832 | return 0; |
833 | } | |
834 | ||
dfef99cd | 835 | pr_debug("Try to find probe point from debuginfo.\n"); |
ff741783 | 836 | /* Searching trace events corresponding to a probe event */ |
ddb2f58f | 837 | ntevs = debuginfo__find_trace_events(dinfo, pev, tevs); |
ff741783 | 838 | |
9b118aca | 839 | if (ntevs == 0) { /* Not found, retry with an alternative */ |
44225521 | 840 | ret = get_alternative_probe_event(dinfo, pev, &tmp); |
9b118aca | 841 | if (!ret) { |
ddb2f58f | 842 | ntevs = debuginfo__find_trace_events(dinfo, pev, tevs); |
9b118aca MH |
843 | /* |
844 | * Write back to the original probe_event for | |
845 | * setting appropriate (user given) event name | |
846 | */ | |
847 | clear_perf_probe_point(&pev->point); | |
848 | memcpy(&pev->point, &tmp, sizeof(tmp)); | |
849 | } | |
850 | } | |
851 | ||
146a1439 | 852 | if (ntevs > 0) { /* Succeeded to find trace events */ |
dfef99cd | 853 | pr_debug("Found %d probe_trace_events.\n", ntevs); |
99e608b5 | 854 | ret = post_process_probe_trace_events(pev, *tevs, ntevs, |
613f050d | 855 | pev->target, pev->uprobes, dinfo); |
5a51fcd1 | 856 | if (ret < 0 || ret == ntevs) { |
613f050d | 857 | pr_debug("Post processing failed or all events are skipped. (%d)\n", ret); |
981d05ad MH |
858 | clear_probe_trace_events(*tevs, ntevs); |
859 | zfree(tevs); | |
613f050d | 860 | ntevs = 0; |
981d05ad | 861 | } |
146a1439 | 862 | } |
4b4da7f7 | 863 | |
613f050d MH |
864 | debuginfo__delete(dinfo); |
865 | ||
146a1439 | 866 | if (ntevs == 0) { /* No error but failed to find probe point. */ |
0687eba7 | 867 | pr_warning("Probe point '%s' not found.\n", |
146a1439 | 868 | synthesize_perf_probe_point(&pev->point)); |
0687eba7 | 869 | return -ENOENT; |
613f050d MH |
870 | } else if (ntevs < 0) { |
871 | /* Error path : ntevs < 0 */ | |
872 | pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs); | |
1c0bd0e8 WN |
873 | if (ntevs == -EBADF) |
874 | pr_warning("Warning: No dwarf info found in the vmlinux - " | |
875 | "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n"); | |
15eca306 | 876 | if (!need_dwarf) { |
0e43e5d2 | 877 | pr_debug("Trying to use symbols.\n"); |
15eca306 MH |
878 | return 0; |
879 | } | |
4b4da7f7 | 880 | } |
15eca306 | 881 | return ntevs; |
4b4da7f7 MH |
882 | } |
883 | ||
884 | #define LINEBUF_SIZE 256 | |
885 | #define NR_ADDITIONAL_LINES 2 | |
886 | ||
fde52dbd | 887 | static int __show_one_line(FILE *fp, int l, bool skip, bool show_num) |
4b4da7f7 | 888 | { |
5f03cba4 | 889 | char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE]; |
befe3414 FBH |
890 | const char *color = show_num ? "" : PERF_COLOR_BLUE; |
891 | const char *prefix = NULL; | |
4b4da7f7 | 892 | |
befe3414 | 893 | do { |
4b4da7f7 MH |
894 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
895 | goto error; | |
befe3414 FBH |
896 | if (skip) |
897 | continue; | |
898 | if (!prefix) { | |
899 | prefix = show_num ? "%7d " : " "; | |
900 | color_fprintf(stdout, color, prefix, l); | |
4b4da7f7 | 901 | } |
befe3414 FBH |
902 | color_fprintf(stdout, color, "%s", buf); |
903 | ||
904 | } while (strchr(buf, '\n') == NULL); | |
146a1439 | 905 | |
fde52dbd | 906 | return 1; |
4b4da7f7 | 907 | error: |
fde52dbd | 908 | if (ferror(fp)) { |
5f03cba4 | 909 | pr_warning("File read error: %s\n", |
c8b5f2c9 | 910 | str_error_r(errno, sbuf, sizeof(sbuf))); |
fde52dbd FBH |
911 | return -1; |
912 | } | |
913 | return 0; | |
914 | } | |
146a1439 | 915 | |
fde52dbd FBH |
916 | static int _show_one_line(FILE *fp, int l, bool skip, bool show_num) |
917 | { | |
918 | int rv = __show_one_line(fp, l, skip, show_num); | |
919 | if (rv == 0) { | |
920 | pr_warning("Source file is shorter than expected.\n"); | |
921 | rv = -1; | |
922 | } | |
923 | return rv; | |
4b4da7f7 MH |
924 | } |
925 | ||
fde52dbd FBH |
926 | #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true) |
927 | #define show_one_line(f,l) _show_one_line(f,l,false,false) | |
928 | #define skip_one_line(f,l) _show_one_line(f,l,true,false) | |
929 | #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false) | |
930 | ||
4b4da7f7 MH |
931 | /* |
932 | * Show line-range always requires debuginfo to find source file and | |
933 | * line number. | |
934 | */ | |
811dd2ae MH |
935 | static int __show_line_range(struct line_range *lr, const char *module, |
936 | bool user) | |
4b4da7f7 | 937 | { |
d3b63d7a | 938 | int l = 1; |
5a62257a | 939 | struct int_node *ln; |
ff741783 | 940 | struct debuginfo *dinfo; |
4b4da7f7 | 941 | FILE *fp; |
ff741783 | 942 | int ret; |
7cf0b79e | 943 | char *tmp; |
5f03cba4 | 944 | char sbuf[STRERR_BUFSIZE]; |
4b4da7f7 MH |
945 | |
946 | /* Search a line range */ | |
92561cb7 MH |
947 | dinfo = open_debuginfo(module, false); |
948 | if (!dinfo) | |
ff741783 | 949 | return -ENOENT; |
146a1439 | 950 | |
ff741783 | 951 | ret = debuginfo__find_line_range(dinfo, lr); |
811dd2ae MH |
952 | if (!ret) { /* Not found, retry with an alternative */ |
953 | ret = get_alternative_line_range(dinfo, lr, module, user); | |
954 | if (!ret) | |
955 | ret = debuginfo__find_line_range(dinfo, lr); | |
956 | } | |
ff741783 | 957 | debuginfo__delete(dinfo); |
5ee05b88 | 958 | if (ret == 0 || ret == -ENOENT) { |
146a1439 MH |
959 | pr_warning("Specified source line is not found.\n"); |
960 | return -ENOENT; | |
961 | } else if (ret < 0) { | |
5ee05b88 | 962 | pr_warning("Debuginfo analysis failed.\n"); |
146a1439 MH |
963 | return ret; |
964 | } | |
4b4da7f7 | 965 | |
7cf0b79e MH |
966 | /* Convert source file path */ |
967 | tmp = lr->path; | |
6a330a3c | 968 | ret = get_real_path(tmp, lr->comp_dir, &lr->path); |
a78604de HK |
969 | |
970 | /* Free old path when new path is assigned */ | |
971 | if (tmp != lr->path) | |
972 | free(tmp); | |
973 | ||
7cf0b79e | 974 | if (ret < 0) { |
5ee05b88 | 975 | pr_warning("Failed to find source file path.\n"); |
7cf0b79e MH |
976 | return ret; |
977 | } | |
978 | ||
4b4da7f7 MH |
979 | setup_pager(); |
980 | ||
981 | if (lr->function) | |
8737ebde | 982 | fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path, |
4b4da7f7 MH |
983 | lr->start - lr->offset); |
984 | else | |
62c15fc4 | 985 | fprintf(stdout, "<%s:%d>\n", lr->path, lr->start); |
4b4da7f7 MH |
986 | |
987 | fp = fopen(lr->path, "r"); | |
146a1439 MH |
988 | if (fp == NULL) { |
989 | pr_warning("Failed to open %s: %s\n", lr->path, | |
c8b5f2c9 | 990 | str_error_r(errno, sbuf, sizeof(sbuf))); |
146a1439 MH |
991 | return -errno; |
992 | } | |
4b4da7f7 | 993 | /* Skip to starting line number */ |
44b81e92 | 994 | while (l < lr->start) { |
fde52dbd | 995 | ret = skip_one_line(fp, l++); |
44b81e92 FBH |
996 | if (ret < 0) |
997 | goto end; | |
998 | } | |
4b4da7f7 | 999 | |
10daf4d0 | 1000 | intlist__for_each_entry(ln, lr->line_list) { |
5a62257a | 1001 | for (; ln->i > l; l++) { |
fde52dbd | 1002 | ret = show_one_line(fp, l - lr->offset); |
44b81e92 FBH |
1003 | if (ret < 0) |
1004 | goto end; | |
1005 | } | |
fde52dbd | 1006 | ret = show_one_line_with_num(fp, l++ - lr->offset); |
146a1439 MH |
1007 | if (ret < 0) |
1008 | goto end; | |
4b4da7f7 MH |
1009 | } |
1010 | ||
1011 | if (lr->end == INT_MAX) | |
1012 | lr->end = l + NR_ADDITIONAL_LINES; | |
fde52dbd FBH |
1013 | while (l <= lr->end) { |
1014 | ret = show_one_line_or_eof(fp, l++ - lr->offset); | |
1015 | if (ret <= 0) | |
44b81e92 FBH |
1016 | break; |
1017 | } | |
146a1439 | 1018 | end: |
4b4da7f7 | 1019 | fclose(fp); |
146a1439 | 1020 | return ret; |
4b4da7f7 MH |
1021 | } |
1022 | ||
2b394bc4 | 1023 | int show_line_range(struct line_range *lr, const char *module, bool user) |
ee45b6c2 MH |
1024 | { |
1025 | int ret; | |
1026 | ||
9bae1e8c | 1027 | ret = init_probe_symbol_maps(user); |
ee45b6c2 MH |
1028 | if (ret < 0) |
1029 | return ret; | |
811dd2ae | 1030 | ret = __show_line_range(lr, module, user); |
9bae1e8c | 1031 | exit_probe_symbol_maps(); |
ee45b6c2 MH |
1032 | |
1033 | return ret; | |
1034 | } | |
1035 | ||
ff741783 MH |
1036 | static int show_available_vars_at(struct debuginfo *dinfo, |
1037 | struct perf_probe_event *pev, | |
ddb2f58f | 1038 | struct strfilter *_filter) |
cf6eb489 MH |
1039 | { |
1040 | char *buf; | |
bd09d7b5 | 1041 | int ret, i, nvars; |
cf6eb489 MH |
1042 | struct str_node *node; |
1043 | struct variable_list *vls = NULL, *vl; | |
9b118aca | 1044 | struct perf_probe_point tmp; |
bd09d7b5 | 1045 | const char *var; |
cf6eb489 MH |
1046 | |
1047 | buf = synthesize_perf_probe_point(&pev->point); | |
1048 | if (!buf) | |
1049 | return -EINVAL; | |
1050 | pr_debug("Searching variables at %s\n", buf); | |
1051 | ||
ddb2f58f | 1052 | ret = debuginfo__find_available_vars_at(dinfo, pev, &vls); |
9b118aca | 1053 | if (!ret) { /* Not found, retry with an alternative */ |
44225521 | 1054 | ret = get_alternative_probe_event(dinfo, pev, &tmp); |
9b118aca MH |
1055 | if (!ret) { |
1056 | ret = debuginfo__find_available_vars_at(dinfo, pev, | |
ddb2f58f | 1057 | &vls); |
9b118aca MH |
1058 | /* Release the old probe_point */ |
1059 | clear_perf_probe_point(&tmp); | |
1060 | } | |
1061 | } | |
bd09d7b5 | 1062 | if (ret <= 0) { |
69e96eaa MH |
1063 | if (ret == 0 || ret == -ENOENT) { |
1064 | pr_err("Failed to find the address of %s\n", buf); | |
1065 | ret = -ENOENT; | |
1066 | } else | |
1067 | pr_warning("Debuginfo analysis failed.\n"); | |
bd09d7b5 MH |
1068 | goto end; |
1069 | } | |
69e96eaa | 1070 | |
bd09d7b5 MH |
1071 | /* Some variables are found */ |
1072 | fprintf(stdout, "Available variables at %s\n", buf); | |
1073 | for (i = 0; i < ret; i++) { | |
1074 | vl = &vls[i]; | |
1075 | /* | |
1076 | * A probe point might be converted to | |
1077 | * several trace points. | |
1078 | */ | |
1079 | fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol, | |
1080 | vl->point.offset); | |
74cf249d | 1081 | zfree(&vl->point.symbol); |
bd09d7b5 MH |
1082 | nvars = 0; |
1083 | if (vl->vars) { | |
602a1f4d | 1084 | strlist__for_each_entry(node, vl->vars) { |
bd09d7b5 MH |
1085 | var = strchr(node->s, '\t') + 1; |
1086 | if (strfilter__compare(_filter, var)) { | |
cf6eb489 | 1087 | fprintf(stdout, "\t\t%s\n", node->s); |
bd09d7b5 MH |
1088 | nvars++; |
1089 | } | |
1090 | } | |
1091 | strlist__delete(vl->vars); | |
cf6eb489 | 1092 | } |
bd09d7b5 MH |
1093 | if (nvars == 0) |
1094 | fprintf(stdout, "\t\t(No matched variables)\n"); | |
1095 | } | |
1096 | free(vls); | |
1097 | end: | |
cf6eb489 MH |
1098 | free(buf); |
1099 | return ret; | |
1100 | } | |
1101 | ||
1102 | /* Show available variables on given probe point */ | |
1103 | int show_available_vars(struct perf_probe_event *pevs, int npevs, | |
ddb2f58f | 1104 | struct strfilter *_filter) |
cf6eb489 | 1105 | { |
ff741783 MH |
1106 | int i, ret = 0; |
1107 | struct debuginfo *dinfo; | |
cf6eb489 | 1108 | |
9bae1e8c | 1109 | ret = init_probe_symbol_maps(pevs->uprobes); |
cf6eb489 MH |
1110 | if (ret < 0) |
1111 | return ret; | |
1112 | ||
44225521 | 1113 | dinfo = open_debuginfo(pevs->target, false); |
ff741783 | 1114 | if (!dinfo) { |
ee45b6c2 MH |
1115 | ret = -ENOENT; |
1116 | goto out; | |
ff741783 MH |
1117 | } |
1118 | ||
cf6eb489 MH |
1119 | setup_pager(); |
1120 | ||
ff741783 | 1121 | for (i = 0; i < npevs && ret >= 0; i++) |
ddb2f58f | 1122 | ret = show_available_vars_at(dinfo, &pevs[i], _filter); |
ff741783 MH |
1123 | |
1124 | debuginfo__delete(dinfo); | |
ee45b6c2 | 1125 | out: |
9bae1e8c | 1126 | exit_probe_symbol_maps(); |
cf6eb489 MH |
1127 | return ret; |
1128 | } | |
1129 | ||
89fe808a | 1130 | #else /* !HAVE_DWARF_SUPPORT */ |
4b4da7f7 | 1131 | |
7737af01 MH |
1132 | static void debuginfo_cache__exit(void) |
1133 | { | |
1134 | } | |
1135 | ||
5a6f6314 MH |
1136 | static int |
1137 | find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused, | |
1138 | struct perf_probe_point *pp __maybe_unused, | |
1139 | bool is_kprobe __maybe_unused) | |
4b4da7f7 | 1140 | { |
5a6f6314 | 1141 | return -ENOSYS; |
4b4da7f7 MH |
1142 | } |
1143 | ||
0e60836b | 1144 | static int try_to_find_probe_trace_events(struct perf_probe_event *pev, |
ddb2f58f | 1145 | struct probe_trace_event **tevs __maybe_unused) |
4b4da7f7 | 1146 | { |
146a1439 MH |
1147 | if (perf_probe_event_need_dwarf(pev)) { |
1148 | pr_warning("Debuginfo-analysis is not supported.\n"); | |
1149 | return -ENOSYS; | |
1150 | } | |
225466f1 | 1151 | |
4b4da7f7 MH |
1152 | return 0; |
1153 | } | |
1154 | ||
1d037ca1 | 1155 | int show_line_range(struct line_range *lr __maybe_unused, |
2b394bc4 MH |
1156 | const char *module __maybe_unused, |
1157 | bool user __maybe_unused) | |
4b4da7f7 | 1158 | { |
146a1439 MH |
1159 | pr_warning("Debuginfo-analysis is not supported.\n"); |
1160 | return -ENOSYS; | |
4b4da7f7 MH |
1161 | } |
1162 | ||
1d037ca1 | 1163 | int show_available_vars(struct perf_probe_event *pevs __maybe_unused, |
ddb2f58f MH |
1164 | int npevs __maybe_unused, |
1165 | struct strfilter *filter __maybe_unused) | |
cf6eb489 MH |
1166 | { |
1167 | pr_warning("Debuginfo-analysis is not supported.\n"); | |
1168 | return -ENOSYS; | |
1169 | } | |
e0faa8d3 MH |
1170 | #endif |
1171 | ||
e53b00d3 MH |
1172 | void line_range__clear(struct line_range *lr) |
1173 | { | |
e53b00d3 MH |
1174 | free(lr->function); |
1175 | free(lr->file); | |
1176 | free(lr->path); | |
1177 | free(lr->comp_dir); | |
5a62257a | 1178 | intlist__delete(lr->line_list); |
e53b00d3 MH |
1179 | memset(lr, 0, sizeof(*lr)); |
1180 | } | |
1181 | ||
5a62257a | 1182 | int line_range__init(struct line_range *lr) |
e53b00d3 MH |
1183 | { |
1184 | memset(lr, 0, sizeof(*lr)); | |
5a62257a MH |
1185 | lr->line_list = intlist__new(NULL); |
1186 | if (!lr->line_list) | |
1187 | return -ENOMEM; | |
1188 | else | |
1189 | return 0; | |
e53b00d3 MH |
1190 | } |
1191 | ||
21dd9ae5 FBH |
1192 | static int parse_line_num(char **ptr, int *val, const char *what) |
1193 | { | |
1194 | const char *start = *ptr; | |
1195 | ||
1196 | errno = 0; | |
1197 | *val = strtol(*ptr, ptr, 0); | |
1198 | if (errno || *ptr == start) { | |
1199 | semantic_error("'%s' is not a valid number.\n", what); | |
1200 | return -EINVAL; | |
1201 | } | |
1202 | return 0; | |
1203 | } | |
1204 | ||
573709fd MH |
1205 | /* Check the name is good for event, group or function */ |
1206 | static bool is_c_func_name(const char *name) | |
1207 | { | |
1208 | if (!isalpha(*name) && *name != '_') | |
1209 | return false; | |
1210 | while (*++name != '\0') { | |
1211 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') | |
1212 | return false; | |
1213 | } | |
1214 | return true; | |
1215 | } | |
1216 | ||
9d95b580 FBH |
1217 | /* |
1218 | * Stuff 'lr' according to the line range described by 'arg'. | |
1219 | * The line range syntax is described by: | |
1220 | * | |
1221 | * SRC[:SLN[+NUM|-ELN]] | |
e116dfa1 | 1222 | * FNC[@SRC][:SLN[+NUM|-ELN]] |
9d95b580 | 1223 | */ |
146a1439 | 1224 | int parse_line_range_desc(const char *arg, struct line_range *lr) |
631c9def | 1225 | { |
e116dfa1 | 1226 | char *range, *file, *name = strdup(arg); |
21dd9ae5 FBH |
1227 | int err; |
1228 | ||
1229 | if (!name) | |
1230 | return -ENOMEM; | |
1231 | ||
1232 | lr->start = 0; | |
1233 | lr->end = INT_MAX; | |
1234 | ||
1235 | range = strchr(name, ':'); | |
1236 | if (range) { | |
1237 | *range++ = '\0'; | |
1238 | ||
1239 | err = parse_line_num(&range, &lr->start, "start line"); | |
1240 | if (err) | |
1241 | goto err; | |
1242 | ||
1243 | if (*range == '+' || *range == '-') { | |
1244 | const char c = *range++; | |
1245 | ||
1246 | err = parse_line_num(&range, &lr->end, "end line"); | |
1247 | if (err) | |
1248 | goto err; | |
1249 | ||
1250 | if (c == '+') { | |
1251 | lr->end += lr->start; | |
1252 | /* | |
1253 | * Adjust the number of lines here. | |
1254 | * If the number of lines == 1, the | |
1255 | * the end of line should be equal to | |
1256 | * the start of line. | |
1257 | */ | |
1258 | lr->end--; | |
1259 | } | |
1260 | } | |
9d95b580 | 1261 | |
d3b63d7a | 1262 | pr_debug("Line range is %d to %d\n", lr->start, lr->end); |
21dd9ae5 FBH |
1263 | |
1264 | err = -EINVAL; | |
d3b63d7a | 1265 | if (lr->start > lr->end) { |
631c9def | 1266 | semantic_error("Start line must be smaller" |
146a1439 | 1267 | " than end line.\n"); |
21dd9ae5 | 1268 | goto err; |
146a1439 | 1269 | } |
21dd9ae5 FBH |
1270 | if (*range != '\0') { |
1271 | semantic_error("Tailing with invalid str '%s'.\n", range); | |
1272 | goto err; | |
146a1439 | 1273 | } |
d3b63d7a | 1274 | } |
02b95dad | 1275 | |
e116dfa1 MH |
1276 | file = strchr(name, '@'); |
1277 | if (file) { | |
1278 | *file = '\0'; | |
1279 | lr->file = strdup(++file); | |
1280 | if (lr->file == NULL) { | |
1281 | err = -ENOMEM; | |
1282 | goto err; | |
1283 | } | |
1284 | lr->function = name; | |
573709fd | 1285 | } else if (strchr(name, '/') || strchr(name, '.')) |
21dd9ae5 | 1286 | lr->file = name; |
573709fd | 1287 | else if (is_c_func_name(name))/* We reuse it for checking funcname */ |
21dd9ae5 | 1288 | lr->function = name; |
573709fd MH |
1289 | else { /* Invalid name */ |
1290 | semantic_error("'%s' is not a valid function name.\n", name); | |
1291 | err = -EINVAL; | |
1292 | goto err; | |
1293 | } | |
146a1439 MH |
1294 | |
1295 | return 0; | |
21dd9ae5 FBH |
1296 | err: |
1297 | free(name); | |
1298 | return err; | |
631c9def MH |
1299 | } |
1300 | ||
36a009fe MH |
1301 | static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev) |
1302 | { | |
1303 | char *ptr; | |
1304 | ||
1305 | ptr = strchr(*arg, ':'); | |
1306 | if (ptr) { | |
1307 | *ptr = '\0'; | |
42bba263 | 1308 | if (!pev->sdt && !is_c_func_name(*arg)) |
36a009fe MH |
1309 | goto ng_name; |
1310 | pev->group = strdup(*arg); | |
1311 | if (!pev->group) | |
1312 | return -ENOMEM; | |
1313 | *arg = ptr + 1; | |
1314 | } else | |
1315 | pev->group = NULL; | |
42bba263 | 1316 | if (!pev->sdt && !is_c_func_name(*arg)) { |
36a009fe MH |
1317 | ng_name: |
1318 | semantic_error("%s is bad for event name -it must " | |
1319 | "follow C symbol-naming rule.\n", *arg); | |
1320 | return -EINVAL; | |
1321 | } | |
1322 | pev->event = strdup(*arg); | |
1323 | if (pev->event == NULL) | |
1324 | return -ENOMEM; | |
1325 | ||
1326 | return 0; | |
1327 | } | |
1328 | ||
50656eec | 1329 | /* Parse probepoint definition. */ |
146a1439 | 1330 | static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev) |
50656eec | 1331 | { |
4235b045 | 1332 | struct perf_probe_point *pp = &pev->point; |
50656eec MH |
1333 | char *ptr, *tmp; |
1334 | char c, nc = 0; | |
3099c026 | 1335 | bool file_spec = false; |
36a009fe MH |
1336 | int ret; |
1337 | ||
50656eec MH |
1338 | /* |
1339 | * <Syntax> | |
8d993d96 MH |
1340 | * perf probe [GRP:][EVENT=]SRC[:LN|;PTN] |
1341 | * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] | |
36a009fe | 1342 | * perf probe %[GRP:]SDT_EVENT |
50656eec | 1343 | */ |
e59d29e8 WN |
1344 | if (!arg) |
1345 | return -EINVAL; | |
50656eec | 1346 | |
af9100ad | 1347 | if (is_sdt_event(arg)) { |
36a009fe | 1348 | pev->sdt = true; |
7e9fca51 MH |
1349 | if (arg[0] == '%') |
1350 | arg++; | |
36a009fe MH |
1351 | } |
1352 | ||
2a9c8c36 | 1353 | ptr = strpbrk(arg, ";=@+%"); |
36a009fe | 1354 | if (pev->sdt) { |
8d993d96 | 1355 | if (ptr) { |
a598180a MH |
1356 | if (*ptr != '@') { |
1357 | semantic_error("%s must be an SDT name.\n", | |
1358 | arg); | |
1359 | return -EINVAL; | |
1360 | } | |
1361 | /* This must be a target file name or build id */ | |
1362 | tmp = build_id_cache__complement(ptr + 1); | |
1363 | if (tmp) { | |
1364 | pev->target = build_id_cache__origname(tmp); | |
1365 | free(tmp); | |
1366 | } else | |
1367 | pev->target = strdup(ptr + 1); | |
1368 | if (!pev->target) | |
1369 | return -ENOMEM; | |
1370 | *ptr = '\0'; | |
146a1439 | 1371 | } |
36a009fe MH |
1372 | ret = parse_perf_probe_event_name(&arg, pev); |
1373 | if (ret == 0) { | |
1374 | if (asprintf(&pev->point.function, "%%%s", pev->event) < 0) | |
1375 | ret = -errno; | |
1376 | } | |
1377 | return ret; | |
1378 | } | |
1379 | ||
1380 | if (ptr && *ptr == '=') { /* Event name */ | |
1381 | *ptr = '\0'; | |
1382 | tmp = ptr + 1; | |
1383 | ret = parse_perf_probe_event_name(&arg, pev); | |
1384 | if (ret < 0) | |
1385 | return ret; | |
1386 | ||
af663d75 MH |
1387 | arg = tmp; |
1388 | } | |
1389 | ||
3099c026 NR |
1390 | /* |
1391 | * Check arg is function or file name and copy it. | |
1392 | * | |
1393 | * We consider arg to be a file spec if and only if it satisfies | |
1394 | * all of the below criteria:: | |
1395 | * - it does not include any of "+@%", | |
1396 | * - it includes one of ":;", and | |
1397 | * - it has a period '.' in the name. | |
1398 | * | |
1399 | * Otherwise, we consider arg to be a function specification. | |
1400 | */ | |
1401 | if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) { | |
1402 | /* This is a file spec if it includes a '.' before ; or : */ | |
1403 | if (memchr(arg, '.', ptr - arg)) | |
1404 | file_spec = true; | |
1405 | } | |
1406 | ||
2a9c8c36 | 1407 | ptr = strpbrk(arg, ";:+@%"); |
50656eec MH |
1408 | if (ptr) { |
1409 | nc = *ptr; | |
1410 | *ptr++ = '\0'; | |
1411 | } | |
1412 | ||
6c6e024f WN |
1413 | if (arg[0] == '\0') |
1414 | tmp = NULL; | |
1415 | else { | |
1416 | tmp = strdup(arg); | |
1417 | if (tmp == NULL) | |
1418 | return -ENOMEM; | |
1419 | } | |
02b95dad | 1420 | |
3099c026 | 1421 | if (file_spec) |
02b95dad | 1422 | pp->file = tmp; |
da15bd9d | 1423 | else { |
02b95dad | 1424 | pp->function = tmp; |
50656eec | 1425 | |
da15bd9d WN |
1426 | /* |
1427 | * Keep pp->function even if this is absolute address, | |
1428 | * so it can mark whether abs_address is valid. | |
1429 | * Which make 'perf probe lib.bin 0x0' possible. | |
1430 | * | |
1431 | * Note that checking length of tmp is not needed | |
1432 | * because when we access tmp[1] we know tmp[0] is '0', | |
1433 | * so tmp[1] should always valid (but could be '\0'). | |
1434 | */ | |
1435 | if (tmp && !strncmp(tmp, "0x", 2)) { | |
1436 | pp->abs_address = strtoul(pp->function, &tmp, 0); | |
1437 | if (*tmp != '\0') { | |
1438 | semantic_error("Invalid absolute address.\n"); | |
1439 | return -EINVAL; | |
1440 | } | |
1441 | } | |
1442 | } | |
1443 | ||
50656eec MH |
1444 | /* Parse other options */ |
1445 | while (ptr) { | |
1446 | arg = ptr; | |
1447 | c = nc; | |
2a9c8c36 | 1448 | if (c == ';') { /* Lazy pattern must be the last part */ |
02b95dad MH |
1449 | pp->lazy_line = strdup(arg); |
1450 | if (pp->lazy_line == NULL) | |
1451 | return -ENOMEM; | |
2a9c8c36 MH |
1452 | break; |
1453 | } | |
1454 | ptr = strpbrk(arg, ";:+@%"); | |
50656eec MH |
1455 | if (ptr) { |
1456 | nc = *ptr; | |
1457 | *ptr++ = '\0'; | |
1458 | } | |
1459 | switch (c) { | |
1460 | case ':': /* Line number */ | |
1461 | pp->line = strtoul(arg, &tmp, 0); | |
146a1439 | 1462 | if (*tmp != '\0') { |
2a9c8c36 | 1463 | semantic_error("There is non-digit char" |
146a1439 MH |
1464 | " in line number.\n"); |
1465 | return -EINVAL; | |
1466 | } | |
50656eec MH |
1467 | break; |
1468 | case '+': /* Byte offset from a symbol */ | |
1469 | pp->offset = strtoul(arg, &tmp, 0); | |
146a1439 | 1470 | if (*tmp != '\0') { |
2a9c8c36 | 1471 | semantic_error("There is non-digit character" |
146a1439 MH |
1472 | " in offset.\n"); |
1473 | return -EINVAL; | |
1474 | } | |
50656eec MH |
1475 | break; |
1476 | case '@': /* File name */ | |
146a1439 MH |
1477 | if (pp->file) { |
1478 | semantic_error("SRC@SRC is not allowed.\n"); | |
1479 | return -EINVAL; | |
1480 | } | |
02b95dad MH |
1481 | pp->file = strdup(arg); |
1482 | if (pp->file == NULL) | |
1483 | return -ENOMEM; | |
50656eec MH |
1484 | break; |
1485 | case '%': /* Probe places */ | |
1486 | if (strcmp(arg, "return") == 0) { | |
1487 | pp->retprobe = 1; | |
146a1439 MH |
1488 | } else { /* Others not supported yet */ |
1489 | semantic_error("%%%s is not supported.\n", arg); | |
1490 | return -ENOTSUP; | |
1491 | } | |
50656eec | 1492 | break; |
146a1439 MH |
1493 | default: /* Buggy case */ |
1494 | pr_err("This program has a bug at %s:%d.\n", | |
1495 | __FILE__, __LINE__); | |
1496 | return -ENOTSUP; | |
50656eec MH |
1497 | break; |
1498 | } | |
1499 | } | |
1500 | ||
1501 | /* Exclusion check */ | |
146a1439 | 1502 | if (pp->lazy_line && pp->line) { |
0e43e5d2 MH |
1503 | semantic_error("Lazy pattern can't be used with" |
1504 | " line number.\n"); | |
146a1439 MH |
1505 | return -EINVAL; |
1506 | } | |
2a9c8c36 | 1507 | |
146a1439 | 1508 | if (pp->lazy_line && pp->offset) { |
0e43e5d2 | 1509 | semantic_error("Lazy pattern can't be used with offset.\n"); |
146a1439 MH |
1510 | return -EINVAL; |
1511 | } | |
2a9c8c36 | 1512 | |
146a1439 | 1513 | if (pp->line && pp->offset) { |
0e43e5d2 | 1514 | semantic_error("Offset can't be used with line number.\n"); |
146a1439 MH |
1515 | return -EINVAL; |
1516 | } | |
50656eec | 1517 | |
146a1439 | 1518 | if (!pp->line && !pp->lazy_line && pp->file && !pp->function) { |
2a9c8c36 | 1519 | semantic_error("File always requires line number or " |
0e43e5d2 | 1520 | "lazy pattern.\n"); |
146a1439 MH |
1521 | return -EINVAL; |
1522 | } | |
50656eec | 1523 | |
146a1439 | 1524 | if (pp->offset && !pp->function) { |
0e43e5d2 | 1525 | semantic_error("Offset requires an entry function.\n"); |
146a1439 MH |
1526 | return -EINVAL; |
1527 | } | |
50656eec | 1528 | |
146a1439 | 1529 | if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) { |
2a9c8c36 | 1530 | semantic_error("Offset/Line/Lazy pattern can't be used with " |
0e43e5d2 | 1531 | "return probe.\n"); |
146a1439 MH |
1532 | return -EINVAL; |
1533 | } | |
50656eec | 1534 | |
4235b045 | 1535 | pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n", |
2a9c8c36 MH |
1536 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe, |
1537 | pp->lazy_line); | |
146a1439 | 1538 | return 0; |
50656eec MH |
1539 | } |
1540 | ||
7df2f329 | 1541 | /* Parse perf-probe event argument */ |
146a1439 | 1542 | static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg) |
7df2f329 | 1543 | { |
b2a3c12b | 1544 | char *tmp, *goodname; |
7df2f329 MH |
1545 | struct perf_probe_arg_field **fieldp; |
1546 | ||
1547 | pr_debug("parsing arg: %s into ", str); | |
1548 | ||
48481938 MH |
1549 | tmp = strchr(str, '='); |
1550 | if (tmp) { | |
02b95dad MH |
1551 | arg->name = strndup(str, tmp - str); |
1552 | if (arg->name == NULL) | |
1553 | return -ENOMEM; | |
11a1ca35 | 1554 | pr_debug("name:%s ", arg->name); |
48481938 MH |
1555 | str = tmp + 1; |
1556 | } | |
1557 | ||
11a1ca35 MH |
1558 | tmp = strchr(str, ':'); |
1559 | if (tmp) { /* Type setting */ | |
1560 | *tmp = '\0'; | |
02b95dad MH |
1561 | arg->type = strdup(tmp + 1); |
1562 | if (arg->type == NULL) | |
1563 | return -ENOMEM; | |
11a1ca35 MH |
1564 | pr_debug("type:%s ", arg->type); |
1565 | } | |
1566 | ||
b2a3c12b | 1567 | tmp = strpbrk(str, "-.["); |
7df2f329 MH |
1568 | if (!is_c_varname(str) || !tmp) { |
1569 | /* A variable, register, symbol or special value */ | |
02b95dad MH |
1570 | arg->var = strdup(str); |
1571 | if (arg->var == NULL) | |
1572 | return -ENOMEM; | |
48481938 | 1573 | pr_debug("%s\n", arg->var); |
146a1439 | 1574 | return 0; |
7df2f329 MH |
1575 | } |
1576 | ||
b2a3c12b | 1577 | /* Structure fields or array element */ |
02b95dad MH |
1578 | arg->var = strndup(str, tmp - str); |
1579 | if (arg->var == NULL) | |
1580 | return -ENOMEM; | |
b2a3c12b | 1581 | goodname = arg->var; |
48481938 | 1582 | pr_debug("%s, ", arg->var); |
7df2f329 MH |
1583 | fieldp = &arg->field; |
1584 | ||
1585 | do { | |
e334016f MH |
1586 | *fieldp = zalloc(sizeof(struct perf_probe_arg_field)); |
1587 | if (*fieldp == NULL) | |
1588 | return -ENOMEM; | |
b2a3c12b MH |
1589 | if (*tmp == '[') { /* Array */ |
1590 | str = tmp; | |
1591 | (*fieldp)->index = strtol(str + 1, &tmp, 0); | |
7df2f329 | 1592 | (*fieldp)->ref = true; |
b2a3c12b MH |
1593 | if (*tmp != ']' || tmp == str + 1) { |
1594 | semantic_error("Array index must be a" | |
1595 | " number.\n"); | |
1596 | return -EINVAL; | |
1597 | } | |
1598 | tmp++; | |
1599 | if (*tmp == '\0') | |
1600 | tmp = NULL; | |
1601 | } else { /* Structure */ | |
1602 | if (*tmp == '.') { | |
1603 | str = tmp + 1; | |
1604 | (*fieldp)->ref = false; | |
1605 | } else if (tmp[1] == '>') { | |
1606 | str = tmp + 2; | |
1607 | (*fieldp)->ref = true; | |
1608 | } else { | |
1609 | semantic_error("Argument parse error: %s\n", | |
1610 | str); | |
1611 | return -EINVAL; | |
1612 | } | |
1613 | tmp = strpbrk(str, "-.["); | |
146a1439 | 1614 | } |
7df2f329 | 1615 | if (tmp) { |
02b95dad MH |
1616 | (*fieldp)->name = strndup(str, tmp - str); |
1617 | if ((*fieldp)->name == NULL) | |
1618 | return -ENOMEM; | |
b2a3c12b MH |
1619 | if (*str != '[') |
1620 | goodname = (*fieldp)->name; | |
7df2f329 MH |
1621 | pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); |
1622 | fieldp = &(*fieldp)->next; | |
1623 | } | |
1624 | } while (tmp); | |
02b95dad MH |
1625 | (*fieldp)->name = strdup(str); |
1626 | if ((*fieldp)->name == NULL) | |
1627 | return -ENOMEM; | |
b2a3c12b MH |
1628 | if (*str != '[') |
1629 | goodname = (*fieldp)->name; | |
7df2f329 | 1630 | pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); |
df0faf4b | 1631 | |
b2a3c12b | 1632 | /* If no name is specified, set the last field name (not array index)*/ |
02b95dad | 1633 | if (!arg->name) { |
b2a3c12b | 1634 | arg->name = strdup(goodname); |
02b95dad MH |
1635 | if (arg->name == NULL) |
1636 | return -ENOMEM; | |
1637 | } | |
146a1439 | 1638 | return 0; |
7df2f329 MH |
1639 | } |
1640 | ||
4235b045 | 1641 | /* Parse perf-probe event command */ |
146a1439 | 1642 | int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) |
50656eec | 1643 | { |
e1c01d61 | 1644 | char **argv; |
146a1439 | 1645 | int argc, i, ret = 0; |
fac13fd5 | 1646 | |
4235b045 | 1647 | argv = argv_split(cmd, &argc); |
146a1439 MH |
1648 | if (!argv) { |
1649 | pr_debug("Failed to split arguments.\n"); | |
1650 | return -ENOMEM; | |
1651 | } | |
1652 | if (argc - 1 > MAX_PROBE_ARGS) { | |
1653 | semantic_error("Too many probe arguments (%d).\n", argc - 1); | |
1654 | ret = -ERANGE; | |
1655 | goto out; | |
1656 | } | |
50656eec | 1657 | /* Parse probe point */ |
146a1439 MH |
1658 | ret = parse_perf_probe_point(argv[0], pev); |
1659 | if (ret < 0) | |
1660 | goto out; | |
50656eec | 1661 | |
e1c01d61 | 1662 | /* Copy arguments and ensure return probe has no C argument */ |
4235b045 | 1663 | pev->nargs = argc - 1; |
e334016f MH |
1664 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
1665 | if (pev->args == NULL) { | |
1666 | ret = -ENOMEM; | |
1667 | goto out; | |
1668 | } | |
146a1439 MH |
1669 | for (i = 0; i < pev->nargs && ret >= 0; i++) { |
1670 | ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); | |
1671 | if (ret >= 0 && | |
1672 | is_c_varname(pev->args[i].var) && pev->point.retprobe) { | |
4235b045 | 1673 | semantic_error("You can't specify local variable for" |
146a1439 MH |
1674 | " kretprobe.\n"); |
1675 | ret = -EINVAL; | |
1676 | } | |
e1c01d61 | 1677 | } |
146a1439 | 1678 | out: |
e1c01d61 | 1679 | argv_free(argv); |
146a1439 MH |
1680 | |
1681 | return ret; | |
50656eec MH |
1682 | } |
1683 | ||
b3f33f93 RB |
1684 | /* Returns true if *any* ARG is either C variable, $params or $vars. */ |
1685 | bool perf_probe_with_var(struct perf_probe_event *pev) | |
1686 | { | |
1687 | int i = 0; | |
1688 | ||
1689 | for (i = 0; i < pev->nargs; i++) | |
1690 | if (is_c_varname(pev->args[i].var) || | |
1691 | !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) || | |
1692 | !strcmp(pev->args[i].var, PROBE_ARG_VARS)) | |
1693 | return true; | |
1694 | return false; | |
1695 | } | |
1696 | ||
4235b045 MH |
1697 | /* Return true if this perf_probe_event requires debuginfo */ |
1698 | bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) | |
1699 | { | |
4235b045 MH |
1700 | if (pev->point.file || pev->point.line || pev->point.lazy_line) |
1701 | return true; | |
1702 | ||
b3f33f93 RB |
1703 | if (perf_probe_with_var(pev)) |
1704 | return true; | |
4235b045 MH |
1705 | |
1706 | return false; | |
1707 | } | |
1708 | ||
0e60836b | 1709 | /* Parse probe_events event into struct probe_point */ |
92f6c72e | 1710 | int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev) |
4de189fe | 1711 | { |
0e60836b | 1712 | struct probe_trace_point *tp = &tev->point; |
4de189fe MH |
1713 | char pr; |
1714 | char *p; | |
bcbd0040 | 1715 | char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str; |
4de189fe MH |
1716 | int ret, i, argc; |
1717 | char **argv; | |
1718 | ||
0e60836b | 1719 | pr_debug("Parsing probe_events: %s\n", cmd); |
4235b045 | 1720 | argv = argv_split(cmd, &argc); |
146a1439 MH |
1721 | if (!argv) { |
1722 | pr_debug("Failed to split arguments.\n"); | |
1723 | return -ENOMEM; | |
1724 | } | |
1725 | if (argc < 2) { | |
1726 | semantic_error("Too few probe arguments.\n"); | |
1727 | ret = -ERANGE; | |
1728 | goto out; | |
1729 | } | |
4de189fe MH |
1730 | |
1731 | /* Scan event and group name. */ | |
bcbd0040 IT |
1732 | argv0_str = strdup(argv[0]); |
1733 | if (argv0_str == NULL) { | |
1734 | ret = -ENOMEM; | |
1735 | goto out; | |
1736 | } | |
1737 | fmt1_str = strtok_r(argv0_str, ":", &fmt); | |
1738 | fmt2_str = strtok_r(NULL, "/", &fmt); | |
1739 | fmt3_str = strtok_r(NULL, " \t", &fmt); | |
1740 | if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL | |
1741 | || fmt3_str == NULL) { | |
146a1439 MH |
1742 | semantic_error("Failed to parse event name: %s\n", argv[0]); |
1743 | ret = -EINVAL; | |
1744 | goto out; | |
1745 | } | |
bcbd0040 IT |
1746 | pr = fmt1_str[0]; |
1747 | tev->group = strdup(fmt2_str); | |
1748 | tev->event = strdup(fmt3_str); | |
1749 | if (tev->group == NULL || tev->event == NULL) { | |
1750 | ret = -ENOMEM; | |
1751 | goto out; | |
1752 | } | |
4235b045 | 1753 | pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr); |
4de189fe | 1754 | |
4235b045 | 1755 | tp->retprobe = (pr == 'r'); |
4de189fe | 1756 | |
190b57fc MH |
1757 | /* Scan module name(if there), function name and offset */ |
1758 | p = strchr(argv[1], ':'); | |
1759 | if (p) { | |
1760 | tp->module = strndup(argv[1], p - argv[1]); | |
844faa4b MH |
1761 | if (!tp->module) { |
1762 | ret = -ENOMEM; | |
1763 | goto out; | |
1764 | } | |
42bba263 | 1765 | tev->uprobes = (tp->module[0] == '/'); |
190b57fc MH |
1766 | p++; |
1767 | } else | |
1768 | p = argv[1]; | |
bcbd0040 | 1769 | fmt1_str = strtok_r(p, "+", &fmt); |
be07afe9 WN |
1770 | /* only the address started with 0x */ |
1771 | if (fmt1_str[0] == '0') { | |
1772 | /* | |
1773 | * Fix a special case: | |
1774 | * if address == 0, kernel reports something like: | |
1775 | * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax | |
1776 | * Newer kernel may fix that, but we want to | |
1777 | * support old kernel also. | |
1778 | */ | |
1779 | if (strcmp(fmt1_str, "0x") == 0) { | |
1780 | if (!argv[2] || strcmp(argv[2], "(null)")) { | |
1781 | ret = -EINVAL; | |
1782 | goto out; | |
1783 | } | |
1784 | tp->address = 0; | |
1785 | ||
1786 | free(argv[2]); | |
1787 | for (i = 2; argv[i + 1] != NULL; i++) | |
1788 | argv[i] = argv[i + 1]; | |
1789 | ||
1790 | argv[i] = NULL; | |
1791 | argc -= 1; | |
1792 | } else | |
1793 | tp->address = strtoul(fmt1_str, NULL, 0); | |
1794 | } else { | |
5a6f6314 MH |
1795 | /* Only the symbol-based probe has offset */ |
1796 | tp->symbol = strdup(fmt1_str); | |
1797 | if (tp->symbol == NULL) { | |
1798 | ret = -ENOMEM; | |
1799 | goto out; | |
1800 | } | |
1801 | fmt2_str = strtok_r(NULL, "", &fmt); | |
1802 | if (fmt2_str == NULL) | |
1803 | tp->offset = 0; | |
1804 | else | |
1805 | tp->offset = strtoul(fmt2_str, NULL, 10); | |
bcbd0040 | 1806 | } |
4de189fe | 1807 | |
4235b045 | 1808 | tev->nargs = argc - 2; |
0e60836b | 1809 | tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); |
e334016f MH |
1810 | if (tev->args == NULL) { |
1811 | ret = -ENOMEM; | |
1812 | goto out; | |
1813 | } | |
4235b045 | 1814 | for (i = 0; i < tev->nargs; i++) { |
4de189fe MH |
1815 | p = strchr(argv[i + 2], '='); |
1816 | if (p) /* We don't need which register is assigned. */ | |
4235b045 MH |
1817 | *p++ = '\0'; |
1818 | else | |
1819 | p = argv[i + 2]; | |
02b95dad | 1820 | tev->args[i].name = strdup(argv[i + 2]); |
4235b045 | 1821 | /* TODO: parse regs and offset */ |
02b95dad MH |
1822 | tev->args[i].value = strdup(p); |
1823 | if (tev->args[i].name == NULL || tev->args[i].value == NULL) { | |
1824 | ret = -ENOMEM; | |
1825 | goto out; | |
1826 | } | |
4de189fe | 1827 | } |
146a1439 MH |
1828 | ret = 0; |
1829 | out: | |
bcbd0040 | 1830 | free(argv0_str); |
4de189fe | 1831 | argv_free(argv); |
146a1439 | 1832 | return ret; |
4de189fe MH |
1833 | } |
1834 | ||
7df2f329 | 1835 | /* Compose only probe arg */ |
909b0360 | 1836 | char *synthesize_perf_probe_arg(struct perf_probe_arg *pa) |
7df2f329 MH |
1837 | { |
1838 | struct perf_probe_arg_field *field = pa->field; | |
909b0360 | 1839 | struct strbuf buf; |
bf4d5f25 MH |
1840 | char *ret = NULL; |
1841 | int err; | |
1842 | ||
1843 | if (strbuf_init(&buf, 64) < 0) | |
1844 | return NULL; | |
7df2f329 | 1845 | |
48481938 | 1846 | if (pa->name && pa->var) |
bf4d5f25 | 1847 | err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var); |
48481938 | 1848 | else |
bf4d5f25 MH |
1849 | err = strbuf_addstr(&buf, pa->name ?: pa->var); |
1850 | if (err) | |
1851 | goto out; | |
7df2f329 MH |
1852 | |
1853 | while (field) { | |
b2a3c12b | 1854 | if (field->name[0] == '[') |
bf4d5f25 | 1855 | err = strbuf_addstr(&buf, field->name); |
b2a3c12b | 1856 | else |
bf4d5f25 MH |
1857 | err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".", |
1858 | field->name); | |
7df2f329 | 1859 | field = field->next; |
bf4d5f25 MH |
1860 | if (err) |
1861 | goto out; | |
7df2f329 | 1862 | } |
11a1ca35 | 1863 | |
909b0360 | 1864 | if (pa->type) |
bf4d5f25 MH |
1865 | if (strbuf_addf(&buf, ":%s", pa->type) < 0) |
1866 | goto out; | |
909b0360 MH |
1867 | |
1868 | ret = strbuf_detach(&buf, NULL); | |
bf4d5f25 MH |
1869 | out: |
1870 | strbuf_release(&buf); | |
146a1439 | 1871 | return ret; |
7df2f329 MH |
1872 | } |
1873 | ||
4235b045 | 1874 | /* Compose only probe point (not argument) */ |
c4ff4920 | 1875 | char *synthesize_perf_probe_point(struct perf_probe_point *pp) |
4de189fe | 1876 | { |
909b0360 | 1877 | struct strbuf buf; |
bf4d5f25 MH |
1878 | char *tmp, *ret = NULL; |
1879 | int len, err = 0; | |
1880 | ||
1881 | if (strbuf_init(&buf, 64) < 0) | |
1882 | return NULL; | |
909b0360 | 1883 | |
909b0360 | 1884 | if (pp->function) { |
bf4d5f25 MH |
1885 | if (strbuf_addstr(&buf, pp->function) < 0) |
1886 | goto out; | |
909b0360 | 1887 | if (pp->offset) |
bf4d5f25 | 1888 | err = strbuf_addf(&buf, "+%lu", pp->offset); |
909b0360 | 1889 | else if (pp->line) |
bf4d5f25 | 1890 | err = strbuf_addf(&buf, ":%d", pp->line); |
909b0360 | 1891 | else if (pp->retprobe) |
bf4d5f25 MH |
1892 | err = strbuf_addstr(&buf, "%return"); |
1893 | if (err) | |
1894 | goto out; | |
fb1587d8 MH |
1895 | } |
1896 | if (pp->file) { | |
32ae2ade FBH |
1897 | tmp = pp->file; |
1898 | len = strlen(tmp); | |
1899 | if (len > 30) { | |
1900 | tmp = strchr(pp->file + len - 30, '/'); | |
1901 | tmp = tmp ? tmp + 1 : pp->file + len - 30; | |
1902 | } | |
bf4d5f25 MH |
1903 | err = strbuf_addf(&buf, "@%s", tmp); |
1904 | if (!err && !pp->function && pp->line) | |
1905 | err = strbuf_addf(&buf, ":%d", pp->line); | |
4de189fe | 1906 | } |
bf4d5f25 MH |
1907 | if (!err) |
1908 | ret = strbuf_detach(&buf, NULL); | |
1909 | out: | |
1910 | strbuf_release(&buf); | |
1911 | return ret; | |
7ef17aaf MH |
1912 | } |
1913 | ||
4235b045 | 1914 | char *synthesize_perf_probe_command(struct perf_probe_event *pev) |
7ef17aaf | 1915 | { |
c4ff4920 MH |
1916 | struct strbuf buf; |
1917 | char *tmp, *ret = NULL; | |
1918 | int i; | |
7ef17aaf | 1919 | |
c4ff4920 | 1920 | if (strbuf_init(&buf, 64)) |
4235b045 | 1921 | return NULL; |
c4ff4920 MH |
1922 | if (pev->event) |
1923 | if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP, | |
1924 | pev->event) < 0) | |
1925 | goto out; | |
1926 | ||
1927 | tmp = synthesize_perf_probe_point(&pev->point); | |
1928 | if (!tmp || strbuf_addstr(&buf, tmp) < 0) | |
1929 | goto out; | |
1930 | free(tmp); | |
4de189fe | 1931 | |
4235b045 | 1932 | for (i = 0; i < pev->nargs; i++) { |
c4ff4920 MH |
1933 | tmp = synthesize_perf_probe_arg(pev->args + i); |
1934 | if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0) | |
1935 | goto out; | |
1936 | free(tmp); | |
4de189fe | 1937 | } |
4de189fe | 1938 | |
c4ff4920 MH |
1939 | ret = strbuf_detach(&buf, NULL); |
1940 | out: | |
1941 | strbuf_release(&buf); | |
1942 | return ret; | |
4235b045 | 1943 | } |
4235b045 | 1944 | |
0e60836b | 1945 | static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref, |
909b0360 | 1946 | struct strbuf *buf, int depth) |
4235b045 | 1947 | { |
bf4d5f25 | 1948 | int err; |
4235b045 | 1949 | if (ref->next) { |
0e60836b | 1950 | depth = __synthesize_probe_trace_arg_ref(ref->next, buf, |
909b0360 | 1951 | depth + 1); |
4235b045 | 1952 | if (depth < 0) |
bf4d5f25 | 1953 | return depth; |
4235b045 | 1954 | } |
bf4d5f25 MH |
1955 | err = strbuf_addf(buf, "%+ld(", ref->offset); |
1956 | return (err < 0) ? err : depth; | |
4de189fe MH |
1957 | } |
1958 | ||
0e60836b | 1959 | static int synthesize_probe_trace_arg(struct probe_trace_arg *arg, |
909b0360 | 1960 | struct strbuf *buf) |
50656eec | 1961 | { |
0e60836b | 1962 | struct probe_trace_arg_ref *ref = arg->ref; |
bf4d5f25 | 1963 | int depth = 0, err; |
4235b045 MH |
1964 | |
1965 | /* Argument name or separator */ | |
1966 | if (arg->name) | |
bf4d5f25 | 1967 | err = strbuf_addf(buf, " %s=", arg->name); |
4235b045 | 1968 | else |
bf4d5f25 MH |
1969 | err = strbuf_addch(buf, ' '); |
1970 | if (err) | |
1971 | return err; | |
4235b045 | 1972 | |
b7dcb857 MH |
1973 | /* Special case: @XXX */ |
1974 | if (arg->value[0] == '@' && arg->ref) | |
1975 | ref = ref->next; | |
1976 | ||
4235b045 | 1977 | /* Dereferencing arguments */ |
b7dcb857 | 1978 | if (ref) { |
909b0360 | 1979 | depth = __synthesize_probe_trace_arg_ref(ref, buf, 1); |
4235b045 MH |
1980 | if (depth < 0) |
1981 | return depth; | |
1982 | } | |
1983 | ||
1984 | /* Print argument value */ | |
b7dcb857 | 1985 | if (arg->value[0] == '@' && arg->ref) |
bf4d5f25 | 1986 | err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset); |
b7dcb857 | 1987 | else |
bf4d5f25 | 1988 | err = strbuf_addstr(buf, arg->value); |
4235b045 MH |
1989 | |
1990 | /* Closing */ | |
bf4d5f25 MH |
1991 | while (!err && depth--) |
1992 | err = strbuf_addch(buf, ')'); | |
1993 | ||
4984912e | 1994 | /* Print argument type */ |
bf4d5f25 MH |
1995 | if (!err && arg->type) |
1996 | err = strbuf_addf(buf, ":%s", arg->type); | |
4235b045 | 1997 | |
bf4d5f25 | 1998 | return err; |
4235b045 MH |
1999 | } |
2000 | ||
0e60836b | 2001 | char *synthesize_probe_trace_command(struct probe_trace_event *tev) |
4235b045 | 2002 | { |
0e60836b | 2003 | struct probe_trace_point *tp = &tev->point; |
909b0360 MH |
2004 | struct strbuf buf; |
2005 | char *ret = NULL; | |
bf4d5f25 | 2006 | int i, err; |
eb948e50 | 2007 | |
da15bd9d WN |
2008 | /* Uprobes must have tp->module */ |
2009 | if (tev->uprobes && !tp->module) | |
909b0360 MH |
2010 | return NULL; |
2011 | ||
bf4d5f25 MH |
2012 | if (strbuf_init(&buf, 32) < 0) |
2013 | return NULL; | |
2014 | ||
2015 | if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p', | |
2016 | tev->group, tev->event) < 0) | |
2017 | goto error; | |
da15bd9d WN |
2018 | /* |
2019 | * If tp->address == 0, then this point must be a | |
2020 | * absolute address uprobe. | |
2021 | * try_to_find_absolute_address() should have made | |
2022 | * tp->symbol to "0x0". | |
2023 | */ | |
2024 | if (tev->uprobes && !tp->address) { | |
2025 | if (!tp->symbol || strcmp(tp->symbol, "0x0")) | |
2026 | goto error; | |
2027 | } | |
eb948e50 MH |
2028 | |
2029 | /* Use the tp->address for uprobes */ | |
225466f1 | 2030 | if (tev->uprobes) |
bf4d5f25 | 2031 | err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address); |
da15bd9d WN |
2032 | else if (!strncmp(tp->symbol, "0x", 2)) |
2033 | /* Absolute address. See try_to_find_absolute_address() */ | |
bf4d5f25 MH |
2034 | err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "", |
2035 | tp->module ? ":" : "", tp->address); | |
225466f1 | 2036 | else |
bf4d5f25 MH |
2037 | err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "", |
2038 | tp->module ? ":" : "", tp->symbol, tp->offset); | |
2039 | if (err) | |
2040 | goto error; | |
50656eec | 2041 | |
909b0360 MH |
2042 | for (i = 0; i < tev->nargs; i++) |
2043 | if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0) | |
50656eec | 2044 | goto error; |
50656eec | 2045 | |
909b0360 | 2046 | ret = strbuf_detach(&buf, NULL); |
50656eec | 2047 | error: |
909b0360 MH |
2048 | strbuf_release(&buf); |
2049 | return ret; | |
4235b045 | 2050 | } |
50656eec | 2051 | |
5a6f6314 MH |
2052 | static int find_perf_probe_point_from_map(struct probe_trace_point *tp, |
2053 | struct perf_probe_point *pp, | |
2054 | bool is_kprobe) | |
2055 | { | |
2056 | struct symbol *sym = NULL; | |
8a2efd6d | 2057 | struct map *map = NULL; |
e486367f | 2058 | u64 addr = tp->address; |
5a6f6314 MH |
2059 | int ret = -ENOENT; |
2060 | ||
2061 | if (!is_kprobe) { | |
2062 | map = dso__new_map(tp->module); | |
2063 | if (!map) | |
2064 | goto out; | |
be39db9f | 2065 | sym = map__find_symbol(map, addr); |
5a6f6314 | 2066 | } else { |
9b239a12 | 2067 | if (tp->symbol && !addr) { |
0a62f686 MH |
2068 | if (kernel_get_symbol_address_by_name(tp->symbol, |
2069 | &addr, true, false) < 0) | |
9b239a12 MH |
2070 | goto out; |
2071 | } | |
5a6f6314 MH |
2072 | if (addr) { |
2073 | addr += tp->offset; | |
2074 | sym = __find_kernel_function(addr, &map); | |
2075 | } | |
2076 | } | |
98d3b258 | 2077 | |
5a6f6314 MH |
2078 | if (!sym) |
2079 | goto out; | |
2080 | ||
2081 | pp->retprobe = tp->retprobe; | |
2082 | pp->offset = addr - map->unmap_ip(map, sym->start); | |
2083 | pp->function = strdup(sym->name); | |
2084 | ret = pp->function ? 0 : -ENOMEM; | |
2085 | ||
2086 | out: | |
2087 | if (map && !is_kprobe) { | |
84c2cafa | 2088 | map__put(map); |
5a6f6314 MH |
2089 | } |
2090 | ||
2091 | return ret; | |
2092 | } | |
2093 | ||
2094 | static int convert_to_perf_probe_point(struct probe_trace_point *tp, | |
da15bd9d WN |
2095 | struct perf_probe_point *pp, |
2096 | bool is_kprobe) | |
5a6f6314 MH |
2097 | { |
2098 | char buf[128]; | |
2099 | int ret; | |
2100 | ||
2101 | ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe); | |
2102 | if (!ret) | |
2103 | return 0; | |
2104 | ret = find_perf_probe_point_from_map(tp, pp, is_kprobe); | |
2105 | if (!ret) | |
2106 | return 0; | |
2107 | ||
2108 | pr_debug("Failed to find probe point from both of dwarf and map.\n"); | |
2109 | ||
2110 | if (tp->symbol) { | |
2111 | pp->function = strdup(tp->symbol); | |
2112 | pp->offset = tp->offset; | |
614e2fdb | 2113 | } else { |
5a6f6314 MH |
2114 | ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address); |
2115 | if (ret < 0) | |
2116 | return ret; | |
2117 | pp->function = strdup(buf); | |
2118 | pp->offset = 0; | |
2119 | } | |
2120 | if (pp->function == NULL) | |
2121 | return -ENOMEM; | |
2122 | ||
2123 | pp->retprobe = tp->retprobe; | |
2124 | ||
2125 | return 0; | |
2126 | } | |
2127 | ||
0e60836b | 2128 | static int convert_to_perf_probe_event(struct probe_trace_event *tev, |
225466f1 | 2129 | struct perf_probe_event *pev, bool is_kprobe) |
4235b045 | 2130 | { |
909b0360 | 2131 | struct strbuf buf = STRBUF_INIT; |
146a1439 | 2132 | int i, ret; |
4235b045 | 2133 | |
4b4da7f7 | 2134 | /* Convert event/group name */ |
02b95dad MH |
2135 | pev->event = strdup(tev->event); |
2136 | pev->group = strdup(tev->group); | |
2137 | if (pev->event == NULL || pev->group == NULL) | |
2138 | return -ENOMEM; | |
fb1587d8 | 2139 | |
4b4da7f7 | 2140 | /* Convert trace_point to probe_point */ |
5a6f6314 | 2141 | ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe); |
146a1439 MH |
2142 | if (ret < 0) |
2143 | return ret; | |
4b4da7f7 | 2144 | |
4235b045 MH |
2145 | /* Convert trace_arg to probe_arg */ |
2146 | pev->nargs = tev->nargs; | |
e334016f MH |
2147 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
2148 | if (pev->args == NULL) | |
2149 | return -ENOMEM; | |
02b95dad | 2150 | for (i = 0; i < tev->nargs && ret >= 0; i++) { |
4235b045 | 2151 | if (tev->args[i].name) |
02b95dad | 2152 | pev->args[i].name = strdup(tev->args[i].name); |
4235b045 | 2153 | else { |
bf4d5f25 MH |
2154 | if ((ret = strbuf_init(&buf, 32)) < 0) |
2155 | goto error; | |
909b0360 MH |
2156 | ret = synthesize_probe_trace_arg(&tev->args[i], &buf); |
2157 | pev->args[i].name = strbuf_detach(&buf, NULL); | |
4235b045 | 2158 | } |
02b95dad MH |
2159 | if (pev->args[i].name == NULL && ret >= 0) |
2160 | ret = -ENOMEM; | |
2161 | } | |
bf4d5f25 | 2162 | error: |
146a1439 MH |
2163 | if (ret < 0) |
2164 | clear_perf_probe_event(pev); | |
2165 | ||
2166 | return ret; | |
4235b045 MH |
2167 | } |
2168 | ||
2169 | void clear_perf_probe_event(struct perf_probe_event *pev) | |
2170 | { | |
7df2f329 | 2171 | struct perf_probe_arg_field *field, *next; |
4235b045 MH |
2172 | int i; |
2173 | ||
f5385650 ACM |
2174 | free(pev->event); |
2175 | free(pev->group); | |
7afb3fab | 2176 | free(pev->target); |
9b118aca | 2177 | clear_perf_probe_point(&pev->point); |
f5385650 | 2178 | |
7df2f329 | 2179 | for (i = 0; i < pev->nargs; i++) { |
f5385650 ACM |
2180 | free(pev->args[i].name); |
2181 | free(pev->args[i].var); | |
2182 | free(pev->args[i].type); | |
7df2f329 MH |
2183 | field = pev->args[i].field; |
2184 | while (field) { | |
2185 | next = field->next; | |
74cf249d | 2186 | zfree(&field->name); |
7df2f329 MH |
2187 | free(field); |
2188 | field = next; | |
2189 | } | |
2190 | } | |
f5385650 | 2191 | free(pev->args); |
4235b045 MH |
2192 | memset(pev, 0, sizeof(*pev)); |
2193 | } | |
2194 | ||
0542bb9c MH |
2195 | #define strdup_or_goto(str, label) \ |
2196 | ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; }) | |
2197 | ||
2198 | static int perf_probe_point__copy(struct perf_probe_point *dst, | |
2199 | struct perf_probe_point *src) | |
2200 | { | |
2201 | dst->file = strdup_or_goto(src->file, out_err); | |
2202 | dst->function = strdup_or_goto(src->function, out_err); | |
2203 | dst->lazy_line = strdup_or_goto(src->lazy_line, out_err); | |
2204 | dst->line = src->line; | |
2205 | dst->retprobe = src->retprobe; | |
2206 | dst->offset = src->offset; | |
2207 | return 0; | |
2208 | ||
2209 | out_err: | |
2210 | clear_perf_probe_point(dst); | |
2211 | return -ENOMEM; | |
2212 | } | |
2213 | ||
2214 | static int perf_probe_arg__copy(struct perf_probe_arg *dst, | |
2215 | struct perf_probe_arg *src) | |
2216 | { | |
2217 | struct perf_probe_arg_field *field, **ppfield; | |
2218 | ||
2219 | dst->name = strdup_or_goto(src->name, out_err); | |
2220 | dst->var = strdup_or_goto(src->var, out_err); | |
2221 | dst->type = strdup_or_goto(src->type, out_err); | |
2222 | ||
2223 | field = src->field; | |
2224 | ppfield = &(dst->field); | |
2225 | while (field) { | |
2226 | *ppfield = zalloc(sizeof(*field)); | |
2227 | if (!*ppfield) | |
2228 | goto out_err; | |
2229 | (*ppfield)->name = strdup_or_goto(field->name, out_err); | |
2230 | (*ppfield)->index = field->index; | |
2231 | (*ppfield)->ref = field->ref; | |
2232 | field = field->next; | |
2233 | ppfield = &((*ppfield)->next); | |
2234 | } | |
2235 | return 0; | |
2236 | out_err: | |
2237 | return -ENOMEM; | |
2238 | } | |
2239 | ||
2240 | int perf_probe_event__copy(struct perf_probe_event *dst, | |
2241 | struct perf_probe_event *src) | |
2242 | { | |
2243 | int i; | |
2244 | ||
2245 | dst->event = strdup_or_goto(src->event, out_err); | |
2246 | dst->group = strdup_or_goto(src->group, out_err); | |
2247 | dst->target = strdup_or_goto(src->target, out_err); | |
2248 | dst->uprobes = src->uprobes; | |
2249 | ||
2250 | if (perf_probe_point__copy(&dst->point, &src->point) < 0) | |
2251 | goto out_err; | |
2252 | ||
2253 | dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs); | |
2254 | if (!dst->args) | |
2255 | goto out_err; | |
2256 | dst->nargs = src->nargs; | |
2257 | ||
2258 | for (i = 0; i < src->nargs; i++) | |
2259 | if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0) | |
2260 | goto out_err; | |
2261 | return 0; | |
2262 | ||
2263 | out_err: | |
2264 | clear_perf_probe_event(dst); | |
2265 | return -ENOMEM; | |
2266 | } | |
2267 | ||
92f6c72e | 2268 | void clear_probe_trace_event(struct probe_trace_event *tev) |
4235b045 | 2269 | { |
0e60836b | 2270 | struct probe_trace_arg_ref *ref, *next; |
4235b045 MH |
2271 | int i; |
2272 | ||
f5385650 ACM |
2273 | free(tev->event); |
2274 | free(tev->group); | |
2275 | free(tev->point.symbol); | |
4c859351 | 2276 | free(tev->point.realname); |
f5385650 | 2277 | free(tev->point.module); |
4235b045 | 2278 | for (i = 0; i < tev->nargs; i++) { |
f5385650 ACM |
2279 | free(tev->args[i].name); |
2280 | free(tev->args[i].value); | |
2281 | free(tev->args[i].type); | |
4235b045 MH |
2282 | ref = tev->args[i].ref; |
2283 | while (ref) { | |
2284 | next = ref->next; | |
2285 | free(ref); | |
2286 | ref = next; | |
2287 | } | |
2288 | } | |
f5385650 | 2289 | free(tev->args); |
4235b045 | 2290 | memset(tev, 0, sizeof(*tev)); |
50656eec MH |
2291 | } |
2292 | ||
9aaf5a5f MH |
2293 | struct kprobe_blacklist_node { |
2294 | struct list_head list; | |
2295 | unsigned long start; | |
2296 | unsigned long end; | |
2297 | char *symbol; | |
2298 | }; | |
2299 | ||
2300 | static void kprobe_blacklist__delete(struct list_head *blacklist) | |
2301 | { | |
2302 | struct kprobe_blacklist_node *node; | |
2303 | ||
2304 | while (!list_empty(blacklist)) { | |
2305 | node = list_first_entry(blacklist, | |
2306 | struct kprobe_blacklist_node, list); | |
2307 | list_del(&node->list); | |
2308 | free(node->symbol); | |
2309 | free(node); | |
2310 | } | |
2311 | } | |
2312 | ||
2313 | static int kprobe_blacklist__load(struct list_head *blacklist) | |
2314 | { | |
2315 | struct kprobe_blacklist_node *node; | |
4605eab3 | 2316 | const char *__debugfs = debugfs__mountpoint(); |
9aaf5a5f MH |
2317 | char buf[PATH_MAX], *p; |
2318 | FILE *fp; | |
2319 | int ret; | |
2320 | ||
2321 | if (__debugfs == NULL) | |
2322 | return -ENOTSUP; | |
2323 | ||
2324 | ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs); | |
2325 | if (ret < 0) | |
2326 | return ret; | |
2327 | ||
2328 | fp = fopen(buf, "r"); | |
2329 | if (!fp) | |
2330 | return -errno; | |
2331 | ||
2332 | ret = 0; | |
2333 | while (fgets(buf, PATH_MAX, fp)) { | |
2334 | node = zalloc(sizeof(*node)); | |
2335 | if (!node) { | |
2336 | ret = -ENOMEM; | |
2337 | break; | |
2338 | } | |
2339 | INIT_LIST_HEAD(&node->list); | |
2340 | list_add_tail(&node->list, blacklist); | |
2341 | if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) { | |
2342 | ret = -EINVAL; | |
2343 | break; | |
2344 | } | |
2345 | p = strchr(buf, '\t'); | |
2346 | if (p) { | |
2347 | p++; | |
2348 | if (p[strlen(p) - 1] == '\n') | |
2349 | p[strlen(p) - 1] = '\0'; | |
2350 | } else | |
2351 | p = (char *)"unknown"; | |
2352 | node->symbol = strdup(p); | |
2353 | if (!node->symbol) { | |
2354 | ret = -ENOMEM; | |
2355 | break; | |
2356 | } | |
2357 | pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n", | |
2358 | node->start, node->end, node->symbol); | |
2359 | ret++; | |
2360 | } | |
2361 | if (ret < 0) | |
2362 | kprobe_blacklist__delete(blacklist); | |
2363 | fclose(fp); | |
2364 | ||
2365 | return ret; | |
2366 | } | |
2367 | ||
2368 | static struct kprobe_blacklist_node * | |
2369 | kprobe_blacklist__find_by_address(struct list_head *blacklist, | |
2370 | unsigned long address) | |
2371 | { | |
2372 | struct kprobe_blacklist_node *node; | |
2373 | ||
2374 | list_for_each_entry(node, blacklist, list) { | |
2375 | if (node->start <= address && address <= node->end) | |
2376 | return node; | |
2377 | } | |
2378 | ||
2379 | return NULL; | |
2380 | } | |
2381 | ||
b031220d MH |
2382 | static LIST_HEAD(kprobe_blacklist); |
2383 | ||
2384 | static void kprobe_blacklist__init(void) | |
2385 | { | |
2386 | if (!list_empty(&kprobe_blacklist)) | |
2387 | return; | |
2388 | ||
2389 | if (kprobe_blacklist__load(&kprobe_blacklist) < 0) | |
2390 | pr_debug("No kprobe blacklist support, ignored\n"); | |
2391 | } | |
2392 | ||
2393 | static void kprobe_blacklist__release(void) | |
2394 | { | |
2395 | kprobe_blacklist__delete(&kprobe_blacklist); | |
2396 | } | |
2397 | ||
2398 | static bool kprobe_blacklist__listed(unsigned long address) | |
2399 | { | |
2400 | return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address); | |
2401 | } | |
2402 | ||
d350bd57 MH |
2403 | static int perf_probe_event__sprintf(const char *group, const char *event, |
2404 | struct perf_probe_event *pev, | |
ba7ecb02 MH |
2405 | const char *module, |
2406 | struct strbuf *result) | |
278498d4 | 2407 | { |
bf4d5f25 | 2408 | int i, ret; |
909b0360 | 2409 | char *buf; |
278498d4 | 2410 | |
909b0360 MH |
2411 | if (asprintf(&buf, "%s:%s", group, event) < 0) |
2412 | return -errno; | |
bf4d5f25 | 2413 | ret = strbuf_addf(result, " %-20s (on ", buf); |
909b0360 | 2414 | free(buf); |
bf4d5f25 MH |
2415 | if (ret) |
2416 | return ret; | |
4235b045 | 2417 | |
909b0360 MH |
2418 | /* Synthesize only event probe point */ |
2419 | buf = synthesize_perf_probe_point(&pev->point); | |
2420 | if (!buf) | |
2421 | return -ENOMEM; | |
bf4d5f25 | 2422 | ret = strbuf_addstr(result, buf); |
909b0360 | 2423 | free(buf); |
146a1439 | 2424 | |
bf4d5f25 MH |
2425 | if (!ret && module) |
2426 | ret = strbuf_addf(result, " in %s", module); | |
278498d4 | 2427 | |
bf4d5f25 MH |
2428 | if (!ret && pev->nargs > 0) { |
2429 | ret = strbuf_add(result, " with", 5); | |
2430 | for (i = 0; !ret && i < pev->nargs; i++) { | |
909b0360 MH |
2431 | buf = synthesize_perf_probe_arg(&pev->args[i]); |
2432 | if (!buf) | |
2433 | return -ENOMEM; | |
bf4d5f25 | 2434 | ret = strbuf_addf(result, " %s", buf); |
909b0360 | 2435 | free(buf); |
7df2f329 | 2436 | } |
278498d4 | 2437 | } |
bf4d5f25 MH |
2438 | if (!ret) |
2439 | ret = strbuf_addch(result, ')'); | |
909b0360 | 2440 | |
bf4d5f25 | 2441 | return ret; |
278498d4 MH |
2442 | } |
2443 | ||
ba7ecb02 | 2444 | /* Show an event */ |
b02137cc NK |
2445 | int show_perf_probe_event(const char *group, const char *event, |
2446 | struct perf_probe_event *pev, | |
2447 | const char *module, bool use_stdout) | |
ba7ecb02 MH |
2448 | { |
2449 | struct strbuf buf = STRBUF_INIT; | |
2450 | int ret; | |
2451 | ||
d350bd57 | 2452 | ret = perf_probe_event__sprintf(group, event, pev, module, &buf); |
ba7ecb02 MH |
2453 | if (ret >= 0) { |
2454 | if (use_stdout) | |
2455 | printf("%s\n", buf.buf); | |
2456 | else | |
2457 | pr_info("%s\n", buf.buf); | |
2458 | } | |
2459 | strbuf_release(&buf); | |
2460 | ||
2461 | return ret; | |
2462 | } | |
2463 | ||
b6a89643 MH |
2464 | static bool filter_probe_trace_event(struct probe_trace_event *tev, |
2465 | struct strfilter *filter) | |
2466 | { | |
2467 | char tmp[128]; | |
2468 | ||
2469 | /* At first, check the event name itself */ | |
2470 | if (strfilter__compare(filter, tev->event)) | |
2471 | return true; | |
2472 | ||
2473 | /* Next, check the combination of name and group */ | |
2474 | if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0) | |
2475 | return false; | |
2476 | return strfilter__compare(filter, tmp); | |
2477 | } | |
2478 | ||
2479 | static int __show_perf_probe_events(int fd, bool is_kprobe, | |
2480 | struct strfilter *filter) | |
4de189fe | 2481 | { |
225466f1 | 2482 | int ret = 0; |
0e60836b | 2483 | struct probe_trace_event tev; |
4235b045 | 2484 | struct perf_probe_event pev; |
4de189fe MH |
2485 | struct strlist *rawlist; |
2486 | struct str_node *ent; | |
2487 | ||
4235b045 MH |
2488 | memset(&tev, 0, sizeof(tev)); |
2489 | memset(&pev, 0, sizeof(pev)); | |
72041334 | 2490 | |
92f6c72e | 2491 | rawlist = probe_file__get_rawlist(fd); |
146a1439 | 2492 | if (!rawlist) |
6eb08660 | 2493 | return -ENOMEM; |
4de189fe | 2494 | |
602a1f4d | 2495 | strlist__for_each_entry(ent, rawlist) { |
0e60836b | 2496 | ret = parse_probe_trace_command(ent->s, &tev); |
146a1439 | 2497 | if (ret >= 0) { |
b6a89643 MH |
2498 | if (!filter_probe_trace_event(&tev, filter)) |
2499 | goto next; | |
225466f1 SD |
2500 | ret = convert_to_perf_probe_event(&tev, &pev, |
2501 | is_kprobe); | |
ba7ecb02 MH |
2502 | if (ret < 0) |
2503 | goto next; | |
d350bd57 MH |
2504 | ret = show_perf_probe_event(pev.group, pev.event, |
2505 | &pev, tev.point.module, | |
ba7ecb02 | 2506 | true); |
146a1439 | 2507 | } |
b6a89643 | 2508 | next: |
4235b045 | 2509 | clear_perf_probe_event(&pev); |
0e60836b | 2510 | clear_probe_trace_event(&tev); |
146a1439 MH |
2511 | if (ret < 0) |
2512 | break; | |
4de189fe | 2513 | } |
4de189fe | 2514 | strlist__delete(rawlist); |
7737af01 MH |
2515 | /* Cleanup cached debuginfo if needed */ |
2516 | debuginfo_cache__exit(); | |
146a1439 MH |
2517 | |
2518 | return ret; | |
4de189fe MH |
2519 | } |
2520 | ||
225466f1 | 2521 | /* List up current perf-probe events */ |
b6a89643 | 2522 | int show_perf_probe_events(struct strfilter *filter) |
225466f1 | 2523 | { |
5e45187c | 2524 | int kp_fd, up_fd, ret; |
225466f1 SD |
2525 | |
2526 | setup_pager(); | |
225466f1 | 2527 | |
1f3736c9 MH |
2528 | if (probe_conf.cache) |
2529 | return probe_cache__show_all_caches(filter); | |
2530 | ||
9bae1e8c | 2531 | ret = init_probe_symbol_maps(false); |
225466f1 SD |
2532 | if (ret < 0) |
2533 | return ret; | |
2534 | ||
92f6c72e MH |
2535 | ret = probe_file__open_both(&kp_fd, &up_fd, 0); |
2536 | if (ret < 0) | |
2537 | return ret; | |
225466f1 | 2538 | |
92f6c72e MH |
2539 | if (kp_fd >= 0) |
2540 | ret = __show_perf_probe_events(kp_fd, true, filter); | |
2541 | if (up_fd >= 0 && ret >= 0) | |
b6a89643 | 2542 | ret = __show_perf_probe_events(up_fd, false, filter); |
92f6c72e MH |
2543 | if (kp_fd > 0) |
2544 | close(kp_fd); | |
2545 | if (up_fd > 0) | |
5e45187c | 2546 | close(up_fd); |
9bae1e8c | 2547 | exit_probe_symbol_maps(); |
225466f1 | 2548 | |
146a1439 | 2549 | return ret; |
50656eec MH |
2550 | } |
2551 | ||
146a1439 MH |
2552 | static int get_new_event_name(char *buf, size_t len, const char *base, |
2553 | struct strlist *namelist, bool allow_suffix) | |
b498ce1f MH |
2554 | { |
2555 | int i, ret; | |
663b1151 | 2556 | char *p, *nbase; |
17f88fcd | 2557 | |
3099c026 NR |
2558 | if (*base == '.') |
2559 | base++; | |
663b1151 MH |
2560 | nbase = strdup(base); |
2561 | if (!nbase) | |
2562 | return -ENOMEM; | |
2563 | ||
2564 | /* Cut off the dot suffixes (e.g. .const, .isra)*/ | |
2565 | p = strchr(nbase, '.'); | |
2566 | if (p && p != nbase) | |
2567 | *p = '\0'; | |
3099c026 | 2568 | |
663b1151 MH |
2569 | /* Try no suffix number */ |
2570 | ret = e_snprintf(buf, len, "%s", nbase); | |
146a1439 | 2571 | if (ret < 0) { |
5f03cba4 | 2572 | pr_debug("snprintf() failed: %d\n", ret); |
663b1151 | 2573 | goto out; |
146a1439 | 2574 | } |
17f88fcd | 2575 | if (!strlist__has_entry(namelist, buf)) |
663b1151 | 2576 | goto out; |
17f88fcd | 2577 | |
d761b08b | 2578 | if (!allow_suffix) { |
03e01f56 WN |
2579 | pr_warning("Error: event \"%s\" already exists.\n" |
2580 | " Hint: Remove existing event by 'perf probe -d'\n" | |
2581 | " or force duplicates by 'perf probe -f'\n" | |
2582 | " or set 'force=yes' in BPF source.\n", | |
2583 | buf); | |
663b1151 MH |
2584 | ret = -EEXIST; |
2585 | goto out; | |
d761b08b MH |
2586 | } |
2587 | ||
17f88fcd MH |
2588 | /* Try to add suffix */ |
2589 | for (i = 1; i < MAX_EVENT_INDEX; i++) { | |
663b1151 | 2590 | ret = e_snprintf(buf, len, "%s_%d", nbase, i); |
146a1439 | 2591 | if (ret < 0) { |
5f03cba4 | 2592 | pr_debug("snprintf() failed: %d\n", ret); |
663b1151 | 2593 | goto out; |
146a1439 | 2594 | } |
b498ce1f MH |
2595 | if (!strlist__has_entry(namelist, buf)) |
2596 | break; | |
2597 | } | |
146a1439 MH |
2598 | if (i == MAX_EVENT_INDEX) { |
2599 | pr_warning("Too many events are on the same function.\n"); | |
2600 | ret = -ERANGE; | |
2601 | } | |
2602 | ||
663b1151 MH |
2603 | out: |
2604 | free(nbase); | |
146a1439 | 2605 | return ret; |
b498ce1f MH |
2606 | } |
2607 | ||
79702f61 MH |
2608 | /* Warn if the current kernel's uprobe implementation is old */ |
2609 | static void warn_uprobe_event_compat(struct probe_trace_event *tev) | |
2610 | { | |
2611 | int i; | |
2612 | char *buf = synthesize_probe_trace_command(tev); | |
2613 | ||
2614 | /* Old uprobe event doesn't support memory dereference */ | |
2615 | if (!tev->uprobes || tev->nargs == 0 || !buf) | |
2616 | goto out; | |
2617 | ||
2618 | for (i = 0; i < tev->nargs; i++) | |
2619 | if (strglobmatch(tev->args[i].value, "[$@+-]*")) { | |
2620 | pr_warning("Please upgrade your kernel to at least " | |
2621 | "3.14 to have access to feature %s\n", | |
2622 | tev->args[i].value); | |
2623 | break; | |
2624 | } | |
2625 | out: | |
2626 | free(buf); | |
2627 | } | |
2628 | ||
a3c9de62 MH |
2629 | /* Set new name from original perf_probe_event and namelist */ |
2630 | static int probe_trace_event__set_name(struct probe_trace_event *tev, | |
2631 | struct perf_probe_event *pev, | |
2632 | struct strlist *namelist, | |
2633 | bool allow_suffix) | |
2634 | { | |
2635 | const char *event, *group; | |
2636 | char buf[64]; | |
2637 | int ret; | |
2638 | ||
bc062230 | 2639 | /* If probe_event or trace_event already have the name, reuse it */ |
42bba263 | 2640 | if (pev->event && !pev->sdt) |
a3c9de62 | 2641 | event = pev->event; |
bc062230 MH |
2642 | else if (tev->event) |
2643 | event = tev->event; | |
2644 | else { | |
2645 | /* Or generate new one from probe point */ | |
da15bd9d WN |
2646 | if (pev->point.function && |
2647 | (strncmp(pev->point.function, "0x", 2) != 0) && | |
2648 | !strisglob(pev->point.function)) | |
a3c9de62 MH |
2649 | event = pev->point.function; |
2650 | else | |
2651 | event = tev->point.realname; | |
bc062230 | 2652 | } |
42bba263 | 2653 | if (pev->group && !pev->sdt) |
a3c9de62 | 2654 | group = pev->group; |
bc062230 MH |
2655 | else if (tev->group) |
2656 | group = tev->group; | |
a3c9de62 MH |
2657 | else |
2658 | group = PERFPROBE_GROUP; | |
2659 | ||
2660 | /* Get an unused new event name */ | |
2661 | ret = get_new_event_name(buf, 64, event, | |
2662 | namelist, allow_suffix); | |
2663 | if (ret < 0) | |
2664 | return ret; | |
2665 | ||
2666 | event = buf; | |
2667 | ||
2668 | tev->event = strdup(event); | |
2669 | tev->group = strdup(group); | |
2670 | if (tev->event == NULL || tev->group == NULL) | |
2671 | return -ENOMEM; | |
2672 | ||
2673 | /* Add added event name to namelist */ | |
2674 | strlist__add(namelist, event); | |
2675 | return 0; | |
2676 | } | |
2677 | ||
1de7b8bf MH |
2678 | static int __open_probe_file_and_namelist(bool uprobe, |
2679 | struct strlist **namelist) | |
50656eec | 2680 | { |
1de7b8bf | 2681 | int fd; |
50656eec | 2682 | |
1de7b8bf | 2683 | fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0)); |
92f6c72e | 2684 | if (fd < 0) |
146a1439 | 2685 | return fd; |
5e45187c | 2686 | |
b498ce1f | 2687 | /* Get current event names */ |
1de7b8bf MH |
2688 | *namelist = probe_file__get_namelist(fd); |
2689 | if (!(*namelist)) { | |
146a1439 | 2690 | pr_debug("Failed to get current event list.\n"); |
1de7b8bf MH |
2691 | close(fd); |
2692 | return -ENOMEM; | |
146a1439 | 2693 | } |
1de7b8bf MH |
2694 | return fd; |
2695 | } | |
2696 | ||
2697 | static int __add_probe_trace_events(struct perf_probe_event *pev, | |
2698 | struct probe_trace_event *tevs, | |
2699 | int ntevs, bool allow_suffix) | |
2700 | { | |
2701 | int i, fd[2] = {-1, -1}, up, ret; | |
2702 | struct probe_trace_event *tev = NULL; | |
2703 | struct probe_cache *cache = NULL; | |
2704 | struct strlist *namelist[2] = {NULL, NULL}; | |
2705 | ||
2706 | up = pev->uprobes ? 1 : 0; | |
2707 | fd[up] = __open_probe_file_and_namelist(up, &namelist[up]); | |
2708 | if (fd[up] < 0) | |
2709 | return fd[up]; | |
4235b045 | 2710 | |
146a1439 | 2711 | ret = 0; |
02b95dad | 2712 | for (i = 0; i < ntevs; i++) { |
4235b045 | 2713 | tev = &tevs[i]; |
1de7b8bf MH |
2714 | up = tev->uprobes ? 1 : 0; |
2715 | if (fd[up] == -1) { /* Open the kprobe/uprobe_events */ | |
2716 | fd[up] = __open_probe_file_and_namelist(up, | |
2717 | &namelist[up]); | |
2718 | if (fd[up] < 0) | |
2719 | goto close_out; | |
2720 | } | |
b031220d | 2721 | /* Skip if the symbol is out of .text or blacklisted */ |
bc062230 | 2722 | if (!tev->point.symbol && !pev->uprobes) |
5a51fcd1 | 2723 | continue; |
9aaf5a5f | 2724 | |
a3c9de62 | 2725 | /* Set new name for tev (and update namelist) */ |
1de7b8bf | 2726 | ret = probe_trace_event__set_name(tev, pev, namelist[up], |
a3c9de62 | 2727 | allow_suffix); |
146a1439 MH |
2728 | if (ret < 0) |
2729 | break; | |
4235b045 | 2730 | |
1de7b8bf | 2731 | ret = probe_file__add_event(fd[up], tev); |
146a1439 MH |
2732 | if (ret < 0) |
2733 | break; | |
4235b045 | 2734 | |
4235b045 MH |
2735 | /* |
2736 | * Probes after the first probe which comes from same | |
2737 | * user input are always allowed to add suffix, because | |
2738 | * there might be several addresses corresponding to | |
2739 | * one code line. | |
2740 | */ | |
2741 | allow_suffix = true; | |
50656eec | 2742 | } |
79702f61 MH |
2743 | if (ret == -EINVAL && pev->uprobes) |
2744 | warn_uprobe_event_compat(tev); | |
2fd457a3 MH |
2745 | if (ret == 0 && probe_conf.cache) { |
2746 | cache = probe_cache__new(pev->target); | |
2747 | if (!cache || | |
2748 | probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 || | |
2749 | probe_cache__commit(cache) < 0) | |
2750 | pr_warning("Failed to add event to probe cache\n"); | |
2751 | probe_cache__delete(cache); | |
2752 | } | |
146a1439 | 2753 | |
ae2cb1ac | 2754 | close_out: |
1de7b8bf MH |
2755 | for (up = 0; up < 2; up++) { |
2756 | strlist__delete(namelist[up]); | |
2757 | if (fd[up] >= 0) | |
2758 | close(fd[up]); | |
2759 | } | |
146a1439 | 2760 | return ret; |
50656eec | 2761 | } |
fa28244d | 2762 | |
6bb536cc WN |
2763 | static int find_probe_functions(struct map *map, char *name, |
2764 | struct symbol **syms) | |
eb948e50 | 2765 | { |
564c62a4 | 2766 | int found = 0; |
0a3873a8 | 2767 | struct symbol *sym; |
4c859351 | 2768 | struct rb_node *tmp; |
564c62a4 | 2769 | |
be39db9f | 2770 | if (map__load(map) < 0) |
75e4a2a6 WN |
2771 | return 0; |
2772 | ||
4c859351 | 2773 | map__for_each_symbol(map, sym, tmp) { |
6bb536cc | 2774 | if (strglobmatch(sym->name, name)) { |
4c859351 | 2775 | found++; |
6bb536cc WN |
2776 | if (syms && found < probe_conf.max_probes) |
2777 | syms[found - 1] = sym; | |
2778 | } | |
eb948e50 | 2779 | } |
564c62a4 NK |
2780 | |
2781 | return found; | |
eb948e50 MH |
2782 | } |
2783 | ||
7b6ff0bd NR |
2784 | void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused, |
2785 | struct probe_trace_event *tev __maybe_unused, | |
0b3c2264 NR |
2786 | struct map *map __maybe_unused, |
2787 | struct symbol *sym __maybe_unused) { } | |
7b6ff0bd | 2788 | |
eb948e50 MH |
2789 | /* |
2790 | * Find probe function addresses from map. | |
2791 | * Return an error or the number of found probe_trace_event | |
2792 | */ | |
2793 | static int find_probe_trace_events_from_map(struct perf_probe_event *pev, | |
ddb2f58f | 2794 | struct probe_trace_event **tevs) |
e0faa8d3 | 2795 | { |
eb948e50 | 2796 | struct map *map = NULL; |
eb948e50 | 2797 | struct ref_reloc_sym *reloc_sym = NULL; |
e0faa8d3 | 2798 | struct symbol *sym; |
6bb536cc | 2799 | struct symbol **syms = NULL; |
0e60836b | 2800 | struct probe_trace_event *tev; |
eb948e50 MH |
2801 | struct perf_probe_point *pp = &pev->point; |
2802 | struct probe_trace_point *tp; | |
564c62a4 | 2803 | int num_matched_functions; |
b031220d | 2804 | int ret, i, j, skipped = 0; |
c61fb959 | 2805 | char *mod_name; |
4235b045 | 2806 | |
44225521 | 2807 | map = get_target_map(pev->target, pev->uprobes); |
eb948e50 MH |
2808 | if (!map) { |
2809 | ret = -EINVAL; | |
2810 | goto out; | |
fb7345bb MH |
2811 | } |
2812 | ||
6bb536cc WN |
2813 | syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes); |
2814 | if (!syms) { | |
2815 | ret = -ENOMEM; | |
2816 | goto out; | |
2817 | } | |
2818 | ||
eb948e50 MH |
2819 | /* |
2820 | * Load matched symbols: Since the different local symbols may have | |
2821 | * same name but different addresses, this lists all the symbols. | |
2822 | */ | |
6bb536cc | 2823 | num_matched_functions = find_probe_functions(map, pp->function, syms); |
564c62a4 | 2824 | if (num_matched_functions == 0) { |
eb948e50 | 2825 | pr_err("Failed to find symbol %s in %s\n", pp->function, |
44225521 | 2826 | pev->target ? : "kernel"); |
eb948e50 MH |
2827 | ret = -ENOENT; |
2828 | goto out; | |
ddb2f58f | 2829 | } else if (num_matched_functions > probe_conf.max_probes) { |
eb948e50 | 2830 | pr_err("Too many functions matched in %s\n", |
44225521 | 2831 | pev->target ? : "kernel"); |
eb948e50 MH |
2832 | ret = -E2BIG; |
2833 | goto out; | |
fb7345bb MH |
2834 | } |
2835 | ||
1a8ac29c | 2836 | /* Note that the symbols in the kmodule are not relocated */ |
7ab31d94 NR |
2837 | if (!pev->uprobes && !pev->target && |
2838 | (!pp->retprobe || kretprobe_offset_is_supported())) { | |
0560a0c4 | 2839 | reloc_sym = kernel_get_ref_reloc_sym(); |
eb948e50 MH |
2840 | if (!reloc_sym) { |
2841 | pr_warning("Relocated base symbol is not found!\n"); | |
2842 | ret = -EINVAL; | |
2843 | goto out; | |
2844 | } | |
2845 | } | |
4235b045 | 2846 | |
eb948e50 MH |
2847 | /* Setup result trace-probe-events */ |
2848 | *tevs = zalloc(sizeof(*tev) * num_matched_functions); | |
2849 | if (!*tevs) { | |
02b95dad | 2850 | ret = -ENOMEM; |
eb948e50 | 2851 | goto out; |
02b95dad | 2852 | } |
ce27a443 | 2853 | |
eb948e50 | 2854 | ret = 0; |
564c62a4 | 2855 | |
6bb536cc WN |
2856 | for (j = 0; j < num_matched_functions; j++) { |
2857 | sym = syms[j]; | |
2858 | ||
eb948e50 MH |
2859 | tev = (*tevs) + ret; |
2860 | tp = &tev->point; | |
2861 | if (ret == num_matched_functions) { | |
2862 | pr_warning("Too many symbols are listed. Skip it.\n"); | |
2863 | break; | |
ce27a443 | 2864 | } |
eb948e50 | 2865 | ret++; |
ce27a443 | 2866 | |
eb948e50 MH |
2867 | if (pp->offset > sym->end - sym->start) { |
2868 | pr_warning("Offset %ld is bigger than the size of %s\n", | |
2869 | pp->offset, sym->name); | |
2870 | ret = -ENOENT; | |
2871 | goto err_out; | |
2872 | } | |
2873 | /* Add one probe point */ | |
2874 | tp->address = map->unmap_ip(map, sym->start) + pp->offset; | |
1a8ac29c MH |
2875 | |
2876 | /* Check the kprobe (not in module) is within .text */ | |
2877 | if (!pev->uprobes && !pev->target && | |
b031220d MH |
2878 | kprobe_warn_out_range(sym->name, tp->address)) { |
2879 | tp->symbol = NULL; /* Skip it */ | |
2880 | skipped++; | |
2881 | } else if (reloc_sym) { | |
eb948e50 MH |
2882 | tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out); |
2883 | tp->offset = tp->address - reloc_sym->addr; | |
2884 | } else { | |
2885 | tp->symbol = strdup_or_goto(sym->name, nomem_out); | |
2886 | tp->offset = pp->offset; | |
2887 | } | |
6bb536cc WN |
2888 | tp->realname = strdup_or_goto(sym->name, nomem_out); |
2889 | ||
eb948e50 | 2890 | tp->retprobe = pp->retprobe; |
c61fb959 RB |
2891 | if (pev->target) { |
2892 | if (pev->uprobes) { | |
2893 | tev->point.module = strdup_or_goto(pev->target, | |
2894 | nomem_out); | |
2895 | } else { | |
2896 | mod_name = find_module_name(pev->target); | |
2897 | tev->point.module = | |
2898 | strdup(mod_name ? mod_name : pev->target); | |
2899 | free(mod_name); | |
2900 | if (!tev->point.module) | |
2901 | goto nomem_out; | |
2902 | } | |
2903 | } | |
eb948e50 MH |
2904 | tev->uprobes = pev->uprobes; |
2905 | tev->nargs = pev->nargs; | |
2906 | if (tev->nargs) { | |
2907 | tev->args = zalloc(sizeof(struct probe_trace_arg) * | |
2908 | tev->nargs); | |
2909 | if (tev->args == NULL) | |
2910 | goto nomem_out; | |
e334016f | 2911 | } |
48481938 | 2912 | for (i = 0; i < tev->nargs; i++) { |
eb948e50 MH |
2913 | if (pev->args[i].name) |
2914 | tev->args[i].name = | |
2915 | strdup_or_goto(pev->args[i].name, | |
2916 | nomem_out); | |
2917 | ||
2918 | tev->args[i].value = strdup_or_goto(pev->args[i].var, | |
2919 | nomem_out); | |
2920 | if (pev->args[i].type) | |
2921 | tev->args[i].type = | |
2922 | strdup_or_goto(pev->args[i].type, | |
2923 | nomem_out); | |
48481938 | 2924 | } |
0b3c2264 | 2925 | arch__fix_tev_from_maps(pev, tev, map, sym); |
4235b045 | 2926 | } |
b031220d MH |
2927 | if (ret == skipped) { |
2928 | ret = -ENOENT; | |
2929 | goto err_out; | |
2930 | } | |
4235b045 | 2931 | |
eb948e50 | 2932 | out: |
eebc509b | 2933 | map__put(map); |
6bb536cc | 2934 | free(syms); |
eb948e50 | 2935 | return ret; |
225466f1 | 2936 | |
eb948e50 MH |
2937 | nomem_out: |
2938 | ret = -ENOMEM; | |
2939 | err_out: | |
2940 | clear_probe_trace_events(*tevs, num_matched_functions); | |
2941 | zfree(tevs); | |
2942 | goto out; | |
2943 | } | |
1c1bc922 | 2944 | |
da15bd9d WN |
2945 | static int try_to_find_absolute_address(struct perf_probe_event *pev, |
2946 | struct probe_trace_event **tevs) | |
2947 | { | |
2948 | struct perf_probe_point *pp = &pev->point; | |
2949 | struct probe_trace_event *tev; | |
2950 | struct probe_trace_point *tp; | |
2951 | int i, err; | |
2952 | ||
2953 | if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2))) | |
2954 | return -EINVAL; | |
2955 | if (perf_probe_event_need_dwarf(pev)) | |
2956 | return -EINVAL; | |
2957 | ||
2958 | /* | |
2959 | * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at | |
2960 | * absolute address. | |
2961 | * | |
2962 | * Only one tev can be generated by this. | |
2963 | */ | |
2964 | *tevs = zalloc(sizeof(*tev)); | |
2965 | if (!*tevs) | |
2966 | return -ENOMEM; | |
2967 | ||
2968 | tev = *tevs; | |
2969 | tp = &tev->point; | |
2970 | ||
2971 | /* | |
2972 | * Don't use tp->offset, use address directly, because | |
2973 | * in synthesize_probe_trace_command() address cannot be | |
2974 | * zero. | |
2975 | */ | |
2976 | tp->address = pev->point.abs_address; | |
2977 | tp->retprobe = pp->retprobe; | |
2978 | tev->uprobes = pev->uprobes; | |
2979 | ||
2980 | err = -ENOMEM; | |
2981 | /* | |
2982 | * Give it a '0x' leading symbol name. | |
2983 | * In __add_probe_trace_events, a NULL symbol is interpreted as | |
2984 | * invalud. | |
2985 | */ | |
2986 | if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0) | |
2987 | goto errout; | |
2988 | ||
2989 | /* For kprobe, check range */ | |
2990 | if ((!tev->uprobes) && | |
2991 | (kprobe_warn_out_range(tev->point.symbol, | |
2992 | tev->point.address))) { | |
2993 | err = -EACCES; | |
2994 | goto errout; | |
2995 | } | |
2996 | ||
2997 | if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0) | |
2998 | goto errout; | |
2999 | ||
3000 | if (pev->target) { | |
3001 | tp->module = strdup(pev->target); | |
3002 | if (!tp->module) | |
3003 | goto errout; | |
3004 | } | |
3005 | ||
3006 | if (tev->group) { | |
3007 | tev->group = strdup(pev->group); | |
3008 | if (!tev->group) | |
3009 | goto errout; | |
3010 | } | |
3011 | ||
3012 | if (pev->event) { | |
3013 | tev->event = strdup(pev->event); | |
3014 | if (!tev->event) | |
3015 | goto errout; | |
3016 | } | |
3017 | ||
3018 | tev->nargs = pev->nargs; | |
3019 | tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); | |
d1d0e29c | 3020 | if (!tev->args) |
da15bd9d | 3021 | goto errout; |
d1d0e29c | 3022 | |
da15bd9d WN |
3023 | for (i = 0; i < tev->nargs; i++) |
3024 | copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]); | |
3025 | ||
3026 | return 1; | |
3027 | ||
3028 | errout: | |
42e233ca ME |
3029 | clear_probe_trace_events(*tevs, 1); |
3030 | *tevs = NULL; | |
da15bd9d WN |
3031 | return err; |
3032 | } | |
3033 | ||
42bba263 MH |
3034 | /* Concatinate two arrays */ |
3035 | static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b) | |
3036 | { | |
3037 | void *ret; | |
3038 | ||
3039 | ret = malloc(sz_a + sz_b); | |
3040 | if (ret) { | |
3041 | memcpy(ret, a, sz_a); | |
3042 | memcpy(ret + sz_a, b, sz_b); | |
3043 | } | |
3044 | return ret; | |
3045 | } | |
3046 | ||
3047 | static int | |
3048 | concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs, | |
3049 | struct probe_trace_event **tevs2, int ntevs2) | |
3050 | { | |
3051 | struct probe_trace_event *new_tevs; | |
3052 | int ret = 0; | |
3053 | ||
f0a30dca | 3054 | if (*ntevs == 0) { |
42bba263 MH |
3055 | *tevs = *tevs2; |
3056 | *ntevs = ntevs2; | |
3057 | *tevs2 = NULL; | |
3058 | return 0; | |
3059 | } | |
3060 | ||
3061 | if (*ntevs + ntevs2 > probe_conf.max_probes) | |
3062 | ret = -E2BIG; | |
3063 | else { | |
3064 | /* Concatinate the array of probe_trace_event */ | |
3065 | new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs), | |
3066 | *tevs2, ntevs2 * sizeof(**tevs2)); | |
3067 | if (!new_tevs) | |
3068 | ret = -ENOMEM; | |
3069 | else { | |
3070 | free(*tevs); | |
3071 | *tevs = new_tevs; | |
3072 | *ntevs += ntevs2; | |
3073 | } | |
3074 | } | |
3075 | if (ret < 0) | |
3076 | clear_probe_trace_events(*tevs2, ntevs2); | |
3077 | zfree(tevs2); | |
3078 | ||
3079 | return ret; | |
3080 | } | |
3081 | ||
3082 | /* | |
3083 | * Try to find probe_trace_event from given probe caches. Return the number | |
3084 | * of cached events found, if an error occurs return the error. | |
3085 | */ | |
3086 | static int find_cached_events(struct perf_probe_event *pev, | |
3087 | struct probe_trace_event **tevs, | |
3088 | const char *target) | |
3089 | { | |
3090 | struct probe_cache *cache; | |
3091 | struct probe_cache_entry *entry; | |
3092 | struct probe_trace_event *tmp_tevs = NULL; | |
3093 | int ntevs = 0; | |
3094 | int ret = 0; | |
3095 | ||
3096 | cache = probe_cache__new(target); | |
3097 | /* Return 0 ("not found") if the target has no probe cache. */ | |
3098 | if (!cache) | |
3099 | return 0; | |
3100 | ||
3101 | for_each_probe_cache_entry(entry, cache) { | |
3102 | /* Skip the cache entry which has no name */ | |
3103 | if (!entry->pev.event || !entry->pev.group) | |
3104 | continue; | |
3105 | if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) && | |
3106 | strglobmatch(entry->pev.event, pev->event)) { | |
3107 | ret = probe_cache_entry__get_event(entry, &tmp_tevs); | |
3108 | if (ret > 0) | |
3109 | ret = concat_probe_trace_events(tevs, &ntevs, | |
3110 | &tmp_tevs, ret); | |
3111 | if (ret < 0) | |
3112 | break; | |
3113 | } | |
3114 | } | |
3115 | probe_cache__delete(cache); | |
3116 | if (ret < 0) { | |
3117 | clear_probe_trace_events(*tevs, ntevs); | |
3118 | zfree(tevs); | |
3119 | } else { | |
3120 | ret = ntevs; | |
3121 | if (ntevs > 0 && target && target[0] == '/') | |
3122 | pev->uprobes = true; | |
3123 | } | |
3124 | ||
3125 | return ret; | |
3126 | } | |
3127 | ||
1de7b8bf MH |
3128 | /* Try to find probe_trace_event from all probe caches */ |
3129 | static int find_cached_events_all(struct perf_probe_event *pev, | |
3130 | struct probe_trace_event **tevs) | |
3131 | { | |
3132 | struct probe_trace_event *tmp_tevs = NULL; | |
3133 | struct strlist *bidlist; | |
3134 | struct str_node *nd; | |
3135 | char *pathname; | |
3136 | int ntevs = 0; | |
3137 | int ret; | |
3138 | ||
3139 | /* Get the buildid list of all valid caches */ | |
3140 | bidlist = build_id_cache__list_all(true); | |
3141 | if (!bidlist) { | |
3142 | ret = -errno; | |
3143 | pr_debug("Failed to get buildids: %d\n", ret); | |
3144 | return ret; | |
3145 | } | |
3146 | ||
3147 | ret = 0; | |
3148 | strlist__for_each_entry(nd, bidlist) { | |
3149 | pathname = build_id_cache__origname(nd->s); | |
3150 | ret = find_cached_events(pev, &tmp_tevs, pathname); | |
3151 | /* In the case of cnt == 0, we just skip it */ | |
3152 | if (ret > 0) | |
3153 | ret = concat_probe_trace_events(tevs, &ntevs, | |
3154 | &tmp_tevs, ret); | |
3155 | free(pathname); | |
3156 | if (ret < 0) | |
3157 | break; | |
3158 | } | |
3159 | strlist__delete(bidlist); | |
3160 | ||
3161 | if (ret < 0) { | |
3162 | clear_probe_trace_events(*tevs, ntevs); | |
3163 | zfree(tevs); | |
3164 | } else | |
3165 | ret = ntevs; | |
3166 | ||
3167 | return ret; | |
3168 | } | |
3169 | ||
bc062230 MH |
3170 | static int find_probe_trace_events_from_cache(struct perf_probe_event *pev, |
3171 | struct probe_trace_event **tevs) | |
3172 | { | |
3173 | struct probe_cache *cache; | |
3174 | struct probe_cache_entry *entry; | |
3175 | struct probe_trace_event *tev; | |
3176 | struct str_node *node; | |
3177 | int ret, i; | |
3178 | ||
1de7b8bf | 3179 | if (pev->sdt) { |
42bba263 | 3180 | /* For SDT/cached events, we use special search functions */ |
1de7b8bf MH |
3181 | if (!pev->target) |
3182 | return find_cached_events_all(pev, tevs); | |
3183 | else | |
3184 | return find_cached_events(pev, tevs, pev->target); | |
3185 | } | |
bc062230 MH |
3186 | cache = probe_cache__new(pev->target); |
3187 | if (!cache) | |
3188 | return 0; | |
3189 | ||
3190 | entry = probe_cache__find(cache, pev); | |
3191 | if (!entry) { | |
36a009fe MH |
3192 | /* SDT must be in the cache */ |
3193 | ret = pev->sdt ? -ENOENT : 0; | |
bc062230 MH |
3194 | goto out; |
3195 | } | |
3196 | ||
3197 | ret = strlist__nr_entries(entry->tevlist); | |
3198 | if (ret > probe_conf.max_probes) { | |
3199 | pr_debug("Too many entries matched in the cache of %s\n", | |
3200 | pev->target ? : "kernel"); | |
3201 | ret = -E2BIG; | |
3202 | goto out; | |
3203 | } | |
3204 | ||
3205 | *tevs = zalloc(ret * sizeof(*tev)); | |
3206 | if (!*tevs) { | |
3207 | ret = -ENOMEM; | |
3208 | goto out; | |
3209 | } | |
3210 | ||
3211 | i = 0; | |
3212 | strlist__for_each_entry(node, entry->tevlist) { | |
3213 | tev = &(*tevs)[i++]; | |
3214 | ret = parse_probe_trace_command(node->s, tev); | |
3215 | if (ret < 0) | |
3216 | goto out; | |
3217 | /* Set the uprobes attribute as same as original */ | |
3218 | tev->uprobes = pev->uprobes; | |
3219 | } | |
3220 | ret = i; | |
3221 | ||
3222 | out: | |
3223 | probe_cache__delete(cache); | |
3224 | return ret; | |
3225 | } | |
3226 | ||
eb948e50 | 3227 | static int convert_to_probe_trace_events(struct perf_probe_event *pev, |
ddb2f58f | 3228 | struct probe_trace_event **tevs) |
eb948e50 MH |
3229 | { |
3230 | int ret; | |
3231 | ||
36a009fe | 3232 | if (!pev->group && !pev->sdt) { |
2a12ec13 MH |
3233 | /* Set group name if not given */ |
3234 | if (!pev->uprobes) { | |
3235 | pev->group = strdup(PERFPROBE_GROUP); | |
3236 | ret = pev->group ? 0 : -ENOMEM; | |
3237 | } else | |
3238 | ret = convert_exec_to_group(pev->target, &pev->group); | |
eb948e50 MH |
3239 | if (ret != 0) { |
3240 | pr_warning("Failed to make a group name.\n"); | |
3241 | return ret; | |
3242 | } | |
02b95dad | 3243 | } |
e334016f | 3244 | |
da15bd9d WN |
3245 | ret = try_to_find_absolute_address(pev, tevs); |
3246 | if (ret > 0) | |
3247 | return ret; | |
3248 | ||
bc062230 MH |
3249 | /* At first, we need to lookup cache entry */ |
3250 | ret = find_probe_trace_events_from_cache(pev, tevs); | |
36a009fe MH |
3251 | if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */ |
3252 | return ret == 0 ? -ENOENT : ret; /* Found in probe cache */ | |
bc062230 | 3253 | |
eb948e50 | 3254 | /* Convert perf_probe_event with debuginfo */ |
ddb2f58f | 3255 | ret = try_to_find_probe_trace_events(pev, tevs); |
eb948e50 MH |
3256 | if (ret != 0) |
3257 | return ret; /* Found in debuginfo or got an error */ | |
3258 | ||
ddb2f58f | 3259 | return find_probe_trace_events_from_map(pev, tevs); |
4235b045 MH |
3260 | } |
3261 | ||
12fae5ef | 3262 | int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs) |
4235b045 | 3263 | { |
844dffa5 | 3264 | int i, ret; |
4235b045 | 3265 | |
4235b045 MH |
3266 | /* Loop 1: convert all events */ |
3267 | for (i = 0; i < npevs; i++) { | |
b031220d | 3268 | /* Init kprobe blacklist if needed */ |
12fae5ef | 3269 | if (!pevs[i].uprobes) |
b031220d | 3270 | kprobe_blacklist__init(); |
4235b045 | 3271 | /* Convert with or without debuginfo */ |
12fae5ef | 3272 | ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs); |
146a1439 | 3273 | if (ret < 0) |
844dffa5 | 3274 | return ret; |
12fae5ef | 3275 | pevs[i].ntevs = ret; |
e0faa8d3 | 3276 | } |
b031220d MH |
3277 | /* This just release blacklist only if allocated */ |
3278 | kprobe_blacklist__release(); | |
e0faa8d3 | 3279 | |
844dffa5 NK |
3280 | return 0; |
3281 | } | |
3282 | ||
1c20b1d1 MH |
3283 | static int show_probe_trace_event(struct probe_trace_event *tev) |
3284 | { | |
3285 | char *buf = synthesize_probe_trace_command(tev); | |
3286 | ||
3287 | if (!buf) { | |
3288 | pr_debug("Failed to synthesize probe trace event.\n"); | |
3289 | return -EINVAL; | |
3290 | } | |
3291 | ||
3292 | /* Showing definition always go stdout */ | |
3293 | printf("%s\n", buf); | |
3294 | free(buf); | |
3295 | ||
3296 | return 0; | |
3297 | } | |
3298 | ||
3299 | int show_probe_trace_events(struct perf_probe_event *pevs, int npevs) | |
3300 | { | |
3301 | struct strlist *namelist = strlist__new(NULL, NULL); | |
3302 | struct probe_trace_event *tev; | |
3303 | struct perf_probe_event *pev; | |
3304 | int i, j, ret = 0; | |
3305 | ||
3306 | if (!namelist) | |
3307 | return -ENOMEM; | |
3308 | ||
3309 | for (j = 0; j < npevs && !ret; j++) { | |
3310 | pev = &pevs[j]; | |
3311 | for (i = 0; i < pev->ntevs && !ret; i++) { | |
3312 | tev = &pev->tevs[i]; | |
3313 | /* Skip if the symbol is out of .text or blacklisted */ | |
3314 | if (!tev->point.symbol && !pev->uprobes) | |
3315 | continue; | |
3316 | ||
3317 | /* Set new name for tev (and update namelist) */ | |
3318 | ret = probe_trace_event__set_name(tev, pev, | |
3319 | namelist, true); | |
3320 | if (!ret) | |
3321 | ret = show_probe_trace_event(tev); | |
3322 | } | |
3323 | } | |
3324 | strlist__delete(namelist); | |
3325 | ||
3326 | return ret; | |
3327 | } | |
3328 | ||
12fae5ef | 3329 | int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs) |
844dffa5 NK |
3330 | { |
3331 | int i, ret = 0; | |
3332 | ||
4235b045 | 3333 | /* Loop 2: add all events */ |
8635bf6e | 3334 | for (i = 0; i < npevs; i++) { |
12fae5ef WN |
3335 | ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs, |
3336 | pevs[i].ntevs, | |
ddb2f58f | 3337 | probe_conf.force_add); |
fbee632d ACM |
3338 | if (ret < 0) |
3339 | break; | |
3340 | } | |
844dffa5 NK |
3341 | return ret; |
3342 | } | |
3343 | ||
12fae5ef | 3344 | void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs) |
844dffa5 NK |
3345 | { |
3346 | int i, j; | |
3347 | ||
449e5b24 MH |
3348 | /* Loop 3: cleanup and free trace events */ |
3349 | for (i = 0; i < npevs; i++) { | |
12fae5ef WN |
3350 | for (j = 0; j < pevs[i].ntevs; j++) |
3351 | clear_probe_trace_event(&pevs[i].tevs[j]); | |
3352 | zfree(&pevs[i].tevs); | |
3353 | pevs[i].ntevs = 0; | |
a43aac29 | 3354 | clear_perf_probe_event(&pevs[i]); |
449e5b24 | 3355 | } |
844dffa5 NK |
3356 | } |
3357 | ||
3358 | int add_perf_probe_events(struct perf_probe_event *pevs, int npevs) | |
3359 | { | |
3360 | int ret; | |
844dffa5 | 3361 | |
9bae1e8c NK |
3362 | ret = init_probe_symbol_maps(pevs->uprobes); |
3363 | if (ret < 0) | |
3364 | return ret; | |
3365 | ||
12fae5ef | 3366 | ret = convert_perf_probe_events(pevs, npevs); |
844dffa5 | 3367 | if (ret == 0) |
12fae5ef | 3368 | ret = apply_perf_probe_events(pevs, npevs); |
844dffa5 | 3369 | |
12fae5ef | 3370 | cleanup_perf_probe_events(pevs, npevs); |
146a1439 | 3371 | |
9bae1e8c | 3372 | exit_probe_symbol_maps(); |
146a1439 | 3373 | return ret; |
e0faa8d3 MH |
3374 | } |
3375 | ||
307a464b | 3376 | int del_perf_probe_events(struct strfilter *filter) |
fa28244d | 3377 | { |
307a464b | 3378 | int ret, ret2, ufd = -1, kfd = -1; |
307a464b MH |
3379 | char *str = strfilter__string(filter); |
3380 | ||
3381 | if (!str) | |
3382 | return -EINVAL; | |
3383 | ||
fa28244d | 3384 | /* Get current event names */ |
92f6c72e MH |
3385 | ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW); |
3386 | if (ret < 0) | |
3387 | goto out; | |
467ec085 | 3388 | |
92f6c72e | 3389 | ret = probe_file__del_events(kfd, filter); |
307a464b | 3390 | if (ret < 0 && ret != -ENOENT) |
225466f1 | 3391 | goto error; |
225466f1 | 3392 | |
92f6c72e | 3393 | ret2 = probe_file__del_events(ufd, filter); |
dddc7ee3 | 3394 | if (ret2 < 0 && ret2 != -ENOENT) { |
307a464b | 3395 | ret = ret2; |
dddc7ee3 MH |
3396 | goto error; |
3397 | } | |
dddc7ee3 | 3398 | ret = 0; |
225466f1 SD |
3399 | |
3400 | error: | |
92f6c72e | 3401 | if (kfd >= 0) |
225466f1 | 3402 | close(kfd); |
92f6c72e | 3403 | if (ufd >= 0) |
225466f1 | 3404 | close(ufd); |
92f6c72e | 3405 | out: |
307a464b | 3406 | free(str); |
146a1439 MH |
3407 | |
3408 | return ret; | |
fa28244d | 3409 | } |
225466f1 | 3410 | |
2df58634 MH |
3411 | int show_available_funcs(const char *target, struct strfilter *_filter, |
3412 | bool user) | |
e80711ca | 3413 | { |
fd227598 | 3414 | struct rb_node *nd; |
e80711ca MH |
3415 | struct map *map; |
3416 | int ret; | |
3417 | ||
9bae1e8c | 3418 | ret = init_probe_symbol_maps(user); |
e80711ca MH |
3419 | if (ret < 0) |
3420 | return ret; | |
3421 | ||
2df58634 | 3422 | /* Get a symbol map */ |
eebc509b | 3423 | map = get_target_map(target, user); |
e80711ca | 3424 | if (!map) { |
2df58634 | 3425 | pr_err("Failed to get a map for %s\n", (target) ? : "kernel"); |
e80711ca MH |
3426 | return -EINVAL; |
3427 | } | |
225466f1 | 3428 | |
be39db9f | 3429 | ret = map__load(map); |
e7049342 MH |
3430 | if (ret) { |
3431 | if (ret == -2) { | |
3432 | char *str = strfilter__string(_filter); | |
3433 | pr_err("Failed to find symbols matched to \"%s\"\n", | |
3434 | str); | |
3435 | free(str); | |
3436 | } else | |
3437 | pr_err("Failed to load symbols in %s\n", | |
3438 | (target) ? : "kernel"); | |
2df58634 MH |
3439 | goto end; |
3440 | } | |
3441 | if (!dso__sorted_by_name(map->dso, map->type)) | |
3442 | dso__sort_by_name(map->dso, map->type); | |
225466f1 | 3443 | |
2df58634 MH |
3444 | /* Show all (filtered) symbols */ |
3445 | setup_pager(); | |
fd227598 ACM |
3446 | |
3447 | for (nd = rb_first(&map->dso->symbol_names[map->type]); nd; nd = rb_next(nd)) { | |
3448 | struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node); | |
3449 | ||
3450 | if (strfilter__compare(_filter, pos->sym.name)) | |
3451 | printf("%s\n", pos->sym.name); | |
3452 | } | |
3453 | ||
2df58634 | 3454 | end: |
eebc509b | 3455 | map__put(map); |
9bae1e8c | 3456 | exit_probe_symbol_maps(); |
225466f1 | 3457 | |
2df58634 | 3458 | return ret; |
225466f1 SD |
3459 | } |
3460 | ||
da15bd9d WN |
3461 | int copy_to_probe_trace_arg(struct probe_trace_arg *tvar, |
3462 | struct perf_probe_arg *pvar) | |
3463 | { | |
3464 | tvar->value = strdup(pvar->var); | |
3465 | if (tvar->value == NULL) | |
3466 | return -ENOMEM; | |
3467 | if (pvar->type) { | |
3468 | tvar->type = strdup(pvar->type); | |
3469 | if (tvar->type == NULL) | |
3470 | return -ENOMEM; | |
3471 | } | |
3472 | if (pvar->name) { | |
3473 | tvar->name = strdup(pvar->name); | |
3474 | if (tvar->name == NULL) | |
3475 | return -ENOMEM; | |
3476 | } else | |
3477 | tvar->name = NULL; | |
3478 | return 0; | |
3479 | } |