libbpf: Add btf__load_vmlinux_btf/btf__load_module_btf
authorHengqi Chen <hengqi.chen@gmail.com>
Fri, 30 Jul 2021 11:40:12 +0000 (19:40 +0800)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 30 Jul 2021 19:21:29 +0000 (12:21 -0700)
Add two new APIs: btf__load_vmlinux_btf and btf__load_module_btf.
btf__load_vmlinux_btf is just an alias to the existing API named
libbpf_find_kernel_btf, rename to be more precisely and consistent
with existing BTF APIs. btf__load_module_btf can be used to load
module BTF, add it for completeness. These two APIs are useful for
implementing tracing tools and introspection tools. This is part
of the effort towards libbpf 1.0 ([0]).

  [0] Closes: https://github.com/libbpf/libbpf/issues/280

Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210730114012.494408-1-hengqi.chen@gmail.com
tools/lib/bpf/btf.c
tools/lib/bpf/btf.h
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf.map

index cafa4f6bd9b12777d2a700136f81223a94abdb69..85de4fd50699e0c42f78e21bae3b117b0fbb3f12 100644 (file)
@@ -4036,7 +4036,7 @@ static void btf_dedup_merge_hypot_map(struct btf_dedup *d)
                 */
                if (d->hypot_adjust_canon)
                        continue;
-               
+
                if (t_kind == BTF_KIND_FWD && c_kind != BTF_KIND_FWD)
                        d->map[t_id] = c_id;
 
@@ -4409,7 +4409,7 @@ static int btf_dedup_remap_types(struct btf_dedup *d)
  * Probe few well-known locations for vmlinux kernel image and try to load BTF
  * data out of it to use for target BTF.
  */
-struct btf *libbpf_find_kernel_btf(void)
+struct btf *btf__load_vmlinux_btf(void)
 {
        struct {
                const char *path_fmt;
@@ -4455,6 +4455,16 @@ struct btf *libbpf_find_kernel_btf(void)
        return libbpf_err_ptr(-ESRCH);
 }
 
+struct btf *libbpf_find_kernel_btf(void) __attribute__((alias("btf__load_vmlinux_btf")));
+
+struct btf *btf__load_module_btf(const char *module_name, struct btf *vmlinux_btf)
+{
+       char path[80];
+
+       snprintf(path, sizeof(path), "/sys/kernel/btf/%s", module_name);
+       return btf__parse_split(path, vmlinux_btf);
+}
+
 int btf_type_visit_type_ids(struct btf_type *t, type_id_visit_fn visit, void *ctx)
 {
        int i, n, err;
index 596a42c8f4f55f28d658e10fc5da080bb9b1f7db..4a711f990904b40cadc4cdf1f2bd5656c9e894f8 100644 (file)
@@ -44,6 +44,10 @@ LIBBPF_API struct btf *btf__parse_elf_split(const char *path, struct btf *base_b
 LIBBPF_API struct btf *btf__parse_raw(const char *path);
 LIBBPF_API struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf);
 
+LIBBPF_API struct btf *btf__load_vmlinux_btf(void);
+LIBBPF_API struct btf *btf__load_module_btf(const char *module_name, struct btf *vmlinux_btf);
+LIBBPF_API struct btf *libbpf_find_kernel_btf(void);
+
 LIBBPF_API struct btf *btf__load_from_kernel_by_id(__u32 id);
 LIBBPF_API struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf);
 LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf);
@@ -93,8 +97,6 @@ int btf_ext__reloc_line_info(const struct btf *btf,
 LIBBPF_API __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext);
 LIBBPF_API __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext);
 
-LIBBPF_API struct btf *libbpf_find_kernel_btf(void);
-
 LIBBPF_API int btf__find_str(struct btf *btf, const char *s);
 LIBBPF_API int btf__add_str(struct btf *btf, const char *s);
 LIBBPF_API int btf__add_type(struct btf *btf, const struct btf *src_btf,
index 313883179919f7a5d1ca3ffe8799d8f7cbc6a54a..cb106e8c42cbd01804ccbb583e78c65c9773fe51 100644 (file)
@@ -2680,7 +2680,7 @@ static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force)
        if (!force && !obj_needs_vmlinux_btf(obj))
                return 0;
 
-       obj->btf_vmlinux = libbpf_find_kernel_btf();
+       obj->btf_vmlinux = btf__load_vmlinux_btf();
        err = libbpf_get_error(obj->btf_vmlinux);
        if (err) {
                pr_warn("Error loading vmlinux BTF: %d\n", err);
@@ -8297,7 +8297,7 @@ int libbpf_find_vmlinux_btf_id(const char *name,
        struct btf *btf;
        int err;
 
-       btf = libbpf_find_kernel_btf();
+       btf = btf__load_vmlinux_btf();
        err = libbpf_get_error(btf);
        if (err) {
                pr_warn("vmlinux BTF is not found\n");
index 5aca3686ca5ee4be4221ae872ef097f57ceee550..58e0fb2c482f9eaf6c705f1f923d92aeed8fa021 100644 (file)
@@ -378,6 +378,8 @@ LIBBPF_0.5.0 {
                btf__load_from_kernel_by_id;
                btf__load_from_kernel_by_id_split;
                btf__load_into_kernel;
+               btf__load_module_btf;
+               btf__load_vmlinux_btf;
                btf_dump__dump_type_data;
                libbpf_set_strict_mode;
 } LIBBPF_0.4.0;