summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Smith <aaron.smith@microsoft.com>2019-04-10 04:57:18 +0000
committerAaron Smith <aaron.smith@microsoft.com>2019-04-10 04:57:18 +0000
commitf8a74c18ec8869f42a5d48ad20c33e3021f68f26 (patch)
treeeb60cfe137022457cd4c181992fd5f351e821aa4
parent5f2b5cd85e5518c8035b4d656bc95f16d12ecf71 (diff)
downloadbcm5719-llvm-f8a74c18ec8869f42a5d48ad20c33e3021f68f26.tar.gz
bcm5719-llvm-f8a74c18ec8869f42a5d48ad20c33e3021f68f26.zip
[lldb-server] Introduce Socket::Initialize and Terminate to simply WSASocket setup
Reviewers: zturner, labath Reviewed By: labath Subscribers: lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D60440 llvm-svn: 358044
-rw-r--r--lldb/include/lldb/Host/Socket.h3
-rw-r--r--lldb/source/Host/common/Socket.cpp27
-rw-r--r--lldb/source/Initialization/SystemInitializerCommon.cpp7
-rw-r--r--lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp7
-rw-r--r--lldb/unittests/Host/MainLoopTest.cpp12
-rw-r--r--lldb/unittests/Host/SocketAddressTest.cpp19
-rw-r--r--lldb/unittests/Host/SocketTest.cpp12
-rw-r--r--lldb/unittests/Process/gdb-remote/GDBRemoteTestUtils.cpp18
8 files changed, 55 insertions, 50 deletions
diff --git a/lldb/include/lldb/Host/Socket.h b/lldb/include/lldb/Host/Socket.h
index 642e85e6563..0fae2c627b9 100644
--- a/lldb/include/lldb/Host/Socket.h
+++ b/lldb/include/lldb/Host/Socket.h
@@ -50,6 +50,9 @@ public:
~Socket() override;
+ static llvm::Error Initialize();
+ static void Terminate();
+
static std::unique_ptr<Socket> Create(const SocketProtocol protocol,
bool child_processes_inherit,
Status &error);
diff --git a/lldb/source/Host/common/Socket.cpp b/lldb/source/Host/common/Socket.cpp
index 6b9cb480067..6fa161af351 100644
--- a/lldb/source/Host/common/Socket.cpp
+++ b/lldb/source/Host/common/Socket.cpp
@@ -19,6 +19,8 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Errno.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/WindowsError.h"
#ifndef LLDB_DISABLE_POSIX
#include "lldb/Host/posix/DomainSocket.h"
@@ -78,6 +80,31 @@ Socket::Socket(SocketProtocol protocol, bool should_close,
Socket::~Socket() { Close(); }
+llvm::Error Socket::Initialize() {
+#if defined(_WIN32)
+ auto wVersion = WINSOCK_VERSION;
+ WSADATA wsaData;
+ int err = ::WSAStartup(wVersion, &wsaData);
+ if (err == 0) {
+ if (wsaData.wVersion < wVersion) {
+ WSACleanup();
+ return llvm::make_error<llvm::StringError>(
+ "WSASock version is not expected.", llvm::inconvertibleErrorCode());
+ }
+ } else {
+ return llvm::errorCodeToError(llvm::mapWindowsError(::WSAGetLastError()));
+ }
+#endif
+
+ return llvm::Error::success();
+}
+
+void Socket::Terminate() {
+#if defined(_WIN32)
+ ::WSACleanup();
+#endif
+}
+
std::unique_ptr<Socket> Socket::Create(const SocketProtocol protocol,
bool child_processes_inherit,
Status &error) {
diff --git a/lldb/source/Initialization/SystemInitializerCommon.cpp b/lldb/source/Initialization/SystemInitializerCommon.cpp
index f69f5b0bf59..e0d76c1023a 100644
--- a/lldb/source/Initialization/SystemInitializerCommon.cpp
+++ b/lldb/source/Initialization/SystemInitializerCommon.cpp
@@ -17,6 +17,7 @@
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/Host.h"
#include "lldb/Host/HostInfo.h"
+#include "lldb/Host/Socket.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Reproducer.h"
#include "lldb/Utility/Timer.h"
@@ -90,6 +91,11 @@ llvm::Error SystemInitializerCommon::Initialize() {
Log::Initialize();
HostInfo::Initialize();
+
+ llvm::Error error = Socket::Initialize();
+ if (error)
+ return error;
+
static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
@@ -132,6 +138,7 @@ void SystemInitializerCommon::Terminate() {
ProcessWindowsLog::Terminate();
#endif
+ Socket::Terminate();
HostInfo::Terminate();
Log::DisableAllLogChannels();
FileSystem::Terminate();
diff --git a/lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp b/lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp
index 17aa3369421..df48d153101 100644
--- a/lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp
+++ b/lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp
@@ -125,8 +125,6 @@ void PlatformWindows::Initialize() {
if (g_initialize_count++ == 0) {
#if defined(_WIN32)
- WSADATA dummy;
- WSAStartup(MAKEWORD(2, 2), &dummy);
// Force a host flag to true for the default platform object.
PlatformSP default_platform_sp(new PlatformWindows(true));
default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
@@ -139,12 +137,9 @@ void PlatformWindows::Initialize() {
}
}
-void PlatformWindows::Terminate(void) {
+void PlatformWindows::Terminate() {
if (g_initialize_count > 0) {
if (--g_initialize_count == 0) {
-#ifdef _WIN32
- WSACleanup();
-#endif
PluginManager::UnregisterPlugin(PlatformWindows::CreateInstance);
}
}
diff --git a/lldb/unittests/Host/MainLoopTest.cpp b/lldb/unittests/Host/MainLoopTest.cpp
index af225de4ab2..d77bd0935f2 100644
--- a/lldb/unittests/Host/MainLoopTest.cpp
+++ b/lldb/unittests/Host/MainLoopTest.cpp
@@ -10,6 +10,7 @@
#include "lldb/Host/ConnectionFileDescriptor.h"
#include "lldb/Host/PseudoTerminal.h"
#include "lldb/Host/common/TCPSocket.h"
+#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include <future>
@@ -19,17 +20,10 @@ namespace {
class MainLoopTest : public testing::Test {
public:
static void SetUpTestCase() {
-#ifdef _MSC_VER
- WSADATA data;
- ASSERT_EQ(0, WSAStartup(MAKEWORD(2, 2), &data));
-#endif
+ ASSERT_THAT_ERROR(Socket::Initialize(), llvm::Succeeded());
}
- static void TearDownTestCase() {
-#ifdef _MSC_VER
- ASSERT_EQ(0, WSACleanup());
-#endif
- }
+ static void TearDownTestCase() { Socket::Terminate(); }
void SetUp() override {
bool child_processes_inherit = false;
diff --git a/lldb/unittests/Host/SocketAddressTest.cpp b/lldb/unittests/Host/SocketAddressTest.cpp
index 42242f4fe64..81bc5dcd03c 100644
--- a/lldb/unittests/Host/SocketAddressTest.cpp
+++ b/lldb/unittests/Host/SocketAddressTest.cpp
@@ -6,29 +6,24 @@
//
//===----------------------------------------------------------------------===//
+#include "lldb/Host/SocketAddress.h"
+#include "lldb/Host/Socket.h"
+#include "llvm/Testing/Support/Error.h"
+
#include "gtest/gtest.h"
-#include "lldb/Host/SocketAddress.h"
+using namespace lldb_private;
namespace {
class SocketAddressTest : public testing::Test {
public:
static void SetUpTestCase() {
-#ifdef _MSC_VER
- WSADATA data;
- ASSERT_EQ(0, WSAStartup(MAKEWORD(2, 2), &data));
-#endif
- }
- static void TearDownTestCase() {
-#ifdef _MSC_VER
- ASSERT_EQ(0, WSACleanup());
-#endif
+ ASSERT_THAT_ERROR(Socket::Initialize(), llvm::Succeeded());
}
+ static void TearDownTestCase() { Socket::Terminate(); }
};
} // namespace
-using namespace lldb_private;
-
TEST_F(SocketAddressTest, Set) {
SocketAddress sa;
ASSERT_TRUE(sa.SetToLocalhost(AF_INET, 1138));
diff --git a/lldb/unittests/Host/SocketTest.cpp b/lldb/unittests/Host/SocketTest.cpp
index 9359764600a..1192fa65f87 100644
--- a/lldb/unittests/Host/SocketTest.cpp
+++ b/lldb/unittests/Host/SocketTest.cpp
@@ -18,6 +18,7 @@
#include "lldb/Host/common/UDPSocket.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
+#include "llvm/Testing/Support/Error.h"
#ifndef LLDB_DISABLE_POSIX
#include "lldb/Host/posix/DomainSocket.h"
@@ -28,17 +29,10 @@ using namespace lldb_private;
class SocketTest : public testing::Test {
public:
void SetUp() override {
-#if defined(_MSC_VER)
- WSADATA data;
- ::WSAStartup(MAKEWORD(2, 2), &data);
-#endif
+ ASSERT_THAT_ERROR(Socket::Initialize(), llvm::Succeeded());
}
- void TearDown() override {
-#if defined(_MSC_VER)
- ::WSACleanup();
-#endif
- }
+ void TearDown() override { Socket::Terminate(); }
protected:
static void AcceptThread(Socket *listen_socket,
diff --git a/lldb/unittests/Process/gdb-remote/GDBRemoteTestUtils.cpp b/lldb/unittests/Process/gdb-remote/GDBRemoteTestUtils.cpp
index cd0ecd4dc31..92e16d981fe 100644
--- a/lldb/unittests/Process/gdb-remote/GDBRemoteTestUtils.cpp
+++ b/lldb/unittests/Process/gdb-remote/GDBRemoteTestUtils.cpp
@@ -7,27 +7,17 @@
//===----------------------------------------------------------------------===//
#include "GDBRemoteTestUtils.h"
-
-#if defined(_MSC_VER)
-#include "lldb/Host/windows/windows.h"
-#include <WinSock2.h>
-#endif
+#include "lldb/Host/Socket.h"
+#include "llvm/Testing/Support/Error.h"
namespace lldb_private {
namespace process_gdb_remote {
void GDBRemoteTest::SetUpTestCase() {
-#if defined(_MSC_VER)
- WSADATA data;
- ::WSAStartup(MAKEWORD(2, 2), &data);
-#endif
+ ASSERT_THAT_ERROR(Socket::Initialize(), llvm::Succeeded());
}
-void GDBRemoteTest::TearDownTestCase() {
-#if defined(_MSC_VER)
- ::WSACleanup();
-#endif
-}
+void GDBRemoteTest::TearDownTestCase() { Socket::Terminate(); }
} // namespace process_gdb_remote
} // namespace lldb_private
OpenPOWER on IntegriCloud