[Lldb-commits] [lldb] Fix connecting via abstract socket (PR #136466)
via lldb-commits
lldb-commits at lists.llvm.org
Sat Apr 19 18:20:59 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Emre Kultursay (emrekultursay)
<details>
<summary>Changes</summary>
Commits 82ee31f and 2e89312 added socket sharing, but only for unix domain sockets. That broke Android, which uses unix-abstract sockets.
---
Full diff: https://github.com/llvm/llvm-project/pull/136466.diff
5 Files Affected:
- (modified) lldb/include/lldb/Host/linux/AbstractSocket.h (+1)
- (modified) lldb/include/lldb/Host/posix/DomainSocket.h (+1)
- (modified) lldb/source/Host/linux/AbstractSocket.cpp (+3)
- (modified) lldb/source/Host/posix/DomainSocket.cpp (+6)
- (modified) lldb/tools/lldb-server/lldb-platform.cpp (+36-4)
``````````diff
diff --git a/lldb/include/lldb/Host/linux/AbstractSocket.h b/lldb/include/lldb/Host/linux/AbstractSocket.h
index accfd01457a5e..c6a0e2f8af63b 100644
--- a/lldb/include/lldb/Host/linux/AbstractSocket.h
+++ b/lldb/include/lldb/Host/linux/AbstractSocket.h
@@ -15,6 +15,7 @@ namespace lldb_private {
class AbstractSocket : public DomainSocket {
public:
AbstractSocket();
+ AbstractSocket(NativeSocket socket, bool should_close);
protected:
size_t GetNameOffset() const override;
diff --git a/lldb/include/lldb/Host/posix/DomainSocket.h b/lldb/include/lldb/Host/posix/DomainSocket.h
index 3dbe6206da2c5..562c011ee73e6 100644
--- a/lldb/include/lldb/Host/posix/DomainSocket.h
+++ b/lldb/include/lldb/Host/posix/DomainSocket.h
@@ -33,6 +33,7 @@ class DomainSocket : public Socket {
protected:
DomainSocket(SocketProtocol protocol);
+ DomainSocket(SocketProtocol protocol, NativeSocket socket, bool should_close);
virtual size_t GetNameOffset() const;
virtual void DeleteSocketFile(llvm::StringRef name);
diff --git a/lldb/source/Host/linux/AbstractSocket.cpp b/lldb/source/Host/linux/AbstractSocket.cpp
index 8393a80e86e72..681aa2d1ebc72 100644
--- a/lldb/source/Host/linux/AbstractSocket.cpp
+++ b/lldb/source/Host/linux/AbstractSocket.cpp
@@ -15,6 +15,9 @@ using namespace lldb_private;
AbstractSocket::AbstractSocket() : DomainSocket(ProtocolUnixAbstract) {}
+AbstractSocket::AbstractSocket(NativeSocket socket, bool should_close)
+ : DomainSocket(ProtocolUnixAbstract, socket, should_close) {}
+
size_t AbstractSocket::GetNameOffset() const { return 1; }
void AbstractSocket::DeleteSocketFile(llvm::StringRef name) {}
diff --git a/lldb/source/Host/posix/DomainSocket.cpp b/lldb/source/Host/posix/DomainSocket.cpp
index 6c490cdda47ed..9b1e4f71bba2a 100644
--- a/lldb/source/Host/posix/DomainSocket.cpp
+++ b/lldb/source/Host/posix/DomainSocket.cpp
@@ -67,6 +67,12 @@ DomainSocket::DomainSocket(NativeSocket socket,
m_socket = socket;
}
+DomainSocket::DomainSocket(SocketProtocol protocol, NativeSocket socket,
+ bool should_close)
+ : Socket(protocol, should_close) {
+ m_socket = socket;
+}
+
Status DomainSocket::Connect(llvm::StringRef name) {
sockaddr_un saddr_un;
socklen_t saddr_un_len;
diff --git a/lldb/tools/lldb-server/lldb-platform.cpp b/lldb/tools/lldb-server/lldb-platform.cpp
index 23d36ffb4cb66..1f12f0902b954 100644
--- a/lldb/tools/lldb-server/lldb-platform.cpp
+++ b/lldb/tools/lldb-server/lldb-platform.cpp
@@ -39,11 +39,19 @@
#if LLDB_ENABLE_POSIX
#include "lldb/Host/posix/DomainSocket.h"
#endif
+#ifdef __linux__
+#include "lldb/Host/linux/AbstractSocket.h"
+#endif
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Status.h"
#include "lldb/Utility/UriParser.h"
+#if LLDB_ENABLE_POSIX
+#include <sys/socket.h>
+#include <sys/un.h>
+#endif
+
using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::lldb_server;
@@ -455,14 +463,28 @@ int main_platform(int argc, char *argv[]) {
lldb_private::Args inferior_arguments;
inferior_arguments.SetArguments(argc, const_cast<const char **>(argv));
+ Log *log = GetLog(LLDBLog::Platform);
Socket::SocketProtocol protocol = Socket::ProtocolUnixDomain;
if (fd != SharedSocket::kInvalidFD) {
// Child process will handle the connection and exit.
- if (gdbserver_port)
+ if (gdbserver_port) {
protocol = Socket::ProtocolTcp;
+ } else {
+#ifdef LLDB_ENABLE_POSIX
+ // Check if fd represents domain socket or abstract socket.
+ struct sockaddr_un addr;
+ socklen_t addr_len = sizeof(addr);
+ if (getsockname(fd, (struct sockaddr*)&addr, &addr_len) == -1) {
+ LLDB_LOGF(log, "lldb-platform child: not a socket or error occurred");
+ return socket_error;
+ }
- Log *log = GetLog(LLDBLog::Platform);
+ if (addr.sun_family == AF_UNIX && addr.sun_path[0] == '\0') {
+ protocol = Socket::ProtocolUnixAbstract;
+ }
+#endif
+ }
NativeSocket sockfd;
error = SharedSocket::GetNativeSocket(fd, sockfd);
@@ -473,17 +495,27 @@ int main_platform(int argc, char *argv[]) {
GDBRemoteCommunicationServerPlatform platform(protocol, gdbserver_port);
Socket *socket;
- if (protocol == Socket::ProtocolTcp)
+ if (protocol == Socket::ProtocolTcp) {
socket = new TCPSocket(sockfd, /*should_close=*/true);
- else {
+ } else if (protocol == Socket::ProtocolUnixDomain) {
#if LLDB_ENABLE_POSIX
socket = new DomainSocket(sockfd, /*should_close=*/true);
#else
WithColor::error() << "lldb-platform child: Unix domain sockets are not "
"supported on this platform.";
return socket_error;
+#endif
+ } else {
+#ifdef __linux__
+ socket = new AbstractSocket(sockfd, /*should_close=*/true);
+#else
+ WithColor::error()
+ << "lldb-platform child: Abstract domain sockets are not "
+ "supported on this platform.";
+ return socket_error;
#endif
}
+
platform.SetConnection(
std::unique_ptr<Connection>(new ConnectionFileDescriptor(socket)));
client_handle(platform, inferior_arguments);
``````````
</details>
https://github.com/llvm/llvm-project/pull/136466
More information about the lldb-commits
mailing list