Skip to content

Commit 73f7c80

Browse files
add scope field separately to subroutine type remove circular dependency
1 parent 2386974 commit 73f7c80

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

pythonbpf/debuginfo/debug_info_generator.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,37 @@ def create_subroutine_type(self, return_type, param_types):
200200
type_array.append(param_types)
201201
return self.module.add_debug_info("DISubroutineType", {"types": type_array})
202202

203+
def create_local_variable_debug_info(
204+
self, name: str, arg: int, var_type: Any
205+
) -> Any:
206+
"""
207+
Create debug info for a local variable (DILocalVariable) without scope.
208+
Example:
209+
!DILocalVariable(name: "ctx", arg: 1, file: !3, line: 20, type: !7)
210+
"""
211+
return self.module.add_debug_info(
212+
"DILocalVariable",
213+
{
214+
"name": name,
215+
"arg": arg,
216+
"file": self.module._file_metadata,
217+
"type": var_type,
218+
},
219+
)
203220

221+
def add_scope_to_local_variable(self, local_variable_debug_info, scope_value):
222+
"""
223+
Add scope information to an existing local variable debug info object.
224+
"""
225+
#TODO: this is a workaround a flaw in the debug info generation. Fix this if possible in the future.
226+
# We should not be touching llvmlite's internals like this.
227+
if hasattr(local_variable_debug_info, 'operands'):
228+
# LLVM metadata operands is a tuple, so we need to rebuild it
229+
existing_operands = local_variable_debug_info.operands
230+
231+
# Convert tuple to list, add scope, convert back to tuple
232+
operands_list = list(existing_operands)
233+
operands_list.append(('scope', scope_value))
234+
235+
# Reassign the new tuple
236+
local_variable_debug_info.operands = tuple(operands_list)

pythonbpf/functions/function_debug_info.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,18 @@ def generate_function_debug_info(
4949
"The first argument should always be a pointer to a struct or a void pointer"
5050
)
5151
context_debug_info = VmlinuxHandlerRegistry.get_struct_debug_info(annotation.id)
52-
pointer_to_context_debug_info = generator.create_pointer_type(context_debug_info, 64)
53-
subroutine_type = generator.create_subroutine_type(return_type, pointer_to_context_debug_info)
54-
print(subroutine_type)
52+
pointer_to_context_debug_info = generator.create_pointer_type(
53+
context_debug_info, 64
54+
)
55+
subroutine_type = generator.create_subroutine_type(
56+
return_type, pointer_to_context_debug_info
57+
)
58+
context_local_variable = generator.create_local_variable_debug_info(
59+
leading_argument_name, 1, pointer_to_context_debug_info
60+
)
61+
62+
# following is just a test.
63+
generator.add_scope_to_local_variable(context_local_variable, module._file_metadata)
64+
print(context_local_variable)
5565
else:
5666
logger.error(f"Invalid annotation type for argument '{leading_argument_name}'")

0 commit comments

Comments
 (0)