Skip to content

Commit 4c8e79f

Browse files
authored
Switch the ScriptedBreakpointResolver over to the ScriptedInterface form (#150720)
This is NFC, I'm modernizing the interface before I add to it in a subsequent commit.
1 parent 6bcff9e commit 4c8e79f

22 files changed

+328
-217
lines changed

lldb/bindings/python/python-wrapper.swig

Lines changed: 12 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -229,78 +229,6 @@ PythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateCommandObject
229229
return pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger_sp)), dict);
230230
}
231231

232-
PythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedBreakpointResolver(
233-
const char *python_class_name, const char *session_dictionary_name,
234-
const StructuredDataImpl &args_impl,
235-
const lldb::BreakpointSP &breakpoint_sp) {
236-
237-
if (python_class_name == NULL || python_class_name[0] == '\0' ||
238-
!session_dictionary_name)
239-
return PythonObject();
240-
241-
PyErr_Cleaner py_err_cleaner(true);
242-
243-
auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
244-
session_dictionary_name);
245-
auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
246-
python_class_name, dict);
247-
248-
if (!pfunc.IsAllocated())
249-
return PythonObject();
250-
251-
PythonObject result =
252-
pfunc(SWIGBridge::ToSWIGWrapper(breakpoint_sp), SWIGBridge::ToSWIGWrapper(args_impl), dict);
253-
// FIXME: At this point we should check that the class we found supports all
254-
// the methods that we need.
255-
256-
if (result.IsAllocated()) {
257-
// Check that __callback__ is defined:
258-
auto callback_func = result.ResolveName<PythonCallable>("__callback__");
259-
if (callback_func.IsAllocated())
260-
return result;
261-
}
262-
return PythonObject();
263-
}
264-
265-
unsigned int lldb_private::python::SWIGBridge::LLDBSwigPythonCallBreakpointResolver(
266-
void *implementor, const char *method_name,
267-
lldb_private::SymbolContext *sym_ctx) {
268-
PyErr_Cleaner py_err_cleaner(false);
269-
PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
270-
auto pfunc = self.ResolveName<PythonCallable>(method_name);
271-
272-
if (!pfunc.IsAllocated())
273-
return 0;
274-
275-
PythonObject result = sym_ctx ? pfunc(SWIGBridge::ToSWIGWrapper(*sym_ctx)) : pfunc();
276-
277-
if (PyErr_Occurred()) {
278-
PyErr_Print();
279-
PyErr_Clear();
280-
return 0;
281-
}
282-
283-
// The callback will return a bool, but we're need to also return ints
284-
// so we're squirrelling the bool through as an int... And if you return
285-
// nothing, we'll continue.
286-
if (strcmp(method_name, "__callback__") == 0) {
287-
if (result.get() == Py_False)
288-
return 0;
289-
else
290-
return 1;
291-
}
292-
293-
long long ret_val = unwrapOrSetPythonException(As<long long>(result));
294-
295-
if (PyErr_Occurred()) {
296-
PyErr_Print();
297-
PyErr_Clear();
298-
return 0;
299-
}
300-
301-
return ret_val;
302-
}
303-
304232
// wrapper that calls an optional instance member of an object taking no
305233
// arguments
306234
static PyObject *LLDBSwigPython_CallOptionalMember(
@@ -554,6 +482,18 @@ void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBStream(PyObject * dat
554482
return sb_ptr;
555483
}
556484

485+
void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBSymbolContext(PyObject * data) {
486+
lldb::SBSymbolContext *sb_ptr = nullptr;
487+
488+
int valid_cast =
489+
SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBSymbolContext, 0);
490+
491+
if (valid_cast == -1)
492+
return NULL;
493+
494+
return sb_ptr;
495+
}
496+
557497
void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBValue(PyObject * data) {
558498
lldb::SBValue *sb_ptr = NULL;
559499

lldb/include/lldb/API/SBSymbolContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class LLDB_API SBSymbolContext {
8080

8181
lldb_private::SymbolContext *get() const;
8282

83+
friend class lldb_private::ScriptInterpreter;
84+
8385
private:
8486
std::unique_ptr<lldb_private::SymbolContext> m_opaque_up;
8587
};

lldb/include/lldb/Breakpoint/BreakpointResolverScripted.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "lldb/Breakpoint/BreakpointResolver.h"
1313
#include "lldb/Core/ModuleSpec.h"
1414
#include "lldb/Core/StructuredDataImpl.h"
15+
#include "lldb/Interpreter/Interfaces/ScriptedBreakpointInterface.h"
1516
#include "lldb/lldb-forward.h"
1617

1718
namespace lldb_private {
@@ -64,7 +65,8 @@ class BreakpointResolverScripted : public BreakpointResolver {
6465
std::string m_class_name;
6566
lldb::SearchDepth m_depth;
6667
StructuredDataImpl m_args;
67-
StructuredData::GenericSP m_implementation_sp;
68+
Status m_error;
69+
lldb::ScriptedBreakpointInterfaceSP m_interface_sp;
6870

6971
BreakpointResolverScripted(const BreakpointResolverScripted &) = delete;
7072
const BreakpointResolverScripted &
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLDB_INTERPRETER_INTERFACES_SCRIPTEDBREAKPOINTINTERFACE_H
10+
#define LLDB_INTERPRETER_INTERFACES_SCRIPTEDBREAKPOINTINTERFACE_H
11+
12+
#include "ScriptedInterface.h"
13+
#include "lldb/Symbol/SymbolContext.h"
14+
#include "lldb/lldb-private.h"
15+
16+
namespace lldb_private {
17+
class ScriptedBreakpointInterface : public ScriptedInterface {
18+
public:
19+
virtual llvm::Expected<StructuredData::GenericSP>
20+
CreatePluginObject(llvm::StringRef class_name, lldb::BreakpointSP break_sp,
21+
const StructuredDataImpl &args_sp) = 0;
22+
23+
/// "ResolverCallback" will get called when a new module is loaded. The
24+
/// new module information is passed in sym_ctx. The Resolver will add
25+
/// any breakpoint locations it found in that module.
26+
virtual bool ResolverCallback(SymbolContext sym_ctx) { return true; }
27+
virtual lldb::SearchDepth GetDepth() { return lldb::eSearchDepthModule; }
28+
virtual std::optional<std::string> GetShortHelp() { return nullptr; }
29+
};
30+
} // namespace lldb_private
31+
32+
#endif // LLDB_INTERPRETER_INTERFACES_SCRIPTEDSTOPHOOKINTERFACE_H

lldb/include/lldb/Interpreter/ScriptInterpreter.h

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "lldb/API/SBLaunchInfo.h"
1919
#include "lldb/API/SBMemoryRegionInfo.h"
2020
#include "lldb/API/SBStream.h"
21+
#include "lldb/API/SBSymbolContext.h"
2122
#include "lldb/Breakpoint/BreakpointOptions.h"
2223
#include "lldb/Core/PluginInterface.h"
2324
#include "lldb/Core/SearchFilter.h"
@@ -29,6 +30,7 @@
2930
#include "lldb/Interpreter/Interfaces/ScriptedProcessInterface.h"
3031
#include "lldb/Interpreter/Interfaces/ScriptedThreadInterface.h"
3132
#include "lldb/Interpreter/ScriptObject.h"
33+
#include "lldb/Symbol/SymbolContext.h"
3234
#include "lldb/Utility/Broadcaster.h"
3335
#include "lldb/Utility/Status.h"
3436
#include "lldb/Utility/StructuredData.h"
@@ -257,26 +259,6 @@ class ScriptInterpreter : public PluginInterface {
257259
return false;
258260
}
259261

260-
virtual StructuredData::GenericSP
261-
CreateScriptedBreakpointResolver(const char *class_name,
262-
const StructuredDataImpl &args_data,
263-
lldb::BreakpointSP &bkpt_sp) {
264-
return StructuredData::GenericSP();
265-
}
266-
267-
virtual bool
268-
ScriptedBreakpointResolverSearchCallback(StructuredData::GenericSP implementor_sp,
269-
SymbolContext *sym_ctx)
270-
{
271-
return false;
272-
}
273-
274-
virtual lldb::SearchDepth
275-
ScriptedBreakpointResolverSearchDepth(StructuredData::GenericSP implementor_sp)
276-
{
277-
return lldb::eSearchDepthModule;
278-
}
279-
280262
virtual StructuredData::ObjectSP
281263
LoadPluginModule(const FileSpec &file_spec, lldb_private::Status &error) {
282264
return StructuredData::ObjectSP();
@@ -566,6 +548,11 @@ class ScriptInterpreter : public PluginInterface {
566548
return {};
567549
}
568550

551+
virtual lldb::ScriptedBreakpointInterfaceSP
552+
CreateScriptedBreakpointInterface() {
553+
return {};
554+
}
555+
569556
virtual StructuredData::ObjectSP
570557
CreateStructuredDataFromScriptObject(ScriptObject obj) {
571558
return {};
@@ -580,6 +567,9 @@ class ScriptInterpreter : public PluginInterface {
580567

581568
lldb::StreamSP GetOpaqueTypeFromSBStream(const lldb::SBStream &stream) const;
582569

570+
SymbolContext
571+
GetOpaqueTypeFromSBSymbolContext(const lldb::SBSymbolContext &sym_ctx) const;
572+
583573
lldb::BreakpointSP
584574
GetOpaqueTypeFromSBBreakpoint(const lldb::SBBreakpoint &breakpoint) const;
585575

lldb/include/lldb/lldb-forward.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ class Scalar;
188188
class ScriptInterpreter;
189189
class ScriptInterpreterLocker;
190190
class ScriptedMetadata;
191+
class ScriptedBreakpointInterface;
191192
class ScriptedPlatformInterface;
192193
class ScriptedProcessInterface;
193194
class ScriptedStopHookInterface;
@@ -418,6 +419,8 @@ typedef std::shared_ptr<lldb_private::ScriptedThreadInterface>
418419
ScriptedThreadInterfaceSP;
419420
typedef std::shared_ptr<lldb_private::ScriptedThreadPlanInterface>
420421
ScriptedThreadPlanInterfaceSP;
422+
typedef std::shared_ptr<lldb_private::ScriptedBreakpointInterface>
423+
ScriptedBreakpointInterfaceSP;
421424
typedef std::shared_ptr<lldb_private::Section> SectionSP;
422425
typedef std::unique_ptr<lldb_private::SectionList> SectionListUP;
423426
typedef std::weak_ptr<lldb_private::Section> SectionWP;

lldb/source/Breakpoint/BreakpointResolverScripted.cpp

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ BreakpointResolverScripted::BreakpointResolverScripted(
3535

3636
void BreakpointResolverScripted::CreateImplementationIfNeeded(
3737
BreakpointSP breakpoint_sp) {
38-
if (m_implementation_sp)
38+
if (m_interface_sp)
3939
return;
4040

4141
if (m_class_name.empty())
@@ -50,8 +50,27 @@ void BreakpointResolverScripted::CreateImplementationIfNeeded(
5050
if (!script_interp)
5151
return;
5252

53-
m_implementation_sp = script_interp->CreateScriptedBreakpointResolver(
54-
m_class_name.c_str(), m_args, breakpoint_sp);
53+
m_interface_sp = script_interp->CreateScriptedBreakpointInterface();
54+
if (!m_interface_sp) {
55+
m_error = Status::FromErrorStringWithFormat(
56+
"BreakpointResolverScripted::%s () - ERROR: %s", __FUNCTION__,
57+
"Script interpreter couldn't create Scripted Breakpoint Interface");
58+
return;
59+
}
60+
61+
auto obj_or_err =
62+
m_interface_sp->CreatePluginObject(m_class_name, breakpoint_sp, m_args);
63+
if (!obj_or_err) {
64+
m_error = Status::FromError(obj_or_err.takeError());
65+
return;
66+
}
67+
68+
StructuredData::ObjectSP object_sp = *obj_or_err;
69+
if (!object_sp || !object_sp->IsValid()) {
70+
m_error = Status::FromErrorStringWithFormat(
71+
"ScriptedBreakpoint::%s () - ERROR: %s", __FUNCTION__,
72+
"Failed to create valid script object");
73+
}
5574
}
5675

5776
void BreakpointResolverScripted::NotifyBreakpointSet() {
@@ -104,13 +123,10 @@ ScriptInterpreter *BreakpointResolverScripted::GetScriptInterpreter() {
104123
Searcher::CallbackReturn BreakpointResolverScripted::SearchCallback(
105124
SearchFilter &filter, SymbolContext &context, Address *addr) {
106125
bool should_continue = true;
107-
if (!m_implementation_sp)
126+
if (!m_interface_sp)
108127
return Searcher::eCallbackReturnStop;
109128

110-
ScriptInterpreter *interp = GetScriptInterpreter();
111-
should_continue = interp->ScriptedBreakpointResolverSearchCallback(
112-
m_implementation_sp,
113-
&context);
129+
should_continue = m_interface_sp->ResolverCallback(context);
114130
if (should_continue)
115131
return Searcher::eCallbackReturnContinue;
116132

@@ -120,25 +136,21 @@ Searcher::CallbackReturn BreakpointResolverScripted::SearchCallback(
120136
lldb::SearchDepth
121137
BreakpointResolverScripted::GetDepth() {
122138
lldb::SearchDepth depth = lldb::eSearchDepthModule;
123-
if (m_implementation_sp) {
124-
ScriptInterpreter *interp = GetScriptInterpreter();
125-
depth = interp->ScriptedBreakpointResolverSearchDepth(
126-
m_implementation_sp);
127-
}
139+
if (m_interface_sp)
140+
depth = m_interface_sp->GetDepth();
141+
128142
return depth;
129143
}
130144

131145
void BreakpointResolverScripted::GetDescription(Stream *s) {
132146
StructuredData::GenericSP generic_sp;
133-
std::string short_help;
147+
std::optional<std::string> short_help;
134148

135-
if (m_implementation_sp) {
136-
ScriptInterpreter *interp = GetScriptInterpreter();
137-
interp->GetShortHelpForCommandObject(m_implementation_sp,
138-
short_help);
149+
if (m_interface_sp) {
150+
short_help = m_interface_sp->GetShortHelp();
139151
}
140-
if (!short_help.empty())
141-
s->PutCString(short_help.c_str());
152+
if (short_help && !short_help->empty())
153+
s->PutCString(short_help->c_str());
142154
else
143155
s->Printf("python class = %s", m_class_name.c_str());
144156
}

lldb/source/Interpreter/ScriptInterpreter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ lldb::StreamSP ScriptInterpreter::GetOpaqueTypeFromSBStream(
116116
return nullptr;
117117
}
118118

119+
SymbolContext ScriptInterpreter::GetOpaqueTypeFromSBSymbolContext(
120+
const lldb::SBSymbolContext &sb_sym_ctx) const {
121+
if (sb_sym_ctx.m_opaque_up)
122+
return *sb_sym_ctx.m_opaque_up.get();
123+
return {};
124+
}
125+
119126
std::optional<MemoryRegionInfo>
120127
ScriptInterpreter::GetOpaqueTypeFromSBMemoryRegionInfo(
121128
const lldb::SBMemoryRegionInfo &mem_region) const {

lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_lldb_library(lldbPluginScriptInterpreterPythonInterfaces PLUGIN
2626
ScriptedProcessPythonInterface.cpp
2727
ScriptedPythonInterface.cpp
2828
ScriptedStopHookPythonInterface.cpp
29+
ScriptedBreakpointPythonInterface.cpp
2930
ScriptedThreadPlanPythonInterface.cpp
3031
ScriptedThreadPythonInterface.cpp
3132

lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptInterpreterPythonInterfaces.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void ScriptInterpreterPythonInterfaces::Initialize() {
2929
ScriptedPlatformPythonInterface::Initialize();
3030
ScriptedProcessPythonInterface::Initialize();
3131
ScriptedStopHookPythonInterface::Initialize();
32+
ScriptedBreakpointPythonInterface::Initialize();
3233
ScriptedThreadPlanPythonInterface::Initialize();
3334
}
3435

@@ -37,6 +38,7 @@ void ScriptInterpreterPythonInterfaces::Terminate() {
3738
ScriptedPlatformPythonInterface::Terminate();
3839
ScriptedProcessPythonInterface::Terminate();
3940
ScriptedStopHookPythonInterface::Terminate();
41+
ScriptedBreakpointPythonInterface::Terminate();
4042
ScriptedThreadPlanPythonInterface::Terminate();
4143
}
4244

0 commit comments

Comments
 (0)