2017-11-19 16:20:03 +00:00
|
|
|
#!/usr/bin/python2
|
2015-12-15 23:05:18 +00:00
|
|
|
|
2017-11-19 15:53:15 +00:00
|
|
|
import argparse
|
2024-07-09 23:15:47 +00:00
|
|
|
import re
|
|
|
|
import sys
|
2017-11-19 15:53:15 +00:00
|
|
|
|
2024-07-09 23:03:12 +00:00
|
|
|
from . import Junk
|
2024-07-09 23:15:47 +00:00
|
|
|
from .AsciiPainter import AsciiPainter
|
|
|
|
from .Emulator import REGISTERS, Emulator
|
|
|
|
from .TikzPainter import TikzPainter
|
2024-07-09 23:03:12 +00:00
|
|
|
|
2024-07-09 23:15:47 +00:00
|
|
|
ARG_DESC = """
|
2024-07-09 23:03:12 +00:00
|
|
|
A silly x86-64 emulator and stack visualizer.
|
2024-07-09 23:15:47 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
PAINTER_ID_TO_CLASS = {'ascii': AsciiPainter, 'tikz': TikzPainter}
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser(description=ARG_DESC)
|
|
|
|
parser.add_argument(
|
|
|
|
'-i',
|
|
|
|
'--input-file',
|
|
|
|
default='-',
|
|
|
|
help='File to use as input',
|
|
|
|
dest='input-file',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-o',
|
|
|
|
'--output-file',
|
|
|
|
default='-',
|
|
|
|
help='File to use as output',
|
|
|
|
dest='output-file',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-p',
|
|
|
|
'--painter',
|
|
|
|
default='ascii',
|
|
|
|
help='Drawer to use',
|
|
|
|
dest='painter-name',
|
|
|
|
choices=PAINTER_ID_TO_CLASS.keys(),
|
|
|
|
)
|
2018-02-06 19:56:33 +00:00
|
|
|
for register in REGISTERS:
|
2024-07-09 23:15:47 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--' + register[1:],
|
|
|
|
nargs=1,
|
|
|
|
type=int,
|
|
|
|
default=None,
|
|
|
|
dest=register,
|
|
|
|
help='the initial value of the register',
|
|
|
|
)
|
2018-02-06 19:56:33 +00:00
|
|
|
args = vars(parser.parse_args())
|
|
|
|
|
|
|
|
# Determine args #
|
|
|
|
registers_init = {}
|
|
|
|
for register in REGISTERS:
|
2024-07-09 23:15:47 +00:00
|
|
|
registers_init[register[1:]] = Junk.Junk('old ' + register[1:])
|
2018-02-06 19:56:33 +00:00
|
|
|
#
|
|
|
|
registers_init['rip'] = 0
|
|
|
|
registers_init['rsp'] = 0
|
|
|
|
registers_init['rbp'] = Junk.Junk('old bp')
|
|
|
|
#
|
|
|
|
for register in REGISTERS:
|
|
|
|
if args[register]:
|
|
|
|
registers_init[register[1:]] = args[register][0]
|
|
|
|
|
|
|
|
#
|
2024-07-09 23:15:47 +00:00
|
|
|
program = ''
|
2018-02-06 19:56:33 +00:00
|
|
|
if args['input-file'] == '-':
|
|
|
|
program = sys.stdin.read()
|
|
|
|
else:
|
|
|
|
with open(args['input-file']) as file:
|
|
|
|
program = file.read()
|
|
|
|
|
|
|
|
# Output file:
|
|
|
|
output_file = None
|
|
|
|
if args['output-file'] == '-':
|
|
|
|
output_file = sys.stdout
|
|
|
|
else:
|
|
|
|
output_file = open(args['output-file'], 'w')
|
|
|
|
|
|
|
|
# Determine painter class
|
|
|
|
painter_class = PAINTER_ID_TO_CLASS[args['painter-name']]
|
|
|
|
|
|
|
|
## Return
|
|
|
|
return (program, registers_init, output_file, painter_class)
|
2017-11-19 15:53:15 +00:00
|
|
|
|
|
|
|
|
2024-07-09 23:15:47 +00:00
|
|
|
def main():
|
2018-02-06 19:56:33 +00:00
|
|
|
(program, registers_init, output_file, painter_class) = parse_args()
|
2017-11-19 15:53:15 +00:00
|
|
|
|
2018-02-06 19:56:33 +00:00
|
|
|
# Determine registers to display
|
2024-07-09 23:15:47 +00:00
|
|
|
registers_to_draw = ['%rip', '%rbp', '%rsp', '']
|
|
|
|
for match in re.findall(r'%r[a-z0-9]{1,2}', program):
|
2018-02-06 19:56:33 +00:00
|
|
|
if match not in registers_to_draw:
|
|
|
|
registers_to_draw.append(match)
|
2017-11-19 15:53:15 +00:00
|
|
|
|
2018-02-06 19:56:33 +00:00
|
|
|
# Setup emulator and drawer
|
|
|
|
emu = Emulator(program)
|
|
|
|
emu.setRegs(**registers_init)
|
2024-07-09 23:15:47 +00:00
|
|
|
emu.setStack(Junk.Junk('junk...'), Junk.Junk('call eip'))
|
2017-11-19 15:53:15 +00:00
|
|
|
|
2018-02-06 19:56:33 +00:00
|
|
|
painter = painter_class(registers_to_draw)
|
2017-11-19 15:53:15 +00:00
|
|
|
|
2018-02-06 19:56:33 +00:00
|
|
|
# Iteratively draw states.
|
|
|
|
painter.saveState(emu)
|
|
|
|
for line_nr in emu:
|
|
|
|
painter.saveState(emu, line_nr)
|
2017-11-19 15:53:15 +00:00
|
|
|
|
2018-02-06 19:56:33 +00:00
|
|
|
# Display result
|
|
|
|
output_file.write(painter.to_string(emu))
|
|
|
|
output_file.close()
|
2015-12-16 23:52:47 +00:00
|
|
|
|
2017-11-19 15:53:15 +00:00
|
|
|
|
2024-07-09 23:15:47 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|