Skip to content

Commit fefd684

Browse files
committed
finish perf_event_output helper integration
1 parent 79f0949 commit fefd684

2 files changed

Lines changed: 50 additions & 7 deletions

File tree

pythonbpf/bpf_helper_handler.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,37 @@ def bpf_perf_event_output_handler(call, map_ptr, module, builder, func, local_sy
359359
data_type = local_var_metadata[data_name]
360360
if data_type in struct_sym_tab:
361361
struct_info = struct_sym_tab[data_type]
362-
data_size = 0
363-
for field_type in struct_info["type"].elements:
364-
if isinstance(field_type, ir.IntType):
365-
data_size += field_type.width // 8
366-
elif isinstance(field_type, ir.PointerType):
367-
data_size += 8
362+
size_val = ir.Constant(ir.IntType(64), struct_info["size"])
363+
else:
364+
raise ValueError(
365+
f"Struct type {data_type} for variable {data_name} not found in struct symbol table.")
366+
else:
367+
raise ValueError(
368+
f"Metadata for variable {data_name} not found in local variable metadata.")
369+
370+
# BPF_F_CURRENT_CPU is 0
371+
flags_val = ir.Constant(ir.IntType(64), 0)
372+
373+
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
374+
data_void_ptr = builder.bitcast(data_ptr, ir.PointerType())
375+
fn_type = ir.FunctionType(
376+
ir.IntType(64),
377+
[ir.PointerType(ir.IntType(8)), ir.PointerType(), ir.IntType(64),
378+
ir.PointerType(), ir.IntType(64)],
379+
var_arg=False
380+
)
381+
fn_ptr_type = ir.PointerType(fn_type)
382+
383+
# helper id
384+
fn_addr = ir.Constant(ir.IntType(64), 25)
385+
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
386+
387+
result = builder.call(
388+
fn_ptr, [ctx_ptr, map_void_ptr, flags_val, data_void_ptr, size_val], tail=False)
389+
return result
390+
else:
391+
raise NotImplementedError(
392+
"Only simple object names are supported as data in perf event output.")
368393

369394

370395
helper_func_list = {

pythonbpf/structs_pass.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,27 @@ def process_bpf_struct(cls_node, module):
3131
field_names.append(item.target.id)
3232
field_types.append(ctypes_to_ir(item.annotation.id))
3333

34+
curr_offset = 0
35+
for ftype in field_types:
36+
if isinstance(ftype, ir.IntType):
37+
fsize = ftype.width // 8
38+
alignment = fsize
39+
elif isinstance(ftype, ir.PointerType):
40+
fsize = 8
41+
alignment = 8
42+
else:
43+
print(f"Unsupported field type in struct {struct_name}")
44+
return
45+
padding = (alignment - (curr_offset % alignment)) % alignment
46+
curr_offset += padding
47+
curr_offset += fsize
48+
final_padding = (8 - (curr_offset % 8)) % 8
49+
total_size = curr_offset + final_padding
50+
3451
struct_type = ir.LiteralStructType(field_types)
3552
structs_sym_tab[struct_name] = {
3653
"type": struct_type,
37-
"fields": {name: idx for idx, name in enumerate(field_names)}
54+
"fields": {name: idx for idx, name in enumerate(field_names)},
55+
"size": total_size
3856
}
3957
print(f"Created struct {struct_name} with fields {field_names}")

0 commit comments

Comments
 (0)