11#include "lldb/Host/Config.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/Support/Errno.h"
22#include "llvm/Support/Error.h"
23#include "llvm/Support/Regex.h"
24#include "llvm/Support/WindowsError.h"
31#include <netinet/in.h>
32#include <netinet/tcp.h>
33#include <sys/socket.h>
44#include <asm-generic/errno-base.h>
48#include <sys/syscall.h>
67 return ::WSAGetLastError() == WSAEINTR;
69 return errno == EINTR;
74 bool child_processes_inherit)
75 :
IOObject(eFDTypeSocket), m_protocol(protocol),
76 m_socket(kInvalidSocketValue),
77 m_child_processes_inherit(child_processes_inherit),
78 m_should_close_fd(should_close) {}
84 auto wVersion = WINSOCK_VERSION;
86 int err = ::WSAStartup(wVersion, &wsaData);
88 if (wsaData.wVersion < wVersion) {
90 return llvm::make_error<llvm::StringError>(
91 "WSASock version is not expected.", llvm::inconvertibleErrorCode());
94 return llvm::errorCodeToError(llvm::mapWindowsError(::WSAGetLastError()));
98 return llvm::Error::success();
108 bool child_processes_inherit,
112 std::unique_ptr<Socket> socket_up;
116 std::make_unique<TCPSocket>(
true, child_processes_inherit);
120 std::make_unique<UDPSocket>(
true, child_processes_inherit);
125 std::make_unique<DomainSocket>(
true, child_processes_inherit);
127 error.SetErrorString(
128 "Unix domain sockets are not supported on this platform.");
134 std::make_unique<AbstractSocket>(child_processes_inherit);
136 error.SetErrorString(
137 "Abstract domain sockets are not supported on this platform.");
148llvm::Expected<std::unique_ptr<Socket>>
150 bool child_processes_inherit) {
152 LLDB_LOG(log,
"host_and_port = {0}", host_and_port);
155 std::unique_ptr<Socket> connect_socket(
158 return error.ToError();
160 error = connect_socket->Connect(host_and_port);
162 return std::move(connect_socket);
164 return error.ToError();
167llvm::Expected<std::unique_ptr<TCPSocket>>
171 LLDB_LOG(log,
"host_and_port = {0}", host_and_port);
173 std::unique_ptr<TCPSocket> listen_socket(
174 new TCPSocket(
true, child_processes_inherit));
176 Status error = listen_socket->Listen(host_and_port, backlog);
178 return error.ToError();
180 return std::move(listen_socket);
183llvm::Expected<std::unique_ptr<UDPSocket>>
185 bool child_processes_inherit) {
190 static llvm::Regex g_regex(
"([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)");
192 llvm::SmallVector<llvm::StringRef, 3> matches;
193 if (g_regex.match(host_and_port, &matches)) {
198 if (to_integer(matches[2], ret.
port, 10))
203 if (to_integer(host_and_port, ret.
port, 10))
207 return llvm::createStringError(llvm::inconvertibleErrorCode(),
208 "invalid host:port specification: '%s'",
209 host_and_port.str().c_str());
219 int bytes_received = 0;
221 bytes_received = ::recv(
m_socket,
static_cast<char *
>(buf), num_bytes, 0);
224 if (bytes_received < 0) {
228 num_bytes = bytes_received;
233 "%p Socket::Read() (socket = %" PRIu64
234 ", src = %p, src_len = %" PRIu64
", flags = 0) => %" PRIi64
236 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket), buf,
237 static_cast<uint64_t
>(num_bytes),
238 static_cast<int64_t
>(bytes_received),
error.AsCString());
245 const size_t src_len = num_bytes;
249 bytes_sent =
Send(buf, num_bytes);
252 if (bytes_sent < 0) {
256 num_bytes = bytes_sent;
261 "%p Socket::Write() (socket = %" PRIu64
262 ", src = %p, src_len = %" PRIu64
", flags = 0) => %" PRIi64
264 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket), buf,
265 static_cast<uint64_t
>(src_len),
266 static_cast<int64_t
>(bytes_sent),
error.AsCString());
278 LLDB_LOGF(log,
"%p Socket::Close (fd = %" PRIu64
")",
279 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket));
282 bool success = closesocket(
m_socket) == 0;
284 bool success = ::close(
m_socket) == 0;
298 socklen_t option_value_size =
sizeof(int);
299 return ::getsockopt(
m_socket, level, option_name, option_value_p,
306 return ::setsockopt(
m_socket, level, option_name, option_value_p,
307 sizeof(option_value));
311 return ::send(
m_socket,
static_cast<const char *
>(buf), num_bytes, 0);
318 error.SetErrorToErrno();
326 auto socket_type = type;
328 if (!child_processes_inherit)
329 socket_type |= SOCK_CLOEXEC;
331 auto sock = ::socket(domain, socket_type, protocol);
342#if defined(ANDROID_USE_ACCEPT_WORKAROUND)
349 int fd = syscall(__NR_accept, sockfd, addr, addrlen);
350 if (fd >= 0 && !child_processes_inherit) {
351 int flags = ::fcntl(fd, F_GETFD);
352 if (flags != -1 && ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1)
358#elif defined(SOCK_CLOEXEC) && defined(HAVE_ACCEPT4)
360 if (!child_processes_inherit) {
361 flags |= SOCK_CLOEXEC;
364 static_cast<NativeSocket>(-1), ::accept4, sockfd, addr, addrlen, flags);
367 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)
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 Mangled &obj)
@ eErrorTypeWin32
Standard Win32 error codes.