11#include "lldb/Host/Config.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/Support/Errno.h"
21#include "llvm/Support/Error.h"
22#include "llvm/Support/Regex.h"
23#include "llvm/Support/WindowsError.h"
30#include <netinet/in.h>
31#include <netinet/tcp.h>
32#include <sys/socket.h>
43#include <asm-generic/errno-base.h>
47#include <sys/syscall.h>
66 return ::WSAGetLastError() == WSAEINTR;
68 return errno == EINTR;
73 bool child_processes_inherit)
74 :
IOObject(eFDTypeSocket), m_protocol(protocol),
75 m_socket(kInvalidSocketValue),
76 m_child_processes_inherit(child_processes_inherit),
77 m_should_close_fd(should_close) {}
83 auto wVersion = WINSOCK_VERSION;
85 int err = ::WSAStartup(wVersion, &wsaData);
87 if (wsaData.wVersion < wVersion) {
89 return llvm::make_error<llvm::StringError>(
90 "WSASock version is not expected.", llvm::inconvertibleErrorCode());
93 return llvm::errorCodeToError(llvm::mapWindowsError(::WSAGetLastError()));
97 return llvm::Error::success();
107 bool child_processes_inherit,
111 std::unique_ptr<Socket> socket_up;
115 std::make_unique<TCPSocket>(
true, child_processes_inherit);
119 std::make_unique<UDPSocket>(
true, child_processes_inherit);
124 std::make_unique<DomainSocket>(
true, child_processes_inherit);
126 error.SetErrorString(
127 "Unix domain sockets are not supported on this platform.");
133 std::make_unique<AbstractSocket>(child_processes_inherit);
135 error.SetErrorString(
136 "Abstract domain sockets are not supported on this platform.");
147llvm::Expected<std::unique_ptr<Socket>>
149 bool child_processes_inherit) {
151 LLDB_LOG(log,
"host_and_port = {0}", host_and_port);
154 std::unique_ptr<Socket> connect_socket(
157 return error.ToError();
159 error = connect_socket->Connect(host_and_port);
161 return std::move(connect_socket);
163 return error.ToError();
166llvm::Expected<std::unique_ptr<TCPSocket>>
170 LLDB_LOG(log,
"host_and_port = {0}", host_and_port);
172 std::unique_ptr<TCPSocket> listen_socket(
173 new TCPSocket(
true, child_processes_inherit));
175 Status error = listen_socket->Listen(host_and_port, backlog);
177 return error.ToError();
179 return std::move(listen_socket);
182llvm::Expected<std::unique_ptr<UDPSocket>>
184 bool child_processes_inherit) {
189 static llvm::Regex g_regex(
"([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)");
191 llvm::SmallVector<llvm::StringRef, 3> matches;
192 if (g_regex.match(host_and_port, &matches)) {
197 if (to_integer(matches[2], ret.
port, 10))
202 if (to_integer(host_and_port, ret.
port, 10))
206 return llvm::createStringError(llvm::inconvertibleErrorCode(),
207 "invalid host:port specification: '%s'",
208 host_and_port.str().c_str());
218 int bytes_received = 0;
220 bytes_received = ::recv(
m_socket,
static_cast<char *
>(buf), num_bytes, 0);
223 if (bytes_received < 0) {
227 num_bytes = bytes_received;
232 "%p Socket::Read() (socket = %" PRIu64
233 ", src = %p, src_len = %" PRIu64
", flags = 0) => %" PRIi64
235 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket), buf,
236 static_cast<uint64_t
>(num_bytes),
237 static_cast<int64_t
>(bytes_received),
error.AsCString());
244 const size_t src_len = num_bytes;
248 bytes_sent =
Send(buf, num_bytes);
251 if (bytes_sent < 0) {
255 num_bytes = bytes_sent;
260 "%p Socket::Write() (socket = %" PRIu64
261 ", src = %p, src_len = %" PRIu64
", flags = 0) => %" PRIi64
263 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket), buf,
264 static_cast<uint64_t
>(src_len),
265 static_cast<int64_t
>(bytes_sent),
error.AsCString());
282 LLDB_LOGF(log,
"%p Socket::Close (fd = %" PRIu64
")",
283 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket));
286 bool success = closesocket(
m_socket) == 0;
288 bool success = ::close(
m_socket) == 0;
302 socklen_t option_value_size =
sizeof(int);
303 return ::getsockopt(
m_socket, level, option_name, option_value_p,
310 return ::setsockopt(
m_socket, level, option_name, option_value_p,
311 sizeof(option_value));
315 return ::send(
m_socket,
static_cast<const char *
>(buf), num_bytes, 0);
322 error.SetErrorToErrno();
330 auto socket_type = type;
332 if (!child_processes_inherit)
333 socket_type |= SOCK_CLOEXEC;
335 auto sock = ::socket(domain, socket_type, protocol);
346#if defined(ANDROID_USE_ACCEPT_WORKAROUND)
353 int fd = syscall(__NR_accept, sockfd, addr, addrlen);
354 if (fd >= 0 && !child_processes_inherit) {
355 int flags = ::fcntl(fd, F_GETFD);
356 if (flags != -1 && ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1)
362#elif defined(SOCK_CLOEXEC) && defined(HAVE_ACCEPT4)
364 if (!child_processes_inherit) {
365 flags |= SOCK_CLOEXEC;
368 static_cast<NativeSocket>(-1), ::accept4, sockfd, addr, addrlen, flags);
371 static_cast<NativeSocket>(-1), ::accept, sockfd, addr, addrlen);
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
#define LLDB_LOGF(log,...)
static bool IsInterrupted()
void * get_socket_option_arg_type
const void * set_socket_option_arg_type
Status Read(void *buf, size_t &num_bytes) override
static llvm::Expected< std::unique_ptr< UDPSocket > > UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit)
static const NativeSocket kInvalidSocketValue
WaitableHandle GetWaitableHandle() override
static NativeSocket CreateSocket(const int domain, const int type, const int protocol, bool child_processes_inherit, Status &error)
virtual size_t Send(const void *buf, const size_t num_bytes)
int SetOption(int level, int option_name, int option_value)
virtual Status PreDisconnect()
static llvm::Expected< HostAndPort > DecodeHostAndPort(llvm::StringRef host_and_port)
int GetOption(int level, int option_name, int &option_value)
Socket(SocketProtocol protocol, bool should_close, bool m_child_process_inherit)
static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr, socklen_t *addrlen, bool child_processes_inherit, Status &error)
bool IsValid() const override
static void SetLastError(Status &error)
static llvm::Error Initialize()
static llvm::Expected< std::unique_ptr< Socket > > TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit)
Status Write(const void *buf, size_t &num_bytes) override
static std::unique_ptr< Socket > Create(const SocketProtocol protocol, bool child_processes_inherit, Status &error)
static llvm::Expected< std::unique_ptr< TCPSocket > > TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit, int backlog=5)
static llvm::Expected< std::unique_ptr< UDPSocket > > Connect(llvm::StringRef name, bool child_processes_inherit)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Stream & operator<<(Stream &s, const SourceLocationSpec &loc)
Dump a SourceLocationSpec object to a stream.
@ eErrorTypeWin32
Standard Win32 error codes.