Skip to content

Commit daa3982

Browse files
Merge pull request #2 from varun-r-mallya/expt
Debug info addition to map
2 parents 1a94f63 + 12bca3c commit daa3982

File tree

2 files changed

+148
-13
lines changed

2 files changed

+148
-13
lines changed

examples/c-form/ex2.bpf.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ struct {
1010
__type(value, u64);
1111
} last SEC(".maps");
1212

13-
struct {
14-
__uint(type, BPF_MAP_TYPE_HASH);
15-
__uint(max_entries, 1);
16-
__type(key, u64);
17-
__type(value, u64);
18-
} last2 SEC(".maps");
19-
2013
SEC("tracepoint/syscalls/sys_enter_execve")
2114
int hello(struct pt_regs *ctx) {
2215
bpf_printk("Hello, World!\n");

pythonbpf/maps_pass.py

Lines changed: 148 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def maps_proc(tree, module, chunks):
2121

2222

2323
def 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,159 @@ 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": 1, # "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+
# scope field does not appear for some reason
126+
type_member = module.add_debug_info("DIDerivedType", {
127+
"tag": 13, # "DW_TAG_member"
128+
"name": "type",
129+
"file": file_metadata, # Use the stored file metadata
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+
"baseType": max_entries_ptr,
140+
"size": 64,
141+
"offset": 64
142+
})
143+
144+
key_member = module.add_debug_info("DIDerivedType", {
145+
"tag": 13, # DW_TAG_member
146+
"name": "key",
147+
"file": file_metadata,
148+
"baseType": key_ptr,
149+
"size": 64,
150+
"offset": 128
151+
})
152+
153+
value_member = module.add_debug_info("DIDerivedType", {
154+
"tag": 13, # DW_TAG_member
155+
"name": "value",
156+
"file": file_metadata,
157+
"baseType": value_ptr,
158+
"size": 64,
159+
"offset": 192
160+
})
161+
162+
# Create the struct type
163+
struct_type = module.add_debug_info("DICompositeType", {
164+
"tag": 19, # DW_TAG_structure_type
165+
"file": file_metadata,
166+
"size": 256, # 4 * 64-bit pointers
167+
"elements": [type_member, max_entries_member, key_member, value_member]
168+
}, is_distinct=True)
169+
170+
# Create global variable debug info
171+
global_var = module.add_debug_info("DIGlobalVariable", {
172+
"name": map_name,
173+
"scope": compile_unit,
174+
"file": file_metadata,
175+
"type": struct_type,
176+
"isLocal": False,
177+
"isDefinition": True
178+
}, is_distinct=True)
179+
180+
# Create global variable expression
181+
global_var_expr = module.add_debug_info("DIGlobalVariableExpression", {
182+
"var": global_var,
183+
"expr": module.add_debug_info("DIExpression", {})
184+
})
185+
186+
# Attach debug info to the global variable
187+
map_global.set_metadata("dbg", global_var_expr)
188+
189+
return global_var_expr
190+
49191

50192
def process_hash_map(map_name, rval, module):
51193
print(f"Creating HashMap map: {map_name}")

0 commit comments

Comments
 (0)