23using namespace platform_android;
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) {
41 LLDB_LOGF(log,
"Connected to Android device \"%s\"", device_id.c_str());
43 if (remote_port != 0) {
44 LLDB_LOGF(log,
"Forwarding remote TCP port %d to local TCP port %d",
45 remote_port, local_port);
49 LLDB_LOGF(log,
"Forwarding remote socket \"%s\" to local TCP port %d",
50 remote_socket_name.str().c_str(), local_port);
52 if (!socket_namespace)
53 return Status(
"Invalid socket namespace");
60 const std::string &device_id) {
67 std::unique_ptr<TCPSocket> tcp_socket(
new TCPSocket(
true,
false));
71 error = tcp_socket->Listen(
"127.0.0.1:0", 1);
73 port = tcp_socket->GetLocalPortNumber();
84 std::string &connect_url) {
86 uint16_t remote_port = 0;
87 std::string socket_name;
94 uint16_t local_port = 0;
95 const char *gdbstub_port = std::getenv(
"ANDROID_PLATFORM_LOCAL_GDB_PORT");
97 local_port = std::stoi(gdbstub_port);
101 if (
error.Success() && log)
102 LLDB_LOGF(log,
"gdbserver connect URL: %s", connect_url.c_str());
104 return error.Success();
118 "\"platform connect\" takes a single argument: <connect-url>");
122 return Status(
"URL is null.");
123 std::optional<URI> parsed_url =
URI::Parse(url);
125 return Status(
"Invalid URL: %s", url);
126 if (parsed_url->hostname !=
"localhost")
130 if (parsed_url->scheme ==
"unix-connect")
132 else if (parsed_url->scheme ==
"unix-abstract-connect")
135 uint16_t local_port = 0;
136 const char *platform_local_port = std::getenv(
"ANDROID_PLATFORM_LOCAL_PORT");
137 if (platform_local_port)
138 local_port = std::stoi(platform_local_port);
140 std::string connect_url;
142 parsed_url->port.value_or(0), parsed_url->path,
151 LLDB_LOGF(log,
"Rewritten platform connect URL: %s", connect_url.c_str());
153 error = PlatformRemoteGDBServer::ConnectRemote(args);
162 return PlatformRemoteGDBServer::DisconnectRemote();
172 const auto port = it->second;
176 "Failed to delete port forwarding (pid=%" PRIu64
177 ", port=%d, device=%s): %s",
185 const uint16_t remote_port, llvm::StringRef remote_socket_name,
186 std::string &connect_url) {
187 static const int kAttempsNum = 5;
191 auto forward = [&](
const uint16_t local,
const uint16_t remote) {
194 if (
error.Success()) {
196 std::ostringstream url_str;
197 url_str <<
"connect://127.0.0.1:" << local;
198 connect_url = url_str.str();
204 return forward(local_port, remote_port);
209 for (
auto i = 0; i < kAttempsNum; ++i) {
210 uint16_t local_port = 0;
215 if (forward(local_port, remote_port).
Success())
223 llvm::StringRef connect_url, llvm::StringRef plugin_name,
230 static lldb::pid_t s_remote_gdbserver_fake_pid = 0xffffffffffffffffULL;
232 std::optional<URI> parsed_url =
URI::Parse(connect_url);
234 error.SetErrorStringWithFormat(
"Invalid URL: %s",
235 connect_url.str().c_str());
239 std::string new_connect_url;
241 parsed_url->port.value_or(0), parsed_url->path,
246 return PlatformRemoteGDBServer::ConnectProcess(new_connect_url, plugin_name,
247 debugger, target,
error);
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
A command line argument class.
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
void ReplaceArgumentAtIndex(size_t idx, llvm::StringRef arg_str, char quote_char='\0')
Replaces the argument value at index idx to arg_str if idx is a valid argument index.
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx.
A class to manage flag bits.
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::shared_ptr< lldb_private::Process > ProcessSP
static std::optional< URI > Parse(llvm::StringRef uri)