xref: /llvm-project/lldb/unittests/Process/gdb-remote/GDBRemoteTestUtils.cpp (revision 93df2fbeabaf5f706ff01aafc7b03a811bf8c5a1)
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 #include "GDBRemoteTestUtils.h"
11 
12 #include "lldb/Host/common/TCPSocket.h"
13 #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
14 
15 #include <future>
16 
17 namespace lldb_private {
18 namespace process_gdb_remote {
19 
20 void GDBRemoteTest::SetUpTestCase() {
21 #if defined(_MSC_VER)
22   WSADATA data;
23   ::WSAStartup(MAKEWORD(2, 2), &data);
24 #endif
25 }
26 
27 void GDBRemoteTest::TearDownTestCase() {
28 #if defined(_MSC_VER)
29   ::WSACleanup();
30 #endif
31 }
32 
33 llvm::Error GDBRemoteTest::Connect(GDBRemoteCommunication &client,
34                                    GDBRemoteCommunication &server) {
35   bool child_processes_inherit = false;
36   TCPSocket listen_socket(true, child_processes_inherit);
37   if (llvm::Error error = listen_socket.Listen("127.0.0.1:0", 5).ToError())
38     return error;
39 
40   Socket *accept_socket;
41   std::future<Status> accept_status = std::async(
42       std::launch::async, [&] { return listen_socket.Accept(accept_socket); });
43 
44   llvm::SmallString<32> remote_addr;
45   llvm::raw_svector_ostream(remote_addr)
46       << "connect://localhost:" << listen_socket.GetLocalPortNumber();
47 
48   std::unique_ptr<ConnectionFileDescriptor> conn_up(
49       new ConnectionFileDescriptor());
50   if (conn_up->Connect(remote_addr, nullptr) != lldb::eConnectionStatusSuccess)
51     return llvm::make_error<llvm::StringError>("Unable to connect",
52                                                llvm::inconvertibleErrorCode());
53 
54   client.SetConnection(conn_up.release());
55   if (llvm::Error error = accept_status.get().ToError())
56     return error;
57 
58   server.SetConnection(new ConnectionFileDescriptor(accept_socket));
59   return llvm::Error::success();
60 }
61 
62 } // namespace process_gdb_remote
63 } // namespace lldb_private
64