From 36c2c0b6958db7928e97824cf5af47a2fda8ae8e Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Sun, 21 Sep 2025 04:48:50 +0530 Subject: [PATCH] Add struct malloc, add struct instantiation to example --- examples/execve5.py | 1 + pythonbpf/functions_pass.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/examples/execve5.py b/examples/execve5.py index 0aff573..ebcf27d 100644 --- a/examples/execve5.py +++ b/examples/execve5.py @@ -19,6 +19,7 @@ def events() -> PerfEventArray: @bpf @section("tracepoint/syscalls/sys_enter_clone") def hello(ctx: c_void_p) -> c_int32: + dataobj = data_t() ts = ktime() process_id = pid() print(f"clone called at {ts} by pid {process_id}") diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index a455685..5baafc0 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -7,6 +7,8 @@ from .type_deducer import ctypes_to_ir from .binary_ops import handle_binary_op from .expr_pass import eval_expr, handle_expr +local_var_metadata = {} + def get_probe_string(func_node): """Extract the probe string from the decorator of the function node.""" @@ -298,6 +300,16 @@ def allocate_mem(module, builder, body, func, ret_type, map_sym_tab, local_sym_t var.align = ir_type.width // 8 print( f"Pre-allocated variable {var_name} for deref") + elif call_type in structs_sym_tab: + struct_info = structs_sym_tab[call_type] + ir_type = struct_info["type"] + var = builder.alloca(ir_type, name=var_name) + + # Null init + builder.store(ir.Constant(ir_type, None), var) + local_var_metadata[var_name] = call_type + print( + f"Pre-allocated variable {var_name} for struct {call_type}") elif isinstance(rval.func, ast.Attribute): ir_type = ir.PointerType(ir.IntType(64)) var = builder.alloca(ir_type, name=var_name)