Skip to content

Commit

Permalink
libbpf-tools/trace_helpers: Fix incorrect DSO information in stacktrace
Browse files Browse the repository at this point in the history
offcputime may display inaccurate DSO information in the stacktrace.
Here's an example of the issue:

It shows the same DSO offset for different addresses, which is incorrect.
  $ ./offcputime -v
    ..
    iovisor#14 0x00007f8b912a8c (/usr/lib/libcbe.so_0x22afa8c)
    iovisor#15 0x000044000a3ee0 (/usr/lib/libcbe.so_0x22afa8c)
    iovisor#16 0x000044001fc56c (/usr/lib/libcbe.so_0x22afa8c)

This is why syms__map_addr_dso simply returns NULL when syms__find_dso also
returns NULL. In that case, the values of dso_name and dso_offset are not
changed. If the dso_name and dso_offset variables have a garbage value before
calling syms__map_addr_dso, those garbage values are maintained after calling
syms__map_addr_dso.

To ensure consistent usage of DSO info and symbol info, the prototype of
syms__map_addr_dso has been modified to be similar to dladdr[1].

This is the prototype of dladdr:
  int dladdr(void *addr, Dl_info *info);

The information is returned in a Dl_info structure. If no symbol matching addr
could be found, then dli_sname and dli_saddr are set to NULL.
  typedef struct {
      const char *dli_fname;  /* Pathname of shared object that
                                contains address */
      void       *dli_fbase;  /* Base address at which shared
                                object is loaded */
      const char *dli_sname;  /* Name of symbol whose definition
                                overlaps addr */
      void       *dli_saddr;  /* Exact address of symbol named
                                in dli_sname */
   } Dl_info;

Similarly, if no symbol matching the addr could be found, then sym_name and
sym_offset are set to NULL in syms__map_addr_dso of this patch.

Also, apply the modified API usage to offcputime, futexctn, and memleak.

[1] https://man7.org/linux/man-pages/man3/dladdr.3.html
  • Loading branch information
ekyooo authored and chenhengqi committed May 2, 2024
1 parent 74fe720 commit fef9003
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 36 deletions.
17 changes: 7 additions & 10 deletions libbpf-tools/futexctn.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ static int print_stack(struct futexctn_bpf *obj, struct hist_key *info)
#else
const struct syms *syms;
const struct sym *sym;
char *dso_name;
unsigned long dso_offset;
struct sym_info sinfo;
int idx = 0;
#endif
int i, err = 0, fd;
Expand Down Expand Up @@ -245,14 +244,12 @@ static int print_stack(struct futexctn_bpf *obj, struct hist_key *info)
else
printf(" [unknown]\n");
} else {
dso_name = NULL;
dso_offset = 0;
sym = syms__map_addr_dso(syms, ip[i], &dso_name, &dso_offset);
printf(" #%-2d 0x%016lx", idx++, ip[i]);
if (sym)
printf(" %s+0x%lx", sym->name, sym->offset);
if (dso_name)
printf(" (%s+0x%lx)", dso_name, dso_offset);
err = syms__map_addr_dso(syms, ip[i], &sinfo);
if (err == 0) {
if (sinfo.sym_name)
printf(" %s+0x%lx", sinfo.sym_name, sinfo.sym_offset);
printf(" (%s+0x%lx)", sinfo.dso_name, sinfo.dso_offset);
}
printf("\n");
}
}
Expand Down
15 changes: 7 additions & 8 deletions libbpf-tools/memleak.c
Original file line number Diff line number Diff line change
Expand Up @@ -766,14 +766,13 @@ void print_stack_frames_by_syms_cache()
if (addr == 0)
break;

char *dso_name;
uint64_t dso_offset;
const struct sym *sym = syms__map_addr_dso(syms, addr, &dso_name, &dso_offset);
if (sym) {
printf("\t%zu [<%016lx>] %s+0x%lx", i, addr, sym->name, sym->offset);
if (dso_name)
printf(" [%s]", dso_name);
printf("\n");
struct sym_info sinfo;
int ret = syms__map_addr_dso(syms, addr, &sinfo);
if (ret == 0) {
printf("\t%zu [<%016lx>]", i, addr);
if (sinfo.sym_name)
printf(" %s+0x%lx", sinfo.sym_name, sinfo.sym_offset);
printf(" [%s]\n", sinfo.dso_name);
} else {
printf("\t%zu [<%016lx>] <%s>\n", i, addr, "null sym");
}
Expand Down
16 changes: 7 additions & 9 deletions libbpf-tools/offcputime.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ static void print_map(struct ksyms *ksyms, struct syms_cache *syms_cache,
int err, i, ifd, sfd;
unsigned long *ip;
struct val_t val;
char *dso_name;
unsigned long dso_offset;
struct sym_info sinfo;
int idx;

ip = calloc(env.perf_max_stack_depth, sizeof(*ip));
Expand Down Expand Up @@ -262,14 +261,13 @@ static void print_map(struct ksyms *ksyms, struct syms_cache *syms_cache,
else
printf(" [unknown]\n");
} else {
dso_name = NULL;
dso_offset = 0;
sym = syms__map_addr_dso(syms, ip[i], &dso_name, &dso_offset);
printf(" #%-2d 0x%016lx", idx++, ip[i]);
if (sym)
printf(" %s+0x%lx", sym->name, sym->offset);
if (dso_name)
printf(" (%s+0x%lx)", dso_name, dso_offset);
err = syms__map_addr_dso(syms, ip[i], &sinfo);
if (err == 0) {
if (sinfo.sym_name)
printf(" %s+0x%lx", sinfo.sym_name, sinfo.sym_offset);
printf(" (%s+0x%lx)", sinfo.dso_name, sinfo.dso_offset);
}
printf("\n");
}
}
Expand Down
21 changes: 15 additions & 6 deletions libbpf-tools/trace_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,20 +738,29 @@ const struct sym *syms__map_addr(const struct syms *syms, unsigned long addr)
return dso__find_sym(dso, offset);
}

