perf probe: Show correct source lines of probes on kmodules

Perf probe always failed to find appropriate line numbers because of
failing to find .text start address offset from debuginfo.

e.g.
  ----
  # ./perf probe -m pcspkr pcspkr_event:5
  Added new events:
    probe:pcspkr_event   (on pcspkr_event:5 in pcspkr)
    probe:pcspkr_event_1 (on pcspkr_event:5 in pcspkr)

  You can now use it in all perf tools, such as:

          perf record -e probe:pcspkr_event_1 -aR sleep 1

  # ./perf probe -l
  Failed to find debug information for address ffffffffa031f006
  Failed to find debug information for address ffffffffa031f016
    probe:pcspkr_event   (on pcspkr_event+6 in pcspkr)
    probe:pcspkr_event_1 (on pcspkr_event+22 in pcspkr)
  ----

This fixes the above issue as below.
1. Get the relative address of the symbol in .text by using
   map->start.
2. Adjust the address by adding the offset of .text section
   in the kernel module binary.

With this fix, perf probe -l shows lines correctly.
  ----
  # ./perf probe -l
    probe:pcspkr_event   (on pcspkr_event:5@drivers/input/misc/pcspkr.c in pcspkr)
    probe:pcspkr_event_1 (on pcspkr_event:5@drivers/input/misc/pcspkr.c in pcspkr)
  ----

Reported-by: Arnaldo Carvalho de Melo <acme@kernel.org>
Signed-off-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/r/20150930164132.3733.24643.stgit@localhost.localdomain
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Masami Hiramatsu
2015-10-01 01:41:33 +09:00
committed by Arnaldo Carvalho de Melo
parent 9135949ddd
commit 9b239a12bc
2 changed files with 65 additions and 12 deletions

View File

@@ -1402,6 +1402,41 @@ int debuginfo__find_available_vars_at(struct debuginfo *dbg,
return (ret < 0) ? ret : af.nvls;
}
/* For the kernel module, we need a special code to get a DIE */
static int debuginfo__get_text_offset(struct debuginfo *dbg, Dwarf_Addr *offs)
{
int n, i;
Elf32_Word shndx;
Elf_Scn *scn;
Elf *elf;
GElf_Shdr mem, *shdr;
const char *p;
elf = dwfl_module_getelf(dbg->mod, &dbg->bias);
if (!elf)
return -EINVAL;
/* Get the number of relocations */
n = dwfl_module_relocations(dbg->mod);
if (n < 0)
return -ENOENT;
/* Search the relocation related .text section */
for (i = 0; i < n; i++) {
p = dwfl_module_relocation_info(dbg->mod, i, &shndx);
if (strcmp(p, ".text") == 0) {
/* OK, get the section header */
scn = elf_getscn(elf, shndx);
if (!scn)
return -ENOENT;
shdr = gelf_getshdr(scn, &mem);
if (!shdr)
return -ENOENT;
*offs = shdr->sh_addr;
}
}
return 0;
}
/* Reverse search */
int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr,
struct perf_probe_point *ppt)
@@ -1410,9 +1445,16 @@ int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr,
Dwarf_Addr _addr = 0, baseaddr = 0;
const char *fname = NULL, *func = NULL, *basefunc = NULL, *tmp;
int baseline = 0, lineno = 0, ret = 0;
bool reloc = false;
retry:
/* Find cu die */
if (!dwarf_addrdie(dbg->dbg, (Dwarf_Addr)addr, &cudie)) {
if (!reloc && debuginfo__get_text_offset(dbg, &baseaddr) == 0) {
addr += baseaddr;
reloc = true;
goto retry;
}
pr_warning("Failed to find debug information for address %lx\n",
addr);
ret = -EINVAL;