xref: /freebsd/contrib/llvm-project/lldb/include/lldb/Host/posix/ConnectionFileDescriptorPosix.h (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
1 //===-- ConnectionFileDescriptorPosix.h -------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_HOST_POSIX_CONNECTIONFILEDESCRIPTORPOSIX_H
10 #define LLDB_HOST_POSIX_CONNECTIONFILEDESCRIPTORPOSIX_H
11 
12 #include <atomic>
13 #include <memory>
14 #include <mutex>
15 
16 #include "lldb/lldb-forward.h"
17 
18 #include "lldb/Host/Pipe.h"
19 #include "lldb/Host/Socket.h"
20 #include "lldb/Utility/Connection.h"
21 #include "lldb/Utility/IOObject.h"
22 
23 namespace lldb_private {
24 
25 class Status;
26 class Socket;
27 class SocketAddress;
28 
29 class ConnectionFileDescriptor : public Connection {
30 public:
31   typedef llvm::function_ref<void(llvm::StringRef local_socket_id)>
32       socket_id_callback_type;
33 
34   ConnectionFileDescriptor(bool child_processes_inherit = false);
35 
36   ConnectionFileDescriptor(int fd, bool owns_fd);
37 
38   ConnectionFileDescriptor(Socket *socket);
39 
40   ~ConnectionFileDescriptor() override;
41 
42   bool IsConnected() const override;
43 
44   lldb::ConnectionStatus Connect(llvm::StringRef url,
45                                  Status *error_ptr) override;
46 
47   lldb::ConnectionStatus Connect(llvm::StringRef url,
48                                  socket_id_callback_type socket_id_callback,
49                                  Status *error_ptr);
50 
51   lldb::ConnectionStatus Disconnect(Status *error_ptr) override;
52 
53   size_t Read(void *dst, size_t dst_len, const Timeout<std::micro> &timeout,
54               lldb::ConnectionStatus &status, Status *error_ptr) override;
55 
56   size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status,
57                Status *error_ptr) override;
58 
59   std::string GetURI() override;
60 
61   lldb::ConnectionStatus BytesAvailable(const Timeout<std::micro> &timeout,
62                                         Status *error_ptr);
63 
64   bool InterruptRead() override;
65 
GetReadObject()66   lldb::IOObjectSP GetReadObject() override { return m_io_sp; }
67 
68   bool GetChildProcessesInherit() const;
69   void SetChildProcessesInherit(bool child_processes_inherit);
70 
71 protected:
72   void OpenCommandPipe();
73 
74   void CloseCommandPipe();
75 
76   lldb::ConnectionStatus
77   AcceptSocket(Socket::SocketProtocol socket_protocol,
78                llvm::StringRef socket_name,
79                llvm::function_ref<void(Socket &)> post_listen_callback,
80                Status *error_ptr);
81 
82   lldb::ConnectionStatus ConnectSocket(Socket::SocketProtocol socket_protocol,
83                                        llvm::StringRef socket_name,
84                                        Status *error_ptr);
85 
86   lldb::ConnectionStatus AcceptTCP(llvm::StringRef host_and_port,
87                                    socket_id_callback_type socket_id_callback,
88                                    Status *error_ptr);
89 
90   lldb::ConnectionStatus ConnectTCP(llvm::StringRef host_and_port,
91                                     socket_id_callback_type socket_id_callback,
92                                     Status *error_ptr);
93 
94   lldb::ConnectionStatus ConnectUDP(llvm::StringRef args,
95                                     socket_id_callback_type socket_id_callback,
96                                     Status *error_ptr);
97 
98   lldb::ConnectionStatus
99   ConnectNamedSocket(llvm::StringRef socket_name,
100                      socket_id_callback_type socket_id_callback,
101                      Status *error_ptr);
102 
103   lldb::ConnectionStatus
104   AcceptNamedSocket(llvm::StringRef socket_name,
105                     socket_id_callback_type socket_id_callback,
106                     Status *error_ptr);
107 
108   lldb::ConnectionStatus
109   AcceptAbstractSocket(llvm::StringRef socket_name,
110                        socket_id_callback_type socket_id_callback,
111                        Status *error_ptr);
112 
113   lldb::ConnectionStatus
114   ConnectAbstractSocket(llvm::StringRef socket_name,
115                         socket_id_callback_type socket_id_callback,
116                         Status *error_ptr);
117 
118   lldb::ConnectionStatus ConnectFD(llvm::StringRef args,
119                                    socket_id_callback_type socket_id_callback,
120                                    Status *error_ptr);
121 
122   lldb::ConnectionStatus ConnectFile(llvm::StringRef args,
123                                      socket_id_callback_type socket_id_callback,
124                                      Status *error_ptr);
125 
126   lldb::ConnectionStatus
127   ConnectSerialPort(llvm::StringRef args,
128                     socket_id_callback_type socket_id_callback,
129                     Status *error_ptr);
130 
131   lldb::IOObjectSP m_io_sp;
132 
133   Pipe m_pipe;
134   std::recursive_mutex m_mutex;
135   std::atomic<bool> m_shutting_down; // This marks that we are shutting down so
136                                      // if we get woken up from
137   // BytesAvailable to disconnect, we won't try to read again.
138   bool m_child_processes_inherit;
139 
140   std::string m_uri;
141 
142 private:
143   void InitializeSocket(Socket *socket);
144 
145   ConnectionFileDescriptor(const ConnectionFileDescriptor &) = delete;
146   const ConnectionFileDescriptor &
147   operator=(const ConnectionFileDescriptor &) = delete;
148 };
149 
150 } // namespace lldb_private
151 
152 #endif // LLDB_HOST_POSIX_CONNECTIONFILEDESCRIPTORPOSIX_H
153