Skip to content

Commit

Permalink
Properly set the remote working directory when using LLDB adapter for…
Browse files Browse the repository at this point in the history
… remote debugging. Fix #565
  • Loading branch information
xusheng6 committed Apr 17, 2024
1 parent 910b5f1 commit 19b6efd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
8 changes: 8 additions & 0 deletions core/adapters/lldbadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ bool LldbAdapter::ExecuteWithArgs(const std::string& path, const std::string& ar
if (Settings::Instance()->Get<bool>("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<bool>("debugger.stopAtSystemEntryPoint") ||
(m_isElFWithoutDynamicLoader && (path == configs.inputFile)))
Expand Down
5 changes: 3 additions & 2 deletions core/debugadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{}
};

Expand Down
2 changes: 1 addition & 1 deletion core/debuggercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
5 changes: 2 additions & 3 deletions docs/guide/remote-debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 19b6efd

Please sign in to comment.