Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98fe889

Browse files
ashgtiJDevlieghere
authored andcommitted
[lldb] For a host socket, add a method to print the listening address. (llvm#118330)
This is most useful if you are listening on an address like 'localhost:0' and want to know the resolved ip + port of the socket listener. (cherry picked from commit 3845624)
1 parent 5568a88 commit 98fe889

File tree

6 files changed

+55
-1
lines changed

6 files changed

+55
-1
lines changed

lldb/include/lldb/Host/Socket.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <memory>
1313
#include <string>
14+
#include <vector>
1415

1516
#include "lldb/lldb-private.h"
1617

@@ -132,6 +133,11 @@ class Socket : public IOObject {
132133
// If this Socket is connected then return the URI used to connect.
133134
virtual std::string GetRemoteConnectionURI() const { return ""; };
134135

136+
// If the Socket is listening then return the URI for clients to connect.
137+
virtual std::vector<std::string> GetListeningConnectionURI() const {
138+
return {};
139+
}
140+
135141
protected:
136142
Socket(SocketProtocol protocol, bool should_close,
137143
bool m_child_process_inherit);

lldb/include/lldb/Host/common/TCPSocket.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "lldb/Host/Socket.h"
1414
#include "lldb/Host/SocketAddress.h"
1515
#include <map>
16+
#include <string>
17+
#include <vector>
1618

1719
namespace lldb_private {
1820
class TCPSocket : public Socket {
@@ -59,6 +61,8 @@ class TCPSocket : public Socket {
5961

6062
std::string GetRemoteConnectionURI() const override;
6163

64+
std::vector<std::string> GetListeningConnectionURI() const override;
65+
6266
private:
6367
TCPSocket(NativeSocket socket, const TCPSocket &listen_socket);
6468

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#define LLDB_HOST_POSIX_DOMAINSOCKET_H
1111

1212
#include "lldb/Host/Socket.h"
13+
#include <string>
14+
#include <vector>
1315

1416
namespace lldb_private {
1517
class DomainSocket : public Socket {
@@ -22,6 +24,8 @@ class DomainSocket : public Socket {
2224

2325
std::string GetRemoteConnectionURI() const override;
2426

27+
std::vector<std::string> GetListeningConnectionURI() const override;
28+
2529
protected:
2630
DomainSocket(SocketProtocol protocol, bool child_processes_inherit);
2731

lldb/source/Host/common/TCPSocket.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ std::string TCPSocket::GetRemoteConnectionURI() const {
137137
return "";
138138
}
139139

140+
std::vector<std::string> TCPSocket::GetListeningConnectionURI() const {
141+
std::vector<std::string> URIs;
142+
for (const auto &[fd, addr] : m_listen_sockets)
143+
URIs.emplace_back(llvm::formatv("connection://[{0}]:{1}",
144+
addr.GetIPAddress(), addr.GetPort()));
145+
return URIs;
146+
}
147+
140148
Status TCPSocket::CreateSocket(int domain) {
141149
Status error;
142150
if (IsValid())

lldb/source/Host/posix/DomainSocket.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,17 @@ std::string DomainSocket::GetRemoteConnectionURI() const {
155155
"{0}://{1}",
156156
GetNameOffset() == 0 ? "unix-connect" : "unix-abstract-connect", name);
157157
}
158+
159+
std::vector<std::string> DomainSocket::GetListeningConnectionURI() const {
160+
if (m_socket == kInvalidSocketValue)
161+
return {};
162+
163+
struct sockaddr_un addr;
164+
bzero(&addr, sizeof(struct sockaddr_un));
165+
addr.sun_family = AF_UNIX;
166+
socklen_t addr_len = sizeof(struct sockaddr_un);
167+
if (::getsockname(m_socket, (struct sockaddr *)&addr, &addr_len) != 0)
168+
return {};
169+
170+
return {llvm::formatv("unix-connect://{0}", addr.sun_path)};
171+
}

lldb/unittests/Host/SocketTest.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,30 @@ TEST_P(SocketTest, TCPListen0GetPort) {
168168
if (!HostSupportsIPv4())
169169
return;
170170
llvm::Expected<std::unique_ptr<TCPSocket>> sock =
171-
Socket::TcpListen("10.10.12.3:0", false);
171+
Socket::TcpListen("10.10.12.3:0", 5);
172172
ASSERT_THAT_EXPECTED(sock, llvm::Succeeded());
173173
ASSERT_TRUE(sock.get()->IsValid());
174174
EXPECT_NE(sock.get()->GetLocalPortNumber(), 0);
175175
}
176176

177+
TEST_P(SocketTest, TCPListen0GetListeningConnectionURI) {
178+
if (!HostSupportsProtocol())
179+
return;
180+
181+
std::string addr = llvm::formatv("[{0}]:0", GetParam().localhost_ip).str();
182+
llvm::Expected<std::unique_ptr<TCPSocket>> sock =
183+
Socket::TcpListen(addr, false);
184+
ASSERT_THAT_EXPECTED(sock, llvm::Succeeded());
185+
ASSERT_TRUE(sock.get()->IsValid());
186+
187+
EXPECT_THAT(
188+
sock.get()->GetListeningConnectionURI(),
189+
testing::ElementsAre(llvm::formatv("connection://[{0}]:{1}",
190+
GetParam().localhost_ip,
191+
sock->get()->GetLocalPortNumber())
192+
.str()));
193+
}
194+
177195
TEST_P(SocketTest, TCPGetConnectURI) {
178196
std::unique_ptr<TCPSocket> socket_a_up;
179197
std::unique_ptr<TCPSocket> socket_b_up;

0 commit comments

Comments
 (0)