1
0

Minor adjustments to ASCII painter

This commit is contained in:
Jon Michael Aanes 2018-02-06 20:20:09 +01:00
parent 0fc2338919
commit 4837fd238a
3 changed files with 24 additions and 6 deletions

View File

@ -42,7 +42,6 @@ class AsciiPainter:
state.append({ 'state': reg_state, 'val': emu.getVal(reg_name) }) state.append({ 'state': reg_state, 'val': emu.getVal(reg_name) })
# Write stack to state # Write stack to state
stack_base_pointer = emu.getVal('%rbp') stack_base_pointer = emu.getVal('%rbp')
print(emu.last_sp, emu.getVal('%rsp'))
base_sp = emu.getVal('%rsp') base_sp = emu.getVal('%rsp')
assert(isinstance(emu.getVal('%rsp'), (int, long))) assert(isinstance(emu.getVal('%rsp'), (int, long)))
state.append({'state':'none', 'val': ''}) state.append({'state':'none', 'val': ''})
@ -52,7 +51,6 @@ class AsciiPainter:
state.append({'state': 'none', 'val': ''}) state.append({'state': 'none', 'val': ''})
for index in range(0, self.max_stack_size): for index in range(0, self.max_stack_size):
stack_i = base_sp + index stack_i = base_sp + index
print(index, stack_i)
if stack_i < emu.max_stack_size: if stack_i < emu.max_stack_size:
state.append({ 'state': emu.regState("m"+str(stack_i)), 'val': emu.stack[stack_i] }) state.append({ 'state': emu.regState("m"+str(stack_i)), 'val': emu.stack[stack_i] })
if stack_base_pointer == index: if stack_base_pointer == index:
@ -61,6 +59,21 @@ class AsciiPainter:
else: else:
state.append({'state':'none', 'val': ''}) state.append({'state':'none', 'val': ''})
def getWidthOfColumns(self, number_states, number_states_pr_block, number_state_vars):
widths = [0 for i in range(0, number_states_pr_block)]
for base_state in range(0, number_states, number_states_pr_block):
for var_i in range(0, number_state_vars):
for state_i in range(0, min(number_states_pr_block, number_states - base_state)):
width = len(str(self.states[base_state+state_i][var_i]['val']))
widths[state_i] = max(widths[state_i], width)
return widths
def getWidthOfNameColumn (self, number_state_vars):
widest = 0
for var_i in range(0, number_state_vars):
widest = max(widest, len(self.nameOfVar(var_i-4)))
return widest
def to_string (self, emu): def to_string (self, emu):
number_states = len(self.states) number_states = len(self.states)
number_state_vars = len(self.states[0]) number_state_vars = len(self.states[0])
@ -70,13 +83,16 @@ class AsciiPainter:
separator = '-' * term_width separator = '-' * term_width
l = [] l = []
name_fmt = '{}{:'+str(self.getWidthOfNameColumn(number_state_vars))+'} '
column_fmt = map(lambda width: '{}{:>'+str(width)+'} ', self.getWidthOfColumns(number_states, number_states_pr_block, number_state_vars))
for base_state in range(0, number_states, number_states_pr_block): for base_state in range(0, number_states, number_states_pr_block):
l.append(separator) l.append(separator)
for var_i in range(0, number_state_vars): for var_i in range(0, number_state_vars):
l.append('{:10}'.format(self.nameOfVar(var_i-4))) l.append(name_fmt.format(NORMAL_COLOR, self.nameOfVar(var_i-4)))
for state_i in range(0, min(number_states_pr_block, number_states - base_state)): for state_i in range(0, min(number_states_pr_block, number_states - base_state)):
var = self.states[base_state+state_i][var_i] var = self.states[base_state+state_i][var_i]
l.append('{}{:>10}{} '.format(REG_STATUS_TO_COLOR[var['state']], var['val'], NORMAL_COLOR)) l.append(column_fmt[state_i].format(REG_STATUS_TO_COLOR[var['state']], var['val'], NORMAL_COLOR))
l.append('\n') l.append('\n')
l.append(separator) l.append(separator)

View File

@ -1,7 +1,8 @@
class JunkComparisonException (BaseException): class JunkComparisonException (BaseException):
pass def __str__ (self):
return "Attempting to perform calculations involving junk values."
class Junk: class Junk:
@ -28,3 +29,4 @@ class Junk:
def __rsub__ (self, other): def __rsub__ (self, other):
return self return self

View File

@ -84,7 +84,7 @@ def main ():
# Setup emulator and drawer # Setup emulator and drawer
emu = Emulator(program) emu = Emulator(program)
emu.setRegs(**registers_init) emu.setRegs(**registers_init)
emu.setStack(Junk.Junk("junk..."), Junk.Junk("calling eip")) emu.setStack(Junk.Junk("junk..."), Junk.Junk("call eip"))
painter = painter_class(registers_to_draw) painter = painter_class(registers_to_draw)