@@ -201,9 +201,75 @@ fn sched_switch_handler(ctx: *trace_event_raw_sched_switch) -> i32 {
201201 check bool " Function should be marked as main" true main_func.is_main;
202202 check string " Function name should match" " sched_switch_handler" main_func.func_name
203203
204+ (* NEW: Target Propagation Tests *)
205+ let test_tracepoint_target_propagation _ =
206+ let source = " @tracepoint(\" sched/sched_switch\" )
207+ fn sched_switch_handler(ctx: *trace_event_raw_sched_switch) -> i32 {
208+ return 0
209+ }" in
210+ let ast = parse_string source in
211+ let typed_ast = type_check_ast ast in
212+ let symbol_table = Kernelscript.Symbol_table. build_symbol_table typed_ast in
213+ let ir_multi_prog = generate_ir typed_ast symbol_table " test_tracepoint" in
214+ let program = List. hd ir_multi_prog.programs in
215+ let main_func = program.entry_function in
216+
217+ (* Test that the target is properly propagated through IR generation *)
218+ check (option string ) " Function should have correct target" (Some " sched/sched_switch" ) main_func.func_target
219+
220+ let test_multiple_tracepoint_targets _ =
221+ (* Test various tracepoint targets to ensure they all work correctly *)
222+ let test_cases = [
223+ (" sched/sched_switch" , " SEC(\" raw_tracepoint/sched_switch\" )" );
224+ (" net/netif_rx" , " SEC(\" raw_tracepoint/netif_rx\" )" );
225+ (" syscalls/sys_enter_read" , " SEC(\" raw_tracepoint/sys_enter_read\" )" );
226+ (" syscalls/sys_exit_write" , " SEC(\" raw_tracepoint/sys_exit_write\" )" );
227+ (" irq/irq_handler_entry" , " SEC(\" raw_tracepoint/irq_handler_entry\" )" );
228+ ] in
229+
230+ List. iter (fun (target , expected_sec ) ->
231+ let source = Printf. sprintf " @tracepoint(\" %s\" )
232+ fn handler(ctx: *trace_event_raw_context) -> i32 {
233+ return 0
234+ }" target in
235+ let ast = parse_string source in
236+ let typed_ast = type_check_ast ast in
237+ let symbol_table = Kernelscript.Symbol_table. build_symbol_table typed_ast in
238+ let ir_multi_prog = generate_ir typed_ast symbol_table " test" in
239+ let c_code = generate_c_multi_program ir_multi_prog in
240+
241+ check bool (Printf. sprintf " Should generate %s for target %s" expected_sec target) true
242+ (Str. search_forward (Str. regexp_string expected_sec) c_code 0 > = 0 )
243+ ) test_cases
244+
245+ let test_sched_switch_bug_regression _ =
246+ (* Regression test: Ensure we don't generate the buggy SEC("raw_tracepoint/sched_sched") *)
247+ let source = " @tracepoint(\" sched/sched_switch\" )
248+ fn sched_switch_handler(ctx: *trace_event_raw_sched_switch) -> i32 {
249+ return 0
250+ }" in
251+ let ast = parse_string source in
252+ let typed_ast = type_check_ast ast in
253+ let symbol_table = Kernelscript.Symbol_table. build_symbol_table typed_ast in
254+ let ir_multi_prog = generate_ir typed_ast symbol_table " test_regression" in
255+ let c_code = generate_c_multi_program ir_multi_prog in
256+
257+ (* Ensure correct SEC() is generated *)
258+ check bool " Should generate correct SEC(raw_tracepoint/sched_switch)" true
259+ (Str. search_forward (Str. regexp_string " SEC(\" raw_tracepoint/sched_switch\" )" ) c_code 0 > = 0 );
260+
261+ (* Ensure buggy SEC() is NOT generated *)
262+ check bool " Should NOT generate buggy SEC(raw_tracepoint/sched_sched)" true
263+ (try
264+ let _ = Str. search_forward (Str. regexp_string " SEC(\" raw_tracepoint/sched_sched\" )" ) c_code 0 in
265+ false (* Found the buggy pattern - test should fail *)
266+ with Not_found ->
267+ true (* Didn't find the buggy pattern - test should pass *)
268+ )
269+
204270(* 4. Code Generation Tests *)
205271let test_raw_tracepoint_section_name_generation _ =
206- (* Test minimal raw tracepoint section name conversion logic *)
272+ (* Test correct raw tracepoint section name generation *)
207273 let source = " @tracepoint(\" sched/sched_switch\" )
208274fn sched_switch_handler(ctx: *trace_event_raw_sched_switch) -> i32 {
209275 return 0
@@ -214,9 +280,9 @@ fn sched_switch_handler(ctx: *trace_event_raw_sched_switch) -> i32 {
214280 let ir_multi_prog = generate_ir typed_ast symbol_table " test_raw_tracepoint" in
215281 let c_code = generate_c_multi_program ir_multi_prog in
216282
217- (* Check that forward slash is converted to underscore in section name *)
218- check bool " Should contain raw_tracepoint section with underscore " true
219- (String. contains c_code ( String. get " SEC(\" raw_tracepoint/sched_sched_switch \" )" 0 ) )
283+ (* Check that the correct SEC() is generated with just the event name *)
284+ check bool " Should contain correct raw_tracepoint/sched_switch section" true
285+ (Str. search_forward ( Str. regexp_string " SEC(\" raw_tracepoint/sched_switch \" )" ) c_code 0 > = 0 )
220286
221287let test_tracepoint_ebpf_codegen _ =
222288 let source = " @tracepoint(\" sched/sched_switch\" )
@@ -230,8 +296,8 @@ fn sched_switch_handler(ctx: *trace_event_raw_sched_switch) -> i32 {
230296 let c_code = generate_c_multi_program ir_multi_prog in
231297
232298 (* Check for tracepoint-specific C code elements *)
233- check bool " Should contain SEC( \" tracepoint \" ) " true
234- (String. contains c_code ( String. get " SEC(\" tracepoint \" )" 0 ) );
299+ check bool " Should contain correct raw_tracepoint SEC" true
300+ (Str. search_forward ( Str. regexp_string " SEC(\" raw_tracepoint/sched_switch \" )" ) c_code 0 > = 0 );
235301 check bool " Should contain function definition" true
236302 (String. contains c_code (String. get " sched_switch_handler" 0 ));
237303 check bool " Should contain struct parameter" true
@@ -342,8 +408,8 @@ fn sys_enter_open_handler(ctx: *trace_event_raw_sys_enter) -> i32 {
342408 let c_code = generate_c_multi_program ir_multi_prog in
343409
344410 (* Comprehensive end-to-end validation *)
345- check bool " Contains tracepoint section" true
346- (String. contains c_code ( String. get " SEC(\" tracepoint \" )" 0 ) );
411+ check bool " Contains correct raw_tracepoint section" true
412+ (Str. search_forward ( Str. regexp_string " SEC(\" raw_tracepoint/sys_enter_open \" )" ) c_code 0 > = 0 );
347413 check bool " Contains function name" true
348414 (String. contains c_code (String. get " sys_enter_open_handler" 0 ));
349415 check bool " Contains context struct" true
@@ -381,6 +447,9 @@ let type_checking_tests = [
381447let ir_generation_tests = [
382448 " tracepoint IR generation" , `Quick , test_tracepoint_ir_generation;
383449 " tracepoint function signature validation" , `Quick , test_tracepoint_function_signature_validation;
450+ " tracepoint target propagation" , `Quick , test_tracepoint_target_propagation;
451+ " multiple tracepoint targets" , `Quick , test_multiple_tracepoint_targets;
452+ " sched_switch bug regression" , `Quick , test_sched_switch_bug_regression;
384453]
385454
386455let code_generation_tests = [
0 commit comments