40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
# Maps code addresses of an executable built with `sakura build -g` to the functions
|
|||
|
|
# they were inlined from, through `addr2line -i`, which reads the executable's
|
||
|
|
# `.petal` file. Shared by the scripts beside it.
|
||
|
|
import os, re, subprocess
|
||
|
|
|
||
|
|
# Paths are shown relative to the current directory, and to the standard library
|
||
|
|
# where SAKURA_STD names it.
|
||
|
|
_prefixes = [os.getcwd().rstrip('/') + '/']
|
||
|
|
if os.environ.get('SAKURA_STD'):
|
||
|
|
_prefixes.append(os.environ['SAKURA_STD'].rstrip('/') + '/')
|
||
|
|
|
||
|
|
def _short(location):
|
||
|
|
location = re.sub(r' \(discriminator.*\)', '', location)
|
||
|
|
for i, prefix in enumerate(_prefixes):
|
||
|
|
if location.startswith(prefix):
|
||
|
|
return ('std/' if i > 0 else '') + location[len(prefix):]
|
||
|
|
return location
|
||
|
|
|
||
|
|
def chains(executable, addresses):
|
||
|
|
"""Each address's frames, innermost first: (function, file:line)."""
|
||
|
|
addresses = sorted(set(addresses))
|
||
|
|
out = subprocess.run(['addr2line', '-e', executable, '-a', '-f', '-i'],
|
||
|
|
input='\n'.join(hex(a) for a in addresses),
|
||
|
|
capture_output=True, text=True).stdout.split('\n')
|
||
|
|
found, current, i = {}, None, 0
|
||
|
|
while i < len(out):
|
||
|
|
line = out[i]
|
||
|
|
if line.startswith('0x'):
|
||
|
|
current = int(line, 16)
|
||
|
|
found[current] = []
|
||
|
|
i += 1
|
||
|
|
elif current is not None and i + 1 < len(out):
|
||
|
|
found[current].append((line, _short(out[i + 1])))
|
||
|
|
i += 2
|
||
|
|
else:
|
||
|
|
i += 1
|
||
|
|
return found
|
||
|
|
|
||
|
|
def frames(found, address):
|
||
|
|
return found.get(address) or [('??', '??:0')]
|