30 const uint16_t local_port,
const uint16_t remote_port,
31 llvm::StringRef remote_socket_name,
32 const std::optional<AdbClient::UnixSocketNamespace> &socket_namespace,
33 std::string &device_id) {
37 if (!resolved_device_id_or_error)
39 device_id = *resolved_device_id_or_error;
42 LLDB_LOGF(log,
"Connected to Android device \"%s\"", device_id.c_str());
44 if (remote_port != 0) {
45 LLDB_LOGF(log,
"Forwarding remote TCP port %d to local TCP port %d",
46 remote_port, local_port);
50 LLDB_LOGF(log,
"Forwarding remote socket \"%s\" to local TCP port %d",
51 remote_socket_name.str().c_str(), local_port);
53 if (!socket_namespace)
61 const std::string &device_id) {
68 std::unique_ptr<TCPSocket> tcp_socket(
new TCPSocket(
true));
72 error = tcp_socket->Listen(
"127.0.0.1:0", 1);
74 port = tcp_socket->GetLocalPortNumber();
85 std::string &connect_url) {
87 uint16_t remote_port = 0;
88 std::string socket_name;
95 uint16_t local_port = 0;
96 const char *gdbstub_port = std::getenv(
"ANDROID_PLATFORM_LOCAL_GDB_PORT");
98 local_port = std::stoi(gdbstub_port);
102 if (
error.Success() && log)
103 LLDB_LOGF(log,
"gdbserver connect URL: %s", connect_url.c_str());
105 return error.Success();
119 "\"platform connect\" takes a single argument: <connect-url>");
124 std::optional<URI> parsed_url =
URI::Parse(url);
127 if (parsed_url->hostname !=
"localhost")
131 if (parsed_url->scheme ==
"unix-connect")
133 else if (parsed_url->scheme ==
"unix-abstract-connect")
136 uint16_t local_port = 0;
137 const char *platform_local_port = std::getenv(
"ANDROID_PLATFORM_LOCAL_PORT");
138 if (platform_local_port)
139 local_port = std::stoi(platform_local_port);
141 std::string connect_url;
143 parsed_url->port.value_or(0), parsed_url->path,
152 LLDB_LOGF(log,
"Rewritten platform connect URL: %s", connect_url.c_str());
154 error = PlatformRemoteGDBServer::ConnectRemote(args);
163 return PlatformRemoteGDBServer::DisconnectRemote();
173 const auto port = it->second;
177 "Failed to delete port forwarding (pid=%" PRIu64
178 ", port=%d, device=%s): %s",
186 const uint16_t remote_port, llvm::StringRef remote_socket_name,
187 std::string &connect_url) {
188 static const int kAttempsNum = 5;
192 auto forward = [&](
const uint16_t local,
const uint16_t remote) {
195 if (
error.Success()) {
197 std::ostringstream url_str;
198 url_str <<
"connect://127.0.0.1:" << local;
199 connect_url = url_str.str();
205 return forward(local_port, remote_port);
210 for (
auto i = 0; i < kAttempsNum; ++i) {
211 uint16_t local_port = 0;
216 if (forward(local_port, remote_port).
Success())
224 llvm::StringRef connect_url, llvm::StringRef plugin_name,
231 static lldb::pid_t s_remote_gdbserver_fake_pid = 0xffffffffffffffffULL;
233 std::optional<URI> parsed_url =
URI::Parse(connect_url);
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.
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status FromErrorString(const char *str)
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
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)