22from llvmlite import ir
33
44def bpf_printk_emitter (call , module , builder , func ):
5- # Handle print statement
5+ if not hasattr (func , "_fmt_counter" ):
6+ func ._fmt_counter = 0
7+
68 for arg in call .args :
79 if isinstance (arg , ast .Constant ) and isinstance (arg .value , str ):
810 fmt_str = arg .value + "\n " + "\0 "
9- # Create a global variable for the format string
11+ fmt_name = f"{ func .name } ____fmt{ func ._fmt_counter } "
12+ func ._fmt_counter += 1
13+
1014 fmt_gvar = ir .GlobalVariable (
11- module , ir .ArrayType (ir .IntType (8 ), len (fmt_str )), name = f" { func . name } ____fmt" )
15+ module , ir .ArrayType (ir .IntType (8 ), len (fmt_str )), name = fmt_name )
1216 fmt_gvar .global_constant = True
1317 fmt_gvar .initializer = ir .Constant ( # type: ignore
1418 ir .ArrayType (ir .IntType (8 ), len (fmt_str )),
@@ -17,17 +21,13 @@ def bpf_printk_emitter(call, module, builder, func):
1721 fmt_gvar .linkage = "internal"
1822 fmt_gvar .align = 1 # type: ignore
1923
20- # Cast the global variable to i8*
21- fmt_ptr = builder .bitcast (
22- fmt_gvar , ir .PointerType ())
24+ fmt_ptr = builder .bitcast (fmt_gvar , ir .PointerType ())
2325
24- # Call bpf_trace_printk (assumed to be at address 6)
2526 fn_type = ir .FunctionType (ir .IntType (
2627 64 ), [ir .PointerType (), ir .IntType (32 )], var_arg = True )
2728 fn_ptr_type = ir .PointerType (fn_type )
2829 fn_addr = ir .Constant (ir .IntType (64 ), 6 )
2930 fn_ptr = builder .inttoptr (fn_addr , fn_ptr_type )
3031
31- # Call the function
3232 builder .call (fn_ptr , [fmt_ptr , ir .Constant (
33- ir .IntType (32 ), len (fmt_str ))], tail = True )
33+ ir .IntType (32 ), len (fmt_str ))], tail = True )
0 commit comments