mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
add function_debug_info.py and format
This commit is contained in:
@ -152,12 +152,14 @@ def handle_variable_assignment(
|
||||
if val_type != var_type:
|
||||
if isinstance(val_type, Field):
|
||||
logger.info("Handling assignment to struct field")
|
||||
#TODO: handling only ctype struct fields for now. Handle other stuff too later.
|
||||
# TODO: handling only ctype struct fields for now. Handle other stuff too later.
|
||||
if var_type == ctypes_to_ir(val_type.type.__name__):
|
||||
builder.store(val, var_ptr)
|
||||
logger.info(f"Assigned ctype struct field to {var_name}")
|
||||
return True
|
||||
logger.error(f"Failed to assign ctype struct field to {var_name}: {val_type} != {var_type}")
|
||||
logger.error(
|
||||
f"Failed to assign ctype struct field to {var_name}: {val_type} != {var_type}"
|
||||
)
|
||||
return False
|
||||
elif isinstance(val_type, ir.IntType) and isinstance(var_type, ir.IntType):
|
||||
# Allow implicit int widening
|
||||
|
||||
9
pythonbpf/functions/function_debug_info.py
Normal file
9
pythonbpf/functions/function_debug_info.py
Normal file
@ -0,0 +1,9 @@
|
||||
import ast
|
||||
|
||||
import llvmlite.ir as ir
|
||||
|
||||
|
||||
def generate_function_debug_info(
|
||||
func_node: ast.FunctionDef, module: ir.Module, func: ir.Function
|
||||
):
|
||||
pass
|
||||
@ -23,7 +23,7 @@ from pythonbpf.allocation_pass import (
|
||||
create_targets_and_rvals,
|
||||
LocalSymbol,
|
||||
)
|
||||
|
||||
from .function_debug_info import generate_function_debug_info
|
||||
from .return_utils import handle_none_return, handle_xdp_return, is_xdp_name
|
||||
from .function_metadata import get_probe_string, is_global_function, infer_return_type
|
||||
|
||||
@ -440,7 +440,7 @@ def func_proc(tree, module, chunks, map_sym_tab, structs_sym_tab):
|
||||
func_type = get_probe_string(func_node)
|
||||
logger.info(f"Found probe_string of {func_node.name}: {func_type}")
|
||||
|
||||
process_bpf_chunk(
|
||||
func = process_bpf_chunk(
|
||||
func_node,
|
||||
module,
|
||||
ctypes_to_ir(infer_return_type(func_node)),
|
||||
@ -448,6 +448,9 @@ def func_proc(tree, module, chunks, map_sym_tab, structs_sym_tab):
|
||||
structs_sym_tab,
|
||||
)
|
||||
|
||||
logger.info(f"Generating Debug Info for Function {func_node.name}")
|
||||
generate_function_debug_info(func_node, module, func)
|
||||
|
||||
|
||||
# TODO: WIP, for string assignment to fixed-size arrays
|
||||
def assign_string_to_array(builder, target_array_ptr, source_string_ptr, array_length):
|
||||
|
||||
@ -88,7 +88,9 @@ class VmlinuxHandler:
|
||||
)
|
||||
builder.function.args[0].type = ir.PointerType(ir.IntType(64))
|
||||
print(builder.function.args[0])
|
||||
field_ptr = self.load_ctx_field(builder, builder.function.args[0], globvar_ir)
|
||||
field_ptr = self.load_ctx_field(
|
||||
builder, builder.function.args[0], globvar_ir
|
||||
)
|
||||
print(field_ptr)
|
||||
# Return pointer to field and field type
|
||||
return field_ptr, field_data
|
||||
@ -112,9 +114,9 @@ class VmlinuxHandler:
|
||||
# Load the offset value
|
||||
offset = builder.load(offset_global)
|
||||
|
||||
# Ensure ctx_arg is treated as i8* (byte pointer)
|
||||
# i8_type = ir.IntType(8)
|
||||
i8_ptr_type = ir.PointerType()
|
||||
# # Ensure ctx_arg is treated as i8* (byte pointer)
|
||||
# # i8_type = ir.IntType(8)
|
||||
# i8_ptr_type = ir.PointerType()
|
||||
|
||||
# Cast ctx_arg to i8* if it isn't already
|
||||
# if str(ctx_arg.type) != str(i8_ptr_type):
|
||||
@ -133,7 +135,7 @@ class VmlinuxHandler:
|
||||
module = builder.function.module
|
||||
|
||||
try:
|
||||
passthrough_fn = module.globals.get('llvm.bpf.passthrough.p0.p0')
|
||||
passthrough_fn = module.globals.get("llvm.bpf.passthrough.p0.p0")
|
||||
if passthrough_fn is None:
|
||||
raise KeyError
|
||||
except (KeyError, AttributeError):
|
||||
@ -144,14 +146,12 @@ class VmlinuxHandler:
|
||||
passthrough_fn = ir.Function(
|
||||
module,
|
||||
passthrough_type,
|
||||
name='llvm.bpf.passthrough.p0.p0',
|
||||
name="llvm.bpf.passthrough.p0.p0",
|
||||
)
|
||||
|
||||
# Call passthrough to satisfy BPF verifier
|
||||
verified_ptr = builder.call(
|
||||
passthrough_fn,
|
||||
[ir.Constant(ir.IntType(32), 0), field_ptr],
|
||||
tail=True
|
||||
passthrough_fn, [ir.Constant(ir.IntType(32), 0), field_ptr], tail=True
|
||||
)
|
||||
|
||||
# Bitcast to i64* (assuming field is 64-bit, adjust if needed)
|
||||
@ -163,7 +163,6 @@ class VmlinuxHandler:
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def has_field(self, struct_name, field_name):
|
||||
"""Check if a vmlinux struct has a specific field"""
|
||||
if self.is_vmlinux_struct(struct_name):
|
||||
|
||||
Reference in New Issue
Block a user