Skip to content

Commit e773462

Browse files
support binary ops with vmlinux enums
1 parent 5955db8 commit e773462

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

pythonbpf/codegen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def processor(source_code, filename, module):
6767
globals_processing(tree, module)
6868
structs_sym_tab = structs_proc(tree, module, bpf_chunks)
6969
map_sym_tab = maps_proc(tree, module, bpf_chunks)
70-
func_proc(tree, module, bpf_chunks, map_sym_tab, structs_sym_tab, vmlinux_symtab)
70+
func_proc(tree, module, bpf_chunks, map_sym_tab, structs_sym_tab)
7171

7272
globals_list_creation(tree, module)
7373

pythonbpf/expr/expr_pass.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ def get_operand_value(
142142
logger.info(f"var is {var}, base_type is {base_type}, depth is {depth}")
143143
val = deref_to_depth(func, builder, var, depth)
144144
return val
145-
raise ValueError(f"Undefined variable: {operand.id}")
145+
else:
146+
# Check if it's a vmlinux enum/constant
147+
vmlinux_result = VmlinuxHandlerRegistry.handle_name(operand.id)
148+
if vmlinux_result is not None:
149+
val, _ = vmlinux_result
150+
return val
146151
elif isinstance(operand, ast.Constant):
147152
if isinstance(operand.value, int):
148153
cst = ir.Constant(ir.IntType(64), int(operand.value))

tests/passing_tests/vmlinux/simple_struct_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pythonbpf import bpf, section, bpfglobal, compile_to_ir
1+
from pythonbpf import bpf, section, bpfglobal, compile_to_ir, compile
22
from vmlinux import TASK_COMM_LEN # noqa: F401
33
from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401
44

@@ -16,7 +16,8 @@
1616
@bpf
1717
@section("tracepoint/syscalls/sys_enter_execve")
1818
def hello_world(ctx: struct_trace_event_raw_sys_enter) -> c_int64:
19-
print("Hello, World")
19+
a = 2 + TASK_COMM_LEN + TASK_COMM_LEN
20+
print(f"Hello, World{a}")
2021
return c_int64(TASK_COMM_LEN)
2122

2223

@@ -27,3 +28,4 @@ def LICENSE() -> str:
2728

2829

2930
compile_to_ir("simple_struct_test.py", "simple_struct_test.ll")
31+
compile()

0 commit comments

Comments
 (0)