From 42d4237b32c5200ab9e98dc1dcbbaaa005230f0a Mon Sep 17 00:00:00 2001 From: Xusheng Date: Thu, 11 Jan 2024 16:43:28 +0800 Subject: [PATCH] Ask the user to provide a platform when the input binary view does not have one. Fix https://github.com/Vector35/debugger/issues/206 --- ui/controlswidget.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++ ui/ui.cpp | 43 ++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/ui/controlswidget.cpp b/ui/controlswidget.cpp index 837e3b6..5fd7e72 100644 --- a/ui/controlswidget.cpp +++ b/ui/controlswidget.cpp @@ -22,6 +22,7 @@ limitations under the License. #include "binaryninjaapi.h" #include "disassemblyview.h" #include "theme.h" +#include "platformdialog.h" #include "ui.h" #include #include "progresstask.h" @@ -142,6 +143,34 @@ void DebugControlsWidget::performLaunch() return; } + auto data = m_controller->GetData(); + if (!data->GetDefaultPlatform()) + { + // No default platform, prompt user to choose one + PlatformDialog dlg(this); + if (dlg.exec() != QDialog::Accepted) + { + QMessageBox::warning(this, "No Platform", "The debugger cannot work if the binary view has no " + "platform and architecture"); + return; + } + + auto platform = dlg.getPlatform(); + if (platform) + { + dlg.saveDefaults(); + } + else + { + QMessageBox::warning(this, "Invalid Platform", "The debugger cannot work if the binary view has no " + "platform and architecture"); + return; + } + + data->SetDefaultArchitecture(platform->GetArchitecture()); + data->SetDefaultPlatform(platform); + } + QString text = QString( "The debugger is %1 the target and preparing the debugger binary view. \n" "This might take a while.").arg("launching"); @@ -168,6 +197,34 @@ void DebugControlsWidget::performAttachPID() if (pid == 0) return; + auto data = m_controller->GetData(); + if (!data->GetDefaultPlatform()) + { + // No default platform, prompt user to choose one + PlatformDialog dlg(this); + if (dlg.exec() != QDialog::Accepted) + { + QMessageBox::warning(this, "No Platform", "The debugger cannot work if the binary view has no " + "platform and architecture"); + return; + } + + auto platform = dlg.getPlatform(); + if (platform) + { + dlg.saveDefaults(); + } + else + { + QMessageBox::warning(this, "Invalid Platform", "The debugger cannot work if the binary view has no " + "platform and architecture"); + return; + } + + data->SetDefaultArchitecture(platform->GetArchitecture()); + data->SetDefaultPlatform(platform); + } + m_controller->SetPIDAttach(pid); QString text = QString( "The debugger is %1 the target and preparing the debugger binary view. \n" diff --git a/ui/ui.cpp b/ui/ui.cpp index 8954f45..2479e9b 100644 --- a/ui/ui.cpp +++ b/ui/ui.cpp @@ -20,6 +20,7 @@ limitations under the License. #include "moduleswidget.h" #include "stackwidget.h" #include "uinotification.h" +#include "platformdialog.h" #include "QPainter" #include #include @@ -309,6 +310,37 @@ void GlobalDebuggerUI::SetupMenu(UIContext* context) return !controller->IsConnectedToDebugServer(); }; + auto ensureBinaryViewHasPlatform = [&](BinaryViewRef data, QWidget* parent) -> bool + { + if (!data->GetDefaultPlatform()) + { + // No default platform, prompt user to choose one + PlatformDialog dlg(parent); + if (dlg.exec() != QDialog::Accepted) + { + QMessageBox::warning(parent, "No Platform", + "The debugger cannot work if the binary view has no platform and architecture"); + return false; + } + + auto platform = dlg.getPlatform(); + if (platform) + { + dlg.saveDefaults(); + } + else + { + QMessageBox::warning(parent, "Invalid Platform", + "The debugger cannot work if the binary view has no platform and architecture"); + return false; + } + + data->SetDefaultArchitecture(platform->GetArchitecture()); + data->SetDefaultPlatform(platform); + } + return true; + }; + UIAction::registerAction("Debug Adapter Settings..."); context->globalActions()->bindAction("Debug Adapter Settings...", UIAction( @@ -348,6 +380,10 @@ void GlobalDebuggerUI::SetupMenu(UIContext* context) if (QMessageBox::question(ctxt.context->mainWindow(), "Launch Target", prompt) != QMessageBox::Yes) return; } + + if (!ensureBinaryViewHasPlatform(controller->GetData(), ctxt.context->mainWindow())) + return; + QString text = QString( "The debugger is launching the target and preparing the debugger binary view. \n" "This might take a while."); @@ -511,6 +547,10 @@ void GlobalDebuggerUI::SetupMenu(UIContext* context) return; controller->SetPIDAttach(pid); + + if (!ensureBinaryViewHasPlatform(controller->GetData(), ctxt.context->mainWindow())) + return; + QString text = QString( "The debugger is attaching to the target and preparing the debugger binary view. \n" "This might take a while."); @@ -609,6 +649,9 @@ void GlobalDebuggerUI::SetupMenu(UIContext* context) if (dialog->exec() != QDialog::Accepted) return; + if (!ensureBinaryViewHasPlatform(controller->GetData(), ctxt.context->mainWindow())) + return; + QString text = QString( "The debugger is connecting to the target and preparing the debugger binary view. \n" "This might take a while.");