Skip to content

Commit cd9d054

Browse files
walking-machineKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
bpftool: Use sysfs vmlinux when dumping BTF by ID
Currently, dumping almost all BTFs specified by id requires using the -B option to pass the base BTF. For most cases the vmlinux BTF sysfs path should work. This patch simplifies dumping by ID usage by attempting to use vmlinux BTF from sysfs, if the first try of loading BTF by ID fails with certain conditions. Signed-off-by: Larysa Zaremba <[email protected]> Reviewed-by: Alexander Lobakin <[email protected]>
1 parent c42fe40 commit cd9d054

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

tools/bpf/bpftool/btf.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,22 @@ static int dump_btf_c(const struct btf *btf,
459459
return err;
460460
}
461461

462+
static const char sysfs_vmlinux[] = "/sys/kernel/btf/vmlinux";
463+
464+
static struct btf *get_vmlinux_btf_from_sysfs(void)
465+
{
466+
struct btf *base;
467+
468+
base = btf__parse(sysfs_vmlinux, NULL);
469+
if (libbpf_get_error(base)) {
470+
p_err("failed to parse vmlinux BTF at '%s': %ld\n",
471+
sysfs_vmlinux, libbpf_get_error(base));
472+
base = NULL;
473+
}
474+
475+
return base;
476+
}
477+
462478
static int do_dump(int argc, char **argv)
463479
{
464480
struct btf *btf = NULL, *base = NULL;
@@ -536,18 +552,11 @@ static int do_dump(int argc, char **argv)
536552
NEXT_ARG();
537553
} else if (is_prefix(src, "file")) {
538554
const char sysfs_prefix[] = "/sys/kernel/btf/";
539-
const char sysfs_vmlinux[] = "/sys/kernel/btf/vmlinux";
540555

541556
if (!base_btf &&
542557
strncmp(*argv, sysfs_prefix, sizeof(sysfs_prefix) - 1) == 0 &&
543-
strcmp(*argv, sysfs_vmlinux) != 0) {
544-
base = btf__parse(sysfs_vmlinux, NULL);
545-
if (libbpf_get_error(base)) {
546-
p_err("failed to parse vmlinux BTF at '%s': %ld\n",
547-
sysfs_vmlinux, libbpf_get_error(base));
548-
base = NULL;
549-
}
550-
}
558+
strcmp(*argv, sysfs_vmlinux))
559+
base = get_vmlinux_btf_from_sysfs();
551560

552561
btf = btf__parse_split(*argv, base ?: base_btf);
553562
err = libbpf_get_error(btf);
@@ -593,6 +602,14 @@ static int do_dump(int argc, char **argv)
593602
if (!btf) {
594603
btf = btf__load_from_kernel_by_id_split(btf_id, base_btf);
595604
err = libbpf_get_error(btf);
605+
if (err == -EINVAL && !base_btf) {
606+
btf__free(base);
607+
base = get_vmlinux_btf_from_sysfs();
608+
p_info("Warning: valid base BTF was not specified with -B option, falling back on standard base BTF (sysfs vmlinux)");
609+
btf = btf__load_from_kernel_by_id_split(btf_id, base);
610+
err = libbpf_get_error(btf);
611+
}
612+
596613
if (err) {
597614
p_err("get btf by id (%u): %s", btf_id, strerror(err));
598615
goto done;

0 commit comments

Comments
 (0)