libbpf: add btf__load_vmlinux_btf/btf__load_module_btf
PATCH v3 1/1 libbpf: add btf__load_vmlinux_btf/btf__load_module_btf
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. [1]
[1] https://github.com/libbpf/libbpf/issues/280
--- tools/lib/bpf/btf.c | 15 ++++++++++++++- tools/lib/bpf/btf.h | 6 ++++-- tools/lib/bpf/libbpf.c | 4 ++-- tools/lib/bpf/libbpf.map | 2 ++ 4 files changed, 22 insertions(+), 5 deletions(-)
tools/lib/bpf/btf.c +14 -1
@@ -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;
@@ -4410,6 +4410,11 @@ static int btf_dedup_remap_types(struct btf_dedup *d)* data out of it to use for target BTF. */ struct btf *libbpf_find_kernel_btf (void ) +{ +return btf__load_vmlinux_btf (); +} + +struct btf *btf__load_vmlinux_btf (void ) {struct {const char *path_fmt;
@@ -4455,6 +4460,14 @@ struct btf *libbpf_find_kernel_btf(void)return libbpf_err_ptr (-ESRCH ); } +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;
tools/lib/bpf/btf.h +4 -2
@@ -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 *libbpf_find_kernel_btf(void); +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 *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,
tools/lib/bpf/libbpf.c +2 -2
@@ -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" );
tools/lib/bpf/libbpf.map +2 -0
@@ -380,4 +380,6 @@ LIBBPF_0.5.0 { btf__load_into_kernel; btf_dump__dump_type_data; libbpf_set_strict_mode; + btf__load_vmlinux_btf; + btf__load_module_btf; } LIBBPF_0.4.0;
Signature · 1 line
2.25.1
Quoted 10 lines · Hengqi Chen
Add two new APIs:
btf__load_vmlinux_btfandbtf__load_module_btf.btf__load_vmlinux_btfis just an alias to the existing API namedlibbpf_find_kernel_btf, rename to be more precisely and consistent with existing BTF APIs.btf__load_module_btfcan 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. [1]
I changed this to
[0] Closes: https://github.com/libbpf/libbpf/issues/280
which will close an associated Github issue when we sync sources to Github next time. Let's see how this works in practice.
Quoted 3 lines
Signed-off-by Hengqi Chen <[email protected]>---
Thanks, applied to bpf-next. But please follow up with a selftest that would utilize this new module BTF API. It's good to have all APIs exercised regularly. Look at test_progs.
Quoted 20 lines
tools/lib/bpf/btf.c | 15 ++++++++++++++- tools/lib/bpf/btf.h | 6 ++++-- tools/lib/bpf/libbpf.c | 4 ++-- tools/lib/bpf/libbpf.map | 2 ++ 4 files changed, 22 insertions(+), 5 deletions(-)tools/lib/bpf/btf.c +1
-1@@ -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;
tools/lib/bpf/btf.c static int btf_dedup_remap_types(struct btf_dedup *d) · line 4412
Quoted 3 lines
tools/lib/bpf/btf.c +0
-0@@ -4410,6 +4410,11 @@ static int btf_dedup_remap_types(struct btf_dedup *d)* data out of it to use for target BTF. */ struct btf *libbpf_find_kernel_btf (void )
I switched this to __attribute__((alias("btf__load_vmlinux_btf"))); to match what Quentin did recently. Also moved comment above to be next to btf__load_vmlinux_btf.
Quoted 8 lines
tools/lib/bpf/btf.c +5
-0+{ +return btf__load_vmlinux_btf (); +} + +struct btf *btf__load_vmlinux_btf (void ) {struct {const char *path_fmt;
[...]
tools/lib/bpf/libbpf.map LIBBPF_0.5.0 { · line 384
Quoted 10 lines
tools/lib/bpf/libbpf.map +2
-0@@ -380,4 +380,6 @@ LIBBPF_0.5.0 { btf__load_into_kernel; btf_dump__dump_type_data; libbpf_set_strict_mode; + btf__load_vmlinux_btf; + btf__load_module_btf;
This list needs to be alphabetically sorted. I'll fix it up while applying, but please remember it for the future.
Quoted 4 lines
tools/lib/bpf/libbpf.map +0
-1} LIBBPF_0.4.0; --2.25.1
Quoted 20 lines · Andrii Nakryiko
Quoted 10 lines · Hengqi Chen
Add two new APIs:
btf__load_vmlinux_btfandbtf__load_module_btf.btf__load_vmlinux_btfis just an alias to the existing API namedlibbpf_find_kernel_btf, rename to be more precisely and consistent with existing BTF APIs.btf__load_module_btfcan 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. [1]I changed this to
[0] Closes: https://github.com/libbpf/libbpf/issues/280
which will close an associated Github issue when we sync sources to Github next time. Let's see how this works in practice.
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
Signed-off-by Hengqi Chen <[email protected]>---
Thanks, applied to bpf-next. But please follow up with a selftest that would utilize this new module BTF API. It's good to have all APIs exercised regularly. Look at
test_progs.
Thanks, will do.
Quoted 19 lines
Quoted 19 lines
tools/lib/bpf/btf.c | 15 ++++++++++++++- tools/lib/bpf/btf.h | 6 ++++-- tools/lib/bpf/libbpf.c | 4 ++-- tools/lib/bpf/libbpf.map | 2 ++ 4 files changed, 22 insertions(+), 5 deletions(-)tools/lib/bpf/btf.c +1
-1@@ -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;
Quoted 9 lines
tools/lib/bpf/btf.c static int btf_dedup_remap_types(struct btf_dedup *d) · line 4412
Quoted 4 lines
tools/lib/bpf/btf.c +0
-0@@ -4410,6 +4410,11 @@ static int btf_dedup_remap_types(struct btf_dedup *d)* data out of it to use for target BTF. */ struct btf *libbpf_find_kernel_btf (void )I switched this to __attribute__((alias("btf__load_vmlinux_btf"))); to match what Quentin did recently. Also moved comment above to be next to
btf__load_vmlinux_btf.
OK, the alias attribute look nicer.
Quoted 11 lines
Quoted 8 lines
tools/lib/bpf/btf.c +5
-0+{ +return btf__load_vmlinux_btf (); +} + +struct btf *btf__load_vmlinux_btf (void ) {struct {const char *path_fmt;[...]
Quoted 14 lines
tools/lib/bpf/libbpf.map LIBBPF_0.5.0 { · line 384
Quoted 10 lines
tools/lib/bpf/libbpf.map +2
-0@@ -380,4 +380,6 @@ LIBBPF_0.5.0 { btf__load_into_kernel; btf_dump__dump_type_data; libbpf_set_strict_mode; + btf__load_vmlinux_btf; + btf__load_module_btf;This list needs to be alphabetically sorted. I'll fix it up while applying, but please remember it for the future.
Yeah, will keep this in mind.
Quoted 4 lines
Quoted 4 lines
tools/lib/bpf/libbpf.map +0
-1} LIBBPF_0.4.0; --2.25.1