Skip to content

Commit

Permalink
Clean up DebugAdapter interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
xusheng6 committed Mar 10, 2023
1 parent baa048e commit 5449e21
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 82 deletions.
54 changes: 30 additions & 24 deletions core/adapters/dbgengadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,26 +559,32 @@ bool DbgEngAdapter::DisconnectDebugServer()
return ret == S_OK;
}

void DbgEngAdapter::Detach()
bool DbgEngAdapter::Detach()
{
m_aboutToBeKilled = true;
m_lastOperationIsStepInto = false;
if (this->m_debugClient)
this->m_debugClient->DetachProcesses();
if (!this->m_debugClient)
return false;

if (this->m_debugClient->DetachProcesses() != S_OK)
return false;

m_debugClient->ExitDispatch(reinterpret_cast<PDEBUG_CLIENT>(m_debugClient));
return true;
}

void DbgEngAdapter::Quit()
bool DbgEngAdapter::Quit()
{
m_aboutToBeKilled = true;
m_lastOperationIsStepInto = false;
if (this->m_debugClient)
{
HRESULT result = this->m_debugClient->TerminateProcesses();
}
if (!this->m_debugClient)
return false;

if (this->m_debugClient->TerminateProcesses() != S_OK)
return false;

m_debugClient->ExitDispatch(reinterpret_cast<PDEBUG_CLIENT>(m_debugClient));
return true;
}

std::vector<DebugProcess> DbgEngAdapter::GetProcessList()
Expand Down Expand Up @@ -995,7 +1001,7 @@ std::vector<DebugModule> DbgEngAdapter::GetModuleList()
bool DbgEngAdapter::BreakInto()
{
if (ExecStatus() == DEBUG_STATUS_BREAK)
return InvalidStatusOrOperation;
return false;

m_lastOperationIsStepInto = false;
// After we call SetInterrupt(), the WaitForEvent() function will return due to a breakpoint exception
Expand All @@ -1005,54 +1011,54 @@ bool DbgEngAdapter::BreakInto()
return true;
}

DebugStopReason DbgEngAdapter::Go()
bool DbgEngAdapter::Go()
{
// TODO: we should have the debugger core to detect the failure and notify the user about it.
// Currently, LLDB directly notifies such errors, which needs to be changed in the future.
if (ExecStatus() != DEBUG_STATUS_BREAK)
return InvalidStatusOrOperation;
return false;

m_lastOperationIsStepInto = false;
if (this->m_debugControl->SetExecutionStatus(DEBUG_STATUS_GO) != S_OK)
return DebugStopReason::InternalError;
return false;

m_debugClient->ExitDispatch(reinterpret_cast<PDEBUG_CLIENT>(m_debugClient));
return UnknownReason;
return true;
}

DebugStopReason DbgEngAdapter::StepInto()
bool DbgEngAdapter::StepInto()
{
if (ExecStatus() != DEBUG_STATUS_BREAK)
return InvalidStatusOrOperation;

m_lastOperationIsStepInto = true;
if (this->m_debugControl->SetExecutionStatus(DEBUG_STATUS_STEP_INTO) != S_OK)
return DebugStopReason::InternalError;
return false;

m_debugClient->ExitDispatch(reinterpret_cast<PDEBUG_CLIENT>(m_debugClient));
return UnknownReason;
return true;
}

DebugStopReason DbgEngAdapter::StepOver()
bool DbgEngAdapter::StepOver()
{
if (ExecStatus() != DEBUG_STATUS_BREAK)
return InvalidStatusOrOperation;
return false;

m_lastOperationIsStepInto = false;
if (this->m_debugControl->SetExecutionStatus(DEBUG_STATUS_STEP_OVER) != S_OK)
return DebugStopReason::InternalError;
return false;

m_debugClient->ExitDispatch(reinterpret_cast<PDEBUG_CLIENT>(m_debugClient));
return UnknownReason;
return true;
}

DebugStopReason DbgEngAdapter::StepReturn()
bool DbgEngAdapter::StepReturn()
{
if (ExecStatus() != DEBUG_STATUS_BREAK)
return InvalidStatusOrOperation;
return false;

InvokeBackendCommand("gu");
return UnknownReason;
return true;
}

