def main(): filename = sys.argv[1] fptr = open(filename, "r") source = fptr.read() fptr.seek(0) source_lines = format_source_lines(fptr.readlines()) fptr.close() draw_header("Source") display_source(source_lines) code = compile(source, filename, "exec") vm = BytecodeVM(code, source_lines, filename) WITH_DEBUGGER = False if not WITH_DEBUGGER: draw_header("Disassembly") dis.dis(code) # Configure the VM and set the settings based on command line. For now use defaults config = configure_vm() config.show_disassembly = True vm.config = config vm.execute() else: debugger = Debugger(code, source_lines, filename) debugger.execute(False)
def run_vm(self): # Run until any breakpoint is hit self.__vm_running = True while True: opmethod, oparg, current_lineno = self.__vm.get_opcode() # Check if any breakpoint got hit if current_lineno in self.__breakpoints.keys(): if self.__breakpoints[current_lineno] is True: breakpoint_hit = self.__vm.exec_frame.line_no_obj.get_source_line(current_lineno) draw_header("Breakpoint Hit: %s" % breakpoint_hit) lines = self.__vm.exec_frame.line_no_obj.get_source_sorrounding_line(current_lineno) print(lines) self.__breakpoint_hit = current_lineno return terminate = self.__vm.execute_opcode(opmethod, oparg) if terminate: break # Reinitialize for next execution self.initialize_vm(self.__code, self.__source, self.__filename) print("App exited...") self.__vm_running = False return
def view_globals(self, global_var=None): globals = self.__vm.exec_frame.globals draw_header("Globals") for k, v in globals.items(): if global_var is None or global_var == k: print("%s: %s" % (k, v))
def run_vm(self): # Run until any breakpoint is hit self.__vm_running = True while True: opmethod, oparg, current_lineno = self.__vm.get_opcode() # Check if any breakpoint got hit if current_lineno in self.__breakpoints.keys(): if self.__breakpoints[current_lineno] is True: breakpoint_hit = self.__vm.exec_frame.line_no_obj.get_source_line( current_lineno) draw_header("Breakpoint Hit: %s" % breakpoint_hit) lines = self.__vm.exec_frame.line_no_obj.get_source_sorrounding_line( current_lineno) print(lines) self.__breakpoint_hit = current_lineno return terminate = self.__vm.execute_opcode(opmethod, oparg) if terminate: break # Reinitialize for next execution self.initialize_vm(self.__code, self.__source, self.__filename) print("App exited...") self.__vm_running = False return
def view_breakpoints(self): draw_header("Breakpoints Set") for bp, status in self.__breakpoints.items(): breakpoint_hit = self.__vm.exec_frame.line_no_obj.get_source_line(bp) breakpoint_hit = breakpoint_hit.strip() if status == True: status = "Enabled" else: status = "Disabled" print("Breakpoint Line %s: %s ---> %s" % (bp, status, breakpoint_hit))
def view_locals(self, local_var=None): draw_header("Locals") current_exec_frame = self.__vm.exec_frame while current_exec_frame is not None: locals = current_exec_frame.locals for k, v in locals.items(): if local_var is None or local_var == k: print("%s: %s" % (k, v)) current_exec_frame = current_exec_frame.parent_exec_frame
def set_local(self, local_var, val_to_set): val, exec_frame = self.__vm.exec_frame.get_local_var_value(local_var) t = type(val) try: exec_frame.set_local_var_value(local_var, t(val_to_set)) except: exec_frame.set_local_var_value(local_var, val_to_set) draw_header("Locals Changed") val, exec_frame = self.__vm.exec_frame.get_local_var_value(local_var) print("%s: %s" % (local_var, val))
def view_backtrace(self): stack_trace = [frame for frame in self.__vm.exec_frame_stack] stack_trace.append(self.__vm.exec_frame) stack_trace.reverse() draw_header("Stacktrace") backtrace = "" for i, frame in enumerate(stack_trace): backtrace += "\t<Frame %s - %s>" % (i, frame) + "\n" print(backtrace)
def view_breakpoints(self): draw_header("Breakpoints Set") for bp, status in self.__breakpoints.items(): breakpoint_hit = self.__vm.exec_frame.line_no_obj.get_source_line( bp) breakpoint_hit = breakpoint_hit.strip() if status == True: status = "Enabled" else: status = "Disabled" print("Breakpoint Line %s: %s ---> %s" % (bp, status, breakpoint_hit))
def __init__(self, code, source, filename): self.__breakpoints = {} self.__prompt = ">>> " self.__debugger_broken = False self.__code = code self.__source = source self.__filename = filename self.initialize_vm(code, source, filename) draw_header("Initializing Debugger...") self.__breakpoint_hit = None self.__vm_running = False