From 5a0ba0acb65cd706e704cc6cc0242429c4a6435a Mon Sep 17 00:00:00 2001 From: Xusheng Date: Mon, 27 Mar 2023 14:28:36 +0800 Subject: [PATCH] WIP on creating sync version of launch: LaunchAndWait() --- api/ffi.h | 1 + core/debuggercontroller.cpp | 34 ++++++++++++++++++++++++++++------ core/debuggercontroller.h | 2 ++ ui/controlswidget.cpp | 27 ++++++++++++++------------- 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/api/ffi.h b/api/ffi.h index 3a88403b..ad090366 100644 --- a/api/ffi.h +++ b/api/ffi.h @@ -289,6 +289,7 @@ extern "C" enum BNDebuggerAdapterOperation { + DebugAdapterLaunch, DebugAdapterGo, DebugAdapterStepInto, DebugAdapterStepOver, diff --git a/core/debuggercontroller.cpp b/core/debuggercontroller.cpp index 07126373..daf45f09 100644 --- a/core/debuggercontroller.cpp +++ b/core/debuggercontroller.cpp @@ -109,22 +109,41 @@ bool DebuggerController::SetIP(uint64_t address) bool DebuggerController::Launch() { - std::unique_lock lock(m_targetControlMutex); + std::thread([&]() { LaunchAndWait(); }).detach(); + return true; +} + +DebugStopReason DebuggerController::LaunchAndWaitInternal() +{ DebuggerEvent event; event.type = LaunchEventType; PostDebuggerEvent(event); if (!CreateDebugAdapter()) - return false; + return InternalError; m_inputFileLoaded = false; - m_initialBreakpointSeen = false; + m_initialBreakpointSeen = false; m_state->MarkDirty(); if (!CreateDebuggerBinaryView()) - return false; + return InternalError; + + return ExecuteAdapterAndWait(DebugAdapterLaunch); +} + + +DebugStopReason DebuggerController::LaunchAndWait() +{ + if (!m_targetControlMutex.try_lock()) + return InternalError; - return Execute(); + auto reason = LaunchAndWaitInternal(); + if (!m_userRequestedBreak && (reason != ProcessExited) && (reason != InternalError)) + NotifyStopped(reason); + + m_targetControlMutex.unlock(); + return reason; } @@ -1722,13 +1741,16 @@ DebugStopReason DebuggerController::ExecuteAdapterAndWait(const DebugAdapterOper m_liveView->AbortAnalysis(); m_adapter->Detach(); break; + case DebugAdapterLaunch: + resumeOK = Execute(); + break; default: break; } bool ok = false; if ((operation == DebugAdapterGo) || (operation == DebugAdapterStepInto) || (operation == DebugAdapterStepOver) - || (operation == DebugAdapterStepReturn)) + || (operation == DebugAdapterStepReturn) || (operation == DebugAdapterLaunch)) { ok = resumeOK; } diff --git a/core/debuggercontroller.h b/core/debuggercontroller.h index 228a8137..bb4cc8bd 100644 --- a/core/debuggercontroller.h +++ b/core/debuggercontroller.h @@ -207,6 +207,8 @@ namespace BinaryNinjaDebugger { bool SetIP(uint64_t address); // target control + DebugStopReason LaunchAndWait(); + DebugStopReason LaunchAndWaitInternal(); bool Launch(); bool Execute(); void Restart(); diff --git a/ui/controlswidget.cpp b/ui/controlswidget.cpp index 05fa1be7..7ff41cbc 100644 --- a/ui/controlswidget.cpp +++ b/ui/controlswidget.cpp @@ -128,19 +128,20 @@ QString DebugControlsWidget::getToolTip(const QString& name) void DebugControlsWidget::performLaunch() { - QString text = QString( - "The debugger is %1 the target and preparing the debugger binary view. \n" - "This might take a while.").arg("launching"); - ProgressTask* task = - new ProgressTask(this, "Launching", text, "", [=](std::function progress) { - m_controller->Launch(); - - // For now, this cant be canceled, as the Debugger model wasn't - // designed with that in mind. This function below can return false if canceling is enabled - progress(1, 1); - return; - }); - task->wait(); +// QString text = QString( +// "The debugger is %1 the target and preparing the debugger binary view. \n" +// "This might take a while.").arg("launching"); +// ProgressTask* task = +// new ProgressTask(this, "Launching", text, "", [=](std::function progress) { +// m_controller->Launch(); +// +// // For now, this cant be canceled, as the Debugger model wasn't +// // designed with that in mind. This function below can return false if canceling is enabled +// progress(1, 1); +// return; +// }); +// task->wait(); + std::thread([&]() { m_controller->Launch(); }).detach(); }