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