bool DbgEngAdapter::Wait(std::chrono::milliseconds timeout)
Expand Down Expand Up @@ -1165,7 +1171,7 @@ std::string DbgEngAdapter::InvokeBackendCommand(const std::string& command)
return "";
}

std::uintptr_t DbgEngAdapter::GetInstructionOffset()
uint64_t DbgEngAdapter::GetInstructionOffset()
{
if (!m_debugRegisters)
return -1;
Expand Down
14 changes: 7 additions & 7 deletions core/adapters/dbgengadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ namespace BinaryNinjaDebugger {
bool ConnectToDebugServer(const std::string& server, std::uint32_t port) override;
bool DisconnectDebugServer() override;

void Detach() override;
void Quit() override;
bool Detach() override;
bool Quit() override;

bool Wait(std::chrono::milliseconds timeout = std::chrono::milliseconds::max());

Expand Down Expand Up @@ -198,13 +198,13 @@ namespace BinaryNinjaDebugger {
uint64_t ExitCode() override;

bool BreakInto() override;
DebugStopReason Go() override;
DebugStopReason StepInto() override;
DebugStopReason StepOver() override;
DebugStopReason StepReturn() override;
bool Go() override;
bool StepInto() override;
bool StepOver() override;
bool StepReturn() override;

std::string InvokeBackendCommand(const std::string& command) override;
std::uintptr_t GetInstructionOffset() override;
uint64_t GetInstructionOffset() override;
uint64_t GetStackPointer() override;

bool SupportFeature(DebugAdapterCapacity feature) override;
Expand Down
46 changes: 23 additions & 23 deletions core/adapters/lldbadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,19 +365,19 @@ bool LldbAdapter::Connect(const std::string& server, std::uint32_t port)
}


void LldbAdapter::Detach()
bool LldbAdapter::Detach()
{
std::unique_lock<std::mutex> lock(m_quitingMutex);
// TODO: return if the operation succeeds
SBError error = m_process.Detach();
return error.Success();
}


void LldbAdapter::Quit()
bool LldbAdapter::Quit()
{
std::unique_lock<std::mutex> lock(m_quitingMutex);
// TODO: return if the operation succeeds
SBError error = m_process.Kill();
return error.Success();
}


Expand Down Expand Up @@ -1075,7 +1075,7 @@ bool LldbAdapter::BreakInto()
}


DebugStopReason LldbAdapter::Go()
bool LldbAdapter::Go()
{
if (m_process.GetState() != lldb::eStateStopped)
{
Expand All @@ -1084,21 +1084,21 @@ DebugStopReason LldbAdapter::Go()
event.data.errorData.shortError = "Go failed";
event.data.errorData.error = fmt::format("LLDB: go failed, process state is not stopped");
PostDebuggerEvent(event);
return DebugStopReason::InternalError;
return false;
}

#ifndef WIN32
SBError error = m_process.Continue();
if (!error.Success())
return DebugStopReason::InternalError;
return false;
#else
InvokeBackendCommand("c");
#endif
return StopReason();
return true;
}


DebugStopReason LldbAdapter::StepInto()
bool LldbAdapter::StepInto()
{
if (m_process.GetState() != lldb::eStateStopped)
{
Expand All @@ -1107,7 +1107,7 @@ DebugStopReason LldbAdapter::StepInto()
event.data.errorData.shortError = "step into failed";
event.data.errorData.error = fmt::format("LLDB: step into failed, process state is not stopped");
PostDebuggerEvent(event);
return DebugStopReason::InternalError;
return false;
}

#ifndef WIN32
Expand All @@ -1119,7 +1119,7 @@ DebugStopReason LldbAdapter::StepInto()
event.data.errorData.shortError = "Step into failed";
event.data.errorData.error = fmt::format("LLDB: step into failed, invalid thread");
PostDebuggerEvent(event);
return DebugStopReason::InternalError;
return false;
}

SBError error;
Expand All @@ -1132,16 +1132,16 @@ DebugStopReason LldbAdapter::StepInto()
event.data.errorData.error =
fmt::format("LLDB: step into failed {}", error.GetCString() ? error.GetCString() : "");
PostDebuggerEvent(event);
return DebugStopReason::InternalError;
return false;
}
#else
InvokeBackendCommand("si");
#endif
return StopReason();
return true;
}


