Skip to content

Commit 737c4d3

Browse files
committed
Support storing and printing string type
1 parent da8a495 commit 737c4d3

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

examples/execve5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def hello(ctx: c_void_p) -> c_int32:
2727
process_id = pid()
2828
dataobj.pid = process_id
2929
dataobj.ts = ts
30-
print(f"clone called at {ts} by pid {process_id}")
30+
print(f"clone called at {ts} by pid {process_id}, str is {strobj}")
3131
events.output(dataobj)
3232
return c_int32(0)
3333

pythonbpf/bpf_helper_handler.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None,
7575
exprs = []
7676

7777
for value in call.args[0].values:
78+
print("Value in f-string:", ast.dump(value))
7879
if isinstance(value, ast.Constant):
7980
if isinstance(value.value, str):
8081
fmt_parts.append(value.value)
@@ -86,10 +87,24 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None,
8687
"Only string and integer constants are supported in f-string.")
8788
elif isinstance(value, ast.FormattedValue):
8889
print("Formatted value:", ast.dump(value))
89-
# Assume int for now
90+
# TODO: Dirty handling here, only checks for int or str
9091
if isinstance(value.value, ast.Name):
91-
fmt_parts.append("%lld")
92-
exprs.append(value.value)
92+
if local_sym_tab and value.value.id in local_sym_tab:
93+
var_ptr, var_type = local_sym_tab[value.value.id]
94+
if isinstance(var_type, ir.IntType):
95+
fmt_parts.append("%lld")
96+
exprs.append(value.value)
97+
elif var_type == ir.PointerType(ir.IntType(8)):
98+
# Case with string
99+
fmt_parts.append("%s")
100+
exprs.append(value.value)
101+
else:
102+
raise NotImplementedError(
103+
"Only integer and pointer types are supported in formatted values.")
104+
print("Formatted value variable:", var_ptr, var_type)
105+
else:
106+
raise ValueError(
107+
f"Variable {value.value.id} not found in local symbol table.")
93108
else:
94109
raise NotImplementedError(
95110
"Only simple variable names are supported in formatted values.")

0 commit comments

Comments
 (0)