Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,141 @@
//===-- AdbClient.h ---------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_AdbClient_h_
#define liblldb_AdbClient_h_
#include "lldb/Utility/Status.h"
#include <chrono>
#include <functional>
#include <list>
#include <memory>
#include <string>
#include <vector>
namespace lldb_private {
class FileSpec;
namespace platform_android {
class AdbClient {
public:
enum UnixSocketNamespace {
UnixSocketNamespaceAbstract,
UnixSocketNamespaceFileSystem,
};
using DeviceIDList = std::list<std::string>;
class SyncService {
friend class AdbClient;
public:
~SyncService();
Status PullFile(const FileSpec &remote_file, const FileSpec &local_file);
Status PushFile(const FileSpec &local_file, const FileSpec &remote_file);
Status Stat(const FileSpec &remote_file, uint32_t &mode, uint32_t &size,
uint32_t &mtime);
bool IsConnected() const;
private:
explicit SyncService(std::unique_ptr<Connection> &&conn);
Status SendSyncRequest(const char *request_id, const uint32_t data_len,
const void *data);
Status ReadSyncHeader(std::string &response_id, uint32_t &data_len);
Status PullFileChunk(std::vector<char> &buffer, bool &eof);
Status ReadAllBytes(void *buffer, size_t size);
Status internalPullFile(const FileSpec &remote_file,
const FileSpec &local_file);
Status internalPushFile(const FileSpec &local_file,
const FileSpec &remote_file);
Status internalStat(const FileSpec &remote_file, uint32_t &mode,
uint32_t &size, uint32_t &mtime);
Status executeCommand(const std::function<Status()> &cmd);
std::unique_ptr<Connection> m_conn;
};
static Status CreateByDeviceID(const std::string &device_id, AdbClient &adb);
AdbClient();
explicit AdbClient(const std::string &device_id);
~AdbClient();
const std::string &GetDeviceID() const;
Status GetDevices(DeviceIDList &device_list);
Status SetPortForwarding(const uint16_t local_port,
const uint16_t remote_port);
Status SetPortForwarding(const uint16_t local_port,
llvm::StringRef remote_socket_name,
const UnixSocketNamespace socket_namespace);
Status DeletePortForwarding(const uint16_t local_port);
Status Shell(const char *command, std::chrono::milliseconds timeout,
std::string *output);
Status ShellToFile(const char *command, std::chrono::milliseconds timeout,
const FileSpec &output_file_spec);
std::unique_ptr<SyncService> GetSyncService(Status &error);
Status SwitchDeviceTransport();
private:
Status Connect();
void SetDeviceID(const std::string &device_id);
Status SendMessage(const std::string &packet, const bool reconnect = true);
Status SendDeviceMessage(const std::string &packet);
Status ReadMessage(std::vector<char> &message);
Status ReadMessageStream(std::vector<char> &message,
std::chrono::milliseconds timeout);
Status GetResponseError(const char *response_id);
Status ReadResponseStatus();
Status Sync();
Status StartSync();
Status internalShell(const char *command, std::chrono::milliseconds timeout,
std::vector<char> &output_buf);
Status ReadAllBytes(void *buffer, size_t size);
std::string m_device_id;
std::unique_ptr<Connection> m_conn;
};
} // namespace platform_android
} // namespace lldb_private
#endif // liblldb_AdbClient_h_

View File

@@ -0,0 +1,13 @@
add_lldb_library(lldbPluginPlatformAndroid PLUGIN
AdbClient.cpp
PlatformAndroid.cpp
PlatformAndroidRemoteGDBServer.cpp
LINK_LIBS
lldbCore
lldbHost
lldbPluginPlatformLinux
lldbPluginPlatformGDB
LINK_COMPONENTS
Support
)

View File

