libbpf: add libbpf_load_vmlinux_btf/libbpf_load_module_btf APIs
PATCH v2 1/1 libbpf: add libbpf_load_vmlinux_btf/libbpf_load_module_btf APIs
Add two new APIs: libbpf_load_vmlinux_btf/libbpf_load_module_btf. libbpf_load_vmlinux_btf is just an alias to the existing API named libbpf_find_kernel_btf, rename it to be more precisely. libbpf_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 efforts towards libbpf 1.0. [1]
[1] https://github.com/libbpf/libbpf/issues/280
--- tools/lib/bpf/btf.c | 15 ++++++++++++++- tools/lib/bpf/btf.h | 2 ++ tools/lib/bpf/libbpf.c | 4 ++-- tools/lib/bpf/libbpf.map | 2 ++ 4 files changed, 20 insertions(+), 3 deletions(-)
tools/lib/bpf/btf.c +14 -1
@@ -4021,7 +4021,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;
@@ -4395,6 +4395,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 libbpf_load_vmlinux_btf (); +} + +struct btf *libbpf_load_vmlinux_btf (void ) {struct {const char *path_fmt;
@@ -4440,6 +4445,14 @@ struct btf *libbpf_find_kernel_btf(void)return libbpf_err_ptr (-ESRCH ); } +struct btf *libbpf_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 +2 -0
@@ -90,6 +90,8 @@ 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 struct btf *libbpf_load_vmlinux_btf(void); +LIBBPF_API struct btf *libbpf_load_module_btf(const char *module_name, struct btf *vmlinux_btf); LIBBPF_API int btf__find_str(struct btf *btf, const char *s); LIBBPF_API int btf__add_str(struct btf *btf, const char *s);
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 =libbpf_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 =libbpf_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
@@ -377,4 +377,6 @@ LIBBPF_0.5.0 { bpf_object__gen_loader; btf_dump__dump_type_data; libbpf_set_strict_mode; + libbpf_load_vmlinux_btf; + libbpf_load_module_btf; } LIBBPF_0.4.0;
Signature · 1 line
2.25.1
Quoted 45 lines · Hengqi Chen
Add two new APIs: libbpf_load_vmlinux_btf/libbpf_load_module_btf.
libbpf_load_vmlinux_btfis just an alias to the existing API namedlibbpf_find_kernel_btf, rename it to be more precisely.libbpf_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 efforts towards libbpf 1.0. [1][1] https://github.com/libbpf/libbpf/issues/280
Signed-off-by Hengqi Chen <[email protected]>--- tools/lib/bpf/btf.c | 15 ++++++++++++++- tools/lib/bpf/btf.h | 2 ++ tools/lib/bpf/libbpf.c | 4 ++-- tools/lib/bpf/libbpf.map | 2 ++ 4 files changed, 20 insertions(+), 3 deletions(-)tools/lib/bpf/btf.c +6
-1@@ -4021,7 +4021,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;@@ -4395,6 +4395,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 libbpf_load_vmlinux_btf (); +} + +struct btf *libbpf_load_vmlinux_btf (void ) {struct {const char *path_fmt;
tools/lib/bpf/btf.c struct btf *libbpf_find_kernel_btf(void) · line 4448
Quoted 4 lines
tools/lib/bpf/btf.c +1
-0@@ -4440,6 +4445,14 @@ struct btf *libbpf_find_kernel_btf(void)return libbpf_err_ptr (-ESRCH ); } +struct btf *libbpf_load_module_btf (const char *module_name,struct btf *vmlinux_btf)
In the light of Quentin's btf__load_from_kernel_by_id(), I'm now wondering if it's better to keep the naming consistent as btf__load_vmlinux_btf() and btf__load_module_btf()? And we should put them after btf__parse() family of constructors, as just another set of (special, but still) constructors. WDYT?
Sorry for a bit of back and forth...
Otherwise everything looks good, thanks.
Quoted 15 lines
tools/lib/bpf/btf.c +7
-0+{ +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 +0
-0
tools/lib/bpf/btf.h LIBBPF_API __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext); · line 94
Quoted 5 lines
tools/lib/bpf/btf.h +2
-0@@ -90,6 +90,8 @@ 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 struct btf *libbpf_load_vmlinux_btf(void); +LIBBPF_API struct btf *libbpf_load_module_btf(const char *module_name, struct btf *vmlinux_btf);
as mentioned above, let's move these right after btf__parse() family, so that all BTF constructor APIs are listed first.
Quoted 39 lines
tools/lib/bpf/btf.h +0
-0LIBBPF_API int btf__find_str(struct btf *btf, const char *s); LIBBPF_API int btf__add_str(struct btf *btf, const char *s);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 =libbpf_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 =libbpf_load_vmlinux_btf (); err =libbpf_get_error (btf);if (err) {pr_warn ("vmlinux BTF is not found\n" );tools/lib/bpf/libbpf.map +2
-1@@ -377,4 +377,6 @@ LIBBPF_0.5.0 { bpf_object__gen_loader; btf_dump__dump_type_data; libbpf_set_strict_mode; + libbpf_load_vmlinux_btf; + libbpf_load_module_btf; } LIBBPF_0.4.0; --2.25.1
Quoted 57 lines · Andrii Nakryiko
Quoted 45 lines · Hengqi Chen
Add two new APIs: libbpf_load_vmlinux_btf/libbpf_load_module_btf.
libbpf_load_vmlinux_btfis just an alias to the existing API namedlibbpf_find_kernel_btf, rename it to be more precisely.libbpf_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 efforts towards libbpf 1.0. [1][1] https://github.com/libbpf/libbpf/issues/280
Signed-off-by Hengqi Chen <[email protected]>--- tools/lib/bpf/btf.c | 15 ++++++++++++++- tools/lib/bpf/btf.h | 2 ++ tools/lib/bpf/libbpf.c | 4 ++-- tools/lib/bpf/libbpf.map | 2 ++ 4 files changed, 20 insertions(+), 3 deletions(-)tools/lib/bpf/btf.c +6
-1@@ -4021,7 +4021,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;@@ -4395,6 +4395,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 libbpf_load_vmlinux_btf (); +} + +struct btf *libbpf_load_vmlinux_btf (void ) {struct {const char *path_fmt;tools/lib/bpf/btf.c struct btf *libbpf_find_kernel_btf(void) · line 4448
Quoted 4 lines
tools/lib/bpf/btf.c +1
-0@@ -4440,6 +4445,14 @@ struct btf *libbpf_find_kernel_btf(void)return libbpf_err_ptr (-ESRCH ); } +struct btf *libbpf_load_module_btf (const char *module_name,struct btf *vmlinux_btf)In the light of Quentin's
btf__load_from_kernel_by_id(), I'm now wondering if it's better to keep the naming consistent asbtf__load_vmlinux_btf()andbtf__load_module_btf()? And we should put them afterbtf__parse()family of constructors, as just another set of (special, but still) constructors. WDYT?
Quoted 4 lines
Sorry for a bit of back and forth...
Otherwise everything looks good, thanks.
Thanks for the review. Will update as you suggested.
Sometimes naming is the hardest part of programming, I am not good at that. :)
(BTW, Quentin's tweet led me to here)
Quoted 17 lines
Quoted 17 lines
tools/lib/bpf/btf.c +7
-0+{ +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 +0
-0@@ -90,6 +90,8 @@ 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);
Quoted 7 lines
tools/lib/bpf/btf.h LIBBPF_API __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext); · line 94
Quoted 3 lines
tools/lib/bpf/btf.h +2
-0LIBBPF_API struct btf *libbpf_find_kernel_btf(void); +LIBBPF_API struct btf *libbpf_load_vmlinux_btf(void); +LIBBPF_API struct btf *libbpf_load_module_btf(const char *module_name, struct btf *vmlinux_btf);as mentioned above, let's move these right after
btf__parse()family, so that all BTF constructor APIs are listed first.
Sure, will do.
Quoted 39 lines
Quoted 39 lines
tools/lib/bpf/btf.h +0
-0LIBBPF_API int btf__find_str(struct btf *btf, const char *s); LIBBPF_API int btf__add_str(struct btf *btf, const char *s);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 =libbpf_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 =libbpf_load_vmlinux_btf (); err =libbpf_get_error (btf);if (err) {pr_warn ("vmlinux BTF is not found\n" );tools/lib/bpf/libbpf.map +2
-1@@ -377,4 +377,6 @@ LIBBPF_0.5.0 { bpf_object__gen_loader; btf_dump__dump_type_data; libbpf_set_strict_mode; + libbpf_load_vmlinux_btf; + libbpf_load_module_btf; } LIBBPF_0.4.0; --2.25.1