Skip to content

Commit d52b9ce

Browse files
separate out bpf_printk IR generator
1 parent 1118e4f commit d52b9ce

2 files changed

Lines changed: 36 additions & 29 deletions

File tree

pythonbpf/bpf_helper_handler.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import ast
2+
from llvmlite import ir
3+
4+
def bpf_printk_emitter(call, module, builder, func):
5+
# Handle print statement
6+
for arg in call.args:
7+
if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
8+
fmt_str = arg.value + "\n" + "\0"
9+
# Create a global variable for the format string
10+
fmt_gvar = ir.GlobalVariable(
11+
module, ir.ArrayType(ir.IntType(8), len(fmt_str)), name=f"{func.name}____fmt")
12+
fmt_gvar.global_constant = True
13+
fmt_gvar.initializer = ir.Constant( # type: ignore
14+
ir.ArrayType(ir.IntType(8), len(fmt_str)),
15+
bytearray(fmt_str.encode("utf8"))
16+
)
17+
fmt_gvar.linkage = "internal"
18+
fmt_gvar.align = 1 # type: ignore
19+
20+
# Cast the global variable to i8*
21+
fmt_ptr = builder.bitcast(
22+
fmt_gvar, ir.PointerType())
23+
24+
# Call bpf_trace_printk (assumed to be at address 6)
25+
fn_type = ir.FunctionType(ir.IntType(
26+
64), [ir.PointerType(), ir.IntType(32)], var_arg=True)
27+
fn_ptr_type = ir.PointerType(fn_type)
28+
fn_addr = ir.Constant(ir.IntType(64), 6)
29+
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
30+
31+
# Call the function
32+
builder.call(fn_ptr, [fmt_ptr, ir.Constant(
33+
ir.IntType(32), len(fmt_str))], tail=True)

pythonbpf/functions_pass.py

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from llvmlite import ir
22
import ast
3+
4+
from .bpf_helper_handler import bpf_printk_emitter
35
from .type_deducer import ctypes_to_ir
46

57
def get_probe_string(func_node):
@@ -26,35 +28,7 @@ def process_func_body(module, builder, func_node, func, ret_type):
2628
if isinstance(stmt, ast.Expr) and isinstance(stmt.value, ast.Call):
2729
call = stmt.value
2830
if isinstance(call.func, ast.Name) and call.func.id == "print":
29-
# Handle print statement
30-
for arg in call.args:
31-
if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
32-
fmt_str = arg.value + "\n" + "\0"
33-
# Create a global variable for the format string
34-
fmt_gvar = ir.GlobalVariable(
35-
module, ir.ArrayType(ir.IntType(8), len(fmt_str)), name=f"{func.name}____fmt")
36-
fmt_gvar.global_constant = True
37-
fmt_gvar.initializer = ir.Constant( # type: ignore
38-
ir.ArrayType(ir.IntType(8), len(fmt_str)),
39-
bytearray(fmt_str.encode("utf8"))
40-
)
41-
fmt_gvar.linkage = "internal"
42-
fmt_gvar.align = 1 # type: ignore
43-
44-
# Cast the global variable to i8*
45-
fmt_ptr = builder.bitcast(
46-
fmt_gvar, ir.PointerType())
47-
48-
# Call bpf_trace_printk (assumed to be at address 6)
49-
fn_type = ir.FunctionType(ir.IntType(
50-
64), [ir.PointerType(), ir.IntType(32)], var_arg=True)
51-
fn_ptr_type = ir.PointerType(fn_type)
52-
fn_addr = ir.Constant(ir.IntType(64), 6)
53-
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
54-
55-
# Call the function
56-
builder.call(fn_ptr, [fmt_ptr, ir.Constant(
57-
ir.IntType(32), len(fmt_str))], tail=True)
31+
bpf_printk_emitter(call, module, builder, func)
5832
elif isinstance(stmt, ast.Return):
5933
if stmt.value is None:
6034
builder.ret(ir.Constant(ir.IntType(32), 0))

0 commit comments

Comments
 (0)