Make remote-android platform to use dynamic local tcp ports when forwarding device ports.

http://reviews.llvm.org/D12510

llvm-svn: 246574
This commit is contained in:
Oleksiy Vyalov 2015-09-01 19:02:14 +00:00
parent 8ea8b343c4
commit e7eabbb550
6 changed files with 98 additions and 42 deletions

View File

@ -131,10 +131,10 @@ AdbClient::GetDevices (DeviceIDList &device_list)
} }
Error Error
AdbClient::SetPortForwarding (const uint16_t port) AdbClient::SetPortForwarding (const uint16_t local_port, const uint16_t remote_port)
{ {
char message[48]; char message[48];
snprintf (message, sizeof (message), "forward:tcp:%d;tcp:%d", port, port); snprintf (message, sizeof (message), "forward:tcp:%d;tcp:%d", local_port, remote_port);
const auto error = SendDeviceMessage (message); const auto error = SendDeviceMessage (message);
if (error.Fail ()) if (error.Fail ())
@ -144,10 +144,10 @@ AdbClient::SetPortForwarding (const uint16_t port)
} }
Error Error
AdbClient::DeletePortForwarding (const uint16_t port) AdbClient::DeletePortForwarding (const uint16_t local_port)
{ {
char message[32]; char message[32];
snprintf (message, sizeof (message), "killforward:tcp:%d", port); snprintf (message, sizeof (message), "killforward:tcp:%d", local_port);
const auto error = SendDeviceMessage (message); const auto error = SendDeviceMessage (message);
if (error.Fail ()) if (error.Fail ())

View File

@ -48,10 +48,10 @@ public:
GetDevices (DeviceIDList &device_list); GetDevices (DeviceIDList &device_list);
Error Error
SetPortForwarding (const uint16_t port); SetPortForwarding (const uint16_t local_port, const uint16_t remote_port);
Error Error
DeletePortForwarding (const uint16_t port); DeletePortForwarding (const uint16_t local_port);
Error Error
PullFile (const FileSpec &remote_file, const FileSpec &local_file); PullFile (const FileSpec &remote_file, const FileSpec &local_file);

View File

@ -10,6 +10,7 @@
// Other libraries and framework includes // Other libraries and framework includes
#include "lldb/Core/Error.h" #include "lldb/Core/Error.h"
#include "lldb/Core/Log.h" #include "lldb/Core/Log.h"
#include "lldb/Host/Socket.h"
// Project includes // Project includes
#include "AdbClient.h" #include "AdbClient.h"
@ -25,7 +26,7 @@ using namespace platform_android;
static const lldb::pid_t g_remote_platform_pid = 0; // Alias for the process id of lldb-platform static const lldb::pid_t g_remote_platform_pid = 0; // Alias for the process id of lldb-platform
static Error static Error
ForwardPortWithAdb (uint16_t port, std::string& device_id) ForwardPortWithAdb (const uint16_t local_port, const uint16_t remote_port, std::string& device_id)
{ {
Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
@ -38,14 +39,27 @@ ForwardPortWithAdb (uint16_t port, std::string& device_id)
if (log) if (log)
log->Printf("Connected to Android device \"%s\"", device_id.c_str ()); log->Printf("Connected to Android device \"%s\"", device_id.c_str ());
return adb.SetPortForwarding(port); return adb.SetPortForwarding(local_port, remote_port);
} }
static Error static Error
DeleteForwardPortWithAdb (uint16_t port, const std::string& device_id) DeleteForwardPortWithAdb (uint16_t local_port, const std::string& device_id)
{ {
AdbClient adb (device_id); AdbClient adb (device_id);
return adb.DeletePortForwarding (port); return adb.DeletePortForwarding (local_port);
}
static Error
FindUnusedPort (uint16_t& port)
{
Socket* socket = nullptr;
auto error = Socket::TcpListen ("localhost:0", false, socket, nullptr);
if (error.Success ())
{
port = socket->GetLocalPortNumber ();
delete socket;
}
return error;
} }
PlatformAndroidRemoteGDBServer::PlatformAndroidRemoteGDBServer () PlatformAndroidRemoteGDBServer::PlatformAndroidRemoteGDBServer ()
@ -61,17 +75,13 @@ PlatformAndroidRemoteGDBServer::~PlatformAndroidRemoteGDBServer ()
uint16_t uint16_t
PlatformAndroidRemoteGDBServer::LaunchGDBserverAndGetPort (lldb::pid_t &pid) PlatformAndroidRemoteGDBServer::LaunchGDBserverAndGetPort (lldb::pid_t &pid)
{ {
uint16_t port = m_gdb_client.LaunchGDBserverAndGetPort (pid, "127.0.0.1"); uint16_t remote_port = m_gdb_client.LaunchGDBserverAndGetPort (pid, "127.0.0.1");
if (port == 0) if (remote_port == 0)
return port; return remote_port;
Error error = ForwardPortWithAdb(port, m_device_id); uint16_t local_port = 0;
if (error.Fail ()) auto error = SetPortForwarding (pid, remote_port, local_port);
return 0; return error.Success() ? local_port : 0;
m_port_forwards[pid] = port;
return port;
} }
bool bool
@ -89,21 +99,28 @@ PlatformAndroidRemoteGDBServer::ConnectRemote (Args& args)
if (args.GetArgumentCount() != 1) if (args.GetArgumentCount() != 1)
return Error("\"platform connect\" takes a single argument: <connect-url>"); return Error("\"platform connect\" takes a single argument: <connect-url>");
int port; int remote_port;
std::string scheme, host, path; std::string scheme, host, path;
const char *url = args.GetArgumentAtIndex (0); const char *url = args.GetArgumentAtIndex (0);
if (!url) if (!url)
return Error("URL is null."); return Error("URL is null.");
if (!UriParser::Parse (url, scheme, host, port, path)) if (!UriParser::Parse (url, scheme, host, remote_port, path))
return Error("Invalid URL: %s", url); return Error("Invalid URL: %s", url);
if (scheme == "adb") if (scheme == "adb")
m_device_id = host; m_device_id = host;
Error error = ForwardPortWithAdb(port, m_device_id); uint16_t local_port = 0;
if (error.Fail()) auto error = SetPortForwarding (g_remote_platform_pid, remote_port, local_port);
if (error.Fail ())
return error; return error;
m_port_forwards[g_remote_platform_pid] = port; const std::string new_url = MakeUrl(
scheme.c_str(), host.c_str(), local_port, path.c_str());
args.ReplaceArgumentAtIndex (0, new_url.c_str ());
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
if (log)
log->Printf("Rewritten URL: %s", new_url.c_str());
error = PlatformRemoteGDBServer::ConnectRemote(args); error = PlatformRemoteGDBServer::ConnectRemote(args);
if (error.Fail ()) if (error.Fail ())
@ -138,10 +155,39 @@ PlatformAndroidRemoteGDBServer::DeleteForwardPort (lldb::pid_t pid)
m_port_forwards.erase(it); m_port_forwards.erase(it);
} }
Error
PlatformAndroidRemoteGDBServer::SetPortForwarding(const lldb::pid_t pid,
const uint16_t remote_port,
uint16_t &local_port)
{
static const int kAttempsNum = 5;
Error error;
// 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.
for (auto i = 0; i < kAttempsNum; ++i)
{
error = FindUnusedPort(local_port);
if (error.Fail())
return error;
error = ForwardPortWithAdb(local_port, remote_port, m_device_id);
if (error.Success())
{
m_port_forwards[pid] = local_port;
break;
}
}
return error;
}
std::string std::string
PlatformAndroidRemoteGDBServer::MakeServerUrl(const char* scheme, PlatformAndroidRemoteGDBServer::MakeUrl(const char* scheme,
const char* hostname, const char* hostname,
uint16_t port) uint16_t port,
const char* path)
{ {
std::ostringstream hostname_str; std::ostringstream hostname_str;
if (!strcmp(scheme, "adb")) if (!strcmp(scheme, "adb"))
@ -149,7 +195,8 @@ PlatformAndroidRemoteGDBServer::MakeServerUrl(const char* scheme,
else else
hostname_str << hostname; hostname_str << hostname;
return PlatformRemoteGDBServer::MakeServerUrl(scheme, return PlatformRemoteGDBServer::MakeUrl(scheme,
hostname_str.str().c_str(), hostname_str.str().c_str(),
port); port,
path);
} }

View File

@ -50,9 +50,13 @@ protected:
DeleteForwardPort (lldb::pid_t pid); DeleteForwardPort (lldb::pid_t pid);
std::string std::string
MakeServerUrl(const char* scheme, MakeUrl(const char* scheme,
const char* hostname, const char* hostname,
uint16_t port) override; uint16_t port,
const char* path) override;
Error
SetPortForwarding(const lldb::pid_t pid, const uint16_t remote_port, uint16_t &local_port);
private: private:
DISALLOW_COPY_AND_ASSIGN (PlatformAndroidRemoteGDBServer); DISALLOW_COPY_AND_ASSIGN (PlatformAndroidRemoteGDBServer);

View File

@ -963,17 +963,21 @@ PlatformRemoteGDBServer::MakeGdbServerUrl(const std::string &platform_scheme,
const char *port_offset_c_str = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_PORT_OFFSET"); const char *port_offset_c_str = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_PORT_OFFSET");
int port_offset = port_offset_c_str ? ::atoi(port_offset_c_str) : 0; int port_offset = port_offset_c_str ? ::atoi(port_offset_c_str) : 0;
return MakeServerUrl(override_scheme ? override_scheme : platform_scheme.c_str(), return MakeUrl(override_scheme ? override_scheme : platform_scheme.c_str(),
override_hostname ? override_hostname : platform_hostname.c_str(), override_hostname ? override_hostname : platform_hostname.c_str(),
port + port_offset); port + port_offset,
nullptr);
} }
std::string std::string
PlatformRemoteGDBServer::MakeServerUrl(const char* scheme, PlatformRemoteGDBServer::MakeUrl(const char* scheme,
const char* hostname, const char* hostname,
uint16_t port) uint16_t port,
const char* path)
{ {
StreamString result; StreamString result;
result.Printf("%s://%s:%u", scheme, hostname, port); result.Printf("%s://%s:%u", scheme, hostname, port);
if (path)
result.Write(path, strlen(path));
return result.GetString(); return result.GetString();
} }

View File

@ -235,9 +235,10 @@ protected:
KillSpawnedProcess (lldb::pid_t pid); KillSpawnedProcess (lldb::pid_t pid);
virtual std::string virtual std::string
MakeServerUrl(const char* scheme, MakeUrl(const char* scheme,
const char* hostname, const char* hostname,
uint16_t port); uint16_t port,
const char* path);
private: private:
std::string std::string