Skip to content

Commit

Permalink
Revert "[lldb] Move ScriptedProcess private state update to implement…
Browse files Browse the repository at this point in the history
…ation"

This reverts commit 3c33d72.
  • Loading branch information
medismailben committed Mar 6, 2023
1 parent 480eb74 commit 20dbb29
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 29 deletions.
30 changes: 18 additions & 12 deletions lldb/examples/python/scripted_process/scripted_process.py
Expand Up @@ -160,24 +160,30 @@ def attach(self, attach_info):
"""
return lldb.SBError()

def resume(self, should_stop=True):
def resume(self):
""" Simulate the scripted process resume.
Args:
should_stop (bool): If True, resume will also
Returns:
lldb.SBError: An `lldb.SBError` with error code 0.
"""
return lldb.SBError()

@abstractmethod
def should_stop(self):
""" Check if the scripted process plugin should produce the stop event.
Returns:
bool: True if scripted process should broadcast a stop event.
False otherwise.
"""
pass

def stop(self):
""" Trigger the scripted process stop.
Returns:
lldb.SBError: An `lldb.SBError` with error code 0.
"""
process = self.target.GetProcess()
if not process:
error = lldb.SBError()
error.SetErrorString("Invalid process.")
return error

process.ForceScriptedState(lldb.eStateRunning);
if (should_stop):
process.ForceScriptedState(lldb.eStateStopped);
return lldb.SBError()

@abstractmethod
Expand Down
4 changes: 4 additions & 0 deletions lldb/include/lldb/Interpreter/ScriptedProcessInterface.h
Expand Up @@ -38,6 +38,10 @@ class ScriptedProcessInterface : virtual public ScriptedInterface {

virtual Status Resume() { return Status("ScriptedProcess did not resume"); }

virtual bool ShouldStop() { return true; }

virtual Status Stop() { return Status("ScriptedProcess did not stop"); }

virtual std::optional<MemoryRegionInfo>
GetMemoryRegionContainingAddress(lldb::addr_t address, Status &error) {
error.SetErrorString("ScriptedProcess have no memory region.");
Expand Down
15 changes: 15 additions & 0 deletions lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
Expand Up @@ -187,6 +187,8 @@ Status ScriptedProcess::DoResume() {
if (resume) {
LLDB_LOGF(log, "ScriptedProcess::%s sending resume", __FUNCTION__);

SetPrivateState(eStateRunning);
SetPrivateState(eStateStopped);
error = GetInterface().Resume();
}

Expand Down Expand Up @@ -221,6 +223,19 @@ void ScriptedProcess::DidAttach(ArchSpec &process_arch) {
process_arch = GetArchitecture();
}

Status ScriptedProcess::DoStop() {
Log *log = GetLog(LLDBLog::Process);

if (GetInterface().ShouldStop()) {
SetPrivateState(eStateStopped);
LLDB_LOGF(log, "ScriptedProcess::%s Immediate stop", __FUNCTION__);
return {};
}

LLDB_LOGF(log, "ScriptedProcess::%s Delayed stop", __FUNCTION__);
return GetInterface().Stop();
}

Status ScriptedProcess::DoDestroy() { return Status(); }

bool ScriptedProcess::IsAlive() { return GetInterface().IsAlive(); }
Expand Down
2 changes: 2 additions & 0 deletions lldb/source/Plugins/Process/scripted/ScriptedProcess.h
Expand Up @@ -94,6 +94,8 @@ class ScriptedProcess : public Process {
ScriptedProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
const ScriptedMetadata &scripted_metadata, Status &error);

Status DoStop();

void Clear();

bool DoUpdateThreadList(ThreadList &old_thread_list,
Expand Down
Expand Up @@ -201,7 +201,6 @@ template <> struct PythonFormat<short> : PassthroughFormat<short, 'h'> {};
template <>
struct PythonFormat<unsigned short> : PassthroughFormat<unsigned short, 'H'> {};
template <> struct PythonFormat<int> : PassthroughFormat<int, 'i'> {};
template <> struct PythonFormat<bool> : PassthroughFormat<bool, 'p'> {};
template <>
struct PythonFormat<unsigned int> : PassthroughFormat<unsigned int, 'I'> {};
template <> struct PythonFormat<long> : PassthroughFormat<long, 'l'> {};
Expand Down
Expand Up @@ -80,8 +80,21 @@ Status ScriptedProcessPythonInterface::Launch() {
}

Status ScriptedProcessPythonInterface::Resume() {
// When calling ScriptedProcess.Resume from lldb we should always stop.
return GetStatusFromMethod("resume", /*should_stop=*/true);
return GetStatusFromMethod("resume");
}

bool ScriptedProcessPythonInterface::ShouldStop() {
Status error;
StructuredData::ObjectSP obj = Dispatch("is_alive", error);

if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
return {};

return obj->GetBooleanValue();
}

Status ScriptedProcessPythonInterface::Stop() {
return GetStatusFromMethod("stop");
}

std::optional<MemoryRegionInfo>
Expand Down
Expand Up @@ -37,6 +37,10 @@ class ScriptedProcessPythonInterface : public ScriptedProcessInterface,

Status Resume() override;

bool ShouldStop() override;

Status Stop() override;

std::optional<MemoryRegionInfo>
GetMemoryRegionContainingAddress(lldb::addr_t address,
Status &error) override;
Expand Down
Expand Up @@ -113,11 +113,6 @@ class ScriptedPythonInterface : virtual public ScriptedInterface {
return {object};
}

python::PythonObject Transform(bool arg) {
// Boolean arguments need to be turned into python objects.
return python::PythonBoolean(arg);
}

python::PythonObject Transform(Status arg) {
return python::ToSWIGWrapper(arg);
}
Expand Down Expand Up @@ -146,15 +141,6 @@ class ScriptedPythonInterface : virtual public ScriptedInterface {
original_arg = ExtractValueFromPythonObject<T>(transformed_arg, error);
}

template <>
void ReverseTransform(bool &original_arg,
python::PythonObject transformed_arg, Status &error) {
python::PythonBoolean boolean_arg = python::PythonBoolean(
python::PyRefType::Borrowed, transformed_arg.get());
if (boolean_arg.IsValid())
original_arg = boolean_arg.GetValue();
}

template <std::size_t... I, typename... Args>
auto TransformTuple(const std::tuple<Args...> &args,
std::index_sequence<I...>) {
Expand Down

0 comments on commit 20dbb29

Please sign in to comment.