11import ast
22
33import llvmlite .ir as ir
4-
4+ import logging
55from pythonbpf .debuginfo import DebugInfoGenerator
66from pythonbpf .expr import VmlinuxHandlerRegistry
7+ import ctypes
8+
9+ logger = logging .getLogger (__name__ )
710
811
912def 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 } '" )
0 commit comments