2016-08-11 06:43:48 +08:00
|
|
|
//===-- SelectHelper.cpp ----------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
// Enable this special support for Apple builds where we can have unlimited
|
|
|
|
// select bounds. We tried switching to poll() and kqueue and we were panicing
|
|
|
|
// the kernel, so we have to stick with select for now.
|
|
|
|
#define _DARWIN_UNLIMITED_SELECT
|
|
|
|
#endif
|
|
|
|
|
2017-04-07 02:12:24 +08:00
|
|
|
#include "lldb/Utility/SelectHelper.h"
|
|
|
|
#include "lldb/Utility/LLDBAssert.h"
|
2017-05-12 12:51:55 +08:00
|
|
|
#include "lldb/Utility/Status.h"
|
2017-04-07 02:12:24 +08:00
|
|
|
#include "lldb/lldb-enumerations.h" // for ErrorType::eErrorTypePOSIX
|
|
|
|
#include "lldb/lldb-types.h" // for socket_t
|
|
|
|
|
|
|
|
#include "llvm/ADT/DenseMap.h" // for DenseMapPair, DenseMap, Dense...
|
|
|
|
#include "llvm/ADT/Optional.h" // for Optional
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <chrono> // for microseconds, seconds, steady...
|
|
|
|
|
2016-08-11 06:43:48 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#if defined(_WIN32)
|
|
|
|
// Define NOMINMAX to avoid macros that conflict with std::min and std::max
|
|
|
|
#define NOMINMAX
|
|
|
|
#include <winsock2.h>
|
|
|
|
#else
|
2017-12-13 04:19:40 +08:00
|
|
|
#include <sys/time.h>
|
2016-08-11 06:43:48 +08:00
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
SelectHelper::SelectHelper()
|
|
|
|
: m_fd_map(), m_end_time() // Infinite timeout unless
|
|
|
|
// SelectHelper::SetTimeout() gets called
|
|
|
|
{}
|
2016-08-11 06:43:48 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void SelectHelper::SetTimeout(const std::chrono::microseconds &timeout) {
|
|
|
|
using namespace std::chrono;
|
|
|
|
m_end_time = steady_clock::time_point(steady_clock::now() + timeout);
|
2016-08-11 06:43:48 +08:00
|
|
|
}
|
|
|
|
|
2016-10-06 01:07:34 +08:00
|
|
|
void SelectHelper::FDSetRead(lldb::socket_t fd) {
|
|
|
|
m_fd_map[fd].read_set = true;
|
|
|
|
}
|
2016-08-11 06:43:48 +08:00
|
|
|
|
2016-10-06 01:07:34 +08:00
|
|
|
void SelectHelper::FDSetWrite(lldb::socket_t fd) {
|
|
|
|
m_fd_map[fd].write_set = true;
|
|
|
|
}
|
2016-08-11 06:43:48 +08:00
|
|
|
|
2016-10-06 01:07:34 +08:00
|
|
|
void SelectHelper::FDSetError(lldb::socket_t fd) {
|
|
|
|
m_fd_map[fd].error_set = true;
|
|
|
|
}
|
2016-08-11 06:43:48 +08:00
|
|
|
|
2016-10-06 01:07:34 +08:00
|
|
|
bool SelectHelper::FDIsSetRead(lldb::socket_t fd) const {
|
2016-09-07 04:57:50 +08:00
|
|
|
auto pos = m_fd_map.find(fd);
|
|
|
|
if (pos != m_fd_map.end())
|
|
|
|
return pos->second.read_is_set;
|
|
|
|
else
|
|
|
|
return false;
|
2016-08-11 06:43:48 +08:00
|
|
|
}
|
|
|
|
|
2016-10-06 01:07:34 +08:00
|
|
|
bool SelectHelper::FDIsSetWrite(lldb::socket_t fd) const {
|
2016-09-07 04:57:50 +08:00
|
|
|
auto pos = m_fd_map.find(fd);
|
|
|
|
if (pos != m_fd_map.end())
|
|
|
|
return pos->second.write_is_set;
|
|
|
|
else
|
|
|
|
return false;
|
2016-08-11 06:43:48 +08:00
|
|
|
}
|
|
|
|
|
2016-10-06 01:07:34 +08:00
|
|
|
bool SelectHelper::FDIsSetError(lldb::socket_t fd) const {
|
2016-09-07 04:57:50 +08:00
|
|
|
auto pos = m_fd_map.find(fd);
|
|
|
|
if (pos != m_fd_map.end())
|
|
|
|
return pos->second.error_is_set;
|
|
|
|
else
|
|
|
|
return false;
|
2016-08-11 06:43:48 +08:00
|
|
|
}
|
|
|
|
|
2016-10-06 01:07:34 +08:00
|
|
|
static void updateMaxFd(llvm::Optional<lldb::socket_t> &vold,
|
|
|
|
lldb::socket_t vnew) {
|
|
|
|
if (!vold.hasValue())
|
|
|
|
vold = vnew;
|
|
|
|
else
|
|
|
|
vold = std::max(*vold, vnew);
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
lldb_private::Status SelectHelper::Select() {
|
|
|
|
lldb_private::Status error;
|
2016-08-12 19:20:21 +08:00
|
|
|
#ifdef _MSC_VER
|
2016-09-07 04:57:50 +08:00
|
|
|
// On windows FD_SETSIZE limits the number of file descriptors, not their
|
|
|
|
// numeric value.
|
|
|
|
lldbassert(m_fd_map.size() <= FD_SETSIZE);
|
|
|
|
if (m_fd_map.size() > FD_SETSIZE)
|
2017-05-12 12:51:55 +08:00
|
|
|
return lldb_private::Status("Too many file descriptors for select()");
|
2016-08-12 19:20:21 +08:00
|
|
|
#endif
|
2016-08-11 06:43:48 +08:00
|
|
|
|
2016-10-06 01:07:34 +08:00
|
|
|
llvm::Optional<lldb::socket_t> max_read_fd;
|
|
|
|
llvm::Optional<lldb::socket_t> max_write_fd;
|
|
|
|
llvm::Optional<lldb::socket_t> max_error_fd;
|
|
|
|
llvm::Optional<lldb::socket_t> max_fd;
|
2016-09-07 04:57:50 +08:00
|
|
|
for (auto &pair : m_fd_map) {
|
|
|
|
pair.second.PrepareForSelect();
|
2016-10-06 01:07:34 +08:00
|
|
|
const lldb::socket_t fd = pair.first;
|
2016-08-12 19:20:21 +08:00
|
|
|
#if !defined(__APPLE__) && !defined(_MSC_VER)
|
2018-06-26 00:10:20 +08:00
|
|
|
lldbassert(fd < static_cast<int>(FD_SETSIZE));
|
|
|
|
if (fd >= static_cast<int>(FD_SETSIZE)) {
|
2016-09-07 04:57:50 +08:00
|
|
|
error.SetErrorStringWithFormat("%i is too large for select()", fd);
|
|
|
|
return error;
|
|
|
|
}
|
2016-08-11 06:43:48 +08:00
|
|
|
#endif
|
2016-10-06 01:07:34 +08:00
|
|
|
if (pair.second.read_set)
|
|
|
|
updateMaxFd(max_read_fd, fd);
|
|
|
|
if (pair.second.write_set)
|
|
|
|
updateMaxFd(max_write_fd, fd);
|
|
|
|
if (pair.second.error_set)
|
|
|
|
updateMaxFd(max_error_fd, fd);
|
|
|
|
updateMaxFd(max_fd, fd);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2016-08-11 06:43:48 +08:00
|
|
|
|
2016-10-06 01:07:34 +08:00
|
|
|
if (!max_fd.hasValue()) {
|
2016-09-07 04:57:50 +08:00
|
|
|
error.SetErrorString("no valid file descriptors");
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2016-10-06 01:07:34 +08:00
|
|
|
const unsigned nfds = static_cast<unsigned>(*max_fd) + 1;
|
2016-09-07 04:57:50 +08:00
|
|
|
fd_set *read_fdset_ptr = nullptr;
|
|
|
|
fd_set *write_fdset_ptr = nullptr;
|
|
|
|
fd_set *error_fdset_ptr = nullptr;
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Initialize and zero out the fdsets
|
|
|
|
//----------------------------------------------------------------------
|
2016-08-11 06:43:48 +08:00
|
|
|
#if defined(__APPLE__)
|
2016-09-07 04:57:50 +08:00
|
|
|
llvm::SmallVector<fd_set, 1> read_fdset;
|
|
|
|
llvm::SmallVector<fd_set, 1> write_fdset;
|
|
|
|
llvm::SmallVector<fd_set, 1> error_fdset;
|
|
|
|
|
2016-10-06 01:07:34 +08:00
|
|
|
if (max_read_fd.hasValue()) {
|
2016-09-07 04:57:50 +08:00
|
|
|
read_fdset.resize((nfds / FD_SETSIZE) + 1);
|
|
|
|
read_fdset_ptr = read_fdset.data();
|
|
|
|
}
|
2016-10-06 01:07:34 +08:00
|
|
|
if (max_write_fd.hasValue()) {
|
2016-09-07 04:57:50 +08:00
|
|
|
write_fdset.resize((nfds / FD_SETSIZE) + 1);
|
|
|
|
write_fdset_ptr = write_fdset.data();
|
|
|
|
}
|
2016-10-06 01:07:34 +08:00
|
|
|
if (max_error_fd.hasValue()) {
|
2016-09-07 04:57:50 +08:00
|
|
|
error_fdset.resize((nfds / FD_SETSIZE) + 1);
|
|
|
|
error_fdset_ptr = error_fdset.data();
|
|
|
|
}
|
|
|
|
for (auto &fd_set : read_fdset)
|
|
|
|
FD_ZERO(&fd_set);
|
|
|
|
for (auto &fd_set : write_fdset)
|
|
|
|
FD_ZERO(&fd_set);
|
|
|
|
for (auto &fd_set : error_fdset)
|
|
|
|
FD_ZERO(&fd_set);
|
2016-08-11 06:43:48 +08:00
|
|
|
#else
|
2016-09-07 04:57:50 +08:00
|
|
|
fd_set read_fdset;
|
|
|
|
fd_set write_fdset;
|
|
|
|
fd_set error_fdset;
|
|
|
|
|
2016-10-06 01:07:34 +08:00
|
|
|
if (max_read_fd.hasValue()) {
|
2016-09-07 04:57:50 +08:00
|
|
|
FD_ZERO(&read_fdset);
|
|
|
|
read_fdset_ptr = &read_fdset;
|
|
|
|
}
|
2016-10-06 01:07:34 +08:00
|
|
|
if (max_write_fd.hasValue()) {
|
2016-09-07 04:57:50 +08:00
|
|
|
FD_ZERO(&write_fdset);
|
|
|
|
write_fdset_ptr = &write_fdset;
|
|
|
|
}
|
2016-10-06 01:07:34 +08:00
|
|
|
if (max_error_fd.hasValue()) {
|
2016-09-07 04:57:50 +08:00
|
|
|
FD_ZERO(&error_fdset);
|
|
|
|
error_fdset_ptr = &error_fdset;
|
|
|
|
}
|
2016-08-11 06:43:48 +08:00
|
|
|
#endif
|
2016-09-07 04:57:50 +08:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Set the FD bits in the fdsets for read/write/error
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
for (auto &pair : m_fd_map) {
|
2016-10-06 01:07:34 +08:00
|
|
|
const lldb::socket_t fd = pair.first;
|
2016-08-11 06:43:48 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (pair.second.read_set)
|
|
|
|
FD_SET(fd, read_fdset_ptr);
|
2016-08-11 06:43:48 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (pair.second.write_set)
|
|
|
|
FD_SET(fd, write_fdset_ptr);
|
2016-08-11 06:43:48 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (pair.second.error_set)
|
|
|
|
FD_SET(fd, error_fdset_ptr);
|
|
|
|
}
|
2016-08-11 06:43:48 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Setup our timeout time value if needed
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
struct timeval *tv_ptr = nullptr;
|
|
|
|
struct timeval tv = {0, 0};
|
2016-08-11 06:43:48 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
while (1) {
|
|
|
|
using namespace std::chrono;
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Setup out relative timeout based on the end time if we have one
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
if (m_end_time.hasValue()) {
|
|
|
|
tv_ptr = &tv;
|
|
|
|
const auto remaining_dur = duration_cast<microseconds>(
|
|
|
|
m_end_time.getValue() - steady_clock::now());
|
|
|
|
if (remaining_dur.count() > 0) {
|
|
|
|
// Wait for a specific amount of time
|
|
|
|
const auto dur_secs = duration_cast<seconds>(remaining_dur);
|
|
|
|
const auto dur_usecs = remaining_dur % seconds(1);
|
|
|
|
tv.tv_sec = dur_secs.count();
|
|
|
|
tv.tv_usec = dur_usecs.count();
|
|
|
|
} else {
|
|
|
|
// Just poll once with no timeout
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const int num_set_fds = ::select(nfds, read_fdset_ptr, write_fdset_ptr,
|
|
|
|
error_fdset_ptr, tv_ptr);
|
|
|
|
if (num_set_fds < 0) {
|
|
|
|
// We got an error
|
|
|
|
error.SetErrorToErrno();
|
|
|
|
if (error.GetError() == EINTR) {
|
|
|
|
error.Clear();
|
|
|
|
continue; // Keep calling select if we get EINTR
|
|
|
|
} else
|
|
|
|
return error;
|
|
|
|
} else if (num_set_fds == 0) {
|
|
|
|
// Timeout
|
|
|
|
error.SetError(ETIMEDOUT, lldb::eErrorTypePOSIX);
|
|
|
|
error.SetErrorString("timed out");
|
|
|
|
return error;
|
|
|
|
} else {
|
2018-05-01 00:49:04 +08:00
|
|
|
// One or more descriptors were set, update the FDInfo::select_is_set
|
|
|
|
// mask so users can ask the SelectHelper class so clients can call one
|
|
|
|
// of:
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
for (auto &pair : m_fd_map) {
|
|
|
|
const int fd = pair.first;
|
|
|
|
|
|
|
|
if (pair.second.read_set) {
|
|
|
|
if (FD_ISSET(fd, read_fdset_ptr))
|
|
|
|
pair.second.read_is_set = true;
|
2016-08-11 06:43:48 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
if (pair.second.write_set) {
|
|
|
|
if (FD_ISSET(fd, write_fdset_ptr))
|
|
|
|
pair.second.write_is_set = true;
|
2016-08-11 06:43:48 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
if (pair.second.error_set) {
|
|
|
|
if (FD_ISSET(fd, error_fdset_ptr))
|
|
|
|
pair.second.error_is_set = true;
|
2016-08-11 06:43:48 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
break;
|
2016-08-11 06:43:48 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
return error;
|
2016-08-11 06:43:48 +08:00
|
|
|
}
|