Skip to content

Commit bbfd17b

Browse files
authored
[emsymbolizer] Make functions resusable from other scripts (NFC) (#25204)
This moves printing from the computing functions to the main function, so that the computing functions can be used from elsewhere.
1 parent ec63152 commit bbfd17b

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

tools/emsymbolizer.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def symbolize_address_symbolizer(module, address, is_dwarf):
8989
# '/abc/def.c:3:5'. If the function or source info is not available, it will
9090
# be printed as '??', in which case we store None. If the line and column info
9191
# is not available, they will be printed as 0, which we store as is.
92+
infos = []
9293
for i in range(0, len(out_lines), 2):
9394
func, loc_str = out_lines[i], out_lines[i + 1]
9495
m = SOURCE_LOC_RE.match(loc_str)
@@ -97,7 +98,8 @@ def symbolize_address_symbolizer(module, address, is_dwarf):
9798
func = None
9899
if source == '??':
99100
source = None
100-
LocationInfo(source, line, column, func).print()
101+
infos.append(LocationInfo(source, line, column, func))
102+
return infos
101103

102104

103105
def get_sourceMappingURL_section(module):
@@ -221,7 +223,7 @@ def symbolize_address_sourcemap(module, address, force_file):
221223
# Print with section offsets to easily compare against dwarf
222224
for k, v in sm.mappings.items():
223225
print(f'{k - csoff:x}: {v}')
224-
sm.lookup(address).print()
226+
return sm.lookup(address)
225227

226228

227229
def symbolize_address_symbolmap(module, address, symbol_map_file):
@@ -246,7 +248,7 @@ def symbolize_address_symbolmap(module, address, symbol_map_file):
246248
print("Address is before the first function")
247249
return
248250

249-
LocationInfo(func=func_names[func_index]).print()
251+
return LocationInfo(func=func_names[func_index])
250252

251253

252254
def main(args):
@@ -257,20 +259,27 @@ def main(args):
257259
if args.addrtype == 'code':
258260
address += get_codesec_offset(module)
259261

262+
def print_loc(loc):
263+
if isinstance(loc, list):
264+
for l in loc:
265+
l.print()
266+
else:
267+
loc.print()
268+
260269
if ((has_debug_line_section(module) and not args.source) or
261270
'dwarf' in args.source):
262-
symbolize_address_symbolizer(module, address, is_dwarf=True)
271+
print_loc(symbolize_address_symbolizer(module, address, is_dwarf=True))
263272
elif ((get_sourceMappingURL_section(module) and not args.source) or
264273
'sourcemap' in args.source):
265-
symbolize_address_sourcemap(module, address, args.file)
274+
print_loc(symbolize_address_sourcemap(module, address, args.file))
266275
elif ((has_name_section(module) and not args.source) or
267276
'names' in args.source):
268-
symbolize_address_symbolizer(module, address, is_dwarf=False)
277+
print_loc(symbolize_address_symbolizer(module, address, is_dwarf=False))
269278
elif ((has_linking_section(module) and not args.source) or
270279
'symtab' in args.source):
271-
symbolize_address_symbolizer(module, address, is_dwarf=False)
280+
print_loc(symbolize_address_symbolizer(module, address, is_dwarf=False))
272281
elif (args.source == 'symbolmap'):
273-
symbolize_address_symbolmap(module, address, args.file)
282+
print_loc(symbolize_address_symbolmap(module, address, args.file))
274283
else:
275284
raise Error('No .debug_line or sourceMappingURL section found in '
276285
f'{module.filename}.'

0 commit comments

Comments
 (0)