Skip to content

Commit

Permalink
[lldb/Interpreter] Discard ScriptedThreadPlan::GetStopDescription ret…
Browse files Browse the repository at this point in the history
…urn value (#96985)

This patch changes `ScriptedThreadPlan::GetStopDescription` behavior by
discarding its return value since it is optional in the first place (the
user doesn't need to provide a return value in their implementation).

This patch also addresses the test failures in TestStepScripted
following 9a9ec22 and re-enables the tests that were XFAIL'd previously.

The issue here was that the `Stream*` that's passed to
`ThreadPlanPython::GetDescription` wasn't being passed by reference to
the python method so it was never updated to reflect how the python
method interacted with it.

This patch solves this issue by making a temporary `StreamSP` that will
be passed to the python method by reference, after what we will copy its
content to the caller `Stream` pointer argument.

---------

Signed-off-by: Med Ismail Bennani <[email protected]>
  • Loading branch information
medismailben authored Jun 28, 2024
1 parent bb83a3d commit 1130e92
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class ScriptedThreadPlanInterface : public ScriptedInterface {

virtual lldb::StateType GetRunState() { return lldb::eStateStepping; }

virtual llvm::Expected<bool> GetStopDescription(lldb_private::Stream *s) {
return true;
virtual llvm::Error GetStopDescription(lldb::StreamSP &stream) {
return llvm::Error::success();
}
};
} // namespace lldb_private
Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Interpreter/ScriptInterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ class ScriptInterpreter : public PluginInterface {

Event *GetOpaqueTypeFromSBEvent(const lldb::SBEvent &event) const;

Stream *GetOpaqueTypeFromSBStream(const lldb::SBStream &stream) const;
lldb::StreamSP GetOpaqueTypeFromSBStream(const lldb::SBStream &stream) const;

lldb::BreakpointSP
GetOpaqueTypeFromSBBreakpoint(const lldb::SBBreakpoint &breakpoint) const;
Expand Down
9 changes: 6 additions & 3 deletions lldb/source/Interpreter/ScriptInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,13 @@ ScriptInterpreter::GetOpaqueTypeFromSBEvent(const lldb::SBEvent &event) const {
return event.m_opaque_ptr;
}

Stream *ScriptInterpreter::GetOpaqueTypeFromSBStream(
lldb::StreamSP ScriptInterpreter::GetOpaqueTypeFromSBStream(
const lldb::SBStream &stream) const {
if (stream.m_opaque_up)
return const_cast<lldb::SBStream &>(stream).m_opaque_up.get();
if (stream.m_opaque_up) {
lldb::StreamSP s = std::make_shared<lldb_private::StreamString>();
*s << const_cast<lldb::SBStream &>(stream).GetData();

This comment has been minimized.

Copy link
@nico

nico Jun 28, 2024

Contributor

This adds a dependency cycle: lldb::SBStream is in source/API, so this makes Interpreter depend on API. But API already depends on Interpreter.

return s;
}

return nullptr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ ScriptedPythonInterface::ScriptedPythonInterface(
ScriptInterpreterPythonImpl &interpreter)
: ScriptedInterface(), m_interpreter(interpreter) {}

template <>
void ScriptedPythonInterface::ReverseTransform(
lldb_private::Stream *&original_arg, python::PythonObject transformed_arg,
Status &error) {
Stream *s = ExtractValueFromPythonObject<Stream *>(transformed_arg, error);
*original_arg = *s;
original_arg->PutCString(static_cast<StreamString *>(s)->GetData());
}

template <>
StructuredData::ArraySP
ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>(
Expand Down Expand Up @@ -74,7 +65,8 @@ Event *ScriptedPythonInterface::ExtractValueFromPythonObject<Event *>(
}

template <>
Stream *ScriptedPythonInterface::ExtractValueFromPythonObject<Stream *>(
lldb::StreamSP
ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StreamSP>(
python::PythonObject &p, Status &error) {
if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>(
python::LLDBSWIGPython_CastPyObjectToSBStream(p.get())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ class ScriptedPythonInterface : virtual public ScriptedInterface {
return python::SWIGBridge::ToSWIGWrapper(arg);
}

python::PythonObject Transform(Stream *arg) {
return python::SWIGBridge::ToSWIGWrapper(arg);
python::PythonObject Transform(lldb::StreamSP arg) {
return python::SWIGBridge::ToSWIGWrapper(arg.get());
}

python::PythonObject Transform(lldb::DataExtractorSP arg) {
Expand Down Expand Up @@ -447,7 +447,8 @@ Event *ScriptedPythonInterface::ExtractValueFromPythonObject<Event *>(
python::PythonObject &p, Status &error);

template <>
Stream *ScriptedPythonInterface::ExtractValueFromPythonObject<Stream *>(
lldb::StreamSP
ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StreamSP>(
python::PythonObject &p, Status &error);

template <>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ lldb::StateType ScriptedThreadPlanPythonInterface::GetRunState() {
static_cast<uint32_t>(lldb::eStateStepping)));
}

llvm::Expected<bool>
ScriptedThreadPlanPythonInterface::GetStopDescription(lldb_private::Stream *s) {
llvm::Error
ScriptedThreadPlanPythonInterface::GetStopDescription(lldb::StreamSP &stream) {
Status error;
Dispatch("stop_description", error, s);
Dispatch("stop_description", error, stream);

if (error.Fail())
return error.ToError();

return true;
return llvm::Error::success();
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ScriptedThreadPlanPythonInterface : public ScriptedThreadPlanInterface,

lldb::StateType GetRunState() override;

llvm::Expected<bool> GetStopDescription(lldb_private::Stream *s) override;
llvm::Error GetStopDescription(lldb::StreamSP &stream) override;
};
} // namespace lldb_private

Expand Down
11 changes: 7 additions & 4 deletions lldb/source/Target/ThreadPlanPython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,16 @@ void ThreadPlanPython::GetDescription(Stream *s, lldb::DescriptionLevel level) {
if (m_implementation_sp) {
ScriptInterpreter *script_interp = GetScriptInterpreter();
if (script_interp) {
auto desc_or_err = m_interface->GetStopDescription(s);
if (!desc_or_err || !*desc_or_err) {
LLDB_LOG_ERROR(GetLog(LLDBLog::Thread), desc_or_err.takeError(),
lldb::StreamSP stream = std::make_shared<lldb_private::StreamString>();
llvm::Error err = m_interface->GetStopDescription(stream);
if (err) {
LLDB_LOG_ERROR(GetLog(LLDBLog::Thread), std::move(err),
"Can't call ScriptedThreadPlan::GetStopDescription.");
s->Printf("Python thread plan implemented by class %s.",
m_class_name.c_str());
}
} else
s->PutCString(
reinterpret_cast<StreamString *>(stream.get())->GetData());
}
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *


class StepScriptedTestCase(TestBase):
NO_DEBUG_INFO_TESTCASE = True

Expand All @@ -15,14 +16,12 @@ def setUp(self):
self.main_source_file = lldb.SBFileSpec("main.c")
self.runCmd("command script import Steps.py")

@expectedFailureAll()
def test_standard_step_out(self):
"""Tests stepping with the scripted thread plan laying over a standard
thread plan for stepping out."""
self.build()
self.step_out_with_scripted_plan("Steps.StepOut")

@expectedFailureAll()
def test_scripted_step_out(self):
"""Tests stepping with the scripted thread plan laying over an another
scripted thread plan for stepping out."""
Expand Down Expand Up @@ -63,12 +62,10 @@ def test_misspelled_plan_name(self):
# Make sure we didn't let the process run:
self.assertEqual(stop_id, process.GetStopID(), "Process didn't run")

@expectedFailureAll()
def test_checking_variable(self):
"""Test that we can call SBValue API's from a scripted thread plan - using SBAPI's to step"""
self.do_test_checking_variable(False)

@expectedFailureAll()
def test_checking_variable_cli(self):
"""Test that we can call SBValue API's from a scripted thread plan - using cli to step"""
self.do_test_checking_variable(True)
Expand Down

0 comments on commit 1130e92

Please sign in to comment.