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 void Connect(GDBRemoteCommunication &client, GDBRemoteCommunication &server) { 34 bool child_processes_inherit = false; 35 Status error; 36 TCPSocket listen_socket(true, child_processes_inherit); 37 ASSERT_FALSE(error.Fail()); 38 error = listen_socket.Listen("127.0.0.1:0", 5); 39 ASSERT_FALSE(error.Fail()); 40 41 Socket *accept_socket; 42 std::future<Status> accept_error = std::async( 43 std::launch::async, [&] { return listen_socket.Accept(accept_socket); }); 44 45 char connect_remote_address[64]; 46 snprintf(connect_remote_address, sizeof(connect_remote_address), 47 "connect://localhost:%u", listen_socket.GetLocalPortNumber()); 48 49 std::unique_ptr<ConnectionFileDescriptor> conn_ap( 50 new ConnectionFileDescriptor()); 51 ASSERT_EQ(conn_ap->Connect(connect_remote_address, nullptr), 52 lldb::eConnectionStatusSuccess); 53 54 client.SetConnection(conn_ap.release()); 55 ASSERT_TRUE(accept_error.get().Success()); 56 server.SetConnection(new ConnectionFileDescriptor(accept_socket)); 57 } 58 59 } // namespace process_gdb_remote 60 } // namespace lldb_private 61