Skip to content

Commit 56ba282

Browse files
committed
[lldb/Interpreter] Make use of ScriptedStopHook{,Python}Interface
This patch makes use of the previously defined `ScriptedStopHook{,Python}Interface` in the `StopHookScripted` class and removes all the SWIG methods that were implemented specifically for this. Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent 3387de3 commit 56ba282

File tree

13 files changed

+92
-215
lines changed

13 files changed

+92
-215
lines changed

lldb/bindings/python/python-wrapper.swig

Lines changed: 13 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -301,104 +301,6 @@ unsigned int lldb_private::python::SWIGBridge::LLDBSwigPythonCallBreakpointResol
301301
return ret_val;
302302
}
303303

304-
PythonObject lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedStopHook(
305-
lldb::TargetSP target_sp, const char *python_class_name,
306-
const char *session_dictionary_name, const StructuredDataImpl &args_impl,
307-
Status &error) {
308-
if (python_class_name == NULL || python_class_name[0] == '\0') {
309-
error = Status::FromErrorString("Empty class name.");
310-
return PythonObject();
311-
}
312-
if (!session_dictionary_name) {
313-
error = Status::FromErrorString("No session dictionary");
314-
return PythonObject();
315-
}
316-
317-
PyErr_Cleaner py_err_cleaner(true);
318-
319-
auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
320-
session_dictionary_name);
321-
auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
322-
python_class_name, dict);
323-
324-
if (!pfunc.IsAllocated()) {
325-
error = Status::FromErrorStringWithFormat("Could not find class: %s.",
326-
python_class_name);
327-
return PythonObject();
328-
}
329-
330-
PythonObject result =
331-
pfunc(SWIGBridge::ToSWIGWrapper(target_sp), SWIGBridge::ToSWIGWrapper(args_impl), dict);
332-
333-
if (result.IsAllocated()) {
334-
// Check that the handle_stop callback is defined:
335-
auto callback_func = result.ResolveName<PythonCallable>("handle_stop");
336-
if (callback_func.IsAllocated()) {
337-
if (auto args_info = callback_func.GetArgInfo()) {
338-
size_t num_args = (*args_info).max_positional_args;
339-
if (num_args != 2) {
340-
error = Status::FromErrorStringWithFormat(
341-
"Wrong number of args for "
342-
"handle_stop callback, should be 2 (excluding self), got: %zu",
343-
num_args);
344-
return PythonObject();
345-
} else
346-
return result;
347-
} else {
348-
error = Status::FromErrorString(
349-
"Couldn't get num arguments for handle_stop "
350-
"callback.");
351-
return PythonObject();
352-
}
353-
return result;
354-
} else {
355-
error = Status::FromErrorStringWithFormat(
356-
"Class \"%s\" is missing the required "
357-
"handle_stop callback.",
358-
python_class_name);
359-
}
360-
}
361-
return PythonObject();
362-
}
363-
364-
bool lldb_private::python::SWIGBridge::LLDBSwigPythonStopHookCallHandleStop(
365-
void *implementor, lldb::ExecutionContextRefSP exc_ctx_sp,
366-
lldb::StreamSP stream) {
367-
// handle_stop will return a bool with the meaning "should_stop"...
368-
// If you return nothing we'll assume we are going to stop.
369-
// Also any errors should return true, since we should stop on error.
370-
371-
PyErr_Cleaner py_err_cleaner(false);
372-
PythonObject self(PyRefType::Borrowed, static_cast<PyObject *>(implementor));
373-
auto pfunc = self.ResolveName<PythonCallable>("handle_stop");
374-
375-
if (!pfunc.IsAllocated())
376-
return true;
377-
378-
std::shared_ptr<lldb::SBStream> sb_stream = std::make_shared<lldb::SBStream>();
379-
PythonObject sb_stream_arg = SWIGBridge::ToSWIGWrapper(sb_stream);
380-
PythonObject result =
381-
pfunc(SWIGBridge::ToSWIGWrapper(std::move(exc_ctx_sp)), sb_stream_arg);
382-
383-
if (PyErr_Occurred()) {
384-
stream->PutCString("Python error occurred handling stop-hook.");
385-
PyErr_Print();
386-
PyErr_Clear();
387-
return true;
388-
}
389-
390-
// Now add the result to the output stream. SBStream only
391-
// makes an internally help StreamString which I can't interpose, so I
392-
// have to copy it over here.
393-
stream->PutCString(sb_stream->GetData());
394-
sb_stream_arg.release();
395-
396-
if (result.get() == Py_False)
397-
return false;
398-
else
399-
return true;
400-
}
401-
402304
// wrapper that calls an optional instance member of an object taking no
403305
// arguments
404306
static PyObject *LLDBSwigPython_CallOptionalMember(
@@ -677,6 +579,19 @@ void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyOb
677579
return sb_ptr;
678580
}
679581

