@@ -21,7 +21,7 @@ def maps_proc(tree, module, chunks):
2121
2222
2323def create_bpf_map (module , map_name , map_params ):
24- """Create a BPF map in the module with the given parameters"""
24+ """Create a BPF map in the module with the given parameters and debug info """
2525
2626 # Create the anonymous struct type for BPF map
2727 map_struct_type = ir .LiteralStructType ([
@@ -35,17 +35,166 @@ def create_bpf_map(module, map_name, map_params):
3535 map_global = ir .GlobalVariable (module , map_struct_type , name = map_name )
3636 map_global .linkage = 'dso_local'
3737 map_global .global_constant = False
38-
39- # Initialize with zeroinitializer (all null pointers)
40- map_global .initializer = ir .Constant (map_struct_type , None ) # type: ignore
41-
38+ map_global .initializer = ir .Constant (map_struct_type , None ) # type: ignore
4239 map_global .section = ".maps"
43- map_global .align = 8 # type: ignore
40+ map_global .align = 8 # type: ignore
41+
42+ # Generate debug info for BTF
43+ create_map_debug_info (module , map_global , map_name , map_params )
4444
4545 print (f"Created BPF map: { map_name } " )
4646 map_sym_tab [map_name ] = map_global
4747 return map_global
4848
49+ def create_map_debug_info (module , map_global , map_name , map_params ):
50+ """Generate debug information metadata for BPF map"""
51+ file_metadata = module
52+ # Get or create compile unit (you may already have this)
53+ if not hasattr (module , '_debug_compile_unit' ):
54+ # Create file metadata
55+ file_metadata = module .add_debug_info ("DIFile" , {
56+ "filename" : "generated.bpf.c" , # Adjust as needed
57+ "directory" : "/generated" , # Adjust as needed
58+ })
59+
60+ # Create compile unit
61+ module ._debug_compile_unit = module .add_debug_info ("DICompileUnit" , {
62+ "language" : 12 , # DW_LANG_C11
63+ "file" : file_metadata ,
64+ "producer" : "PythonBPF DSL Compiler" ,
65+ "isOptimized" : True ,
66+ "runtimeVersion" : 0 ,
67+ "emissionKind" : 1 ,
68+ "splitDebugInlining" : False ,
69+ "nameTableKind" : 0
70+ }, is_distinct = True )
71+
72+ module .add_named_metadata ("llvm.dbg.cu" , module ._debug_compile_unit )
73+
74+ compile_unit = module ._debug_compile_unit
75+
76+ # Create basic type for unsigned int (32-bit)
77+ uint_type = module .add_debug_info ("DIBasicType" , {
78+ "name" : "unsigned int" ,
79+ "size" : 32 ,
80+ "encoding" : 7
81+ })
82+
83+ # Create basic type for unsigned long long (64-bit)
84+ ulong_type = module .add_debug_info ("DIBasicType" , {
85+ "name" : "unsigned long long" ,
86+ "size" : 64 ,
87+ "encoding" : 7 # "DW_ATE_unsigned"
88+ })
89+
90+ # Create array type for map type field (array of 1 unsigned int)
91+ array_subrange = module .add_debug_info ("DISubrange" , {"count" : 1 })
92+ array_type = module .add_debug_info ("DICompositeType" , {
93+ "tag" : 17 , # "DW_TAG_array_type"
94+ "baseType" : uint_type ,
95+ "size" : 32 ,
96+ "elements" : [array_subrange ]
97+ })
98+
99+ # Create pointer types
100+ type_ptr = module .add_debug_info ("DIDerivedType" , {
101+ "tag" : 15 , # DW_TAG_pointer_type
102+ "baseType" : array_type ,
103+ "size" : 64
104+ })
105+
106+ max_entries_ptr = module .add_debug_info ("DIDerivedType" , {
107+ "tag" : 15 , # DW_TAG_pointer_type
108+ "baseType" : array_type ,
109+ "size" : 64
110+ })
111+
112+ key_ptr = module .add_debug_info ("DIDerivedType" , {
113+ "tag" : 15 , # DW_TAG_pointer_type
114+ "baseType" : uint_type , # Adjust based on actual key type
115+ "size" : 64
116+ })
117+
118+ value_ptr = module .add_debug_info ("DIDerivedType" , {
119+ "tag" : 15 , # DW_TAG_pointer_type
120+ "baseType" : ulong_type , # Adjust based on actual value type
121+ "size" : 64
122+ })
123+
124+ # Create struct members
125+ type_member = module .add_debug_info ("DIDerivedType" , {
126+ "tag" : 13 , # "DW_TAG_member"
127+ "name" : "type" ,
128+ "file" : file_metadata , # Use the stored file metadata
129+ "line" : 7 , # You may want to track actual line numbers
130+ "baseType" : type_ptr ,
131+ "size" : 64 ,
132+ "offset" : 0
133+ })
134+
135+ max_entries_member = module .add_debug_info ("DIDerivedType" , {
136+ "tag" : 13 , # DW_TAG_member
137+ "name" : "max_entries" ,
138+ "file" : file_metadata ,
139+ "line" : 8 ,
140+ "baseType" : max_entries_ptr ,
141+ "size" : 64 ,
142+ "offset" : 64
143+ })
144+
145+ key_member = module .add_debug_info ("DIDerivedType" , {
146+ "tag" : 13 , # DW_TAG_member
147+ "name" : "key" ,
148+ "file" : file_metadata ,
149+ "line" : 9 ,
150+ "baseType" : key_ptr ,
151+ "size" : 64 ,
152+ "offset" : 128
153+ })
154+
155+ value_member = module .add_debug_info ("DIDerivedType" , {
156+ "tag" : 13 , # DW_TAG_member
157+ "name" : "value" ,
158+ "file" : file_metadata ,
159+ "line" : 10 ,
160+ "baseType" : value_ptr ,
161+ "size" : 64 ,
162+ "offset" : 192
163+ })
164+
165+ # Create the struct type
166+ struct_type = module .add_debug_info ("DICompositeType" , {
167+ "tag" : 19 , # DW_TAG_structure_type
168+ "name" : "anon" , # Anonymous struct
169+ "file" : file_metadata ,
170+ "line" : 6 , # Adjust line number
171+ "size" : 256 , # 4 * 64-bit pointers
172+ "align" : 64 ,
173+ "elements" : [type_member , max_entries_member , key_member , value_member ]
174+ })
175+
176+ # Create global variable debug info
177+ global_var = module .add_debug_info ("DIGlobalVariable" , {
178+ "name" : map_name ,
179+ "scope" : compile_unit ,
180+ "file" : file_metadata ,
181+ "line" : 11 , # Adjust line number
182+ "type" : struct_type ,
183+ "isLocal" : False ,
184+ "isDefinition" : True
185+ })
186+
187+ # Create global variable expression
188+ global_var_expr = module .add_debug_info ("DIGlobalVariableExpression" , {
189+ "var" : global_var ,
190+ "expr" : module .add_debug_info ("DIExpression" , {})
191+ })
192+
193+ # Attach debug info to the global variable
194+ map_global .set_metadata ("dbg" , global_var_expr )
195+
196+ return global_var_expr
197+
49198
50199def process_hash_map (map_name , rval , module ):
51200 print (f"Creating HashMap map: { map_name } " )
0 commit comments