Skip to content

Commit 2386974

Browse files
create debug info to subroutine type
1 parent 8bd210c commit 2386974

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

pythonbpf/debuginfo/debug_info_generator.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,20 @@ def create_global_var_debug_info(
184184
"DIGlobalVariableExpression",
185185
{"var": global_var, "expr": self.module.add_debug_info("DIExpression", {})},
186186
)
187+
188+
def get_int64_type(self):
189+
return self.get_basic_type("long", 64, dc.DW_ATE_signed)
190+
191+
def create_subroutine_type(self, return_type, param_types):
192+
"""
193+
Create a DISubroutineType given return type and list of parameter types.
194+
Equivalent to: !DISubroutineType(types: !{ret, args...})
195+
"""
196+
type_array = [return_type]
197+
if isinstance(param_types, (list, tuple)):
198+
type_array.extend(param_types)
199+
else:
200+
type_array.append(param_types)
201+
return self.module.add_debug_info("DISubroutineType", {"types": type_array})
202+
203+
Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import ast
22

33
import llvmlite.ir as ir
4-
4+
import logging
55
from pythonbpf.debuginfo import DebugInfoGenerator
66
from pythonbpf.expr import VmlinuxHandlerRegistry
7+
import ctypes
8+
9+
logger = logging.getLogger(__name__)
710

811

912
def generate_function_debug_info(
@@ -12,10 +15,42 @@ def generate_function_debug_info(
1215
generator = DebugInfoGenerator(module)
1316
leading_argument = func_node.args.args[0]
1417
leading_argument_name = leading_argument.arg
15-
# TODO: add ctypes handling as well here
16-
print(leading_argument.arg, leading_argument.annotation.id)
17-
context_debug_info = VmlinuxHandlerRegistry.get_struct_debug_info(
18-
name=leading_argument.annotation.id
19-
)
20-
print(context_debug_info)
21-
pass
18+
annotation = leading_argument.annotation
19+
if func_node.returns is None:
20+
# TODO: should check if this logic is consistent with function return type handling elsewhere
21+
return_type = ctypes.c_int64()
22+
elif hasattr(func_node.returns, "id"):
23+
return_type = func_node.returns.id
24+
if return_type == "c_int32":
25+
return_type = generator.get_int32_type()
26+
elif return_type == "c_int64":
27+
return_type = generator.get_int64_type()
28+
elif return_type == "c_uint32":
29+
return_type = generator.get_uint32_type()
30+
elif return_type == "c_uint64":
31+
return_type = generator.get_uint64_type()
32+
else:
33+
logger.warning(
34+
"Return type should be int32, int64, uint32 or uint64 only. Falling back to int64"
35+
)
36+
return_type = generator.get_int64_type()
37+
else:
38+
return_type = ctypes.c_int64()
39+
# context processing
40+
if annotation is None:
41+
logger.warning("Type of context of function not found.")
42+
return
43+
if hasattr(annotation, "id"):
44+
ctype_name = annotation.id
45+
if ctype_name == "c_void_p":
46+
return
47+
elif ctype_name.startswith("ctypes"):
48+
raise SyntaxError(
49+
"The first argument should always be a pointer to a struct or a void pointer"
50+
)
51+
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)
55+
else:
56+
logger.error(f"Invalid annotation type for argument '{leading_argument_name}'")

tests/failing_tests/vmlinux/struct_field_access.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pythonbpf import compile # noqa: F401
55
from vmlinux import TASK_COMM_LEN # noqa: F401
66
from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401
7-
from ctypes import c_int64, c_void_p # noqa: F401
7+
from ctypes import c_int64, c_int32, c_void_p # noqa: F401
88

99

1010
# from vmlinux import struct_uinput_device
@@ -13,10 +13,10 @@
1313

1414
@bpf
1515
@section("tracepoint/syscalls/sys_enter_execve")
16-
def hello_world(ctx: struct_trace_event_raw_sys_enter) -> c_int64:
16+
def hello_world(ctx: struct_trace_event_raw_sys_enter) -> c_int32:
1717
b = ctx.id
1818
print(f"This is context field {b}")
19-
return c_int64(0)
19+
return c_int32(0)
2020

2121

2222
@bpf

0 commit comments

Comments
 (0)