582+
void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBExecutionContext(PyObject *
583+
data) {
584+
lldb::SBExecutionContext *sb_ptr = NULL;
585+
586+
int valid_cast = SWIG_ConvertPtr(data, (void **)&sb_ptr,
587+
SWIGTYPE_p_lldb__SBExecutionContext, 0);
588+
589+
if (valid_cast == -1)
590+
return NULL;
591+
592+
return sb_ptr;
593+
}
594+
680595
bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommand(
681596
const char *python_function_name, const char *session_dictionary_name,
682597
lldb::DebuggerSP debugger, const char *args,

lldb/include/lldb/API/SBExecutionContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <vector>
1717

1818
namespace lldb_private {
19+
class ScriptInterpreter;
1920
namespace python {
2021
class SWIGBridge;
2122
}
@@ -55,6 +56,7 @@ class LLDB_API SBExecutionContext {
5556

5657
protected:
5758
friend class lldb_private::python::SWIGBridge;
59+
friend class lldb_private::ScriptInterpreter;
5860

5961
lldb_private::ExecutionContextRef *get() const;
6062

lldb/include/lldb/Interpreter/ScriptInterpreter.h

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "lldb/API/SBData.h"
1515
#include "lldb/API/SBError.h"
1616
#include "lldb/API/SBEvent.h"
17+
#include "lldb/API/SBExecutionContext.h"
1718
#include "lldb/API/SBLaunchInfo.h"
1819
#include "lldb/API/SBMemoryRegionInfo.h"
1920
#include "lldb/API/SBStream.h"
@@ -271,24 +272,6 @@ class ScriptInterpreter : public PluginInterface {
271272
return lldb::eSearchDepthModule;
272273
}
273274

274-
virtual StructuredData::GenericSP
275-
CreateScriptedStopHook(lldb::TargetSP target_sp, const char *class_name,
276-
const StructuredDataImpl &args_data, Status &error) {
277-
error =
278-
Status::FromErrorString("Creating scripted stop-hooks with the current "
279-
"script interpreter is not supported.");
280-
return StructuredData::GenericSP();
281-
}
282-
283-
// This dispatches to the handle_stop method of the stop-hook class. It
284-
// returns a "should_stop" bool.
285-
virtual bool
286-
ScriptedStopHookHandleStop(StructuredData::GenericSP implementor_sp,
287-
ExecutionContext &exc_ctx,
288-
lldb::StreamSP stream_sp) {
289-
return true;
290-
}
291-
292275
virtual StructuredData::ObjectSP
293276
LoadPluginModule(const FileSpec &file_spec, lldb_private::Status &error) {
294277
return StructuredData::ObjectSP();
@@ -591,6 +574,9 @@ class ScriptInterpreter : public PluginInterface {
591574
std::optional<MemoryRegionInfo> GetOpaqueTypeFromSBMemoryRegionInfo(
592575
const lldb::SBMemoryRegionInfo &mem_region) const;
593576

577+
lldb::ExecutionContextRefSP GetOpaqueTypeFromSBExecutionContext(
578+
const lldb::SBExecutionContext &exe_ctx) const;
579+
594580
protected:
595581
Debugger &m_debugger;
596582
lldb::ScriptLanguage m_script_lang;

lldb/include/lldb/Target/Target.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,8 +1391,7 @@ class Target : public std::enable_shared_from_this<Target>,
13911391
/// This holds the dictionary of keys & values that can be used to
13921392
/// parametrize any given callback's behavior.
13931393
StructuredDataImpl m_extra_args;
1394-
/// This holds the python callback object.
1395-
StructuredData::GenericSP m_implementation_sp;
1394+
lldb::ScriptedStopHookInterfaceSP m_interface_sp;
13961395

13971396
/// Use CreateStopHook to make a new empty stop hook. The GetCommandPointer
13981397
/// and fill it with commands, and SetSpecifier to set the specifier shared

lldb/source/Interpreter/ScriptInterpreter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ ScriptInterpreter::GetOpaqueTypeFromSBMemoryRegionInfo(
125125
return *mem_region.m_opaque_up.get();
126126
}
127127

128+
lldb::ExecutionContextRefSP
129+
ScriptInterpreter::GetOpaqueTypeFromSBExecutionContext(
130+
const lldb::SBExecutionContext &exe_ctx) const {
131+
return exe_ctx.m_exe_ctx_sp;
132+
}
133+
128134
lldb::ScriptLanguage
129135
ScriptInterpreter::StringToLanguage(const llvm::StringRef &language) {
130136
if (language.equals_insensitive(LanguageToString(eScriptLanguageNone)))

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,23 @@ ScriptedPythonInterface::ExtractValueFromPythonObject<
159159
return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
160160
}
161161

162+
template <>
163+
lldb::ExecutionContextRefSP
164+
ScriptedPythonInterface::ExtractValueFromPythonObject<
165+
lldb::ExecutionContextRefSP>(python::PythonObject &p, Status &error) {
166+
167+
lldb::SBExecutionContext *sb_exe_ctx =
168+
reinterpret_cast<lldb::SBExecutionContext *>(
169+
python::LLDBSWIGPython_CastPyObjectToSBExecutionContext(p.get()));
170+
171+
if (!sb_exe_ctx) {
172+
error = Status::FromErrorStringWithFormat(
173+
"Couldn't cast lldb::SBExecutionContext to "
174+
"lldb::ExecutionContextRefSP.");
175+
return {};
176+
}
177+
178+
return m_interpreter.GetOpaqueTypeFromSBExecutionContext(*sb_exe_ctx);
179+
}
180+
162181
#endif

lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPythonInterface.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,10 @@ class ScriptedPythonInterface : virtual public ScriptedInterface {
405405
return python::SWIGBridge::ToSWIGWrapper(arg);
406406
}
407407

408+
python::PythonObject Transform(lldb::TargetSP arg) {
409+
return python::SWIGBridge::ToSWIGWrapper(arg);
410+
}
411+
408412
python::PythonObject Transform(lldb::ProcessSP arg) {
409413
return python::SWIGBridge::ToSWIGWrapper(arg);
410414
}
@@ -557,6 +561,11 @@ std::optional<MemoryRegionInfo>
557561
ScriptedPythonInterface::ExtractValueFromPythonObject<
558562
std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error);
559563

564+
template <>
565+
lldb::ExecutionContextRefSP
566+
ScriptedPythonInterface::ExtractValueFromPythonObject<
567+
lldb::ExecutionContextRefSP>(python::PythonObject &p, Status &error);
568+
560569
} // namespace lldb_private
561570

562571
#endif // LLDB_ENABLE_PYTHON

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,6 @@ class SWIGBridge {
157157
const char *method_name,
158158
lldb_private::SymbolContext *sym_ctx);
159159

160-
static python::PythonObject LLDBSwigPythonCreateScriptedStopHook(
161-
lldb::TargetSP target_sp, const char *python_class_name,
162-
const char *session_dictionary_name, const StructuredDataImpl &args,
163-
lldb_private::Status &error);
164-
165-
static bool
166-
LLDBSwigPythonStopHookCallHandleStop(void *implementor,
167-
lldb::ExecutionContextRefSP exc_ctx,
168-
lldb::StreamSP stream);
169-
170160
static size_t LLDBSwigPython_CalculateNumChildren(PyObject *implementor,
171161
uint32_t max);
172162

@@ -266,6 +256,7 @@ void *LLDBSWIGPython_CastPyObjectToSBEvent(PyObject *data);
266256
void *LLDBSWIGPython_CastPyObjectToSBStream(PyObject *data);
267257
void *LLDBSWIGPython_CastPyObjectToSBValue(PyObject *data);
268258
void *LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data);
259+
void *LLDBSWIGPython_CastPyObjectToSBExecutionContext(PyObject *data);
269260
} // namespace python
270261

271262
} // namespace lldb_private

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

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,57 +1659,6 @@ ScriptInterpreterPythonImpl::ScriptedBreakpointResolverSearchDepth(
16591659
return lldb::eSearchDepthModule;
16601660
}
16611661

