On Mon, Jan 31, 2022 at 8:13 AM Alan Maguire <alan.maguire@xxxxxxxxxx> wrote: > > get_lib_path(path_substr) returns full path to a library > containing path_substr (such as "libc-") found via > /proc/self/maps. Caller is responsible for freeing > the returned string. > > Signed-off-by: Alan Maguire <alan.maguire@xxxxxxxxxx> > --- > tools/testing/selftests/bpf/trace_helpers.c | 17 +++++++++++++++++ > tools/testing/selftests/bpf/trace_helpers.h | 2 ++ > 2 files changed, 19 insertions(+) > > diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c > index ca6abae..49e5f0d 100644 > --- a/tools/testing/selftests/bpf/trace_helpers.c > +++ b/tools/testing/selftests/bpf/trace_helpers.c > @@ -216,3 +216,20 @@ ssize_t get_rel_offset(uintptr_t addr) > fclose(f); > return -EINVAL; > } > + > +char *get_lib_path(const char *path_substr) > +{ > + char *found = NULL; > + char lib_path[512]; > + FILE *f; > + > + f = fopen("/proc/self/maps", "r"); > + while (fscanf(f, "%*s %*s %*s %*s %*s %[^\n]", lib_path) == 1) { I think it can be followed by " (deleted)", right? Do we want to detect that and do something about it? > + if (strstr(lib_path, path_substr) == NULL) > + continue; > + found = strdup(lib_path); > + break; > + } > + fclose(f); > + return found; > +} > diff --git a/tools/testing/selftests/bpf/trace_helpers.h b/tools/testing/selftests/bpf/trace_helpers.h > index 238a9c9..ff379f6 100644 > --- a/tools/testing/selftests/bpf/trace_helpers.h > +++ b/tools/testing/selftests/bpf/trace_helpers.h > @@ -20,5 +20,7 @@ struct ksym { > > ssize_t get_uprobe_offset(const void *addr); > ssize_t get_rel_offset(uintptr_t addr); > +/* Return allocated string path to library that contains path_substr. */ > +char *get_lib_path(const char *path_substr); > > #endif > -- > 1.8.3.1 >