diff --git a/lldb/source/Plugins/Platform/Android/AdbClient.cpp b/lldb/source/Plugins/Platform/Android/AdbClient.cpp deleted file mode 100644 index ffccd6d628aade32c5a8e394e4f816c40ed2bffa..0000000000000000000000000000000000000000 --- a/lldb/source/Plugins/Platform/Android/AdbClient.cpp +++ /dev/null @@ -1,653 +0,0 @@ -//===-- AdbClient.cpp -----------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "AdbClient.h" - -#include "llvm/ADT/STLExtras.h" -#include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/StringRef.h" -#include "llvm/Support/FileUtilities.h" - -#include "lldb/Host/ConnectionFileDescriptor.h" -#include "lldb/Host/FileSystem.h" -#include "lldb/Host/PosixApi.h" -#include "lldb/Utility/DataBuffer.h" -#include "lldb/Utility/DataBufferHeap.h" -#include "lldb/Utility/DataEncoder.h" -#include "lldb/Utility/DataExtractor.h" -#include "lldb/Utility/FileSpec.h" -#include "lldb/Utility/StreamString.h" -#include "lldb/Utility/Timeout.h" - -#include - -#include -#include -#include -#include - -// On Windows, transitive dependencies pull in , which defines a -// macro that clashes with a method name. -#ifdef SendMessage -#undef SendMessage -#endif - -using namespace lldb; -using namespace lldb_private; -using namespace lldb_private::platform_android; -using namespace std::chrono; - -namespace { - -const seconds kReadTimeout(20); -const char *kOKAY = "OKAY"; -const char *kFAIL = "FAIL"; -const char *kDATA = "DATA"; -const char *kDONE = "DONE"; - -const char *kSEND = "SEND"; -const char *kRECV = "RECV"; -const char *kSTAT = "STAT"; - -const size_t kSyncPacketLen = 8; -// Maximum size of a filesync DATA packet. -const size_t kMaxPushData = 2 * 1024; -// Default mode for pushed files. -const uint32_t kDefaultMode = 0100770; // S_IFREG | S_IRWXU | S_IRWXG - -const char *kSocketNamespaceAbstract = "localabstract"; -const char *kSocketNamespaceFileSystem = "localfilesystem"; - -Status ReadAllBytes(Connection &conn, void *buffer, size_t size) { - - Status error; - ConnectionStatus status; - char *read_buffer = static_cast(buffer); - - auto now = steady_clock::now(); - const auto deadline = now + kReadTimeout; - size_t total_read_bytes = 0; - while (total_read_bytes < size && now < deadline) { - auto read_bytes = - conn.Read(read_buffer + total_read_bytes, size - total_read_bytes, - duration_cast(deadline - now), status, &error); - if (error.Fail()) - return error; - total_read_bytes += read_bytes; - if (status != eConnectionStatusSuccess) - break; - now = steady_clock::now(); - } - if (total_read_bytes < size) - error = Status( - "Unable to read requested number of bytes. Connection status: %d.", - status); - return error; -} - -} // namespace - -Status AdbClient::CreateByDeviceID(const std::string &device_id, - AdbClient &adb) { - Status error; - std::string android_serial; - if (!device_id.empty()) - android_serial = device_id; - else if (const char *env_serial = std::getenv("ANDROID_SERIAL")) - android_serial = env_serial; - - if (android_serial.empty()) { - DeviceIDList connected_devices; - error = adb.GetDevices(connected_devices); - if (error.Fail()) - return error; - - if (connected_devices.size() != 1) - return Status("Expected a single connected device, got instead %zu - try " - "setting 'ANDROID_SERIAL'", - connected_devices.size()); - adb.SetDeviceID(connected_devices.front()); - } else { - adb.SetDeviceID(android_serial); - } - return error; -} - -AdbClient::AdbClient() {} - -AdbClient::AdbClient(const std::string &device_id) : m_device_id(device_id) {} - -AdbClient::~AdbClient() {} - -void AdbClient::SetDeviceID(const std::string &device_id) { - m_device_id = device_id; -} - -const std::string &AdbClient::GetDeviceID() const { return m_device_id; } - -Status AdbClient::Connect() { - Status error; - m_conn = std::make_unique(); - std::string port = "5037"; - if (const char *env_port = std::getenv("ANDROID_ADB_SERVER_PORT")) { - port = env_port; - } - std::string uri = "connect://127.0.0.1:" + port; - m_conn->Connect(uri.c_str(), &error); - - return error; -} - -Status AdbClient::GetDevices(DeviceIDList &device_list) { - device_list.clear(); - - auto error = SendMessage("host:devices"); - if (error.Fail()) - return error; - - error = ReadResponseStatus(); - if (error.Fail()) - return error; - - std::vector in_buffer; - error = ReadMessage(in_buffer); - - llvm::StringRef response(&in_buffer[0], in_buffer.size()); - llvm::SmallVector devices; - response.split(devices, "\n", -1, false); - - for (const auto &device : devices) - device_list.push_back(std::string(device.split('\t').first)); - - // Force disconnect since ADB closes connection after host:devices response - // is sent. - m_conn.reset(); - return error; -} - -Status AdbClient::SetPortForwarding(const uint16_t local_port, - const uint16_t remote_port) { - char message[48]; - snprintf(message, sizeof(message), "forward:tcp:%d;tcp:%d", local_port, - remote_port); - - const auto error = SendDeviceMessage(message); - if (error.Fail()) - return error; - - return ReadResponseStatus(); -} - -Status -AdbClient::SetPortForwarding(const uint16_t local_port, - llvm::StringRef remote_socket_name, - const UnixSocketNamespace socket_namespace) { - char message[PATH_MAX]; - const char *sock_namespace_str = - (socket_namespace == UnixSocketNamespaceAbstract) - ? kSocketNamespaceAbstract - : kSocketNamespaceFileSystem; - snprintf(message, sizeof(message), "forward:tcp:%d;%s:%s", local_port, - sock_namespace_str, remote_socket_name.str().c_str()); - - const auto error = SendDeviceMessage(message); - if (error.Fail()) - return error; - - return ReadResponseStatus(); -} - -Status AdbClient::DeletePortForwarding(const uint16_t local_port) { - char message[32]; - snprintf(message, sizeof(message), "killforward:tcp:%d", local_port); - - const auto error = SendDeviceMessage(message); - if (error.Fail()) - return error; - - return ReadResponseStatus(); -} - -Status AdbClient::SendMessage(const std::string &packet, const bool reconnect) { - Status error; - if (!m_conn || reconnect) { - error = Connect(); - if (error.Fail()) - return error; - } - - char length_buffer[5]; - snprintf(length_buffer, sizeof(length_buffer), "%04x", - static_cast(packet.size())); - - ConnectionStatus status; - - m_conn->Write(length_buffer, 4, status, &error); - if (error.Fail()) - return error; - - m_conn->Write(packet.c_str(), packet.size(), status, &error); - return error; -} - -Status AdbClient::SendDeviceMessage(const std::string &packet) { - std::ostringstream msg; - msg << "host-serial:" << m_device_id << ":" << packet; - return SendMessage(msg.str()); -} - -Status AdbClient::ReadMessage(std::vector &message) { - message.clear(); - - char buffer[5]; - buffer[4] = 0; - - auto error = ReadAllBytes(buffer, 4); - if (error.Fail()) - return error; - - unsigned int packet_len = 0; - sscanf(buffer, "%x", &packet_len); - - message.resize(packet_len, 0); - error = ReadAllBytes(&message[0], packet_len); - if (error.Fail()) - message.clear(); - - return error; -} - -Status AdbClient::ReadMessageStream(std::vector &message, - milliseconds timeout) { - auto start = steady_clock::now(); - message.clear(); - - Status error; - lldb::ConnectionStatus status = lldb::eConnectionStatusSuccess; - char buffer[1024]; - while (error.Success() && status == lldb::eConnectionStatusSuccess) { - auto end = steady_clock::now(); - auto elapsed = end - start; - if (elapsed >= timeout) - return Status("Timed out"); - - size_t n = m_conn->Read(buffer, sizeof(buffer), - duration_cast(timeout - elapsed), - status, &error); - if (n > 0) - message.insert(message.end(), &buffer[0], &buffer[n]); - } - return error; -} - -Status AdbClient::ReadResponseStatus() { - char response_id[5]; - - static const size_t packet_len = 4; - response_id[packet_len] = 0; - - auto error = ReadAllBytes(response_id, packet_len); - if (error.Fail()) - return error; - - if (strncmp(response_id, kOKAY, packet_len) != 0) - return GetResponseError(response_id); - - return error; -} - -Status AdbClient::GetResponseError(const char *response_id) { - if (strcmp(response_id, kFAIL) != 0) - return Status("Got unexpected response id from adb: \"%s\"", response_id); - - std::vector error_message; - auto error = ReadMessage(error_message); - if (error.Success()) - error.SetErrorString( - std::string(&error_message[0], error_message.size()).c_str()); - - return error; -} - -Status AdbClient::SwitchDeviceTransport() { - std::ostringstream msg; - msg << "host:transport:" << m_device_id; - - auto error = SendMessage(msg.str()); - if (error.Fail()) - return error; - - return ReadResponseStatus(); -} - -Status AdbClient::StartSync() { - auto error = SwitchDeviceTransport(); - if (error.Fail()) - return Status("Failed to switch to device transport: %s", - error.AsCString()); - - error = Sync(); - if (error.Fail()) - return Status("Sync failed: %s", error.AsCString()); - - return error; -} - -Status AdbClient::Sync() { - auto error = SendMessage("sync:", false); - if (error.Fail()) - return error; - - return ReadResponseStatus(); -} - -Status AdbClient::ReadAllBytes(void *buffer, size_t size) { - return ::ReadAllBytes(*m_conn, buffer, size); -} - -Status AdbClient::internalShell(const char *command, milliseconds timeout, - std::vector &output_buf) { - output_buf.clear(); - - auto error = SwitchDeviceTransport(); - if (error.Fail()) - return Status("Failed to switch to device transport: %s", - error.AsCString()); - - StreamString adb_command; - adb_command.Printf("shell:%s", command); - error = SendMessage(std::string(adb_command.GetString()), false); - if (error.Fail()) - return error; - - error = ReadResponseStatus(); - if (error.Fail()) - return error; - - error = ReadMessageStream(output_buf, timeout); - if (error.Fail()) - return error; - - // ADB doesn't propagate return code of shell execution - if - // output starts with /system/bin/sh: most likely command failed. - static const char *kShellPrefix = "/system/bin/sh:"; - if (output_buf.size() > strlen(kShellPrefix)) { - if (!memcmp(&output_buf[0], kShellPrefix, strlen(kShellPrefix))) - return Status("Shell command %s failed: %s", command, - std::string(output_buf.begin(), output_buf.end()).c_str()); - } - - return Status(); -} - -Status AdbClient::Shell(const char *command, milliseconds timeout, - std::string *output) { - std::vector output_buffer; - auto error = internalShell(command, timeout, output_buffer); - if (error.Fail()) - return error; - - if (output) - output->assign(output_buffer.begin(), output_buffer.end()); - return error; -} - -Status AdbClient::ShellToFile(const char *command, milliseconds timeout, - const FileSpec &output_file_spec) { - std::vector output_buffer; - auto error = internalShell(command, timeout, output_buffer); - if (error.Fail()) - return error; - - const auto output_filename = output_file_spec.GetPath(); - std::error_code EC; - llvm::raw_fd_ostream dst(output_filename, EC, llvm::sys::fs::OF_None); - if (EC) - return Status("Unable to open local file %s", output_filename.c_str()); - - dst.write(&output_buffer[0], output_buffer.size()); - dst.close(); - if (dst.has_error()) - return Status("Failed to write file %s", output_filename.c_str()); - return Status(); -} - -std::unique_ptr -AdbClient::GetSyncService(Status &error) { - std::unique_ptr sync_service; - error = StartSync(); - if (error.Success()) - sync_service.reset(new SyncService(std::move(m_conn))); - - return sync_service; -} - -Status AdbClient::SyncService::internalPullFile(const FileSpec &remote_file, - const FileSpec &local_file) { - const auto local_file_path = local_file.GetPath(); - llvm::FileRemover local_file_remover(local_file_path); - - std::error_code EC; - llvm::raw_fd_ostream dst(local_file_path, EC, llvm::sys::fs::OF_None); - if (EC) - return Status("Unable to open local file %s", local_file_path.c_str()); - - const auto remote_file_path = remote_file.GetPath(false); - auto error = SendSyncRequest(kRECV, remote_file_path.length(), - remote_file_path.c_str()); - if (error.Fail()) - return error; - - std::vector chunk; - bool eof = false; - while (!eof) { - error = PullFileChunk(chunk, eof); - if (error.Fail()) - return error; - if (!eof) - dst.write(&chunk[0], chunk.size()); - } - dst.close(); - if (dst.has_error()) - return Status("Failed to write file %s", local_file_path.c_str()); - - local_file_remover.releaseFile(); - return error; -} - -Status AdbClient::SyncService::internalPushFile(const FileSpec &local_file, - const FileSpec &remote_file) { - const auto local_file_path(local_file.GetPath()); - std::ifstream src(local_file_path.c_str(), std::ios::in | std::ios::binary); - if (!src.is_open()) - return Status("Unable to open local file %s", local_file_path.c_str()); - - std::stringstream file_description; - file_description << remote_file.GetPath(false).c_str() << "," << kDefaultMode; - std::string file_description_str = file_description.str(); - auto error = SendSyncRequest(kSEND, file_description_str.length(), - file_description_str.c_str()); - if (error.Fail()) - return error; - - char chunk[kMaxPushData]; - while (!src.eof() && !src.read(chunk, kMaxPushData).bad()) { - size_t chunk_size = src.gcount(); - error = SendSyncRequest(kDATA, chunk_size, chunk); - if (error.Fail()) - return Status("Failed to send file chunk: %s", error.AsCString()); - } - error = SendSyncRequest( - kDONE, llvm::sys::toTimeT(FileSystem::Instance().GetModificationTime(local_file)), - nullptr); - if (error.Fail()) - return error; - - std::string response_id; - uint32_t data_len; - error = ReadSyncHeader(response_id, data_len); - if (error.Fail()) - return Status("Failed to read DONE response: %s", error.AsCString()); - if (response_id == kFAIL) { - std::string error_message(data_len, 0); - error = ReadAllBytes(&error_message[0], data_len); - if (error.Fail()) - return Status("Failed to read DONE error message: %s", error.AsCString()); - return Status("Failed to push file: %s", error_message.c_str()); - } else if (response_id != kOKAY) - return Status("Got unexpected DONE response: %s", response_id.c_str()); - - // If there was an error reading the source file, finish the adb file - // transfer first so that adb isn't expecting any more data. - if (src.bad()) - return Status("Failed read on %s", local_file_path.c_str()); - return error; -} - -Status AdbClient::SyncService::internalStat(const FileSpec &remote_file, - uint32_t &mode, uint32_t &size, - uint32_t &mtime) { - const std::string remote_file_path(remote_file.GetPath(false)); - auto error = SendSyncRequest(kSTAT, remote_file_path.length(), - remote_file_path.c_str()); - if (error.Fail()) - return Status("Failed to send request: %s", error.AsCString()); - - static const size_t stat_len = strlen(kSTAT); - static const size_t response_len = stat_len + (sizeof(uint32_t) * 3); - - std::vector buffer(response_len); - error = ReadAllBytes(&buffer[0], buffer.size()); - if (error.Fail()) - return Status("Failed to read response: %s", error.AsCString()); - - DataExtractor extractor(&buffer[0], buffer.size(), eByteOrderLittle, - sizeof(void *)); - offset_t offset = 0; - - const void *command = extractor.GetData(&offset, stat_len); - if (!command) - return Status("Failed to get response command"); - const char *command_str = static_cast(command); - if (strncmp(command_str, kSTAT, stat_len)) - return Status("Got invalid stat command: %s", command_str); - - mode = extractor.GetU32(&offset); - size = extractor.GetU32(&offset); - mtime = extractor.GetU32(&offset); - return Status(); -} - -Status AdbClient::SyncService::PullFile(const FileSpec &remote_file, - const FileSpec &local_file) { - return executeCommand([this, &remote_file, &local_file]() { - return internalPullFile(remote_file, local_file); - }); -} - -Status AdbClient::SyncService::PushFile(const FileSpec &local_file, - const FileSpec &remote_file) { - return executeCommand([this, &local_file, &remote_file]() { - return internalPushFile(local_file, remote_file); - }); -} - -Status AdbClient::SyncService::Stat(const FileSpec &remote_file, uint32_t &mode, - uint32_t &size, uint32_t &mtime) { - return executeCommand([this, &remote_file, &mode, &size, &mtime]() { - return internalStat(remote_file, mode, size, mtime); - }); -} - -bool AdbClient::SyncService::IsConnected() const { - return m_conn && m_conn->IsConnected(); -} - -AdbClient::SyncService::SyncService(std::unique_ptr &&conn) - : m_conn(std::move(conn)) {} - -Status -AdbClient::SyncService::executeCommand(const std::function &cmd) { - if (!m_conn) - return Status("SyncService is disconnected"); - - const auto error = cmd(); - if (error.Fail()) - m_conn.reset(); - - return error; -} - -AdbClient::SyncService::~SyncService() {} - -Status AdbClient::SyncService::SendSyncRequest(const char *request_id, - const uint32_t data_len, - const void *data) { - const DataBufferSP data_sp(new DataBufferHeap(kSyncPacketLen, 0)); - DataEncoder encoder(data_sp, eByteOrderLittle, sizeof(void *)); - auto offset = encoder.PutData(0, request_id, strlen(request_id)); - encoder.PutUnsigned(offset, 4, data_len); - - Status error; - ConnectionStatus status; - m_conn->Write(data_sp->GetBytes(), kSyncPacketLen, status, &error); - if (error.Fail()) - return error; - - if (data) - m_conn->Write(data, data_len, status, &error); - return error; -} - -Status AdbClient::SyncService::ReadSyncHeader(std::string &response_id, - uint32_t &data_len) { - char buffer[kSyncPacketLen]; - - auto error = ReadAllBytes(buffer, kSyncPacketLen); - if (error.Success()) { - response_id.assign(&buffer[0], 4); - DataExtractor extractor(&buffer[4], 4, eByteOrderLittle, sizeof(void *)); - offset_t offset = 0; - data_len = extractor.GetU32(&offset); - } - - return error; -} - -Status AdbClient::SyncService::PullFileChunk(std::vector &buffer, - bool &eof) { - buffer.clear(); - - std::string response_id; - uint32_t data_len; - auto error = ReadSyncHeader(response_id, data_len); - if (error.Fail()) - return error; - - if (response_id == kDATA) { - buffer.resize(data_len, 0); - error = ReadAllBytes(&buffer[0], data_len); - if (error.Fail()) - buffer.clear(); - } else if (response_id == kDONE) { - eof = true; - } else if (response_id == kFAIL) { - std::string error_message(data_len, 0); - error = ReadAllBytes(&error_message[0], data_len); - if (error.Fail()) - return Status("Failed to read pull error message: %s", error.AsCString()); - return Status("Failed to pull file: %s", error_message.c_str()); - } else - return Status("Pull failed with unknown response: %s", response_id.c_str()); - - return Status(); -} - -Status AdbClient::SyncService::ReadAllBytes(void *buffer, size_t size) { - return ::ReadAllBytes(*m_conn, buffer, size); -} diff --git a/lldb/source/Plugins/Platform/Android/AdbClient.h b/lldb/source/Plugins/Platform/Android/AdbClient.h deleted file mode 100644 index 04ad3819f57f8dac5afbfe6dca4cdf281337e9db..0000000000000000000000000000000000000000 --- a/lldb/source/Plugins/Platform/Android/AdbClient.h +++ /dev/null @@ -1,140 +0,0 @@ -//===-- AdbClient.h ---------------------------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_ADBCLIENT_H -#define LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_ADBCLIENT_H - -#include "lldb/Utility/Status.h" -#include -#include -#include -#include -#include -#include - -namespace lldb_private { - -class FileSpec; - -namespace platform_android { - -class AdbClient { -public: - enum UnixSocketNamespace { - UnixSocketNamespaceAbstract, - UnixSocketNamespaceFileSystem, - }; - - using DeviceIDList = std::list; - - 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 &&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 &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 &cmd); - - std::unique_ptr 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 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 &message); - - Status ReadMessageStream(std::vector &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 &output_buf); - - Status ReadAllBytes(void *buffer, size_t size); - - std::string m_device_id; - std::unique_ptr m_conn; -}; - -} // namespace platform_android -} // namespace lldb_private - -#endif // LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_ADBCLIENT_H diff --git a/lldb/source/Plugins/Platform/Android/CMakeLists.txt b/lldb/source/Plugins/Platform/Android/CMakeLists.txt deleted file mode 100644 index 5abb51a0b94acc79bff7b8e0c902d61f45b7f71c..0000000000000000000000000000000000000000 --- a/lldb/source/Plugins/Platform/Android/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -add_lldb_library(lldbPluginPlatformAndroid PLUGIN - AdbClient.cpp - PlatformAndroid.cpp - PlatformAndroidRemoteGDBServer.cpp - - LINK_LIBS - lldbCore - lldbHost - lldbPluginPlatformLinux - lldbPluginPlatformGDB - LINK_COMPONENTS - Support - ) diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp b/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp deleted file mode 100644 index 2cd4abbf14ad8c6478b5f7a46b52bb85f1470e15..0000000000000000000000000000000000000000 --- a/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp +++ /dev/null @@ -1,393 +0,0 @@ -//===-- PlatformAndroid.cpp -----------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "lldb/Core/Module.h" -#include "lldb/Core/PluginManager.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/Scalar.h" -#include "lldb/Utility/UriParser.h" - -#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; - -LLDB_PLUGIN_DEFINE(PlatformAndroid) - -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 = ""; - - const char *triple_cstr = - arch ? arch->GetTriple().getTriple().c_str() : ""; - - LLDB_LOGF(log, "PlatformAndroid::%s(force=%s, arch={%s,%s})", __FUNCTION__, - force ? "true" : "false", arch_name, triple_cstr); - } - - bool create = force; - if (!create && 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 if - // "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.getEnvironment()) { - 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::EnvironmentType::UnknownEnvironment: - create = !arch->TripleEnvironmentWasSpecified(); - break; -#endif - default: - create = false; - break; - } - } - } - - if (create) { - LLDB_LOGF(log, "PlatformAndroid::%s() creating remote-android platform", - __FUNCTION__); - return PlatformSP(new PlatformAndroid(false)); - } - - LLDB_LOGF( - log, "PlatformAndroid::%s() aborting creation of remote-android platform", - __FUNCTION__); - - return PlatformSP(); -} - -PlatformAndroid::PlatformAndroid(bool is_host) - : PlatformLinux(is_host), m_sdk_version(0) {} - -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 = std::string(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), FileSpec::Style::posix); - 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)); - LLDB_LOGF(log, "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), FileSpec::Style::posix); - 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); - LLDB_LOGF(log, "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 != ".oat" && extension != ".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> - 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()) - LLDB_LOGF(log, "Failed to remove temp directory: %s", error.AsCString()); - }); - - FileSpec symfile_platform_filespec(tmpdir); - 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_os_version = llvm::VersionTuple(GetSdkVersion()); - return !m_os_version.empty(); -} - -llvm::StringRef -PlatformAndroid::GetLibdlFunctionDeclarations(lldb_private::Process *process) { - SymbolContextList matching_symbols; - std::vector dl_open_names = { "__dl_dlopen", "dlopen" }; - const char *dl_open_name = nullptr; - Target &target = process->GetTarget(); - for (auto name: dl_open_names) { - target.GetImages().FindFunctionSymbols( - ConstString(name), eFunctionNameTypeFull, matching_symbols); - if (matching_symbols.GetSize()) { - 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; -} diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroid.h b/lldb/source/Plugins/Platform/Android/PlatformAndroid.h deleted file mode 100644 index 990e1d099404b4e67822468a50c40176d88ac815..0000000000000000000000000000000000000000 --- a/lldb/source/Plugins/Platform/Android/PlatformAndroid.h +++ /dev/null @@ -1,82 +0,0 @@ -//===-- PlatformAndroid.h ---------------------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_PLATFORMANDROID_H -#define LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_PLATFORMANDROID_H - -#include -#include - -#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); - - 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 m_adb_sync_svc; - std::string m_device_id; - uint32_t m_sdk_version; -}; - -} // namespace platofor_android -} // namespace lldb_private - -#endif // LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_PLATFORMANDROID_H diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp b/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp deleted file mode 100644 index 6dd5306a93e822cafc11a1c8c05c1433576074e5..0000000000000000000000000000000000000000 --- a/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp +++ /dev/null @@ -1,227 +0,0 @@ -//===-- PlatformAndroidRemoteGDBServer.cpp --------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#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 - -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 &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(); - LLDB_LOGF(log, "Connected to Android device \"%s\"", device_id.c_str()); - - if (remote_port != 0) { - LLDB_LOGF(log, "Forwarding remote TCP port %d to local TCP port %d", - remote_port, local_port); - return adb.SetPortForwarding(local_port, remote_port); - } - - LLDB_LOGF(log, "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 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) - LLDB_LOGF(log, "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: "); - - 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 = std::string(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)); - LLDB_LOGF(log, "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()) { - LLDB_LOGF(log, - "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://127.0.0.1:" << 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); -} diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h b/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h deleted file mode 100644 index c3e0d0f66e8dc08d0eea750278b267ba735a5aea..0000000000000000000000000000000000000000 --- a/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h +++ /dev/null @@ -1,66 +0,0 @@ -//===-- PlatformAndroidRemoteGDBServer.h ------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_PLATFORMANDROIDREMOTEGDBSERVER_H -#define LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_PLATFORMANDROIDREMOTEGDBSERVER_H - -#include -#include - -#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 m_port_forwards; - llvm::Optional 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: - PlatformAndroidRemoteGDBServer(const PlatformAndroidRemoteGDBServer &) = - delete; - const PlatformAndroidRemoteGDBServer & - operator=(const PlatformAndroidRemoteGDBServer &) = delete; -}; - -} // namespace platform_android -} // namespace lldb_private - -#endif // LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_PLATFORMANDROIDREMOTEGDBSERVER_H diff --git a/lldb/source/Plugins/Platform/CMakeLists.txt b/lldb/source/Plugins/Platform/CMakeLists.txt index 8c532955a9ddc2272f2232792f2695914b8853f0..0b143968858d61bc0261fd9cf5c8f4042fb91e8b 100644 --- a/lldb/source/Plugins/Platform/CMakeLists.txt +++ b/lldb/source/Plugins/Platform/CMakeLists.txt @@ -14,5 +14,4 @@ add_subdirectory(POSIX) add_subdirectory(gdb-server) -add_subdirectory(Android) add_subdirectory(OHOS) diff --git a/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp b/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp index 1b54c2cce2253389e29a7fd2f542c5ebcb1bd77a..e1447f935cae76e7297a2c95bd95edf3ec2727fc 100644 --- a/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp +++ b/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp @@ -80,7 +80,8 @@ static Status DeleteForwardPortWithHdc(std::pair remote_s if (log) log->Printf("Delete port forwarding %d -> %s, device=%s", local_port, remote_socket_name.c_str(), device_id.c_str()); - + if (!socket_namespace) + return Status("Invalid socket namespace"); HdcClient hdc(device_id); return hdc.DeletePortForwarding(local_port, remote_socket_name, *socket_namespace); } @@ -108,10 +109,10 @@ static Status FindUnusedPort(uint16_t &port) { PlatformOHOSRemoteGDBServer::PlatformOHOSRemoteGDBServer() {} PlatformOHOSRemoteGDBServer::~PlatformOHOSRemoteGDBServer() { - for (const auto &it : m_port_forwards){ + for (const auto &it : m_port_forwards) { DeleteForwardPortWithHdc(it.second, m_device_id); } - for (const auto &it_socket : m_remote_socket_name){ + for (const auto &it_socket : m_remote_socket_name) { DeleteForwardPortWithHdc(it_socket.second, m_socket_namespace, m_device_id); } } @@ -193,7 +194,7 @@ void PlatformOHOSRemoteGDBServer::DeleteForwardPort(lldb::pid_t pid) { auto it = m_port_forwards.find(pid); auto it_socket = m_remote_socket_name.find(pid); - if (it != m_port_forwards.end() && it->second.second != 0){ + if (it != m_port_forwards.end() && it->second.second != 0) { const auto error = DeleteForwardPortWithHdc(it->second, m_device_id); if (error.Fail()) { if (log) @@ -205,7 +206,7 @@ void PlatformOHOSRemoteGDBServer::DeleteForwardPort(lldb::pid_t pid) { m_port_forwards.erase(it); } - if(it_socket != m_remote_socket_name.end()){ + if(it_socket != m_remote_socket_name.end()) { const auto error_Socket = DeleteForwardPortWithHdc(it_socket->second, m_socket_namespace, m_device_id); if (error_Socket.Fail()) { if (log)