xref: /llvm-project/lldb/unittests/Process/gdb-remote/GDBRemoteTestUtils.cpp (revision b9c1b51e45b845debb76d8658edabca70ca56079)
1 //===-- GDBRemoteTestUtils.cpp ----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #if defined(_MSC_VER) && (_HAS_EXCEPTIONS == 0)
11 // Workaround for MSVC standard library bug, which fails to include <thread>
12 // when
13 // exceptions are disabled.
14 #include <eh.h>
15 #endif
16 
17 #include "GDBRemoteTestUtils.h"
18 
19 #include "lldb/Host/common/TCPSocket.h"
20 #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
21 
22 #include <future>
23 
24 namespace lldb_private {
25 namespace process_gdb_remote {
26 
27 void GDBRemoteTest::SetUpTestCase() {
28 #if defined(_MSC_VER)
29   WSADATA data;
30   ::WSAStartup(MAKEWORD(2, 2), &data);
31 #endif
32 }
33 
34 void GDBRemoteTest::TearDownTestCase() {
35 #if defined(_MSC_VER)
36   ::WSACleanup();
37 #endif
38 }
39 
40 void Connect(GDBRemoteCommunication &client, GDBRemoteCommunication &server) {
41   bool child_processes_inherit = false;
42   Error error;
43   TCPSocket listen_socket(child_processes_inherit, error);
44   ASSERT_FALSE(error.Fail());
45   error = listen_socket.Listen("127.0.0.1:0", 5);
46   ASSERT_FALSE(error.Fail());
47 
48   Socket *accept_socket;
49   std::future<Error> accept_error = std::async(std::launch::async, [&] {
50     return listen_socket.Accept("127.0.0.1:0", child_processes_inherit,
51                                 accept_socket);
52   });
53 
54   char connect_remote_address[64];
55   snprintf(connect_remote_address, sizeof(connect_remote_address),
56            "connect://localhost:%u", listen_socket.GetLocalPortNumber());
57 
58   std::unique_ptr<ConnectionFileDescriptor> conn_ap(
59       new ConnectionFileDescriptor());
60   ASSERT_EQ(conn_ap->Connect(connect_remote_address, nullptr),
61             lldb::eConnectionStatusSuccess);
62 
63   client.SetConnection(conn_ap.release());
64   ASSERT_TRUE(accept_error.get().Success());
65   server.SetConnection(new ConnectionFileDescriptor(accept_socket));
66 }
67 
68 } // namespace process_gdb_remote
69 } // namespace lldb_private
70