@@ -37,8 +37,45 @@ type program_template = {
37
37
38
38
39
39
40
- (* * Check if a type name is a well-known kernel type *)
41
- let is_well_known_kernel_type ?btf_path = Kernel_types. is_well_known_ebpf_type ?btf_path
40
+ (* * Cache for BTF-extracted kernel types to avoid re-parsing *)
41
+ let kernel_types_cache : (string, string list) Hashtbl.t = Hashtbl. create 16
42
+
43
+ (* * Extract kernel types from BTF file with caching *)
44
+ let get_kernel_types_from_btf btf_path =
45
+ match Hashtbl. find_opt kernel_types_cache btf_path with
46
+ | Some cached_types -> cached_types
47
+ | None ->
48
+ let kernel_types = Btf_binary_parser. extract_all_kernel_struct_and_enum_names btf_path in
49
+ Hashtbl. add kernel_types_cache btf_path kernel_types;
50
+ kernel_types
51
+
52
+ (* * Check if a type name is a well-known eBPF kernel type using BTF. *)
53
+ let is_well_known_ebpf_type ?btf_path type_name =
54
+ match btf_path with
55
+ | Some path when Sys. file_exists path ->
56
+ let kernel_types = get_kernel_types_from_btf path in
57
+ List. mem type_name kernel_types
58
+ | Some path ->
59
+ failwith (sprintf " BTF file not found: %s" path)
60
+ | None ->
61
+ failwith " BTF file path is required for kernel type detection. Use --btf-vmlinux-path option."
62
+
63
+ (* * Clear the kernel types cache (useful for testing or when BTF file changes) *)
64
+ let clear_kernel_types_cache () =
65
+ Hashtbl. clear kernel_types_cache
66
+
67
+ (* * Get all known kernel types for the given BTF file (for debugging/inspection) *)
68
+ let get_all_kernel_types ?btf_path () =
69
+ match btf_path with
70
+ | Some path when Sys. file_exists path ->
71
+ get_kernel_types_from_btf path
72
+ | Some path ->
73
+ failwith (sprintf " BTF file not found: %s" path)
74
+ | None ->
75
+ failwith " BTF file path is required for kernel type inspection. Use --btf-vmlinux-path option."
76
+
77
+ (* * Check if a type name is a well-known kernel type (alias for compatibility) *)
78
+ let is_well_known_kernel_type ?btf_path = is_well_known_ebpf_type ?btf_path
42
79
43
80
(* * Create hardcoded enum definitions for constants that can't be extracted from BTF *)
44
81
let create_hardcoded_tc_action_enum () = {
@@ -82,7 +119,7 @@ let get_program_template prog_type btf_path =
82
119
kind = bt.Btf_binary_parser. kind;
83
120
size = bt.Btf_binary_parser. size;
84
121
members = bt.Btf_binary_parser. members;
85
- kernel_defined = is_well_known_kernel_type ?btf_path bt.Btf_binary_parser. name;
122
+ kernel_defined = is_well_known_ebpf_type ?btf_path bt.Btf_binary_parser. name;
86
123
}) binary_types
87
124
| Some path -> failwith (sprintf " BTF file not found: %s" path)
88
125
| None -> failwith " BTF file path is required. Use --btf-vmlinux-path option."
@@ -147,7 +184,7 @@ let get_tracepoint_program_template category_event btf_path =
147
184
kind = bt.Btf_binary_parser. kind;
148
185
size = bt.Btf_binary_parser. size;
149
186
members = bt.Btf_binary_parser. members;
150
- kernel_defined = is_well_known_kernel_type ?btf_path bt.Btf_binary_parser. name;
187
+ kernel_defined = is_well_known_ebpf_type ?btf_path bt.Btf_binary_parser. name;
151
188
}) binary_types
152
189
| Some path -> failwith (sprintf " BTF file not found: %s" path)
153
190
| None -> failwith " BTF file path is required for tracepoint extraction. Use --btf-vmlinux-path option."
0 commit comments