LLDB.py
10.3 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# 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
"""Interface for communicating with the LLDB debugger via its python interface.
"""
import imp
import os
from subprocess import CalledProcessError, check_output, STDOUT
import sys
from dex.debugger.DebuggerBase import DebuggerBase
from dex.dextIR import FrameIR, LocIR, StepIR, StopReason, ValueIR
from dex.dextIR import StackFrame, SourceLocation, ProgramState
from dex.utils.Exceptions import DebuggerException, LoadDebuggerException
from dex.utils.ReturnCode import ReturnCode
class LLDB(DebuggerBase):
def __init__(self, context, *args):
self.lldb_executable = context.options.lldb_executable
self._debugger = None
self._target = None
self._process = None
self._thread = None
super(LLDB, self).__init__(context, *args)
def _custom_init(self):
self._debugger = self._interface.SBDebugger.Create()
self._debugger.SetAsync(False)
self._target = self._debugger.CreateTargetWithFileAndArch(
self.context.options.executable, self.context.options.arch)
if not self._target:
raise LoadDebuggerException(
'could not create target for executable "{}" with arch:{}'.
format(self.context.options.executable,
self.context.options.arch))
def _custom_exit(self):
if getattr(self, '_process', None):
self._process.Kill()
if getattr(self, '_debugger', None) and getattr(self, '_target', None):
self._debugger.DeleteTarget(self._target)
def _translate_stop_reason(self, reason):
if reason == self._interface.eStopReasonNone:
return None
if reason == self._interface.eStopReasonBreakpoint:
return StopReason.BREAKPOINT
if reason == self._interface.eStopReasonPlanComplete:
return StopReason.STEP
if reason == self._interface.eStopReasonThreadExiting:
return StopReason.PROGRAM_EXIT
if reason == self._interface.eStopReasonException:
return StopReason.ERROR
return StopReason.OTHER
def _load_interface(self):
try:
args = [self.lldb_executable, '-P']
pythonpath = check_output(
args, stderr=STDOUT).rstrip().decode('utf-8')
except CalledProcessError as e:
raise LoadDebuggerException(str(e), sys.exc_info())
except OSError as e:
raise LoadDebuggerException(
'{} ["{}"]'.format(e.strerror, self.lldb_executable),
sys.exc_info())
if not os.path.isdir(pythonpath):
raise LoadDebuggerException(
'path "{}" does not exist [result of {}]'.format(
pythonpath, args), sys.exc_info())
try:
module_info = imp.find_module('lldb', [pythonpath])
return imp.load_module('lldb', *module_info)
except ImportError as e:
msg = str(e)
if msg.endswith('not a valid Win32 application.'):
msg = '{} [Are you mixing 32-bit and 64-bit binaries?]'.format(
msg)
raise LoadDebuggerException(msg, sys.exc_info())
@classmethod
def get_name(cls):
return 'lldb'
@classmethod
def get_option_name(cls):
return 'lldb'
@property
def version(self):
try:
return self._interface.SBDebugger_GetVersionString()
except AttributeError:
return None
def clear_breakpoints(self):
self._target.DeleteAllBreakpoints()
def _add_breakpoint(self, file_, line):
if not self._target.BreakpointCreateByLocation(file_, line):
raise DebuggerException(
'could not add breakpoint [{}:{}]'.format(file_, line))
def _add_conditional_breakpoint(self, file_, line, condition):
bp = self._target.BreakpointCreateByLocation(file_, line)
if bp:
bp.SetCondition(condition)
else:
raise DebuggerException(
'could not add breakpoint [{}:{}]'.format(file_, line))
def _delete_conditional_breakpoint(self, file_, line, condition):
bp_count = self._target.GetNumBreakpoints()
bps = [self._target.GetBreakpointAtIndex(ix) for ix in range(0, bp_count)]
for bp in bps:
bp_cond = bp.GetCondition()
bp_cond = bp_cond if bp_cond is not None else ''
if bp_cond != condition:
continue
# If one of the bound bp locations for this bp is bound to the same
# line in file_ above, then delete the entire parent bp and all
# bp locs.
# https://lldb.llvm.org/python_reference/lldb.SBBreakpoint-class.html
for breakpoint_location in bp:
sb_address = breakpoint_location.GetAddress()
sb_line_entry = sb_address.GetLineEntry()
bl_line = sb_line_entry.GetLine()
sb_file_entry = sb_line_entry.GetFileSpec()
bl_dir = sb_file_entry.GetDirectory()
bl_file_name = sb_file_entry.GetFilename()
bl_file_path = os.path.join(bl_dir, bl_file_name)
if bl_file_path == file_ and bl_line == line:
self._target.BreakpointDelete(bp.GetID())
break
def launch(self):
self._process = self._target.LaunchSimple(None, None, os.getcwd())
if not self._process or self._process.GetNumThreads() == 0:
raise DebuggerException('could not launch process')
if self._process.GetNumThreads() != 1:
raise DebuggerException('multiple threads not supported')
self._thread = self._process.GetThreadAtIndex(0)
assert self._thread, (self._process, self._thread)
def step(self):
self._thread.StepInto()
def go(self) -> ReturnCode:
self._process.Continue()
return ReturnCode.OK
def _get_step_info(self, watches, step_index):
frames = []
state_frames = []
for i in range(0, self._thread.GetNumFrames()):
sb_frame = self._thread.GetFrameAtIndex(i)
sb_line = sb_frame.GetLineEntry()
sb_filespec = sb_line.GetFileSpec()
try:
path = os.path.join(sb_filespec.GetDirectory(),
sb_filespec.GetFilename())
except (AttributeError, TypeError):
path = None
function = self._sanitize_function_name(sb_frame.GetFunctionName())
loc_dict = {
'path': path,
'lineno': sb_line.GetLine(),
'column': sb_line.GetColumn()
}
loc = LocIR(**loc_dict)
frame = FrameIR(
function=function, is_inlined=sb_frame.IsInlined(), loc=loc)
if any(
name in (frame.function or '') # pylint: disable=no-member
for name in self.frames_below_main):
break
frames.append(frame)
state_frame = StackFrame(function=frame.function,
is_inlined=frame.is_inlined,
location=SourceLocation(**loc_dict),
watches={})
for expr in map(
lambda watch, idx=i: self.evaluate_expression(watch, idx),
watches):
state_frame.watches[expr.expression] = expr
state_frames.append(state_frame)
if len(frames) == 1 and frames[0].function is None:
frames = []
state_frames = []
reason = self._translate_stop_reason(self._thread.GetStopReason())
return StepIR(
step_index=step_index, frames=frames, stop_reason=reason,
program_state=ProgramState(state_frames))
@property
def is_running(self):
# We're not running in async mode so this is always False.
return False
@property
def is_finished(self):
return not self._thread.GetFrameAtIndex(0)
@property
def frames_below_main(self):
return ['__scrt_common_main_seh', '__libc_start_main']
def evaluate_expression(self, expression, frame_idx=0) -> ValueIR:
result = self._thread.GetFrameAtIndex(frame_idx
).EvaluateExpression(expression)
error_string = str(result.error)
value = result.value
could_evaluate = not any(s in error_string for s in [
"Can't run the expression locally",
"use of undeclared identifier",
"no member named",
"Couldn't lookup symbols",
"reference to local variable",
"invalid use of 'this' outside of a non-static member function",
])
is_optimized_away = any(s in error_string for s in [
'value may have been optimized out',
])
is_irretrievable = any(s in error_string for s in [
"couldn't get the value of variable",
"couldn't read its memory",
"couldn't read from memory",
"Cannot access memory at address",
"invalid address (fault address:",
])
if could_evaluate and not is_irretrievable and not is_optimized_away:
assert error_string == 'success', (error_string, expression, value)
# assert result.value is not None, (result.value, expression)
if error_string == 'success':
error_string = None
# attempt to find expression as a variable, if found, take the variable
# obj's type information as it's 'usually' more accurate.
var_result = self._thread.GetFrameAtIndex(frame_idx).FindVariable(expression)
if str(var_result.error) == 'success':
type_name = var_result.type.GetDisplayTypeName()
else:
type_name = result.type.GetDisplayTypeName()
return ValueIR(
expression=expression,
value=value,
type_name=type_name,
error_string=error_string,
could_evaluate=could_evaluate,
is_optimized_away=is_optimized_away,
is_irretrievable=is_irretrievable,
)