@@ -0,0 +1,403 @@
//===-- PlatformAndroid.cpp -------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Core/Module.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Scalar.h"
#include "lldb/Core/Section.h"
#include "lldb/Core/ValueObject.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Host/StringConvert.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/UriParser.h"
// Project includes
#include "AdbClient.h"
#include "PlatformAndroid.h"
#include "PlatformAndroidRemoteGDBServer.h"
#include "lldb/Target/Target.h"
using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::platform_android;
using namespace std::chrono;
static uint32_t g_initialize_count = 0;
static const unsigned int g_android_default_cache_size =
2048; // Fits inside 4k adb packet.
void PlatformAndroid::Initialize() {
PlatformLinux::Initialize();
if (g_initialize_count++ == 0) {
#if defined(__ANDROID__)
PlatformSP default_platform_sp(new PlatformAndroid(true));
default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
Platform::SetHostPlatform(default_platform_sp);
#endif
PluginManager::RegisterPlugin(
PlatformAndroid::GetPluginNameStatic(false),
PlatformAndroid::GetPluginDescriptionStatic(false),
PlatformAndroid::CreateInstance);
}
}
void PlatformAndroid::Terminate() {
if (g_initialize_count > 0) {
if (--g_initialize_count == 0) {
PluginManager::UnregisterPlugin(PlatformAndroid::CreateInstance);
}
}
PlatformLinux::Terminate();
}
PlatformSP PlatformAndroid::CreateInstance(bool force, const ArchSpec *arch) {
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
if (log) {
const char *arch_name;
if (arch && arch->GetArchitectureName())
arch_name = arch->GetArchitectureName();
else
arch_name = "<null>";
const char *triple_cstr =
arch ? arch->GetTriple().getTriple().c_str() : "<null>";
log->Printf("PlatformAndroid::%s(force=%s, arch={%s,%s})", __FUNCTION__,
force ? "true" : "false", arch_name, triple_cstr);
}
bool create = force;
if (create == false && arch && arch->IsValid()) {
const llvm::Triple &triple = arch->GetTriple();
switch (triple.getVendor()) {
case llvm::Triple::PC:
create = true;
break;
#if defined(__ANDROID__)
// Only accept "unknown" for the vendor if the host is android and
// it "unknown" wasn't specified (it was just returned because it
// was NOT specified_
case llvm::Triple::VendorType::UnknownVendor:
create = !arch->TripleVendorWasSpecified();
break;
#endif
default:
break;
}
if (create) {
switch (triple.getOS()) {
case llvm::Triple::Android:
break;
#if defined(__ANDROID__)
// Only accept "unknown" for the OS if the host is android and
// it "unknown" wasn't specified (it was just returned because it
// was NOT specified)
case llvm::Triple::OSType::UnknownOS:
create = !arch->TripleOSWasSpecified();
break;
#endif
default:
create = false;
break;
}
}
}
if (create) {
if (log)
log->Printf("PlatformAndroid::%s() creating remote-android platform",
__FUNCTION__);
return PlatformSP(new PlatformAndroid(false));
}
if (log)
log->Printf(
"PlatformAndroid::%s() aborting creation of remote-android platform",
__FUNCTION__);
return PlatformSP();
}
PlatformAndroid::PlatformAndroid(bool is_host)
: PlatformLinux(is_host), m_sdk_version(0) {}
PlatformAndroid::~PlatformAndroid() {}
ConstString PlatformAndroid::GetPluginNameStatic(bool is_host) {
if (is_host) {
static ConstString g_host_name(Platform::GetHostPlatformName());
return g_host_name;
} else {
static ConstString g_remote_name("remote-android");
return g_remote_name;
}
}
const char *PlatformAndroid::GetPluginDescriptionStatic(bool is_host) {
if (is_host)
return "Local Android user platform plug-in.";
else
return "Remote Android user platform plug-in.";
}
ConstString PlatformAndroid::GetPluginName() {
return GetPluginNameStatic(IsHost());
}
Status PlatformAndroid::ConnectRemote(Args &args) {
m_device_id.clear();
if (IsHost()) {
return Status("can't connect to the host platform '%s', always connected",
GetPluginName().GetCString());
}
if (!m_remote_platform_sp)
m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer());
int port;
llvm::StringRef scheme, host, path;
const char *url = args.GetArgumentAtIndex(0);
if (!url)
return Status("URL is null.");
if (!UriParser::Parse(url, scheme, host, port, path))
return Status("Invalid URL: %s", url);
if (host != "localhost")
m_device_id = host;
auto error = PlatformLinux::ConnectRemote(args);
if (error.Success()) {
AdbClient adb;
error = AdbClient::CreateByDeviceID(m_device_id, adb);
if (error.Fail())
return error;
m_device_id = adb.GetDeviceID();
}
return error;
}
Status PlatformAndroid::GetFile(const FileSpec &source,
const FileSpec &destination) {
if (IsHost() || !m_remote_platform_sp)
return PlatformLinux::GetFile(source, destination);
FileSpec source_spec(source.GetPath(false), false,
FileSpec::ePathSyntaxPosix);
if (source_spec.IsRelative())
source_spec = GetRemoteWorkingDirectory().CopyByAppendingPathComponent(
source_spec.GetCString(false));
Status error;
auto sync_service = GetSyncService(error);
if (error.Fail())
return error;
uint32_t mode = 0, size = 0, mtime = 0;
error = sync_service->Stat(source_spec, mode, size, mtime);
if (error.Fail())
return error;
if (mode != 0)
return sync_service->PullFile(source_spec, destination);
auto source_file = source_spec.GetCString(false);
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
if (log)
log->Printf("Got mode == 0 on '%s': try to get file via 'shell cat'",
source_file);
if (strchr(source_file, '\'') != nullptr)
return Status("Doesn't support single-quotes in filenames");
// mode == 0 can signify that adbd cannot access the file
// due security constraints - try "cat ..." as a fallback.
AdbClient adb(m_device_id);
char cmd[PATH_MAX];
snprintf(cmd, sizeof(cmd), "cat '%s'", source_file);
return adb.ShellToFile(cmd, minutes(1), destination);
}
Status PlatformAndroid::PutFile(const FileSpec &source,
const FileSpec &destination, uint32_t uid,
uint32_t gid) {
if (IsHost() || !m_remote_platform_sp)
return PlatformLinux::PutFile(source, destination, uid, gid);
FileSpec destination_spec(destination.GetPath(false), false,
FileSpec::ePathSyntaxPosix);
if (destination_spec.IsRelative())
destination_spec = GetRemoteWorkingDirectory().CopyByAppendingPathComponent(
destination_spec.GetCString(false));
// TODO: Set correct uid and gid on remote file.
Status error;
auto sync_service = GetSyncService(error);
if (error.Fail())
return error;
return sync_service->PushFile(source, destination_spec);
}
const char *PlatformAndroid::GetCacheHostname() { return m_device_id.c_str(); }
Status PlatformAndroid::DownloadModuleSlice(const FileSpec &src_file_spec,
const uint64_t src_offset,
const uint64_t src_size,
const FileSpec &dst_file_spec) {
if (src_offset != 0)
return Status("Invalid offset - %" PRIu64, src_offset);
return GetFile(src_file_spec, dst_file_spec);
}
Status PlatformAndroid::DisconnectRemote() {
Status error = PlatformLinux::DisconnectRemote();
if (error.Success()) {
m_device_id.clear();
m_sdk_version = 0;
}
return error;
}
uint32_t PlatformAndroid::GetDefaultMemoryCacheLineSize() {
return g_android_default_cache_size;
}
uint32_t PlatformAndroid::GetSdkVersion() {
if (!IsConnected())
return 0;
if (m_sdk_version != 0)
return m_sdk_version;
std::string version_string;
AdbClient adb(m_device_id);
Status error =
adb.Shell("getprop ro.build.version.sdk", seconds(5), &version_string);
version_string = llvm::StringRef(version_string).trim().str();
if (error.Fail() || version_string.empty()) {
Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM);
if (log)
log->Printf("Get SDK version failed. (error: %s, output: %s)",
error.AsCString(), version_string.c_str());
return 0;
}
m_sdk_version = StringConvert::ToUInt32(version_string.c_str());
return m_sdk_version;
}
Status PlatformAndroid::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
const FileSpec &dst_file_spec) {
// For oat file we can try to fetch additional debug info from the device
ConstString extension = module_sp->GetFileSpec().GetFileNameExtension();
if (extension != ConstString("oat") && extension != ConstString("odex"))
return Status(
"Symbol file downloading only supported for oat and odex files");
// If we have no information about the platform file we can't execute oatdump
if (!module_sp->GetPlatformFileSpec())
return Status("No platform file specified");
// Symbolizer isn't available before SDK version 23
if (GetSdkVersion() < 23)
return Status("Symbol file generation only supported on SDK 23+");
// If we already have symtab then we don't have to try and generate one
if (module_sp->GetSectionList()->FindSectionByName(ConstString(".symtab")) !=
nullptr)
return Status("Symtab already available in the module");
AdbClient adb(m_device_id);
std::string tmpdir;
Status error = adb.Shell("mktemp --directory --tmpdir /data/local/tmp",
seconds(5), &tmpdir);
if (error.Fail() || tmpdir.empty())
return Status("Failed to generate temporary directory on the device (%s)",
error.AsCString());
tmpdir = llvm::StringRef(tmpdir).trim().str();
// Create file remover for the temporary directory created on the device
std::unique_ptr<std::string, std::function<void(std::string *)>>
tmpdir_remover(&tmpdir, [&adb](std::string *s) {
StreamString command;
command.Printf("rm -rf %s", s->c_str());
Status error = adb.Shell(command.GetData(), seconds(5), nullptr);
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
if (log && error.Fail())
log->Printf("Failed to remove temp directory: %s", error.AsCString());
});
FileSpec symfile_platform_filespec(tmpdir, false);
symfile_platform_filespec.AppendPathComponent("symbolized.oat");
// Execute oatdump on the remote device to generate a file with symtab
StreamString command;
command.Printf("oatdump --symbolize=%s --output=%s",
module_sp->GetPlatformFileSpec().GetCString(false),
symfile_platform_filespec.GetCString(false));
error = adb.Shell(command.GetData(), minutes(1), nullptr);
if (error.Fail())
return Status("Oatdump failed: %s", error.AsCString());
// Download the symbolfile from the remote device
return GetFile(symfile_platform_filespec, dst_file_spec);
}
bool PlatformAndroid::GetRemoteOSVersion() {
m_major_os_version = GetSdkVersion();
m_minor_os_version = 0;
m_update_os_version = 0;
return m_major_os_version != 0;
}
llvm::StringRef
PlatformAndroid::GetLibdlFunctionDeclarations(lldb_private::Process *process) {
SymbolContextList matching_symbols;
std::vector<const char *> dl_open_names = { "__dl_dlopen", "dlopen" };
const char *dl_open_name = nullptr;
Target &target = process->GetTarget();
for (auto name: dl_open_names) {
if (target.GetImages().FindFunctionSymbols(ConstString(name),
eFunctionNameTypeFull,
matching_symbols)) {
dl_open_name = name;
break;
}
}
// Older platform versions have the dl function symbols mangled
if (dl_open_name == dl_open_names[0])
return R"(
extern "C" void* dlopen(const char*, int) asm("__dl_dlopen");
extern "C" void* dlsym(void*, const char*) asm("__dl_dlsym");
extern "C" int dlclose(void*) asm("__dl_dlclose");
extern "C" char* dlerror(void) asm("__dl_dlerror");
)";
return PlatformPOSIX::GetLibdlFunctionDeclarations(process);
}
AdbClient::SyncService *PlatformAndroid::GetSyncService(Status &error) {
if (m_adb_sync_svc && m_adb_sync_svc->IsConnected())
return m_adb_sync_svc.get();
AdbClient adb(m_device_id);
m_adb_sync_svc = adb.GetSyncService(error);
return (error.Success()) ? m_adb_sync_svc.get() : nullptr;
}

