From 3bf85e733efb05f16f11b33ff9185e0d85b3d4b3 Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Mon, 27 Oct 2025 02:59:59 +0530 Subject: [PATCH] add DI subprogram to make CO-RE work fully. --- pythonbpf/debuginfo/debug_info_generator.py | 37 ++++++++++++++++++--- pythonbpf/functions/function_debug_info.py | 12 +++++-- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/pythonbpf/debuginfo/debug_info_generator.py b/pythonbpf/debuginfo/debug_info_generator.py index 7d319ba..01929a2 100644 --- a/pythonbpf/debuginfo/debug_info_generator.py +++ b/pythonbpf/debuginfo/debug_info_generator.py @@ -222,15 +222,44 @@ class DebugInfoGenerator: """ Add scope information to an existing local variable debug info object. """ - #TODO: this is a workaround a flaw in the debug info generation. Fix this if possible in the future. + # TODO: this is a workaround a flaw in the debug info generation. Fix this if possible in the future. # We should not be touching llvmlite's internals like this. - if hasattr(local_variable_debug_info, 'operands'): + if hasattr(local_variable_debug_info, "operands"): # LLVM metadata operands is a tuple, so we need to rebuild it existing_operands = local_variable_debug_info.operands # Convert tuple to list, add scope, convert back to tuple operands_list = list(existing_operands) - operands_list.append(('scope', scope_value)) + operands_list.append(("scope", scope_value)) # Reassign the new tuple - local_variable_debug_info.operands = tuple(operands_list) \ No newline at end of file + local_variable_debug_info.operands = tuple(operands_list) + + def create_subprogram( + self, name: str, subroutine_type: Any, retained_nodes: List[Any] + ) -> Any: + """ + Create a DISubprogram for a function. + + Args: + name: Function name + subroutine_type: DISubroutineType for the function signature + retained_nodes: List of DILocalVariable nodes for function parameters/variables + + Returns: + DISubprogram metadata + """ + return self.module.add_debug_info( + "DISubprogram", + { + "name": name, + "scope": self.module._file_metadata, + "file": self.module._file_metadata, + "type": subroutine_type, + # "flags": dc.DW_FLAG_Prototyped | dc.DW_FLAG_AllCallsDescribed, + # "spFlags": dc.DW_SPFLAG_Definition | dc.DW_SPFLAG_Optimized, + "unit": self.module._debug_compile_unit, + "retainedNodes": retained_nodes, + }, + is_distinct=True, + ) diff --git a/pythonbpf/functions/function_debug_info.py b/pythonbpf/functions/function_debug_info.py index 482f228..f924ebc 100644 --- a/pythonbpf/functions/function_debug_info.py +++ b/pythonbpf/functions/function_debug_info.py @@ -58,9 +58,15 @@ def generate_function_debug_info( context_local_variable = generator.create_local_variable_debug_info( leading_argument_name, 1, pointer_to_context_debug_info ) + retained_nodes = [context_local_variable] + print("function name", func_node.name) + subprogram_debug_info = generator.create_subprogram( + func_node.name, subroutine_type, retained_nodes + ) + generator.add_scope_to_local_variable( + context_local_variable, subprogram_debug_info + ) + func.set_metadata("dbg", subprogram_debug_info) - # following is just a test. - generator.add_scope_to_local_variable(context_local_variable, module._file_metadata) - print(context_local_variable) else: logger.error(f"Invalid annotation type for argument '{leading_argument_name}'")