Skip to content

Commit 60737d9

Browse files
committed
Improve error handling in compile, pass structs_sym_tab and maps_sym_tab to BpfProgram
1 parent fc55b7e commit 60737d9

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

pythonbpf/codegen.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def processor(source_code, filename, module):
5555
func_proc(tree, module, bpf_chunks, map_sym_tab, structs_sym_tab)
5656

5757
globals_list_creation(tree, module)
58+
return structs_sym_tab, map_sym_tab
5859

5960

6061
def compile_to_ir(filename: str, output: str, loglevel=logging.INFO):
@@ -80,7 +81,7 @@ def compile_to_ir(filename: str, output: str, loglevel=logging.INFO):
8081
True,
8182
)
8283

83-
processor(source, filename, module)
84+
structs_sym_tab, maps_sym_tab = processor(source, filename, module)
8485

8586
wchar_size = module.add_metadata(
8687
[
@@ -127,7 +128,7 @@ def compile_to_ir(filename: str, output: str, loglevel=logging.INFO):
127128
f.write(str(module))
128129
f.write("\n")
129130

130-
return output
131+
return output, structs_sym_tab, maps_sym_tab
131132

132133

133134
def _run_llc(ll_file, obj_file):
@@ -165,15 +166,14 @@ def compile(loglevel=logging.INFO) -> bool:
165166
ll_file = Path("/tmp") / caller_file.with_suffix(".ll").name
166167
o_file = caller_file.with_suffix(".o")
167168

168-
success = True
169-
success = (
170-
compile_to_ir(str(caller_file), str(ll_file), loglevel=loglevel) and success
171-
)
169+
compile_to_ir(str(caller_file), str(ll_file), loglevel=loglevel)
172170

173-
success = _run_llc(ll_file, o_file) and success
171+
if not _run_llc(ll_file, o_file):
172+
logger.error("Compilation to object file failed.")
173+
return False
174174

175175
logger.info(f"Object written to {o_file}")
176-
return success
176+
return True
177177

178178

179179
def BPF(loglevel=logging.INFO) -> BpfProgram:
@@ -189,7 +189,11 @@ def BPF(loglevel=logging.INFO) -> BpfProgram:
189189
f.write(src)
190190
f.flush()
191191
source = f.name
192-
compile_to_ir(source, str(inter.name), loglevel=loglevel)
192+
_, structs_sym_tab, maps_sym_tab = compile_to_ir(
193+
source, str(inter.name), loglevel=loglevel
194+
)
193195
_run_llc(str(inter.name), str(obj_file.name))
194196

195-
return BpfProgram(str(obj_file.name))
197+
return BpfProgram(
198+
str(obj_file.name), structs=structs_sym_tab, maps=maps_sym_tab
199+
)

0 commit comments

Comments
 (0)