1 1.1 christos /* Event loop machinery for GDB, the GNU debugger. 2 1.1.1.3 christos Copyright (C) 1999-2024 Free Software Foundation, Inc. 3 1.1 christos Written by Elena Zannoni <ezannoni (at) cygnus.com> of Cygnus Solutions. 4 1.1 christos 5 1.1 christos This file is part of GDB. 6 1.1 christos 7 1.1 christos This program is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 3 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos This program is distributed in the hope that it will be useful, 13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 christos GNU General Public License for more details. 16 1.1 christos 17 1.1 christos You should have received a copy of the GNU General Public License 18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 1.1 christos 20 1.1 christos #include "gdbsupport/event-loop.h" 21 1.1 christos 22 1.1 christos #include <chrono> 23 1.1 christos 24 1.1 christos #ifdef HAVE_POLL 25 1.1 christos #if defined (HAVE_POLL_H) 26 1.1 christos #include <poll.h> 27 1.1 christos #elif defined (HAVE_SYS_POLL_H) 28 1.1 christos #include <sys/poll.h> 29 1.1 christos #endif 30 1.1 christos #endif 31 1.1 christos 32 1.1 christos #include <sys/types.h> 33 1.1 christos #include "gdbsupport/gdb_sys_time.h" 34 1.1 christos #include "gdbsupport/gdb_select.h" 35 1.1.1.3 christos #include <optional> 36 1.1.1.2 christos #include "gdbsupport/scope-exit.h" 37 1.1.1.2 christos 38 1.1.1.2 christos /* See event-loop.h. */ 39 1.1.1.2 christos 40 1.1.1.2 christos debug_event_loop_kind debug_event_loop; 41 1.1 christos 42 1.1 christos /* Tell create_file_handler what events we are interested in. 43 1.1 christos This is used by the select version of the event loop. */ 44 1.1 christos 45 1.1 christos #define GDB_READABLE (1<<1) 46 1.1 christos #define GDB_WRITABLE (1<<2) 47 1.1 christos #define GDB_EXCEPTION (1<<3) 48 1.1 christos 49 1.1 christos /* Information about each file descriptor we register with the event 50 1.1 christos loop. */ 51 1.1 christos 52 1.1.1.2 christos struct file_handler 53 1.1.1.2 christos { 54 1.1.1.2 christos /* File descriptor. */ 55 1.1.1.2 christos int fd; 56 1.1 christos 57 1.1.1.2 christos /* Events we want to monitor: POLLIN, etc. */ 58 1.1.1.2 christos int mask; 59 1.1.1.2 christos 60 1.1.1.2 christos /* Events that have been seen since the last time. */ 61 1.1.1.2 christos int ready_mask; 62 1.1.1.2 christos 63 1.1.1.2 christos /* Procedure to call when fd is ready. */ 64 1.1.1.2 christos handler_func *proc; 65 1.1.1.2 christos 66 1.1.1.2 christos /* Argument to pass to proc. */ 67 1.1.1.2 christos gdb_client_data client_data; 68 1.1 christos 69 1.1.1.2 christos /* User-friendly name of this handler. */ 70 1.1.1.2 christos std::string name; 71 1.1.1.2 christos 72 1.1.1.2 christos /* If set, this file descriptor is used for a user interface. */ 73 1.1.1.2 christos bool is_ui; 74 1.1.1.2 christos 75 1.1.1.2 christos /* Was an error detected on this fd? */ 76 1.1.1.2 christos int error; 77 1.1.1.2 christos 78 1.1.1.2 christos /* Next registered file descriptor. */ 79 1.1.1.2 christos struct file_handler *next_file; 80 1.1.1.2 christos }; 81 1.1.1.2 christos 82 1.1.1.2 christos #ifdef HAVE_POLL 83 1.1.1.2 christos /* Do we use poll or select? Some systems have poll, but then it's 84 1.1.1.2 christos not useable with all kinds of files. We probe that whenever a new 85 1.1.1.2 christos file handler is added. */ 86 1.1.1.2 christos static bool use_poll = true; 87 1.1.1.2 christos #endif 88 1.1 christos 89 1.1 christos #ifdef USE_WIN32API 90 1.1 christos #include <windows.h> 91 1.1 christos #include <io.h> 92 1.1 christos #endif 93 1.1 christos 94 1.1 christos /* Gdb_notifier is just a list of file descriptors gdb is interested in. 95 1.1 christos These are the input file descriptor, and the target file 96 1.1 christos descriptor. We have two flavors of the notifier, one for platforms 97 1.1 christos that have the POLL function, the other for those that don't, and 98 1.1 christos only support SELECT. Each of the elements in the gdb_notifier list is 99 1.1 christos basically a description of what kind of events gdb is interested 100 1.1 christos in, for each fd. */ 101 1.1 christos 102 1.1 christos static struct 103 1.1 christos { 104 1.1 christos /* Ptr to head of file handler list. */ 105 1.1 christos file_handler *first_file_handler; 106 1.1 christos 107 1.1 christos /* Next file handler to handle, for the select variant. To level 108 1.1 christos the fairness across event sources, we serve file handlers in a 109 1.1 christos round-robin-like fashion. The number and order of the polled 110 1.1 christos file handlers may change between invocations, but this is good 111 1.1 christos enough. */ 112 1.1 christos file_handler *next_file_handler; 113 1.1 christos 114 1.1 christos #ifdef HAVE_POLL 115 1.1.1.3 christos /* Descriptors to poll. */ 116 1.1.1.3 christos std::vector<struct pollfd> poll_fds; 117 1.1 christos 118 1.1 christos /* Next file descriptor to handle, for the poll variant. To level 119 1.1 christos the fairness across event sources, we poll the file descriptors 120 1.1 christos in a round-robin-like fashion. The number and order of the 121 1.1 christos polled file descriptors may change between invocations, but 122 1.1 christos this is good enough. */ 123 1.1 christos int next_poll_fds_index; 124 1.1 christos 125 1.1 christos /* Timeout in milliseconds for calls to poll(). */ 126 1.1 christos int poll_timeout; 127 1.1 christos #endif 128 1.1 christos 129 1.1 christos /* Masks to be used in the next call to select. 130 1.1 christos Bits are set in response to calls to create_file_handler. */ 131 1.1 christos fd_set check_masks[3]; 132 1.1 christos 133 1.1 christos /* What file descriptors were found ready by select. */ 134 1.1 christos fd_set ready_masks[3]; 135 1.1 christos 136 1.1 christos /* Number of file descriptors to monitor (for poll). */ 137 1.1 christos /* Number of valid bits (highest fd value + 1) (for select). */ 138 1.1 christos int num_fds; 139 1.1 christos 140 1.1 christos /* Time structure for calls to select(). */ 141 1.1 christos struct timeval select_timeout; 142 1.1 christos 143 1.1 christos /* Flag to tell whether the timeout should be used. */ 144 1.1 christos int timeout_valid; 145 1.1 christos } 146 1.1 christos gdb_notifier; 147 1.1 christos 148 1.1 christos /* Structure associated with a timer. PROC will be executed at the 149 1.1 christos first occasion after WHEN. */ 150 1.1 christos struct gdb_timer 151 1.1 christos { 152 1.1 christos std::chrono::steady_clock::time_point when; 153 1.1 christos int timer_id; 154 1.1 christos struct gdb_timer *next; 155 1.1 christos timer_handler_func *proc; /* Function to call to do the work. */ 156 1.1 christos gdb_client_data client_data; /* Argument to async_handler_func. */ 157 1.1 christos }; 158 1.1 christos 159 1.1 christos /* List of currently active timers. It is sorted in order of 160 1.1 christos increasing timers. */ 161 1.1 christos static struct 162 1.1 christos { 163 1.1 christos /* Pointer to first in timer list. */ 164 1.1 christos struct gdb_timer *first_timer; 165 1.1 christos 166 1.1 christos /* Id of the last timer created. */ 167 1.1 christos int num_timers; 168 1.1 christos } 169 1.1 christos timer_list; 170 1.1 christos 171 1.1 christos static void create_file_handler (int fd, int mask, handler_func *proc, 172 1.1.1.2 christos gdb_client_data client_data, 173 1.1.1.2 christos std::string &&name, bool is_ui); 174 1.1 christos static int gdb_wait_for_event (int); 175 1.1 christos static int update_wait_timeout (void); 176 1.1 christos static int poll_timers (void); 177 1.1 christos 178 1.1 christos /* Process one high level event. If nothing is ready at this time, 180 1.1.1.2 christos wait at most MSTIMEOUT milliseconds for something to happen (via 181 1.1.1.2 christos gdb_wait_for_event), then process it. Returns >0 if something was 182 1.1.1.2 christos done, <0 if there are no event sources to wait for, =0 if timeout occurred. 183 1.1.1.2 christos A timeout of 0 allows to serve an already pending event, but does not 184 1.1.1.2 christos wait if none found. 185 1.1.1.2 christos Setting the timeout to a negative value disables it. 186 1.1.1.2 christos The timeout is never used by gdb itself, it is however needed to 187 1.1 christos integrate gdb event handling within Insight's GUI event loop. */ 188 1.1 christos 189 1.1.1.2 christos int 190 1.1 christos gdb_do_one_event (int mstimeout) 191 1.1 christos { 192 1.1 christos static int event_source_head = 0; 193 1.1 christos const int number_of_sources = 3; 194 1.1 christos int current = 0; 195 1.1 christos 196 1.1 christos /* First let's see if there are any asynchronous signal handlers 197 1.1 christos that are ready. These would be the result of invoking any of the 198 1.1 christos signal handlers. */ 199 1.1 christos if (invoke_async_signal_handlers ()) 200 1.1 christos return 1; 201 1.1 christos 202 1.1 christos /* To level the fairness across event sources, we poll them in a 203 1.1 christos round-robin fashion. */ 204 1.1 christos for (current = 0; current < number_of_sources; current++) 205 1.1 christos { 206 1.1 christos int res; 207 1.1 christos 208 1.1 christos switch (event_source_head) 209 1.1 christos { 210 1.1 christos case 0: 211 1.1 christos /* Are any timers that are ready? */ 212 1.1 christos res = poll_timers (); 213 1.1 christos break; 214 1.1 christos case 1: 215 1.1 christos /* Are there events already waiting to be collected on the 216 1.1 christos monitored file descriptors? */ 217 1.1 christos res = gdb_wait_for_event (0); 218 1.1 christos break; 219 1.1 christos case 2: 220 1.1 christos /* Are there any asynchronous event handlers ready? */ 221 1.1 christos res = check_async_event_handlers (); 222 1.1 christos break; 223 1.1.1.2 christos default: 224 1.1 christos internal_error ("unexpected event_source_head %d", 225 1.1 christos event_source_head); 226 1.1 christos } 227 1.1 christos 228 1.1 christos event_source_head++; 229 1.1 christos if (event_source_head == number_of_sources) 230 1.1 christos event_source_head = 0; 231 1.1 christos 232 1.1 christos if (res > 0) 233 1.1 christos return 1; 234 1.1 christos } 235 1.1.1.2 christos 236 1.1.1.2 christos if (mstimeout == 0) 237 1.1.1.2 christos return 0; /* 0ms timeout: do not wait for an event. */ 238 1.1 christos 239 1.1 christos /* Block waiting for a new event. If gdb_wait_for_event returns -1, 240 1.1 christos we should get out because this means that there are no event 241 1.1.1.2 christos sources left. This will make the event loop stop, and the 242 1.1.1.2 christos application exit. 243 1.1.1.2 christos If a timeout has been given, a new timer is set accordingly 244 1.1.1.2 christos to abort event wait. It is deleted upon gdb_wait_for_event 245 1.1.1.2 christos termination and thus should never be triggered. 246 1.1.1.2 christos When the timeout is reached, events are not monitored again: 247 1.1.1.2 christos they already have been checked in the loop above. */ 248 1.1.1.3 christos 249 1.1.1.2 christos std::optional<int> timer_id; 250 1.1.1.2 christos 251 1.1.1.2 christos SCOPE_EXIT 252 1.1.1.2 christos { 253 1.1.1.2 christos if (timer_id.has_value ()) 254 1.1.1.2 christos delete_timer (*timer_id); 255 1.1.1.2 christos }; 256 1.1.1.2 christos 257 1.1.1.2 christos if (mstimeout > 0) 258 1.1.1.2 christos timer_id = create_timer (mstimeout, 259 1.1.1.2 christos [] (gdb_client_data arg) 260 1.1.1.3 christos { 261 1.1.1.2 christos ((std::optional<int> *) arg)->reset (); 262 1.1.1.2 christos }, 263 1.1.1.2 christos &timer_id); 264 1.1 christos return gdb_wait_for_event (1); 265 1.1 christos } 266 1.1.1.2 christos 267 1.1 christos /* See event-loop.h */ 268 1.1 christos 269 1.1.1.2 christos void 270 1.1.1.2 christos add_file_handler (int fd, handler_func *proc, gdb_client_data client_data, 271 1.1 christos std::string &&name, bool is_ui) 272 1.1 christos { 273 1.1 christos #ifdef HAVE_POLL 274 1.1 christos if (use_poll) 275 1.1.1.2 christos { 276 1.1.1.2 christos struct pollfd fds; 277 1.1 christos 278 1.1.1.2 christos /* Check to see if poll () is usable. If not, we'll switch to 279 1.1.1.2 christos use select. This can happen on systems like 280 1.1.1.2 christos m68k-motorola-sys, `poll' cannot be used to wait for `stdin'. 281 1.1.1.2 christos On m68k-motorola-sysv, tty's are not stream-based and not 282 1.1 christos `poll'able. */ 283 1.1 christos fds.fd = fd; 284 1.1 christos fds.events = POLLIN; 285 1.1.1.2 christos if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL)) 286 1.1 christos use_poll = false; 287 1.1 christos } 288 1.1 christos if (use_poll) 289 1.1.1.2 christos { 290 1.1.1.2 christos create_file_handler (fd, POLLIN, proc, client_data, std::move (name), 291 1.1 christos is_ui); 292 1.1 christos } 293 1.1.1.2 christos else 294 1.1.1.2 christos #endif /* HAVE_POLL */ 295 1.1.1.2 christos create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION, 296 1.1 christos proc, client_data, std::move (name), is_ui); 297 1.1 christos } 298 1.1.1.2 christos 299 1.1 christos /* Helper for add_file_handler. 300 1.1 christos 301 1.1 christos For the poll case, MASK is a combination (OR) of POLLIN, 302 1.1 christos POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND: 303 1.1 christos these are the events we are interested in. If any of them occurs, 304 1.1 christos proc should be called. 305 1.1 christos 306 1.1 christos For the select case, MASK is a combination of READABLE, WRITABLE, 307 1.1 christos EXCEPTION. PROC is the procedure that will be called when an event 308 1.1 christos occurs for FD. CLIENT_DATA is the argument to pass to PROC. */ 309 1.1 christos 310 1.1.1.2 christos static void 311 1.1.1.2 christos create_file_handler (int fd, int mask, handler_func * proc, 312 1.1.1.2 christos gdb_client_data client_data, std::string &&name, 313 1.1 christos bool is_ui) 314 1.1 christos { 315 1.1 christos file_handler *file_ptr; 316 1.1 christos 317 1.1 christos /* Do we already have a file handler for this file? (We may be 318 1.1 christos changing its associated procedure). */ 319 1.1 christos for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL; 320 1.1 christos file_ptr = file_ptr->next_file) 321 1.1 christos { 322 1.1 christos if (file_ptr->fd == fd) 323 1.1 christos break; 324 1.1 christos } 325 1.1 christos 326 1.1 christos /* It is a new file descriptor. Add it to the list. Otherwise, just 327 1.1 christos change the data associated with it. */ 328 1.1 christos if (file_ptr == NULL) 329 1.1.1.2 christos { 330 1.1 christos file_ptr = new file_handler; 331 1.1 christos file_ptr->fd = fd; 332 1.1 christos file_ptr->ready_mask = 0; 333 1.1 christos file_ptr->next_file = gdb_notifier.first_file_handler; 334 1.1 christos gdb_notifier.first_file_handler = file_ptr; 335 1.1.1.2 christos 336 1.1 christos #ifdef HAVE_POLL 337 1.1 christos if (use_poll) 338 1.1 christos { 339 1.1.1.3 christos gdb_notifier.num_fds++; 340 1.1.1.3 christos struct pollfd new_fd; 341 1.1.1.3 christos new_fd.fd = fd; 342 1.1.1.3 christos new_fd.events = mask; 343 1.1.1.3 christos new_fd.revents = 0; 344 1.1 christos gdb_notifier.poll_fds.push_back (new_fd); 345 1.1 christos } 346 1.1.1.2 christos else 347 1.1 christos #endif /* HAVE_POLL */ 348 1.1 christos { 349 1.1 christos if (mask & GDB_READABLE) 350 1.1 christos FD_SET (fd, &gdb_notifier.check_masks[0]); 351 1.1 christos else 352 1.1 christos FD_CLR (fd, &gdb_notifier.check_masks[0]); 353 1.1 christos 354 1.1 christos if (mask & GDB_WRITABLE) 355 1.1 christos FD_SET (fd, &gdb_notifier.check_masks[1]); 356 1.1 christos else 357 1.1 christos FD_CLR (fd, &gdb_notifier.check_masks[1]); 358 1.1 christos 359 1.1 christos if (mask & GDB_EXCEPTION) 360 1.1 christos FD_SET (fd, &gdb_notifier.check_masks[2]); 361 1.1 christos else 362 1.1 christos FD_CLR (fd, &gdb_notifier.check_masks[2]); 363 1.1 christos 364 1.1 christos if (gdb_notifier.num_fds <= fd) 365 1.1 christos gdb_notifier.num_fds = fd + 1; 366 1.1 christos } 367 1.1 christos } 368 1.1 christos 369 1.1 christos file_ptr->proc = proc; 370 1.1 christos file_ptr->client_data = client_data; 371 1.1.1.2 christos file_ptr->mask = mask; 372 1.1.1.2 christos file_ptr->name = std::move (name); 373 1.1 christos file_ptr->is_ui = is_ui; 374 1.1 christos } 375 1.1 christos 376 1.1 christos /* Return the next file handler to handle, and advance to the next 377 1.1 christos file handler, wrapping around if the end of the list is 378 1.1 christos reached. */ 379 1.1 christos 380 1.1 christos static file_handler * 381 1.1 christos get_next_file_handler_to_handle_and_advance (void) 382 1.1 christos { 383 1.1 christos file_handler *curr_next; 384 1.1 christos 385 1.1 christos /* The first time around, this is still NULL. */ 386 1.1 christos if (gdb_notifier.next_file_handler == NULL) 387 1.1 christos gdb_notifier.next_file_handler = gdb_notifier.first_file_handler; 388 1.1 christos 389 1.1 christos curr_next = gdb_notifier.next_file_handler; 390 1.1 christos gdb_assert (curr_next != NULL); 391 1.1 christos 392 1.1 christos /* Advance. */ 393 1.1 christos gdb_notifier.next_file_handler = curr_next->next_file; 394 1.1 christos /* Wrap around, if necessary. */ 395 1.1 christos if (gdb_notifier.next_file_handler == NULL) 396 1.1 christos gdb_notifier.next_file_handler = gdb_notifier.first_file_handler; 397 1.1 christos 398 1.1 christos return curr_next; 399 1.1 christos } 400 1.1 christos 401 1.1 christos /* Remove the file descriptor FD from the list of monitored fd's: 402 1.1 christos i.e. we don't care anymore about events on the FD. */ 403 1.1 christos void 404 1.1 christos delete_file_handler (int fd) 405 1.1 christos { 406 1.1 christos file_handler *file_ptr, *prev_ptr = NULL; 407 1.1 christos int i; 408 1.1 christos 409 1.1 christos /* Find the entry for the given file. */ 410 1.1 christos 411 1.1 christos for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL; 412 1.1 christos file_ptr = file_ptr->next_file) 413 1.1 christos { 414 1.1 christos if (file_ptr->fd == fd) 415 1.1 christos break; 416 1.1 christos } 417 1.1 christos 418 1.1 christos if (file_ptr == NULL) 419 1.1 christos return; 420 1.1.1.2 christos 421 1.1 christos #ifdef HAVE_POLL 422 1.1 christos if (use_poll) 423 1.1.1.3 christos { 424 1.1.1.3 christos auto iter = std::remove_if (gdb_notifier.poll_fds.begin (), 425 1.1.1.3 christos gdb_notifier.poll_fds.end (), 426 1.1.1.3 christos [=] (const pollfd &item) 427 1.1.1.3 christos { 428 1.1.1.3 christos return item.fd == fd; 429 1.1.1.3 christos }); 430 1.1 christos gdb_notifier.poll_fds.erase (iter, gdb_notifier.poll_fds.end()); 431 1.1 christos gdb_notifier.num_fds--; 432 1.1 christos } 433 1.1.1.2 christos else 434 1.1 christos #endif /* HAVE_POLL */ 435 1.1 christos { 436 1.1 christos if (file_ptr->mask & GDB_READABLE) 437 1.1 christos FD_CLR (fd, &gdb_notifier.check_masks[0]); 438 1.1 christos if (file_ptr->mask & GDB_WRITABLE) 439 1.1 christos FD_CLR (fd, &gdb_notifier.check_masks[1]); 440 1.1 christos if (file_ptr->mask & GDB_EXCEPTION) 441 1.1 christos FD_CLR (fd, &gdb_notifier.check_masks[2]); 442 1.1 christos 443 1.1 christos /* Find current max fd. */ 444 1.1 christos 445 1.1 christos if ((fd + 1) == gdb_notifier.num_fds) 446 1.1 christos { 447 1.1 christos gdb_notifier.num_fds--; 448 1.1 christos for (i = gdb_notifier.num_fds; i; i--) 449 1.1 christos { 450 1.1 christos if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0]) 451 1.1 christos || FD_ISSET (i - 1, &gdb_notifier.check_masks[1]) 452 1.1 christos || FD_ISSET (i - 1, &gdb_notifier.check_masks[2])) 453 1.1 christos break; 454 1.1 christos } 455 1.1 christos gdb_notifier.num_fds = i; 456 1.1 christos } 457 1.1 christos } 458 1.1 christos 459 1.1 christos /* Deactivate the file descriptor, by clearing its mask, 460 1.1 christos so that it will not fire again. */ 461 1.1 christos 462 1.1 christos file_ptr->mask = 0; 463 1.1 christos 464 1.1 christos /* If this file handler was going to be the next one to be handled, 465 1.1 christos advance to the next's next, if any. */ 466 1.1 christos if (gdb_notifier.next_file_handler == file_ptr) 467 1.1 christos { 468 1.1 christos if (file_ptr->next_file == NULL 469 1.1 christos && file_ptr == gdb_notifier.first_file_handler) 470 1.1 christos gdb_notifier.next_file_handler = NULL; 471 1.1 christos else 472 1.1 christos get_next_file_handler_to_handle_and_advance (); 473 1.1 christos } 474 1.1 christos 475 1.1 christos /* Get rid of the file handler in the file handler list. */ 476 1.1 christos if (file_ptr == gdb_notifier.first_file_handler) 477 1.1 christos gdb_notifier.first_file_handler = file_ptr->next_file; 478 1.1 christos else 479 1.1 christos { 480 1.1 christos for (prev_ptr = gdb_notifier.first_file_handler; 481 1.1 christos prev_ptr->next_file != file_ptr; 482 1.1 christos prev_ptr = prev_ptr->next_file) 483 1.1 christos ; 484 1.1 christos prev_ptr->next_file = file_ptr->next_file; 485 1.1.1.2 christos } 486 1.1.1.2 christos 487 1.1 christos delete file_ptr; 488 1.1 christos } 489 1.1 christos 490 1.1 christos /* Handle the given event by calling the procedure associated to the 491 1.1 christos corresponding file handler. */ 492 1.1 christos 493 1.1 christos static void 494 1.1 christos handle_file_event (file_handler *file_ptr, int ready_mask) 495 1.1 christos { 496 1.1 christos int mask; 497 1.1.1.2 christos 498 1.1.1.2 christos /* See if the desired events (mask) match the received events 499 1.1 christos (ready_mask). */ 500 1.1 christos 501 1.1.1.2 christos #ifdef HAVE_POLL 502 1.1.1.2 christos if (use_poll) 503 1.1.1.2 christos { 504 1.1 christos int error_mask; 505 1.1.1.2 christos 506 1.1.1.2 christos /* With poll, the ready_mask could have any of three events set 507 1.1.1.2 christos to 1: POLLHUP, POLLERR, POLLNVAL. These events cannot be 508 1.1.1.2 christos used in the requested event mask (events), but they can be 509 1.1.1.2 christos returned in the return mask (revents). We need to check for 510 1.1.1.2 christos those event too, and add them to the mask which will be 511 1.1.1.2 christos passed to the handler. */ 512 1.1.1.2 christos 513 1.1.1.2 christos /* POLLHUP means EOF, but can be combined with POLLIN to 514 1.1.1.2 christos signal more data to read. */ 515 1.1.1.2 christos error_mask = POLLHUP | POLLERR | POLLNVAL; 516 1.1.1.2 christos mask = ready_mask & (file_ptr->mask | error_mask); 517 1.1.1.2 christos 518 1.1.1.2 christos if ((mask & (POLLERR | POLLNVAL)) != 0) 519 1.1.1.2 christos { 520 1.1.1.2 christos /* Work in progress. We may need to tell somebody 521 1.1.1.2 christos what kind of error we had. */ 522 1.1.1.2 christos if (mask & POLLERR) 523 1.1.1.2 christos warning (_("Error detected on fd %d"), file_ptr->fd); 524 1.1.1.2 christos if (mask & POLLNVAL) 525 1.1.1.2 christos warning (_("Invalid or non-`poll'able fd %d"), 526 1.1.1.2 christos file_ptr->fd); 527 1.1.1.2 christos file_ptr->error = 1; 528 1.1.1.2 christos } 529 1.1.1.2 christos else 530 1.1.1.2 christos file_ptr->error = 0; 531 1.1.1.2 christos } 532 1.1.1.2 christos else 533 1.1.1.2 christos #endif /* HAVE_POLL */ 534 1.1.1.2 christos { 535 1.1.1.2 christos if (ready_mask & GDB_EXCEPTION) 536 1.1.1.2 christos { 537 1.1.1.2 christos warning (_("Exception condition detected on fd %d"), 538 1.1.1.2 christos file_ptr->fd); 539 1.1 christos file_ptr->error = 1; 540 1.1.1.2 christos } 541 1.1.1.2 christos else 542 1.1.1.2 christos file_ptr->error = 0; 543 1.1.1.2 christos mask = ready_mask & file_ptr->mask; 544 1.1.1.2 christos } 545 1.1.1.2 christos 546 1.1.1.2 christos /* If there was a match, then call the handler. */ 547 1.1.1.2 christos if (mask != 0) 548 1.1.1.2 christos { 549 1.1.1.2 christos event_loop_ui_debug_printf (file_ptr->is_ui, 550 1.1.1.2 christos "invoking fd file handler `%s`", 551 1.1.1.2 christos file_ptr->name.c_str ()); 552 1.1 christos file_ptr->proc (file_ptr->error, file_ptr->client_data); 553 1.1 christos } 554 1.1 christos } 555 1.1 christos 556 1.1 christos /* Wait for new events on the monitored file descriptors. Run the 557 1.1 christos event handler if the first descriptor that is detected by the poll. 558 1.1 christos If BLOCK and if there are no events, this function will block in 559 1.1 christos the call to poll. Return 1 if an event was handled. Return -1 if 560 1.1 christos there are no file descriptors to monitor. Return 1 if an event was 561 1.1 christos handled, otherwise returns 0. */ 562 1.1 christos 563 1.1 christos static int 564 1.1 christos gdb_wait_for_event (int block) 565 1.1 christos { 566 1.1 christos file_handler *file_ptr; 567 1.1 christos int num_found = 0; 568 1.1 christos 569 1.1 christos /* Make sure all output is done before getting another event. */ 570 1.1 christos flush_streams (); 571 1.1 christos 572 1.1 christos if (gdb_notifier.num_fds == 0) 573 1.1 christos return -1; 574 1.1 christos 575 1.1 christos if (block) 576 1.1 christos update_wait_timeout (); 577 1.1.1.2 christos 578 1.1 christos #ifdef HAVE_POLL 579 1.1 christos if (use_poll) 580 1.1 christos { 581 1.1 christos int timeout; 582 1.1 christos 583 1.1 christos if (block) 584 1.1 christos timeout = gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1; 585 1.1 christos else 586 1.1 christos timeout = 0; 587 1.1.1.3 christos 588 1.1 christos num_found = poll (gdb_notifier.poll_fds.data (), 589 1.1 christos (unsigned long) gdb_notifier.num_fds, timeout); 590 1.1 christos 591 1.1 christos /* Don't print anything if we get out of poll because of a 592 1.1 christos signal. */ 593 1.1 christos if (num_found == -1 && errno != EINTR) 594 1.1 christos perror_with_name (("poll")); 595 1.1 christos } 596 1.1.1.2 christos else 597 1.1 christos #endif /* HAVE_POLL */ 598 1.1 christos { 599 1.1 christos struct timeval select_timeout; 600 1.1 christos struct timeval *timeout_p; 601 1.1 christos 602 1.1 christos if (block) 603 1.1 christos timeout_p = gdb_notifier.timeout_valid 604 1.1 christos ? &gdb_notifier.select_timeout : NULL; 605 1.1 christos else 606 1.1 christos { 607 1.1 christos memset (&select_timeout, 0, sizeof (select_timeout)); 608 1.1 christos timeout_p = &select_timeout; 609 1.1 christos } 610 1.1 christos 611 1.1 christos gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0]; 612 1.1 christos gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1]; 613 1.1 christos gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2]; 614 1.1 christos num_found = gdb_select (gdb_notifier.num_fds, 615 1.1 christos &gdb_notifier.ready_masks[0], 616 1.1 christos &gdb_notifier.ready_masks[1], 617 1.1 christos &gdb_notifier.ready_masks[2], 618 1.1 christos timeout_p); 619 1.1 christos 620 1.1 christos /* Clear the masks after an error from select. */ 621 1.1 christos if (num_found == -1) 622 1.1 christos { 623 1.1 christos FD_ZERO (&gdb_notifier.ready_masks[0]); 624 1.1 christos FD_ZERO (&gdb_notifier.ready_masks[1]); 625 1.1 christos FD_ZERO (&gdb_notifier.ready_masks[2]); 626 1.1 christos 627 1.1 christos /* Dont print anything if we got a signal, let gdb handle 628 1.1 christos it. */ 629 1.1 christos if (errno != EINTR) 630 1.1 christos perror_with_name (("select")); 631 1.1 christos } 632 1.1 christos } 633 1.1 christos 634 1.1 christos /* Avoid looking at poll_fds[i]->revents if no event fired. */ 635 1.1 christos if (num_found <= 0) 636 1.1 christos return 0; 637 1.1 christos 638 1.1 christos /* Run event handlers. We always run just one handler and go back 639 1.1 christos to polling, in case a handler changes the notifier list. Since 640 1.1 christos events for sources we haven't consumed yet wake poll/select 641 1.1 christos immediately, no event is lost. */ 642 1.1 christos 643 1.1 christos /* To level the fairness across event descriptors, we handle them in 644 1.1 christos a round-robin-like fashion. The number and order of descriptors 645 1.1.1.2 christos may change between invocations, but this is good enough. */ 646 1.1 christos #ifdef HAVE_POLL 647 1.1 christos if (use_poll) 648 1.1 christos { 649 1.1 christos int i; 650 1.1 christos int mask; 651 1.1 christos 652 1.1 christos while (1) 653 1.1 christos { 654 1.1 christos if (gdb_notifier.next_poll_fds_index >= gdb_notifier.num_fds) 655 1.1 christos gdb_notifier.next_poll_fds_index = 0; 656 1.1 christos i = gdb_notifier.next_poll_fds_index++; 657 1.1 christos 658 1.1.1.3 christos gdb_assert (i < gdb_notifier.num_fds); 659 1.1 christos if (gdb_notifier.poll_fds[i].revents) 660 1.1 christos break; 661 1.1 christos } 662 1.1 christos 663 1.1 christos for (file_ptr = gdb_notifier.first_file_handler; 664 1.1 christos file_ptr != NULL; 665 1.1 christos file_ptr = file_ptr->next_file) 666 1.1.1.3 christos { 667 1.1 christos if (file_ptr->fd == gdb_notifier.poll_fds[i].fd) 668 1.1 christos break; 669 1.1 christos } 670 1.1 christos gdb_assert (file_ptr != NULL); 671 1.1.1.3 christos 672 1.1 christos mask = gdb_notifier.poll_fds[i].revents; 673 1.1 christos handle_file_event (file_ptr, mask); 674 1.1 christos return 1; 675 1.1 christos } 676 1.1.1.2 christos else 677 1.1 christos #endif /* HAVE_POLL */ 678 1.1 christos { 679 1.1 christos /* See comment about even source fairness above. */ 680 1.1 christos int mask = 0; 681 1.1 christos 682 1.1 christos do 683 1.1 christos { 684 1.1 christos file_ptr = get_next_file_handler_to_handle_and_advance (); 685 1.1 christos 686 1.1 christos if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0])) 687 1.1 christos mask |= GDB_READABLE; 688 1.1 christos if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1])) 689 1.1 christos mask |= GDB_WRITABLE; 690 1.1 christos if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2])) 691 1.1 christos mask |= GDB_EXCEPTION; 692 1.1 christos } 693 1.1 christos while (mask == 0); 694 1.1 christos 695 1.1 christos handle_file_event (file_ptr, mask); 696 1.1 christos return 1; 697 1.1 christos } 698 1.1 christos return 0; 699 1.1 christos } 700 1.1 christos 701 1.1 christos /* Create a timer that will expire in MS milliseconds from now. When 703 1.1 christos the timer is ready, PROC will be executed. At creation, the timer 704 1.1 christos is added to the timers queue. This queue is kept sorted in order 705 1.1 christos of increasing timers. Return a handle to the timer struct. */ 706 1.1 christos 707 1.1 christos int 708 1.1 christos create_timer (int ms, timer_handler_func *proc, 709 1.1 christos gdb_client_data client_data) 710 1.1 christos { 711 1.1 christos using namespace std::chrono; 712 1.1 christos struct gdb_timer *timer_ptr, *timer_index, *prev_timer; 713 1.1 christos 714 1.1 christos steady_clock::time_point time_now = steady_clock::now (); 715 1.1 christos 716 1.1 christos timer_ptr = new gdb_timer (); 717 1.1 christos timer_ptr->when = time_now + milliseconds (ms); 718 1.1 christos timer_ptr->proc = proc; 719 1.1 christos timer_ptr->client_data = client_data; 720 1.1 christos timer_list.num_timers++; 721 1.1 christos timer_ptr->timer_id = timer_list.num_timers; 722 1.1 christos 723 1.1 christos /* Now add the timer to the timer queue, making sure it is sorted in 724 1.1 christos increasing order of expiration. */ 725 1.1 christos 726 1.1 christos for (timer_index = timer_list.first_timer; 727 1.1 christos timer_index != NULL; 728 1.1 christos timer_index = timer_index->next) 729 1.1 christos { 730 1.1 christos if (timer_index->when > timer_ptr->when) 731 1.1 christos break; 732 1.1 christos } 733 1.1 christos 734 1.1 christos if (timer_index == timer_list.first_timer) 735 1.1 christos { 736 1.1 christos timer_ptr->next = timer_list.first_timer; 737 1.1 christos timer_list.first_timer = timer_ptr; 738 1.1 christos 739 1.1 christos } 740 1.1 christos else 741 1.1 christos { 742 1.1 christos for (prev_timer = timer_list.first_timer; 743 1.1 christos prev_timer->next != timer_index; 744 1.1 christos prev_timer = prev_timer->next) 745 1.1 christos ; 746 1.1 christos 747 1.1 christos prev_timer->next = timer_ptr; 748 1.1 christos timer_ptr->next = timer_index; 749 1.1 christos } 750 1.1 christos 751 1.1 christos gdb_notifier.timeout_valid = 0; 752 1.1 christos return timer_ptr->timer_id; 753 1.1 christos } 754 1.1 christos 755 1.1 christos /* There is a chance that the creator of the timer wants to get rid of 756 1.1 christos it before it expires. */ 757 1.1 christos void 758 1.1 christos delete_timer (int id) 759 1.1 christos { 760 1.1 christos struct gdb_timer *timer_ptr, *prev_timer = NULL; 761 1.1 christos 762 1.1 christos /* Find the entry for the given timer. */ 763 1.1 christos 764 1.1 christos for (timer_ptr = timer_list.first_timer; timer_ptr != NULL; 765 1.1 christos timer_ptr = timer_ptr->next) 766 1.1 christos { 767 1.1 christos if (timer_ptr->timer_id == id) 768 1.1 christos break; 769 1.1 christos } 770 1.1 christos 771 1.1 christos if (timer_ptr == NULL) 772 1.1 christos return; 773 1.1 christos /* Get rid of the timer in the timer list. */ 774 1.1 christos if (timer_ptr == timer_list.first_timer) 775 1.1 christos timer_list.first_timer = timer_ptr->next; 776 1.1 christos else 777 1.1 christos { 778 1.1 christos for (prev_timer = timer_list.first_timer; 779 1.1 christos prev_timer->next != timer_ptr; 780 1.1 christos prev_timer = prev_timer->next) 781 1.1 christos ; 782 1.1 christos prev_timer->next = timer_ptr->next; 783 1.1 christos } 784 1.1 christos delete timer_ptr; 785 1.1 christos 786 1.1 christos gdb_notifier.timeout_valid = 0; 787 1.1 christos } 788 1.1 christos 789 1.1 christos /* Convert a std::chrono duration to a struct timeval. */ 790 1.1 christos 791 1.1 christos template<typename Duration> 792 1.1 christos static struct timeval 793 1.1 christos duration_cast_timeval (const Duration &d) 794 1.1 christos { 795 1.1 christos using namespace std::chrono; 796 1.1 christos seconds sec = duration_cast<seconds> (d); 797 1.1 christos microseconds msec = duration_cast<microseconds> (d - sec); 798 1.1 christos 799 1.1 christos struct timeval tv; 800 1.1 christos tv.tv_sec = sec.count (); 801 1.1 christos tv.tv_usec = msec.count (); 802 1.1 christos return tv; 803 1.1 christos } 804 1.1 christos 805 1.1 christos /* Update the timeout for the select() or poll(). Returns true if the 806 1.1 christos timer has already expired, false otherwise. */ 807 1.1 christos 808 1.1 christos static int 809 1.1 christos update_wait_timeout (void) 810 1.1 christos { 811 1.1 christos if (timer_list.first_timer != NULL) 812 1.1 christos { 813 1.1 christos using namespace std::chrono; 814 1.1 christos steady_clock::time_point time_now = steady_clock::now (); 815 1.1 christos struct timeval timeout; 816 1.1 christos 817 1.1 christos if (timer_list.first_timer->when < time_now) 818 1.1 christos { 819 1.1 christos /* It expired already. */ 820 1.1 christos timeout.tv_sec = 0; 821 1.1 christos timeout.tv_usec = 0; 822 1.1 christos } 823 1.1 christos else 824 1.1 christos { 825 1.1 christos steady_clock::duration d = timer_list.first_timer->when - time_now; 826 1.1 christos timeout = duration_cast_timeval (d); 827 1.1 christos } 828 1.1 christos 829 1.1.1.2 christos /* Update the timeout for select/ poll. */ 830 1.1.1.2 christos #ifdef HAVE_POLL 831 1.1 christos if (use_poll) 832 1.1.1.2 christos gdb_notifier.poll_timeout = timeout.tv_sec * 1000; 833 1.1 christos else 834 1.1 christos #endif /* HAVE_POLL */ 835 1.1 christos { 836 1.1 christos gdb_notifier.select_timeout.tv_sec = timeout.tv_sec; 837 1.1 christos gdb_notifier.select_timeout.tv_usec = timeout.tv_usec; 838 1.1 christos } 839 1.1 christos gdb_notifier.timeout_valid = 1; 840 1.1 christos 841 1.1 christos if (timer_list.first_timer->when < time_now) 842 1.1 christos return 1; 843 1.1 christos } 844 1.1 christos else 845 1.1 christos gdb_notifier.timeout_valid = 0; 846 1.1 christos 847 1.1 christos return 0; 848 1.1 christos } 849 1.1 christos 850 1.1 christos /* Check whether a timer in the timers queue is ready. If a timer is 851 1.1 christos ready, call its handler and return. Update the timeout for the 852 1.1 christos select() or poll() as well. Return 1 if an event was handled, 853 1.1 christos otherwise returns 0.*/ 854 1.1 christos 855 1.1 christos static int 856 1.1 christos poll_timers (void) 857 1.1 christos { 858 1.1 christos if (update_wait_timeout ()) 859 1.1 christos { 860 1.1 christos struct gdb_timer *timer_ptr = timer_list.first_timer; 861 1.1 christos timer_handler_func *proc = timer_ptr->proc; 862 1.1 christos gdb_client_data client_data = timer_ptr->client_data; 863 1.1 christos 864 1.1 christos /* Get rid of the timer from the beginning of the list. */ 865 1.1 christos timer_list.first_timer = timer_ptr->next; 866 1.1 christos 867 1.1 christos /* Delete the timer before calling the callback, not after, in 868 1.1 christos case the callback itself decides to try deleting the timer 869 1.1 christos too. */ 870 1.1 christos delete timer_ptr; 871 1.1 christos 872 1.1 christos /* Call the procedure associated with that timer. */ 873 1.1 christos (proc) (client_data); 874 1.1 christos 875 1.1 christos return 1; 876 1.1 christos } 877 1.1 christos 878 return 0; 879 } 880