[PATCH 1/2] libbpf: show more info about missing ".BTF" section

Changbin Du
2022-11-26T11:14:15Z

Show more information about why failed instead of just saying "No such file or
directory".

Now will print below info:
libbpf: can not find '.BTF' section
libbpf: is CONFIG_DEBUG_INFO_BTF enabled for kernel?
Error: failed to load BTF from /home/changbin/work/linux/vmlinux: No such file or directory

Signed-off-by: Changbin Du <changbin.du@gmail.com>
---
tools/lib/bpf/btf.c | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index d88647da2c7f..3f661d991808 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -906,6 +906,15 @@ struct btf *btf__new(const void *data, __u32 size)
return libbpf_ptr(btf_new(data, size, NULL));
}

+static bool is_vmlinux(const char *path)
+{
+ size_t path_len = strlen(path);
+ size_t suffix_len = strlen("vmlinux");
+
+ return (path_len >= suffix_len) &&
+ (!memcmp(path + path_len - suffix_len, "vmlinux", suffix_len));
+}
+
static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
struct btf_ext **btf_ext)
{
@@ -990,6 +999,9 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
err = 0;

if (!btf_data) {
+ pr_warn("can not find '%s' section\n", BTF_ELF_SEC);
+ if (is_vmlinux(path))
+ pr_warn("is CONFIG_DEBUG_INFO_BTF enabled for kernel?\n");
err = -ENOENT;
goto done;
}
--
2.37.2