xref: /NextBSD/contrib/llvm/tools/lldb/include/lldb/Host/posix/ConnectionFileDescriptorPosix.h (revision 84d351007654069f9643c8e4b4802a7f5f08ee42)
1 //===-- ConnectionFileDescriptorPosix.h -------------------------*- 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 #ifndef liblldb_Host_posix_ConnectionFileDescriptorPosix_h_
11 #define liblldb_Host_posix_ConnectionFileDescriptorPosix_h_
12 
13 // C++ Includes
14 #include <atomic>
15 #include <memory>
16 
17 #include "lldb/lldb-forward.h"
18 
19 // Other libraries and framework includes
20 // Project includes
21 #include "lldb/Core/Connection.h"
22 #include "lldb/Host/Mutex.h"
23 #include "lldb/Host/Pipe.h"
24 #include "lldb/Host/Predicate.h"
25 #include "lldb/Host/IOObject.h"
26 
27 namespace lldb_private
28 {
29 
30 class Error;
31 class Socket;
32 class SocketAddress;
33 
34 class ConnectionFileDescriptor : public Connection
35 {
36   public:
37     ConnectionFileDescriptor(bool child_processes_inherit = false);
38 
39     ConnectionFileDescriptor(int fd, bool owns_fd);
40 
41     ConnectionFileDescriptor(Socket* socket);
42 
43     virtual ~ConnectionFileDescriptor();
44 
45     bool IsConnected() const override;
46 
47     lldb::ConnectionStatus Connect(const char *s, Error *error_ptr) override;
48 
49     lldb::ConnectionStatus Disconnect(Error *error_ptr) override;
50 
51     size_t Read(void *dst, size_t dst_len, uint32_t timeout_usec, lldb::ConnectionStatus &status, Error *error_ptr) override;
52 
53     size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status, Error *error_ptr) override;
54 
55     std::string GetURI() override;
56 
57     lldb::ConnectionStatus BytesAvailable(uint32_t timeout_usec, Error *error_ptr);
58 
59     bool InterruptRead() override;
60 
61     lldb::IOObjectSP
GetReadObject()62     GetReadObject() override
63     {
64         return m_read_sp;
65     }
66 
67     uint16_t GetListeningPort(uint32_t timeout_sec);
68 
69     bool GetChildProcessesInherit() const;
70     void SetChildProcessesInherit(bool child_processes_inherit);
71 
72   protected:
73     void OpenCommandPipe();
74 
75     void CloseCommandPipe();
76 
77     lldb::ConnectionStatus SocketListenAndAccept(const char *host_and_port, Error *error_ptr);
78 
79     lldb::ConnectionStatus ConnectTCP(const char *host_and_port, Error *error_ptr);
80 
81     lldb::ConnectionStatus ConnectUDP(const char *args, Error *error_ptr);
82 
83     lldb::ConnectionStatus NamedSocketConnect(const char *socket_name, Error *error_ptr);
84 
85     lldb::ConnectionStatus NamedSocketAccept(const char *socket_name, Error *error_ptr);
86 
87     lldb::IOObjectSP m_read_sp;
88     lldb::IOObjectSP m_write_sp;
89 
90     Predicate<uint16_t> m_port_predicate; // Used when binding to port zero to wait for the thread
91                                           // that creates the socket, binds and listens to resolve
92                                           // the port number.
93 
94     Pipe m_pipe;
95     Mutex m_mutex;
96     std::atomic<bool> m_shutting_down; // This marks that we are shutting down so if we get woken up from
97                                        // BytesAvailable to disconnect, we won't try to read again.
98     bool m_waiting_for_accept;
99     bool m_child_processes_inherit;
100 
101     std::string m_uri;
102 
103   private:
104     void InitializeSocket(Socket* socket);
105 
106     DISALLOW_COPY_AND_ASSIGN(ConnectionFileDescriptor);
107 };
108 
109 } // namespace lldb_private
110 
111 #endif // liblldb_ConnectionFileDescriptor_h_
112