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;
88 if (s.m_protocol == protocol)
97 if (!strcmp(s.m_scheme, scheme)) {
98 protocol = s.m_protocol;
106 bool child_processes_inherit)
107 :
IOObject(eFDTypeSocket), m_protocol(protocol),
108 m_socket(kInvalidSocketValue),
109 m_child_processes_inherit(child_processes_inherit),
110 m_should_close_fd(should_close) {}
116 auto wVersion = WINSOCK_VERSION;
118 int err = ::WSAStartup(wVersion, &wsaData);
120 if (wsaData.wVersion < wVersion) {
122 return llvm::createStringError(
"WSASock version is not expected.");
125 return llvm::errorCodeToError(llvm::mapWindowsError(::WSAGetLastError()));
129 return llvm::Error::success();
139 bool child_processes_inherit,
143 std::unique_ptr<Socket> socket_up;
147 std::make_unique<TCPSocket>(
true, child_processes_inherit);
151 std::make_unique<UDPSocket>(
true, child_processes_inherit);
156 std::make_unique<DomainSocket>(
true, child_processes_inherit);
158 error.SetErrorString(
159 "Unix domain sockets are not supported on this platform.");
165 std::make_unique<AbstractSocket>(child_processes_inherit);
167 error.SetErrorString(
168 "Abstract domain sockets are not supported on this platform.");
179llvm::Expected<std::unique_ptr<Socket>>
181 bool child_processes_inherit) {
183 LLDB_LOG(log,
"host_and_port = {0}", host_and_port);
186 std::unique_ptr<Socket> connect_socket(
189 return error.ToError();
191 error = connect_socket->Connect(host_and_port);
193 return std::move(connect_socket);
195 return error.ToError();
198llvm::Expected<std::unique_ptr<TCPSocket>>
202 LLDB_LOG(log,
"host_and_port = {0}", host_and_port);
204 std::unique_ptr<TCPSocket> listen_socket(
205 new TCPSocket(
true, child_processes_inherit));
207 Status error = listen_socket->Listen(host_and_port, backlog);
209 return error.ToError();
211 return std::move(listen_socket);
214llvm::Expected<std::unique_ptr<UDPSocket>>
216 bool child_processes_inherit) {
221 static llvm::Regex g_regex(
"([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)");
223 llvm::SmallVector<llvm::StringRef, 3> matches;
224 if (g_regex.match(host_and_port, &matches)) {
229 if (to_integer(matches[2], ret.
port, 10))
234 if (to_integer(host_and_port, ret.
port, 10))
238 return llvm::createStringError(llvm::inconvertibleErrorCode(),
239 "invalid host:port specification: '%s'",
240 host_and_port.str().c_str());
250 int bytes_received = 0;
252 bytes_received = ::recv(
m_socket,
static_cast<char *
>(buf), num_bytes, 0);
255 if (bytes_received < 0) {
259 num_bytes = bytes_received;
264 "%p Socket::Read() (socket = %" PRIu64
265 ", src = %p, src_len = %" PRIu64
", flags = 0) => %" PRIi64
267 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket), buf,
268 static_cast<uint64_t
>(num_bytes),
269 static_cast<int64_t
>(bytes_received),
error.AsCString());
276 const size_t src_len = num_bytes;
280 bytes_sent =
Send(buf, num_bytes);
283 if (bytes_sent < 0) {
287 num_bytes = bytes_sent;
292 "%p Socket::Write() (socket = %" PRIu64
293 ", src = %p, src_len = %" PRIu64
", flags = 0) => %" PRIi64
295 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket), buf,
296 static_cast<uint64_t
>(src_len),
297 static_cast<int64_t
>(bytes_sent),
error.AsCString());
309 LLDB_LOGF(log,
"%p Socket::Close (fd = %" PRIu64
")",
310 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket));
313 bool success = closesocket(
m_socket) == 0;
315 bool success = ::close(
m_socket) == 0;
329 socklen_t option_value_size =
sizeof(int);
330 return ::getsockopt(
m_socket, level, option_name, option_value_p,
337 return ::setsockopt(
m_socket, level, option_name, option_value_p,
338 sizeof(option_value));
342 return ::send(
m_socket,
static_cast<const char *
>(buf), num_bytes, 0);
349 error.SetErrorToErrno();
357 auto socket_type = type;
359 if (!child_processes_inherit)
360 socket_type |= SOCK_CLOEXEC;
362 auto sock = ::socket(domain, socket_type, protocol);
373#if defined(ANDROID_USE_ACCEPT_WORKAROUND)
380 int fd = syscall(__NR_accept, sockfd, addr, addrlen);
381 if (fd >= 0 && !child_processes_inherit) {
382 int flags = ::fcntl(fd, F_GETFD);
383 if (flags != -1 && ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1)
389#elif defined(SOCK_CLOEXEC) && defined(HAVE_ACCEPT4)
391 if (!child_processes_inherit) {
392 flags |= SOCK_CLOEXEC;
395 static_cast<NativeSocket>(-1), ::accept4, sockfd, addr, addrlen, flags);
398 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
static SocketScheme socket_schemes[]
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 bool FindProtocolByScheme(const char *scheme, SocketProtocol &protocol)
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 const char * FindSchemeByProtocol(const SocketProtocol protocol)
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.
const Socket::SocketProtocol m_protocol