View File

@@ -0,0 +1,95 @@
//===-- PlatformAndroid.h ---------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_PlatformAndroid_h_
#define liblldb_PlatformAndroid_h_
// C Includes
// C++ Includes
#include <memory>
#include <string>
// Other libraries and framework includes
// Project includes
#include "Plugins/Platform/Linux/PlatformLinux.h"
#include "AdbClient.h"
namespace lldb_private {
namespace platform_android {
class PlatformAndroid : public platform_linux::PlatformLinux {
public:
PlatformAndroid(bool is_host);
~PlatformAndroid() override;
static void Initialize();
static void Terminate();
//------------------------------------------------------------
// lldb_private::PluginInterface functions
//------------------------------------------------------------
static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch);
static ConstString GetPluginNameStatic(bool is_host);
static const char *GetPluginDescriptionStatic(bool is_host);
ConstString GetPluginName() override;
uint32_t GetPluginVersion() override { return 1; }
//------------------------------------------------------------
// lldb_private::Platform functions
//------------------------------------------------------------
Status ConnectRemote(Args &args) override;
Status GetFile(const FileSpec &source, const FileSpec &destination) override;
Status PutFile(const FileSpec &source, const FileSpec &destination,
uint32_t uid = UINT32_MAX, uint32_t gid = UINT32_MAX) override;
uint32_t GetSdkVersion();
bool GetRemoteOSVersion() override;
Status DisconnectRemote() override;
uint32_t GetDefaultMemoryCacheLineSize() override;
protected:
const char *GetCacheHostname() override;
Status DownloadModuleSlice(const FileSpec &src_file_spec,
const uint64_t src_offset, const uint64_t src_size,
const FileSpec &dst_file_spec) override;
Status DownloadSymbolFile(const lldb::ModuleSP &module_sp,
const FileSpec &dst_file_spec) override;
llvm::StringRef
GetLibdlFunctionDeclarations(lldb_private::Process *process) override;
private:
AdbClient::SyncService *GetSyncService(Status &error);
std::unique_ptr<AdbClient::SyncService> m_adb_sync_svc;
std::string m_device_id;
uint32_t m_sdk_version;
DISALLOW_COPY_AND_ASSIGN(PlatformAndroid);
};
} // namespace platofor_android
} // namespace lldb_private
#endif // liblldb_PlatformAndroid_h_

