From d5b34228cd2c81d6e0204856685e770a2496041d Mon Sep 17 00:00:00 2001 From: xwx1135370 Date: Tue, 5 Jul 2022 12:46:55 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90OHOS=E3=80=91=E3=80=90lldb=E3=80=91fpo?= =?UTF-8?q?rt=20rm=20message=20sent=20by=20lldb=20to=20hdc=20does=20not=20?= =?UTF-8?q?match=20fport?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xwx1135370 --- .../Plugins/Platform/OHOS/HdcClient.cpp | 22 ++++++- lldb/source/Plugins/Platform/OHOS/HdcClient.h | 3 + .../OHOS/PlatformOHOSRemoteGDBServer.cpp | 63 ++++++++++++++----- .../OHOS/PlatformOHOSRemoteGDBServer.h | 1 + 4 files changed, 74 insertions(+), 15 deletions(-) diff --git a/lldb/source/Plugins/Platform/OHOS/HdcClient.cpp b/lldb/source/Plugins/Platform/OHOS/HdcClient.cpp index 97fa08fb44da..6807d24cf317 100644 --- a/lldb/source/Plugins/Platform/OHOS/HdcClient.cpp +++ b/lldb/source/Plugins/Platform/OHOS/HdcClient.cpp @@ -235,7 +235,27 @@ Status HdcClient::DeletePortForwarding(std::pair fwd) { if (error.Fail()) return error; - return ReadResponseStatus(nullptr); + return ReadResponseStatus("Remove forward ruler success"); +} + +Status HdcClient::DeletePortForwarding(const uint16_t local_port, + const std::string remote_socket_name, + const UnixSocketNamespace socket_namespace) { + const char *sock_namespace_str = + (socket_namespace == UnixSocketNamespaceAbstract) + ? kSocketNamespaceAbstract + : kSocketNamespaceFileSystem; + char message[PATH_MAX] = ""; + + snprintf(message, sizeof(message), "fport rm tcp:%d %s:%s", local_port, + sock_namespace_str, remote_socket_name.c_str()); + + const auto error = SendMessage(message); + if (error.Fail()){ + return error; + } + + return ReadResponseStatus("Remove forward ruler success"); } Status HdcClient::TransferFile(const char *direction, const FileSpec &src, diff --git a/lldb/source/Plugins/Platform/OHOS/HdcClient.h b/lldb/source/Plugins/Platform/OHOS/HdcClient.h index 64a5a324325a..39b6569b447a 100644 --- a/lldb/source/Plugins/Platform/OHOS/HdcClient.h +++ b/lldb/source/Plugins/Platform/OHOS/HdcClient.h @@ -51,6 +51,9 @@ public: const UnixSocketNamespace socket_namespace); Status DeletePortForwarding(std::pair fwd); + + Status DeletePortForwarding(const uint16_t local_port, const std::string remote_socket_name, + const UnixSocketNamespace socket_namespace); Status RecvFile(const FileSpec &src, const FileSpec &dst); diff --git a/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp b/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp index 62dc0639afef..1b54c2cce225 100644 --- a/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp +++ b/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp @@ -70,6 +70,21 @@ static Status DeleteForwardPortWithHdc(std::pair ports, return hdc.DeletePortForwarding(ports); } +static Status DeleteForwardPortWithHdc(std::pair remote_socket, + const llvm::Optional &socket_namespace, + const std::string &device_id) { + + Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); + uint16_t local_port = remote_socket.first; + std::string remote_socket_name = remote_socket.second; + if (log) + log->Printf("Delete port forwarding %d -> %s, device=%s", local_port, + remote_socket_name.c_str(), device_id.c_str()); + + HdcClient hdc(device_id); + return hdc.DeletePortForwarding(local_port, remote_socket_name, *socket_namespace); +} + static Status FindUnusedPort(uint16_t &port) { Status error; @@ -93,8 +108,12 @@ 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){ + DeleteForwardPortWithHdc(it_socket.second, m_socket_namespace, m_device_id); + } } bool PlatformOHOSRemoteGDBServer::LaunchGDBServer(lldb::pid_t &pid, @@ -173,19 +192,30 @@ void PlatformOHOSRemoteGDBServer::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 = DeleteForwardPortWithHdc(port, m_device_id); - if (error.Fail()) { - if (log) - log->Printf("Failed to delete port forwarding (pid=%" PRIu64 - ", fwd=(%d -> %d), device=%s): %s", - pid, port.first, port.second, m_device_id.c_str(), - error.AsCString()); + auto it_socket = m_remote_socket_name.find(pid); + if (it != m_port_forwards.end() && it->second.second != 0){ + const auto error = DeleteForwardPortWithHdc(it->second, m_device_id); + if (error.Fail()) { + if (log) + log->Printf("Failed to delete port forwarding (pid=%" PRIu64 + ", fwd=(%d -> %d), device=%s): %s", + pid, it->second.first, it->second.second, m_device_id.c_str(), + error.AsCString()); + } + m_port_forwards.erase(it); + } + + 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) + log->Printf("Failed to delete port forwarding (pid=%" PRIu64 + ", fwd=(%d->%s)device=%s): %s", pid, it_socket->second.first, it_socket->second.second.c_str(), m_device_id.c_str(),error_Socket.AsCString()); + } + m_remote_socket_name.erase(it_socket); } - m_port_forwards.erase(it); + + return; } Status PlatformOHOSRemoteGDBServer::MakeConnectURL( @@ -206,7 +236,12 @@ Status PlatformOHOSRemoteGDBServer::MakeConnectURL( error = ForwardPortWithHdc(local_port, remote_port, remote_socket_name, m_socket_namespace, m_device_id); if (error.Success()) { - m_port_forwards[pid] = {local_port, remote_port}; + if (remote_port != 0){ + m_port_forwards[pid] = {local_port, remote_port}; + } + else{ + m_remote_socket_name[pid] ={local_port, remote_socket_name.str()}; + } std::ostringstream url_str; url_str << "connect://localhost:" << local_port; connect_url = url_str.str(); diff --git a/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.h b/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.h index f9818589275d..680ddc1e733c 100644 --- a/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.h +++ b/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.h @@ -41,6 +41,7 @@ public: protected: std::string m_device_id; std::map> m_port_forwards; + std::map> m_remote_socket_name; llvm::Optional m_socket_namespace; bool LaunchGDBServer(lldb::pid_t &pid, std::string &connect_url) override; -- Gitee