-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsymbolize.py
54 lines (49 loc) · 1.42 KB
/
symbolize.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# SPDX-License-Identifier: MIT
#
# Copyright 2024 Cisco Systems, Inc. and its affiliates
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
import json
import sys
import numpy as np
f = open("symbol-store.json","r")
symbols = json.load(f)
f.close()
l = [int(x,16) for x in symbols.keys()]
nl = np.array(l, dtype=np.uint64)
addresses = np.sort(nl)
last_addr = 0
last_found = 0
def findSymbol(addr):
global last_addr
global last_found
#high chance that next instruction belongs to the same function
# this "prediction" can fail in certain situations
# but it's inconsequential and greatly speeds things up
if abs(addr-last_addr) < 16: #my approx. at a longest instruction
found = last_found
else:
try:
found = addresses[addresses<=addr][-1]
except:
return None, None
last_found = found
last_addr = addr
return found,symbols["0x%.16x"%found]
#input()
def symbolize(address):
addr , name = findSymbol(address)
if not addr:
return hex(address)
offset = (address - addr)
if offset > 0:
name = "%s+0x%x"%(name,offset)
return name
if __name__ == '__main__':
trace = open(sys.argv[1],"r")
of = open(sys.argv[2],"w")
for l in trace.readlines():
of.write(symbolize(int(l.strip(),16))+"\n")
of.close()