Skip to content

Commit 8b05c0b

Browse files
committed
[lldb] Improve breakpoint management for interactive scripted process
This patch improves breakpoint management when doing interactive scripted process debugging. In other to know which process set a breakpoint, we need to do some book keeping on the multiplexer scripted process. When initializing the multiplexer, we will first copy breakpoints that are already set on the driving target. Everytime we launch or resume, we should copy breakpoints from the multiplexer to the driving process. When creating a breakpoint from a child process, it needs to be set both on the multiplexer and on the driving process. We also tag the created breakpoint with the name and pid of the originator process. This patch also implements all the requirement to achieve proper breakpoint management. That involves: - Adding python interator for breakpoints and watchpoints in SBTarget - Add a new `ScriptedProcess.create_breakpoint` python method Differential Revision: https://reviews.llvm.org/D148548 Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent e67a700 commit 8b05c0b

File tree

16 files changed

+243
-4
lines changed

16 files changed

+243
-4
lines changed

lldb/bindings/interface/SBTarget.i

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ public:
10491049

10501050
def get_modules_access_object(self):
10511051
'''An accessor function that returns a modules_access() object which allows lazy module access from a lldb.SBTarget object.'''
1052-
return self.modules_access (self)
1052+
return self.modules_access(self)
10531053

10541054
def get_modules_array(self):
10551055
'''An accessor function that returns a list() that contains all modules in a lldb.SBTarget object.'''
@@ -1068,18 +1068,81 @@ public:
10681068
object.'''
10691069
return lldb_iter(self, 'GetNumBreakpoints', 'GetBreakpointAtIndex')
10701070

1071+
class bkpts_access(object):
1072+
'''A helper object that will lazily hand out bkpts for a target when supplied an index.'''
1073+
def __init__(self, sbtarget):
1074+
self.sbtarget = sbtarget
1075+
1076+
def __len__(self):
1077+
if self.sbtarget:
1078+
return int(self.sbtarget.GetNumBreakpoints())
1079+
return 0
1080+
1081+
def __getitem__(self, key):
1082+
if isinstance(key, int):
1083+
count = len(self)
1084+
if -count <= key < count:
1085+
key %= count
1086+
return self.sbtarget.GetBreakpointAtIndex(key)
1087+
return None
1088+
1089+
def get_bkpts_access_object(self):
1090+
'''An accessor function that returns a bkpts_access() object which allows lazy bkpt access from a lldb.SBtarget object.'''
1091+
return self.bkpts_access(self)
1092+
1093+
def get_target_bkpts(self):
1094+
'''An accessor function that returns a list() that contains all bkpts in a lldb.SBtarget object.'''
1095+
bkpts = []
1096+
for idx in range(self.GetNumBreakpoints()):
1097+
bkpts.append(self.GetBreakpointAtIndex(idx))
1098+
return bkpts
1099+
10711100
def watchpoint_iter(self):
10721101
'''Returns an iterator over all watchpoints in a lldb.SBTarget
10731102
object.'''
10741103
return lldb_iter(self, 'GetNumWatchpoints', 'GetWatchpointAtIndex')
10751104

1105+
class watchpoints_access(object):
1106+
'''A helper object that will lazily hand out watchpoints for a target when supplied an index.'''
1107+
def __init__(self, sbtarget):
1108+
self.sbtarget = sbtarget
1109+
1110+
def __len__(self):
1111+
if self.sbtarget:
1112+
return int(self.sbtarget.GetNumWatchpoints())
1113+
return 0
1114+
1115+
def __getitem__(self, key):
1116+
if isinstance(key, int):
1117+
count = len(self)
1118+
if -count <= key < count:
1119+
key %= count
1120+
return self.sbtarget.GetWatchpointAtIndex(key)
1121+
return None
1122+
1123+
def get_watchpoints_access_object(self):
1124+
'''An accessor function that returns a watchpoints_access() object which allows lazy watchpoint access from a lldb.SBtarget object.'''
1125+
return self.watchpoints_access(self)
1126+
1127+
def get_target_watchpoints(self):
1128+
'''An accessor function that returns a list() that contains all watchpoints in a lldb.SBtarget object.'''
1129+
watchpoints = []
1130+
for idx in range(self.GetNumWatchpoints()):
1131+
bkpts.append(self.GetWatchpointAtIndex(idx))
1132+
return watchpoints
1133+
1134+
10761135
modules = property(get_modules_array, None, doc='''A read only property that returns a list() of lldb.SBModule objects contained in this target. This list is a list all modules that the target currently is tracking (the main executable and all dependent shared libraries).''')
10771136
module = property(get_modules_access_object, None, doc=r'''A read only property that returns an object that implements python operator overloading with the square brackets().\n target.module[<int>] allows array access to any modules.\n target.module[<str>] allows access to modules by basename, full path, or uuid string value.\n target.module[uuid.UUID()] allows module access by UUID.\n target.module[re] allows module access using a regular expression that matches the module full path.''')
10781137
process = property(GetProcess, None, doc='''A read only property that returns an lldb object that represents the process (lldb.SBProcess) that this target owns.''')
10791138
executable = property(GetExecutable, None, doc='''A read only property that returns an lldb object that represents the main executable module (lldb.SBModule) for this target.''')
10801139
debugger = property(GetDebugger, None, doc='''A read only property that returns an lldb object that represents the debugger (lldb.SBDebugger) that owns this target.''')
10811140
num_breakpoints = property(GetNumBreakpoints, None, doc='''A read only property that returns the number of breakpoints that this target has as an integer.''')
1141+
breakpoints = property(get_target_bkpts, None, doc='''A read only property that returns a list() of lldb.SBBreakpoint objects for all breakpoints in this target.''')
1142+
breakpoint = property(get_bkpts_access_object, None, doc='''A read only property that returns an object that can be used to access breakpoints as an array ("bkpt_12 = lldb.target.bkpt[12]").''')
10821143
num_watchpoints = property(GetNumWatchpoints, None, doc='''A read only property that returns the number of watchpoints that this target has as an integer.''')
1144+
watchpoints = property(get_target_watchpoints, None, doc='''A read only property that returns a list() of lldb.SBwatchpoint objects for all watchpoints in this target.''')
1145+
watchpoint = property(get_watchpoints_access_object, None, doc='''A read only property that returns an object that can be used to access watchpoints as an array ("watchpoint_12 = lldb.target.watchpoint[12]").''')
10831146
broadcaster = property(GetBroadcaster, None, doc='''A read only property that an lldb object that represents the broadcaster (lldb.SBBroadcaster) for this target.''')
10841147
byte_order = property(GetByteOrder, None, doc='''A read only property that returns an lldb enumeration value (lldb.eByteOrderLittle, lldb.eByteOrderBig, lldb.eByteOrderInvalid) that represents the byte order for this target.''')
10851148
addr_size = property(GetAddressByteSize, None, doc='''A read only property that returns the size in bytes of an address for this target.''')

lldb/bindings/python/python-wrapper.swig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,18 @@ void *lldb_private::LLDBSWIGPython_CastPyObjectToSBData(PyObject * data) {
690690
return sb_ptr;
691691
}
692692

693+
void *lldb_private::LLDBSWIGPython_CastPyObjectToSBBreakpoint(PyObject * data) {
694+
lldb::SBBreakpoint *sb_ptr = nullptr;
695+
696+
int valid_cast =
697+
SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBBreakpoint, 0);
698+
699+
if (valid_cast == -1)
700+
return NULL;
701+
702+
return sb_ptr;
703+
}
704+
693705
void *lldb_private::LLDBSWIGPython_CastPyObjectToSBAttachInfo(PyObject * data) {
694706
lldb::SBAttachInfo *sb_ptr = nullptr;
695707

lldb/examples/python/scripted_process/scripted_process.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,23 @@ def get_process_metadata(self):
208208
"""
209209
return self.metadata
210210

211+
def create_breakpoint(self, addr, error):
212+
""" Create a breakpoint in the scripted process from an address.
213+
This is mainly used with interactive scripted process debugging.
214+
215+
Args:
216+
addr (int): Address at which the breakpoint should be set.
217+
error (lldb.SBError): Error object.
218+
219+
Returns:
220+
SBBreakpoint: A valid breakpoint object that was created a the specified
221+
address. None if the breakpoint creation failed.
222+
"""
223+
error.SetErrorString("%s doesn't support creating breakpoints."
224+
% self.__class__.__name__)
225+
return False
226+
227+
211228
class ScriptedThread(metaclass=ABCMeta):
212229

213230
"""

lldb/include/lldb/API/SBBreakpoint.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
class SBBreakpointListImpl;
1515

16+
namespace lldb_private {
17+
class ScriptInterpreter;
18+
}
19+
1620
namespace lldb {
1721

1822
class LLDB_API SBBreakpoint {
@@ -151,6 +155,8 @@ class LLDB_API SBBreakpoint {
151155
friend class SBBreakpointName;
152156
friend class SBTarget;
153157

158+
friend class lldb_private::ScriptInterpreter;
159+
154160
lldb::BreakpointSP GetSP() const;
155161

156162
lldb::BreakpointWP m_opaque_wp;

lldb/include/lldb/Interpreter/ScriptInterpreter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLDB_INTERPRETER_SCRIPTINTERPRETER_H
1111

1212
#include "lldb/API/SBAttachInfo.h"
13+
#include "lldb/API/SBBreakpoint.h"
1314
#include "lldb/API/SBData.h"
1415
#include "lldb/API/SBError.h"
1516
#include "lldb/API/SBLaunchInfo.h"
@@ -570,6 +571,9 @@ class ScriptInterpreter : public PluginInterface {
570571

571572
Status GetStatusFromSBError(const lldb::SBError &error) const;
572573

574+
lldb::BreakpointSP
575+
GetOpaqueTypeFromSBBreakpoint(const lldb::SBBreakpoint &breakpoint) const;
576+
573577
lldb::ProcessAttachInfoSP
574578
GetOpaqueTypeFromSBAttachInfo(const lldb::SBAttachInfo &attach_info) const;
575579

lldb/include/lldb/Interpreter/ScriptedProcessInterface.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class ScriptedProcessInterface : virtual public ScriptedInterface {
4545

4646
virtual StructuredData::DictionarySP GetThreadsInfo() { return {}; }
4747

48+
virtual bool CreateBreakpoint(lldb::addr_t addr, Status &error) {
49+
error.SetErrorString("ScriptedProcess don't support creating breakpoints.");
50+
return {};
51+
}
52+
4853
virtual lldb::DataExtractorSP
4954
ReadMemoryAtAddress(lldb::addr_t address, size_t size, Status &error) {
5055
return {};

lldb/source/Interpreter/ScriptInterpreter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ ScriptInterpreter::GetDataExtractorFromSBData(const lldb::SBData &data) const {
7676
return data.m_opaque_sp;
7777
}
7878

79+
lldb::BreakpointSP ScriptInterpreter::GetOpaqueTypeFromSBBreakpoint(
80+
const lldb::SBBreakpoint &breakpoint) const {
81+
return breakpoint.m_opaque_wp.lock();
82+
}
83+
7984
lldb::ProcessAttachInfoSP ScriptInterpreter::GetOpaqueTypeFromSBAttachInfo(
8085
const lldb::SBAttachInfo &attach_info) const {
8186
return attach_info.m_opaque_sp;

lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ Status ScriptedProcess::EnableBreakpointSite(BreakpointSite *bp_site) {
257257
return Status("Scripted Processes don't support hardware breakpoints");
258258
}
259259

260-
return EnableSoftwareBreakpoint(bp_site);
260+
Status error;
261+
GetInterface().CreateBreakpoint(bp_site->GetLoadAddress(), error);
262+
263+
return error;
261264
}
262265

263266
ArchSpec ScriptedProcess::GetArchitecture() {

lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ python::ScopedPythonObject<lldb::SBEvent> ToSWIGWrapper(Event *event);
9191
} // namespace python
9292

9393
void *LLDBSWIGPython_CastPyObjectToSBData(PyObject *data);
94+
void *LLDBSWIGPython_CastPyObjectToSBBreakpoint(PyObject *data);
9495
void *LLDBSWIGPython_CastPyObjectToSBAttachInfo(PyObject *data);
9596
void *LLDBSWIGPython_CastPyObjectToSBLaunchInfo(PyObject *data);
9697
void *LLDBSWIGPython_CastPyObjectToSBError(PyObject *data);

lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ StructuredData::DictionarySP ScriptedProcessPythonInterface::GetThreadsInfo() {
109109
return dict;
110110
}
111111

112+
bool ScriptedProcessPythonInterface::CreateBreakpoint(lldb::addr_t addr,
113+
Status &error) {
114+
Status py_error;
115+
StructuredData::ObjectSP obj =
116+
Dispatch("create_breakpoint", py_error, addr, error);
117+
118+
// If there was an error on the python call, surface it to the user.
119+
if (py_error.Fail())
120+
error = py_error;
121+
122+
if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
123+
return {};
124+
125+
return obj->GetBooleanValue();
126+
}
127+
112128
lldb::DataExtractorSP ScriptedProcessPythonInterface::ReadMemoryAtAddress(
113129
lldb::addr_t address, size_t size, Status &error) {
114130
Status py_error;

0 commit comments

Comments
 (0)