11from llvmlite import ir
22import ast
3+
4+ from .bpf_helper_handler import bpf_printk_emitter
35from .type_deducer import ctypes_to_ir
46
57def get_probe_string (func_node ):
@@ -26,35 +28,7 @@ def process_func_body(module, builder, func_node, func, ret_type):
2628 if isinstance (stmt , ast .Expr ) and isinstance (stmt .value , ast .Call ):
2729 call = stmt .value
2830 if isinstance (call .func , ast .Name ) and call .func .id == "print" :
29- # Handle print statement
30- for arg in call .args :
31- if isinstance (arg , ast .Constant ) and isinstance (arg .value , str ):
32- fmt_str = arg .value + "\n " + "\0 "
33- # Create a global variable for the format string
34- fmt_gvar = ir .GlobalVariable (
35- module , ir .ArrayType (ir .IntType (8 ), len (fmt_str )), name = f"{ func .name } ____fmt" )
36- fmt_gvar .global_constant = True
37- fmt_gvar .initializer = ir .Constant ( # type: ignore
38- ir .ArrayType (ir .IntType (8 ), len (fmt_str )),
39- bytearray (fmt_str .encode ("utf8" ))
40- )
41- fmt_gvar .linkage = "internal"
42- fmt_gvar .align = 1 # type: ignore
43-
44- # Cast the global variable to i8*
45- fmt_ptr = builder .bitcast (
46- fmt_gvar , ir .PointerType ())
47-
48- # Call bpf_trace_printk (assumed to be at address 6)
49- fn_type = ir .FunctionType (ir .IntType (
50- 64 ), [ir .PointerType (), ir .IntType (32 )], var_arg = True )
51- fn_ptr_type = ir .PointerType (fn_type )
52- fn_addr = ir .Constant (ir .IntType (64 ), 6 )
53- fn_ptr = builder .inttoptr (fn_addr , fn_ptr_type )
54-
55- # Call the function
56- builder .call (fn_ptr , [fmt_ptr , ir .Constant (
57- ir .IntType (32 ), len (fmt_str ))], tail = True )
31+ bpf_printk_emitter (call , module , builder , func )
5832 elif isinstance (stmt , ast .Return ):
5933 if stmt .value is None :
6034 builder .ret (ir .Constant (ir .IntType (32 ), 0 ))
0 commit comments