DebugStopReason LldbAdapter::StepOver()
bool LldbAdapter::StepOver()
{
if (m_process.GetState() != lldb::eStateStopped)
{
Expand All @@ -1150,7 +1150,7 @@ DebugStopReason LldbAdapter::StepOver()
event.data.errorData.shortError = "Step over failed";
event.data.errorData.error = fmt::format("LLDB: step over failed, process state is not stopped");
PostDebuggerEvent(event);
return DebugStopReason::InternalError;
return false;
}

#ifndef WIN32
Expand All @@ -1162,7 +1162,7 @@ DebugStopReason LldbAdapter::StepOver()
event.data.errorData.shortError = "Step over failed";
event.data.errorData.error = fmt::format("LLDB: step over failed, invalid thread");
PostDebuggerEvent(event);
return DebugStopReason::InternalError;
return false;
}

SBError error;
Expand All @@ -1175,16 +1175,16 @@ DebugStopReason LldbAdapter::StepOver()
event.data.errorData.error =
fmt::format("LLDB: step over failed {}", error.GetCString() ? error.GetCString() : "");
PostDebuggerEvent(event);
return DebugStopReason::InternalError;
return false;
}
#else
InvokeBackendCommand("ni");
#endif
return StopReason();
return true;
}


DebugStopReason LldbAdapter::StepReturn()
bool LldbAdapter::StepReturn()
{
if (m_process.GetState() != lldb::eStateStopped)
{
Expand All @@ -1193,7 +1193,7 @@ DebugStopReason LldbAdapter::StepReturn()
event.data.errorData.shortError = "Step return failed";
event.data.errorData.error = fmt::format("LLDB: step return failed, process state is not stopped");
PostDebuggerEvent(event);
return DebugStopReason::InternalError;
return false;
}

// The following method, calling StepOutOfFrame(), will receive an unexpected lldb::eStateRunning event when the
Expand Down Expand Up @@ -1223,11 +1223,11 @@ DebugStopReason LldbAdapter::StepReturn()
event.data.errorData.shortError = "Step return failed";
event.data.errorData.error = fmt::format("LLDB: step return failed, {}", result);
PostDebuggerEvent(event);
return DebugStopReason::InternalError;
return false;
}

//#endif
return StopReason();
return true;
}


Expand All @@ -1251,7 +1251,7 @@ std::string LldbAdapter::InvokeBackendCommand(const std::string& command)
}


uintptr_t LldbAdapter::GetInstructionOffset()
uint64_t LldbAdapter::GetInstructionOffset()
{
SBThread thread = m_process.GetSelectedThread();
if (!thread.IsValid())
Expand Down
14 changes: 7 additions & 7 deletions core/adapters/lldbadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ namespace BinaryNinjaDebugger {

bool Connect(const std::string& server, std::uint32_t port) override;

void Detach() override;
bool Detach() override;

void Quit() override;
bool Quit() override;

std::vector<DebugProcess> GetProcessList() override;

Expand Down Expand Up @@ -110,17 +110,17 @@ namespace BinaryNinjaDebugger {

bool BreakInto() override;

DebugStopReason Go() override;
bool Go() override;

DebugStopReason StepInto() override;
bool StepInto() override;

DebugStopReason StepOver() override;
bool StepOver() override;

DebugStopReason StepReturn() override;
bool StepReturn() override;

std::string InvokeBackendCommand(const std::string& command) override;

uintptr_t GetInstructionOffset() override;
uint64_t GetInstructionOffset() override;

uint64_t GetStackPointer() override;

Expand Down
4 changes: 2 additions & 2 deletions core/debugadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ bool DebugModule::IsSameBaseModule(const std::string& module1, const std::string
}


DebugStopReason DebugAdapter::StepReturn()
bool DebugAdapter::StepReturn()
{
return OperationNotSupported;
return false;
}


Expand Down
Loading

0 comments on commit 5449e21

Please sign in to comment.