1# DExTer : Debugging Experience Tester 2# ~~~~~~ ~ ~~ ~ ~~ 3# 4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5# See https://llvm.org/LICENSE.txt for license information. 6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7"""Default class for controlling debuggers.""" 8 9from itertools import chain 10import os 11import time 12 13from dex.debugger.DebuggerControllers.DebuggerControllerBase import DebuggerControllerBase 14from dex.debugger.DebuggerControllers.ControllerHelpers import in_source_file, update_step_watches 15from dex.utils.Exceptions import DebuggerException, LoadDebuggerException 16 17class DefaultController(DebuggerControllerBase): 18 def __init__(self, context, step_collection): 19 self.context = context 20 self.step_collection = step_collection 21 self.source_files = self.context.options.source_files 22 self.watches = set() 23 self.step_index = 0 24 25 def _break_point_all_lines(self): 26 for s in self.context.options.source_files: 27 with open(s, 'r') as fp: 28 num_lines = len(fp.readlines()) 29 for line in range(1, num_lines + 1): 30 try: 31 self.debugger.add_breakpoint(s, line) 32 except DebuggerException: 33 raise LoadDebuggerException(DebuggerException.msg) 34 35 def _run_debugger_custom(self): 36 self.step_collection.debugger = self.debugger.debugger_info 37 self._break_point_all_lines() 38 self.debugger.launch() 39 40 for command_obj in chain.from_iterable(self.step_collection.commands.values()): 41 self.watches.update(command_obj.get_watches()) 42 43 max_steps = self.context.options.max_steps 44 for _ in range(max_steps): 45 while self.debugger.is_running: 46 pass 47 48 if self.debugger.is_finished: 49 break 50 51 self.step_index += 1 52 step_info = self.debugger.get_step_info(self.watches, self.step_index) 53 54 if step_info.current_frame: 55 update_step_watches(step_info, self.watches, self.step_collection.commands) 56 self.step_collection.new_step(self.context, step_info) 57 58 if in_source_file(self.source_files, step_info): 59 self.debugger.step() 60 else: 61 self.debugger.go() 62 63 time.sleep(self.context.options.pause_between_steps) 64 else: 65 raise DebuggerException( 66 'maximum number of steps reached ({})'.format(max_steps)) 67