perf probe: Fix getting the kernel map
[linux/fpc-iii.git] / tools / perf / util / probe-event.c
blobcc4773157b9bb1b49f14acfa24276086488bae36
1 /*
2 * probe-event.c : perf-probe definition to probe_events format converter
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <inttypes.h>
23 #include <sys/utsname.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <limits.h>
34 #include <elf.h>
36 #include "util.h"
37 #include "event.h"
38 #include "strlist.h"
39 #include "strfilter.h"
40 #include "debug.h"
41 #include "cache.h"
42 #include "color.h"
43 #include "symbol.h"
44 #include "thread.h"
45 #include <api/fs/fs.h>
46 #include "trace-event.h" /* For __maybe_unused */
47 #include "probe-event.h"
48 #include "probe-finder.h"
49 #include "probe-file.h"
50 #include "session.h"
51 #include "string2.h"
53 #include "sane_ctype.h"
55 #define PERFPROBE_GROUP "probe"
57 bool probe_event_dry_run; /* Dry run flag */
58 struct probe_conf probe_conf;
60 #define semantic_error(msg ...) pr_err("Semantic error :" msg)
62 int e_snprintf(char *str, size_t size, const char *format, ...)
64 int ret;
65 va_list ap;
66 va_start(ap, format);
67 ret = vsnprintf(str, size, format, ap);
68 va_end(ap);
69 if (ret >= (int)size)
70 ret = -E2BIG;
71 return ret;
74 static struct machine *host_machine;
76 /* Initialize symbol maps and path of vmlinux/modules */
77 int init_probe_symbol_maps(bool user_only)
79 int ret;
81 symbol_conf.sort_by_name = true;
82 symbol_conf.allow_aliases = true;
83 ret = symbol__init(NULL);
84 if (ret < 0) {
85 pr_debug("Failed to init symbol map.\n");
86 goto out;
89 if (host_machine || user_only) /* already initialized */
90 return 0;
92 if (symbol_conf.vmlinux_name)
93 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
95 host_machine = machine__new_host();
96 if (!host_machine) {
97 pr_debug("machine__new_host() failed.\n");
98 symbol__exit();
99 ret = -1;
101 out:
102 if (ret < 0)
103 pr_warning("Failed to init vmlinux path.\n");
104 return ret;
107 void exit_probe_symbol_maps(void)
109 machine__delete(host_machine);
110 host_machine = NULL;
111 symbol__exit();
114 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
116 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
117 struct kmap *kmap;
118 struct map *map = machine__kernel_map(host_machine);
120 if (map__load(map) < 0)
121 return NULL;
123 kmap = map__kmap(map);
124 if (!kmap)
125 return NULL;
126 return kmap->ref_reloc_sym;
129 static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
130 bool reloc, bool reladdr)
132 struct ref_reloc_sym *reloc_sym;
133 struct symbol *sym;
134 struct map *map;
136 /* ref_reloc_sym is just a label. Need a special fix*/
137 reloc_sym = kernel_get_ref_reloc_sym();
138 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
139 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
140 else {
141 sym = machine__find_kernel_symbol_by_name(host_machine, name, &map);
142 if (!sym)
143 return -ENOENT;
144 *addr = map->unmap_ip(map, sym->start) -
145 ((reloc) ? 0 : map->reloc) -
146 ((reladdr) ? map->start : 0);
148 return 0;
151 static struct map *kernel_get_module_map(const char *module)
153 struct maps *maps = machine__kernel_maps(host_machine);
154 struct map *pos;
156 /* A file path -- this is an offline module */
157 if (module && strchr(module, '/'))
158 return dso__new_map(module);
160 if (!module) {
161 pos = machine__kernel_map(host_machine);
162 return map__get(pos);
165 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
166 /* short_name is "[module]" */
167 if (strncmp(pos->dso->short_name + 1, module,
168 pos->dso->short_name_len - 2) == 0 &&
169 module[pos->dso->short_name_len - 2] == '\0') {
170 return map__get(pos);
173 return NULL;
176 struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user)
178 /* Init maps of given executable or kernel */
179 if (user) {
180 struct map *map;
182 map = dso__new_map(target);
183 if (map && map->dso)
184 map->dso->nsinfo = nsinfo__get(nsi);
185 return map;
186 } else {
187 return kernel_get_module_map(target);
191 static int convert_exec_to_group(const char *exec, char **result)
193 char *ptr1, *ptr2, *exec_copy;
194 char buf[64];
195 int ret;
197 exec_copy = strdup(exec);
198 if (!exec_copy)
199 return -ENOMEM;
201 ptr1 = basename(exec_copy);
202 if (!ptr1) {
203 ret = -EINVAL;
204 goto out;
207 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
208 if (!isalnum(*ptr2) && *ptr2 != '_') {
209 *ptr2 = '\0';
210 break;
214 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
215 if (ret < 0)
216 goto out;
218 *result = strdup(buf);
219 ret = *result ? 0 : -ENOMEM;
221 out:
222 free(exec_copy);
223 return ret;
226 static void clear_perf_probe_point(struct perf_probe_point *pp)
228 free(pp->file);
229 free(pp->function);
230 free(pp->lazy_line);
233 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
235 int i;
237 for (i = 0; i < ntevs; i++)
238 clear_probe_trace_event(tevs + i);
241 static bool kprobe_blacklist__listed(unsigned long address);
242 static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
244 u64 etext_addr = 0;
245 int ret;
247 /* Get the address of _etext for checking non-probable text symbol */
248 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
249 false, false);
251 if (ret == 0 && etext_addr < address)
252 pr_warning("%s is out of .text, skip it.\n", symbol);
253 else if (kprobe_blacklist__listed(address))
254 pr_warning("%s is blacklisted function, skip it.\n", symbol);
255 else
256 return false;
258 return true;
262 * @module can be module name of module file path. In case of path,
263 * inspect elf and find out what is actual module name.
264 * Caller has to free mod_name after using it.
266 static char *find_module_name(const char *module)
268 int fd;
269 Elf *elf;
270 GElf_Ehdr ehdr;
271 GElf_Shdr shdr;
272 Elf_Data *data;
273 Elf_Scn *sec;
274 char *mod_name = NULL;
275 int name_offset;
277 fd = open(module, O_RDONLY);
278 if (fd < 0)
279 return NULL;
281 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
282 if (elf == NULL)
283 goto elf_err;
285 if (gelf_getehdr(elf, &ehdr) == NULL)
286 goto ret_err;
288 sec = elf_section_by_name(elf, &ehdr, &shdr,
289 ".gnu.linkonce.this_module", NULL);
290 if (!sec)
291 goto ret_err;
293 data = elf_getdata(sec, NULL);
294 if (!data || !data->d_buf)
295 goto ret_err;
298 * NOTE:
299 * '.gnu.linkonce.this_module' section of kernel module elf directly
300 * maps to 'struct module' from linux/module.h. This section contains
301 * actual module name which will be used by kernel after loading it.
302 * But, we cannot use 'struct module' here since linux/module.h is not
303 * exposed to user-space. Offset of 'name' has remained same from long
304 * time, so hardcoding it here.
306 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
307 name_offset = 12;
308 else /* expect ELFCLASS64 by default */
309 name_offset = 24;
311 mod_name = strdup((char *)data->d_buf + name_offset);
313 ret_err:
314 elf_end(elf);
315 elf_err:
316 close(fd);
317 return mod_name;
320 #ifdef HAVE_DWARF_SUPPORT
322 static int kernel_get_module_dso(const char *module, struct dso **pdso)
324 struct dso *dso;
325 struct map *map;
326 const char *vmlinux_name;
327 int ret = 0;
329 if (module) {
330 char module_name[128];
332 snprintf(module_name, sizeof(module_name), "[%s]", module);
333 map = map_groups__find_by_name(&host_machine->kmaps, module_name);
334 if (map) {
335 dso = map->dso;
336 goto found;
338 pr_debug("Failed to find module %s.\n", module);
339 return -ENOENT;
342 map = machine__kernel_map(host_machine);
343 dso = map->dso;
345 vmlinux_name = symbol_conf.vmlinux_name;
346 dso->load_errno = 0;
347 if (vmlinux_name)
348 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
349 else
350 ret = dso__load_vmlinux_path(dso, map);
351 found:
352 *pdso = dso;
353 return ret;
357 * Some binaries like glibc have special symbols which are on the symbol
358 * table, but not in the debuginfo. If we can find the address of the
359 * symbol from map, we can translate the address back to the probe point.
361 static int find_alternative_probe_point(struct debuginfo *dinfo,
362 struct perf_probe_point *pp,
363 struct perf_probe_point *result,
364 const char *target, struct nsinfo *nsi,
365 bool uprobes)
367 struct map *map = NULL;
368 struct symbol *sym;
369 u64 address = 0;
370 int ret = -ENOENT;
372 /* This can work only for function-name based one */
373 if (!pp->function || pp->file)
374 return -ENOTSUP;
376 map = get_target_map(target, nsi, uprobes);
377 if (!map)
378 return -EINVAL;
380 /* Find the address of given function */
381 map__for_each_symbol_by_name(map, pp->function, sym) {
382 if (uprobes)
383 address = sym->start;
384 else
385 address = map->unmap_ip(map, sym->start) - map->reloc;
386 break;
388 if (!address) {
389 ret = -ENOENT;
390 goto out;
392 pr_debug("Symbol %s address found : %" PRIx64 "\n",
393 pp->function, address);
395 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
396 result);
397 if (ret <= 0)
398 ret = (!ret) ? -ENOENT : ret;
399 else {
400 result->offset += pp->offset;
401 result->line += pp->line;
402 result->retprobe = pp->retprobe;
403 ret = 0;
406 out:
407 map__put(map);
408 return ret;
412 static int get_alternative_probe_event(struct debuginfo *dinfo,
413 struct perf_probe_event *pev,
414 struct perf_probe_point *tmp)
416 int ret;
418 memcpy(tmp, &pev->point, sizeof(*tmp));
419 memset(&pev->point, 0, sizeof(pev->point));
420 ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target,
421 pev->nsi, pev->uprobes);
422 if (ret < 0)
423 memcpy(&pev->point, tmp, sizeof(*tmp));
425 return ret;
428 static int get_alternative_line_range(struct debuginfo *dinfo,
429 struct line_range *lr,
430 const char *target, bool user)
432 struct perf_probe_point pp = { .function = lr->function,
433 .file = lr->file,
434 .line = lr->start };
435 struct perf_probe_point result;
436 int ret, len = 0;
438 memset(&result, 0, sizeof(result));
440 if (lr->end != INT_MAX)
441 len = lr->end - lr->start;
442 ret = find_alternative_probe_point(dinfo, &pp, &result,
443 target, NULL, user);
444 if (!ret) {
445 lr->function = result.function;
446 lr->file = result.file;
447 lr->start = result.line;
448 if (lr->end != INT_MAX)
449 lr->end = lr->start + len;
450 clear_perf_probe_point(&pp);
452 return ret;
455 /* Open new debuginfo of given module */
456 static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi,
457 bool silent)
459 const char *path = module;
460 char reason[STRERR_BUFSIZE];
461 struct debuginfo *ret = NULL;
462 struct dso *dso = NULL;
463 struct nscookie nsc;
464 int err;
466 if (!module || !strchr(module, '/')) {
467 err = kernel_get_module_dso(module, &dso);
468 if (err < 0) {
469 if (!dso || dso->load_errno == 0) {
470 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
471 strcpy(reason, "(unknown)");
472 } else
473 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
474 if (!silent)
475 pr_err("Failed to find the path for %s: %s\n",
476 module ?: "kernel", reason);
477 return NULL;
479 path = dso->long_name;
481 nsinfo__mountns_enter(nsi, &nsc);
482 ret = debuginfo__new(path);
483 if (!ret && !silent) {
484 pr_warning("The %s file has no debug information.\n", path);
485 if (!module || !strtailcmp(path, ".ko"))
486 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
487 else
488 pr_warning("Rebuild with -g, ");
489 pr_warning("or install an appropriate debuginfo package.\n");
491 nsinfo__mountns_exit(&nsc);
492 return ret;
495 /* For caching the last debuginfo */
496 static struct debuginfo *debuginfo_cache;
497 static char *debuginfo_cache_path;
499 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
501 const char *path = module;
503 /* If the module is NULL, it should be the kernel. */
504 if (!module)
505 path = "kernel";
507 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
508 goto out;
510 /* Copy module path */
511 free(debuginfo_cache_path);
512 debuginfo_cache_path = strdup(path);
513 if (!debuginfo_cache_path) {
514 debuginfo__delete(debuginfo_cache);
515 debuginfo_cache = NULL;
516 goto out;
519 debuginfo_cache = open_debuginfo(module, NULL, silent);
520 if (!debuginfo_cache)
521 zfree(&debuginfo_cache_path);
522 out:
523 return debuginfo_cache;
526 static void debuginfo_cache__exit(void)
528 debuginfo__delete(debuginfo_cache);
529 debuginfo_cache = NULL;
530 zfree(&debuginfo_cache_path);
534 static int get_text_start_address(const char *exec, unsigned long *address,
535 struct nsinfo *nsi)
537 Elf *elf;
538 GElf_Ehdr ehdr;
539 GElf_Shdr shdr;
540 int fd, ret = -ENOENT;
541 struct nscookie nsc;
543 nsinfo__mountns_enter(nsi, &nsc);
544 fd = open(exec, O_RDONLY);
545 nsinfo__mountns_exit(&nsc);
546 if (fd < 0)
547 return -errno;
549 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
550 if (elf == NULL) {
551 ret = -EINVAL;
552 goto out_close;
555 if (gelf_getehdr(elf, &ehdr) == NULL)
556 goto out;
558 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
559 goto out;
561 *address = shdr.sh_addr - shdr.sh_offset;
562 ret = 0;
563 out:
564 elf_end(elf);
565 out_close:
566 close(fd);
568 return ret;
572 * Convert trace point to probe point with debuginfo
574 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
575 struct perf_probe_point *pp,
576 bool is_kprobe)
578 struct debuginfo *dinfo = NULL;
579 unsigned long stext = 0;
580 u64 addr = tp->address;
581 int ret = -ENOENT;
583 /* convert the address to dwarf address */
584 if (!is_kprobe) {
585 if (!addr) {
586 ret = -EINVAL;
587 goto error;
589 ret = get_text_start_address(tp->module, &stext, NULL);
590 if (ret < 0)
591 goto error;
592 addr += stext;
593 } else if (tp->symbol) {
594 /* If the module is given, this returns relative address */
595 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
596 false, !!tp->module);
597 if (ret != 0)
598 goto error;
599 addr += tp->offset;
602 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
603 tp->module ? : "kernel");
605 dinfo = debuginfo_cache__open(tp->module, verbose <= 0);
606 if (dinfo)
607 ret = debuginfo__find_probe_point(dinfo,
608 (unsigned long)addr, pp);
609 else
610 ret = -ENOENT;
612 if (ret > 0) {
613 pp->retprobe = tp->retprobe;
614 return 0;
616 error:
617 pr_debug("Failed to find corresponding probes from debuginfo.\n");
618 return ret ? : -ENOENT;
621 /* Adjust symbol name and address */
622 static int post_process_probe_trace_point(struct probe_trace_point *tp,
623 struct map *map, unsigned long offs)
625 struct symbol *sym;
626 u64 addr = tp->address - offs;
628 sym = map__find_symbol(map, addr);
629 if (!sym)
630 return -ENOENT;
632 if (strcmp(sym->name, tp->symbol)) {
633 /* If we have no realname, use symbol for it */
634 if (!tp->realname)
635 tp->realname = tp->symbol;
636 else
637 free(tp->symbol);
638 tp->symbol = strdup(sym->name);
639 if (!tp->symbol)
640 return -ENOMEM;
642 tp->offset = addr - sym->start;
643 tp->address -= offs;
645 return 0;
649 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
650 * and generate new symbols with suffixes such as .constprop.N or .isra.N
651 * etc. Since those symbols are not recorded in DWARF, we have to find
652 * correct generated symbols from offline ELF binary.
653 * For online kernel or uprobes we don't need this because those are
654 * rebased on _text, or already a section relative address.
656 static int
657 post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
658 int ntevs, const char *pathname)
660 struct map *map;
661 unsigned long stext = 0;
662 int i, ret = 0;
664 /* Prepare a map for offline binary */
665 map = dso__new_map(pathname);
666 if (!map || get_text_start_address(pathname, &stext, NULL) < 0) {
667 pr_warning("Failed to get ELF symbols for %s\n", pathname);
668 return -EINVAL;
671 for (i = 0; i < ntevs; i++) {
672 ret = post_process_probe_trace_point(&tevs[i].point,
673 map, stext);
674 if (ret < 0)
675 break;
677 map__put(map);
679 return ret;
682 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
683 int ntevs, const char *exec,
684 struct nsinfo *nsi)
686 int i, ret = 0;
687 unsigned long stext = 0;
689 if (!exec)
690 return 0;
692 ret = get_text_start_address(exec, &stext, nsi);
693 if (ret < 0)
694 return ret;
696 for (i = 0; i < ntevs && ret >= 0; i++) {
697 /* point.address is the address of point.symbol + point.offset */
698 tevs[i].point.address -= stext;
699 tevs[i].point.module = strdup(exec);
700 if (!tevs[i].point.module) {
701 ret = -ENOMEM;
702 break;
704 tevs[i].uprobes = true;
707 return ret;
710 static int
711 post_process_module_probe_trace_events(struct probe_trace_event *tevs,
712 int ntevs, const char *module,
713 struct debuginfo *dinfo)
715 Dwarf_Addr text_offs = 0;
716 int i, ret = 0;
717 char *mod_name = NULL;
718 struct map *map;
720 if (!module)
721 return 0;
723 map = get_target_map(module, NULL, false);
724 if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
725 pr_warning("Failed to get ELF symbols for %s\n", module);
726 return -EINVAL;
729 mod_name = find_module_name(module);
730 for (i = 0; i < ntevs; i++) {
731 ret = post_process_probe_trace_point(&tevs[i].point,
732 map, (unsigned long)text_offs);
733 if (ret < 0)
734 break;
735 tevs[i].point.module =
736 strdup(mod_name ? mod_name : module);
737 if (!tevs[i].point.module) {
738 ret = -ENOMEM;
739 break;
743 free(mod_name);
744 map__put(map);
746 return ret;
749 static int
750 post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
751 int ntevs)
753 struct ref_reloc_sym *reloc_sym;
754 char *tmp;
755 int i, skipped = 0;
757 /* Skip post process if the target is an offline kernel */
758 if (symbol_conf.ignore_vmlinux_buildid)
759 return post_process_offline_probe_trace_events(tevs, ntevs,
760 symbol_conf.vmlinux_name);
762 reloc_sym = kernel_get_ref_reloc_sym();
763 if (!reloc_sym) {
764 pr_warning("Relocated base symbol is not found!\n");
765 return -EINVAL;
768 for (i = 0; i < ntevs; i++) {
769 if (!tevs[i].point.address)
770 continue;
771 if (tevs[i].point.retprobe && !kretprobe_offset_is_supported())
772 continue;
773 /* If we found a wrong one, mark it by NULL symbol */
774 if (kprobe_warn_out_range(tevs[i].point.symbol,
775 tevs[i].point.address)) {
776 tmp = NULL;
777 skipped++;
778 } else {
779 tmp = strdup(reloc_sym->name);
780 if (!tmp)
781 return -ENOMEM;
783 /* If we have no realname, use symbol for it */
784 if (!tevs[i].point.realname)
785 tevs[i].point.realname = tevs[i].point.symbol;
786 else
787 free(tevs[i].point.symbol);
788 tevs[i].point.symbol = tmp;
789 tevs[i].point.offset = tevs[i].point.address -
790 reloc_sym->unrelocated_addr;
792 return skipped;
795 void __weak
796 arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
797 int ntevs __maybe_unused)
801 /* Post processing the probe events */
802 static int post_process_probe_trace_events(struct perf_probe_event *pev,
803 struct probe_trace_event *tevs,
804 int ntevs, const char *module,
805 bool uprobe, struct debuginfo *dinfo)
807 int ret;
809 if (uprobe)
810 ret = add_exec_to_probe_trace_events(tevs, ntevs, module,
811 pev->nsi);
812 else if (module)
813 /* Currently ref_reloc_sym based probe is not for drivers */
814 ret = post_process_module_probe_trace_events(tevs, ntevs,
815 module, dinfo);
816 else
817 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
819 if (ret >= 0)
820 arch__post_process_probe_trace_events(pev, ntevs);
822 return ret;
825 /* Try to find perf_probe_event with debuginfo */
826 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
827 struct probe_trace_event **tevs)
829 bool need_dwarf = perf_probe_event_need_dwarf(pev);
830 struct perf_probe_point tmp;
831 struct debuginfo *dinfo;
832 int ntevs, ret = 0;
834 dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf);
835 if (!dinfo) {
836 if (need_dwarf)
837 return -ENOENT;
838 pr_debug("Could not open debuginfo. Try to use symbols.\n");
839 return 0;
842 pr_debug("Try to find probe point from debuginfo.\n");
843 /* Searching trace events corresponding to a probe event */
844 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
846 if (ntevs == 0) { /* Not found, retry with an alternative */
847 ret = get_alternative_probe_event(dinfo, pev, &tmp);
848 if (!ret) {
849 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
851 * Write back to the original probe_event for
852 * setting appropriate (user given) event name
854 clear_perf_probe_point(&pev->point);
855 memcpy(&pev->point, &tmp, sizeof(tmp));
859 if (ntevs > 0) { /* Succeeded to find trace events */
860 pr_debug("Found %d probe_trace_events.\n", ntevs);
861 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
862 pev->target, pev->uprobes, dinfo);
863 if (ret < 0 || ret == ntevs) {
864 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
865 clear_probe_trace_events(*tevs, ntevs);
866 zfree(tevs);
867 ntevs = 0;
871 debuginfo__delete(dinfo);
873 if (ntevs == 0) { /* No error but failed to find probe point. */
874 pr_warning("Probe point '%s' not found.\n",
875 synthesize_perf_probe_point(&pev->point));
876 return -ENOENT;
877 } else if (ntevs < 0) {
878 /* Error path : ntevs < 0 */
879 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
880 if (ntevs == -EBADF)
881 pr_warning("Warning: No dwarf info found in the vmlinux - "
882 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
883 if (!need_dwarf) {
884 pr_debug("Trying to use symbols.\n");
885 return 0;
888 return ntevs;
891 #define LINEBUF_SIZE 256
892 #define NR_ADDITIONAL_LINES 2
894 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
896 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
897 const char *color = show_num ? "" : PERF_COLOR_BLUE;
898 const char *prefix = NULL;
900 do {
901 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
902 goto error;
903 if (skip)
904 continue;
905 if (!prefix) {
906 prefix = show_num ? "%7d " : " ";
907 color_fprintf(stdout, color, prefix, l);
909 color_fprintf(stdout, color, "%s", buf);
911 } while (strchr(buf, '\n') == NULL);
913 return 1;
914 error:
915 if (ferror(fp)) {
916 pr_warning("File read error: %s\n",
917 str_error_r(errno, sbuf, sizeof(sbuf)));
918 return -1;
920 return 0;
923 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
925 int rv = __show_one_line(fp, l, skip, show_num);
926 if (rv == 0) {
927 pr_warning("Source file is shorter than expected.\n");
928 rv = -1;
930 return rv;
933 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
934 #define show_one_line(f,l) _show_one_line(f,l,false,false)
935 #define skip_one_line(f,l) _show_one_line(f,l,true,false)
936 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
939 * Show line-range always requires debuginfo to find source file and
940 * line number.
942 static int __show_line_range(struct line_range *lr, const char *module,
943 bool user)
945 int l = 1;
946 struct int_node *ln;
947 struct debuginfo *dinfo;
948 FILE *fp;
949 int ret;
950 char *tmp;
951 char sbuf[STRERR_BUFSIZE];
953 /* Search a line range */
954 dinfo = open_debuginfo(module, NULL, false);
955 if (!dinfo)
956 return -ENOENT;
958 ret = debuginfo__find_line_range(dinfo, lr);
959 if (!ret) { /* Not found, retry with an alternative */
960 ret = get_alternative_line_range(dinfo, lr, module, user);
961 if (!ret)
962 ret = debuginfo__find_line_range(dinfo, lr);
964 debuginfo__delete(dinfo);
965 if (ret == 0 || ret == -ENOENT) {
966 pr_warning("Specified source line is not found.\n");
967 return -ENOENT;
968 } else if (ret < 0) {
969 pr_warning("Debuginfo analysis failed.\n");
970 return ret;
973 /* Convert source file path */
974 tmp = lr->path;
975 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
977 /* Free old path when new path is assigned */
978 if (tmp != lr->path)
979 free(tmp);
981 if (ret < 0) {
982 pr_warning("Failed to find source file path.\n");
983 return ret;
986 setup_pager();
988 if (lr->function)
989 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
990 lr->start - lr->offset);
991 else
992 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
994 fp = fopen(lr->path, "r");
995 if (fp == NULL) {
996 pr_warning("Failed to open %s: %s\n", lr->path,
997 str_error_r(errno, sbuf, sizeof(sbuf)));
998 return -errno;
1000 /* Skip to starting line number */
1001 while (l < lr->start) {
1002 ret = skip_one_line(fp, l++);
1003 if (ret < 0)
1004 goto end;
1007 intlist__for_each_entry(ln, lr->line_list) {
1008 for (; ln->i > l; l++) {
1009 ret = show_one_line(fp, l - lr->offset);
1010 if (ret < 0)
1011 goto end;
1013 ret = show_one_line_with_num(fp, l++ - lr->offset);
1014 if (ret < 0)
1015 goto end;
1018 if (lr->end == INT_MAX)
1019 lr->end = l + NR_ADDITIONAL_LINES;
1020 while (l <= lr->end) {
1021 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1022 if (ret <= 0)
1023 break;
1025 end:
1026 fclose(fp);
1027 return ret;
1030 int show_line_range(struct line_range *lr, const char *module,
1031 struct nsinfo *nsi, bool user)
1033 int ret;
1034 struct nscookie nsc;
1036 ret = init_probe_symbol_maps(user);
1037 if (ret < 0)
1038 return ret;
1039 nsinfo__mountns_enter(nsi, &nsc);
1040 ret = __show_line_range(lr, module, user);
1041 nsinfo__mountns_exit(&nsc);
1042 exit_probe_symbol_maps();
1044 return ret;
1047 static int show_available_vars_at(struct debuginfo *dinfo,
1048 struct perf_probe_event *pev,
1049 struct strfilter *_filter)
1051 char *buf;
1052 int ret, i, nvars;
1053 struct str_node *node;
1054 struct variable_list *vls = NULL, *vl;
1055 struct perf_probe_point tmp;
1056 const char *var;
1058 buf = synthesize_perf_probe_point(&pev->point);
1059 if (!buf)
1060 return -EINVAL;
1061 pr_debug("Searching variables at %s\n", buf);
1063 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
1064 if (!ret) { /* Not found, retry with an alternative */
1065 ret = get_alternative_probe_event(dinfo, pev, &tmp);
1066 if (!ret) {
1067 ret = debuginfo__find_available_vars_at(dinfo, pev,
1068 &vls);
1069 /* Release the old probe_point */
1070 clear_perf_probe_point(&tmp);
1073 if (ret <= 0) {
1074 if (ret == 0 || ret == -ENOENT) {
1075 pr_err("Failed to find the address of %s\n", buf);
1076 ret = -ENOENT;
1077 } else
1078 pr_warning("Debuginfo analysis failed.\n");
1079 goto end;
1082 /* Some variables are found */
1083 fprintf(stdout, "Available variables at %s\n", buf);
1084 for (i = 0; i < ret; i++) {
1085 vl = &vls[i];
1087 * A probe point might be converted to
1088 * several trace points.
1090 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1091 vl->point.offset);
1092 zfree(&vl->point.symbol);
1093 nvars = 0;
1094 if (vl->vars) {
1095 strlist__for_each_entry(node, vl->vars) {
1096 var = strchr(node->s, '\t') + 1;
1097 if (strfilter__compare(_filter, var)) {
1098 fprintf(stdout, "\t\t%s\n", node->s);
1099 nvars++;
1102 strlist__delete(vl->vars);
1104 if (nvars == 0)
1105 fprintf(stdout, "\t\t(No matched variables)\n");
1107 free(vls);
1108 end:
1109 free(buf);
1110 return ret;
1113 /* Show available variables on given probe point */
1114 int show_available_vars(struct perf_probe_event *pevs, int npevs,
1115 struct strfilter *_filter)
1117 int i, ret = 0;
1118 struct debuginfo *dinfo;
1120 ret = init_probe_symbol_maps(pevs->uprobes);
1121 if (ret < 0)
1122 return ret;
1124 dinfo = open_debuginfo(pevs->target, pevs->nsi, false);
1125 if (!dinfo) {
1126 ret = -ENOENT;
1127 goto out;
1130 setup_pager();
1132 for (i = 0; i < npevs && ret >= 0; i++)
1133 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
1135 debuginfo__delete(dinfo);
1136 out:
1137 exit_probe_symbol_maps();
1138 return ret;
1141 #else /* !HAVE_DWARF_SUPPORT */
1143 static void debuginfo_cache__exit(void)
1147 static int
1148 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1149 struct perf_probe_point *pp __maybe_unused,
1150 bool is_kprobe __maybe_unused)
1152 return -ENOSYS;
1155 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
1156 struct probe_trace_event **tevs __maybe_unused)
1158 if (perf_probe_event_need_dwarf(pev)) {
1159 pr_warning("Debuginfo-analysis is not supported.\n");
1160 return -ENOSYS;
1163 return 0;
1166 int show_line_range(struct line_range *lr __maybe_unused,
1167 const char *module __maybe_unused,
1168 struct nsinfo *nsi __maybe_unused,
1169 bool user __maybe_unused)
1171 pr_warning("Debuginfo-analysis is not supported.\n");
1172 return -ENOSYS;
1175 int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
1176 int npevs __maybe_unused,
1177 struct strfilter *filter __maybe_unused)
1179 pr_warning("Debuginfo-analysis is not supported.\n");
1180 return -ENOSYS;
1182 #endif
1184 void line_range__clear(struct line_range *lr)
1186 free(lr->function);
1187 free(lr->file);
1188 free(lr->path);
1189 free(lr->comp_dir);
1190 intlist__delete(lr->line_list);
1191 memset(lr, 0, sizeof(*lr));
1194 int line_range__init(struct line_range *lr)
1196 memset(lr, 0, sizeof(*lr));
1197 lr->line_list = intlist__new(NULL);
1198 if (!lr->line_list)
1199 return -ENOMEM;
1200 else
1201 return 0;
1204 static int parse_line_num(char **ptr, int *val, const char *what)
1206 const char *start = *ptr;
1208 errno = 0;
1209 *val = strtol(*ptr, ptr, 0);
1210 if (errno || *ptr == start) {
1211 semantic_error("'%s' is not a valid number.\n", what);
1212 return -EINVAL;
1214 return 0;
1217 /* Check the name is good for event, group or function */
1218 static bool is_c_func_name(const char *name)
1220 if (!isalpha(*name) && *name != '_')
1221 return false;
1222 while (*++name != '\0') {
1223 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1224 return false;
1226 return true;
1230 * Stuff 'lr' according to the line range described by 'arg'.
1231 * The line range syntax is described by:
1233 * SRC[:SLN[+NUM|-ELN]]
1234 * FNC[@SRC][:SLN[+NUM|-ELN]]
1236 int parse_line_range_desc(const char *arg, struct line_range *lr)
1238 char *range, *file, *name = strdup(arg);
1239 int err;
1241 if (!name)
1242 return -ENOMEM;
1244 lr->start = 0;
1245 lr->end = INT_MAX;
1247 range = strchr(name, ':');
1248 if (range) {
1249 *range++ = '\0';
1251 err = parse_line_num(&range, &lr->start, "start line");
1252 if (err)
1253 goto err;
1255 if (*range == '+' || *range == '-') {
1256 const char c = *range++;
1258 err = parse_line_num(&range, &lr->end, "end line");
1259 if (err)
1260 goto err;
1262 if (c == '+') {
1263 lr->end += lr->start;
1265 * Adjust the number of lines here.
1266 * If the number of lines == 1, the
1267 * the end of line should be equal to
1268 * the start of line.
1270 lr->end--;
1274 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
1276 err = -EINVAL;
1277 if (lr->start > lr->end) {
1278 semantic_error("Start line must be smaller"
1279 " than end line.\n");
1280 goto err;
1282 if (*range != '\0') {
1283 semantic_error("Tailing with invalid str '%s'.\n", range);
1284 goto err;
1288 file = strchr(name, '@');
1289 if (file) {
1290 *file = '\0';
1291 lr->file = strdup(++file);
1292 if (lr->file == NULL) {
1293 err = -ENOMEM;
1294 goto err;
1296 lr->function = name;
1297 } else if (strchr(name, '/') || strchr(name, '.'))
1298 lr->file = name;
1299 else if (is_c_func_name(name))/* We reuse it for checking funcname */
1300 lr->function = name;
1301 else { /* Invalid name */
1302 semantic_error("'%s' is not a valid function name.\n", name);
1303 err = -EINVAL;
1304 goto err;
1307 return 0;
1308 err:
1309 free(name);
1310 return err;
1313 static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1315 char *ptr;
1317 ptr = strpbrk_esc(*arg, ":");
1318 if (ptr) {
1319 *ptr = '\0';
1320 if (!pev->sdt && !is_c_func_name(*arg))
1321 goto ng_name;
1322 pev->group = strdup_esc(*arg);
1323 if (!pev->group)
1324 return -ENOMEM;
1325 *arg = ptr + 1;
1326 } else
1327 pev->group = NULL;
1329 pev->event = strdup_esc(*arg);
1330 if (pev->event == NULL)
1331 return -ENOMEM;
1333 if (!pev->sdt && !is_c_func_name(pev->event)) {
1334 zfree(&pev->event);
1335 ng_name:
1336 zfree(&pev->group);
1337 semantic_error("%s is bad for event name -it must "
1338 "follow C symbol-naming rule.\n", *arg);
1339 return -EINVAL;
1341 return 0;
1344 /* Parse probepoint definition. */
1345 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
1347 struct perf_probe_point *pp = &pev->point;
1348 char *ptr, *tmp;
1349 char c, nc = 0;
1350 bool file_spec = false;
1351 int ret;
1354 * <Syntax>
1355 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1356 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
1357 * perf probe %[GRP:]SDT_EVENT
1359 if (!arg)
1360 return -EINVAL;
1362 if (is_sdt_event(arg)) {
1363 pev->sdt = true;
1364 if (arg[0] == '%')
1365 arg++;
1368 ptr = strpbrk_esc(arg, ";=@+%");
1369 if (pev->sdt) {
1370 if (ptr) {
1371 if (*ptr != '@') {
1372 semantic_error("%s must be an SDT name.\n",
1373 arg);
1374 return -EINVAL;
1376 /* This must be a target file name or build id */
1377 tmp = build_id_cache__complement(ptr + 1);
1378 if (tmp) {
1379 pev->target = build_id_cache__origname(tmp);
1380 free(tmp);
1381 } else
1382 pev->target = strdup_esc(ptr + 1);
1383 if (!pev->target)
1384 return -ENOMEM;
1385 *ptr = '\0';
1387 ret = parse_perf_probe_event_name(&arg, pev);
1388 if (ret == 0) {
1389 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1390 ret = -errno;
1392 return ret;
1395 if (ptr && *ptr == '=') { /* Event name */
1396 *ptr = '\0';
1397 tmp = ptr + 1;
1398 ret = parse_perf_probe_event_name(&arg, pev);
1399 if (ret < 0)
1400 return ret;
1402 arg = tmp;
1406 * Check arg is function or file name and copy it.
1408 * We consider arg to be a file spec if and only if it satisfies
1409 * all of the below criteria::
1410 * - it does not include any of "+@%",
1411 * - it includes one of ":;", and
1412 * - it has a period '.' in the name.
1414 * Otherwise, we consider arg to be a function specification.
1416 if (!strpbrk_esc(arg, "+@%")) {
1417 ptr = strpbrk_esc(arg, ";:");
1418 /* This is a file spec if it includes a '.' before ; or : */
1419 if (ptr && memchr(arg, '.', ptr - arg))
1420 file_spec = true;
1423 ptr = strpbrk_esc(arg, ";:+@%");
1424 if (ptr) {
1425 nc = *ptr;
1426 *ptr++ = '\0';
1429 if (arg[0] == '\0')
1430 tmp = NULL;
1431 else {
1432 tmp = strdup_esc(arg);
1433 if (tmp == NULL)
1434 return -ENOMEM;
1437 if (file_spec)
1438 pp->file = tmp;
1439 else {
1440 pp->function = tmp;
1443 * Keep pp->function even if this is absolute address,
1444 * so it can mark whether abs_address is valid.
1445 * Which make 'perf probe lib.bin 0x0' possible.
1447 * Note that checking length of tmp is not needed
1448 * because when we access tmp[1] we know tmp[0] is '0',
1449 * so tmp[1] should always valid (but could be '\0').
1451 if (tmp && !strncmp(tmp, "0x", 2)) {
1452 pp->abs_address = strtoul(pp->function, &tmp, 0);
1453 if (*tmp != '\0') {
1454 semantic_error("Invalid absolute address.\n");
1455 return -EINVAL;
1460 /* Parse other options */
1461 while (ptr) {
1462 arg = ptr;
1463 c = nc;
1464 if (c == ';') { /* Lazy pattern must be the last part */
1465 pp->lazy_line = strdup(arg); /* let leave escapes */
1466 if (pp->lazy_line == NULL)
1467 return -ENOMEM;
1468 break;
1470 ptr = strpbrk_esc(arg, ";:+@%");
1471 if (ptr) {
1472 nc = *ptr;
1473 *ptr++ = '\0';
1475 switch (c) {
1476 case ':': /* Line number */
1477 pp->line = strtoul(arg, &tmp, 0);
1478 if (*tmp != '\0') {
1479 semantic_error("There is non-digit char"
1480 " in line number.\n");
1481 return -EINVAL;
1483 break;
1484 case '+': /* Byte offset from a symbol */
1485 pp->offset = strtoul(arg, &tmp, 0);
1486 if (*tmp != '\0') {
1487 semantic_error("There is non-digit character"
1488 " in offset.\n");
1489 return -EINVAL;
1491 break;
1492 case '@': /* File name */
1493 if (pp->file) {
1494 semantic_error("SRC@SRC is not allowed.\n");
1495 return -EINVAL;
1497 pp->file = strdup_esc(arg);
1498 if (pp->file == NULL)
1499 return -ENOMEM;
1500 break;
1501 case '%': /* Probe places */
1502 if (strcmp(arg, "return") == 0) {
1503 pp->retprobe = 1;
1504 } else { /* Others not supported yet */
1505 semantic_error("%%%s is not supported.\n", arg);
1506 return -ENOTSUP;
1508 break;
1509 default: /* Buggy case */
1510 pr_err("This program has a bug at %s:%d.\n",
1511 __FILE__, __LINE__);
1512 return -ENOTSUP;
1513 break;
1517 /* Exclusion check */
1518 if (pp->lazy_line && pp->line) {
1519 semantic_error("Lazy pattern can't be used with"
1520 " line number.\n");
1521 return -EINVAL;
1524 if (pp->lazy_line && pp->offset) {
1525 semantic_error("Lazy pattern can't be used with offset.\n");
1526 return -EINVAL;
1529 if (pp->line && pp->offset) {
1530 semantic_error("Offset can't be used with line number.\n");
1531 return -EINVAL;
1534 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
1535 semantic_error("File always requires line number or "
1536 "lazy pattern.\n");
1537 return -EINVAL;
1540 if (pp->offset && !pp->function) {
1541 semantic_error("Offset requires an entry function.\n");
1542 return -EINVAL;
1545 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
1546 semantic_error("Offset/Line/Lazy pattern can't be used with "
1547 "return probe.\n");
1548 return -EINVAL;
1551 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
1552 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1553 pp->lazy_line);
1554 return 0;
1557 /* Parse perf-probe event argument */
1558 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
1560 char *tmp, *goodname;
1561 struct perf_probe_arg_field **fieldp;
1563 pr_debug("parsing arg: %s into ", str);
1565 tmp = strchr(str, '=');
1566 if (tmp) {
1567 arg->name = strndup(str, tmp - str);
1568 if (arg->name == NULL)
1569 return -ENOMEM;
1570 pr_debug("name:%s ", arg->name);
1571 str = tmp + 1;
1574 tmp = strchr(str, ':');
1575 if (tmp) { /* Type setting */
1576 *tmp = '\0';
1577 arg->type = strdup(tmp + 1);
1578 if (arg->type == NULL)
1579 return -ENOMEM;
1580 pr_debug("type:%s ", arg->type);
1583 tmp = strpbrk(str, "-.[");
1584 if (!is_c_varname(str) || !tmp) {
1585 /* A variable, register, symbol or special value */
1586 arg->var = strdup(str);
1587 if (arg->var == NULL)
1588 return -ENOMEM;
1589 pr_debug("%s\n", arg->var);
1590 return 0;
1593 /* Structure fields or array element */
1594 arg->var = strndup(str, tmp - str);
1595 if (arg->var == NULL)
1596 return -ENOMEM;
1597 goodname = arg->var;
1598 pr_debug("%s, ", arg->var);
1599 fieldp = &arg->field;
1601 do {
1602 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1603 if (*fieldp == NULL)
1604 return -ENOMEM;
1605 if (*tmp == '[') { /* Array */
1606 str = tmp;
1607 (*fieldp)->index = strtol(str + 1, &tmp, 0);
1608 (*fieldp)->ref = true;
1609 if (*tmp != ']' || tmp == str + 1) {
1610 semantic_error("Array index must be a"
1611 " number.\n");
1612 return -EINVAL;
1614 tmp++;
1615 if (*tmp == '\0')
1616 tmp = NULL;
1617 } else { /* Structure */
1618 if (*tmp == '.') {
1619 str = tmp + 1;
1620 (*fieldp)->ref = false;
1621 } else if (tmp[1] == '>') {
1622 str = tmp + 2;
1623 (*fieldp)->ref = true;
1624 } else {
1625 semantic_error("Argument parse error: %s\n",
1626 str);
1627 return -EINVAL;
1629 tmp = strpbrk(str, "-.[");
1631 if (tmp) {
1632 (*fieldp)->name = strndup(str, tmp - str);
1633 if ((*fieldp)->name == NULL)
1634 return -ENOMEM;
1635 if (*str != '[')
1636 goodname = (*fieldp)->name;
1637 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1638 fieldp = &(*fieldp)->next;
1640 } while (tmp);
1641 (*fieldp)->name = strdup(str);
1642 if ((*fieldp)->name == NULL)
1643 return -ENOMEM;
1644 if (*str != '[')
1645 goodname = (*fieldp)->name;
1646 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
1648 /* If no name is specified, set the last field name (not array index)*/
1649 if (!arg->name) {
1650 arg->name = strdup(goodname);
1651 if (arg->name == NULL)
1652 return -ENOMEM;
1654 return 0;
1657 /* Parse perf-probe event command */
1658 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
1660 char **argv;
1661 int argc, i, ret = 0;
1663 argv = argv_split(cmd, &argc);
1664 if (!argv) {
1665 pr_debug("Failed to split arguments.\n");
1666 return -ENOMEM;
1668 if (argc - 1 > MAX_PROBE_ARGS) {
1669 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1670 ret = -ERANGE;
1671 goto out;
1673 /* Parse probe point */
1674 ret = parse_perf_probe_point(argv[0], pev);
1675 if (ret < 0)
1676 goto out;
1678 /* Copy arguments and ensure return probe has no C argument */
1679 pev->nargs = argc - 1;
1680 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1681 if (pev->args == NULL) {
1682 ret = -ENOMEM;
1683 goto out;
1685 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1686 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1687 if (ret >= 0 &&
1688 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
1689 semantic_error("You can't specify local variable for"
1690 " kretprobe.\n");
1691 ret = -EINVAL;
1694 out:
1695 argv_free(argv);
1697 return ret;
1700 /* Returns true if *any* ARG is either C variable, $params or $vars. */
1701 bool perf_probe_with_var(struct perf_probe_event *pev)
1703 int i = 0;
1705 for (i = 0; i < pev->nargs; i++)
1706 if (is_c_varname(pev->args[i].var) ||
1707 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1708 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1709 return true;
1710 return false;
1713 /* Return true if this perf_probe_event requires debuginfo */
1714 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
1716 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1717 return true;
1719 if (perf_probe_with_var(pev))
1720 return true;
1722 return false;
1725 /* Parse probe_events event into struct probe_point */
1726 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
1728 struct probe_trace_point *tp = &tev->point;
1729 char pr;
1730 char *p;
1731 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
1732 int ret, i, argc;
1733 char **argv;
1735 pr_debug("Parsing probe_events: %s\n", cmd);
1736 argv = argv_split(cmd, &argc);
1737 if (!argv) {
1738 pr_debug("Failed to split arguments.\n");
1739 return -ENOMEM;
1741 if (argc < 2) {
1742 semantic_error("Too few probe arguments.\n");
1743 ret = -ERANGE;
1744 goto out;
1747 /* Scan event and group name. */
1748 argv0_str = strdup(argv[0]);
1749 if (argv0_str == NULL) {
1750 ret = -ENOMEM;
1751 goto out;
1753 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1754 fmt2_str = strtok_r(NULL, "/", &fmt);
1755 fmt3_str = strtok_r(NULL, " \t", &fmt);
1756 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1757 || fmt3_str == NULL) {
1758 semantic_error("Failed to parse event name: %s\n", argv[0]);
1759 ret = -EINVAL;
1760 goto out;
1762 pr = fmt1_str[0];
1763 tev->group = strdup(fmt2_str);
1764 tev->event = strdup(fmt3_str);
1765 if (tev->group == NULL || tev->event == NULL) {
1766 ret = -ENOMEM;
1767 goto out;
1769 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
1771 tp->retprobe = (pr == 'r');
1773 /* Scan module name(if there), function name and offset */
1774 p = strchr(argv[1], ':');
1775 if (p) {
1776 tp->module = strndup(argv[1], p - argv[1]);
1777 if (!tp->module) {
1778 ret = -ENOMEM;
1779 goto out;
1781 tev->uprobes = (tp->module[0] == '/');
1782 p++;
1783 } else
1784 p = argv[1];
1785 fmt1_str = strtok_r(p, "+", &fmt);
1786 /* only the address started with 0x */
1787 if (fmt1_str[0] == '0') {
1789 * Fix a special case:
1790 * if address == 0, kernel reports something like:
1791 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1792 * Newer kernel may fix that, but we want to
1793 * support old kernel also.
1795 if (strcmp(fmt1_str, "0x") == 0) {
1796 if (!argv[2] || strcmp(argv[2], "(null)")) {
1797 ret = -EINVAL;
1798 goto out;
1800 tp->address = 0;
1802 free(argv[2]);
1803 for (i = 2; argv[i + 1] != NULL; i++)
1804 argv[i] = argv[i + 1];
1806 argv[i] = NULL;
1807 argc -= 1;
1808 } else
1809 tp->address = strtoul(fmt1_str, NULL, 0);
1810 } else {
1811 /* Only the symbol-based probe has offset */
1812 tp->symbol = strdup(fmt1_str);
1813 if (tp->symbol == NULL) {
1814 ret = -ENOMEM;
1815 goto out;
1817 fmt2_str = strtok_r(NULL, "", &fmt);
1818 if (fmt2_str == NULL)
1819 tp->offset = 0;
1820 else
1821 tp->offset = strtoul(fmt2_str, NULL, 10);
1824 if (tev->uprobes) {
1825 fmt2_str = strchr(p, '(');
1826 if (fmt2_str)
1827 tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0);
1830 tev->nargs = argc - 2;
1831 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
1832 if (tev->args == NULL) {
1833 ret = -ENOMEM;
1834 goto out;
1836 for (i = 0; i < tev->nargs; i++) {
1837 p = strchr(argv[i + 2], '=');
1838 if (p) /* We don't need which register is assigned. */
1839 *p++ = '\0';
1840 else
1841 p = argv[i + 2];
1842 tev->args[i].name = strdup(argv[i + 2]);
1843 /* TODO: parse regs and offset */
1844 tev->args[i].value = strdup(p);
1845 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1846 ret = -ENOMEM;
1847 goto out;
1850 ret = 0;
1851 out:
1852 free(argv0_str);
1853 argv_free(argv);
1854 return ret;
1857 /* Compose only probe arg */
1858 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
1860 struct perf_probe_arg_field *field = pa->field;
1861 struct strbuf buf;
1862 char *ret = NULL;
1863 int err;
1865 if (strbuf_init(&buf, 64) < 0)
1866 return NULL;
1868 if (pa->name && pa->var)
1869 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
1870 else
1871 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1872 if (err)
1873 goto out;
1875 while (field) {
1876 if (field->name[0] == '[')
1877 err = strbuf_addstr(&buf, field->name);
1878 else
1879 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1880 field->name);
1881 field = field->next;
1882 if (err)
1883 goto out;
1886 if (pa->type)
1887 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1888 goto out;
1890 ret = strbuf_detach(&buf, NULL);
1891 out:
1892 strbuf_release(&buf);
1893 return ret;
1896 /* Compose only probe point (not argument) */
1897 char *synthesize_perf_probe_point(struct perf_probe_point *pp)
1899 struct strbuf buf;
1900 char *tmp, *ret = NULL;
1901 int len, err = 0;
1903 if (strbuf_init(&buf, 64) < 0)
1904 return NULL;
1906 if (pp->function) {
1907 if (strbuf_addstr(&buf, pp->function) < 0)
1908 goto out;
1909 if (pp->offset)
1910 err = strbuf_addf(&buf, "+%lu", pp->offset);
1911 else if (pp->line)
1912 err = strbuf_addf(&buf, ":%d", pp->line);
1913 else if (pp->retprobe)
1914 err = strbuf_addstr(&buf, "%return");
1915 if (err)
1916 goto out;
1918 if (pp->file) {
1919 tmp = pp->file;
1920 len = strlen(tmp);
1921 if (len > 30) {
1922 tmp = strchr(pp->file + len - 30, '/');
1923 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1925 err = strbuf_addf(&buf, "@%s", tmp);
1926 if (!err && !pp->function && pp->line)
1927 err = strbuf_addf(&buf, ":%d", pp->line);
1929 if (!err)
1930 ret = strbuf_detach(&buf, NULL);
1931 out:
1932 strbuf_release(&buf);
1933 return ret;
1936 char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1938 struct strbuf buf;
1939 char *tmp, *ret = NULL;
1940 int i;
1942 if (strbuf_init(&buf, 64))
1943 return NULL;
1944 if (pev->event)
1945 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1946 pev->event) < 0)
1947 goto out;
1949 tmp = synthesize_perf_probe_point(&pev->point);
1950 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1951 goto out;
1952 free(tmp);
1954 for (i = 0; i < pev->nargs; i++) {
1955 tmp = synthesize_perf_probe_arg(pev->args + i);
1956 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1957 goto out;
1958 free(tmp);
1961 ret = strbuf_detach(&buf, NULL);
1962 out:
1963 strbuf_release(&buf);
1964 return ret;
1967 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
1968 struct strbuf *buf, int depth)
1970 int err;
1971 if (ref->next) {
1972 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
1973 depth + 1);
1974 if (depth < 0)
1975 return depth;
1977 err = strbuf_addf(buf, "%+ld(", ref->offset);
1978 return (err < 0) ? err : depth;
1981 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
1982 struct strbuf *buf)
1984 struct probe_trace_arg_ref *ref = arg->ref;
1985 int depth = 0, err;
1987 /* Argument name or separator */
1988 if (arg->name)
1989 err = strbuf_addf(buf, " %s=", arg->name);
1990 else
1991 err = strbuf_addch(buf, ' ');
1992 if (err)
1993 return err;
1995 /* Special case: @XXX */
1996 if (arg->value[0] == '@' && arg->ref)
1997 ref = ref->next;
1999 /* Dereferencing arguments */
2000 if (ref) {
2001 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
2002 if (depth < 0)
2003 return depth;
2006 /* Print argument value */
2007 if (arg->value[0] == '@' && arg->ref)
2008 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
2009 else
2010 err = strbuf_addstr(buf, arg->value);
2012 /* Closing */
2013 while (!err && depth--)
2014 err = strbuf_addch(buf, ')');
2016 /* Print argument type */
2017 if (!err && arg->type)
2018 err = strbuf_addf(buf, ":%s", arg->type);
2020 return err;
2023 static int
2024 synthesize_uprobe_trace_def(struct probe_trace_event *tev, struct strbuf *buf)
2026 struct probe_trace_point *tp = &tev->point;
2027 int err;
2029 err = strbuf_addf(buf, "%s:0x%lx", tp->module, tp->address);
2031 if (err >= 0 && tp->ref_ctr_offset) {
2032 if (!uprobe_ref_ctr_is_supported())
2033 return -1;
2034 err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset);
2036 return err >= 0 ? 0 : -1;
2039 char *synthesize_probe_trace_command(struct probe_trace_event *tev)
2041 struct probe_trace_point *tp = &tev->point;
2042 struct strbuf buf;
2043 char *ret = NULL;
2044 int i, err;
2046 /* Uprobes must have tp->module */
2047 if (tev->uprobes && !tp->module)
2048 return NULL;
2050 if (strbuf_init(&buf, 32) < 0)
2051 return NULL;
2053 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2054 tev->group, tev->event) < 0)
2055 goto error;
2057 * If tp->address == 0, then this point must be a
2058 * absolute address uprobe.
2059 * try_to_find_absolute_address() should have made
2060 * tp->symbol to "0x0".
2062 if (tev->uprobes && !tp->address) {
2063 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2064 goto error;
2067 /* Use the tp->address for uprobes */
2068 if (tev->uprobes) {
2069 err = synthesize_uprobe_trace_def(tev, &buf);
2070 } else if (!strncmp(tp->symbol, "0x", 2)) {
2071 /* Absolute address. See try_to_find_absolute_address() */
2072 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2073 tp->module ? ":" : "", tp->address);
2074 } else {
2075 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2076 tp->module ? ":" : "", tp->symbol, tp->offset);
2079 if (err)
2080 goto error;
2082 for (i = 0; i < tev->nargs; i++)
2083 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
2084 goto error;
2086 ret = strbuf_detach(&buf, NULL);
2087 error:
2088 strbuf_release(&buf);
2089 return ret;
2092 static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2093 struct perf_probe_point *pp,
2094 bool is_kprobe)
2096 struct symbol *sym = NULL;
2097 struct map *map = NULL;
2098 u64 addr = tp->address;
2099 int ret = -ENOENT;
2101 if (!is_kprobe) {
2102 map = dso__new_map(tp->module);
2103 if (!map)
2104 goto out;
2105 sym = map__find_symbol(map, addr);
2106 } else {
2107 if (tp->symbol && !addr) {
2108 if (kernel_get_symbol_address_by_name(tp->symbol,
2109 &addr, true, false) < 0)
2110 goto out;
2112 if (addr) {
2113 addr += tp->offset;
2114 sym = machine__find_kernel_symbol(host_machine, addr, &map);
2118 if (!sym)
2119 goto out;
2121 pp->retprobe = tp->retprobe;
2122 pp->offset = addr - map->unmap_ip(map, sym->start);
2123 pp->function = strdup(sym->name);
2124 ret = pp->function ? 0 : -ENOMEM;
2126 out:
2127 if (map && !is_kprobe) {
2128 map__put(map);
2131 return ret;
2134 static int convert_to_perf_probe_point(struct probe_trace_point *tp,
2135 struct perf_probe_point *pp,
2136 bool is_kprobe)
2138 char buf[128];
2139 int ret;
2141 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2142 if (!ret)
2143 return 0;
2144 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2145 if (!ret)
2146 return 0;
2148 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2150 if (tp->symbol) {
2151 pp->function = strdup(tp->symbol);
2152 pp->offset = tp->offset;
2153 } else {
2154 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2155 if (ret < 0)
2156 return ret;
2157 pp->function = strdup(buf);
2158 pp->offset = 0;
2160 if (pp->function == NULL)
2161 return -ENOMEM;
2163 pp->retprobe = tp->retprobe;
2165 return 0;
2168 static int convert_to_perf_probe_event(struct probe_trace_event *tev,
2169 struct perf_probe_event *pev, bool is_kprobe)
2171 struct strbuf buf = STRBUF_INIT;
2172 int i, ret;
2174 /* Convert event/group name */
2175 pev->event = strdup(tev->event);
2176 pev->group = strdup(tev->group);
2177 if (pev->event == NULL || pev->group == NULL)
2178 return -ENOMEM;
2180 /* Convert trace_point to probe_point */
2181 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
2182 if (ret < 0)
2183 return ret;
2185 /* Convert trace_arg to probe_arg */
2186 pev->nargs = tev->nargs;
2187 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2188 if (pev->args == NULL)
2189 return -ENOMEM;
2190 for (i = 0; i < tev->nargs && ret >= 0; i++) {
2191 if (tev->args[i].name)
2192 pev->args[i].name = strdup(tev->args[i].name);
2193 else {
2194 if ((ret = strbuf_init(&buf, 32)) < 0)
2195 goto error;
2196 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2197 pev->args[i].name = strbuf_detach(&buf, NULL);
2199 if (pev->args[i].name == NULL && ret >= 0)
2200 ret = -ENOMEM;
2202 error:
2203 if (ret < 0)
2204 clear_perf_probe_event(pev);
2206 return ret;
2209 void clear_perf_probe_event(struct perf_probe_event *pev)
2211 struct perf_probe_arg_field *field, *next;
2212 int i;
2214 free(pev->event);
2215 free(pev->group);
2216 free(pev->target);
2217 clear_perf_probe_point(&pev->point);
2219 for (i = 0; i < pev->nargs; i++) {
2220 free(pev->args[i].name);
2221 free(pev->args[i].var);
2222 free(pev->args[i].type);
2223 field = pev->args[i].field;
2224 while (field) {
2225 next = field->next;
2226 zfree(&field->name);
2227 free(field);
2228 field = next;
2231 free(pev->args);
2232 memset(pev, 0, sizeof(*pev));
2235 #define strdup_or_goto(str, label) \
2236 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2238 static int perf_probe_point__copy(struct perf_probe_point *dst,
2239 struct perf_probe_point *src)
2241 dst->file = strdup_or_goto(src->file, out_err);
2242 dst->function = strdup_or_goto(src->function, out_err);
2243 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2244 dst->line = src->line;
2245 dst->retprobe = src->retprobe;
2246 dst->offset = src->offset;
2247 return 0;
2249 out_err:
2250 clear_perf_probe_point(dst);
2251 return -ENOMEM;
2254 static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2255 struct perf_probe_arg *src)
2257 struct perf_probe_arg_field *field, **ppfield;
2259 dst->name = strdup_or_goto(src->name, out_err);
2260 dst->var = strdup_or_goto(src->var, out_err);
2261 dst->type = strdup_or_goto(src->type, out_err);
2263 field = src->field;
2264 ppfield = &(dst->field);
2265 while (field) {
2266 *ppfield = zalloc(sizeof(*field));
2267 if (!*ppfield)
2268 goto out_err;
2269 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2270 (*ppfield)->index = field->index;
2271 (*ppfield)->ref = field->ref;
2272 field = field->next;
2273 ppfield = &((*ppfield)->next);
2275 return 0;
2276 out_err:
2277 return -ENOMEM;
2280 int perf_probe_event__copy(struct perf_probe_event *dst,
2281 struct perf_probe_event *src)
2283 int i;
2285 dst->event = strdup_or_goto(src->event, out_err);
2286 dst->group = strdup_or_goto(src->group, out_err);
2287 dst->target = strdup_or_goto(src->target, out_err);
2288 dst->uprobes = src->uprobes;
2290 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2291 goto out_err;
2293 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2294 if (!dst->args)
2295 goto out_err;
2296 dst->nargs = src->nargs;
2298 for (i = 0; i < src->nargs; i++)
2299 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2300 goto out_err;
2301 return 0;
2303 out_err:
2304 clear_perf_probe_event(dst);
2305 return -ENOMEM;
2308 void clear_probe_trace_event(struct probe_trace_event *tev)
2310 struct probe_trace_arg_ref *ref, *next;
2311 int i;
2313 free(tev->event);
2314 free(tev->group);
2315 free(tev->point.symbol);
2316 free(tev->point.realname);
2317 free(tev->point.module);
2318 for (i = 0; i < tev->nargs; i++) {
2319 free(tev->args[i].name);
2320 free(tev->args[i].value);
2321 free(tev->args[i].type);
2322 ref = tev->args[i].ref;
2323 while (ref) {
2324 next = ref->next;
2325 free(ref);
2326 ref = next;
2329 free(tev->args);
2330 memset(tev, 0, sizeof(*tev));
2333 struct kprobe_blacklist_node {
2334 struct list_head list;
2335 unsigned long start;
2336 unsigned long end;
2337 char *symbol;
2340 static void kprobe_blacklist__delete(struct list_head *blacklist)
2342 struct kprobe_blacklist_node *node;
2344 while (!list_empty(blacklist)) {
2345 node = list_first_entry(blacklist,
2346 struct kprobe_blacklist_node, list);
2347 list_del(&node->list);
2348 free(node->symbol);
2349 free(node);
2353 static int kprobe_blacklist__load(struct list_head *blacklist)
2355 struct kprobe_blacklist_node *node;
2356 const char *__debugfs = debugfs__mountpoint();
2357 char buf[PATH_MAX], *p;
2358 FILE *fp;
2359 int ret;
2361 if (__debugfs == NULL)
2362 return -ENOTSUP;
2364 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2365 if (ret < 0)
2366 return ret;
2368 fp = fopen(buf, "r");
2369 if (!fp)
2370 return -errno;
2372 ret = 0;
2373 while (fgets(buf, PATH_MAX, fp)) {
2374 node = zalloc(sizeof(*node));
2375 if (!node) {
2376 ret = -ENOMEM;
2377 break;
2379 INIT_LIST_HEAD(&node->list);
2380 list_add_tail(&node->list, blacklist);
2381 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2382 ret = -EINVAL;
2383 break;
2385 p = strchr(buf, '\t');
2386 if (p) {
2387 p++;
2388 if (p[strlen(p) - 1] == '\n')
2389 p[strlen(p) - 1] = '\0';
2390 } else
2391 p = (char *)"unknown";
2392 node->symbol = strdup(p);
2393 if (!node->symbol) {
2394 ret = -ENOMEM;
2395 break;
2397 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2398 node->start, node->end, node->symbol);
2399 ret++;
2401 if (ret < 0)
2402 kprobe_blacklist__delete(blacklist);
2403 fclose(fp);
2405 return ret;
2408 static struct kprobe_blacklist_node *
2409 kprobe_blacklist__find_by_address(struct list_head *blacklist,
2410 unsigned long address)
2412 struct kprobe_blacklist_node *node;
2414 list_for_each_entry(node, blacklist, list) {
2415 if (node->start <= address && address < node->end)
2416 return node;
2419 return NULL;
2422 static LIST_HEAD(kprobe_blacklist);
2424 static void kprobe_blacklist__init(void)
2426 if (!list_empty(&kprobe_blacklist))
2427 return;
2429 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2430 pr_debug("No kprobe blacklist support, ignored\n");
2433 static void kprobe_blacklist__release(void)
2435 kprobe_blacklist__delete(&kprobe_blacklist);
2438 static bool kprobe_blacklist__listed(unsigned long address)
2440 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2443 static int perf_probe_event__sprintf(const char *group, const char *event,
2444 struct perf_probe_event *pev,
2445 const char *module,
2446 struct strbuf *result)
2448 int i, ret;
2449 char *buf;
2451 if (asprintf(&buf, "%s:%s", group, event) < 0)
2452 return -errno;
2453 ret = strbuf_addf(result, " %-20s (on ", buf);
2454 free(buf);
2455 if (ret)
2456 return ret;
2458 /* Synthesize only event probe point */
2459 buf = synthesize_perf_probe_point(&pev->point);
2460 if (!buf)
2461 return -ENOMEM;
2462 ret = strbuf_addstr(result, buf);
2463 free(buf);
2465 if (!ret && module)
2466 ret = strbuf_addf(result, " in %s", module);
2468 if (!ret && pev->nargs > 0) {
2469 ret = strbuf_add(result, " with", 5);
2470 for (i = 0; !ret && i < pev->nargs; i++) {
2471 buf = synthesize_perf_probe_arg(&pev->args[i]);
2472 if (!buf)
2473 return -ENOMEM;
2474 ret = strbuf_addf(result, " %s", buf);
2475 free(buf);
2478 if (!ret)
2479 ret = strbuf_addch(result, ')');
2481 return ret;
2484 /* Show an event */
2485 int show_perf_probe_event(const char *group, const char *event,
2486 struct perf_probe_event *pev,
2487 const char *module, bool use_stdout)
2489 struct strbuf buf = STRBUF_INIT;
2490 int ret;
2492 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
2493 if (ret >= 0) {
2494 if (use_stdout)
2495 printf("%s\n", buf.buf);
2496 else
2497 pr_info("%s\n", buf.buf);
2499 strbuf_release(&buf);
2501 return ret;
2504 static bool filter_probe_trace_event(struct probe_trace_event *tev,
2505 struct strfilter *filter)
2507 char tmp[128];
2509 /* At first, check the event name itself */
2510 if (strfilter__compare(filter, tev->event))
2511 return true;
2513 /* Next, check the combination of name and group */
2514 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2515 return false;
2516 return strfilter__compare(filter, tmp);
2519 static int __show_perf_probe_events(int fd, bool is_kprobe,
2520 struct strfilter *filter)
2522 int ret = 0;
2523 struct probe_trace_event tev;
2524 struct perf_probe_event pev;
2525 struct strlist *rawlist;
2526 struct str_node *ent;
2528 memset(&tev, 0, sizeof(tev));
2529 memset(&pev, 0, sizeof(pev));
2531 rawlist = probe_file__get_rawlist(fd);
2532 if (!rawlist)
2533 return -ENOMEM;
2535 strlist__for_each_entry(ent, rawlist) {
2536 ret = parse_probe_trace_command(ent->s, &tev);
2537 if (ret >= 0) {
2538 if (!filter_probe_trace_event(&tev, filter))
2539 goto next;
2540 ret = convert_to_perf_probe_event(&tev, &pev,
2541 is_kprobe);
2542 if (ret < 0)
2543 goto next;
2544 ret = show_perf_probe_event(pev.group, pev.event,
2545 &pev, tev.point.module,
2546 true);
2548 next:
2549 clear_perf_probe_event(&pev);
2550 clear_probe_trace_event(&tev);
2551 if (ret < 0)
2552 break;
2554 strlist__delete(rawlist);
2555 /* Cleanup cached debuginfo if needed */
2556 debuginfo_cache__exit();
2558 return ret;
2561 /* List up current perf-probe events */
2562 int show_perf_probe_events(struct strfilter *filter)
2564 int kp_fd, up_fd, ret;
2566 setup_pager();
2568 if (probe_conf.cache)
2569 return probe_cache__show_all_caches(filter);
2571 ret = init_probe_symbol_maps(false);
2572 if (ret < 0)
2573 return ret;
2575 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2576 if (ret < 0)
2577 return ret;
2579 if (kp_fd >= 0)
2580 ret = __show_perf_probe_events(kp_fd, true, filter);
2581 if (up_fd >= 0 && ret >= 0)
2582 ret = __show_perf_probe_events(up_fd, false, filter);
2583 if (kp_fd > 0)
2584 close(kp_fd);
2585 if (up_fd > 0)
2586 close(up_fd);
2587 exit_probe_symbol_maps();
2589 return ret;
2592 static int get_new_event_name(char *buf, size_t len, const char *base,
2593 struct strlist *namelist, bool ret_event,
2594 bool allow_suffix)
2596 int i, ret;
2597 char *p, *nbase;
2599 if (*base == '.')
2600 base++;
2601 nbase = strdup(base);
2602 if (!nbase)
2603 return -ENOMEM;
2605 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2606 p = strpbrk(nbase, ".@");
2607 if (p && p != nbase)
2608 *p = '\0';
2610 /* Try no suffix number */
2611 ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : "");
2612 if (ret < 0) {
2613 pr_debug("snprintf() failed: %d\n", ret);
2614 goto out;
2616 if (!strlist__has_entry(namelist, buf))
2617 goto out;
2619 if (!allow_suffix) {
2620 pr_warning("Error: event \"%s\" already exists.\n"
2621 " Hint: Remove existing event by 'perf probe -d'\n"
2622 " or force duplicates by 'perf probe -f'\n"
2623 " or set 'force=yes' in BPF source.\n",
2624 buf);
2625 ret = -EEXIST;
2626 goto out;
2629 /* Try to add suffix */
2630 for (i = 1; i < MAX_EVENT_INDEX; i++) {
2631 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
2632 if (ret < 0) {
2633 pr_debug("snprintf() failed: %d\n", ret);
2634 goto out;
2636 if (!strlist__has_entry(namelist, buf))
2637 break;
2639 if (i == MAX_EVENT_INDEX) {
2640 pr_warning("Too many events are on the same function.\n");
2641 ret = -ERANGE;
2644 out:
2645 free(nbase);
2647 /* Final validation */
2648 if (ret >= 0 && !is_c_func_name(buf)) {
2649 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2650 buf);
2651 ret = -EINVAL;
2654 return ret;
2657 /* Warn if the current kernel's uprobe implementation is old */
2658 static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2660 int i;
2661 char *buf = synthesize_probe_trace_command(tev);
2662 struct probe_trace_point *tp = &tev->point;
2664 if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) {
2665 pr_warning("A semaphore is associated with %s:%s and "
2666 "seems your kernel doesn't support it.\n",
2667 tev->group, tev->event);
2670 /* Old uprobe event doesn't support memory dereference */
2671 if (!tev->uprobes || tev->nargs == 0 || !buf)
2672 goto out;
2674 for (i = 0; i < tev->nargs; i++)
2675 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2676 pr_warning("Please upgrade your kernel to at least "
2677 "3.14 to have access to feature %s\n",
2678 tev->args[i].value);
2679 break;
2681 out:
2682 free(buf);
2685 /* Set new name from original perf_probe_event and namelist */
2686 static int probe_trace_event__set_name(struct probe_trace_event *tev,
2687 struct perf_probe_event *pev,
2688 struct strlist *namelist,
2689 bool allow_suffix)
2691 const char *event, *group;
2692 char buf[64];
2693 int ret;
2695 /* If probe_event or trace_event already have the name, reuse it */
2696 if (pev->event && !pev->sdt)
2697 event = pev->event;
2698 else if (tev->event)
2699 event = tev->event;
2700 else {
2701 /* Or generate new one from probe point */
2702 if (pev->point.function &&
2703 (strncmp(pev->point.function, "0x", 2) != 0) &&
2704 !strisglob(pev->point.function))
2705 event = pev->point.function;
2706 else
2707 event = tev->point.realname;
2709 if (pev->group && !pev->sdt)
2710 group = pev->group;
2711 else if (tev->group)
2712 group = tev->group;
2713 else
2714 group = PERFPROBE_GROUP;
2716 /* Get an unused new event name */
2717 ret = get_new_event_name(buf, 64, event, namelist,
2718 tev->point.retprobe, allow_suffix);
2719 if (ret < 0)
2720 return ret;
2722 event = buf;
2724 tev->event = strdup(event);
2725 tev->group = strdup(group);
2726 if (tev->event == NULL || tev->group == NULL)
2727 return -ENOMEM;
2729 /* Add added event name to namelist */
2730 strlist__add(namelist, event);
2731 return 0;
2734 static int __open_probe_file_and_namelist(bool uprobe,
2735 struct strlist **namelist)
2737 int fd;
2739 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
2740 if (fd < 0)
2741 return fd;
2743 /* Get current event names */
2744 *namelist = probe_file__get_namelist(fd);
2745 if (!(*namelist)) {
2746 pr_debug("Failed to get current event list.\n");
2747 close(fd);
2748 return -ENOMEM;
2750 return fd;
2753 static int __add_probe_trace_events(struct perf_probe_event *pev,
2754 struct probe_trace_event *tevs,
2755 int ntevs, bool allow_suffix)
2757 int i, fd[2] = {-1, -1}, up, ret;
2758 struct probe_trace_event *tev = NULL;
2759 struct probe_cache *cache = NULL;
2760 struct strlist *namelist[2] = {NULL, NULL};
2761 struct nscookie nsc;
2763 up = pev->uprobes ? 1 : 0;
2764 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2765 if (fd[up] < 0)
2766 return fd[up];
2768 ret = 0;
2769 for (i = 0; i < ntevs; i++) {
2770 tev = &tevs[i];
2771 up = tev->uprobes ? 1 : 0;
2772 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2773 fd[up] = __open_probe_file_and_namelist(up,
2774 &namelist[up]);
2775 if (fd[up] < 0)
2776 goto close_out;
2778 /* Skip if the symbol is out of .text or blacklisted */
2779 if (!tev->point.symbol && !pev->uprobes)
2780 continue;
2782 /* Set new name for tev (and update namelist) */
2783 ret = probe_trace_event__set_name(tev, pev, namelist[up],
2784 allow_suffix);
2785 if (ret < 0)
2786 break;
2788 nsinfo__mountns_enter(pev->nsi, &nsc);
2789 ret = probe_file__add_event(fd[up], tev);
2790 nsinfo__mountns_exit(&nsc);
2791 if (ret < 0)
2792 break;
2795 * Probes after the first probe which comes from same
2796 * user input are always allowed to add suffix, because
2797 * there might be several addresses corresponding to
2798 * one code line.
2800 allow_suffix = true;
2802 if (ret == -EINVAL && pev->uprobes)
2803 warn_uprobe_event_compat(tev);
2804 if (ret == 0 && probe_conf.cache) {
2805 cache = probe_cache__new(pev->target, pev->nsi);
2806 if (!cache ||
2807 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2808 probe_cache__commit(cache) < 0)
2809 pr_warning("Failed to add event to probe cache\n");
2810 probe_cache__delete(cache);
2813 close_out:
2814 for (up = 0; up < 2; up++) {
2815 strlist__delete(namelist[up]);
2816 if (fd[up] >= 0)
2817 close(fd[up]);
2819 return ret;
2822 static int find_probe_functions(struct map *map, char *name,
2823 struct symbol **syms)
2825 int found = 0;
2826 struct symbol *sym;
2827 struct rb_node *tmp;
2828 const char *norm, *ver;
2829 char *buf = NULL;
2830 bool cut_version = true;
2832 if (map__load(map) < 0)
2833 return 0;
2835 /* If user gives a version, don't cut off the version from symbols */
2836 if (strchr(name, '@'))
2837 cut_version = false;
2839 map__for_each_symbol(map, sym, tmp) {
2840 norm = arch__normalize_symbol_name(sym->name);
2841 if (!norm)
2842 continue;
2844 if (cut_version) {
2845 /* We don't care about default symbol or not */
2846 ver = strchr(norm, '@');
2847 if (ver) {
2848 buf = strndup(norm, ver - norm);
2849 if (!buf)
2850 return -ENOMEM;
2851 norm = buf;
2855 if (strglobmatch(norm, name)) {
2856 found++;
2857 if (syms && found < probe_conf.max_probes)
2858 syms[found - 1] = sym;
2860 if (buf)
2861 zfree(&buf);
2864 return found;
2867 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2868 struct probe_trace_event *tev __maybe_unused,
2869 struct map *map __maybe_unused,
2870 struct symbol *sym __maybe_unused) { }
2873 * Find probe function addresses from map.
2874 * Return an error or the number of found probe_trace_event
2876 static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2877 struct probe_trace_event **tevs)
2879 struct map *map = NULL;
2880 struct ref_reloc_sym *reloc_sym = NULL;
2881 struct symbol *sym;
2882 struct symbol **syms = NULL;
2883 struct probe_trace_event *tev;
2884 struct perf_probe_point *pp = &pev->point;
2885 struct probe_trace_point *tp;
2886 int num_matched_functions;
2887 int ret, i, j, skipped = 0;
2888 char *mod_name;
2890 map = get_target_map(pev->target, pev->nsi, pev->uprobes);
2891 if (!map) {
2892 ret = -EINVAL;
2893 goto out;
2896 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2897 if (!syms) {
2898 ret = -ENOMEM;
2899 goto out;
2903 * Load matched symbols: Since the different local symbols may have
2904 * same name but different addresses, this lists all the symbols.
2906 num_matched_functions = find_probe_functions(map, pp->function, syms);
2907 if (num_matched_functions <= 0) {
2908 pr_err("Failed to find symbol %s in %s\n", pp->function,
2909 pev->target ? : "kernel");
2910 ret = -ENOENT;
2911 goto out;
2912 } else if (num_matched_functions > probe_conf.max_probes) {
2913 pr_err("Too many functions matched in %s\n",
2914 pev->target ? : "kernel");
2915 ret = -E2BIG;
2916 goto out;
2919 /* Note that the symbols in the kmodule are not relocated */
2920 if (!pev->uprobes && !pev->target &&
2921 (!pp->retprobe || kretprobe_offset_is_supported())) {
2922 reloc_sym = kernel_get_ref_reloc_sym();
2923 if (!reloc_sym) {
2924 pr_warning("Relocated base symbol is not found!\n");
2925 ret = -EINVAL;
2926 goto out;
2930 /* Setup result trace-probe-events */
2931 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2932 if (!*tevs) {
2933 ret = -ENOMEM;
2934 goto out;
2937 ret = 0;
2939 for (j = 0; j < num_matched_functions; j++) {
2940 sym = syms[j];
2942 tev = (*tevs) + ret;
2943 tp = &tev->point;
2944 if (ret == num_matched_functions) {
2945 pr_warning("Too many symbols are listed. Skip it.\n");
2946 break;
2948 ret++;
2950 if (pp->offset > sym->end - sym->start) {
2951 pr_warning("Offset %ld is bigger than the size of %s\n",
2952 pp->offset, sym->name);
2953 ret = -ENOENT;
2954 goto err_out;
2956 /* Add one probe point */
2957 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2959 /* Check the kprobe (not in module) is within .text */
2960 if (!pev->uprobes && !pev->target &&
2961 kprobe_warn_out_range(sym->name, tp->address)) {
2962 tp->symbol = NULL; /* Skip it */
2963 skipped++;
2964 } else if (reloc_sym) {
2965 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2966 tp->offset = tp->address - reloc_sym->addr;
2967 } else {
2968 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2969 tp->offset = pp->offset;
2971 tp->realname = strdup_or_goto(sym->name, nomem_out);
2973 tp->retprobe = pp->retprobe;
2974 if (pev->target) {
2975 if (pev->uprobes) {
2976 tev->point.module = strdup_or_goto(pev->target,
2977 nomem_out);
2978 } else {
2979 mod_name = find_module_name(pev->target);
2980 tev->point.module =
2981 strdup(mod_name ? mod_name : pev->target);
2982 free(mod_name);
2983 if (!tev->point.module)
2984 goto nomem_out;
2987 tev->uprobes = pev->uprobes;
2988 tev->nargs = pev->nargs;
2989 if (tev->nargs) {
2990 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2991 tev->nargs);
2992 if (tev->args == NULL)
2993 goto nomem_out;
2995 for (i = 0; i < tev->nargs; i++) {
2996 if (pev->args[i].name)
2997 tev->args[i].name =
2998 strdup_or_goto(pev->args[i].name,
2999 nomem_out);
3001 tev->args[i].value = strdup_or_goto(pev->args[i].var,
3002 nomem_out);
3003 if (pev->args[i].type)
3004 tev->args[i].type =
3005 strdup_or_goto(pev->args[i].type,
3006 nomem_out);
3008 arch__fix_tev_from_maps(pev, tev, map, sym);
3010 if (ret == skipped) {
3011 ret = -ENOENT;
3012 goto err_out;
3015 out:
3016 map__put(map);
3017 free(syms);
3018 return ret;
3020 nomem_out:
3021 ret = -ENOMEM;
3022 err_out:
3023 clear_probe_trace_events(*tevs, num_matched_functions);
3024 zfree(tevs);
3025 goto out;
3028 static int try_to_find_absolute_address(struct perf_probe_event *pev,
3029 struct probe_trace_event **tevs)
3031 struct perf_probe_point *pp = &pev->point;
3032 struct probe_trace_event *tev;
3033 struct probe_trace_point *tp;
3034 int i, err;
3036 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
3037 return -EINVAL;
3038 if (perf_probe_event_need_dwarf(pev))
3039 return -EINVAL;
3042 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3043 * absolute address.
3045 * Only one tev can be generated by this.
3047 *tevs = zalloc(sizeof(*tev));
3048 if (!*tevs)
3049 return -ENOMEM;
3051 tev = *tevs;
3052 tp = &tev->point;
3055 * Don't use tp->offset, use address directly, because
3056 * in synthesize_probe_trace_command() address cannot be
3057 * zero.
3059 tp->address = pev->point.abs_address;
3060 tp->retprobe = pp->retprobe;
3061 tev->uprobes = pev->uprobes;
3063 err = -ENOMEM;
3065 * Give it a '0x' leading symbol name.
3066 * In __add_probe_trace_events, a NULL symbol is interpreted as
3067 * invalid.
3069 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
3070 goto errout;
3072 /* For kprobe, check range */
3073 if ((!tev->uprobes) &&
3074 (kprobe_warn_out_range(tev->point.symbol,
3075 tev->point.address))) {
3076 err = -EACCES;
3077 goto errout;
3080 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
3081 goto errout;
3083 if (pev->target) {
3084 tp->module = strdup(pev->target);
3085 if (!tp->module)
3086 goto errout;
3089 if (tev->group) {
3090 tev->group = strdup(pev->group);
3091 if (!tev->group)
3092 goto errout;
3095 if (pev->event) {
3096 tev->event = strdup(pev->event);
3097 if (!tev->event)
3098 goto errout;
3101 tev->nargs = pev->nargs;
3102 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
3103 if (!tev->args)
3104 goto errout;
3106 for (i = 0; i < tev->nargs; i++)
3107 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3109 return 1;
3111 errout:
3112 clear_probe_trace_events(*tevs, 1);
3113 *tevs = NULL;
3114 return err;
3117 /* Concatinate two arrays */
3118 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3120 void *ret;
3122 ret = malloc(sz_a + sz_b);
3123 if (ret) {
3124 memcpy(ret, a, sz_a);
3125 memcpy(ret + sz_a, b, sz_b);
3127 return ret;
3130 static int
3131 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3132 struct probe_trace_event **tevs2, int ntevs2)
3134 struct probe_trace_event *new_tevs;
3135 int ret = 0;
3137 if (*ntevs == 0) {
3138 *tevs = *tevs2;
3139 *ntevs = ntevs2;
3140 *tevs2 = NULL;
3141 return 0;
3144 if (*ntevs + ntevs2 > probe_conf.max_probes)
3145 ret = -E2BIG;
3146 else {
3147 /* Concatinate the array of probe_trace_event */
3148 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3149 *tevs2, ntevs2 * sizeof(**tevs2));
3150 if (!new_tevs)
3151 ret = -ENOMEM;
3152 else {
3153 free(*tevs);
3154 *tevs = new_tevs;
3155 *ntevs += ntevs2;
3158 if (ret < 0)
3159 clear_probe_trace_events(*tevs2, ntevs2);
3160 zfree(tevs2);
3162 return ret;
3166 * Try to find probe_trace_event from given probe caches. Return the number
3167 * of cached events found, if an error occurs return the error.
3169 static int find_cached_events(struct perf_probe_event *pev,
3170 struct probe_trace_event **tevs,
3171 const char *target)
3173 struct probe_cache *cache;
3174 struct probe_cache_entry *entry;
3175 struct probe_trace_event *tmp_tevs = NULL;
3176 int ntevs = 0;
3177 int ret = 0;
3179 cache = probe_cache__new(target, pev->nsi);
3180 /* Return 0 ("not found") if the target has no probe cache. */
3181 if (!cache)
3182 return 0;
3184 for_each_probe_cache_entry(entry, cache) {
3185 /* Skip the cache entry which has no name */
3186 if (!entry->pev.event || !entry->pev.group)
3187 continue;
3188 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3189 strglobmatch(entry->pev.event, pev->event)) {
3190 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3191 if (ret > 0)
3192 ret = concat_probe_trace_events(tevs, &ntevs,
3193 &tmp_tevs, ret);
3194 if (ret < 0)
3195 break;
3198 probe_cache__delete(cache);
3199 if (ret < 0) {
3200 clear_probe_trace_events(*tevs, ntevs);
3201 zfree(tevs);
3202 } else {
3203 ret = ntevs;
3204 if (ntevs > 0 && target && target[0] == '/')
3205 pev->uprobes = true;
3208 return ret;
3211 /* Try to find probe_trace_event from all probe caches */
3212 static int find_cached_events_all(struct perf_probe_event *pev,
3213 struct probe_trace_event **tevs)
3215 struct probe_trace_event *tmp_tevs = NULL;
3216 struct strlist *bidlist;
3217 struct str_node *nd;
3218 char *pathname;
3219 int ntevs = 0;
3220 int ret;
3222 /* Get the buildid list of all valid caches */
3223 bidlist = build_id_cache__list_all(true);
3224 if (!bidlist) {
3225 ret = -errno;
3226 pr_debug("Failed to get buildids: %d\n", ret);
3227 return ret;
3230 ret = 0;
3231 strlist__for_each_entry(nd, bidlist) {
3232 pathname = build_id_cache__origname(nd->s);
3233 ret = find_cached_events(pev, &tmp_tevs, pathname);
3234 /* In the case of cnt == 0, we just skip it */
3235 if (ret > 0)
3236 ret = concat_probe_trace_events(tevs, &ntevs,
3237 &tmp_tevs, ret);
3238 free(pathname);
3239 if (ret < 0)
3240 break;
3242 strlist__delete(bidlist);
3244 if (ret < 0) {
3245 clear_probe_trace_events(*tevs, ntevs);
3246 zfree(tevs);
3247 } else
3248 ret = ntevs;
3250 return ret;
3253 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3254 struct probe_trace_event **tevs)
3256 struct probe_cache *cache;
3257 struct probe_cache_entry *entry;
3258 struct probe_trace_event *tev;
3259 struct str_node *node;
3260 int ret, i;
3262 if (pev->sdt) {
3263 /* For SDT/cached events, we use special search functions */
3264 if (!pev->target)
3265 return find_cached_events_all(pev, tevs);
3266 else
3267 return find_cached_events(pev, tevs, pev->target);
3269 cache = probe_cache__new(pev->target, pev->nsi);
3270 if (!cache)
3271 return 0;
3273 entry = probe_cache__find(cache, pev);
3274 if (!entry) {
3275 /* SDT must be in the cache */
3276 ret = pev->sdt ? -ENOENT : 0;
3277 goto out;
3280 ret = strlist__nr_entries(entry->tevlist);
3281 if (ret > probe_conf.max_probes) {
3282 pr_debug("Too many entries matched in the cache of %s\n",
3283 pev->target ? : "kernel");
3284 ret = -E2BIG;
3285 goto out;
3288 *tevs = zalloc(ret * sizeof(*tev));
3289 if (!*tevs) {
3290 ret = -ENOMEM;
3291 goto out;
3294 i = 0;
3295 strlist__for_each_entry(node, entry->tevlist) {
3296 tev = &(*tevs)[i++];
3297 ret = parse_probe_trace_command(node->s, tev);
3298 if (ret < 0)
3299 goto out;
3300 /* Set the uprobes attribute as same as original */
3301 tev->uprobes = pev->uprobes;
3303 ret = i;
3305 out:
3306 probe_cache__delete(cache);
3307 return ret;
3310 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
3311 struct probe_trace_event **tevs)
3313 int ret;
3315 if (!pev->group && !pev->sdt) {
3316 /* Set group name if not given */
3317 if (!pev->uprobes) {
3318 pev->group = strdup(PERFPROBE_GROUP);
3319 ret = pev->group ? 0 : -ENOMEM;
3320 } else
3321 ret = convert_exec_to_group(pev->target, &pev->group);
3322 if (ret != 0) {
3323 pr_warning("Failed to make a group name.\n");
3324 return ret;
3328 ret = try_to_find_absolute_address(pev, tevs);
3329 if (ret > 0)
3330 return ret;
3332 /* At first, we need to lookup cache entry */
3333 ret = find_probe_trace_events_from_cache(pev, tevs);
3334 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3335 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
3337 /* Convert perf_probe_event with debuginfo */
3338 ret = try_to_find_probe_trace_events(pev, tevs);
3339 if (ret != 0)
3340 return ret; /* Found in debuginfo or got an error */
3342 return find_probe_trace_events_from_map(pev, tevs);
3345 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3347 int i, ret;
3349 /* Loop 1: convert all events */
3350 for (i = 0; i < npevs; i++) {
3351 /* Init kprobe blacklist if needed */
3352 if (!pevs[i].uprobes)
3353 kprobe_blacklist__init();
3354 /* Convert with or without debuginfo */
3355 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
3356 if (ret < 0)
3357 return ret;
3358 pevs[i].ntevs = ret;
3360 /* This just release blacklist only if allocated */
3361 kprobe_blacklist__release();
3363 return 0;
3366 static int show_probe_trace_event(struct probe_trace_event *tev)
3368 char *buf = synthesize_probe_trace_command(tev);
3370 if (!buf) {
3371 pr_debug("Failed to synthesize probe trace event.\n");
3372 return -EINVAL;
3375 /* Showing definition always go stdout */
3376 printf("%s\n", buf);
3377 free(buf);
3379 return 0;
3382 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3384 struct strlist *namelist = strlist__new(NULL, NULL);
3385 struct probe_trace_event *tev;
3386 struct perf_probe_event *pev;
3387 int i, j, ret = 0;
3389 if (!namelist)
3390 return -ENOMEM;
3392 for (j = 0; j < npevs && !ret; j++) {
3393 pev = &pevs[j];
3394 for (i = 0; i < pev->ntevs && !ret; i++) {
3395 tev = &pev->tevs[i];
3396 /* Skip if the symbol is out of .text or blacklisted */
3397 if (!tev->point.symbol && !pev->uprobes)
3398 continue;
3400 /* Set new name for tev (and update namelist) */
3401 ret = probe_trace_event__set_name(tev, pev,
3402 namelist, true);
3403 if (!ret)
3404 ret = show_probe_trace_event(tev);
3407 strlist__delete(namelist);
3409 return ret;
3412 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3414 int i, ret = 0;
3416 /* Loop 2: add all events */
3417 for (i = 0; i < npevs; i++) {
3418 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3419 pevs[i].ntevs,
3420 probe_conf.force_add);
3421 if (ret < 0)
3422 break;
3424 return ret;
3427 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3429 int i, j;
3430 struct perf_probe_event *pev;
3432 /* Loop 3: cleanup and free trace events */
3433 for (i = 0; i < npevs; i++) {
3434 pev = &pevs[i];
3435 for (j = 0; j < pevs[i].ntevs; j++)
3436 clear_probe_trace_event(&pevs[i].tevs[j]);
3437 zfree(&pevs[i].tevs);
3438 pevs[i].ntevs = 0;
3439 nsinfo__zput(pev->nsi);
3440 clear_perf_probe_event(&pevs[i]);
3444 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3446 int ret;
3448 ret = init_probe_symbol_maps(pevs->uprobes);
3449 if (ret < 0)
3450 return ret;
3452 ret = convert_perf_probe_events(pevs, npevs);
3453 if (ret == 0)
3454 ret = apply_perf_probe_events(pevs, npevs);
3456 cleanup_perf_probe_events(pevs, npevs);
3458 exit_probe_symbol_maps();
3459 return ret;
3462 int del_perf_probe_events(struct strfilter *filter)
3464 int ret, ret2, ufd = -1, kfd = -1;
3465 char *str = strfilter__string(filter);
3467 if (!str)
3468 return -EINVAL;
3470 /* Get current event names */
3471 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3472 if (ret < 0)
3473 goto out;
3475 ret = probe_file__del_events(kfd, filter);
3476 if (ret < 0 && ret != -ENOENT)
3477 goto error;
3479 ret2 = probe_file__del_events(ufd, filter);
3480 if (ret2 < 0 && ret2 != -ENOENT) {
3481 ret = ret2;
3482 goto error;
3484 ret = 0;
3486 error:
3487 if (kfd >= 0)
3488 close(kfd);
3489 if (ufd >= 0)
3490 close(ufd);
3491 out:
3492 free(str);
3494 return ret;
3497 int show_available_funcs(const char *target, struct nsinfo *nsi,
3498 struct strfilter *_filter, bool user)
3500 struct rb_node *nd;
3501 struct map *map;
3502 int ret;
3504 ret = init_probe_symbol_maps(user);
3505 if (ret < 0)
3506 return ret;
3508 /* Get a symbol map */
3509 map = get_target_map(target, nsi, user);
3510 if (!map) {
3511 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
3512 return -EINVAL;
3515 ret = map__load(map);
3516 if (ret) {
3517 if (ret == -2) {
3518 char *str = strfilter__string(_filter);
3519 pr_err("Failed to find symbols matched to \"%s\"\n",
3520 str);
3521 free(str);
3522 } else
3523 pr_err("Failed to load symbols in %s\n",
3524 (target) ? : "kernel");
3525 goto end;
3527 if (!dso__sorted_by_name(map->dso))
3528 dso__sort_by_name(map->dso);
3530 /* Show all (filtered) symbols */
3531 setup_pager();
3533 for (nd = rb_first(&map->dso->symbol_names); nd; nd = rb_next(nd)) {
3534 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3536 if (strfilter__compare(_filter, pos->sym.name))
3537 printf("%s\n", pos->sym.name);
3539 end:
3540 map__put(map);
3541 exit_probe_symbol_maps();
3543 return ret;
3546 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3547 struct perf_probe_arg *pvar)
3549 tvar->value = strdup(pvar->var);
3550 if (tvar->value == NULL)
3551 return -ENOMEM;
3552 if (pvar->type) {
3553 tvar->type = strdup(pvar->type);
3554 if (tvar->type == NULL)
3555 return -ENOMEM;
3557 if (pvar->name) {
3558 tvar->name = strdup(pvar->name);
3559 if (tvar->name == NULL)
3560 return -ENOMEM;
3561 } else
3562 tvar->name = NULL;
3563 return 0;