Skip to content

Commit 869d270

Browse files
committed
Add BTF kind constants for struct, union, enum, and enum64 in the binary parser and stubs.
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent 71e60ea commit 869d270

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

src/btf_binary_parser.ml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ external btf_extract_kernel_struct_and_enum_names : btf_handle -> string list =
4242
external btf_extract_kfuncs : btf_handle -> (string * string) list = "btf_extract_kfuncs_stub"
4343
external btf_free : btf_handle -> unit = "btf_free_stub"
4444

45+
(** BTF kind constants from C headers *)
46+
external btf_kind_struct : unit -> int = "btf_kind_struct_stub"
47+
external btf_kind_union : unit -> int = "btf_kind_union_stub"
48+
external btf_kind_enum : unit -> int = "btf_kind_enum_stub"
49+
external btf_kind_enum64 : unit -> int = "btf_kind_enum64_stub"
50+
4551
(** Parse BTF file and extract requested types using libbpf *)
4652
let parse_btf_file btf_path target_types =
4753
try
@@ -82,17 +88,17 @@ let parse_btf_file btf_path target_types =
8288

8389
(* Check if this is a target type *)
8490
if List.mem name target_types then (
85-
let kind_str = match kind_int with
86-
| 4 -> "struct"
87-
| 5 -> "union"
88-
| 6 -> "enum"
89-
| 19 -> "enum64"
90-
| _ -> "unknown"
91+
let kind_str =
92+
if kind_int = btf_kind_struct () then "struct"
93+
else if kind_int = btf_kind_union () then "union"
94+
else if kind_int = btf_kind_enum () then "enum"
95+
else if kind_int = btf_kind_enum64 () then "enum64"
96+
else "unknown"
9197
in
9298

9399
(* Get members for struct/union/enum types *)
94100
let members =
95-
if kind_int = 4 || kind_int = 5 then (
101+
if kind_int = btf_kind_struct () || kind_int = btf_kind_union () then (
96102
(* Struct/Union: resolve member types *)
97103
try
98104
let member_array = btf_type_get_members btf_handle i in
@@ -123,8 +129,7 @@ let parse_btf_file btf_path target_types =
123129
Some (List.rev resolved_members)
124130
with
125131
| _ -> None
126-
) else if kind_int = 6 || kind_int = 19 then (
127-
(* Enum (kind 6) or Enum64 (kind 19): extract enum values *)
132+
) else if kind_int = btf_kind_enum () || kind_int = btf_kind_enum64 () then (
128133
try
129134
let member_array = btf_type_get_members btf_handle i in
130135
let member_list = Array.to_list member_array in

src/btf_stubs.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@
2626
#include <caml/fail.h>
2727
#include <caml/custom.h>
2828

29+
/* BTF kind constants - expose to OCaml */
30+
value btf_kind_struct_stub(value unit) {
31+
CAMLparam1(unit);
32+
CAMLreturn(Val_int(BTF_KIND_STRUCT));
33+
}
34+
35+
value btf_kind_union_stub(value unit) {
36+
CAMLparam1(unit);
37+
CAMLreturn(Val_int(BTF_KIND_UNION));
38+
}
39+
40+
value btf_kind_enum_stub(value unit) {
41+
CAMLparam1(unit);
42+
CAMLreturn(Val_int(BTF_KIND_ENUM));
43+
}
44+
45+
value btf_kind_enum64_stub(value unit) {
46+
CAMLparam1(unit);
47+
CAMLreturn(Val_int(BTF_KIND_ENUM64));
48+
}
49+
2950
/* Debug macro */
3051
/* #define DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__) */
3152
#define DEBUG_PRINT(...)

0 commit comments

Comments
 (0)