2018-07-12 16:57:04 +00:00
|
|
|
import cmd
|
2018-07-24 08:25:15 +00:00
|
|
|
import readline
|
2018-07-12 16:57:04 +00:00
|
|
|
|
|
|
|
from eth_utils import to_hex
|
2018-07-18 14:19:49 +00:00
|
|
|
|
2018-07-13 16:25:22 +00:00
|
|
|
import evm
|
2018-07-12 16:57:04 +00:00
|
|
|
from evm import constants
|
|
|
|
from evm.vm.opcode import as_opcode
|
2018-07-20 17:09:09 +00:00
|
|
|
|
2018-07-12 16:57:04 +00:00
|
|
|
from vyper.opcodes import opcodes as vyper_opcodes
|
2018-07-20 17:09:09 +00:00
|
|
|
from vdb.variables import (
|
|
|
|
parse_global,
|
|
|
|
parse_local
|
|
|
|
)
|
2018-07-12 16:57:04 +00:00
|
|
|
|
|
|
|
commands = [
|
|
|
|
'continue',
|
|
|
|
'locals',
|
|
|
|
'globals'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2018-07-18 14:09:25 +00:00
|
|
|
def history(stdout):
|
2018-07-12 16:57:04 +00:00
|
|
|
for i in range(1, readline.get_current_history_length() + 1):
|
2018-07-18 14:09:25 +00:00
|
|
|
stdout.write("%3d %s" % (i, readline.get_history_item(i)) + '\n')
|
2018-07-12 16:57:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
logo = """
|
|
|
|
__ __
|
|
|
|
\ \ _ / /
|
|
|
|
\ v v / Vyper Debugger
|
|
|
|
\ / 0.0.0b1
|
|
|
|
\ / "help" to get a list of commands
|
|
|
|
v
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class VyperDebugCmd(cmd.Cmd):
|
2018-07-18 13:42:18 +00:00
|
|
|
prompt = '\033[92mvdb\033[0m> '
|
|
|
|
intro = logo
|
|
|
|
|
2018-07-18 14:09:25 +00:00
|
|
|
def __init__(self, computation, line_no=None, source_code=None, source_map=None,
|
|
|
|
stdout=None, stdin=None):
|
2018-07-12 16:57:04 +00:00
|
|
|
if source_map is None:
|
|
|
|
source_map = {}
|
|
|
|
self.computation = computation
|
|
|
|
self.source_code = source_code
|
|
|
|
self.line_no = line_no
|
2018-07-20 17:09:09 +00:00
|
|
|
self.global_vars = source_map.get("globals", {})
|
|
|
|
self.local_vars = source_map.get("locals", {})
|
2018-07-18 13:42:18 +00:00
|
|
|
super().__init__(stdin=stdin, stdout=stdout)
|
|
|
|
if stdout or stdin:
|
|
|
|
self.use_rawinput = False
|
2018-07-12 16:57:04 +00:00
|
|
|
|
|
|
|
def _print_code_position(self):
|
|
|
|
|
|
|
|
if not all((self.source_code, self.line_no)):
|
2018-07-18 13:42:18 +00:00
|
|
|
self.stdout.write('No source loaded' + '\n')
|
2018-07-12 16:57:04 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
lines = self.source_code.splitlines()
|
|
|
|
begin = self.line_no - 1 if self.line_no > 1 else 0
|
|
|
|
end = self.line_no + 1 if self.line_no < len(lines) else self.line_no
|
|
|
|
for idx, line in enumerate(lines[begin - 1:end]):
|
|
|
|
line_number = begin + idx
|
|
|
|
if line_number == self.line_no:
|
2018-07-18 13:42:18 +00:00
|
|
|
self.stdout.write("--> \033[92m{}\033[0m\t{}".format(line_number, line) + '\n')
|
2018-07-12 16:57:04 +00:00
|
|
|
else:
|
2018-07-18 13:42:18 +00:00
|
|
|
self.stdout.write(" \033[92m{}\033[0m\t{}".format(line_number, line) + '\n')
|
2018-07-12 16:57:04 +00:00
|
|
|
|
|
|
|
def preloop(self):
|
|
|
|
super().preloop()
|
|
|
|
self._print_code_position()
|
|
|
|
|
|
|
|
def postloop(self):
|
2018-07-18 13:42:18 +00:00
|
|
|
self.stdout.write('Exiting vdb' + '\n')
|
2018-07-12 16:57:04 +00:00
|
|
|
super().postloop()
|
|
|
|
|
|
|
|
def do_state(self, *args):
|
|
|
|
""" Show current EVM state information. """
|
2018-07-18 13:42:18 +00:00
|
|
|
self.stdout.write('Block Number => {}'.format(self.computation.state.block_number) + '\n')
|
|
|
|
self.stdout.write('Program Counter => {}'.format(self.computation.code.pc) + '\n')
|
|
|
|
self.stdout.write('Memory Size => {}'.format(len(self.computation._memory)) + '\n')
|
|
|
|
self.stdout.write('Gas Remaining => {}'.format(self.computation.get_gas_remaining()) + '\n')
|
2018-07-12 16:57:04 +00:00
|
|
|
|
|
|
|
def do_globals(self, *args):
|
2018-07-20 17:09:09 +00:00
|
|
|
if not self.global_vars:
|
2018-07-18 13:42:18 +00:00
|
|
|
self.stdout.write('No globals found.' + '\n')
|
|
|
|
self.stdout.write('Name\t\tType' + '\n')
|
2018-07-20 17:09:09 +00:00
|
|
|
for name, info in self.global_vars.items():
|
2018-07-18 13:42:18 +00:00
|
|
|
self.stdout.write('self.{}\t\t{}'.format(name, info['type']) + '\n')
|
2018-07-12 16:57:04 +00:00
|
|
|
|
|
|
|
def _get_fn_name_locals(self):
|
2018-07-20 17:09:09 +00:00
|
|
|
for fn_name, info in self.local_vars.items():
|
2018-07-12 16:57:04 +00:00
|
|
|
if info['from_lineno'] < self.line_no < info['to_lineno']:
|
|
|
|
return fn_name, info['variables']
|
|
|
|
return '', {}
|
|
|
|
|
|
|
|
def do_locals(self, *args):
|
2018-07-20 17:09:09 +00:00
|
|
|
if not self.local_vars:
|
2018-07-18 14:09:25 +00:00
|
|
|
self.stdout.write('No locals found.\n')
|
2018-07-12 16:57:04 +00:00
|
|
|
fn_name, variables = self._get_fn_name_locals()
|
2018-07-18 13:42:18 +00:00
|
|
|
self.stdout.write('Function: {}'.format(fn_name) + '\n')
|
|
|
|
self.stdout.write('Name\t\tType' + '\n')
|
2018-07-12 16:57:04 +00:00
|
|
|
for name, info in variables.items():
|
2018-07-18 13:42:18 +00:00
|
|
|
self.stdout.write('{}\t\t{}'.format(name, info['type']) + '\n')
|
2018-07-12 16:57:04 +00:00
|
|
|
|
2018-07-24 08:25:15 +00:00
|
|
|
def completenames(self, text, *ignored):
|
|
|
|
line = text.strip()
|
|
|
|
if 'self.' in line:
|
|
|
|
return [
|
|
|
|
'self.' + x
|
|
|
|
for x in self.globals.keys()
|
|
|
|
if x.startswith(line.split('self.')[1])
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
dotext = 'do_' + text
|
|
|
|
cmds = [a[3:] for a in self.get_names() if a.startswith(dotext)]
|
|
|
|
_, local_vars = self._get_fn_name_locals()
|
|
|
|
return cmds + [x for x in local_vars.keys() if x.startswith(line)]
|
|
|
|
|
2018-07-12 16:57:04 +00:00
|
|
|
def default(self, line):
|
2018-07-24 08:25:15 +00:00
|
|
|
line = line.strip()
|
2018-07-12 16:57:04 +00:00
|
|
|
fn_name, local_variables = self._get_fn_name_locals()
|
|
|
|
|
|
|
|
if line.startswith('self.') and len(line) > 4:
|
2018-07-20 17:09:09 +00:00
|
|
|
parse_global(
|
|
|
|
self.stdout, self.global_vars, self.computation, line
|
|
|
|
)
|
2018-07-12 16:57:04 +00:00
|
|
|
elif line in local_variables:
|
2018-07-20 17:09:09 +00:00
|
|
|
parse_local(
|
2018-07-24 08:25:15 +00:00
|
|
|
self.stdout, local_variables, self.computation, line
|
2018-07-20 17:09:09 +00:00
|
|
|
)
|
2018-07-12 16:57:04 +00:00
|
|
|
else:
|
|
|
|
self.stdout.write('*** Unknown syntax: %s\n' % line)
|
|
|
|
|
|
|
|
def do_stack(self, *args):
|
|
|
|
""" Show contents of the stack """
|
|
|
|
for idx, value in enumerate(self.computation._stack.values):
|
2018-07-18 13:42:18 +00:00
|
|
|
self.stdout.write("{}\t{}".format(idx, to_hex(value)) + '\n')
|
2018-07-12 16:57:04 +00:00
|
|
|
else:
|
2018-07-18 14:09:25 +00:00
|
|
|
self.stdout.write("Stack is empty\n")
|
2018-07-12 16:57:04 +00:00
|
|
|
|
|
|
|
def do_pdb(self, *args):
|
|
|
|
# Break out to pdb for vdb debugging.
|
|
|
|
import pdb; pdb.set_trace() # noqa
|
|
|
|
|
|
|
|
def do_history(self, *args):
|
2018-07-18 14:09:25 +00:00
|
|
|
history(self.stdout)
|
2018-07-12 16:57:04 +00:00
|
|
|
|
|
|
|
def emptyline(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def do_quit(self, *args):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def do_exit(self, *args):
|
|
|
|
""" Exit vdb """
|
|
|
|
return True
|
|
|
|
|
|
|
|
def do_continue(self, *args):
|
|
|
|
""" Exit vdb """
|
|
|
|
return True
|
|
|
|
|
|
|
|
def do_EOF(self, line):
|
|
|
|
""" Exit vdb """
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
original_opcodes = evm.vm.forks.byzantium.computation.ByzantiumComputation.opcodes
|
|
|
|
|
|
|
|
|
2018-07-18 13:42:18 +00:00
|
|
|
def set_evm_opcode_debugger(source_code=None, source_map=None, stdin=None, stdout=None):
|
2018-07-12 16:57:04 +00:00
|
|
|
|
|
|
|
def debug_opcode(computation):
|
|
|
|
line_no = computation.stack_pop(num_items=1, type_hint=constants.UINT256)
|
2018-07-18 13:42:18 +00:00
|
|
|
VyperDebugCmd(
|
|
|
|
computation,
|
|
|
|
line_no=line_no,
|
|
|
|
source_code=source_code,
|
|
|
|
source_map=source_map,
|
|
|
|
stdin=stdin,
|
|
|
|
stdout=stdout
|
|
|
|
).cmdloop()
|
2018-07-12 16:57:04 +00:00
|
|
|
|
|
|
|
opcodes = original_opcodes.copy()
|
|
|
|
opcodes[vyper_opcodes['DEBUG'][0]] = as_opcode(
|
|
|
|
logic_fn=debug_opcode,
|
|
|
|
mnemonic="DEBUG",
|
|
|
|
gas_cost=0
|
|
|
|
)
|
|
|
|
|
|
|
|
setattr(evm.vm.forks.byzantium.computation.ByzantiumComputation, 'opcodes', opcodes)
|
|
|
|
|
|
|
|
|
|
|
|
def set_evm_opcode_pass():
|
|
|
|
|
|
|
|
def debug_opcode(computation):
|
|
|
|
computation.stack_pop(num_items=1, type_hint=constants.UINT256)
|
|
|
|
|
|
|
|
opcodes = original_opcodes.copy()
|
|
|
|
opcodes[vyper_opcodes['DEBUG'][0]] = as_opcode(
|
|
|
|
logic_fn=debug_opcode,
|
|
|
|
mnemonic="DEBUG",
|
|
|
|
gas_cost=0
|
|
|
|
)
|
|
|
|
setattr(evm.vm.forks.byzantium.computation.ByzantiumComputation, 'opcodes', opcodes)
|