Add domain socket support to gdb-remote protocol and lldb-server.

http://reviews.llvm.org/D13881

llvm-svn: 250933
This commit is contained in:
Oleksiy Vyalov
2015-10-21 19:34:26 +00:00
parent 526b5f9cd4
commit 9fe526c2e7
21 changed files with 507 additions and 319 deletions

View File

@@ -24,7 +24,10 @@ using namespace platform_android;
static const lldb::pid_t g_remote_platform_pid = 0; // Alias for the process id of lldb-platform
static Error
ForwardPortWithAdb (const uint16_t local_port, const uint16_t remote_port, std::string& device_id)
ForwardPortWithAdb (const uint16_t local_port,
const uint16_t remote_port,
const char* remote_socket_name,
std::string& device_id)
{
Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
@@ -37,7 +40,16 @@ ForwardPortWithAdb (const uint16_t local_port, const uint16_t remote_port, std::
if (log)
log->Printf("Connected to Android device \"%s\"", device_id.c_str ());
return adb.SetPortForwarding(local_port, remote_port);
if (remote_port != 0)
{
if (log)
log->Printf("Forwarding remote TCP port %d to local TCP port %d", remote_port, local_port);
return adb.SetPortForwarding(local_port, remote_port);
}
if (log)
log->Printf("Forwarding remote socket \"%s\" to local TCP port %d", remote_socket_name, local_port);
return adb.SetPortForwarding(local_port, remote_socket_name);
}
static Error
@@ -72,16 +84,24 @@ PlatformAndroidRemoteGDBServer::~PlatformAndroidRemoteGDBServer ()
DeleteForwardPortWithAdb(it.second, m_device_id);
}
uint16_t
PlatformAndroidRemoteGDBServer::LaunchGDBserverAndGetPort (lldb::pid_t &pid)
bool
PlatformAndroidRemoteGDBServer::LaunchGDBServer (lldb::pid_t &pid, std::string &connect_url)
{
uint16_t remote_port = m_gdb_client.LaunchGDBserverAndGetPort (pid, "127.0.0.1");
if (remote_port == 0)
return remote_port;
uint16_t remote_port = 0;
std::string socket_name;
if (!m_gdb_client.LaunchGDBServer ("127.0.0.1", pid, remote_port, socket_name))
return false;
uint16_t local_port = 0;
auto error = SetPortForwarding (pid, remote_port, local_port);
return error.Success() ? local_port : 0;
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
auto error = MakeConnectURL (pid,
remote_port,
socket_name.c_str (),
connect_url);
if (error.Success() && log)
log->Printf("gdbserver connect URL: %s", connect_url.c_str());
return error.Success();
}
bool
@@ -106,21 +126,22 @@ PlatformAndroidRemoteGDBServer::ConnectRemote (Args& args)
return Error("URL is null.");
if (!UriParser::Parse (url, scheme, host, remote_port, path))
return Error("Invalid URL: %s", url);
if (scheme == "adb")
m_device_id = host;
m_device_id = host;
std::string connect_url;
auto error = MakeConnectURL (g_remote_platform_pid,
(remote_port < 0) ? 0 : remote_port,
path.c_str (),
connect_url);
uint16_t local_port = 0;
auto error = SetPortForwarding (g_remote_platform_pid, remote_port, local_port);
if (error.Fail ())
return error;
const std::string new_url = MakeUrl(
scheme.c_str(), host.c_str(), local_port, path.c_str());
args.ReplaceArgumentAtIndex (0, new_url.c_str ());
args.ReplaceArgumentAtIndex (0, connect_url.c_str ());
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
if (log)
log->Printf("Rewritten URL: %s", new_url.c_str());
log->Printf("Rewritten platform connect URL: %s", connect_url.c_str());
error = PlatformRemoteGDBServer::ConnectRemote(args);
if (error.Fail ())
@@ -156,9 +177,10 @@ PlatformAndroidRemoteGDBServer::DeleteForwardPort (lldb::pid_t pid)
}
Error
PlatformAndroidRemoteGDBServer::SetPortForwarding(const lldb::pid_t pid,
const uint16_t remote_port,
uint16_t &local_port)
PlatformAndroidRemoteGDBServer::MakeConnectURL(const lldb::pid_t pid,
const uint16_t remote_port,
const char* remote_socket_name,
std::string& connect_url)
{
static const int kAttempsNum = 5;
@@ -168,35 +190,21 @@ PlatformAndroidRemoteGDBServer::SetPortForwarding(const lldb::pid_t pid,
// adding the loop to mitigate such problem.
for (auto i = 0; i < kAttempsNum; ++i)
{
uint16_t local_port = 0;
error = FindUnusedPort(local_port);
if (error.Fail())
return error;
error = ForwardPortWithAdb(local_port, remote_port, m_device_id);
error = ForwardPortWithAdb(local_port, remote_port, remote_socket_name, m_device_id);
if (error.Success())
{
m_port_forwards[pid] = local_port;
std::ostringstream url_str;
url_str << "connect://localhost:" << local_port;
connect_url = url_str.str();
break;
}
}
return error;
}
std::string
PlatformAndroidRemoteGDBServer::MakeUrl(const char* scheme,
const char* hostname,
uint16_t port,
const char* path)
{
std::ostringstream hostname_str;
if (!strcmp(scheme, "adb"))
hostname_str << "[" << hostname << "]";
else
hostname_str << hostname;
return PlatformRemoteGDBServer::MakeUrl(scheme,
hostname_str.str().c_str(),
port,
path);
}