-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathanalyze.py
executable file
·535 lines (465 loc) · 17.2 KB
/
analyze.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#!/usr/bin/python3
# Copyright 2020 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# This is a simple tool to parse debug output from the RISC-V assembler and
# simulator to generate information useful for debugging.
#
# Current features:
# * Call stack
#
# To use it, first execute your test with the flags `--print-all-code` and
# `--trace-sim`, dumping the output to a file. Then execute this tool, passing
# it that dump file and it will generate the call stack to stdout.
#
# $ cctest --print-all-code -trace-sim test-interpreter-intrinsics/Call &> out
# $ analyze.py out
import sys
import argparse
import re
import struct
import binascii
class Trampoline:
def __init__(self):
self.start = 0
self.end = 0
def __repr__(self):
return f"Trampoline: {self.start} - {self.end}"
def hasPC(self, pc):
if pc >= self.start and pc <= self.end:
return True
return False
class Function:
def __init__(self, kind):
self.kind = kind
self.name = "unnamed"
self.compiler = ""
self.address = 0
self.trampoline = None
self.start = 0
self.end = 0
def __repr__(self):
return f"Function: {self.name}"
def __str__(self):
return self.name
# Returns a tuple with the first value indicating if the PC is within this
# function and the second indicating if it is in the trampoline
def hasPC(self, pc):
if pc >= self.start and pc <= self.end:
return True, False
elif self.trampoline.hasPC(pc):
return True, True
return False, False
class Instruction:
def __init__(self, line, pc, insn, operands, offset):
self.line = line
self.pc = pc
self.insn = insn
self.operands = operands
self.offset = offset
def __repr__(self):
return f"{hex(self.pc)} {self.insn} {','.join(self.operands)}"
# Create an Instruction from a line of the code dump, or if it
# does not look like an instruction, return None
# A normal instruction looks like:
# 0x55a1aa324b38 178 00008393 mv t2, ra
@classmethod
def fromLine(cls, line):
words = line.split()
if len(words) < 4:
return None
pc = None
offset = None
insnHex = None
try:
pc = int(words[0], 16)
offset = int(words[1], 16)
insnHex = int(words[2], 16)
except ValueError:
pass
if pc is None or offset is None or insnHex is None:
return None
insn = words[3]
operands = []
for idx in range(4, len(words)):
word = words[idx]
parts = re.split('[\(\)]', word)
for part in parts:
if len(part) > 0:
operands.append(part.strip(','))
if not word.endswith(','):
# This is the last operand
break
return cls(line, pc, insn, operands, offset)
class InstructionTrace:
def __init__(self, line, pc, insn, operands, result, count):
self.line = line
self.pc = pc
self.insn = insn
self.operands = operands
self.result = result
self.count = count
def __repr__(self):
return f"{hex(self.pc)}\t{self.insn} {','.join(self.operands)}\t({insn.count})"
# Create an InstructionTrace from a line of the simulator trace, or if it
# does not look like an instruction, return None
# A normal instruction looks like:
# 0x00a0caf43be0 00000e37 lui t3, 0x0 0000000000000000 (71) int64:0 uint64:0
@classmethod
def fromLine(cls, line):
words = line.split()
if len(words) < 3:
return None
if not words[0].startswith('0x') or len(words[1]) != 8:
return None
insn = words[2]
pc = None
insnHex = None
try:
pc = int(words[0], 16)
insnHex = int(words[1], 16)
except ValueError:
pass
countRes = re.search('\(([1-9][0-9]* *)\)', line)
if pc is None or insnHex is None or (countRes is None and
not isControlFlow(insn)):
return None
count = -1
if countRes is not None:
count = int(countRes.group(1))
operands = []
result = None
if insn == 'ret' or insn == 'ecall': # No operands
pass
else:
resIdx = 3
for idx in range(3, len(words)):
resIdx = idx + 1
word = words[idx]
parts = re.split('[\(\)]', word)
for part in parts:
if len(part) > 0:
operands.append(part.strip(','))
# Check for end of operands with special case for the rounding mode
if not word.startswith('[') and not word.endswith(','):
# This is the last operand
break
# Skip over the branch/jump destination
if resIdx + 1 < len(words) and words[resIdx] == '->':
resIdx += 2
# The result is the next word after the operands
if resIdx < len(words) and not isStore(insn) and not isBranch(insn):
result = int(words[resIdx], 16)
if args.target == 'mips' and (insn == 'bal' or insn == 'jalr'):
result = pc + 8
return cls(line, pc, insn, operands, result, count)
def isStore(self):
return isStore(self.insn)
def isBranch(self):
return isBranch(self.insn)
def isJump(self):
return isJump(self.insn)
def isJumpAndLink(self):
return isJumpAndLink(self.insn)
def isCall(self):
if self.insn == 'jalr' and \
((args.target == 'riscv' and
(self.operands[0] == 'ra' or len(self.operands) == 1)) or
(args.target == 'mips' and self.operands[1] == 'ra')):
return True
return False
def isReturn(self):
if self.insn == 'ret' or (self.insn == 'jr' and self.operands[0] == 'ra'):
return True
return False
def jumpTarget(self):
if self.insn == 'j': # j 4
return self.pc + int(self.operands[0])
elif self.insn == 'jal' and len(self.operands) == 2 and self.operands[0] == 'zero_reg':
return self.pc + int(self.operands[1])
elif self.insn == 'jr':
if len(self.operands) == 2: # jr 4(t4)
return int(self.operands[0]) + registers[self.operands[1]]
else: # jr t4
return registers[self.operands[0]]
elif self.insn == 'jalr' and len(self.operands) == 3 and self.operands[0] == 'zero_reg':
return int(self.operands[1]) + registers[self.operands[2]]
else:
return None
def callTarget(self):
if not self.isCall():
return None
if args.target == 'riscv':
# jalr xN
if len(self.operands) == 1:
return registers[self.operands[0]]
offset = None
try:
int(self.operands[0])
except ValueError:
pass
# jalr M(xN)
if offset is not None:
return offset + registers[self.operands[1]]
else: # jalr xM, xN
return registers[self.operands[1]]
elif args.target == 'mips':
return registers[self.operands[0]]
else:
return None
def getDestinationReg(self):
if len(self.operands) == 0:
return None
if self.isStore():
return None
elif self.isBranch():
return None
elif self.isJump():
return None
elif self.isJumpAndLink():
if len(self.operands) == 1: # Implicit ra
return 'ra'
elif args.target == 'riscv':
return self.operands[0]
elif args.target == 'mips':
return self.operands[1]
return self.operands[0]
unknownFunc = Function('unknown')
unknownFunc.name = 'unknown'
hostFunc = Function('host')
hostFunc.name = 'host'
class FunctionCall:
indentLevel = 0
def __init__(self, func, pc, ra, sp=None, fp=None):
self.func = func
self.pc = pc
self.ra = ra
self.sp = sp
self.fp = fp
self.indentLevel = FunctionCall.indentLevel
FunctionCall.indentLevel = FunctionCall.indentLevel + 1
def returnFrom(self, ra=None, sp=None, fp=None):
if ra is not None and ra != self.ra:
print(
f"### WARNING: Expected return address = {self.ra}, actual = {ra}")
if sp is not None and self.sp is not None and sp != self.sp:
print(
f"### WARNING: Expected stack pointer = {self.sp}, actual = {sp}")
if fp is not None and self.fp is not None and fp != self.fp:
print(
f"### WARNING: Expected frame pointer = {self.fp}, actual = {fp}")
FunctionCall.indentLevel = FunctionCall.indentLevel - 1
def isStore(s):
if s in ["sd", "sw", "sh", "sb", "fsd", "fsw"]:
return True
return False
def isBranch(s):
if s[0] == 'b':
return True
return False
def isJump(s):
if s == "j" or s == "jr":
return True
return False
def isJumpAndLink(s):
if s[0:3] == "jal" or s == "bal":
return True
return False
def isControlFlow(s):
return isBranch(s) or isJump(s) or isJumpAndLink(s) or s == 'ecall'
def printArgs(indentLevel=0):
print(
f"### {' ' * call.indentLevel} sp={hex(registers['sp'])} fp={hex(registers['fp'])}")
print(f"### {' ' * call.indentLevel} Args:", end='')
for i in range(0, 8):
r = f"a{i}"
val = '?'
if r in registers:
val = hex(registers[r])
print(f" {val}", end='')
print()
if args.fp:
print(f"### {' ' * call.indentLevel} FPArgs:", end='')
if args.target == 'riscv':
prefix = "fa"
start = 0
else: # mips
prefix="f"
start = 12
for i in range(0, 8):
r = f"{prefix}{start + i}"
val = '?'
fpval = '?'
if r in registers:
val = format(registers[r], '016x')
fpval = struct.unpack('>d', binascii.unhexlify(val))[0]
print(f" {fpval} ({val})", end='')
print()
def printReturnValues(indentLevel=0):
print(f"### {' ' * call.indentLevel} Returned: i: ", end='')
prefix = 'a' if args.target == 'riscv' else 'v'
for i in range(0, 2):
r = f"{prefix}{i}"
val = '?'
if r in registers:
val = format(registers[r], '016x')
print(f" {val}", end='')
if args.fp:
val = '?'
fpval = '?'
r = 'fa0' if args.target == 'riscv' else 'f0'
if r in registers:
val = format(registers[r], '016x')
fpval = struct.unpack('>d', binascii.unhexlify(val))[0]
print(f", f: {fpval} ({val})")
else:
print()
parser = argparse.ArgumentParser()
parser.add_argument('--inline', action='store_true', default=False,
dest='inline', help='Print comments inline with trace')
parser.add_argument('--target', default='riscv',
help='Specify the target architecture')
parser.add_argument('--print-host-calls', action='store_true', default=False,
dest='print_host_calls', help='Print info about calls to host functions')
parser.add_argument('--fp', action='store_true', default=False,
dest='fp', help='Print floating point arguments and return values')
parser.add_argument('logfile', nargs=1)
args = parser.parse_args()
tracefile = open(args.logfile[0])
functions = {}
current = None
inTrampoline = False
inBody = False
inSafePoints = False
skip = 0
inTraceSim = False
callStack = []
registers = {}
nextLine = tracefile.readline()
while nextLine:
line = nextLine
nextLine = tracefile.readline()
if skip > 0:
skip = skip - 1
continue
words = line.split()
if len(words) == 0:
continue
if words[0] == "kind":
# Start a new function
current = Function(words[2])
elif words[0] == "kind:":
# Start a new function
current = Function(words[1:])
elif words[0] == "name":
current.name = words[2]
elif words[0] == "compiler":
current.compiler = words[2]
elif words[0] == "compiler:":
current.compiler = words[1]
elif words[0] == "address":
current.address = words[2]
elif words[0] == "Trampoline":
current.trampoline = Trampoline()
inTrampoline = True
elif words[0] == "Instructions":
inTrampoline = False
inBody = True
elif words[0] == "Safepoints" or words[0] == "Deoptimization":
inBody = False
inSafePoints = True
elif words[0] == "RelocInfo":
inSafePoints = False
# End this function
inBody = False
functions[current.start] = current
if current.trampoline is not None:
functions[current.trampoline.start] = current
current = None
# skip the next line
skip = 1
elif words[0] == "---":
inTraceSim = False
elif words[0] == "CallImpl:":
# Record registers from the output
registers['a0'] = int(words[11], 16)
registers['a1'] = int(words[15], 16)
registers['a2'] = int(words[19], 16)
registers['a3'] = int(words[23], 16)
registers['a4'] = int(words[27], 16)
registers['a5'] = int(words[31], 16)
addr = int(words[7], 16)
func = functions[addr]
call = FunctionCall(func, addr, 0xFFFFFFFFFFFFFFFE)
callStack.append(call)
print(f"### Start in {func.name}")
inTraceSim = True
else:
if inSafePoints:
continue
if not inTraceSim:
insn = Instruction.fromLine(line)
if insn is not None:
if insn.offset == 0:
if inTrampoline:
current.trampoline.start = insn.pc
elif inBody:
current.start = insn.pc
else:
if inTrampoline:
current.trampoline.end = insn.pc
elif inBody:
current.end = insn.pc
else:
insn = InstructionTrace.fromLine(line)
if insn is not None:
dest = insn.getDestinationReg()
if dest is not None and insn.result is not None:
registers[dest] = insn.result
if insn.isCall():
addr = insn.callTarget()
if addr in functions.keys():
func = functions[addr]
call = FunctionCall(func, addr, insn.result,
registers['sp'], registers['fp'])
callStack.append(call)
print(
f"### {' ' * call.indentLevel}Call {func.name} {insn.count}")
printArgs(call.indentLevel)
else:
func = unknownFunc
if nextLine and nextLine.startswith("Call to host function"):
if not args.print_host_calls:
continue
func = hostFunc
call = FunctionCall(func, addr, insn.result,
registers['sp'], registers['fp'])
callStack.append(call)
print(
f"### {' ' * call.indentLevel}Call {func.name} {insn.count}")
printArgs(call.indentLevel)
if insn.isReturn():
call = callStack.pop()
print(
f"### {' ' * call.indentLevel}Return from {call.func.name} {insn.count}")
printReturnValues(call.indentLevel)
call.returnFrom(registers['ra'],
registers['sp'], registers['fp'])
if insn.jumpTarget() in functions:
func = functions[insn.jumpTarget()]
print(
f"### {' ' * call.indentLevel}Jump to {func.name} {insn.count}")
printArgs(call.indentLevel)
if args.inline:
print(line, end='')
if words[0] == "Returned" and args.print_host_calls:
prefix = 'a' if args.target == 'riscv' else 'v'
registers[f'{prefix}1'] = int(words[1], 16)
registers[f'{prefix}0'] = int(words[3], 16)
call = callStack.pop()
print(
f"### {' ' * call.indentLevel}Return from {call.func.name}")
printReturnValues(call.indentLevel)
call.returnFrom()
line = nextLine
tracefile.close()