@@ -32,6 +32,7 @@ type program_template = {
3232 return_type : string ;
3333 includes : string list ;
3434 types : btf_type_info list ;
35+ function_signatures : (string * string ) list ; (* Function name and signature for kprobe targets *)
3536}
3637
3738
@@ -106,12 +107,16 @@ let rec get_program_template prog_type btf_path =
106107
107108 let all_types = filtered_types @ hardcoded_types in
108109
110+ (* No function signatures for generic program templates - kprobe uses specific template *)
111+ let function_signatures = [] in
112+
109113 {
110114 program_type = prog_type;
111115 context_type = context_type;
112116 return_type = return_type;
113117 includes = [" linux/bpf.h" ; " linux/pkt_cls.h" ; " linux/if_ether.h" ; " linux/ip.h" ; " linux/tcp.h" ; " linux/udp.h" ];
114118 types = all_types;
119+ function_signatures = function_signatures;
115120 }
116121
117122(* * Extract specific types from BTF file using binary parser *)
@@ -141,7 +146,40 @@ and extract_types_from_btf btf_path type_names =
141146 | exn ->
142147 failwith (sprintf " BTF extraction failed: %s" (Printexc. to_string exn ))
143148
144-
149+ (* * Get kprobe program template for a specific target function *)
150+ let get_kprobe_program_template target_function btf_path =
151+ let context_type = " *pt_regs" in
152+ let return_type = " i32" in
153+ let common_types = [" pt_regs" ] in
154+
155+ (* Extract types from BTF - BTF file is required *)
156+ let extracted_types = match btf_path with
157+ | Some path when Sys. file_exists path -> extract_types_from_btf path common_types
158+ | Some path -> failwith (sprintf " BTF file not found: %s" path)
159+ | None -> failwith " BTF file path is required. Use --btf-vmlinux-path option."
160+ in
161+
162+ (* Extract specific function signature for the target *)
163+ let function_signatures = match btf_path with
164+ | Some path when Sys. file_exists path ->
165+ printf " 🔧 Extracting function signature for %s...\n " target_function;
166+ let signatures = Btf_binary_parser. extract_kernel_function_signatures path [target_function] in
167+ if signatures = [] then
168+ printf " ⚠️ Function '%s' not found in BTF - proceeding without signature\n " target_function
169+ else
170+ printf " ✅ Extracted signature for %s\n " target_function;
171+ signatures
172+ | _ -> []
173+ in
174+
175+ {
176+ program_type = " kprobe" ;
177+ context_type = context_type;
178+ return_type = return_type;
179+ includes = [" linux/bpf.h" ; " linux/pkt_cls.h" ; " linux/if_ether.h" ; " linux/ip.h" ; " linux/tcp.h" ; " linux/udp.h" ];
180+ types = extracted_types;
181+ function_signatures = function_signatures;
182+ }
145183
146184(* * Extract struct_ops definitions from BTF and generate KernelScript code *)
147185let extract_struct_ops_definitions btf_path struct_ops_names =
@@ -230,13 +268,38 @@ let generate_kernelscript_source template project_name =
230268 | [] -> " 0"
231269 in
232270
271+ (* Generate function signature comments for kprobe programs *)
272+ let (function_signatures_comment, target_function_name) =
273+ if template.program_type = " kprobe" && template.function_signatures <> [] then
274+ let signature_lines = List. map (fun (func_name , signature ) ->
275+ sprintf " // Target function: %s -> %s" func_name signature
276+ ) template.function_signatures in
277+ let comment = sprintf " \n // Target kernel function signature:\n %s\n "
278+ (String. concat " \n " signature_lines) in
279+ let first_func = match template.function_signatures with
280+ | (name , _ ) :: _ -> name
281+ | [] -> " target_function"
282+ in
283+ (comment, first_func)
284+ else
285+ (" " , " target_function" )
286+ in
287+
288+ (* Customize attach call for kprobe *)
289+ let attach_target = if template.program_type = " kprobe" then target_function_name else " eth0" in
290+ let attach_comment = if template.program_type = " kprobe" then
291+ " // Attach kprobe to target kernel function"
292+ else
293+ " // TODO: Update interface name and attachment parameters"
294+ in
295+
233296 sprintf {|% s
234297// Generated by KernelScript compiler with direct BTF parsing
235-
298+ % s
236299% s
237300
238301@% s
239- fn % s_handler(ctx: * %s ) -> % s {
302+ fn % s_handler(ctx: %s ) -> % s {
240303 // TODO : Implement your % s logic here
241304
242305 return % s
@@ -245,8 +308,8 @@ fn %s_handler(ctx: *%s) -> %s {
245308fn main() -> i32 {
246309 var prog = load(% s_handler)
247310
248- // TODO : Update interface name and attachment parameters
249- var result = attach(prog, " eth0 " , 0 )
311+ % s
312+ var result = attach(prog, " %s " , 0 )
250313
251314 if (result == 0 ) {
252315 print(" %s program loaded successfully" )
@@ -257,4 +320,4 @@ fn main() -> i32 {
257320
258321 return 0
259322}
260- | } context_comment type_definitions template.program_type project_name template.context_type template.return_type template.program_type sample_return project_name template.program_type template.program_type
323+ | } context_comment function_signatures_comment type_definitions template.program_type project_name template.context_type template.return_type template.program_type sample_return project_name attach_comment attach_target template.program_type template.program_type
0 commit comments