<s> from __future__ import print_function <EOL> import re <EOL> import shlex <EOL> import struct <EOL> import string <EOL> import logging <EOL> import threading <EOL> from voltron . api import * <EOL> from voltron . plugin import * <EOL> from voltron . dbg import * <EOL> try : <EOL> import vtrace <EOL> import vdb <EOL> import envi <EOL> HAVE_VDB = True <EOL> except ImportError : <EOL> HAVE_VDB = False <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> if HAVE_VDB : <EOL> class NotAStringError ( Exception ) : <EOL> pass <EOL> class FailedToReadMemoryError ( Exception ) : <EOL> pass <EOL> class VDBAdaptor ( DebuggerAdaptor ) : <EOL> """<STR_LIT>""" <EOL> archs = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> sizes = { <EOL> '<STR_LIT>' : <NUM_LIT:4> , <EOL> '<STR_LIT>' : <NUM_LIT:8> , <EOL> '<STR_LIT>' : <NUM_LIT:4> <EOL> } <EOL> reg_names = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> } <EOL> def __init__ ( self , host , * args , ** kwargs ) : <EOL> self . listeners = [ ] <EOL> self . host_lock = threading . RLock ( ) <EOL> self . _vdb = host <EOL> self . _vtrace = vtrace <EOL> def version ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" <EOL> def _target ( self , target_id = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> d = { } <EOL> d [ "<STR_LIT:id>" ] = <NUM_LIT:0> <EOL> d [ "<STR_LIT:state>" ] = self . _state ( ) <EOL> d [ "<STR_LIT:file>" ] = self . _vdb . getTrace ( ) . metadata [ '<STR_LIT>' ] <EOL> d [ "<STR_LIT>" ] = self . get_arch ( ) <EOL> d [ '<STR_LIT>' ] = self . get_byte_order ( ) <EOL> d [ '<STR_LIT>' ] = self . get_addr_size ( ) <EOL> return d <EOL> @ lock_host <EOL> def target ( self , target_id = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _target ( ) <EOL> @ lock_host <EOL> def targets ( self , target_ids = None ) : <EOL> """<STR_LIT>""" <EOL> return [ self . _target ( ) ] <EOL> @ validate_target <EOL> @ lock_host <EOL> def state ( self , target_id = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _state ( ) <EOL> @ validate_busy <EOL> @ validate_target <EOL> @ lock_host <EOL> def registers ( self , target_id = <NUM_LIT:0> , thread_id = None , registers = [ ] ) : <EOL> """<STR_LIT>""" <EOL> arch = self . get_arch ( ) <EOL> if arch in self . reg_names : <EOL> if '<STR_LIT>' in registers : <EOL> registers . remove ( '<STR_LIT>' ) <EOL> registers . append ( self . reg_names [ arch ] [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in registers : <EOL> registers . remove ( '<STR_LIT>' ) <EOL> registers . append ( self . reg_names [ arch ] [ '<STR_LIT>' ] ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" . format ( target [ '<STR_LIT>' ] ) ) <EOL> if registers != [ ] : <EOL> regs = { } <EOL> for reg in registers : <EOL> regs [ reg ] = self . get_register ( reg ) <EOL> else : <EOL> log . debug ( '<STR_LIT>' . format ( arch ) ) <EOL> if arch == "<STR_LIT>" : <EOL> regs = self . get_registers_x86_64 ( ) <EOL> elif arch == "<STR_LIT>" : <EOL> regs = self . get_registers_x86 ( ) <EOL> elif arch == "<STR_LIT>" : <EOL> regs = self . get_registers_arm ( ) <EOL> else : <EOL> raise UnknownArchitectureException ( ) <EOL> return regs <EOL> @ validate_busy <EOL> @ validate_target <EOL> @ lock_host <EOL> def stack_pointer ( self , target_id = <NUM_LIT:0> , thread_id = None ) : <EOL> """<STR_LIT>""" <EOL> arch = self . get_arch ( ) <EOL> if arch in self . reg_names : <EOL> sp_name = self . reg_names [ arch ] [ '<STR_LIT>' ] <EOL> sp = self . get_register ( sp_name ) <EOL> else : <EOL> raise UnknownArchitectureException ( ) <EOL> return sp_name , sp <EOL> @ validate_busy <EOL> @ validate_target <EOL> @ lock_host <EOL> def program_counter ( self , target_id = <NUM_LIT:0> , thread_id = None ) : <EOL> """<STR_LIT>""" <EOL> arch = self . get_arch ( ) <EOL> if arch in self . reg_names : <EOL> pc_name = self . reg_names [ arch ] [ '<STR_LIT>' ] <EOL> pc = self . get_register ( pc_name ) <EOL> else : <EOL> raise UnknownArchitectureException ( ) <EOL> return pc_name , pc <EOL> @ validate_busy <EOL> @ validate_target <EOL> @ lock_host <EOL> def memory ( self , address , length , target_id = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> log . debug ( '<STR_LIT>' . format ( length , address ) ) <EOL> t = self . _vdb . getTrace ( ) <EOL> try : <EOL> return t . readMemory ( address , length ) <EOL> except : <EOL> raise FailedToReadMemoryError ( ) <EOL> @ validate_busy <EOL> @ validate_target <EOL> @ lock_host <EOL> def stack ( self , length , target_id = <NUM_LIT:0> , thread_id = None ) : <EOL> """<STR_LIT>""" <EOL> sp_name , sp = self . stack_pointer ( target_id = target_id , thread_id = thread_id ) <EOL> memory = self . memory ( sp , length , target_id = target_id ) <EOL> return memory <EOL> def _get_n_opcodes_length ( self , address , count ) : <EOL> """<STR_LIT>""" <EOL> length = <NUM_LIT:0> <EOL> t = self . _vdb . getTrace ( ) <EOL> arch = self . _vdb . arch . getArchId ( ) <EOL> for i in xrange ( count ) : <EOL> op = t . parseOpcode ( address + length , arch = arch ) <EOL> length += op . size <EOL> return length <EOL> @ validate_busy <EOL> @ validate_target <EOL> @ lock_host <EOL> def disassemble ( self , target_id = <NUM_LIT:0> , address = None , count = <NUM_LIT:16> ) : <EOL> """<STR_LIT>""" <EOL> if address == None : <EOL> pc_name , address = self . program_counter ( target_id = target_id ) <EOL> length = self . _get_n_opcodes_length ( address , count ) <EOL> can = envi . memcanvas . StringMemoryCanvas ( self . _vdb . memobj , self . _vdb . symobj ) <EOL> can . renderMemory ( address , length , self . _vdb . opcoderend ) <EOL> return str ( can ) <EOL> def _get_ascii_string ( self , address , min_length = <NUM_LIT:4> , max_length = <NUM_LIT:32> ) : <EOL> """<STR_LIT>""" <EOL> cs = [ ] <EOL> for i in xrange ( max_length ) : <EOL> try : <EOL> c = self . memory ( address + i , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> except FailedToReadMemoryError : <EOL> break <EOL> if ord ( c ) == <NUM_LIT:0> : <EOL> break <EOL> elif c not in string . printable : <EOL> break <EOL> else : <EOL> cs . append ( c ) <EOL> if len ( cs ) >= min_length : <EOL> return "<STR_LIT>" . join ( cs ) <EOL> else : <EOL> raise NotAStringError ( ) <EOL> def _get_unicode_string ( self , address , min_length = <NUM_LIT:4> , max_length = <NUM_LIT:32> ) : <EOL> """<STR_LIT>""" <EOL> cs = [ ] <EOL> for i in xrange ( max_length ) : <EOL> try : <EOL> b = self . memory ( address + ( i * <NUM_LIT:2> ) , <NUM_LIT:2> ) <EOL> except FailedToReadMemoryError : <EOL> break <EOL> if ord ( b [ <NUM_LIT:1> ] ) != <NUM_LIT:0> : <EOL> break <EOL> c = b [ <NUM_LIT:0> ] <EOL> if ord ( c ) == <NUM_LIT:0> : <EOL> break <EOL> elif c not in string . printable : <EOL> break <EOL> else : <EOL> cs . append ( c ) <EOL> if len ( cs ) >= min_length : <EOL> return "<STR_LIT>" . join ( cs ) <EOL> else : <EOL> raise NotAStringError ( ) <EOL> @ validate_busy <EOL> @ validate_target <EOL> @ lock_host <EOL> def dereference ( self , pointer , target_id = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> fmt = ( '<STR_LIT:<>' if self . get_byte_order ( ) == '<STR_LIT>' else '<STR_LIT:>>' ) + { <NUM_LIT:2> : '<STR_LIT:H>' , <NUM_LIT:4> : '<STR_LIT:L>' , <NUM_LIT:8> : '<STR_LIT>' } [ self . get_addr_size ( ) ] <EOL> addr = pointer <EOL> chain = [ ] <EOL> while True : <EOL> try : <EOL> mem = self . memory ( addr , self . get_addr_size ( ) ) <EOL> except FailedToReadMemoryError : <EOL> break <EOL> except Exception as e : <EOL> print ( e ) <EOL> print ( type ( e ) ) <EOL> print ( e . __class__ . __name__ ) <EOL> break <EOL> log . debug ( "<STR_LIT>" . format ( mem ) ) <EOL> ( ptr , ) = struct . unpack ( fmt , mem ) <EOL> if ptr in chain : <EOL> break <EOL> chain . append ( ( '<STR_LIT>' , addr ) ) <EOL> addr = ptr <EOL> p , addr = chain [ - <NUM_LIT:1> ] <EOL> output = self . _vdb . reprPointer ( addr ) <EOL> if "<STR_LIT>" not in output : <EOL> chain . append ( ( '<STR_LIT>' , output ) ) <EOL> log . debug ( "<STR_LIT>" . format ( str ( chain [ - <NUM_LIT:1> ] ) ) ) <EOL> else : <EOL> log . debug ( "<STR_LIT>" ) <EOL> try : <EOL> chain . append ( ( "<STR_LIT:string>" , self . _get_ascii_string ( addr ) ) ) <EOL> except NotAStringError : <EOL> try : <EOL> chain . append ( ( "<STR_LIT:string>" , self . _get_unicode_string ( addr ) ) ) <EOL> except NotAStringError : <EOL> pass <EOL> log . debug ( "<STR_LIT>" . format ( chain ) ) <EOL> return chain <EOL> @ lock_host <EOL> def command ( self , command = None ) : <EOL> """<STR_LIT>""" <EOL> if command : <EOL> oldcan = self . _vdb . canvas <EOL> newcan = envi . memcanvas . StringMemoryCanvas ( self . _vdb . memobj , self . _vdb . symobj ) <EOL> try : <EOL> self . _vdb . canvas = newcan <EOL> self . _vdb . onecmd ( command ) <EOL> finally : <EOL> self . _vdb . canvas = oldcan <EOL> return str ( newcan ) . rstrip ( "<STR_LIT:\n>" ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return res <EOL> @ lock_host <EOL> def disassembly_flavor ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" <EOL> def capabilities ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ '<STR_LIT>' ] <EOL> def _state ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _vdb . getTrace ( ) . isAttached ( ) : <EOL> state = "<STR_LIT>" <EOL> else : <EOL> if self . _vdb . getTrace ( ) . isRunning ( ) : <EOL> state = "<STR_LIT>" <EOL> else : <EOL> state = "<STR_LIT>" <EOL> return state <EOL> def get_registers ( self ) : <EOL> return self . _vdb . getTrace ( ) . getRegisters ( ) <EOL> def get_register ( self , reg_name ) : <EOL> return self . get_registers ( ) [ reg_name ] <EOL> def get_registers_x86_64 ( self ) : <EOL> return self . get_registers ( ) <EOL> def get_registers_x86 ( self ) : <EOL> return self . get_registers ( ) <EOL> def get_registers_arm ( self ) : <EOL> return self . get_registers ( ) <EOL> def get_registers_sse ( self , num = <NUM_LIT:8> ) : <EOL> sse = { } <EOL> for k , v in self . get_registers ( ) . items ( ) : <EOL> if k . startswith ( "<STR_LIT>" ) : <EOL> sse [ k ] = v <EOL> return sse <EOL> def get_registers_fpu ( self ) : <EOL> fpu = { } <EOL> for k , v in self . get_registers ( ) . items ( ) : <EOL> if k . startswith ( "<STR_LIT>" ) : <EOL> fpu [ k ] = v <EOL> return fpu <EOL> def get_next_instruction ( self ) : <EOL> dis = self . disassemble ( address = self . program_counter ( ) [ <NUM_LIT:1> ] , count = <NUM_LIT:1> ) <EOL> return dis . partition ( "<STR_LIT:\n>" ) [ <NUM_LIT:0> ] . strip ( ) <EOL> def get_arch ( self ) : <EOL> arch = self . _vdb . getTrace ( ) . getMeta ( "<STR_LIT>" ) <EOL> return self . archs [ arch ] <EOL> def get_addr_size ( self ) : <EOL> arch = self . get_arch ( ) <EOL> return self . sizes [ arch ] <EOL> def get_byte_order ( self ) : <EOL> return "<STR_LIT>" <EOL> class VDBCommand ( DebuggerCommand , vtrace . Notifier ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host ) : <EOL> """<STR_LIT>""" <EOL> super ( VDBCommand , self ) . __init__ ( ) <EOL> self . _vdb = host <EOL> self . _vtrace = vtrace <EOL> self . register_hooks ( ) <EOL> def invoke ( self , arg , from_tty ) : <EOL> self . handle_command ( arg ) <EOL> def register_hooks ( self ) : <EOL> self . _vdb . registerNotifier ( vtrace . NOTIFY_ALL , self ) <EOL> def unregister_hooks ( self ) : <EOL> self . _vdb . deregisterNotifier ( vtrace . NOTIFY_ALL , self ) <EOL> def notify ( self , event , trace ) : <EOL> if event == self . _vtrace . NOTIFY_DETACH : <EOL> self . exit_handler ( event ) <EOL> elif event == self . _vtrace . NOTIFY_EXIT : <EOL> self . exit_handler ( event ) <EOL> elif event == self . _vtrace . NOTIFY_BREAK : <EOL> self . stop_handler ( event ) <EOL> elif event == self . _vtrace . NOTIFY_STEP : <EOL> self . stop_handler ( event ) <EOL> elif event == self . _vtrace . NOTIFY_CONTINUE : <EOL> self . cont_handler ( event ) <EOL> def stop_handler ( self , event ) : <EOL> self . adaptor . update_state ( ) <EOL> voltron . server . dispatch_queue ( ) <EOL> log . debug ( '<STR_LIT>' ) <EOL> def exit_handler ( self , event ) : <EOL> log . debug ( '<STR_LIT>' ) <EOL> voltron . server . cancel_queue ( ) <EOL> voltron . server . stop ( ) <EOL> def cont_handler ( self , event ) : <EOL> log . debug ( '<STR_LIT>' ) <EOL> class VDBAdaptorPlugin ( DebuggerAdaptorPlugin ) : <EOL> host = '<STR_LIT>' <EOL> adaptor_class = VDBAdaptor <EOL> command_class = VDBCommand </s>
