Skip to content

Commit 36c2c0b

Browse files
committed
Add struct malloc, add struct instantiation to example
1 parent 63c44fa commit 36c2c0b

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

examples/execve5.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def events() -> PerfEventArray:
1919
@bpf
2020
@section("tracepoint/syscalls/sys_enter_clone")
2121
def hello(ctx: c_void_p) -> c_int32:
22+
dataobj = data_t()
2223
ts = ktime()
2324
process_id = pid()
2425
print(f"clone called at {ts} by pid {process_id}")

pythonbpf/functions_pass.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from .binary_ops import handle_binary_op
88
from .expr_pass import eval_expr, handle_expr
99

10+
local_var_metadata = {}
11+
1012

1113
def get_probe_string(func_node):
1214
"""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
298300
var.align = ir_type.width // 8
299301
print(
300302
f"Pre-allocated variable {var_name} for deref")
303+
elif call_type in structs_sym_tab:
304+
struct_info = structs_sym_tab[call_type]
305+
ir_type = struct_info["type"]
306+
var = builder.alloca(ir_type, name=var_name)
307+
308+
# Null init
309+
builder.store(ir.Constant(ir_type, None), var)
310+
local_var_metadata[var_name] = call_type
311+
print(
312+
f"Pre-allocated variable {var_name} for struct {call_type}")
301313
elif isinstance(rval.func, ast.Attribute):
302314
ir_type = ir.PointerType(ir.IntType(64))
303315
var = builder.alloca(ir_type, name=var_name)

0 commit comments

Comments
 (0)