Skip to content

Commit 19b6efd

Browse files
committed
Properly set the remote working directory when using LLDB adapter for remote debugging. Fix #565
1 parent 910b5f1 commit 19b6efd

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

core/adapters/lldbadapter.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ bool LldbAdapter::ExecuteWithArgs(const std::string& path, const std::string& ar
208208
if (Settings::Instance()->Get<bool>("debugger.stopAtEntryPoint") && m_hasEntryFunction)
209209
AddBreakpoint(ModuleNameAndOffset(configs.inputFile, m_entryPoint - m_start));
210210

211+
if (configs.connectedToDebugServer)
212+
{
213+
// During remote debugging. lldb will try to upload the samples to the working directory before launching.
214+
// The working directory defaults to the path the lldb-server is in, which is likely not the intended one.
215+
// Here we set the remote working directory to the one specified by the user
216+
auto result = InvokeBackendCommand(fmt::format("platform settings -w \"{}\"", workingDir));
217+
}
218+
211219
std::string launchCommand = "process launch";
212220
if (Settings::Instance()->Get<bool>("debugger.stopAtSystemEntryPoint") ||
213221
(m_isElFWithoutDynamicLoader && (path == configs.inputFile)))

core/debugadapter.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ namespace BinaryNinjaDebugger {
6262
{
6363
bool requestTerminalEmulator;
6464
std::string inputFile;
65+
bool connectedToDebugServer;
6566

6667
LaunchConfigurations() : requestTerminalEmulator(true) {}
6768

68-
LaunchConfigurations(bool terminal, const std::string& file) : requestTerminalEmulator(terminal),
69-
inputFile(file)
69+
LaunchConfigurations(bool terminal, const std::string& file, bool debugServer) :
70+
requestTerminalEmulator(terminal), inputFile(file), connectedToDebugServer(debugServer)
7071
{}
7172
};
7273

core/debuggercontroller.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ bool DebuggerController::Execute()
253253

254254
std::string filePath = m_state->GetExecutablePath();
255255
bool requestTerminal = m_state->GetRequestTerminalEmulator();
256-
LaunchConfigurations configs = {requestTerminal, m_state->GetInputFile()};
256+
LaunchConfigurations configs = {requestTerminal, m_state->GetInputFile(), m_state->IsConnectedToDebugServer()};
257257

258258
#ifdef WIN32
259259
/* temporary solution (not great, sorry!), we probably won't have to do this once we introduce std::filesystem::path */

docs/guide/remote-debugging.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,8 @@ If the remote host is a macOS system, select `remote-macosx`. If the remote host
202202

203203

204204
- Click `Accept`. A message box will show up if the connection is successful.
205-
- 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.
206-
- 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.
207-
- 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.
205+
- 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.
206+
- 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.
208207
- Launch the target
209208

210209
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.

0 commit comments

Comments
 (0)