Skip to contentSkip to conversation

libbpf: introduce line_info and func_info getters

PATCH Superseded Mykyta Yatsenko 2 participants 4 messages Mar 31, 2025

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover libbpf: introduce line_info and func_info getters

From: Mykyta Yatsenko <[email protected]>

This patchset introduces new libbpf API getters that enable the retrieval of .BTF.ext line and func info.
This change enables users to load bpf_program directly using bpf_prog_load, bypassing the higher-level bpf_object__load API. Providing line and function info is essential for BPF program verification in some cases.

v1 -> v2
* Move bpf_line_info_min and bpf_func_info_min from libbpf_internal.h to btf.h. Did not remove _min suffix, because there already are bpf_line_info and bpf_func_info structs in uapi/../bpf.h.

Mykyta Yatsenko (2):
libbpf: make bpf_line/func_info_min public
libbpf: add getters for BTF.ext func and line info

 tools/lib/bpf/btf.h             | 14 ++++++++++++++
 tools/lib/bpf/libbpf.c          | 20 ++++++++++++++++++++
 tools/lib/bpf/libbpf.h          |  6 ++++++
 tools/lib/bpf/libbpf.map        |  4 ++++
 tools/lib/bpf/libbpf_internal.h | 14 --------------
 5 files changed, 44 insertions(+), 14 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH v2 1/2 libbpf: make bpf_line/func_info_min public

From: Mykyta Yatsenko <[email protected]>

Move struct bpf_line_info_min and bpf_func_info_min to public header btf.h, allowing to use these structures in public APIs. This change is needed for the next patch, introducing getters for bpf_program's func and line infos.

---
 tools/lib/bpf/btf.h             | 14 ++++++++++++++
 tools/lib/bpf/libbpf_internal.h | 14 --------------
 2 files changed, 14 insertions(+), 14 deletions(-)
tools/lib/bpf/btf.h +14 -0
index 4392451d634b..1e57cedf9d58 100644
@@ -610,6 +610,20 @@ static inline struct btf_decl_tag *btf_decl_tag(const struct btf_type *t)
 	return (struct btf_decl_tag *)(t + 1);
 }
 
+/* Represents func_info encoded in .BTF.ext section of ELF */
+struct bpf_func_info_min {
+	__u32   insn_off;
+	__u32   type_id;
+};
+
+/* Represents line_info encoded in .BTF.ext section of ELF */
+struct bpf_line_info_min {
+	__u32	insn_off;
+	__u32	file_name_off;
+	__u32	line_off;
+	__u32	line_col;
+};
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
tools/lib/bpf/libbpf_internal.h +0 -14
index 76669c73dcd1..6bc9a9919295 100644
@@ -501,20 +501,6 @@ struct btf_ext_info_sec {
 	__u8	data[];
 };
 
-/* The minimum bpf_func_info checked by the loader */
-struct bpf_func_info_min {
-	__u32   insn_off;
-	__u32   type_id;
-};
-
-/* The minimum bpf_line_info checked by the loader */
-struct bpf_line_info_min {
-	__u32	insn_off;
-	__u32	file_name_off;
-	__u32	line_off;
-	__u32	line_col;
-};
-
 /* Functions to byte-swap info records */
 
 typedef void (*info_rec_bswap_fn)(void *);
Signature · 1 line
2.49.0
#2 lore raw

PATCH v2 2/2 libbpf: add getters for BTF.ext func and line info

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Introducing new libbpf API getters for BTF.ext func and line info, namely:
bpf_program__func_info
bpf_program__func_info_cnt
bpf_program__line_info
bpf_program__line_info_cnt

This change enables scenarios, when user needs to load bpf_program directly using bpf_prog_load, instead of higher-level bpf_object__load. Line and func info are required for checking BTF info in verifier; verification may fail without these fields if, for example, program calls bpf_obj_new.

---
 tools/lib/bpf/libbpf.c   | 20 ++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |  6 ++++++
 tools/lib/bpf/libbpf.map |  4 ++++
 3 files changed, 30 insertions(+)
tools/lib/bpf/libbpf.c +20 -0
index 6b85060f07b3..86f6aff76ef2 100644
@@ -9455,6 +9455,26 @@ int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log
 	return 0;
 }
 
+struct bpf_func_info_min *bpf_program__func_info(const struct bpf_program *prog)
+{
+	return prog->func_info;
+}
+
+__u32 bpf_program__func_info_cnt(const struct bpf_program *prog)
+{
+	return prog->func_info_cnt;
+}
+
+struct bpf_line_info_min *bpf_program__line_info(const struct bpf_program *prog)
+{
+	return prog->line_info;
+}
+
+__u32 bpf_program__line_info_cnt(const struct bpf_program *prog)
+{
+	return prog->line_info_cnt;
+}
+
 #define SEC_DEF(sec_pfx, ptype, atype, flags, ...) {			    \
 	.sec = (char *)sec_pfx,						    \
 	.prog_type = BPF_PROG_TYPE_##ptype,				    \
tools/lib/bpf/libbpf.h +6 -0
index e0605403f977..a6ec87fb0fb9 100644
@@ -940,6 +940,12 @@ LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_le
 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
 
