Skip to content

Commit 2aedf97

Browse files
committed
Fix connecting via abstract socket
Commits 82ee31f and 2e89312 added socket sharing, but only for unix domain sockets. That broke Android, which uses unix-abstract sockets.
1 parent 3215fd7 commit 2aedf97

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

lldb/include/lldb/Host/linux/AbstractSocket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace lldb_private {
1515
class AbstractSocket : public DomainSocket {
1616
public:
1717
AbstractSocket();
18+
AbstractSocket(NativeSocket socket, bool should_close);
1819

1920
protected:
2021
size_t GetNameOffset() const override;

lldb/include/lldb/Host/posix/DomainSocket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class DomainSocket : public Socket {
3333

3434
protected:
3535
DomainSocket(SocketProtocol protocol);
36+
DomainSocket(SocketProtocol protocol, NativeSocket socket, bool should_close);
3637

3738
virtual size_t GetNameOffset() const;
3839
virtual void DeleteSocketFile(llvm::StringRef name);

lldb/source/Host/linux/AbstractSocket.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ using namespace lldb_private;
1515

1616
AbstractSocket::AbstractSocket() : DomainSocket(ProtocolUnixAbstract) {}
1717

18+
AbstractSocket::AbstractSocket(NativeSocket socket, bool should_close)
19+
: DomainSocket(ProtocolUnixAbstract, socket, should_close) {}
20+
1821
size_t AbstractSocket::GetNameOffset() const { return 1; }
1922

2023
void AbstractSocket::DeleteSocketFile(llvm::StringRef name) {}

lldb/source/Host/posix/DomainSocket.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ DomainSocket::DomainSocket(NativeSocket socket,
6767
m_socket = socket;
6868
}
6969

70+
DomainSocket::DomainSocket(SocketProtocol protocol, NativeSocket socket,
71+
bool should_close)
72+
: Socket(protocol, should_close) {
73+
m_socket = socket;
74+
}
75+
7076
Status DomainSocket::Connect(llvm::StringRef name) {
7177
sockaddr_un saddr_un;
7278
socklen_t saddr_un_len;

lldb/tools/lldb-server/lldb-platform.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,19 @@
3939
#if LLDB_ENABLE_POSIX
4040
#include "lldb/Host/posix/DomainSocket.h"
4141
#endif
42+
#ifdef __linux__
43+
#include "lldb/Host/linux/AbstractSocket.h"
44+
#endif
4245
#include "lldb/Utility/FileSpec.h"
4346
#include "lldb/Utility/LLDBLog.h"
4447
#include "lldb/Utility/Status.h"
4548
#include "lldb/Utility/UriParser.h"
4649

50+
#if LLDB_ENABLE_POSIX
51+
#include <sys/socket.h>
52+
#include <sys/un.h>
53+
#endif
54+
4755
using namespace lldb;
4856
using namespace lldb_private;
4957
using namespace lldb_private::lldb_server;
@@ -455,14 +463,28 @@ int main_platform(int argc, char *argv[]) {
455463
lldb_private::Args inferior_arguments;
456464
inferior_arguments.SetArguments(argc, const_cast<const char **>(argv));
457465

466+
Log *log = GetLog(LLDBLog::Platform);
458467
Socket::SocketProtocol protocol = Socket::ProtocolUnixDomain;
459468

460469
if (fd != SharedSocket::kInvalidFD) {
461470
// Child process will handle the connection and exit.
462-
if (gdbserver_port)
471+
if (gdbserver_port) {
463472
protocol = Socket::ProtocolTcp;
473+
} else {
474+
#ifdef LLDB_ENABLE_POSIX
475+
// Check if fd represents domain socket or abstract socket.
476+
struct sockaddr_un addr;
477+
socklen_t addr_len = sizeof(addr);
478+
if (getsockname(fd, (struct sockaddr *)&addr, &addr_len) == -1) {
479+
LLDB_LOGF(log, "lldb-platform child: not a socket or error occurred");
480+
return socket_error;
481+
}
464482

465-
Log *log = GetLog(LLDBLog::Platform);
483+
if (addr.sun_family == AF_UNIX && addr.sun_path[0] == '\0') {
484+
protocol = Socket::ProtocolUnixAbstract;
485+
}
486+
#endif
487+
}
466488

467489
NativeSocket sockfd;
468490
error = SharedSocket::GetNativeSocket(fd, sockfd);
@@ -473,17 +495,27 @@ int main_platform(int argc, char *argv[]) {
473495

474496
GDBRemoteCommunicationServerPlatform platform(protocol, gdbserver_port);
475497
Socket *socket;
476-
if (protocol == Socket::ProtocolTcp)
498+
if (protocol == Socket::ProtocolTcp) {
477499
socket = new TCPSocket(sockfd, /*should_close=*/true);
478-
else {
500+
} else if (protocol == Socket::ProtocolUnixDomain) {
479501
#if LLDB_ENABLE_POSIX
480502
socket = new DomainSocket(sockfd, /*should_close=*/true);
481503
#else
482504
WithColor::error() << "lldb-platform child: Unix domain sockets are not "
483505
"supported on this platform.";
484506
return socket_error;
507+
#endif
508+
} else {
509+
#ifdef __linux__
510+
socket = new AbstractSocket(sockfd, /*should_close=*/true);
511+
#else
512+
WithColor::error()
513+
<< "lldb-platform child: Abstract domain sockets are not "
514+
"supported on this platform.";
515+
return socket_error;
485516
#endif
486517
}
518+
487519
platform.SetConnection(
488520
std::unique_ptr<Connection>(new ConnectionFileDescriptor(socket)));
489521
client_handle(platform, inferior_arguments);

0 commit comments

Comments
 (0)