def test_callvirt_one_parameter_instance_puts_this_pointer_and_parameter_on_stack(self): from VM import VM from MethodDefinition import MethodDefinition vm = VM() paramObject = Variable() paramObject.type = Types.Int32 m = MethodDefinition() m.name = "TestMethod" m.namespace = "A.B" m.returnType = Types.Int32 m.parameters = [paramObject] m.names = "A.B" m.attributes.append(MethodDefinition.AttributeTypes["instance"]) vm.methods.append(m) r = ReferenceType() vm.stack.push(r) v = Variable(8888) vm.stack.push(v) self.assertEqual(vm.currentMethod, None) c = callvirt("instance int32 A.B::TestMethod ( int32 )") c.execute(vm) self.assertEqual(vm.currentMethod.methodDefinition, m) self.assertEqual(vm.stack.get_number_of_frames(), 2) self.assertEqual(len(vm.current_method().parameters), 2) self.assertEqual(vm.current_method().parameters[1], v) self.assertEqual(vm.current_method().parameters[0], r)
def global_aggregator(): def compute_func(in_streams, out_streams): """ Parameters ---------- in_streams: list of Stream in_streams is a list of anomaly streams with one stream from each sensor. An anomaly stream is a sequence of 0.0 and 1.0 where 0.0 indicates no anomaly and 1.0 indicates an anomaly. out_streams: list of Stream This list consists of a single stream that contains 0.0 when no global anomaly across all sensors is detected and 1.0 when a global anomaly is detected. """ aggregate_anomalies(in_streams, out_streams, timed_window_size=2) proc = distributed_process(compute_func=compute_func, in_stream_names=['in_1', 'in_2'], out_stream_names=[], connect_sources=[], name='global aggregator') vm = VM(processes=[proc], connections=[], subscribers=[(proc, 'in_1', 'S1'), (proc, 'in_2', 'S2')]) vm.start()
def test_callvirt_one_parameter_int(self): from VM import VM from MethodDefinition import MethodDefinition vm = VM() paramObject = Variable() paramObject.type = Types.Int32 m = MethodDefinition() m.namespace = "A.B" m.name = "TestMethod" m.returnType = Types.Int32 m.parameters = [paramObject] vm.methods.append(m) param = Variable() param.value = 123 param.type = Types.Int32 vm.stack.push(param) c = callvirt("int32 A.B::TestMethod ( int32 )") c.execute(vm) self.assertEqual(vm.currentMethod.methodDefinition, m) self.assertEqual(vm.stack.get_number_of_frames(), 2) self.assertEqual(vm.current_method().parameters[0], param)
def single_process_subscriber(): # This VM has a single process called proc_1. # This process has a single input stream that we # call 'in' and it has no output streams; so # out_stream_names is empty. Elements arriving on # the input stream are copied to a file called # result.dat (See compute_func.) # This process has no source threads that # generate data. The input comes only from # subscribing to a stream. Because this process # has no source threads, connect_sources is # empty. Likewise, since it has no actuators, # connect_actuators is empty. def compute_func(in_streams, out_streams): stream_to_file(in_streams[0], 'result.dat') proc_1 = distributed_process(compute_func=compute_func, in_stream_names=['in'], out_stream_names=[], connect_sources=[], connect_actuators=[], name='proc_1') # This VM consists of a single process. So, it has # no connections to other processes within the same # shared-memory multicore machine. # It is a subscriber to a stream called # copy_of_source_list # Elements received on this stream are passed to the # stream called 'in' inside the process called proc_1. vm_1 = VM(processes=[proc_1], connections=[], subscribers=[(proc_1, 'in', 'copy_of_source_list')]) vm_1.start()
def test_callvirt_one_parameter_instance_puts_this_pointer_and_parameter_on_stack( self): from VM import VM from MethodDefinition import MethodDefinition vm = VM() paramObject = Variable() paramObject.type = Types.Int32 m = MethodDefinition() m.name = 'TestMethod' m.namespace = 'A.B' m.returnType = Types.Int32 m.parameters = [paramObject] m.names = 'A.B' m.attributes.append(MethodDefinition.AttributeTypes['instance']) vm.methods.append(m) r = ReferenceType() vm.stack.push(r) v = Variable(8888) vm.stack.push(v) self.assertEqual(vm.currentMethod, None) c = callvirt('instance int32 A.B::TestMethod ( int32 )') c.execute(vm) self.assertEqual(vm.currentMethod.methodDefinition, m) self.assertEqual(vm.stack.get_number_of_frames(), 2) self.assertEqual(len(vm.current_method().parameters), 2) self.assertEqual(vm.current_method().parameters[1], v) self.assertEqual(vm.current_method().parameters[0], r)
def test_callvirt_one_parameter_int(self): from VM import VM from MethodDefinition import MethodDefinition vm = VM() paramObject = Variable() paramObject.type = Types.Int32 m = MethodDefinition() m.namespace = 'A.B' m.name = 'TestMethod' m.returnType = Types.Int32 m.parameters = [paramObject] vm.methods.append(m) param = Variable() param.value = 123 param.type = Types.Int32 vm.stack.push(param) c = callvirt('int32 A.B::TestMethod ( int32 )') c.execute(vm) self.assertEqual(vm.currentMethod.methodDefinition, m) self.assertEqual(vm.stack.get_number_of_frames(), 2) self.assertEqual(vm.current_method().parameters[0], param)
def two_process_publisher(): source_list = range(10) def source(out_stream): return source_list_to_stream(source_list, out_stream) def compute_0(in_streams, out_streams): map_element( func=lambda x: x, in_stream=in_streams[0], out_stream=out_streams[0]) proc_0 = distributed_process( compute_func=compute_0, in_stream_names=['in'], out_stream_names=['out'], connect_sources=[('in', source)], name='process_0') def compute_1(in_streams, out_streams): map_element( func=lambda x: x+10, in_stream=in_streams[0], out_stream=out_streams[0]) proc_1 = distributed_process( compute_func=compute_1, in_stream_names=['in'], out_stream_names=['out'], connect_sources=[], name='process_1' ) vm_0 = VM( processes=[proc_0, proc_1], connections=[(proc_0, 'out', proc_1, 'in')], publishers=[(proc_1, 'out', 'publication')]) vm_0.start()
def test_execute_ends_catch_block(self): from VM import VM vm = VM() vm.get_protected_blocks().append(ProtectedBlock()) x = EndCatch() x.execute(vm) self.assertEqual(len(vm.get_protected_blocks()), 0)
def test_execute_ends_try_block(self): from VM import VM vm = VM() vm.get_protected_blocks().append(ProtectedBlock()) x = EndTry() x.execute(vm) self.assertEqual(len(vm.get_protected_blocks()), 0)
def test_throw_object(self): from VM import VM vm = VM() x = throw() x.execute(vm) index = vm.get_instruction_pointer() self.assertEqual(3, index)
def test_throw_no_arguments_throws_exception(self): from VM import VM vm = VM() x = throw("asdf") # fixme optional parameters x.execute(vm) index = vm.get_instruction_pointer() self.assertEqual(3, index)
def test_throw_no_arguments_throws_exception(self): from VM import VM vm = VM() x = throw('asdf') # fixme optional parameters x.execute(vm) index = vm.get_instruction_pointer() self.assertEqual(3, index)
def test_execute_0(self): from VM import VM vm = VM() x = ldarg('0') m = MethodDefinition() m.parameters.append(Variable(987)) vm.set_current_method(m) x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(vm.stack.pop().value, 987)
def test_execute_0(self): from VM import VM vm = VM() x = ldloc('0') m = MethodDefinition() m.locals.append(Variable(987)) vm.set_current_method(m) x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(vm.stack.pop().value, 987)
def test_execute_1(self): from VM import VM vm = VM() x = ldarg('1') m = MethodDefinition() m.parameters.append(Variable(0)) m.parameters.append(Variable(987)) vm.set_current_method(m) x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(vm.stack.pop().value, 987)
def test_execute_0(self): from VM import VM vm = VM() x = stloc('0') m = MethodDefinition() m.locals.append(Variable(0)) vm.set_current_method(m) vm.stack.push(Variable(987)) x.execute(vm) self.assertEqual(vm.stack.count(), 0) self.assertEqual(m.locals[0].value, 987)
def test_execute_1(self): from VM import VM vm = VM() x = ldloc("1") m = MethodDefinition() m.locals.append(Variable(0)) m.locals.append(Variable(987)) vm.set_current_method(m) x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(vm.stack.pop().value, 987)
def single_process_publication_subscriber(): """ The application in this example consists of single process. The process has no source and no actuator. It has a single in_stream called 'in'. This example creates a virtual machine (vm) which subscribes to a stream called 'sequence'. The steps for creating a process are: (1) Define the sources. In this example we have no sources. (2) Define the actuators. In this example we have no actuators. (3) Define compute_func. This process has a single input stream and no output stream. (4) Create the process by calling distributed_process() Final step After creating all processes, specify the connections between processes and run the virtual machine.. """ # SKIP STEPS 1, 2 BECAUSE NO SOURCES OR ACTUATORS. # STEP 3: DEFINE COMPUTE_FUNC def g(in_streams, out_streams): def print_element(v): print 'stream element is ', v sink_element(func=print_element, in_stream=in_streams[0]) # STEP 4: CREATE PROCESSES proc_1 = distributed_process(compute_func=g, in_stream_names=['in'], out_stream_names=[], connect_sources=[], connect_actuators=[], name='proc_1') # FINAL STEP: CREATE A VM AND START IT. # Since this application has a single process it has no # connections between processes. The process, proc_1, subscribes # to a stream called 'sequence'. This process does not publish # streams. vm_1 = VM(processes=[proc_1], connections=[], subscribers=[(proc_1, 'in', 'sequence')]) vm_1.start()
def test_execute_s_label(self): from VM import VM vm = VM() x = ldloc('s ghi') m = MethodDefinition() m.locals.append(Variable(0, name='abc')) m.locals.append(Variable(0, name='def')) m.locals.append(Variable(987, name='ghi')) m.locals.append(Variable(0, name='jkl')) vm.set_current_method(m) x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(vm.stack.pop().value, 987)
def test_execute_1(self): from VM import VM vm = VM() x = stloc('1') m = MethodDefinition() m.locals.append(Variable(0)) m.locals.append(Variable(0)) vm.set_current_method(m) vm.stack.push(Variable(987)) x.execute(vm) self.assertEqual(vm.stack.count(), 0) self.assertEqual(m.locals[0].value, 0) self.assertEqual(m.locals[1].value, 987)
def test_execute_s_label(self): from VM import VM vm = VM() x = ldloc("s ghi") m = MethodDefinition() m.locals.append(Variable(0, name="abc")) m.locals.append(Variable(0, name="def")) m.locals.append(Variable(987, name="ghi")) m.locals.append(Variable(0, name="jkl")) vm.set_current_method(m) x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(vm.stack.pop().value, 987)
def test_execute_reference_type_parameter(self): from VM import VM vm = VM() foo = ClassDefinition() foo.namespace = 'ConsoleApplication1' foo.name = 'foo' fooType = Types.register_custom_type(foo) fooObject = ReferenceType() fooObject.name = 'f' fooObject.type = fooType fooObject.value = Variable(3333) bar = ClassDefinition() bar.namespace = 'ConsoleApplication1' bar.name = 'bar' barType = Types.register_custom_type(bar) barObject = ReferenceType() barObject.type = barType field = ReferenceType() field.name = 'f' field.type = fooType barObject.add_field(field) vm.stack.push(barObject) x = ldfld('class ConsoleApplication1.foo ConsoleApplication1.bar::f') x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(barObject.fields[0], vm.stack.pop())
def test_execute_multiple_fields(self): from VM import VM vm = VM() c = ClassDefinition() c.namespace = 'a' c.name = 'b' v = Variable() v.name = 'abc' v.type = Types.Int32 v2 = Variable() v2.name = 'def' v2.type = Types.Int32 c.fieldDefinitions.append(v2) r = ReferenceType() t = Types.register_custom_type(c) r.type = t r.add_field(v) r.add_field(v2) vm.stack.push(r) x = ldfld('int32 ConsoleApplication1.foo::def') x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(r.fields[1], vm.stack.pop())
def test_execute_not_enough_stack_values(self): from VM import VM vm = VM() x = ldlen('') self.assertRaises(StackStateException, x.execute, vm)
def test_newobj_no_parameters_initializes_int_field_to_zero(self): from VM import VM vm = VM() m = MethodDefinition() m.name = 'ctor' m.namespace = 'testnamespace.testclass' vm.methods.append(m) c = ClassDefinition() c.name = 'testclass' c.namespace = 'testnamespace' c.methods.append(m) v = Variable() v.name = 'xyz' v.type = Types.Int32 c.fieldDefinitions.append(v) t = Types.register_custom_type(c) n = newobj('instance void testnamespace.testclass::.ctor()') n.execute(vm) Types.unregister_custom_type(t) o = vm.stack.pop() self.assertEqual(o.type, t) self.assertEqual(len(o.fields), 1) self.assertEqual(o.fields[0].value, 0) self.assertEqual(len(o.fieldNames), 1) self.assertEqual(o.fieldNames[0], 'xyz')
def test_execute_reference_type_stores_reference_type_in_local_but_doesnt_change_name(self): from VM import VM vm = VM() x = stloc('0') m = MethodDefinition() localr = ReferenceType() localr.name = 'foobar' m.locals.append(localr) vm.set_current_method(m) r2 = ReferenceType() vm.stack.push(r2) x.execute(vm) self.assertEqual(vm.stack.count(), 0) self.assertEqual(m.locals[0], r2) self.assertEqual(m.locals[0].name, 'foobar')
def test_execute_not_enough_stack_values(self): from VM import VM vm = VM() vm.stack.push(1) x = mul() self.assertRaises(StackStateException, x.execute, vm)
def test_execute_notEnoughStackValues(self): from VM import VM vm = VM() vm.stack.push(Variable(1)) x = add('') self.assertRaises(StackStateException, x.execute, vm)
def connect(self, addr: tuple): """ addr: <ip, port> """ # precondition if not self.bound: self.bind(('',)) self.connected = False payload = dict( action="connect", data=addr ) self.interface.sendall(VM_Message(payload).pack()) resp = VM.read_message(self.interface).payload if resp["status"] == 0: self.logger.debug("Connection established with {}:{}".format(*addr)) self.connected = True self.remote_addr = addr else: self.logger.error("Connection failed") raise RuntimeError("Connection failed with status code {}, {}". format(resp["status"], resp["message"]))
def create(self): """ creates the VM that this Cassandra Node will run on :return: """ flavor = env_vars["cassandra_%s_flavor" % self.type] #create the VM self.vm = VM(self.name, flavor, self.image, create=True)
def test_execute_multiple_times_starts_nested_try_blocks(self): from VM import VM vm = VM() x = BeginTry() x.execute(vm) x.execute(vm) x.execute(vm) self.assertEquals(len(vm.protected_blocks), 3)
def test_execute(self): from VM import VM vm = VM() vm.stack.push(Variable(5)) x = pop('') x.execute(vm) self.assertEqual(vm.stack.count(), 0)
def test_execute_reference_type_stores_reference_type_in_local_but_doesnt_change_name( self): from VM import VM vm = VM() x = stloc('0') m = MethodDefinition() localr = ReferenceType() localr.name = 'foobar' m.locals.append(localr) vm.set_current_method(m) r2 = ReferenceType() vm.stack.push(r2) x.execute(vm) self.assertEqual(vm.stack.count(), 0) self.assertEqual(m.locals[0], r2) self.assertEqual(m.locals[0].name, 'foobar')
def testExecute(self): from VM import VM vm = VM() x = ldstr('Hello world') x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(vm.stack.pop(), 'Hello world')
def test_execute_i4_hex(self): from VM import VM vm = VM() x = ldc('i4 0x4d2') x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(vm.stack.pop().value, 0x4d2)
def test_execute_r8(self): from VM import VM vm = VM() x = ldc('r8 999988887777.111122223333') x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(vm.stack.pop().value, 999988887777.111122223333)
def test_execute_r4(self): from VM import VM vm = VM() x = ldc('r4 1.234') x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(vm.stack.pop().value, 1.234)
def test_execute_s_label(self): from VM import VM vm = VM() x = stloc('s def') m = MethodDefinition() m.locals.append(Variable(0, name='xyz')) m.locals.append(Variable(0, name='abc')) m.locals.append(Variable(0, name='def')) m.locals.append(Variable(0, name='ghi')) vm.set_current_method(m) vm.stack.push(Variable(987)) x.execute(vm) self.assertEqual(vm.stack.count(), 0) self.assertEqual(m.locals[0].value, 0) self.assertEqual(m.locals[1].value, 0) self.assertEqual(m.locals[2].value, 987) self.assertEqual(m.locals[3].value, 0)
def test_execute_i4dots(self): from VM import VM vm = VM() x = ldc('i4.s 123') x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(vm.stack.pop().value, 123)
def test_execute_floats(self): from VM import VM vm = VM() vm.stack.push(Variable(123.4)) vm.stack.push(Variable(0.01)) x = mul() x.execute(vm) self.assertEqual(vm.stack.count(), 1) self.assertEqual(vm.stack.pop().value, 123.4 * 0.01)
def test_execute_instance_clears_stack_with_void_return_type(self): from VM import VM vm = VM() m = Method() m.name = 'TestMethod' m.attributes.append(MethodDefinition.AttributeTypes['instance']) m.returnType = Types.Void m.maxStack = 99 m.parameters = [] vm.methods.append(m) vm.stack.push(111) vm.execute_method(m) vm.stack.push(124) vm.stack.push(987) r = Ret() r.execute(vm) self.assertEqual(vm.current_method(), None) self.assertEqual(vm.stack.count(), 1) self.assertEqual(vm.stack.pop(), 111)
def test_leave_s(self): from VM import VM from MethodDefinition import MethodDefinition vm = VM() m = MethodDefinition() x = ldc('i4.1') m.instructions.append(x) m.instructions.append(x) m.instructions.append(x) dest = ldc('i4.3') dest.label = 'zzz' m.instructions.append(dest) vm.set_current_method(m) x = leave('s zzz') x.execute(vm) index = vm.get_instruction_pointer() self.assertEqual(3, index);
def test_br(self): from VM import VM from MethodDefinition import MethodDefinition vm = VM() m = MethodDefinition() x = ldc('i4.1') m.instructions.append(x) m.instructions.append(x) m.instructions.append(x) dest = ldc('i4.3') dest.label = 'asdf' m.instructions.append(dest) vm.set_current_method(m) x = br('asdf') # fixme optional parameters x.execute(vm) index = vm.get_instruction_pointer() self.assertEqual(3, index);
def test_execute_true_s(self): from VM import VM from MethodDefinition import MethodDefinition vm = VM() m = MethodDefinition() x = ldc('i4.1') m.instructions.append(x) m.instructions.append(x) m.instructions.append(x) dest = ldc('i4.3') dest.label = 'asdf' m.instructions.append(dest) vm.set_current_method(m) vm.stack.push(Variable(1)) x = brtrue('s asdf') # fixme optional parameters x.execute(vm) index = vm.get_instruction_pointer() self.assertEqual(3, index); self.assertEqual(vm.stack.count(), 0)
def test_execute_void_no_parameters(self): from VM import VM vm = VM() m = Method() m.name = 'TestMethod' m.returnType = Types.Void m.parameters = [] vm.methods.append(m) self.assertEqual(vm.current_method(), None) vm.execute_method(m) self.assertEqual(vm.current_method(), m) r = Ret() r.execute(vm) self.assertEqual(vm.current_method(), None)
def test_execute_int_no_parameters_returns_value_on_stack(self): from VM import VM vm = VM() m = Method() m.name = 'TestMethod' m.returnType = Types.Int32 m.parameters = [] vm.methods.append(m) v = Variable(888) self.assertEqual(vm.current_method(), None) self.assertEqual(vm.stack.get_frame_count(), 0) vm.execute_method(m) vm.stack.push(v) self.assertEqual(vm.current_method(), m) # fixme - test return value too r = Ret() r.execute(vm) self.assertEqual(vm.current_method(), None) self.assertEqual(vm.stack.get_frame_count(), 1) self.assertEqual(vm.stack.pop(), v)
from VM import VM if __name__ == '__main__': vm = VM() vm.load('scripts/test3.tis') # vm.load('scripts/signal_edge_detector.tis') # vm.load('scripts/mov_04.tis') vm.run() vm.compare_io()
def __init__(self): self.vm = VM() self.vm.add_hook(DebugHooks.PreInstruction, self.pre_instruction_hook) self.vm.add_hook(DebugHooks.PreMethod, self.pre_method_hook) self.vm.add_hook(DebugHooks.PostMethod, self.post_method_hook)
class CassandraNode: """ Class that represents a node in a cassandra cluster. Can be of type 'SEED' or 'REGULAR' (default) """ #static vars image = env_vars["cassandra_base_image"] def __init__(self, name=None, node_type="REGULAR", create=False, vm=None): """ Creates a CassandraNode object. :param name: :param node_type: if "SEED" then will be treated as seednode :param create: if True then the actual VM will be created :param vm: if not None then this CassandraNode will be created from an existing vm """ self.bootstraped = False self.name = name self.type = node_type self.vm = None if not vm is None: # init a node from a VM self.from_vm(vm) if create: self.create() def __str__(self): rv = "Cassandra Node || name: %s, type: %s" % (self.name, self.type) return rv def create(self): """ creates the VM that this Cassandra Node will run on :return: """ flavor = env_vars["cassandra_%s_flavor" % self.type] #create the VM self.vm = VM(self.name, flavor, self.image, create=True) def from_vm(self, vm): """ Creates a CassandraNode from an existing VM :param vm: :return: """ if not vm.created: print "this VM is not created, so you cann't create a node from it" self.name = vm.name self.vm = vm if "seed" in vm.name: self.type = "SEED" elif "client" in vm.name: self.type = "CLIENT" else: self.type = "REGULAR" def bootstrap(self, params = None): """ Bootstraps a node with the rest of the Casandra cluster """ command = "" print "NODE: [%s] running bootstrap scripts" % self.name if self.type == "SEED": command += get_script_text("cassandra_seednode_bootstrap") elif self.type == "CLIENT": if self.name.endswith('1'): command += get_script_text("ganglia_endpoint") command += get_script_text("cassandra_client_bootstrap") else: command = get_script_text("cassandra_node_bootstrap") timer = Timer.get_timer() self.vm.run_command(command, silent=True) print "NODE: %s is now bootstrapped (took %d sec)" % (self.name, timer.stop()) self.bootstraped = True def decommission(self): """ Cecommissions a node from the Cassandra Cluster :return: """ print "NODE: Decommissioning node: " + self.name keyspace = env_vars['keyspace'] timer = Timer.get_timer() self.vm.run_command("nodetool repair -h %s %s" % (self.name, keyspace)) self.vm.run_command("nodetool decommission") print "NODE: %s is decommissioned (took %d secs)" % (self.name, timer.stop()) #self.vm.shutdown() def kill(self): command = get_script_text("cassandra_kill") self.vm.run_command(command, silent=True) def get_status(self): """ Gets the status of the node as far as Cassandra is concerned (uses hooks inside of VM) :return: """ if self.vm.get_cloud_status() != "ACTIVE": return "stopped" #wait for the vm to be ready and SSH-able self.vm.wait_ready() status = self.vm.run_command("ctool status", indent=0, prefix='') return status.strip() def inject_server_hosts(self, hosts): text = "" for h in hosts: text += h + "\n" print "injecting: \n"+text self.vm.run_command("echo '%s' > /opt/hosts") def __str__(self): str = "Node %s" % self.name return str
def run_test(self, fileName): vm = VM() file = os.getcwd() + '/tests/' + fileName vm.load(file) vm.start() return vm.stack.pop()
class Debugger(): def __init__(self): self.vm = VM() self.vm.add_hook(DebugHooks.PreInstruction, self.pre_instruction_hook) self.vm.add_hook(DebugHooks.PreMethod, self.pre_method_hook) self.vm.add_hook(DebugHooks.PostMethod, self.post_method_hook) def pre_instruction_hook(self, instruction): print instruction.label + ':\t' + instruction.name self.handle_input() def pre_method_hook(self, method): print 'Entered method ' + method.methodDefinition.namespace + '::' + method.methodDefinition.name def post_method_hook(self, method): print 'Exited method ' + method.methodDefinition.namespace + '::' + method.methodDefinition.name def handle_input(self): while True: r = raw_input('> ') if r == 's': for item in reversed(self.vm.stack.stack): print item elif r == 'q': exit() elif r == 'g': self.vm.remove_hook(DebugHooks.PreInstruction, self.pre_instruction_hook) return elif r == 'm': print 'method' elif r.startswith('l '): filename = '../tests/' + r[2:] + ".il" try: self.vm.load(filename) print 'Loaded ' + filename self.vm.start() print 'Execution finished' print 'Return code: ' + str(self.vm.stack.pop()) return except IOError: print 'Unable to load file' except Exception as e: print 'Error: ' + str(e) traceback.print_exc(file=sys.stdout) else: return def start(self): while True: self.handle_input()