View File

@@ -0,0 +1,234 @@
//===-- PlatformAndroidRemoteGDBServer.cpp ----------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Other libraries and framework includes
#include "lldb/Host/ConnectionFileDescriptor.h"
#include "lldb/Host/common/TCPSocket.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Status.h"
#include "lldb/Utility/UriParser.h"
#include "PlatformAndroidRemoteGDBServer.h"
#include <sstream>
using namespace lldb;
using namespace lldb_private;
using namespace platform_android;
static const lldb::pid_t g_remote_platform_pid =
0; // Alias for the process id of lldb-platform
static Status ForwardPortWithAdb(
const uint16_t local_port, const uint16_t remote_port,
llvm::StringRef remote_socket_name,
const llvm::Optional<AdbClient::UnixSocketNamespace> &socket_namespace,
std::string &device_id) {
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
AdbClient adb;
auto error = AdbClient::CreateByDeviceID(device_id, adb);
if (error.Fail())
return error;
device_id = adb.GetDeviceID();
if (log)
log->Printf("Connected to Android device \"%s\"", device_id.c_str());
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.str().c_str(), local_port);
if (!socket_namespace)
return Status("Invalid socket namespace");
return adb.SetPortForwarding(local_port, remote_socket_name,
*socket_namespace);
}
static Status DeleteForwardPortWithAdb(uint16_t local_port,
const std::string &device_id) {
AdbClient adb(device_id);
return adb.DeletePortForwarding(local_port);
}
static Status FindUnusedPort(uint16_t &port) {
Status error;
std::unique_ptr<TCPSocket> tcp_socket(new TCPSocket(true, false));
if (error.Fail())
return error;
error = tcp_socket->Listen("127.0.0.1:0", 1);
if (error.Success())
port = tcp_socket->GetLocalPortNumber();
return error;
}
PlatformAndroidRemoteGDBServer::PlatformAndroidRemoteGDBServer() {}
PlatformAndroidRemoteGDBServer::~PlatformAndroidRemoteGDBServer() {
for (const auto &it : m_port_forwards)
DeleteForwardPortWithAdb(it.second, m_device_id);
}
bool PlatformAndroidRemoteGDBServer::LaunchGDBServer(lldb::pid_t &pid,
std::string &connect_url) {
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;
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 PlatformAndroidRemoteGDBServer::KillSpawnedProcess(lldb::pid_t pid) {
DeleteForwardPort(pid);
return m_gdb_client.KillSpawnedProcess(pid);
}
Status PlatformAndroidRemoteGDBServer::ConnectRemote(Args &args) {
m_device_id.clear();
if (args.GetArgumentCount() != 1)
return Status(
"\"platform connect\" takes a single argument: <connect-url>");
int remote_port;
llvm::StringRef scheme, host, path;
const char *url = args.GetArgumentAtIndex(0);
if (!url)
return Status("URL is null.");
if (!UriParser::Parse(url, scheme, host, remote_port, path))
return Status("Invalid URL: %s", url);
if (host != "localhost")
m_device_id = host;
m_socket_namespace.reset();
if (scheme == ConnectionFileDescriptor::UNIX_CONNECT_SCHEME)
m_socket_namespace = AdbClient::UnixSocketNamespaceFileSystem;
else if (scheme == ConnectionFileDescriptor::UNIX_ABSTRACT_CONNECT_SCHEME)
m_socket_namespace = AdbClient::UnixSocketNamespaceAbstract;
std::string connect_url;
auto error =
MakeConnectURL(g_remote_platform_pid, (remote_port < 0) ? 0 : remote_port,
path, connect_url);
if (error.Fail())
return error;
args.ReplaceArgumentAtIndex(0, connect_url);
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
if (log)
log->Printf("Rewritten platform connect URL: %s", connect_url.c_str());
error = PlatformRemoteGDBServer::ConnectRemote(args);
if (error.Fail())
DeleteForwardPort(g_remote_platform_pid);
return error;
}
Status PlatformAndroidRemoteGDBServer::DisconnectRemote() {
DeleteForwardPort(g_remote_platform_pid);
return PlatformRemoteGDBServer::DisconnectRemote();
}
void PlatformAndroidRemoteGDBServer::DeleteForwardPort(lldb::pid_t pid) {
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
auto it = m_port_forwards.find(pid);
if (it == m_port_forwards.end())
return;
const auto port = it->second;
const auto error = DeleteForwardPortWithAdb(port, m_device_id);
if (error.Fail()) {
if (log)
log->Printf("Failed to delete port forwarding (pid=%" PRIu64
", port=%d, device=%s): %s",
pid, port, m_device_id.c_str(), error.AsCString());
}
m_port_forwards.erase(it);
}
Status PlatformAndroidRemoteGDBServer::MakeConnectURL(
const lldb::pid_t pid, const uint16_t remote_port,
llvm::StringRef remote_socket_name, std::string &connect_url) {
static const int kAttempsNum = 5;
Status 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) {
uint16_t local_port = 0;
error = FindUnusedPort(local_port);
if (error.Fail())
return error;
error = ForwardPortWithAdb(local_port, remote_port, remote_socket_name,
m_socket_namespace, 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;
}
lldb::ProcessSP PlatformAndroidRemoteGDBServer::ConnectProcess(
llvm::StringRef connect_url, llvm::StringRef plugin_name,
lldb_private::Debugger &debugger, lldb_private::Target *target,
lldb_private::Status &error) {
// We don't have the pid of the remote gdbserver when it isn't started by us
// but we still want
// to store the list of port forwards we set up in our port forward map.
// Generate a fake pid for
// these cases what won't collide with any other valid pid on android.
static lldb::pid_t s_remote_gdbserver_fake_pid = 0xffffffffffffffffULL;
int remote_port;
llvm::StringRef scheme, host, path;
if (!UriParser::Parse(connect_url, scheme, host, remote_port, path)) {
error.SetErrorStringWithFormat("Invalid URL: %s",
connect_url.str().c_str());
return nullptr;
}
std::string new_connect_url;
error = MakeConnectURL(s_remote_gdbserver_fake_pid--,
(remote_port < 0) ? 0 : remote_port, path,
new_connect_url);
if (error.Fail())
return nullptr;
return PlatformRemoteGDBServer::ConnectProcess(new_connect_url, plugin_name,
debugger, target, error);
}

View File

@@ -0,0 +1,68 @@
//===-- PlatformAndroidRemoteGDBServer.h ------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_PlatformAndroidRemoteGDBServer_h_
#define liblldb_PlatformAndroidRemoteGDBServer_h_
// C Includes
// C++ Includes
#include <map>
#include <utility>
// Other libraries and framework includes
// Project includes
#include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
#include "llvm/ADT/Optional.h"
#include "AdbClient.h"
namespace lldb_private {
namespace platform_android {
class PlatformAndroidRemoteGDBServer
: public platform_gdb_server::PlatformRemoteGDBServer {
public:
PlatformAndroidRemoteGDBServer();
~PlatformAndroidRemoteGDBServer() override;
Status ConnectRemote(Args &args) override;
Status DisconnectRemote() override;
lldb::ProcessSP ConnectProcess(llvm::StringRef connect_url,
llvm::StringRef plugin_name,
lldb_private::Debugger &debugger,
lldb_private::Target *target,
lldb_private::Status &error) override;
protected:
std::string m_device_id;
std::map<lldb::pid_t, uint16_t> m_port_forwards;
llvm::Optional<AdbClient::UnixSocketNamespace> m_socket_namespace;
bool LaunchGDBServer(lldb::pid_t &pid, std::string &connect_url) override;
bool KillSpawnedProcess(lldb::pid_t pid) override;
void DeleteForwardPort(lldb::pid_t pid);
Status MakeConnectURL(const lldb::pid_t pid, const uint16_t remote_port,
llvm::StringRef remote_socket_name,
std::string &connect_url);
private:
DISALLOW_COPY_AND_ASSIGN(PlatformAndroidRemoteGDBServer);
};
} // namespace platform_android
} // namespace lldb_private
#endif // liblldb_PlatformAndroidRemoteGDBServer_h_