Skip to content

Commit

Permalink
Make the Connect operation async
Browse files Browse the repository at this point in the history
  • Loading branch information
xusheng6 committed Apr 11, 2023
1 parent 873cc9e commit 335f4d1
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 33 deletions.
3 changes: 2 additions & 1 deletion api/debuggerapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,8 @@ namespace BinaryNinjaDebuggerAPI {
void Restart();
void Quit();
void QuitAndWait();
void Connect();
bool Connect();
DebugStopReason ConnectAndWait();
bool ConnectToDebugServer();
bool DisconnectDebugServer();
void Detach();
Expand Down
10 changes: 8 additions & 2 deletions api/debuggercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,15 @@ void DebuggerController::QuitAndWait()
}


void DebuggerController::Connect()
bool DebuggerController::Connect()
{
BNDebuggerConnect(m_object);
return BNDebuggerConnect(m_object);
}


DebugStopReason DebuggerController::ConnectAndWait()
{
return BNDebuggerConnectAndWait(m_object);
}


Expand Down
4 changes: 3 additions & 1 deletion api/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ extern "C"
{
DebugAdapterLaunch,
DebugAdapterAttach,
DebugAdapterConnect,
DebugAdapterGo,
DebugAdapterStepInto,
DebugAdapterStepOver,
Expand Down Expand Up @@ -358,7 +359,8 @@ extern "C"
DEBUGGER_FFI_API void BNDebuggerRestart(BNDebuggerController* controller);
DEBUGGER_FFI_API void BNDebuggerQuit(BNDebuggerController* controller);
DEBUGGER_FFI_API void BNDebuggerQuitAndWait(BNDebuggerController* controller);
DEBUGGER_FFI_API void BNDebuggerConnect(BNDebuggerController* controller);
DEBUGGER_FFI_API bool BNDebuggerConnect(BNDebuggerController* controller);
DEBUGGER_FFI_API BNDebugStopReason BNDebuggerConnectAndWait(BNDebuggerController* controller);
DEBUGGER_FFI_API bool BNDebuggerConnectToDebugServer(BNDebuggerController* controller);
DEBUGGER_FFI_API bool BNDebuggerDisconnectDebugServer(BNDebuggerController* controller);
DEBUGGER_FFI_API void BNDebuggerDetach(BNDebuggerController* controller);
Expand Down
12 changes: 10 additions & 2 deletions api/python/debuggercontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,13 +755,21 @@ def quit_and_wait(self) -> None:
"""
dbgcore.BNDebuggerQuitAndWait(self.handle)

def connect(self) -> None:
def connect(self) -> bool:
"""
Connect to a remote target (process)
The host and port of the remote target must first be specified by setting `remote_host` and `remote_port`
"""
dbgcore.BNDebuggerConnect(self.handle)
return dbgcore.BNDebuggerConnect(self.handle)

def connect_and_wait(self) -> bool:
"""
Connect to a remote target (process) and wait for all debugger events to be processed
The host and port of the remote target must first be specified by setting `remote_host` and `remote_port`
"""
return dbgcore.BNDebuggerConnectAndWait(self.handle)

def connect_to_debug_server(self) -> bool:
"""
Expand Down
2 changes: 1 addition & 1 deletion cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ int main(int argc, const char* argv[])
int32_t port = std::stoi(portStr);
debugger->SetRemoteHost(host);
debugger->SetRemotePort(port);
debugger->Connect();
debugger->ConnectAndWait();
}
else if (strcmp(argv[2], "--attach") == 0)
{
Expand Down
4 changes: 2 additions & 2 deletions core/adapters/lldbadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ bool LldbAdapter::Connect(const std::string& server, std::uint32_t port)
if (!m_target.IsValid())
{
DebuggerEvent event;
event.type = ErrorEventType;
event.type = LaunchFailureEventType;
event.data.errorData.shortError = fmt::format("LLDB failed to connect to target.");
event.data.errorData.error =
fmt::format("LLDB failed to connect to target with \"{}\"", err.GetCString() ? err.GetCString() : "");
Expand All @@ -360,7 +360,7 @@ bool LldbAdapter::Connect(const std::string& server, std::uint32_t port)
if (!m_process.IsValid() || (m_process.GetState() == StateType::eStateInvalid) || err.Fail())
{
DebuggerEvent event;
event.type = ErrorEventType;
event.type = LaunchFailureEventType;
event.data.errorData.shortError = fmt::format("LLDB failed to connect to target.");
event.data.errorData.error =
fmt::format("LLDB Failed to connect to target with \"{}\"", err.GetCString() ? err.GetCString() : "");
Expand Down
65 changes: 45 additions & 20 deletions core/debuggercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,46 @@ DebugStopReason DebuggerController::AttachAndWait()
}


bool DebuggerController::Connect()
{
std::thread([&]() { ConnectAndWait(); }).detach();
return true;
}


DebugStopReason DebuggerController::ConnectAndWaitInternal()
{
DebuggerEvent event;
event.type = LaunchEventType;
PostDebuggerEvent(event);

if (!CreateDebugAdapter())
return InternalError;

m_inputFileLoaded = false;
m_initialBreakpointSeen = false;
m_state->MarkDirty();
if (!CreateDebuggerBinaryView())
return InternalError;

return ExecuteAdapterAndWait(DebugAdapterConnect);
}


DebugStopReason DebuggerController::ConnectAndWait()
{
if (!m_targetControlMutex.try_lock())
return InternalError;

auto reason = ConnectAndWaitInternal();
if (!m_userRequestedBreak && (reason != ProcessExited) && (reason != InternalError))
NotifyStopped(reason);

m_targetControlMutex.unlock();
return reason;
}


bool DebuggerController::Execute()
{
std::unique_lock<std::recursive_mutex> lock(m_targetControlMutex);
Expand Down Expand Up @@ -759,25 +799,6 @@ void DebuggerController::Restart()
}


void DebuggerController::Connect()
{
if (m_state->IsConnected())
return;

if (!CreateDebugAdapter())
return;

m_inputFileLoaded = false;
m_initialBreakpointSeen = false;
m_state->MarkDirty();
m_state->SetConnectionStatus(DebugAdapterConnectingStatus);
CreateDebuggerBinaryView();
NotifyEvent(ConnectEventType);

bool ok = m_adapter->Connect(m_state->GetRemoteHost(), m_state->GetRemotePort());
}


bool DebuggerController::ConnectToDebugServer()
{
if (m_state->IsConnectedToDebugServer())
Expand Down Expand Up @@ -1773,13 +1794,17 @@ DebugStopReason DebuggerController::ExecuteAdapterAndWait(const DebugAdapterOper
case DebugAdapterAttach:
resumeOK = m_adapter->Attach(m_state->GetPIDAttach());
break;
case DebugAdapterConnect:
resumeOK = m_adapter->Connect(m_state->GetRemoteHost(), m_state->GetRemotePort());
break;
default:
break;
}

bool ok = false;
if ((operation == DebugAdapterGo) || (operation == DebugAdapterStepInto) || (operation == DebugAdapterStepOver)
|| (operation == DebugAdapterStepReturn) || (operation == DebugAdapterLaunch))
|| (operation == DebugAdapterStepReturn) || (operation == DebugAdapterLaunch)
|| (operation == DebugAdapterConnect) || (operation == DebugAdapterAttach))
{
ok = resumeOK;
}
Expand Down
6 changes: 4 additions & 2 deletions core/debuggercontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ namespace BinaryNinjaDebugger {
// the callbacks.
DebugStopReason LaunchAndWaitInternal();
DebugStopReason AttachAndWaitInternal();
DebugStopReason ConnectAndWaitInternal();
DebugStopReason PauseAndWaitInternal();
DebugStopReason GoAndWaitInternal();
DebugStopReason StepIntoAndWaitInternal();
Expand Down Expand Up @@ -211,16 +212,16 @@ namespace BinaryNinjaDebugger {
// target control
bool Execute();
void Restart();
void Connect();
bool ConnectToDebugServer();
bool DisconnectDebugServer();
// Convenience function, either launch the target process or connect to a remote, depending on the selected
// adapter
void LaunchOrConnect();
bool Attach();

// Asynchronous APIs.
bool Launch();
bool Connect();
bool Attach();
void Detach();
bool Go();
void Quit();
Expand All @@ -236,6 +237,7 @@ namespace BinaryNinjaDebugger {
DebugStopReason LaunchAndWait();
DebugStopReason GoAndWait();
DebugStopReason AttachAndWait();
DebugStopReason ConnectAndWait();
DebugStopReason StepIntoAndWait(BNFunctionGraphType il = NormalFunctionGraph);
DebugStopReason StepOverAndWait(BNFunctionGraphType il = NormalFunctionGraph);
DebugStopReason StepReturnAndWait();
Expand Down
10 changes: 8 additions & 2 deletions core/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,15 @@ void BNDebuggerQuitAndWait(BNDebuggerController* controller)
}


void BNDebuggerConnect(BNDebuggerController* controller)
bool BNDebuggerConnect(BNDebuggerController* controller)
{
controller->object->Connect();
return controller->object->Connect();
}


BNDebugStopReason BNDebuggerConnectAndWait(BNDebuggerController* controller)
{
return controller->object->ConnectAndWait();
}


Expand Down

0 comments on commit 335f4d1

Please sign in to comment.