diff --git a/lldb/docs/use/remote.rst b/lldb/docs/use/remote.rst --- a/lldb/docs/use/remote.rst +++ b/lldb/docs/use/remote.rst @@ -135,6 +135,11 @@ commands: get-file, put-file, mkdir, etc. The environment can be prepared further using the platform shell command. +When using the "remote-android" platform, the client LLDB forwards two ports, one +for connecting to the platform, and another for connecting to the gdbserver. +The client ports are configurable through the environment variables +ANDROID_PLATFORM_PORT and ANDROID_PLATFORM_GDB_PORT, respectively. + Launching a locally built process on the remote machine ------------------------------------------------------- diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h b/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h --- a/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h +++ b/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h @@ -49,7 +49,8 @@ void DeleteForwardPort(lldb::pid_t pid); - Status MakeConnectURL(const lldb::pid_t pid, const uint16_t remote_port, + Status MakeConnectURL(const lldb::pid_t pid, const uint16_t local_port, + const uint16_t remote_port, llvm::StringRef remote_socket_name, std::string &connect_url); diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp b/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp --- a/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp +++ b/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp @@ -90,8 +90,13 @@ Log *log = GetLog(LLDBLog::Platform); - auto error = - MakeConnectURL(pid, remote_port, socket_name.c_str(), connect_url); + uint16_t local_port = 0; + const char *gdbstub_port = std::getenv("ANDROID_PLATFORM_LOCAL_GDB_PORT"); + if (gdbstub_port) + local_port = std::stoi(gdbstub_port); + + auto error = MakeConnectURL(pid, local_port, remote_port, socket_name.c_str(), + connect_url); if (error.Success() && log) LLDB_LOGF(log, "gdbserver connect URL: %s", connect_url.c_str()); @@ -126,10 +131,15 @@ else if (parsed_url->scheme == "unix-abstract-connect") m_socket_namespace = AdbClient::UnixSocketNamespaceAbstract; + uint16_t local_port = 0; + const char *platform_local_port = std::getenv("ANDROID_PLATFORM_LOCAL_PORT"); + if (platform_local_port) + local_port = std::stoi(platform_local_port); + std::string connect_url; - auto error = - MakeConnectURL(g_remote_platform_pid, parsed_url->port.value_or(0), - parsed_url->path, connect_url); + auto error = MakeConnectURL(g_remote_platform_pid, local_port, + parsed_url->port.value_or(0), parsed_url->path, + connect_url); if (error.Fail()) return error; @@ -170,11 +180,28 @@ } Status PlatformAndroidRemoteGDBServer::MakeConnectURL( - const lldb::pid_t pid, const uint16_t remote_port, - llvm::StringRef remote_socket_name, std::string &connect_url) { + const lldb::pid_t pid, const uint16_t local_port, + const uint16_t remote_port, llvm::StringRef remote_socket_name, + std::string &connect_url) { static const int kAttempsNum = 5; Status error; + + auto forward = [&](const uint16_t local, const uint16_t remote) { + error = ForwardPortWithAdb(local, remote, remote_socket_name, + m_socket_namespace, m_device_id); + if (error.Success()) { + m_port_forwards[pid] = local; + std::ostringstream url_str; + url_str << "connect://127.0.0.1:" << local; + connect_url = url_str.str(); + } + return error; + }; + + if (local_port != 0) + return forward(local_port, remote_port); + // There is a race possibility that somebody will occupy a port while we're // in between FindUnusedPort and ForwardPortWithAdb - adding the loop to // mitigate such problem. @@ -184,15 +211,8 @@ if (error.Fail()) return error; - error = ForwardPortWithAdb(local_port, remote_port, remote_socket_name, - m_socket_namespace, m_device_id); - if (error.Success()) { - m_port_forwards[pid] = local_port; - std::ostringstream url_str; - url_str << "connect://127.0.0.1:" << local_port; - connect_url = url_str.str(); + if (forward(local_port, remote_port).Success()) break; - } } return error; @@ -216,7 +236,7 @@ } std::string new_connect_url; - error = MakeConnectURL(s_remote_gdbserver_fake_pid--, + error = MakeConnectURL(s_remote_gdbserver_fake_pid--, 0, parsed_url->port.value_or(0), parsed_url->path, new_connect_url); if (error.Fail())