1 //===-- MainLoop.cpp --------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Config/llvm-config.h" 11 12 #include "lldb/Host/MainLoop.h" 13 #include "lldb/Utility/Error.h" 14 #include <algorithm> 15 #include <cassert> 16 #include <cerrno> 17 #include <csignal> 18 #include <vector> 19 #include <time.h> 20 21 // Multiplexing is implemented using kqueue on systems that support it (BSD 22 // variants including OSX). On linux we use ppoll, while android uses pselect 23 // (ppoll is present but not implemented properly). On windows we use WSApoll 24 // (which does not support signals). 25 26 #if HAVE_SYS_EVENT_H 27 #include <sys/event.h> 28 #elif defined(LLVM_ON_WIN32) 29 #include <winsock2.h> 30 #else 31 #include <poll.h> 32 #endif 33 34 #ifdef LLVM_ON_WIN32 35 #define POLL WSAPoll 36 #else 37 #define POLL poll 38 #endif 39 40 #ifdef __ANDROID__ 41 #define FORCE_PSELECT 42 #endif 43 44 #if SIGNAL_POLLING_UNSUPPORTED 45 #ifdef LLVM_ON_WIN32 46 typedef int sigset_t; 47 typedef int siginfo_t; 48 #endif 49 50 int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout_ts, 51 const sigset_t *) { 52 int timeout = 53 (timeout_ts == nullptr) 54 ? -1 55 : (timeout_ts->tv_sec * 1000 + timeout_ts->tv_nsec / 1000000); 56 return POLL(fds, nfds, timeout); 57 } 58 59 #endif 60 61 using namespace lldb; 62 using namespace lldb_private; 63 64 static sig_atomic_t g_signal_flags[NSIG]; 65 66 static void SignalHandler(int signo, siginfo_t *info, void *) { 67 assert(signo < NSIG); 68 g_signal_flags[signo] = 1; 69 } 70 71 class MainLoop::RunImpl { 72 public: 73 RunImpl(MainLoop &loop); 74 ~RunImpl() = default; 75 76 Error Poll(); 77 void ProcessEvents(); 78 79 private: 80 MainLoop &loop; 81 82 #if HAVE_SYS_EVENT_H 83 std::vector<struct kevent> in_events; 84 struct kevent out_events[4]; 85 int num_events = -1; 86 87 #else 88 #ifdef FORCE_PSELECT 89 fd_set read_fd_set; 90 #else 91 std::vector<struct pollfd> read_fds; 92 #endif 93 94 sigset_t get_sigmask(); 95 #endif 96 }; 97 98 #if HAVE_SYS_EVENT_H 99 MainLoop::RunImpl::RunImpl(MainLoop &loop) : loop(loop) { 100 in_events.reserve(loop.m_read_fds.size()); 101 } 102 103 Error MainLoop::RunImpl::Poll() { 104 in_events.resize(loop.m_read_fds.size()); 105 unsigned i = 0; 106 for (auto &fd : loop.m_read_fds) 107 EV_SET(&in_events[i++], fd.first, EVFILT_READ, EV_ADD, 0, 0, 0); 108 109 num_events = kevent(loop.m_kqueue, in_events.data(), in_events.size(), 110 out_events, llvm::array_lengthof(out_events), nullptr); 111 112 if (num_events < 0) 113 return Error("kevent() failed with error %d\n", num_events); 114 return Error(); 115 } 116 117 void MainLoop::RunImpl::ProcessEvents() { 118 assert(num_events >= 0); 119 for (int i = 0; i < num_events; ++i) { 120 if (loop.m_terminate_request) 121 return; 122 switch (out_events[i].filter) { 123 case EVFILT_READ: 124 loop.ProcessReadObject(out_events[i].ident); 125 break; 126 case EVFILT_SIGNAL: 127 loop.ProcessSignal(out_events[i].ident); 128 break; 129 default: 130 llvm_unreachable("Unknown event"); 131 } 132 } 133 } 134 #else 135 MainLoop::RunImpl::RunImpl(MainLoop &loop) : loop(loop) { 136 #ifndef FORCE_PSELECT 137 read_fds.reserve(loop.m_read_fds.size()); 138 #endif 139 } 140 141 sigset_t MainLoop::RunImpl::get_sigmask() { 142 #if SIGNAL_POLLING_UNSUPPORTED 143 return 0; 144 #else 145 sigset_t sigmask; 146 int ret = pthread_sigmask(SIG_SETMASK, nullptr, &sigmask); 147 assert(ret == 0); 148 (void) ret; 149 150 for (const auto &sig : loop.m_signals) 151 sigdelset(&sigmask, sig.first); 152 return sigmask; 153 #endif 154 } 155 156 #ifdef FORCE_PSELECT 157 Error MainLoop::RunImpl::Poll() { 158 FD_ZERO(&read_fd_set); 159 int nfds = 0; 160 for (const auto &fd : loop.m_read_fds) { 161 FD_SET(fd.first, &read_fd_set); 162 nfds = std::max(nfds, fd.first + 1); 163 } 164 165 sigset_t sigmask = get_sigmask(); 166 if (pselect(nfds, &read_fd_set, nullptr, nullptr, nullptr, &sigmask) == -1 && 167 errno != EINTR) 168 return Error(errno, eErrorTypePOSIX); 169 170 return Error(); 171 } 172 #else 173 Error MainLoop::RunImpl::Poll() { 174 read_fds.clear(); 175 176 sigset_t sigmask = get_sigmask(); 177 178 for (const auto &fd : loop.m_read_fds) { 179 struct pollfd pfd; 180 pfd.fd = fd.first; 181 pfd.events = POLLIN; 182 pfd.revents = 0; 183 read_fds.push_back(pfd); 184 } 185 186 if (ppoll(read_fds.data(), read_fds.size(), nullptr, &sigmask) == -1 && 187 errno != EINTR) 188 return Error(errno, eErrorTypePOSIX); 189 190 return Error(); 191 } 192 #endif 193 194 void MainLoop::RunImpl::ProcessEvents() { 195 #ifdef FORCE_PSELECT 196 for (const auto &fd : loop.m_read_fds) { 197 if (!FD_ISSET(fd.first, &read_fd_set)) 198 continue; 199 IOObject::WaitableHandle handle = fd.first; 200 #else 201 for (const auto &fd : read_fds) { 202 if ((fd.revents & POLLIN) == 0) 203 continue; 204 IOObject::WaitableHandle handle = fd.fd; 205 #endif 206 if (loop.m_terminate_request) 207 return; 208 209 loop.ProcessReadObject(handle); 210 } 211 212 for (const auto &entry : loop.m_signals) { 213 if (loop.m_terminate_request) 214 return; 215 if (g_signal_flags[entry.first] == 0) 216 continue; // No signal 217 g_signal_flags[entry.first] = 0; 218 loop.ProcessSignal(entry.first); 219 } 220 } 221 #endif 222 223 MainLoop::MainLoop() { 224 #if HAVE_SYS_EVENT_H 225 m_kqueue = kqueue(); 226 assert(m_kqueue >= 0); 227 #endif 228 } 229 MainLoop::~MainLoop() { 230 #if HAVE_SYS_EVENT_H 231 close(m_kqueue); 232 #endif 233 assert(m_read_fds.size() == 0); 234 assert(m_signals.size() == 0); 235 } 236 237 MainLoop::ReadHandleUP 238 MainLoop::RegisterReadObject(const IOObjectSP &object_sp, 239 const Callback &callback, Error &error) { 240 #ifdef LLVM_ON_WIN32 241 if (object_sp->GetFdType() != IOObject:: eFDTypeSocket) { 242 error.SetErrorString("MainLoop: non-socket types unsupported on Windows"); 243 return nullptr; 244 } 245 #endif 246 if (!object_sp || !object_sp->IsValid()) { 247 error.SetErrorString("IO object is not valid."); 248 return nullptr; 249 } 250 251 const bool inserted = 252 m_read_fds.insert({object_sp->GetWaitableHandle(), callback}).second; 253 if (!inserted) { 254 error.SetErrorStringWithFormat("File descriptor %d already monitored.", 255 object_sp->GetWaitableHandle()); 256 return nullptr; 257 } 258 259 return CreateReadHandle(object_sp); 260 } 261 262 // We shall block the signal, then install the signal handler. The signal will 263 // be unblocked in 264 // the Run() function to check for signal delivery. 265 MainLoop::SignalHandleUP 266 MainLoop::RegisterSignal(int signo, const Callback &callback, 267 Error &error) { 268 #ifdef SIGNAL_POLLING_UNSUPPORTED 269 error.SetErrorString("Signal polling is not supported on this platform."); 270 return nullptr; 271 #else 272 if (m_signals.find(signo) != m_signals.end()) { 273 error.SetErrorStringWithFormat("Signal %d already monitored.", signo); 274 return nullptr; 275 } 276 277 SignalInfo info; 278 info.callback = callback; 279 struct sigaction new_action; 280 new_action.sa_sigaction = &SignalHandler; 281 new_action.sa_flags = SA_SIGINFO; 282 sigemptyset(&new_action.sa_mask); 283 sigaddset(&new_action.sa_mask, signo); 284 sigset_t old_set; 285 286 g_signal_flags[signo] = 0; 287 288 // Even if using kqueue, the signal handler will still be invoked, so it's 289 // important to replace it with our "bening" handler. 290 int ret = sigaction(signo, &new_action, &info.old_action); 291 assert(ret == 0 && "sigaction failed"); 292 293 #if HAVE_SYS_EVENT_H 294 struct kevent ev; 295 EV_SET(&ev, signo, EVFILT_SIGNAL, EV_ADD, 0, 0, 0); 296 ret = kevent(m_kqueue, &ev, 1, nullptr, 0, nullptr); 297 assert(ret == 0); 298 #endif 299 300 // If we're using kqueue, the signal needs to be unblocked in order to recieve 301 // it. If using pselect/ppoll, we need to block it, and later unblock it as a 302 // part of the system call. 303 ret = pthread_sigmask(HAVE_SYS_EVENT_H ? SIG_UNBLOCK : SIG_BLOCK, 304 &new_action.sa_mask, &old_set); 305 assert(ret == 0 && "pthread_sigmask failed"); 306 info.was_blocked = sigismember(&old_set, signo); 307 m_signals.insert({signo, info}); 308 309 return SignalHandleUP(new SignalHandle(*this, signo)); 310 #endif 311 } 312 313 void MainLoop::UnregisterReadObject(IOObject::WaitableHandle handle) { 314 bool erased = m_read_fds.erase(handle); 315 UNUSED_IF_ASSERT_DISABLED(erased); 316 assert(erased); 317 } 318 319 void MainLoop::UnregisterSignal(int signo) { 320 #if SIGNAL_POLLING_UNSUPPORTED 321 Error("Signal polling is not supported on this platform."); 322 #else 323 auto it = m_signals.find(signo); 324 assert(it != m_signals.end()); 325 326 sigaction(signo, &it->second.old_action, nullptr); 327 328 sigset_t set; 329 sigemptyset(&set); 330 sigaddset(&set, signo); 331 int ret = pthread_sigmask(it->second.was_blocked ? SIG_BLOCK : SIG_UNBLOCK, 332 &set, nullptr); 333 assert(ret == 0); 334 (void)ret; 335 336 #if HAVE_SYS_EVENT_H 337 struct kevent ev; 338 EV_SET(&ev, signo, EVFILT_SIGNAL, EV_DELETE, 0, 0, 0); 339 ret = kevent(m_kqueue, &ev, 1, nullptr, 0, nullptr); 340 assert(ret == 0); 341 #endif 342 343 m_signals.erase(it); 344 #endif 345 } 346 347 Error MainLoop::Run() { 348 m_terminate_request = false; 349 350 Error error; 351 RunImpl impl(*this); 352 353 // run until termination or until we run out of things to listen to 354 while (!m_terminate_request && (!m_read_fds.empty() || !m_signals.empty())) { 355 356 error = impl.Poll(); 357 if (error.Fail()) 358 return error; 359 360 impl.ProcessEvents(); 361 362 if (m_terminate_request) 363 return Error(); 364 } 365 return Error(); 366 } 367 368 void MainLoop::ProcessSignal(int signo) { 369 auto it = m_signals.find(signo); 370 if (it != m_signals.end()) 371 it->second.callback(*this); // Do the work 372 } 373 374 void MainLoop::ProcessReadObject(IOObject::WaitableHandle handle) { 375 auto it = m_read_fds.find(handle); 376 if (it != m_read_fds.end()) 377 it->second(*this); // Do the work 378 } 379