diff --git a/pythonbpf/expr/vmlinux_registry.py b/pythonbpf/expr/vmlinux_registry.py index 5bae334..28e5026 100644 --- a/pythonbpf/expr/vmlinux_registry.py +++ b/pythonbpf/expr/vmlinux_registry.py @@ -39,6 +39,12 @@ class VmlinuxHandlerRegistry: ) return None + @classmethod + def get_struct_debug_info(cls, name): + if cls._handler is None: + return False + return cls._handler.get_struct_debug_info(name) + @classmethod def is_vmlinux_struct(cls, name): """Check if a name refers to a vmlinux struct""" diff --git a/pythonbpf/functions/function_debug_info.py b/pythonbpf/functions/function_debug_info.py index 9e1c31c..b508076 100644 --- a/pythonbpf/functions/function_debug_info.py +++ b/pythonbpf/functions/function_debug_info.py @@ -2,8 +2,20 @@ import ast import llvmlite.ir as ir +from pythonbpf.debuginfo import DebugInfoGenerator +from pythonbpf.expr import VmlinuxHandlerRegistry + def generate_function_debug_info( func_node: ast.FunctionDef, module: ir.Module, func: ir.Function ): + generator = DebugInfoGenerator(module) + leading_argument = func_node.args.args[0] + leading_argument_name = leading_argument.arg + # TODO: add ctypes handling as well here + print(leading_argument.arg, leading_argument.annotation.id) + context_debug_info = VmlinuxHandlerRegistry.get_struct_debug_info( + name=leading_argument.annotation.id + ) + print(context_debug_info) pass diff --git a/pythonbpf/vmlinux_parser/assignment_info.py b/pythonbpf/vmlinux_parser/assignment_info.py index 1092978..7b48854 100644 --- a/pythonbpf/vmlinux_parser/assignment_info.py +++ b/pythonbpf/vmlinux_parser/assignment_info.py @@ -33,3 +33,4 @@ class AssignmentInfo: # Value is a tuple that contains the global variable representing that field # along with all the information about that field as a Field type. members: Optional[Dict[str, tuple[ir.GlobalVariable, Field]]] # For structs. + debug_info: Any diff --git a/pythonbpf/vmlinux_parser/import_detector.py b/pythonbpf/vmlinux_parser/import_detector.py index 7cffae6..d90c478 100644 --- a/pythonbpf/vmlinux_parser/import_detector.py +++ b/pythonbpf/vmlinux_parser/import_detector.py @@ -148,6 +148,7 @@ def process_vmlinux_assign(node, module, assignments: dict[str, AssignmentInfo]) pointer_level=None, signature=None, members=None, + debug_info=None, ) logger.info( f"Added assignment: {target_name} = {node.value.value!r} of type {type(node.value.value)}" diff --git a/pythonbpf/vmlinux_parser/ir_gen/ir_generation.py b/pythonbpf/vmlinux_parser/ir_gen/ir_generation.py index 960671e..14a74ad 100644 --- a/pythonbpf/vmlinux_parser/ir_gen/ir_generation.py +++ b/pythonbpf/vmlinux_parser/ir_gen/ir_generation.py @@ -73,9 +73,8 @@ class IRGenerator: ) # Generate IR first to populate field names - self.generated_debug_info.append( - (struct, self.gen_ir(struct, self.generated_debug_info)) - ) + struct_debug_info = self.gen_ir(struct, self.generated_debug_info) + self.generated_debug_info.append((struct, struct_debug_info)) # Fill the assignments dictionary with struct information if struct.name not in self.assignments: @@ -105,6 +104,7 @@ class IRGenerator: pointer_level=None, signature=None, members=members_dict, + debug_info=struct_debug_info, ) logger.info(f"Added struct assignment info for {struct.name}") diff --git a/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py b/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py index 7b71245..5caa593 100644 --- a/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py +++ b/pythonbpf/vmlinux_parser/vmlinux_exports_handler.py @@ -1,4 +1,6 @@ import logging +from typing import Any + from llvmlite import ir from pythonbpf.local_symbol import LocalSymbol @@ -40,6 +42,15 @@ class VmlinuxHandler: and self.vmlinux_symtab[name].value_type == AssignmentType.CONSTANT ) + def get_struct_debug_info(self, name: str) -> Any: + if ( + name in self.vmlinux_symtab + and self.vmlinux_symtab[name].value_type == AssignmentType.STRUCT + ): + return self.vmlinux_symtab[name].debug_info + else: + raise ValueError(f"{name} is not a vmlinux struct type") + def get_vmlinux_struct_type(self, name): """Check if name is a vmlinux struct type""" if (