Skip to content

Commit 190baf2

Browse files
support vmlinux enum in printk handler
1 parent c3f3d1e commit 190baf2

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

pythonbpf/helper/printk_formatter.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from llvmlite import ir
55
from pythonbpf.expr import eval_expr, get_base_type_and_depth, deref_to_depth
6+
from pythonbpf.expr.vmlinux_registry import VmlinuxHandlerRegistry
67

78
logger = logging.getLogger(__name__)
89

@@ -108,6 +109,16 @@ def _process_name_in_fval(name_node, fmt_parts, exprs, local_sym_tab):
108109
if local_sym_tab and name_node.id in local_sym_tab:
109110
_, var_type, tmp = local_sym_tab[name_node.id]
110111
_populate_fval(var_type, name_node, fmt_parts, exprs)
112+
else:
113+
# Try to resolve through vmlinux registry if not in local symbol table
114+
result = VmlinuxHandlerRegistry.handle_name(name_node.id)
115+
if result:
116+
val, var_type = result
117+
_populate_fval(var_type, name_node, fmt_parts, exprs)
118+
else:
119+
raise ValueError(
120+
f"Variable '{name_node.id}' not found in symbol table or vmlinux"
121+
)
111122

112123

113124
def _process_attr_in_fval(attr_node, fmt_parts, exprs, local_sym_tab, struct_sym_tab):

tests/passing_tests/vmlinux/simple_struct_test.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from pythonbpf import bpf, section, bpfglobal, compile_to_ir, compile
1+
import logging
2+
3+
from pythonbpf import bpf, section, bpfglobal, compile_to_ir
4+
from pythonbpf import compile # noqa: F401
25
from vmlinux import TASK_COMM_LEN # noqa: F401
36
from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401
47

@@ -17,7 +20,7 @@
1720
@section("tracepoint/syscalls/sys_enter_execve")
1821
def hello_world(ctx: struct_trace_event_raw_sys_enter) -> c_int64:
1922
a = 2 + TASK_COMM_LEN + TASK_COMM_LEN
20-
print(f"Hello, World{a}")
23+
print(f"Hello, World{TASK_COMM_LEN} and {a}")
2124
return c_int64(TASK_COMM_LEN)
2225

2326

@@ -27,5 +30,5 @@ def LICENSE() -> str:
2730
return "GPL"
2831

2932

30-
compile_to_ir("simple_struct_test.py", "simple_struct_test.ll")
33+
compile_to_ir("simple_struct_test.py", "simple_struct_test.ll", loglevel=logging.DEBUG)
3134
compile()

0 commit comments

Comments
 (0)