From 19b6efd5af6a7196df8a4994eca6edfbef2a6449 Mon Sep 17 00:00:00 2001 From: Xusheng Date: Wed, 17 Apr 2024 16:30:59 +0800 Subject: [PATCH] Properly set the remote working directory when using LLDB adapter for remote debugging. Fix https://github.com/Vector35/debugger/issues/565 --- core/adapters/lldbadapter.cpp | 8 ++++++++ core/debugadapter.h | 5 +++-- core/debuggercontroller.cpp | 2 +- docs/guide/remote-debugging.md | 5 ++--- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/core/adapters/lldbadapter.cpp b/core/adapters/lldbadapter.cpp index 484c8d4..a541207 100644 --- a/core/adapters/lldbadapter.cpp +++ b/core/adapters/lldbadapter.cpp @@ -208,6 +208,14 @@ bool LldbAdapter::ExecuteWithArgs(const std::string& path, const std::string& ar if (Settings::Instance()->Get("debugger.stopAtEntryPoint") && m_hasEntryFunction) AddBreakpoint(ModuleNameAndOffset(configs.inputFile, m_entryPoint - m_start)); + if (configs.connectedToDebugServer) + { + // During remote debugging. lldb will try to upload the samples to the working directory before launching. + // The working directory defaults to the path the lldb-server is in, which is likely not the intended one. + // Here we set the remote working directory to the one specified by the user + auto result = InvokeBackendCommand(fmt::format("platform settings -w \"{}\"", workingDir)); + } + std::string launchCommand = "process launch"; if (Settings::Instance()->Get("debugger.stopAtSystemEntryPoint") || (m_isElFWithoutDynamicLoader && (path == configs.inputFile))) diff --git a/core/debugadapter.h b/core/debugadapter.h index ccfdfcf..9150b1d 100644 --- a/core/debugadapter.h +++ b/core/debugadapter.h @@ -62,11 +62,12 @@ namespace BinaryNinjaDebugger { { bool requestTerminalEmulator; std::string inputFile; + bool connectedToDebugServer; LaunchConfigurations() : requestTerminalEmulator(true) {} - LaunchConfigurations(bool terminal, const std::string& file) : requestTerminalEmulator(terminal), - inputFile(file) + LaunchConfigurations(bool terminal, const std::string& file, bool debugServer) : + requestTerminalEmulator(terminal), inputFile(file), connectedToDebugServer(debugServer) {} }; diff --git a/core/debuggercontroller.cpp b/core/debuggercontroller.cpp index bcef868..8c850a0 100644 --- a/core/debuggercontroller.cpp +++ b/core/debuggercontroller.cpp @@ -253,7 +253,7 @@ bool DebuggerController::Execute() std::string filePath = m_state->GetExecutablePath(); bool requestTerminal = m_state->GetRequestTerminalEmulator(); - LaunchConfigurations configs = {requestTerminal, m_state->GetInputFile()}; + LaunchConfigurations configs = {requestTerminal, m_state->GetInputFile(), m_state->IsConnectedToDebugServer()}; #ifdef WIN32 /* temporary solution (not great, sorry!), we probably won't have to do this once we introduce std::filesystem::path */ diff --git a/docs/guide/remote-debugging.md b/docs/guide/remote-debugging.md index 97b8855..130f5f6 100644 --- a/docs/guide/remote-debugging.md +++ b/docs/guide/remote-debugging.md @@ -202,9 +202,8 @@ If the remote host is a macOS system, select `remote-macosx`. If the remote host - Click `Accept`. A message box will show up if the connection is successful. -- Now one can launch the target in the same way as local debugging. However, since the path of the executable on the remote machine is very likely to be different from the path on the local machine. There are two ways to deal with it. - - Do nothing. LLDB will copy the executable on the local system to the remote system, likely into a tmp folder, and then execute it. This works when the executable is insensitive to its location. - - Change the platform working directory. If the executable already exists on the remote system, we can change the platform working directory to the folder which the executable is in. LLDB will detect the existence of the executable and launch it. To do it, run the command `platform settings -w /path/to/desided/directory` in the Debugger Console widget in the global area panel. +- Open the `Debug Adapter Settings` dialog, and set the `Working Directory` to the *remote* directory that you wish to launch the process in. Do not leave the path unchanged since it will then be a local path, and there will be an error during launch. +- Do NOT change the `Executable Path` to a remote path. Set it to the local path where the executable is in. During launch, LLDB will copy the executable to the remote host, put it in the working directory we supplied above, and launch it. Setting a remote path here will cause errors. LLDB is smart enough to check the hash of the file so that it will only copy the file once. - Launch the target One can also attach to a process running on the remote machine via its PID. In that case, there is no need to change the current working directory.