Skip to content

Commit

Permalink
core: Add method to get the vmlinux BTF filename, allow overriding it…
Browse files Browse the repository at this point in the history
… via env var

In systems where BTF isn't available there were reports that the
simplest pahole call, without any args, segfaults.

To have a proper test before fixing this problem, allow overriding the
/sys/kernel/btf/vmlinux filename, so that even in systems with BTF we an
point it to a invalid location, making pahole think that there is no BTF
available and thus fallback to something that currently segfaults.

Using it:

  $ pahole list_head
  struct list_head {
  	struct list_head *         next;                 /*     0     8 */
  	struct list_head *         prev;                 /*     8     8 */

  	/* size: 16, cachelines: 1, members: 2 */
  	/* last cacheline: 16 bytes */
  };
  $
  $ PAHOLE_VMLINUX_BTF_FILENAME=foobar pahole list_head
  Segmentation fault (core dumped)
  $

Reported-by: Matthias Schwarzott <[email protected]>
Reviewed-by: Alan Maguire <[email protected]>
Tested-by: Alan Maguire <[email protected]>
Tested-by: Eduard Zingerman <[email protected]>
Cc: Andrii Nakryiko <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Song Liu <[email protected]>
Cc: Yonghong Song <[email protected]>
Link: https://lore.kernel.org/all/ZzzPUhCaew6rHwL_@x1
Link: https://lore.kernel.org/all/ZzzZpqA8ZH-6-Aj9@x1
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
  • Loading branch information
acmel committed Nov 19, 2024
1 parent 65b7fd6 commit ce69ca1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
17 changes: 15 additions & 2 deletions dwarves.c
Original file line number Diff line number Diff line change
Expand Up @@ -2807,6 +2807,19 @@ static int filename__sprintf_build_id(const char *pathname, char *sbuild_id)
static int vmlinux_path__nr_entries;
static char **vmlinux_path;

const char *vmlinux_path__btf_filename(void)
{
static const char *vmlinux_btf;

if (vmlinux_btf == NULL) {
vmlinux_btf = getenv("PAHOLE_VMLINUX_BTF_FILENAME");
if (vmlinux_btf == NULL)
vmlinux_btf = "/sys/kernel/btf/vmlinux";
}

return vmlinux_btf;
}

static void vmlinux_path__exit(void)
{
while (--vmlinux_path__nr_entries >= 0)
Expand Down Expand Up @@ -2933,15 +2946,15 @@ static int cus__load_running_kernel(struct cus *cus, struct conf_load *conf)
int err = 0;

if ((!conf || conf->format_path == NULL || strncmp(conf->format_path, "btf", 3) == 0) &&
access("/sys/kernel/btf/vmlinux", R_OK) == 0) {
access(vmlinux_path__btf_filename(), R_OK) == 0) {
int loader = debugging_formats__loader("btf");
if (loader == -1)
goto try_elf;

if (conf && conf->conf_fprintf)
conf->conf_fprintf->has_alignment_info = debug_fmt_table[loader]->has_alignment_info;

if (debug_fmt_table[loader]->load_file(cus, conf, "/sys/kernel/btf/vmlinux") == 0)
if (debug_fmt_table[loader]->load_file(cus, conf, vmlinux_path__btf_filename()) == 0)
return 0;
}
try_elf:
Expand Down
2 changes: 2 additions & 0 deletions dwarves.h
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,8 @@ void dwarves__resolve_cacheline_size(const struct conf_load *conf, uint16_t user

const char *dwarf_tag_name(const uint32_t tag);

const char *vmlinux_path__btf_filename(void);

const char *vmlinux_path__find_running_kernel(void);

struct argp_state;
Expand Down
4 changes: 4 additions & 0 deletions man-pages/pahole.1
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ struct list_head {
};
$
.fi
It is possible to override the /sys/kernel/btf/vmlinux file location by setting
the PAHOLE_VMLINUX_BTF_FILENAME environment variable. This may be useful for
testing, scripting when using a different BTF for vmlinux. Used in the pahole
regression tests.

If BTF is not present and no file is passed, then a vmlinux that matches the
build-id for the running kernel will be looked up in the usual places,
Expand Down
2 changes: 1 addition & 1 deletion pahole.c
Original file line number Diff line number Diff line change
Expand Up @@ -3754,7 +3754,7 @@ int main(int argc, char *argv[])
if (filename &&
strstarts(filename, "/sys/kernel/btf/") &&
strstr(filename, "/vmlinux") == NULL) {
base_btf_file = "/sys/kernel/btf/vmlinux";
base_btf_file = vmlinux_path__btf_filename();
conf_load.base_btf = btf__parse(base_btf_file, NULL);
if (libbpf_get_error(conf_load.base_btf)) {
fprintf(stderr, "Failed to parse base BTF '%s': %ld\n",
Expand Down

0 comments on commit ce69ca1

Please sign in to comment.