diff --git a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h --- a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h +++ b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.h @@ -42,6 +42,13 @@ bool HostSupportsIPv6(); bool HostSupportsIPv4(); + +/// Return an IP for localhost based on host support. +/// +/// This will return either "127.0.0.1" if IPv4 is detected, or "[::1]" if IPv6 +/// is detected. If neither are detected, return an error. +llvm::Expected GetLocalhostIP(); + } // namespace lldb_private #endif diff --git a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp --- a/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp +++ b/lldb/unittests/TestingSupport/Host/SocketTestUtilities.cpp @@ -126,3 +126,13 @@ bool lldb_private::HostSupportsIPv6() { return CheckIPSupport("IPv6", "[::1]:0"); } + +llvm::Expected lldb_private::GetLocalhostIP() { + if (HostSupportsIPv4()) + return "127.0.0.1"; + if (HostSupportsIPv6()) + return "[::1]"; + return llvm::make_error( + "Neither IPv4 nor IPv6 appear to be supported", + llvm::inconvertibleErrorCode()); +} diff --git a/lldb/unittests/tools/lldb-server/tests/CMakeLists.txt b/lldb/unittests/tools/lldb-server/tests/CMakeLists.txt --- a/lldb/unittests/tools/lldb-server/tests/CMakeLists.txt +++ b/lldb/unittests/tools/lldb-server/tests/CMakeLists.txt @@ -7,6 +7,7 @@ LINK_LIBS lldbHost lldbCore + lldbHostHelpers lldbInterpreter lldbTarget lldbPluginPlatformLinux diff --git a/lldb/unittests/tools/lldb-server/tests/TestClient.cpp b/lldb/unittests/tools/lldb-server/tests/TestClient.cpp --- a/lldb/unittests/tools/lldb-server/tests/TestClient.cpp +++ b/lldb/unittests/tools/lldb-server/tests/TestClient.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "TestClient.h" +#include "TestingSupport/Host/SocketTestUtilities.h" #include "lldb/Host/HostInfo.h" #include "lldb/Host/common/TCPSocket.h" #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h" @@ -77,14 +78,20 @@ args.AppendArgument("--log-flags=0x800000"); } + auto LocalhostIPOrErr = GetLocalhostIP(); + if (!LocalhostIPOrErr) + return LocalhostIPOrErr.takeError(); + const std::string &LocalhostIP = *LocalhostIPOrErr; + Status status; TCPSocket listen_socket(true, false); - status = listen_socket.Listen("127.0.0.1:0", 5); + status = listen_socket.Listen(LocalhostIP + ":0", 5); if (status.Fail()) return status.ToError(); args.AppendArgument( - ("127.0.0.1:" + Twine(listen_socket.GetLocalPortNumber())).str()); + formatv("{0}:{1}", LocalhostIP, listen_socket.GetLocalPortNumber()) + .str()); for (StringRef arg : ServerArgs) args.AppendArgument(arg);