1662-
StructuredData::GenericSP ScriptInterpreterPythonImpl::CreateScriptedStopHook(
1663-
TargetSP target_sp, const char *class_name,
1664-
const StructuredDataImpl &args_data, Status &error) {
1665-
1666-
if (!target_sp) {
1667-
error = Status::FromErrorString("No target for scripted stop-hook.");
1668-
return StructuredData::GenericSP();
1669-
}
1670-
1671-
if (class_name == nullptr || class_name[0] == '\0') {
1672-
error = Status::FromErrorString("No class name for scripted stop-hook.");
1673-
return StructuredData::GenericSP();
1674-
}
1675-
1676-
ScriptInterpreterPythonImpl *python_interpreter =
1677-
GetPythonInterpreter(m_debugger);
1678-
1679-
if (!python_interpreter) {
1680-
error = Status::FromErrorString(
1681-
"No script interpreter for scripted stop-hook.");
1682-
return StructuredData::GenericSP();
1683-
}
1684-
1685-
Locker py_lock(this,
1686-
Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1687-
1688-
PythonObject ret_val = SWIGBridge::LLDBSwigPythonCreateScriptedStopHook(
1689-
target_sp, class_name, python_interpreter->m_dictionary_name.c_str(),
1690-
args_data, error);
1691-
1692-
return StructuredData::GenericSP(
1693-
new StructuredPythonObject(std::move(ret_val)));
1694-
}
1695-
1696-
bool ScriptInterpreterPythonImpl::ScriptedStopHookHandleStop(
1697-
StructuredData::GenericSP implementor_sp, ExecutionContext &exc_ctx,
1698-
lldb::StreamSP stream_sp) {
1699-
assert(implementor_sp &&
1700-
"can't call a stop hook with an invalid implementor");
1701-
assert(stream_sp && "can't call a stop hook with an invalid stream");
1702-
1703-
Locker py_lock(this,
1704-
Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1705-
1706-
lldb::ExecutionContextRefSP exc_ctx_ref_sp(new ExecutionContextRef(exc_ctx));
1707-
1708-
bool ret_val = SWIGBridge::LLDBSwigPythonStopHookCallHandleStop(
1709-
implementor_sp->GetValue(), exc_ctx_ref_sp, stream_sp);
1710-
return ret_val;
1711-
}
1712-
17131662
StructuredData::ObjectSP
17141663
ScriptInterpreterPythonImpl::LoadPluginModule(const FileSpec &file_spec,
17151664
lldb_private::Status &error) {

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,6 @@ class ScriptInterpreterPythonImpl : public ScriptInterpreterPython {
9191
lldb::SearchDepth ScriptedBreakpointResolverSearchDepth(
9292
StructuredData::GenericSP implementor_sp) override;
9393

94-
StructuredData::GenericSP
95-
CreateScriptedStopHook(lldb::TargetSP target_sp, const char *class_name,
96-
const StructuredDataImpl &args_data,
97-
Status &error) override;
98-
99-
bool ScriptedStopHookHandleStop(StructuredData::GenericSP implementor_sp,
100-
ExecutionContext &exc_ctx,
101-
lldb::StreamSP stream_sp) override;
102-
10394
StructuredData::GenericSP
10495
CreateFrameRecognizer(const char *class_name) override;
10596

0 commit comments

Comments
 (0)