-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
201 lines (184 loc) · 4.5 KB
/
parse.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# coding: utf-8
import re
import sys
state = dict()
code = dict()
tMin = 1
t = tMin-1
if len(sys.argv) != 2:
print "usage: script.py trace_filename"
exit(1)
with open(sys.argv[1]) as f:
data = f
for lineId, line in enumerate(data):
if lineId % 65536 == 0:
sys.stdout.write("\rParsing (%dK lines computed)..." % (lineId/1000))
sys.stdout.flush()
line = line.strip()
if line.startswith('trace'):
elems = line.split('/')
if elems[1] == 'state':
tmp = dict()
for i in range(2, len(elems)):
register, _, content = elems[i].partition(':')
tmp[register] = content
state[t] = tmp
elif elems[1] == 'instruction':
code[elems[2]] = elems[3]
t += 1
sys.stdout.write("\nParsing completed\n")
sys.stdout.flush()
assert len(state) > 0
# Get back to a valid final state
while t not in state or state[t] == {}:
t -= 1
tMax = t
print state[tMax]
def htoi(h):
return int(h, 16)
def itoh4(i):
return '%0.4X' % i
pcMin = htoi(min(code.keys()))
pcMax = htoi(max(code.keys()))
def prevInstAddr(addr, count=1):
nextAddr = addr-1
while count>0 and nextAddr>=pcMin and nextAddr<=pcMax:
if itoh4(nextAddr) in code:
addr = nextAddr
count -= 1
nextAddr -= 1
return addr
def nextInstAddr(addr, count=1):
nextAddr = addr+1
while count>0 and nextAddr>=pcMin and nextAddr<=pcMax:
if itoh4(nextAddr) in code:
addr = nextAddr
count -= 1
nextAddr += 1
return addr
print '\n'*80
breakpoints = set()
t = tMin
winPos = None
winSize = 40 # At least 5
winBorderSize = 1
reverse = False
while True:
# --- printing ---
print '\n'*80
print ' | '.join(map('='.join, sorted(state[t].items())))
print
pc = state[t]['pc']
if winPos == None:
winPos = prevInstAddr(htoi(pc), winBorderSize)
else:
winEndPos = nextInstAddr(winPos, (winSize-winBorderSize)-1)
if htoi(pc) < winPos or winEndPos < htoi(pc):
winPos = prevInstAddr(htoi(pc), winBorderSize)
addr = winPos
for i in range(winSize):
if addr <= pcMax:
breakInfo = '●' if itoh4(addr) in breakpoints else ' '
cursorInfo = '>' if itoh4(addr) == pc else ' '
print '%s%s %s: %s' % (breakInfo, cursorInfo, itoh4(addr), code[itoh4(addr)])
if addr != pcMax:
addr = nextInstAddr(addr)
else:
addr = 0xFFFF
else:
print ' XXXX: --'
# --- inputs ---
while True:
userInput = raw_input()
if userInput == 'q':
exit(0)
if userInput == 'start':
t = tMin
break
if userInput == '':
if reverse:
t = max(t-1, tMin)
else:
t = min(t+1, tMax)
break
if userInput in ('n', '+', ''):
t = min(t+1, tMax)
break
if userInput in ('rn', '-'):
t = max(t-1, tMin)
break
if userInput in ('s', '++'):
stoppoint = itoh4(nextInstAddr(htoi(pc)))
t = min(t+1, tMax)
while True:
pc = state[t]['pc']
if pc in breakpoints or t == tMax or pc == stoppoint:
break
t = t+1
break
if userInput in ('rs', '--'):
stoppoint = itoh4(prevInstAddr(htoi(pc)))
t = max(t-1, tMin)
while True:
pc = state[t]['pc']
if pc in breakpoints or t == tMin or pc == stoppoint:
break
t = t-1
break
if userInput in ('r', 'reverse'):
reverse = not reverse
break
m = re.match(r'(?:c|continue)(?:\s+([a-zA-Z0-9]{3,4}))?', userInput)
if m:
stoppoint = None
if m.group(1):
stoppoint = itoh4(htoi(m.group(1)))
t = min(t+1, tMax)
while True:
pc = state[t]['pc']
if pc in breakpoints or t == tMax or pc == stoppoint:
break
t = t+1
break
m = re.match(r'(?:rc|rcontinue)(?:\s+([a-zA-Z0-9]{3,4}))?', userInput)
if m:
stoppoint = None
if m.group(1):
stoppoint = itoh4(htoi(m.group(1)))
t = max(t-1, tMin)
while True:
pc = state[t]['pc']
if pc in breakpoints or t == tMin or pc == stoppoint:
break
t = t-1
break
m = re.match(r'(?:rc|rcontinue)(?:\s+([a-zA-Z0-9]{3,4}))?', userInput)
if m:
stoppoint = None
if m.group(1):
stoppoint = itoh4(htoi(m.group(1)))
t = max(t-1, tMin)
while True:
pc = state[t]['pc']
if pc in breakpoints or t == tMin or pc == stoppoint:
break
t = t-1
break
# Should count call
# if userInput in ('ret'):
# t = min(t+1, tMax)
# while True:
# pc = state[t]['pc']
# if pc in breakpoints or t == tMax or code[pc].startswith('RET'):
# break
# t = t+1
# break
m = re.match(r'(?:b|br|break)\s+([a-zA-Z0-9]{3,4})', userInput)
if m:
breakpoints.add(itoh4(htoi(m.group(1))))
break
m = re.match(r'(?:rb|rbr|rbreak)\s+([a-zA-Z0-9]{3,4})', userInput)
if m:
breakpoints.remove(itoh4(htoi(m.group(1))))
break
print 'Bad command'