DebuggerBase.py
6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# DExTer : Debugging Experience Tester
# ~~~~~~ ~ ~~ ~ ~~
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
"""Base class for all debugger interface implementations."""
import abc
from itertools import chain
import os
import sys
import time
import traceback
from dex.dextIR import DebuggerIR, ValueIR
from dex.utils.Exceptions import DebuggerException
from dex.utils.Exceptions import NotYetLoadedDebuggerException
from dex.utils.ReturnCode import ReturnCode
class DebuggerBase(object, metaclass=abc.ABCMeta):
def __init__(self, context, step_collection):
self.context = context
self.steps = step_collection
self._interface = None
self.has_loaded = False
self._loading_error = NotYetLoadedDebuggerException()
self.watches = set()
try:
self._interface = self._load_interface()
self.has_loaded = True
self._loading_error = None
except DebuggerException:
self._loading_error = sys.exc_info()
self.step_index = 0
def __enter__(self):
try:
self._custom_init()
self.clear_breakpoints()
self.add_breakpoints()
except DebuggerException:
self._loading_error = sys.exc_info()
return self
def __exit__(self, *args):
self._custom_exit()
def _custom_init(self):
pass
def _custom_exit(self):
pass
@property
def debugger_info(self):
return DebuggerIR(name=self.name, version=self.version)
@property
def is_available(self):
return self.has_loaded and self.loading_error is None
@property
def loading_error(self):
return (str(self._loading_error[1])
if self._loading_error is not None else None)
@property
def loading_error_trace(self):
if not self._loading_error:
return None
tb = traceback.format_exception(*self._loading_error)
if self._loading_error[1].orig_exception is not None:
orig_exception = traceback.format_exception(
*self._loading_error[1].orig_exception)
if ''.join(orig_exception) not in ''.join(tb):
tb.extend(['\n'])
tb.extend(orig_exception)
tb = ''.join(tb).splitlines(True)
return tb
def add_breakpoints(self):
for s in self.context.options.source_files:
with open(s, 'r') as fp:
num_lines = len(fp.readlines())
for line in range(1, num_lines + 1):
self.add_breakpoint(s, line)
def _update_step_watches(self, step_info):
loc = step_info.current_location
watch_cmds = ['DexUnreachable', 'DexExpectStepOrder']
towatch = chain.from_iterable(self.steps.commands[x]
for x in watch_cmds
if x in self.steps.commands)
try:
# Iterate over all watches of the types named in watch_cmds
for watch in towatch:
if (os.path.exists(loc.path)
and os.path.samefile(watch.path, loc.path)
and watch.lineno == loc.lineno):
result = watch.eval(self)
step_info.watches.update(result)
break
except KeyError:
pass
def _sanitize_function_name(self, name): # pylint: disable=no-self-use
"""If the function name returned by the debugger needs any post-
processing to make it fit (for example, if it includes a byte offset),
do that here.
"""
return name
def start(self):
self.steps.clear_steps()
self.launch()
for command_obj in chain.from_iterable(self.steps.commands.values()):
self.watches.update(command_obj.get_watches())
max_steps = self.context.options.max_steps
for _ in range(max_steps):
while self.is_running:
pass
if self.is_finished:
break
self.step_index += 1
step_info = self.get_step_info()
if step_info.current_frame:
self._update_step_watches(step_info)
self.steps.new_step(self.context, step_info)
if self.in_source_file(step_info):
self.step()
else:
self.go()
time.sleep(self.context.options.pause_between_steps)
else:
raise DebuggerException(
'maximum number of steps reached ({})'.format(max_steps))
def in_source_file(self, step_info):
if not step_info.current_frame:
return False
if not step_info.current_location.path:
return False
if not os.path.exists(step_info.current_location.path):
return False
return any(os.path.samefile(step_info.current_location.path, f) \
for f in self.context.options.source_files)
@abc.abstractmethod
def _load_interface(self):
pass
@classmethod
def get_option_name(cls):
"""Short name that will be used on the command line to specify this
debugger.
"""
raise NotImplementedError()
@classmethod
def get_name(cls):
"""Full name of this debugger."""
raise NotImplementedError()
@property
def name(self):
return self.__class__.get_name()
@property
def option_name(self):
return self.__class__.get_option_name()
@abc.abstractproperty
def version(self):
pass
@abc.abstractmethod
def clear_breakpoints(self):
pass
@abc.abstractmethod
def add_breakpoint(self, file_, line):
pass
@abc.abstractmethod
def launch(self):
pass
@abc.abstractmethod
def step(self):
pass
@abc.abstractmethod
def go(self) -> ReturnCode:
pass
@abc.abstractmethod
def get_step_info(self):
pass
@abc.abstractproperty
def is_running(self):
pass
@abc.abstractproperty
def is_finished(self):
pass
@abc.abstractproperty
def frames_below_main(self):
pass
@abc.abstractmethod
def evaluate_expression(self, expression, frame_idx=0) -> ValueIR:
pass