From 7bf6f9c48c8abd9266ffcc2fc811909965b5a5c6 Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Sun, 26 Oct 2025 15:12:36 +0530 Subject: [PATCH] add function_debug_info.py and format --- pythonbpf/assign_pass.py | 6 ++++-- pythonbpf/functions/function_debug_info.py | 9 +++++++++ pythonbpf/functions/functions_pass.py | 7 +++++-- .../vmlinux_parser/vmlinux_exports_handler.py | 19 +++++++++---------- 4 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 pythonbpf/functions/function_debug_info.py diff --git a/pythonbpf/assign_pass.py b/pythonbpf/assign_pass.py index fbec338..a1c2798 100644 --- a/pythonbpf/assign_pass.py +++ b/pythonbpf/assign_pass.py @@ -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 diff --git a/pythonbpf/functions/function_debug_info.py b/pythonbpf/functions/function_debug_info.py new file mode 100644 index 0000000..9e1c31c --- /dev/null +++ b/pythonbpf/functions/function_debug_info.py @@ -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 diff --git a/pythonbpf/functions/functions_pass.py b/pythonbpf/functions/functions_pass.py index e2d49fc..f300e12 100644 --- a/pythonbpf/functions/functions_pass.py +++ b/pythonbpf/functions/functions_pass.py @@ -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): diff --git a/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py b/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py index dc56993..7b71245 100644 --- a/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py +++ b/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py @@ -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):