From 737c4d30391d91f882091b0aeda42792b2a73a80 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 26 Sep 2025 04:17:29 +0530 Subject: [PATCH] Support storing and printing string type --- examples/execve5.py | 2 +- pythonbpf/bpf_helper_handler.py | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/examples/execve5.py b/examples/execve5.py index f716420..691f008 100644 --- a/examples/execve5.py +++ b/examples/execve5.py @@ -27,7 +27,7 @@ def hello(ctx: c_void_p) -> c_int32: process_id = pid() dataobj.pid = process_id dataobj.ts = ts - print(f"clone called at {ts} by pid {process_id}") + print(f"clone called at {ts} by pid {process_id}, str is {strobj}") events.output(dataobj) return c_int32(0) diff --git a/pythonbpf/bpf_helper_handler.py b/pythonbpf/bpf_helper_handler.py index ae4f97b..34b1f04 100644 --- a/pythonbpf/bpf_helper_handler.py +++ b/pythonbpf/bpf_helper_handler.py @@ -75,6 +75,7 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None, exprs = [] for value in call.args[0].values: + print("Value in f-string:", ast.dump(value)) if isinstance(value, ast.Constant): if isinstance(value.value, str): fmt_parts.append(value.value) @@ -86,10 +87,24 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None, "Only string and integer constants are supported in f-string.") elif isinstance(value, ast.FormattedValue): print("Formatted value:", ast.dump(value)) - # Assume int for now + # TODO: Dirty handling here, only checks for int or str if isinstance(value.value, ast.Name): - fmt_parts.append("%lld") - exprs.append(value.value) + if local_sym_tab and value.value.id in local_sym_tab: + var_ptr, var_type = local_sym_tab[value.value.id] + if isinstance(var_type, ir.IntType): + fmt_parts.append("%lld") + exprs.append(value.value) + elif var_type == ir.PointerType(ir.IntType(8)): + # Case with string + fmt_parts.append("%s") + exprs.append(value.value) + else: + raise NotImplementedError( + "Only integer and pointer types are supported in formatted values.") + print("Formatted value variable:", var_ptr, var_type) + else: + raise ValueError( + f"Variable {value.value.id} not found in local symbol table.") else: raise NotImplementedError( "Only simple variable names are supported in formatted values.")