const struct sym *syms__map_addr_dso(const struct syms *syms, unsigned long addr,
char **dso_name, unsigned long *dso_offset)
int syms__map_addr_dso(const struct syms *syms, unsigned long addr,
struct sym_info *sinfo)
{
struct dso *dso;
struct sym *sym;
uint64_t offset;

memset(sinfo, 0x0, sizeof(struct sym_info));

dso = syms__find_dso(syms, addr, &offset);
if (!dso)
return NULL;
return -1;

*dso_name = dso->name;
*dso_offset = offset;
sinfo->dso_name = dso->name;
sinfo->dso_offset = offset;

return dso__find_sym(dso, offset);
sym = dso__find_sym(dso, offset);
if (sym) {
sinfo->sym_name = sym->name;
sinfo->sym_offset = sym->offset;
}

return 0;
}

struct syms_cache {
Expand Down
11 changes: 9 additions & 2 deletions libbpf-tools/trace_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ struct sym {
unsigned long offset;
};

struct sym_info {
const char *dso_name;
unsigned long dso_offset;
const char *sym_name;
unsigned long sym_offset;
};

struct syms;

struct syms *syms__load_pid(int tgid);
struct syms *syms__load_file(const char *fname);
void syms__free(struct syms *syms);
const struct sym *syms__map_addr(const struct syms *syms, unsigned long addr);
const struct sym *syms__map_addr_dso(const struct syms *syms, unsigned long addr,
char **dso_name, unsigned long *dso_offset);
int syms__map_addr_dso(const struct syms *syms, unsigned long addr,
struct sym_info *sinfo);

struct syms_cache;

Expand Down
2 changes: 1 addition & 1 deletion src/cc/libbpf
Submodule libbpf updated 57 files
+1 −1 .gitattributes
+0 −3 .github/PULL_REQUEST_TEMPLATE.md
+0 −1 .github/actions/build-selftests/build_selftests.sh
+86,130 −83,255 .github/actions/build-selftests/vmlinux.h
+0 −18 .github/actions/vmtest/action.yml
+2 −6 .readthedocs.yaml
+1 −1 BPF-CHECKPOINT-COMMIT
+1 −1 CHECKPOINT-COMMIT
+1 −1 README.md
+70 −0 ci/diffs/0001-s390-define-RUNTIME_DISCARD_EXIT-to-fix-link-error-w.patch
+46 −0 ci/diffs/0001-selftests-bpf-Select-CONFIG_FUNCTION_ERROR_INJECTION.patch
+4 −0 ci/vmtest/configs/ALLOWLIST-5.5.0
+115 −2 ci/vmtest/configs/DENYLIST-5.5.0
+0 −11 ci/vmtest/configs/DENYLIST-latest
+0 −1 ci/vmtest/configs/DENYLIST-latest.s390x
+3 −10 ci/vmtest/run_selftests.sh
+0 −1 docs/conf.py
+0 −10 docs/program_types.rst
+1 −2 docs/sphinx/requirements.txt
+0 −7 include/linux/filter.h
+0 −2 include/linux/kernel.h
+44 −324 include/uapi/linux/bpf.h
+0 −5 include/uapi/linux/fcntl.h
+0 −141 include/uapi/linux/if_link.h
+1 −59 include/uapi/linux/if_xdp.h
+1 −95 include/uapi/linux/netdev.h
+1 −15 include/uapi/linux/perf_event.h
+47 −0 include/uapi/linux/pkt_cls.h
+109 −0 include/uapi/linux/pkt_sched.h
+3 −4 scripts/build-fuzzers.sh
+3 −6 src/Makefile
+48 −173 src/bpf.c
+20 −155 src/bpf.h
+1 −33 src/bpf_core_read.h
+12 −30 src/bpf_helper_defs.h
+5 −14 src/bpf_helpers.h
+4 −3 src/bpf_tracing.h
+3 −169 src/btf.c
+3 −19 src/btf_dump.c
+0 −558 src/elf.c
+0 −503 src/features.c
+7 −7 src/gen_loader.c
+10 −0 src/hashmap.h
+768 −1,468 src/libbpf.c
+4 −208 src/libbpf.h
+0 −22 src/libbpf.map
+0 −19 src/libbpf_common.h
+4 −94 src/libbpf_internal.h
+4 −13 src/libbpf_probes.c
+1 −1 src/libbpf_version.h
+4 −23 src/linker.c
+0 −5 src/netlink.c
+1 −1 src/relo_core.c
+13 −72 src/ringbuf.c
+0 −3 src/str_error.h
+2 −2 src/usdt.bpf.h
+41 −85 src/usdt.c

0 comments on commit fef9003

Please sign in to comment.