32 lines
1.5 KiB
Python
32 lines
1.5 KiB
Python
# Disassembles a function of an executable built with `sakura build -g`, beside what
|
|||
|
|
# each instruction cost and the line and function it came from.
|
||
|
|
#
|
||
|
|
# python3 annotate.py PROFILE EXECUTABLE FUNCTION
|
||
|
|
#
|
||
|
|
# The profile is one costs.py has read; FUNCTION is a symbol, as `checker::slot`.
|
||
|
|
import os, pickle, re, subprocess, sys
|
||
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||
|
|
import inlined
|
||
|
|
|
||
|
|
profile, executable, name = sys.argv[1], sys.argv[2], sys.argv[3]
|
||
|
|
events, own, calls, counts, found = pickle.load(open(profile + '.pkl', 'rb'))
|
||
|
|
for line in subprocess.run(['nm', '-S', executable], capture_output=True, text=True).stdout.split('\n'):
|
||
|
|
parts = line.split(None, 3)
|
||
|
|
if len(parts) != 4 or parts[3] != name:
|
||
|
|
continue
|
||
|
|
start, size = int(parts[0], 16), int(parts[1], 16)
|
||
|
|
listing = subprocess.run(['objdump', '-d', '--no-show-raw-insn', f'--start-address={start}',
|
||
|
|
f'--stop-address={start + size}', executable], capture_output=True, text=True).stdout
|
||
|
|
mapped = inlined.chains(executable, range(start, start + size))
|
||
|
|
last = None
|
||
|
|
for each in listing.split('\n'):
|
||
|
|
m = re.match(r'\s*([0-9a-f]+):\s*(.*)', each)
|
||
|
|
if not m:
|
||
|
|
continue
|
||
|
|
address = int(m.group(1), 16)
|
||
|
|
chain = inlined.frames(mapped, address)
|
||
|
|
if chain[0] != last:
|
||
|
|
print(f' ; {chain[0][1]} [{chain[0][0]}]')
|
||
|
|
last = chain[0]
|
||
|
|
print(f'{own.get(address, [0])[0]:>12,} {m.group(2)}')
|
||
|
|
break
|