1 /* Event loop machinery for GDB, the GNU debugger. 2 Copyright (C) 1999-2002, 2005-2012 Free Software Foundation, Inc. 3 Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 #include "defs.h" 21 #include "event-loop.h" 22 #include "event-top.h" 23 24 #ifdef HAVE_POLL 25 #if defined (HAVE_POLL_H) 26 #include <poll.h> 27 #elif defined (HAVE_SYS_POLL_H) 28 #include <sys/poll.h> 29 #endif 30 #endif 31 32 #include <sys/types.h> 33 #include "gdb_string.h" 34 #include <errno.h> 35 #include <sys/time.h> 36 #include "exceptions.h" 37 #include "gdb_assert.h" 38 #include "gdb_select.h" 39 40 /* Tell create_file_handler what events we are interested in. 41 This is used by the select version of the event loop. */ 42 43 #define GDB_READABLE (1<<1) 44 #define GDB_WRITABLE (1<<2) 45 #define GDB_EXCEPTION (1<<3) 46 47 /* Data point to pass to the event handler. */ 48 typedef union event_data 49 { 50 void *ptr; 51 int integer; 52 } event_data; 53 54 typedef struct gdb_event gdb_event; 55 typedef void (event_handler_func) (event_data); 56 57 /* Event for the GDB event system. Events are queued by calling 58 async_queue_event and serviced later on by gdb_do_one_event. An 59 event can be, for instance, a file descriptor becoming ready to be 60 read. Servicing an event simply means that the procedure PROC will 61 be called. We have 2 queues, one for file handlers that we listen 62 to in the event loop, and one for the file handlers+events that are 63 ready. The procedure PROC associated with each event is dependant 64 of the event source. In the case of monitored file descriptors, it 65 is always the same (handle_file_event). Its duty is to invoke the 66 handler associated with the file descriptor whose state change 67 generated the event, plus doing other cleanups and such. In the 68 case of async signal handlers, it is 69 invoke_async_signal_handler. */ 70 71 struct gdb_event 72 { 73 /* Procedure to call to service this event. */ 74 event_handler_func *proc; 75 76 /* Data to pass to the event handler. */ 77 event_data data; 78 79 /* Next in list of events or NULL. */ 80 struct gdb_event *next_event; 81 }; 82 83 /* Information about each file descriptor we register with the event 84 loop. */ 85 86 typedef struct file_handler 87 { 88 int fd; /* File descriptor. */ 89 int mask; /* Events we want to monitor: POLLIN, etc. */ 90 int ready_mask; /* Events that have been seen since 91 the last time. */ 92 handler_func *proc; /* Procedure to call when fd is ready. */ 93 gdb_client_data client_data; /* Argument to pass to proc. */ 94 int error; /* Was an error detected on this fd? */ 95 struct file_handler *next_file; /* Next registered file descriptor. */ 96 } 97 file_handler; 98 99 /* PROC is a function to be invoked when the READY flag is set. This 100 happens when there has been a signal and the corresponding signal 101 handler has 'triggered' this async_signal_handler for execution. 102 The actual work to be done in response to a signal will be carried 103 out by PROC at a later time, within process_event. This provides a 104 deferred execution of signal handlers. 105 106 Async_init_signals takes care of setting up such an 107 async_signal_handler for each interesting signal. */ 108 109 typedef struct async_signal_handler 110 { 111 int ready; /* If ready, call this handler 112 from the main event loop, using 113 invoke_async_handler. */ 114 struct async_signal_handler *next_handler; /* Ptr to next handler. */ 115 sig_handler_func *proc; /* Function to call to do the work. */ 116 gdb_client_data client_data; /* Argument to async_handler_func. */ 117 } 118 async_signal_handler; 119 120 /* PROC is a function to be invoked when the READY flag is set. This 121 happens when the event has been marked with 122 MARK_ASYNC_EVENT_HANDLER. The actual work to be done in response 123 to an event will be carried out by PROC at a later time, within 124 process_event. This provides a deferred execution of event 125 handlers. */ 126 typedef struct async_event_handler 127 { 128 /* If ready, call this handler from the main event loop, using 129 invoke_event_handler. */ 130 int ready; 131 132 /* Point to next handler. */ 133 struct async_event_handler *next_handler; 134 135 /* Function to call to do the work. */ 136 async_event_handler_func *proc; 137 138 /* Argument to PROC. */ 139 gdb_client_data client_data; 140 } 141 async_event_handler; 142 143 144 /* Event queue: 145 - the first event in the queue is the head of the queue. 146 It will be the next to be serviced. 147 - the last event in the queue 148 149 Events can be inserted at the front of the queue or at the end of 150 the queue. Events will be extracted from the queue for processing 151 starting from the head. Therefore, events inserted at the head of 152 the queue will be processed in a last in first out fashion, while 153 those inserted at the tail of the queue will be processed in a first 154 in first out manner. All the fields are NULL if the queue is 155 empty. */ 156 157 static struct 158 { 159 gdb_event *first_event; /* First pending event. */ 160 gdb_event *last_event; /* Last pending event. */ 161 } 162 event_queue; 163 164 /* Gdb_notifier is just a list of file descriptors gdb is interested in. 165 These are the input file descriptor, and the target file 166 descriptor. We have two flavors of the notifier, one for platforms 167 that have the POLL function, the other for those that don't, and 168 only support SELECT. Each of the elements in the gdb_notifier list is 169 basically a description of what kind of events gdb is interested 170 in, for each fd. */ 171 172 /* As of 1999-04-30 only the input file descriptor is registered with the 173 event loop. */ 174 175 /* Do we use poll or select ? */ 176 #ifdef HAVE_POLL 177 #define USE_POLL 1 178 #else 179 #define USE_POLL 0 180 #endif /* HAVE_POLL */ 181 182 static unsigned char use_poll = USE_POLL; 183 184 #ifdef USE_WIN32API 185 #include <windows.h> 186 #include <io.h> 187 #endif 188 189 static struct 190 { 191 /* Ptr to head of file handler list. */ 192 file_handler *first_file_handler; 193 194 #ifdef HAVE_POLL 195 /* Ptr to array of pollfd structures. */ 196 struct pollfd *poll_fds; 197 198 /* Timeout in milliseconds for calls to poll(). */ 199 int poll_timeout; 200 #endif 201 202 /* Masks to be used in the next call to select. 203 Bits are set in response to calls to create_file_handler. */ 204 fd_set check_masks[3]; 205 206 /* What file descriptors were found ready by select. */ 207 fd_set ready_masks[3]; 208 209 /* Number of file descriptors to monitor (for poll). */ 210 /* Number of valid bits (highest fd value + 1) (for select). */ 211 int num_fds; 212 213 /* Time structure for calls to select(). */ 214 struct timeval select_timeout; 215 216 /* Flag to tell whether the timeout should be used. */ 217 int timeout_valid; 218 } 219 gdb_notifier; 220 221 /* Structure associated with a timer. PROC will be executed at the 222 first occasion after WHEN. */ 223 struct gdb_timer 224 { 225 struct timeval when; 226 int timer_id; 227 struct gdb_timer *next; 228 timer_handler_func *proc; /* Function to call to do the work. */ 229 gdb_client_data client_data; /* Argument to async_handler_func. */ 230 }; 231 232 /* List of currently active timers. It is sorted in order of 233 increasing timers. */ 234 static struct 235 { 236 /* Pointer to first in timer list. */ 237 struct gdb_timer *first_timer; 238 239 /* Id of the last timer created. */ 240 int num_timers; 241 } 242 timer_list; 243 244 /* All the async_signal_handlers gdb is interested in are kept onto 245 this list. */ 246 static struct 247 { 248 /* Pointer to first in handler list. */ 249 async_signal_handler *first_handler; 250 251 /* Pointer to last in handler list. */ 252 async_signal_handler *last_handler; 253 } 254 sighandler_list; 255 256 /* All the async_event_handlers gdb is interested in are kept onto 257 this list. */ 258 static struct 259 { 260 /* Pointer to first in handler list. */ 261 async_event_handler *first_handler; 262 263 /* Pointer to last in handler list. */ 264 async_event_handler *last_handler; 265 } 266 async_event_handler_list; 267 268 static int invoke_async_signal_handlers (void); 269 static void create_file_handler (int fd, int mask, handler_func *proc, 270 gdb_client_data client_data); 271 static void handle_file_event (event_data data); 272 static void check_async_event_handlers (void); 273 static int gdb_wait_for_event (int); 274 static void poll_timers (void); 275 276 277 /* Insert an event object into the gdb event queue at 278 the specified position. 279 POSITION can be head or tail, with values TAIL, HEAD. 280 EVENT_PTR points to the event to be inserted into the queue. 281 The caller must allocate memory for the event. It is freed 282 after the event has ben handled. 283 Events in the queue will be processed head to tail, therefore, 284 events inserted at the head of the queue will be processed 285 as last in first out. Event appended at the tail of the queue 286 will be processed first in first out. */ 287 static void 288 async_queue_event (gdb_event * event_ptr, queue_position position) 289 { 290 if (position == TAIL) 291 { 292 /* The event will become the new last_event. */ 293 294 event_ptr->next_event = NULL; 295 if (event_queue.first_event == NULL) 296 event_queue.first_event = event_ptr; 297 else 298 event_queue.last_event->next_event = event_ptr; 299 event_queue.last_event = event_ptr; 300 } 301 else if (position == HEAD) 302 { 303 /* The event becomes the new first_event. */ 304 305 event_ptr->next_event = event_queue.first_event; 306 if (event_queue.first_event == NULL) 307 event_queue.last_event = event_ptr; 308 event_queue.first_event = event_ptr; 309 } 310 } 311 312 /* Create a generic event, to be enqueued in the event queue for 313 processing. PROC is the procedure associated to the event. DATA 314 is passed to PROC upon PROC invocation. */ 315 316 static gdb_event * 317 create_event (event_handler_func proc, event_data data) 318 { 319 gdb_event *event; 320 321 event = xmalloc (sizeof (*event)); 322 event->proc = proc; 323 event->data = data; 324 325 return event; 326 } 327 328 /* Create a file event, to be enqueued in the event queue for 329 processing. The procedure associated to this event is always 330 handle_file_event, which will in turn invoke the one that was 331 associated to FD when it was registered with the event loop. */ 332 static gdb_event * 333 create_file_event (int fd) 334 { 335 event_data data; 336 337 data.integer = fd; 338 return create_event (handle_file_event, data); 339 } 340 341 /* Process one event. 342 The event can be the next one to be serviced in the event queue, 343 or an asynchronous event handler can be invoked in response to 344 the reception of a signal. 345 If an event was processed (either way), 1 is returned otherwise 346 0 is returned. 347 Scan the queue from head to tail, processing therefore the high 348 priority events first, by invoking the associated event handler 349 procedure. */ 350 static int 351 process_event (void) 352 { 353 gdb_event *event_ptr, *prev_ptr; 354 event_handler_func *proc; 355 event_data data; 356 357 /* First let's see if there are any asynchronous event handlers that 358 are ready. These would be the result of invoking any of the 359 signal handlers. */ 360 361 if (invoke_async_signal_handlers ()) 362 return 1; 363 364 /* Look in the event queue to find an event that is ready 365 to be processed. */ 366 367 for (event_ptr = event_queue.first_event; event_ptr != NULL; 368 event_ptr = event_ptr->next_event) 369 { 370 /* Call the handler for the event. */ 371 372 proc = event_ptr->proc; 373 data = event_ptr->data; 374 375 /* Let's get rid of the event from the event queue. We need to 376 do this now because while processing the event, the proc 377 function could end up calling 'error' and therefore jump out 378 to the caller of this function, gdb_do_one_event. In that 379 case, we would have on the event queue an event wich has been 380 processed, but not deleted. */ 381 382 if (event_queue.first_event == event_ptr) 383 { 384 event_queue.first_event = event_ptr->next_event; 385 if (event_ptr->next_event == NULL) 386 event_queue.last_event = NULL; 387 } 388 else 389 { 390 prev_ptr = event_queue.first_event; 391 while (prev_ptr->next_event != event_ptr) 392 prev_ptr = prev_ptr->next_event; 393 394 prev_ptr->next_event = event_ptr->next_event; 395 if (event_ptr->next_event == NULL) 396 event_queue.last_event = prev_ptr; 397 } 398 xfree (event_ptr); 399 400 /* Now call the procedure associated with the event. */ 401 (*proc) (data); 402 return 1; 403 } 404 405 /* This is the case if there are no event on the event queue. */ 406 return 0; 407 } 408 409 /* Process one high level event. If nothing is ready at this time, 410 wait for something to happen (via gdb_wait_for_event), then process 411 it. Returns >0 if something was done otherwise returns <0 (this 412 can happen if there are no event sources to wait for). */ 413 414 int 415 gdb_do_one_event (void) 416 { 417 static int event_source_head = 0; 418 const int number_of_sources = 3; 419 int current = 0; 420 421 /* Any events already waiting in the queue? */ 422 if (process_event ()) 423 return 1; 424 425 /* To level the fairness across event sources, we poll them in a 426 round-robin fashion. */ 427 for (current = 0; current < number_of_sources; current++) 428 { 429 switch (event_source_head) 430 { 431 case 0: 432 /* Are any timers that are ready? If so, put an event on the 433 queue. */ 434 poll_timers (); 435 break; 436 case 1: 437 /* Are there events already waiting to be collected on the 438 monitored file descriptors? */ 439 gdb_wait_for_event (0); 440 break; 441 case 2: 442 /* Are there any asynchronous event handlers ready? */ 443 check_async_event_handlers (); 444 break; 445 } 446 447 event_source_head++; 448 if (event_source_head == number_of_sources) 449 event_source_head = 0; 450 } 451 452 /* Handle any new events collected. */ 453 if (process_event ()) 454 return 1; 455 456 /* Block waiting for a new event. If gdb_wait_for_event returns -1, 457 we should get out because this means that there are no event 458 sources left. This will make the event loop stop, and the 459 application exit. */ 460 461 if (gdb_wait_for_event (1) < 0) 462 return -1; 463 464 /* Handle any new events occurred while waiting. */ 465 if (process_event ()) 466 return 1; 467 468 /* If gdb_wait_for_event has returned 1, it means that one event has 469 been handled. We break out of the loop. */ 470 return 1; 471 } 472 473 /* Start up the event loop. This is the entry point to the event loop 474 from the command loop. */ 475 476 void 477 start_event_loop (void) 478 { 479 /* Loop until there is nothing to do. This is the entry point to 480 the event loop engine. gdb_do_one_event will process one event 481 for each invocation. It blocks waiting for an event and then 482 processes it. */ 483 while (1) 484 { 485 volatile struct gdb_exception ex; 486 int result = 0; 487 488 TRY_CATCH (ex, RETURN_MASK_ALL) 489 { 490 result = gdb_do_one_event (); 491 } 492 if (ex.reason < 0) 493 { 494 exception_print (gdb_stderr, ex); 495 496 /* If any exception escaped to here, we better enable 497 stdin. Otherwise, any command that calls async_disable_stdin, 498 and then throws, will leave stdin inoperable. */ 499 async_enable_stdin (); 500 /* If we long-jumped out of do_one_event, we probably didn't 501 get around to resetting the prompt, which leaves readline 502 in a messed-up state. Reset it here. */ 503 /* FIXME: this should really be a call to a hook that is 504 interface specific, because interfaces can display the 505 prompt in their own way. */ 506 display_gdb_prompt (0); 507 /* This call looks bizarre, but it is required. If the user 508 entered a command that caused an error, 509 after_char_processing_hook won't be called from 510 rl_callback_read_char_wrapper. Using a cleanup there 511 won't work, since we want this function to be called 512 after a new prompt is printed. */ 513 if (after_char_processing_hook) 514 (*after_char_processing_hook) (); 515 /* Maybe better to set a flag to be checked somewhere as to 516 whether display the prompt or not. */ 517 } 518 if (result < 0) 519 break; 520 } 521 522 /* We are done with the event loop. There are no more event sources 523 to listen to. So we exit GDB. */ 524 return; 525 } 526 527 528 /* Wrapper function for create_file_handler, so that the caller 529 doesn't have to know implementation details about the use of poll 530 vs. select. */ 531 void 532 add_file_handler (int fd, handler_func * proc, gdb_client_data client_data) 533 { 534 #ifdef HAVE_POLL 535 struct pollfd fds; 536 #endif 537 538 if (use_poll) 539 { 540 #ifdef HAVE_POLL 541 /* Check to see if poll () is usable. If not, we'll switch to 542 use select. This can happen on systems like 543 m68k-motorola-sys, `poll' cannot be used to wait for `stdin'. 544 On m68k-motorola-sysv, tty's are not stream-based and not 545 `poll'able. */ 546 fds.fd = fd; 547 fds.events = POLLIN; 548 if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL)) 549 use_poll = 0; 550 #else 551 internal_error (__FILE__, __LINE__, 552 _("use_poll without HAVE_POLL")); 553 #endif /* HAVE_POLL */ 554 } 555 if (use_poll) 556 { 557 #ifdef HAVE_POLL 558 create_file_handler (fd, POLLIN, proc, client_data); 559 #else 560 internal_error (__FILE__, __LINE__, 561 _("use_poll without HAVE_POLL")); 562 #endif 563 } 564 else 565 create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION, 566 proc, client_data); 567 } 568 569 /* Add a file handler/descriptor to the list of descriptors we are 570 interested in. 571 572 FD is the file descriptor for the file/stream to be listened to. 573 574 For the poll case, MASK is a combination (OR) of POLLIN, 575 POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND: 576 these are the events we are interested in. If any of them occurs, 577 proc should be called. 578 579 For the select case, MASK is a combination of READABLE, WRITABLE, 580 EXCEPTION. PROC is the procedure that will be called when an event 581 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */ 582 583 static void 584 create_file_handler (int fd, int mask, handler_func * proc, 585 gdb_client_data client_data) 586 { 587 file_handler *file_ptr; 588 589 /* Do we already have a file handler for this file? (We may be 590 changing its associated procedure). */ 591 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL; 592 file_ptr = file_ptr->next_file) 593 { 594 if (file_ptr->fd == fd) 595 break; 596 } 597 598 /* It is a new file descriptor. Add it to the list. Otherwise, just 599 change the data associated with it. */ 600 if (file_ptr == NULL) 601 { 602 file_ptr = (file_handler *) xmalloc (sizeof (file_handler)); 603 file_ptr->fd = fd; 604 file_ptr->ready_mask = 0; 605 file_ptr->next_file = gdb_notifier.first_file_handler; 606 gdb_notifier.first_file_handler = file_ptr; 607 608 if (use_poll) 609 { 610 #ifdef HAVE_POLL 611 gdb_notifier.num_fds++; 612 if (gdb_notifier.poll_fds) 613 gdb_notifier.poll_fds = 614 (struct pollfd *) xrealloc (gdb_notifier.poll_fds, 615 (gdb_notifier.num_fds 616 * sizeof (struct pollfd))); 617 else 618 gdb_notifier.poll_fds = 619 (struct pollfd *) xmalloc (sizeof (struct pollfd)); 620 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd = fd; 621 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events = mask; 622 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents = 0; 623 #else 624 internal_error (__FILE__, __LINE__, 625 _("use_poll without HAVE_POLL")); 626 #endif /* HAVE_POLL */ 627 } 628 else 629 { 630 if (mask & GDB_READABLE) 631 FD_SET (fd, &gdb_notifier.check_masks[0]); 632 else 633 FD_CLR (fd, &gdb_notifier.check_masks[0]); 634 635 if (mask & GDB_WRITABLE) 636 FD_SET (fd, &gdb_notifier.check_masks[1]); 637 else 638 FD_CLR (fd, &gdb_notifier.check_masks[1]); 639 640 if (mask & GDB_EXCEPTION) 641 FD_SET (fd, &gdb_notifier.check_masks[2]); 642 else 643 FD_CLR (fd, &gdb_notifier.check_masks[2]); 644 645 if (gdb_notifier.num_fds <= fd) 646 gdb_notifier.num_fds = fd + 1; 647 } 648 } 649 650 file_ptr->proc = proc; 651 file_ptr->client_data = client_data; 652 file_ptr->mask = mask; 653 } 654 655 /* Remove the file descriptor FD from the list of monitored fd's: 656 i.e. we don't care anymore about events on the FD. */ 657 void 658 delete_file_handler (int fd) 659 { 660 file_handler *file_ptr, *prev_ptr = NULL; 661 int i; 662 #ifdef HAVE_POLL 663 int j; 664 struct pollfd *new_poll_fds; 665 #endif 666 667 /* Find the entry for the given file. */ 668 669 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL; 670 file_ptr = file_ptr->next_file) 671 { 672 if (file_ptr->fd == fd) 673 break; 674 } 675 676 if (file_ptr == NULL) 677 return; 678 679 if (use_poll) 680 { 681 #ifdef HAVE_POLL 682 /* Create a new poll_fds array by copying every fd's information 683 but the one we want to get rid of. */ 684 685 new_poll_fds = (struct pollfd *) 686 xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd)); 687 688 for (i = 0, j = 0; i < gdb_notifier.num_fds; i++) 689 { 690 if ((gdb_notifier.poll_fds + i)->fd != fd) 691 { 692 (new_poll_fds + j)->fd = (gdb_notifier.poll_fds + i)->fd; 693 (new_poll_fds + j)->events = (gdb_notifier.poll_fds + i)->events; 694 (new_poll_fds + j)->revents 695 = (gdb_notifier.poll_fds + i)->revents; 696 j++; 697 } 698 } 699 xfree (gdb_notifier.poll_fds); 700 gdb_notifier.poll_fds = new_poll_fds; 701 gdb_notifier.num_fds--; 702 #else 703 internal_error (__FILE__, __LINE__, 704 _("use_poll without HAVE_POLL")); 705 #endif /* HAVE_POLL */ 706 } 707 else 708 { 709 if (file_ptr->mask & GDB_READABLE) 710 FD_CLR (fd, &gdb_notifier.check_masks[0]); 711 if (file_ptr->mask & GDB_WRITABLE) 712 FD_CLR (fd, &gdb_notifier.check_masks[1]); 713 if (file_ptr->mask & GDB_EXCEPTION) 714 FD_CLR (fd, &gdb_notifier.check_masks[2]); 715 716 /* Find current max fd. */ 717 718 if ((fd + 1) == gdb_notifier.num_fds) 719 { 720 gdb_notifier.num_fds--; 721 for (i = gdb_notifier.num_fds; i; i--) 722 { 723 if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0]) 724 || FD_ISSET (i - 1, &gdb_notifier.check_masks[1]) 725 || FD_ISSET (i - 1, &gdb_notifier.check_masks[2])) 726 break; 727 } 728 gdb_notifier.num_fds = i; 729 } 730 } 731 732 /* Deactivate the file descriptor, by clearing its mask, 733 so that it will not fire again. */ 734 735 file_ptr->mask = 0; 736 737 /* Get rid of the file handler in the file handler list. */ 738 if (file_ptr == gdb_notifier.first_file_handler) 739 gdb_notifier.first_file_handler = file_ptr->next_file; 740 else 741 { 742 for (prev_ptr = gdb_notifier.first_file_handler; 743 prev_ptr->next_file != file_ptr; 744 prev_ptr = prev_ptr->next_file) 745 ; 746 prev_ptr->next_file = file_ptr->next_file; 747 } 748 xfree (file_ptr); 749 } 750 751 /* Handle the given event by calling the procedure associated to the 752 corresponding file handler. Called by process_event indirectly, 753 through event_ptr->proc. EVENT_FILE_DESC is file descriptor of the 754 event in the front of the event queue. */ 755 static void 756 handle_file_event (event_data data) 757 { 758 file_handler *file_ptr; 759 int mask; 760 #ifdef HAVE_POLL 761 int error_mask; 762 #endif 763 int event_file_desc = data.integer; 764 765 /* Search the file handler list to find one that matches the fd in 766 the event. */ 767 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL; 768 file_ptr = file_ptr->next_file) 769 { 770 if (file_ptr->fd == event_file_desc) 771 { 772 /* With poll, the ready_mask could have any of three events 773 set to 1: POLLHUP, POLLERR, POLLNVAL. These events 774 cannot be used in the requested event mask (events), but 775 they can be returned in the return mask (revents). We 776 need to check for those event too, and add them to the 777 mask which will be passed to the handler. */ 778 779 /* See if the desired events (mask) match the received 780 events (ready_mask). */ 781 782 if (use_poll) 783 { 784 #ifdef HAVE_POLL 785 /* POLLHUP means EOF, but can be combined with POLLIN to 786 signal more data to read. */ 787 error_mask = POLLHUP | POLLERR | POLLNVAL; 788 mask = file_ptr->ready_mask & (file_ptr->mask | error_mask); 789 790 if ((mask & (POLLERR | POLLNVAL)) != 0) 791 { 792 /* Work in progress. We may need to tell somebody 793 what kind of error we had. */ 794 if (mask & POLLERR) 795 printf_unfiltered (_("Error detected on fd %d\n"), 796 file_ptr->fd); 797 if (mask & POLLNVAL) 798 printf_unfiltered (_("Invalid or non-`poll'able fd %d\n"), 799 file_ptr->fd); 800 file_ptr->error = 1; 801 } 802 else 803 file_ptr->error = 0; 804 #else 805 internal_error (__FILE__, __LINE__, 806 _("use_poll without HAVE_POLL")); 807 #endif /* HAVE_POLL */ 808 } 809 else 810 { 811 if (file_ptr->ready_mask & GDB_EXCEPTION) 812 { 813 printf_unfiltered (_("Exception condition detected " 814 "on fd %d\n"), file_ptr->fd); 815 file_ptr->error = 1; 816 } 817 else 818 file_ptr->error = 0; 819 mask = file_ptr->ready_mask & file_ptr->mask; 820 } 821 822 /* Clear the received events for next time around. */ 823 file_ptr->ready_mask = 0; 824 825 /* If there was a match, then call the handler. */ 826 if (mask != 0) 827 (*file_ptr->proc) (file_ptr->error, file_ptr->client_data); 828 break; 829 } 830 } 831 } 832 833 /* Called by gdb_do_one_event to wait for new events on the monitored 834 file descriptors. Queue file events as they are detected by the 835 poll. If BLOCK and if there are no events, this function will 836 block in the call to poll. Return -1 if there are no file 837 descriptors to monitor, otherwise return 0. */ 838 static int 839 gdb_wait_for_event (int block) 840 { 841 file_handler *file_ptr; 842 gdb_event *file_event_ptr; 843 int num_found = 0; 844 int i; 845 846 /* Make sure all output is done before getting another event. */ 847 gdb_flush (gdb_stdout); 848 gdb_flush (gdb_stderr); 849 850 if (gdb_notifier.num_fds == 0) 851 return -1; 852 853 if (use_poll) 854 { 855 #ifdef HAVE_POLL 856 int timeout; 857 858 if (block) 859 timeout = gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1; 860 else 861 timeout = 0; 862 863 num_found = poll (gdb_notifier.poll_fds, 864 (unsigned long) gdb_notifier.num_fds, timeout); 865 866 /* Don't print anything if we get out of poll because of a 867 signal. */ 868 if (num_found == -1 && errno != EINTR) 869 perror_with_name (("poll")); 870 #else 871 internal_error (__FILE__, __LINE__, 872 _("use_poll without HAVE_POLL")); 873 #endif /* HAVE_POLL */ 874 } 875 else 876 { 877 struct timeval select_timeout; 878 struct timeval *timeout_p; 879 880 if (block) 881 timeout_p = gdb_notifier.timeout_valid 882 ? &gdb_notifier.select_timeout : NULL; 883 else 884 { 885 memset (&select_timeout, 0, sizeof (select_timeout)); 886 timeout_p = &select_timeout; 887 } 888 889 gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0]; 890 gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1]; 891 gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2]; 892 num_found = gdb_select (gdb_notifier.num_fds, 893 &gdb_notifier.ready_masks[0], 894 &gdb_notifier.ready_masks[1], 895 &gdb_notifier.ready_masks[2], 896 timeout_p); 897 898 /* Clear the masks after an error from select. */ 899 if (num_found == -1) 900 { 901 FD_ZERO (&gdb_notifier.ready_masks[0]); 902 FD_ZERO (&gdb_notifier.ready_masks[1]); 903 FD_ZERO (&gdb_notifier.ready_masks[2]); 904 905 /* Dont print anything if we got a signal, let gdb handle 906 it. */ 907 if (errno != EINTR) 908 perror_with_name (("select")); 909 } 910 } 911 912 /* Enqueue all detected file events. */ 913 914 if (use_poll) 915 { 916 #ifdef HAVE_POLL 917 for (i = 0; (i < gdb_notifier.num_fds) && (num_found > 0); i++) 918 { 919 if ((gdb_notifier.poll_fds + i)->revents) 920 num_found--; 921 else 922 continue; 923 924 for (file_ptr = gdb_notifier.first_file_handler; 925 file_ptr != NULL; 926 file_ptr = file_ptr->next_file) 927 { 928 if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd) 929 break; 930 } 931 932 if (file_ptr) 933 { 934 /* Enqueue an event only if this is still a new event for 935 this fd. */ 936 if (file_ptr->ready_mask == 0) 937 { 938 file_event_ptr = create_file_event (file_ptr->fd); 939 async_queue_event (file_event_ptr, TAIL); 940 } 941 file_ptr->ready_mask = (gdb_notifier.poll_fds + i)->revents; 942 } 943 } 944 #else 945 internal_error (__FILE__, __LINE__, 946 _("use_poll without HAVE_POLL")); 947 #endif /* HAVE_POLL */ 948 } 949 else 950 { 951 for (file_ptr = gdb_notifier.first_file_handler; 952 (file_ptr != NULL) && (num_found > 0); 953 file_ptr = file_ptr->next_file) 954 { 955 int mask = 0; 956 957 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0])) 958 mask |= GDB_READABLE; 959 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1])) 960 mask |= GDB_WRITABLE; 961 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2])) 962 mask |= GDB_EXCEPTION; 963 964 if (!mask) 965 continue; 966 else 967 num_found--; 968 969 /* Enqueue an event only if this is still a new event for 970 this fd. */ 971 972 if (file_ptr->ready_mask == 0) 973 { 974 file_event_ptr = create_file_event (file_ptr->fd); 975 async_queue_event (file_event_ptr, TAIL); 976 } 977 file_ptr->ready_mask = mask; 978 } 979 } 980 return 0; 981 } 982 983 984 /* Create an asynchronous handler, allocating memory for it. 985 Return a pointer to the newly created handler. 986 This pointer will be used to invoke the handler by 987 invoke_async_signal_handler. 988 PROC is the function to call with CLIENT_DATA argument 989 whenever the handler is invoked. */ 990 async_signal_handler * 991 create_async_signal_handler (sig_handler_func * proc, 992 gdb_client_data client_data) 993 { 994 async_signal_handler *async_handler_ptr; 995 996 async_handler_ptr = 997 (async_signal_handler *) xmalloc (sizeof (async_signal_handler)); 998 async_handler_ptr->ready = 0; 999 async_handler_ptr->next_handler = NULL; 1000 async_handler_ptr->proc = proc; 1001 async_handler_ptr->client_data = client_data; 1002 if (sighandler_list.first_handler == NULL) 1003 sighandler_list.first_handler = async_handler_ptr; 1004 else 1005 sighandler_list.last_handler->next_handler = async_handler_ptr; 1006 sighandler_list.last_handler = async_handler_ptr; 1007 return async_handler_ptr; 1008 } 1009 1010 /* Call the handler from HANDLER immediately. This function runs 1011 signal handlers when returning to the event loop would be too 1012 slow. */ 1013 void 1014 call_async_signal_handler (struct async_signal_handler *handler) 1015 { 1016 (*handler->proc) (handler->client_data); 1017 } 1018 1019 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information 1020 will be used when the handlers are invoked, after we have waited 1021 for some event. The caller of this function is the interrupt 1022 handler associated with a signal. */ 1023 void 1024 mark_async_signal_handler (async_signal_handler * async_handler_ptr) 1025 { 1026 async_handler_ptr->ready = 1; 1027 } 1028 1029 /* Call all the handlers that are ready. Returns true if any was 1030 indeed ready. */ 1031 static int 1032 invoke_async_signal_handlers (void) 1033 { 1034 async_signal_handler *async_handler_ptr; 1035 int any_ready = 0; 1036 1037 /* Invoke ready handlers. */ 1038 1039 while (1) 1040 { 1041 for (async_handler_ptr = sighandler_list.first_handler; 1042 async_handler_ptr != NULL; 1043 async_handler_ptr = async_handler_ptr->next_handler) 1044 { 1045 if (async_handler_ptr->ready) 1046 break; 1047 } 1048 if (async_handler_ptr == NULL) 1049 break; 1050 any_ready = 1; 1051 async_handler_ptr->ready = 0; 1052 (*async_handler_ptr->proc) (async_handler_ptr->client_data); 1053 } 1054 1055 return any_ready; 1056 } 1057 1058 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR). 1059 Free the space allocated for it. */ 1060 void 1061 delete_async_signal_handler (async_signal_handler ** async_handler_ptr) 1062 { 1063 async_signal_handler *prev_ptr; 1064 1065 if (sighandler_list.first_handler == (*async_handler_ptr)) 1066 { 1067 sighandler_list.first_handler = (*async_handler_ptr)->next_handler; 1068 if (sighandler_list.first_handler == NULL) 1069 sighandler_list.last_handler = NULL; 1070 } 1071 else 1072 { 1073 prev_ptr = sighandler_list.first_handler; 1074 while (prev_ptr && prev_ptr->next_handler != (*async_handler_ptr)) 1075 prev_ptr = prev_ptr->next_handler; 1076 gdb_assert (prev_ptr); 1077 prev_ptr->next_handler = (*async_handler_ptr)->next_handler; 1078 if (sighandler_list.last_handler == (*async_handler_ptr)) 1079 sighandler_list.last_handler = prev_ptr; 1080 } 1081 xfree ((*async_handler_ptr)); 1082 (*async_handler_ptr) = NULL; 1083 } 1084 1085 /* Create an asynchronous event handler, allocating memory for it. 1086 Return a pointer to the newly created handler. PROC is the 1087 function to call with CLIENT_DATA argument whenever the handler is 1088 invoked. */ 1089 async_event_handler * 1090 create_async_event_handler (async_event_handler_func *proc, 1091 gdb_client_data client_data) 1092 { 1093 async_event_handler *h; 1094 1095 h = xmalloc (sizeof (*h)); 1096 h->ready = 0; 1097 h->next_handler = NULL; 1098 h->proc = proc; 1099 h->client_data = client_data; 1100 if (async_event_handler_list.first_handler == NULL) 1101 async_event_handler_list.first_handler = h; 1102 else 1103 async_event_handler_list.last_handler->next_handler = h; 1104 async_event_handler_list.last_handler = h; 1105 return h; 1106 } 1107 1108 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information 1109 will be used by gdb_do_one_event. The caller will be whoever 1110 created the event source, and wants to signal that the event is 1111 ready to be handled. */ 1112 void 1113 mark_async_event_handler (async_event_handler *async_handler_ptr) 1114 { 1115 async_handler_ptr->ready = 1; 1116 } 1117 1118 struct async_event_handler_data 1119 { 1120 async_event_handler_func* proc; 1121 gdb_client_data client_data; 1122 }; 1123 1124 static void 1125 invoke_async_event_handler (event_data data) 1126 { 1127 struct async_event_handler_data *hdata = data.ptr; 1128 async_event_handler_func* proc = hdata->proc; 1129 gdb_client_data client_data = hdata->client_data; 1130 1131 xfree (hdata); 1132 (*proc) (client_data); 1133 } 1134 1135 /* Check if any asynchronous event handlers are ready, and queue 1136 events in the ready queue for any that are. */ 1137 static void 1138 check_async_event_handlers (void) 1139 { 1140 async_event_handler *async_handler_ptr; 1141 struct async_event_handler_data *hdata; 1142 struct gdb_event *event_ptr; 1143 event_data data; 1144 1145 for (async_handler_ptr = async_event_handler_list.first_handler; 1146 async_handler_ptr != NULL; 1147 async_handler_ptr = async_handler_ptr->next_handler) 1148 { 1149 if (async_handler_ptr->ready) 1150 { 1151 async_handler_ptr->ready = 0; 1152 1153 hdata = xmalloc (sizeof (*hdata)); 1154 1155 hdata->proc = async_handler_ptr->proc; 1156 hdata->client_data = async_handler_ptr->client_data; 1157 1158 data.ptr = hdata; 1159 1160 event_ptr = create_event (invoke_async_event_handler, data); 1161 async_queue_event (event_ptr, TAIL); 1162 } 1163 } 1164 } 1165 1166 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR). 1167 Free the space allocated for it. */ 1168 void 1169 delete_async_event_handler (async_event_handler **async_handler_ptr) 1170 { 1171 async_event_handler *prev_ptr; 1172 1173 if (async_event_handler_list.first_handler == *async_handler_ptr) 1174 { 1175 async_event_handler_list.first_handler 1176 = (*async_handler_ptr)->next_handler; 1177 if (async_event_handler_list.first_handler == NULL) 1178 async_event_handler_list.last_handler = NULL; 1179 } 1180 else 1181 { 1182 prev_ptr = async_event_handler_list.first_handler; 1183 while (prev_ptr && prev_ptr->next_handler != *async_handler_ptr) 1184 prev_ptr = prev_ptr->next_handler; 1185 gdb_assert (prev_ptr); 1186 prev_ptr->next_handler = (*async_handler_ptr)->next_handler; 1187 if (async_event_handler_list.last_handler == (*async_handler_ptr)) 1188 async_event_handler_list.last_handler = prev_ptr; 1189 } 1190 xfree (*async_handler_ptr); 1191 *async_handler_ptr = NULL; 1192 } 1193 1194 /* Create a timer that will expire in MILLISECONDS from now. When the 1195 timer is ready, PROC will be executed. At creation, the timer is 1196 aded to the timers queue. This queue is kept sorted in order of 1197 increasing timers. Return a handle to the timer struct. */ 1198 int 1199 create_timer (int milliseconds, timer_handler_func * proc, 1200 gdb_client_data client_data) 1201 { 1202 struct gdb_timer *timer_ptr, *timer_index, *prev_timer; 1203 struct timeval time_now, delta; 1204 1205 /* Compute seconds. */ 1206 delta.tv_sec = milliseconds / 1000; 1207 /* Compute microseconds. */ 1208 delta.tv_usec = (milliseconds % 1000) * 1000; 1209 1210 gettimeofday (&time_now, NULL); 1211 1212 timer_ptr = (struct gdb_timer *) xmalloc (sizeof (*timer_ptr)); 1213 timer_ptr->when.tv_sec = time_now.tv_sec + delta.tv_sec; 1214 timer_ptr->when.tv_usec = time_now.tv_usec + delta.tv_usec; 1215 /* Carry? */ 1216 if (timer_ptr->when.tv_usec >= 1000000) 1217 { 1218 timer_ptr->when.tv_sec += 1; 1219 timer_ptr->when.tv_usec -= 1000000; 1220 } 1221 timer_ptr->proc = proc; 1222 timer_ptr->client_data = client_data; 1223 timer_list.num_timers++; 1224 timer_ptr->timer_id = timer_list.num_timers; 1225 1226 /* Now add the timer to the timer queue, making sure it is sorted in 1227 increasing order of expiration. */ 1228 1229 for (timer_index = timer_list.first_timer; 1230 timer_index != NULL; 1231 timer_index = timer_index->next) 1232 { 1233 /* If the seconds field is greater or if it is the same, but the 1234 microsecond field is greater. */ 1235 if ((timer_index->when.tv_sec > timer_ptr->when.tv_sec) 1236 || ((timer_index->when.tv_sec == timer_ptr->when.tv_sec) 1237 && (timer_index->when.tv_usec > timer_ptr->when.tv_usec))) 1238 break; 1239 } 1240 1241 if (timer_index == timer_list.first_timer) 1242 { 1243 timer_ptr->next = timer_list.first_timer; 1244 timer_list.first_timer = timer_ptr; 1245 1246 } 1247 else 1248 { 1249 for (prev_timer = timer_list.first_timer; 1250 prev_timer->next != timer_index; 1251 prev_timer = prev_timer->next) 1252 ; 1253 1254 prev_timer->next = timer_ptr; 1255 timer_ptr->next = timer_index; 1256 } 1257 1258 gdb_notifier.timeout_valid = 0; 1259 return timer_ptr->timer_id; 1260 } 1261 1262 /* There is a chance that the creator of the timer wants to get rid of 1263 it before it expires. */ 1264 void 1265 delete_timer (int id) 1266 { 1267 struct gdb_timer *timer_ptr, *prev_timer = NULL; 1268 1269 /* Find the entry for the given timer. */ 1270 1271 for (timer_ptr = timer_list.first_timer; timer_ptr != NULL; 1272 timer_ptr = timer_ptr->next) 1273 { 1274 if (timer_ptr->timer_id == id) 1275 break; 1276 } 1277 1278 if (timer_ptr == NULL) 1279 return; 1280 /* Get rid of the timer in the timer list. */ 1281 if (timer_ptr == timer_list.first_timer) 1282 timer_list.first_timer = timer_ptr->next; 1283 else 1284 { 1285 for (prev_timer = timer_list.first_timer; 1286 prev_timer->next != timer_ptr; 1287 prev_timer = prev_timer->next) 1288 ; 1289 prev_timer->next = timer_ptr->next; 1290 } 1291 xfree (timer_ptr); 1292 1293 gdb_notifier.timeout_valid = 0; 1294 } 1295 1296 /* When a timer event is put on the event queue, it will be handled by 1297 this function. Just call the associated procedure and delete the 1298 timer event from the event queue. Repeat this for each timer that 1299 has expired. */ 1300 static void 1301 handle_timer_event (event_data dummy) 1302 { 1303 struct timeval time_now; 1304 struct gdb_timer *timer_ptr, *saved_timer; 1305 1306 gettimeofday (&time_now, NULL); 1307 timer_ptr = timer_list.first_timer; 1308 1309 while (timer_ptr != NULL) 1310 { 1311 if ((timer_ptr->when.tv_sec > time_now.tv_sec) 1312 || ((timer_ptr->when.tv_sec == time_now.tv_sec) 1313 && (timer_ptr->when.tv_usec > time_now.tv_usec))) 1314 break; 1315 1316 /* Get rid of the timer from the beginning of the list. */ 1317 timer_list.first_timer = timer_ptr->next; 1318 saved_timer = timer_ptr; 1319 timer_ptr = timer_ptr->next; 1320 /* Call the procedure associated with that timer. */ 1321 (*saved_timer->proc) (saved_timer->client_data); 1322 xfree (saved_timer); 1323 } 1324 1325 gdb_notifier.timeout_valid = 0; 1326 } 1327 1328 /* Check whether any timers in the timers queue are ready. If at least 1329 one timer is ready, stick an event onto the event queue. Even in 1330 case more than one timer is ready, one event is enough, because the 1331 handle_timer_event() will go through the timers list and call the 1332 procedures associated with all that have expired.l Update the 1333 timeout for the select() or poll() as well. */ 1334 static void 1335 poll_timers (void) 1336 { 1337 struct timeval time_now, delta; 1338 gdb_event *event_ptr; 1339 1340 if (timer_list.first_timer != NULL) 1341 { 1342 gettimeofday (&time_now, NULL); 1343 delta.tv_sec = timer_list.first_timer->when.tv_sec - time_now.tv_sec; 1344 delta.tv_usec = timer_list.first_timer->when.tv_usec - time_now.tv_usec; 1345 /* Borrow? */ 1346 if (delta.tv_usec < 0) 1347 { 1348 delta.tv_sec -= 1; 1349 delta.tv_usec += 1000000; 1350 } 1351 1352 /* Oops it expired already. Tell select / poll to return 1353 immediately. (Cannot simply test if delta.tv_sec is negative 1354 because time_t might be unsigned.) */ 1355 if (timer_list.first_timer->when.tv_sec < time_now.tv_sec 1356 || (timer_list.first_timer->when.tv_sec == time_now.tv_sec 1357 && timer_list.first_timer->when.tv_usec < time_now.tv_usec)) 1358 { 1359 delta.tv_sec = 0; 1360 delta.tv_usec = 0; 1361 } 1362 1363 if (delta.tv_sec == 0 && delta.tv_usec == 0) 1364 { 1365 event_ptr = (gdb_event *) xmalloc (sizeof (gdb_event)); 1366 event_ptr->proc = handle_timer_event; 1367 event_ptr->data.integer = timer_list.first_timer->timer_id; 1368 async_queue_event (event_ptr, TAIL); 1369 } 1370 1371 /* Now we need to update the timeout for select/ poll, because 1372 we don't want to sit there while this timer is expiring. */ 1373 if (use_poll) 1374 { 1375 #ifdef HAVE_POLL 1376 gdb_notifier.poll_timeout = delta.tv_sec * 1000; 1377 #else 1378 internal_error (__FILE__, __LINE__, 1379 _("use_poll without HAVE_POLL")); 1380 #endif /* HAVE_POLL */ 1381 } 1382 else 1383 { 1384 gdb_notifier.select_timeout.tv_sec = delta.tv_sec; 1385 gdb_notifier.select_timeout.tv_usec = delta.tv_usec; 1386 } 1387 gdb_notifier.timeout_valid = 1; 1388 } 1389 else 1390 gdb_notifier.timeout_valid = 0; 1391 } 1392