Skip to content

Commit 801046e

Browse files
committed
Revert "[lldb] Fix SWIG wrapper compilation error"
...and "[lldb/Interpreter] Introduce `ScriptedStopHook{,Python}Interface` & make use of it (#105449)" This reverts commit 76b827b, and commit 1e131dd because the first commit caused the test command-stop-hook-output.test to fail.
1 parent 413b12a commit 801046e

21 files changed

+221
-298
lines changed

lldb/bindings/python/python-wrapper.swig

Lines changed: 98 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,104 @@ 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+
304402
// wrapper that calls an optional instance member of an object taking no
305403
// arguments
306404
static PyObject *LLDBSwigPython_CallOptionalMember(
@@ -579,19 +677,6 @@ void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyOb
579677
return sb_ptr;
580678
}
581679

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-
595680
bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommand(
596681
const char *python_function_name, const char *session_dictionary_name,
597682
lldb::DebuggerSP debugger, const char *args,

lldb/include/lldb/API/SBExecutionContext.h

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

1818
namespace lldb_private {
19-
class ScriptInterpreter;
2019
namespace python {
2120
class SWIGBridge;
2221
}
@@ -56,7 +55,6 @@ class LLDB_API SBExecutionContext {
5655

5756
protected:
5857
friend class lldb_private::python::SWIGBridge;
59-
friend class lldb_private::ScriptInterpreter;
6058

6159
lldb_private::ExecutionContextRef *get() const;
6260

lldb/include/lldb/Interpreter/Interfaces/ScriptedStopHookInterface.h

Lines changed: 0 additions & 33 deletions
This file was deleted.

lldb/include/lldb/Interpreter/ScriptInterpreter.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "lldb/API/SBData.h"
1515
#include "lldb/API/SBError.h"
1616
#include "lldb/API/SBEvent.h"
17-
#include "lldb/API/SBExecutionContext.h"
1817
#include "lldb/API/SBLaunchInfo.h"
1918
#include "lldb/API/SBMemoryRegionInfo.h"
2019
#include "lldb/API/SBStream.h"
@@ -272,6 +271,24 @@ class ScriptInterpreter : public PluginInterface {
272271
return lldb::eSearchDepthModule;
273272
}
274273

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+
275292
virtual StructuredData::ObjectSP
276293
LoadPluginModule(const FileSpec &file_spec, lldb_private::Status &error) {
277294
return StructuredData::ObjectSP();
@@ -544,10 +561,6 @@ class ScriptInterpreter : public PluginInterface {
544561
return {};
545562
}
546563

547-
virtual lldb::ScriptedStopHookInterfaceSP CreateScriptedStopHookInterface() {
548-
return {};
549-
}
550-
551564
virtual StructuredData::ObjectSP
552565
CreateStructuredDataFromScriptObject(ScriptObject obj) {
553566
return {};
@@ -574,9 +587,6 @@ class ScriptInterpreter : public PluginInterface {
574587
std::optional<MemoryRegionInfo> GetOpaqueTypeFromSBMemoryRegionInfo(
575588
const lldb::SBMemoryRegionInfo &mem_region) const;
576589

577-
lldb::ExecutionContextRefSP GetOpaqueTypeFromSBExecutionContext(
578-
const lldb::SBExecutionContext &exe_ctx) const;
579-
580590
protected:
581591
Debugger &m_debugger;
582592
lldb::ScriptLanguage m_script_lang;

lldb/include/lldb/Target/Target.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,8 @@ 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-
lldb::ScriptedStopHookInterfaceSP m_interface_sp;
1394+
/// This holds the python callback object.
1395+
StructuredData::GenericSP m_implementation_sp;
13951396

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

lldb/include/lldb/lldb-forward.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ class ScriptInterpreterLocker;
190190
class ScriptedMetadata;
191191
class ScriptedPlatformInterface;
192192
class ScriptedProcessInterface;
193-
class ScriptedStopHookInterface;
194193
class ScriptedThreadInterface;
195194
class ScriptedThreadPlanInterface;
196195
class ScriptedSyntheticChildren;
@@ -409,8 +408,6 @@ typedef std::unique_ptr<lldb_private::ScriptedPlatformInterface>
409408
ScriptedPlatformInterfaceUP;
410409
typedef std::unique_ptr<lldb_private::ScriptedProcessInterface>
411410
ScriptedProcessInterfaceUP;
412-
typedef std::shared_ptr<lldb_private::ScriptedStopHookInterface>
413-
ScriptedStopHookInterfaceSP;
414411
typedef std::shared_ptr<lldb_private::ScriptedThreadInterface>
415412
ScriptedThreadInterfaceSP;
416413
typedef std::shared_ptr<lldb_private::ScriptedThreadPlanInterface>

lldb/source/Interpreter/ScriptInterpreter.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,6 @@ 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-
134128
lldb::ScriptLanguage
135129
ScriptInterpreter::StringToLanguage(const llvm::StringRef &language) {
136130
if (language.equals_insensitive(LanguageToString(eScriptLanguageNone)))

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ add_lldb_library(lldbPluginScriptInterpreterPythonInterfaces PLUGIN
2525
ScriptedPlatformPythonInterface.cpp
2626
ScriptedProcessPythonInterface.cpp
2727
ScriptedPythonInterface.cpp
28-
ScriptedStopHookPythonInterface.cpp
2928
ScriptedThreadPlanPythonInterface.cpp
3029
ScriptedThreadPythonInterface.cpp
3130

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ void ScriptInterpreterPythonInterfaces::Initialize() {
2828
OperatingSystemPythonInterface::Initialize();
2929
ScriptedPlatformPythonInterface::Initialize();
3030
ScriptedProcessPythonInterface::Initialize();
31-
ScriptedStopHookPythonInterface::Initialize();
3231
ScriptedThreadPlanPythonInterface::Initialize();
3332
}
3433

3534
void ScriptInterpreterPythonInterfaces::Terminate() {
3635
OperatingSystemPythonInterface::Terminate();
3736
ScriptedPlatformPythonInterface::Terminate();
3837
ScriptedProcessPythonInterface::Terminate();
39-
ScriptedStopHookPythonInterface::Terminate();
4038
ScriptedThreadPlanPythonInterface::Terminate();
4139
}
4240

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "OperatingSystemPythonInterface.h"
1919
#include "ScriptedPlatformPythonInterface.h"
2020
#include "ScriptedProcessPythonInterface.h"
21-
#include "ScriptedStopHookPythonInterface.h"
2221
#include "ScriptedThreadPlanPythonInterface.h"
2322

2423
namespace lldb_private {

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,4 @@ 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-
181162
#endif

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

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -180,35 +180,12 @@ class ScriptedPythonInterface : virtual public ScriptedInterface {
180180
llvm::Expected<PythonObject> expected_return_object =
181181
create_error("Resulting object is not initialized.");
182182

183-
// This relax the requirement on the number of argument for
184-
// initializing scripting extension if the size of the interface
185-
// parameter pack contains 1 less element than the extension maximum
186-
// number of positional arguments for this initializer.
187-
//
188-
// This addresses the cases where the embedded interpreter session
189-
// dictionary is passed to the extension initializer which is not used
190-
// most of the time.
191-
size_t num_args = sizeof...(Args);
192-
if (num_args != arg_info->max_positional_args) {
193-
if (num_args != arg_info->max_positional_args - 1)
194-
return create_error("Passed arguments ({0}) doesn't match the number "
195-
"of expected arguments ({1}).",
196-
num_args, arg_info->max_positional_args);
197-
198-
std::apply(
199-
[&init, &expected_return_object](auto &&...args) {
200-
llvm::consumeError(expected_return_object.takeError());
201-
expected_return_object = init(args...);
202-
},
203-
std::tuple_cat(transformed_args, std::make_tuple(dict)));
204-
} else {
205-
std::apply(
206-
[&init, &expected_return_object](auto &&...args) {
207-
llvm::consumeError(expected_return_object.takeError());
208-
expected_return_object = init(args...);
209-
},
210-
transformed_args);
211-
}
183+
std::apply(
184+
[&init, &expected_return_object](auto &&...args) {
185+
llvm::consumeError(expected_return_object.takeError());
186+
expected_return_object = init(args...);
187+
},
188+
transformed_args);
212189

213190
if (!expected_return_object)
214191
return expected_return_object.takeError();
@@ -428,10 +405,6 @@ class ScriptedPythonInterface : virtual public ScriptedInterface {
428405
return python::SWIGBridge::ToSWIGWrapper(arg);
429406
}
430407

431-
python::PythonObject Transform(lldb::TargetSP arg) {
432-
return python::SWIGBridge::ToSWIGWrapper(arg);
433-
}
434-
435408
python::PythonObject Transform(lldb::ProcessSP arg) {
436409
return python::SWIGBridge::ToSWIGWrapper(arg);
437410
}
@@ -584,11 +557,6 @@ std::optional<MemoryRegionInfo>
584557
ScriptedPythonInterface::ExtractValueFromPythonObject<
585558
std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error);
586559

587-
template <>
588-
lldb::ExecutionContextRefSP
589-
ScriptedPythonInterface::ExtractValueFromPythonObject<
590-
lldb::ExecutionContextRefSP>(python::PythonObject &p, Status &error);
591-
592560
} // namespace lldb_private
593561

594562
#endif // LLDB_ENABLE_PYTHON

0 commit comments

Comments
 (0)