[Lldb-commits] [lldb] r358044 - [lldb-server] Introduce Socket::Initialize and Terminate to simply WSASocket setup

Aaron Smith via lldb-commits lldb-commits at lists.llvm.org
Tue Apr 9 21:57:18 PDT 2019


Author: asmith
Date: Tue Apr  9 21:57:18 2019
New Revision: 358044

URL: http://llvm.org/viewvc/llvm-project?rev=358044&view=rev
Log:
[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

Modified:
    lldb/trunk/include/lldb/Host/Socket.h
    lldb/trunk/source/Host/common/Socket.cpp
    lldb/trunk/source/Initialization/SystemInitializerCommon.cpp
    lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp
    lldb/trunk/unittests/Host/MainLoopTest.cpp
    lldb/trunk/unittests/Host/SocketAddressTest.cpp
    lldb/trunk/unittests/Host/SocketTest.cpp
    lldb/trunk/unittests/Process/gdb-remote/GDBRemoteTestUtils.cpp

Modified: lldb/trunk/include/lldb/Host/Socket.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/Socket.h?rev=358044&r1=358043&r2=358044&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/Socket.h (original)
+++ lldb/trunk/include/lldb/Host/Socket.h Tue Apr  9 21:57:18 2019
@@ -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);

Modified: lldb/trunk/source/Host/common/Socket.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Socket.cpp?rev=358044&r1=358043&r2=358044&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Socket.cpp (original)
+++ lldb/trunk/source/Host/common/Socket.cpp Tue Apr  9 21:57:18 2019
@@ -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,
 
 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) {

Modified: lldb/trunk/source/Initialization/SystemInitializerCommon.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Initialization/SystemInitializerCommon.cpp?rev=358044&r1=358043&r2=358044&view=diff
==============================================================================
--- lldb/trunk/source/Initialization/SystemInitializerCommon.cpp (original)
+++ lldb/trunk/source/Initialization/SystemInitializerCommon.cpp Tue Apr  9 21:57:18 2019
@@ -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::Ini
 
   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();

Modified: lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp?rev=358044&r1=358043&r2=358044&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp Tue Apr  9 21:57:18 2019
@@ -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);
     }
   }

Modified: lldb/trunk/unittests/Host/MainLoopTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Host/MainLoopTest.cpp?rev=358044&r1=358043&r2=358044&view=diff
==============================================================================
--- lldb/trunk/unittests/Host/MainLoopTest.cpp (original)
+++ lldb/trunk/unittests/Host/MainLoopTest.cpp Tue Apr  9 21:57:18 2019
@@ -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;

Modified: lldb/trunk/unittests/Host/SocketAddressTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Host/SocketAddressTest.cpp?rev=358044&r1=358043&r2=358044&view=diff
==============================================================================
--- lldb/trunk/unittests/Host/SocketAddressTest.cpp (original)
+++ lldb/trunk/unittests/Host/SocketAddressTest.cpp Tue Apr  9 21:57:18 2019
@@ -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));

Modified: lldb/trunk/unittests/Host/SocketTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Host/SocketTest.cpp?rev=358044&r1=358043&r2=358044&view=diff
==============================================================================
--- lldb/trunk/unittests/Host/SocketTest.cpp (original)
+++ lldb/trunk/unittests/Host/SocketTest.cpp Tue Apr  9 21:57:18 2019
@@ -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,

Modified: lldb/trunk/unittests/Process/gdb-remote/GDBRemoteTestUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Process/gdb-remote/GDBRemoteTestUtils.cpp?rev=358044&r1=358043&r2=358044&view=diff
==============================================================================
--- lldb/trunk/unittests/Process/gdb-remote/GDBRemoteTestUtils.cpp (original)
+++ lldb/trunk/unittests/Process/gdb-remote/GDBRemoteTestUtils.cpp Tue Apr  9 21:57:18 2019
@@ -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




More information about the lldb-commits mailing list