11#include "lldb/Host/Config.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringExtras.h"
22#include "llvm/Support/Errno.h"
23#include "llvm/Support/Error.h"
24#include "llvm/Support/Regex.h"
25#include "llvm/Support/WindowsError.h"
32#include <netinet/in.h>
33#include <netinet/tcp.h>
34#include <sys/socket.h>
45#include <asm-generic/errno-base.h>
49#include <sys/syscall.h>
70 return ::WSAGetLastError() == WSAEINTR;
72 return errno == EINTR;
82 error = m_socket_pipe.CreateNew(
true);
86 m_fd = m_socket_pipe.GetReadPipe();
96 m_socket_pipe.CloseReadFileDescriptor();
98 WSAPROTOCOL_INFO protocol_info;
99 if (::WSADuplicateSocket(m_socket, child_pid, &protocol_info) ==
101 int last_error = ::WSAGetLastError();
103 "WSADuplicateSocket() failed, error: %d", last_error);
108 m_socket_pipe.WriteWithTimeout(&protocol_info,
sizeof(protocol_info),
109 std::chrono::seconds(10), num_bytes);
112 if (num_bytes !=
sizeof(protocol_info))
114 "WriteWithTimeout(WSAPROTOCOL_INFO) failed: {0} bytes", num_bytes);
123 WSAPROTOCOL_INFO protocol_info;
129 std::chrono::seconds(10), num_bytes);
132 if (num_bytes !=
sizeof(protocol_info)) {
134 "socket_pipe.ReadWithTimeout(WSAPROTOCOL_INFO) failed: {0} bytes",
138 socket = ::WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
139 FROM_PROTOCOL_INFO, &protocol_info, 0, 0);
140 if (socket == INVALID_SOCKET) {
142 "WSASocket(FROM_PROTOCOL_INFO) failed: error {0}", ::WSAGetLastError());
166 if (s.m_protocol == protocol)
175 if (!strcmp(s.m_scheme, scheme)) {
176 protocol = s.m_protocol;
184 :
IOObject(eFDTypeSocket), m_protocol(protocol),
185 m_socket(kInvalidSocketValue), m_should_close_fd(should_close) {}
191 auto wVersion = WINSOCK_VERSION;
193 int err = ::WSAStartup(wVersion, &wsaData);
195 if (wsaData.wVersion < wVersion) {
197 return llvm::createStringError(
"WSASock version is not expected.");
200 return llvm::errorCodeToError(llvm::mapWindowsError(::WSAGetLastError()));
204 return llvm::Error::success();
217 const bool should_close =
true;
218 std::unique_ptr<Socket> socket_up;
221 socket_up = std::make_unique<TCPSocket>(should_close);
224 socket_up = std::make_unique<UDPSocket>(should_close);
228 socket_up = std::make_unique<DomainSocket>(should_close);
231 "Unix domain sockets are not supported on this platform.");
236 socket_up = std::make_unique<AbstractSocket>();
239 "Abstract domain sockets are not supported on this platform.");
250llvm::Expected<std::unique_ptr<Socket>>
253 LLDB_LOG(log,
"host_and_port = {0}", host_and_port);
258 return error.ToError();
260 error = connect_socket->Connect(host_and_port);
262 return std::move(connect_socket);
264 return error.ToError();
267llvm::Expected<std::unique_ptr<TCPSocket>>
270 LLDB_LOG(log,
"host_and_port = {0}", host_and_port);
272 std::unique_ptr<TCPSocket> listen_socket(
275 Status error = listen_socket->Listen(host_and_port, backlog);
277 return error.ToError();
279 return std::move(listen_socket);
282llvm::Expected<std::unique_ptr<UDPSocket>>
288 static llvm::Regex g_regex(
"([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)");
290 llvm::SmallVector<llvm::StringRef, 3> matches;
291 if (g_regex.match(host_and_port, &matches)) {
296 if (to_integer(matches[2], ret.
port, 10))
301 if (to_integer(host_and_port, ret.
port, 10))
305 return llvm::createStringError(llvm::inconvertibleErrorCode(),
306 "invalid host:port specification: '%s'",
307 host_and_port.str().c_str());
317 int bytes_received = 0;
319 bytes_received = ::recv(
m_socket,
static_cast<char *
>(buf), num_bytes, 0);
322 if (bytes_received < 0) {
326 num_bytes = bytes_received;
331 "%p Socket::Read() (socket = %" PRIu64
332 ", src = %p, src_len = %" PRIu64
", flags = 0) => %" PRIi64
334 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket), buf,
335 static_cast<uint64_t
>(num_bytes),
336 static_cast<int64_t
>(bytes_received),
error.AsCString());
343 const size_t src_len = num_bytes;
347 bytes_sent =
Send(buf, num_bytes);
350 if (bytes_sent < 0) {
354 num_bytes = bytes_sent;
359 "%p Socket::Write() (socket = %" PRIu64
360 ", src = %p, src_len = %" PRIu64
", flags = 0) => %" PRIi64
362 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket), buf,
363 static_cast<uint64_t
>(src_len),
364 static_cast<int64_t
>(bytes_sent),
error.AsCString());
376 LLDB_LOGF(log,
"%p Socket::Close (fd = %" PRIu64
")",
377 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket));
393 socklen_t option_value_size =
sizeof(int);
394 return ::getsockopt(sockfd, level, option_name, option_value_p,
402 return ::setsockopt(sockfd, level, option_name, option_value_p,
403 sizeof(option_value));
407 return ::send(
m_socket,
static_cast<const char *
>(buf), num_bytes, 0);
421 EC = llvm::mapWindowsError(WSAGetLastError());
423 EC = std::error_code(errno, std::generic_category());
430 return ::closesocket(sockfd);
432 return ::close(sockfd);
439 auto socket_type = type;
441 socket_type |= SOCK_CLOEXEC;
443 auto sock = ::socket(domain, socket_type, protocol);
453 llvm::Expected<std::vector<MainLoopBase::ReadHandleUP>> expected_handles =
455 [&accept_loop, &socket](std::unique_ptr<Socket> sock) {
456 socket = sock.release();
459 if (!expected_handles)
469 return Status(std::make_error_code(std::errc::timed_out));
475#if defined(ANDROID_USE_ACCEPT_WORKAROUND)
482 int fd = syscall(__NR_accept, sockfd, addr, addrlen);
484 int flags = ::fcntl(fd, F_GETFD);
485 if (flags != -1 && ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1)
491#elif defined(SOCK_CLOEXEC) && defined(HAVE_ACCEPT4)
492 int flags = SOCK_CLOEXEC;
494 static_cast<NativeSocket>(-1), ::accept4, sockfd, addr, addrlen, flags);
497 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[]
void AddCallback(const Callback &callback, std::chrono::nanoseconds delay)
virtual void RequestTermination()
A posix-based implementation of Pipe, a class that abtracts unix style pipes.
Status ReadWithTimeout(void *buf, size_t size, const std::chrono::microseconds &timeout, size_t &bytes_read) override
static const shared_fd_t kInvalidFD
SharedSocket(const Socket *socket, Status &error)
Status CompleteSending(lldb::pid_t child_pid)
static Status GetNativeSocket(shared_fd_t fd, NativeSocket &socket)
static std::unique_ptr< Socket > Create(const SocketProtocol protocol, Status &error)
Status Read(void *buf, size_t &num_bytes) override
NativeSocket GetNativeSocket() const
static const NativeSocket kInvalidSocketValue
WaitableHandle GetWaitableHandle() override
virtual size_t Send(const void *buf, const size_t num_bytes)
static llvm::Expected< std::unique_ptr< UDPSocket > > UdpConnect(llvm::StringRef host_and_port)
virtual llvm::Expected< std::vector< MainLoopBase::ReadHandleUP > > Accept(MainLoopBase &loop, std::function< void(std::unique_ptr< Socket > socket)> sock_cb)=0
static NativeSocket CreateSocket(const int domain, const int type, const int protocol, Status &error)
static int GetOption(NativeSocket sockfd, int level, int option_name, int &option_value)
static llvm::Expected< HostAndPort > DecodeHostAndPort(llvm::StringRef host_and_port)
static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr, socklen_t *addrlen, Status &error)
static bool FindProtocolByScheme(const char *scheme, SocketProtocol &protocol)
bool IsValid() const override
static Status GetLastError()
static llvm::Expected< std::unique_ptr< Socket > > TcpConnect(llvm::StringRef host_and_port)
static void SetLastError(Status &error)
static llvm::Error Initialize()
static int CloseSocket(NativeSocket sockfd)
static const char * FindSchemeByProtocol(const SocketProtocol protocol)
static int SetOption(NativeSocket sockfd, int level, int option_name, int option_value)
Status Write(const void *buf, size_t &num_bytes) override
static llvm::Expected< std::unique_ptr< TCPSocket > > TcpListen(llvm::StringRef host_and_port, int backlog=5)
Socket(SocketProtocol protocol, bool should_close)
static Status FromErrno()
Set the current error to errno.
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status FromErrorString(const char *str)
bool Fail() const
Test for error condition.
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
static llvm::Expected< std::unique_ptr< UDPSocket > > CreateConnected(llvm::StringRef name)
#define LLDB_INVALID_PIPE
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