-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotater.py
executable file
·34 lines (29 loc) · 1.46 KB
/
annotater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
import re, argparse, json
# Regex for the line of instruction:
# 1: 48 89 e5 movq %rsp, %rbp
inst_regex = re.compile(r'\s*([0-9a-f]+):\s+(?:[0-9a-f]{2}(?:\s|$))+(.*)')
def ratio_number(k, n):
return '{:.2f}%'.format(k / n * 100)
def annotate(disasm, perf):
with open(disasm, 'r') as disasm_file, open(perf, 'r') as perf_file, open(perf[:-5] + '.annotated', 'w') as annotated:
json_data = json.load(perf_file)
global_icount = int(json_data['global'][0]['total'])
insn_count = json_data['insn']
icounts = {pc_execution['pc']: int(pc_execution['execution']) for pc_execution in insn_count}
print('Total dynamic icount: ' + format(global_icount, ','), file=annotated)
for line in disasm_file:
line = line.rstrip()
if matches := inst_regex.match(line):
pc = matches.group(1).lstrip('0')
execution = icounts[pc] if pc in icounts else 0
if execution:
line += ' | ' + format(execution, ',') + '({})'.format(ratio_number(execution, global_icount))
print(line, file=annotated)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Annotate disam with icount info.')
parser.add_argument('disasm', help='disasm file generated by [llvm-]objdump')
parser.add_argument('perf', help='SDE perf data of JSON format')
args = parser.parse_args()
annotate(args.disasm, args.perf)