+LIBBPF_API struct bpf_func_info_min *bpf_program__func_info(const struct bpf_program *prog);
+LIBBPF_API __u32 bpf_program__func_info_cnt(const struct bpf_program *prog);
+
+LIBBPF_API struct bpf_line_info_min *bpf_program__line_info(const struct bpf_program *prog);
+LIBBPF_API __u32 bpf_program__line_info_cnt(const struct bpf_program *prog);
+
 /**
  * @brief **bpf_program__set_attach_target()** sets BTF-based attach target
  * for supported BPF program types:
tools/lib/bpf/libbpf.map +4 -0
index d8b71f22f197..1205f9a4fe04 100644
@@ -437,6 +437,10 @@ LIBBPF_1.6.0 {
 		bpf_linker__add_fd;
 		bpf_linker__new_fd;
 		bpf_object__prepare;
+		bpf_program__func_info;
+		bpf_program__func_info_cnt;
+		bpf_program__line_info;
+		bpf_program__line_info_cnt;
 		btf__add_decl_attr;
 		btf__add_type_attr;
 } LIBBPF_1.5.0;
Signature · 1 line
2.49.0
#3 lore raw

Re: [PATCH bpf-next v2 2/2] libbpf: add getters for BTF.ext func and line info

Quoted 41 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Introducing new libbpf API getters for BTF.ext func and line info, namely:
bpf_program__func_info
bpf_program__func_info_cnt
bpf_program__line_info
bpf_program__line_info_cnt

This change enables scenarios, when user needs to load bpf_program directly using bpf_prog_load, instead of higher-level bpf_object__load. Line and func info are required for checking BTF info in verifier; verification may fail without these fields if, for example, program calls bpf_obj_new.

---
 tools/lib/bpf/libbpf.c   | 20 ++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |  6 ++++++
 tools/lib/bpf/libbpf.map |  4 ++++
 3 files changed, 30 insertions(+)
tools/lib/bpf/libbpf.c +10 -0
index 6b85060f07b3..86f6aff76ef2 100644
@@ -9455,6 +9455,26 @@ int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log
        return 0;
 }
 
+struct bpf_func_info_min *bpf_program__func_info(const struct bpf_program *prog)
+{
+       return prog->func_info;
+}
+
+__u32 bpf_program__func_info_cnt(const struct bpf_program *prog)
+{
+       return prog->func_info_cnt;
+}
+

tools/lib/bpf/libbpf.c int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log · line 9477

Quoted 10 lines
tools/lib/bpf/libbpf.c +10 -0
+struct bpf_line_info_min *bpf_program__line_info(const struct bpf_program *prog)
+{
+       return prog->line_info;
+}
+
+__u32 bpf_program__line_info_cnt(const struct bpf_program *prog)
+{
+       return prog->line_info_cnt;
+}
+

I didn't realize that we already have bpf_line_info/bpf_func_info as part of Linux UAPI. If that's the case, let's keep _min variants where they are (internal), and use public types. No need for patch #1.

Let's just add:

if (prog->func_info_rec_size != sizeof(struct bpf_func_info_min))

    return libbpf_err_ptr(-EOPNOTSUPP);

everywhere so we don't accidentally return invalid data, ok?

Also, let's do a bit of testing here. I think it would be nice to have a selftests that loads some simple BPF program (perhaps with non-inlined subprog to test func/line info with subprogs) first through normal skeleton load approach. Capture its func_info/line_info from kernel. Then use these new APIs you are adding and do bpf_prog_load(), and then compare that we got identical func_info/line_info (as reported by the kernel). Can you please add that?

Quoted 38 lines
tools/lib/bpf/libbpf.c +0 -0
 #define SEC_DEF(sec_pfx, ptype, atype, flags, ...) {                       \
        .sec = (char *)sec_pfx,                                             \
        .prog_type = BPF_PROG_TYPE_##ptype,                                 \
tools/lib/bpf/libbpf.h +6 -0
index e0605403f977..a6ec87fb0fb9 100644
@@ -940,6 +940,12 @@ LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_le
 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
 
+LIBBPF_API struct bpf_func_info_min *bpf_program__func_info(const struct bpf_program *prog);
+LIBBPF_API __u32 bpf_program__func_info_cnt(const struct bpf_program *prog);
+
+LIBBPF_API struct bpf_line_info_min *bpf_program__line_info(const struct bpf_program *prog);
+LIBBPF_API __u32 bpf_program__line_info_cnt(const struct bpf_program *prog);
+
 /**
  * @brief **bpf_program__set_attach_target()** sets BTF-based attach target
  * for supported BPF program types:
tools/lib/bpf/libbpf.map +4 -1
index d8b71f22f197..1205f9a4fe04 100644
@@ -437,6 +437,10 @@ LIBBPF_1.6.0 {
                bpf_linker__add_fd;
                bpf_linker__new_fd;
                bpf_object__prepare;
+               bpf_program__func_info;
+               bpf_program__func_info_cnt;
+               bpf_program__line_info;
+               bpf_program__line_info_cnt;
                btf__add_decl_attr;
                btf__add_type_attr;
 } LIBBPF_1.5.0;
--

2.49.0