| 1 | //===-- PlatformAndroidRemoteGDBServer.cpp --------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Host/ConnectionFileDescriptor.h" |
| 10 | #include "lldb/Host/common/TCPSocket.h" |
| 11 | #include "lldb/Utility/LLDBLog.h" |
| 12 | #include "lldb/Utility/Log.h" |
| 13 | #include "lldb/Utility/Status.h" |
| 14 | #include "lldb/Utility/UriParser.h" |
| 15 | |
| 16 | #include "PlatformAndroidRemoteGDBServer.h" |
| 17 | |
| 18 | #include <optional> |
| 19 | #include <sstream> |
| 20 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | using namespace platform_android; |
| 24 | |
| 25 | static const lldb::pid_t g_remote_platform_pid = |
| 26 | 0; // Alias for the process id of lldb-platform |
| 27 | |
| 28 | static Status ForwardPortWithAdb( |
| 29 | const uint16_t local_port, const uint16_t remote_port, |
| 30 | llvm::StringRef remote_socket_name, |
| 31 | const std::optional<AdbClient::UnixSocketNamespace> &socket_namespace, |
| 32 | std::string &device_id) { |
| 33 | Log *log = GetLog(mask: LLDBLog::Platform); |
| 34 | |
| 35 | AdbClient adb; |
| 36 | auto error = AdbClient::CreateByDeviceID(device_id, adb); |
| 37 | if (error.Fail()) |
| 38 | return error; |
| 39 | |
| 40 | device_id = adb.GetDeviceID(); |
| 41 | LLDB_LOGF(log, "Connected to Android device \"%s\"" , device_id.c_str()); |
| 42 | |
| 43 | if (remote_port != 0) { |
| 44 | LLDB_LOGF(log, "Forwarding remote TCP port %d to local TCP port %d" , |
| 45 | remote_port, local_port); |
| 46 | return adb.SetPortForwarding(local_port, remote_port); |
| 47 | } |
| 48 | |
| 49 | LLDB_LOGF(log, "Forwarding remote socket \"%s\" to local TCP port %d" , |
| 50 | remote_socket_name.str().c_str(), local_port); |
| 51 | |
| 52 | if (!socket_namespace) |
| 53 | return Status::FromErrorString(str: "Invalid socket namespace" ); |
| 54 | |
| 55 | return adb.SetPortForwarding(local_port, remote_socket_name, |
| 56 | socket_namespace: *socket_namespace); |
| 57 | } |
| 58 | |
| 59 | static Status DeleteForwardPortWithAdb(uint16_t local_port, |
| 60 | const std::string &device_id) { |
| 61 | AdbClient adb(device_id); |
| 62 | return adb.DeletePortForwarding(local_port); |
| 63 | } |
| 64 | |
| 65 | static Status FindUnusedPort(uint16_t &port) { |
| 66 | Status error; |
| 67 | std::unique_ptr<TCPSocket> tcp_socket(new TCPSocket(true)); |
| 68 | if (error.Fail()) |
| 69 | return error; |
| 70 | |
| 71 | error = tcp_socket->Listen(name: "127.0.0.1:0" , backlog: 1); |
| 72 | if (error.Success()) |
| 73 | port = tcp_socket->GetLocalPortNumber(); |
| 74 | |
| 75 | return error; |
| 76 | } |
| 77 | |
| 78 | PlatformAndroidRemoteGDBServer::~PlatformAndroidRemoteGDBServer() { |
| 79 | for (const auto &it : m_port_forwards) |
| 80 | DeleteForwardPortWithAdb(local_port: it.second, device_id: m_device_id); |
| 81 | } |
| 82 | |
| 83 | bool PlatformAndroidRemoteGDBServer::LaunchGDBServer(lldb::pid_t &pid, |
| 84 | std::string &connect_url) { |
| 85 | assert(IsConnected()); |
| 86 | uint16_t remote_port = 0; |
| 87 | std::string socket_name; |
| 88 | if (!m_gdb_client_up->LaunchGDBServer(remote_accept_hostname: "127.0.0.1" , pid, port&: remote_port, |
| 89 | socket_name)) |
| 90 | return false; |
| 91 | |
| 92 | Log *log = GetLog(mask: LLDBLog::Platform); |
| 93 | |
| 94 | uint16_t local_port = 0; |
| 95 | const char *gdbstub_port = std::getenv(name: "ANDROID_PLATFORM_LOCAL_GDB_PORT" ); |
| 96 | if (gdbstub_port) |
| 97 | local_port = std::stoi(str: gdbstub_port); |
| 98 | |
| 99 | auto error = MakeConnectURL(pid, local_port, remote_port, remote_socket_name: socket_name.c_str(), |
| 100 | connect_url); |
| 101 | if (error.Success() && log) |
| 102 | LLDB_LOGF(log, "gdbserver connect URL: %s" , connect_url.c_str()); |
| 103 | |
| 104 | return error.Success(); |
| 105 | } |
| 106 | |
| 107 | bool PlatformAndroidRemoteGDBServer::KillSpawnedProcess(lldb::pid_t pid) { |
| 108 | assert(IsConnected()); |
| 109 | DeleteForwardPort(pid); |
| 110 | return m_gdb_client_up->KillSpawnedProcess(pid); |
| 111 | } |
| 112 | |
| 113 | Status PlatformAndroidRemoteGDBServer::ConnectRemote(Args &args) { |
| 114 | m_device_id.clear(); |
| 115 | |
| 116 | if (args.GetArgumentCount() != 1) |
| 117 | return Status::FromErrorString( |
| 118 | str: "\"platform connect\" takes a single argument: <connect-url>" ); |
| 119 | |
| 120 | const char *url = args.GetArgumentAtIndex(idx: 0); |
| 121 | if (!url) |
| 122 | return Status::FromErrorString(str: "URL is null." ); |
| 123 | std::optional<URI> parsed_url = URI::Parse(uri: url); |
| 124 | if (!parsed_url) |
| 125 | return Status::FromErrorStringWithFormat(format: "Invalid URL: %s" , url); |
| 126 | if (parsed_url->hostname != "localhost" ) |
| 127 | m_device_id = parsed_url->hostname.str(); |
| 128 | |
| 129 | m_socket_namespace.reset(); |
| 130 | if (parsed_url->scheme == "unix-connect" ) |
| 131 | m_socket_namespace = AdbClient::UnixSocketNamespaceFileSystem; |
| 132 | else if (parsed_url->scheme == "unix-abstract-connect" ) |
| 133 | m_socket_namespace = AdbClient::UnixSocketNamespaceAbstract; |
| 134 | |
| 135 | uint16_t local_port = 0; |
| 136 | const char *platform_local_port = std::getenv(name: "ANDROID_PLATFORM_LOCAL_PORT" ); |
| 137 | if (platform_local_port) |
| 138 | local_port = std::stoi(str: platform_local_port); |
| 139 | |
| 140 | std::string connect_url; |
| 141 | auto error = MakeConnectURL(pid: g_remote_platform_pid, local_port, |
| 142 | remote_port: parsed_url->port.value_or(u: 0), remote_socket_name: parsed_url->path, |
| 143 | connect_url); |
| 144 | |
| 145 | if (error.Fail()) |
| 146 | return error; |
| 147 | |
| 148 | args.ReplaceArgumentAtIndex(idx: 0, arg_str: connect_url); |
| 149 | |
| 150 | Log *log = GetLog(mask: LLDBLog::Platform); |
| 151 | LLDB_LOGF(log, "Rewritten platform connect URL: %s" , connect_url.c_str()); |
| 152 | |
| 153 | error = PlatformRemoteGDBServer::ConnectRemote(args); |
| 154 | if (error.Fail()) |
| 155 | DeleteForwardPort(pid: g_remote_platform_pid); |
| 156 | |
| 157 | return error; |
| 158 | } |
| 159 | |
| 160 | Status PlatformAndroidRemoteGDBServer::DisconnectRemote() { |
| 161 | DeleteForwardPort(pid: g_remote_platform_pid); |
| 162 | return PlatformRemoteGDBServer::DisconnectRemote(); |
| 163 | } |
| 164 | |
| 165 | void PlatformAndroidRemoteGDBServer::DeleteForwardPort(lldb::pid_t pid) { |
| 166 | Log *log = GetLog(mask: LLDBLog::Platform); |
| 167 | |
| 168 | auto it = m_port_forwards.find(x: pid); |
| 169 | if (it == m_port_forwards.end()) |
| 170 | return; |
| 171 | |
| 172 | const auto port = it->second; |
| 173 | const auto error = DeleteForwardPortWithAdb(local_port: port, device_id: m_device_id); |
| 174 | if (error.Fail()) { |
| 175 | LLDB_LOGF(log, |
| 176 | "Failed to delete port forwarding (pid=%" PRIu64 |
| 177 | ", port=%d, device=%s): %s" , |
| 178 | pid, port, m_device_id.c_str(), error.AsCString()); |
| 179 | } |
| 180 | m_port_forwards.erase(position: it); |
| 181 | } |
| 182 | |
| 183 | Status PlatformAndroidRemoteGDBServer::MakeConnectURL( |
| 184 | const lldb::pid_t pid, const uint16_t local_port, |
| 185 | const uint16_t remote_port, llvm::StringRef remote_socket_name, |
| 186 | std::string &connect_url) { |
| 187 | static const int kAttempsNum = 5; |
| 188 | |
| 189 | Status error; |
| 190 | |
| 191 | auto forward = [&](const uint16_t local, const uint16_t remote) { |
| 192 | Status error = ForwardPortWithAdb(local_port: local, remote_port: remote, remote_socket_name, |
| 193 | socket_namespace: m_socket_namespace, device_id&: m_device_id); |
| 194 | if (error.Success()) { |
| 195 | m_port_forwards[pid] = local; |
| 196 | std::ostringstream url_str; |
| 197 | url_str << "connect://127.0.0.1:" << local; |
| 198 | connect_url = url_str.str(); |
| 199 | } |
| 200 | return error; |
| 201 | }; |
| 202 | |
| 203 | if (local_port != 0) |
| 204 | return forward(local_port, remote_port); |
| 205 | |
| 206 | // There is a race possibility that somebody will occupy a port while we're |
| 207 | // in between FindUnusedPort and ForwardPortWithAdb - adding the loop to |
| 208 | // mitigate such problem. |
| 209 | for (auto i = 0; i < kAttempsNum; ++i) { |
| 210 | uint16_t local_port = 0; |
| 211 | error = FindUnusedPort(port&: local_port); |
| 212 | if (error.Fail()) |
| 213 | return error; |
| 214 | |
| 215 | if (forward(local_port, remote_port).Success()) |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | return error; |
| 220 | } |
| 221 | |
| 222 | lldb::ProcessSP PlatformAndroidRemoteGDBServer::ConnectProcess( |
| 223 | llvm::StringRef connect_url, llvm::StringRef plugin_name, |
| 224 | lldb_private::Debugger &debugger, lldb_private::Target *target, |
| 225 | lldb_private::Status &error) { |
| 226 | // We don't have the pid of the remote gdbserver when it isn't started by us |
| 227 | // but we still want to store the list of port forwards we set up in our port |
| 228 | // forward map. Generate a fake pid for these cases what won't collide with |
| 229 | // any other valid pid on android. |
| 230 | static lldb::pid_t s_remote_gdbserver_fake_pid = 0xffffffffffffffffULL; |
| 231 | |
| 232 | std::optional<URI> parsed_url = URI::Parse(uri: connect_url); |
| 233 | if (!parsed_url) { |
| 234 | error = Status::FromErrorStringWithFormatv(format: "Invalid URL: {0}" , args&: connect_url); |
| 235 | return nullptr; |
| 236 | } |
| 237 | |
| 238 | std::string new_connect_url; |
| 239 | error = MakeConnectURL(pid: s_remote_gdbserver_fake_pid--, local_port: 0, |
| 240 | remote_port: parsed_url->port.value_or(u: 0), remote_socket_name: parsed_url->path, |
| 241 | connect_url&: new_connect_url); |
| 242 | if (error.Fail()) |
| 243 | return nullptr; |
| 244 | |
| 245 | return PlatformRemoteGDBServer::ConnectProcess(connect_url: new_connect_url, plugin_name, |
| 246 | debugger, target, error); |
| 247 | } |
| 248 | |