1130803Smarcel/* Event loop machinery for GDB, the GNU debugger. 2130803Smarcel Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc. 3130803Smarcel Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions. 4130803Smarcel 5130803Smarcel This file is part of GDB. 6130803Smarcel 7130803Smarcel This program is free software; you can redistribute it and/or modify 8130803Smarcel it under the terms of the GNU General Public License as published by 9130803Smarcel the Free Software Foundation; either version 2 of the License, or 10130803Smarcel (at your option) any later version. 11130803Smarcel 12130803Smarcel This program is distributed in the hope that it will be useful, 13130803Smarcel but WITHOUT ANY WARRANTY; without even the implied warranty of 14130803Smarcel MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15130803Smarcel GNU General Public License for more details. 16130803Smarcel 17130803Smarcel You should have received a copy of the GNU General Public License 18130803Smarcel along with this program; if not, write to the Free Software 19130803Smarcel Foundation, Inc., 59 Temple Place - Suite 330, 20130803Smarcel Boston, MA 02111-1307, USA. */ 21130803Smarcel 22130803Smarcel#include "defs.h" 23130803Smarcel#include "event-loop.h" 24130803Smarcel#include "event-top.h" 25130803Smarcel 26130803Smarcel#ifdef HAVE_POLL 27130803Smarcel#if defined (HAVE_POLL_H) 28130803Smarcel#include <poll.h> 29130803Smarcel#elif defined (HAVE_SYS_POLL_H) 30130803Smarcel#include <sys/poll.h> 31130803Smarcel#endif 32130803Smarcel#endif 33130803Smarcel 34130803Smarcel#include <sys/types.h> 35130803Smarcel#include "gdb_string.h" 36130803Smarcel#include <errno.h> 37130803Smarcel#include <sys/time.h> 38130803Smarcel 39130803Smarceltypedef struct gdb_event gdb_event; 40130803Smarceltypedef void (event_handler_func) (int); 41130803Smarcel 42130803Smarcel/* Event for the GDB event system. Events are queued by calling 43130803Smarcel async_queue_event and serviced later on by gdb_do_one_event. An 44130803Smarcel event can be, for instance, a file descriptor becoming ready to be 45130803Smarcel read. Servicing an event simply means that the procedure PROC will 46130803Smarcel be called. We have 2 queues, one for file handlers that we listen 47130803Smarcel to in the event loop, and one for the file handlers+events that are 48130803Smarcel ready. The procedure PROC associated with each event is always the 49130803Smarcel same (handle_file_event). Its duty is to invoke the handler 50130803Smarcel associated with the file descriptor whose state change generated 51130803Smarcel the event, plus doing other cleanups and such. */ 52130803Smarcel 53130803Smarcelstruct gdb_event 54130803Smarcel { 55130803Smarcel event_handler_func *proc; /* Procedure to call to service this event. */ 56130803Smarcel int fd; /* File descriptor that is ready. */ 57130803Smarcel struct gdb_event *next_event; /* Next in list of events or NULL. */ 58130803Smarcel }; 59130803Smarcel 60130803Smarcel/* Information about each file descriptor we register with the event 61130803Smarcel loop. */ 62130803Smarcel 63130803Smarceltypedef struct file_handler 64130803Smarcel { 65130803Smarcel int fd; /* File descriptor. */ 66130803Smarcel int mask; /* Events we want to monitor: POLLIN, etc. */ 67130803Smarcel int ready_mask; /* Events that have been seen since 68130803Smarcel the last time. */ 69130803Smarcel handler_func *proc; /* Procedure to call when fd is ready. */ 70130803Smarcel gdb_client_data client_data; /* Argument to pass to proc. */ 71130803Smarcel int error; /* Was an error detected on this fd? */ 72130803Smarcel struct file_handler *next_file; /* Next registered file descriptor. */ 73130803Smarcel } 74130803Smarcelfile_handler; 75130803Smarcel 76130803Smarcel/* PROC is a function to be invoked when the READY flag is set. This 77130803Smarcel happens when there has been a signal and the corresponding signal 78130803Smarcel handler has 'triggered' this async_signal_handler for 79130803Smarcel execution. The actual work to be done in response to a signal will 80130803Smarcel be carried out by PROC at a later time, within process_event. This 81130803Smarcel provides a deferred execution of signal handlers. 82130803Smarcel Async_init_signals takes care of setting up such an 83130803Smarcel asyn_signal_handler for each interesting signal. */ 84130803Smarceltypedef struct async_signal_handler 85130803Smarcel { 86130803Smarcel int ready; /* If ready, call this handler from the main event loop, 87130803Smarcel using invoke_async_handler. */ 88130803Smarcel struct async_signal_handler *next_handler; /* Ptr to next handler */ 89130803Smarcel sig_handler_func *proc; /* Function to call to do the work */ 90130803Smarcel gdb_client_data client_data; /* Argument to async_handler_func */ 91130803Smarcel } 92130803Smarcelasync_signal_handler; 93130803Smarcel 94130803Smarcel 95130803Smarcel/* Event queue: 96130803Smarcel - the first event in the queue is the head of the queue. 97130803Smarcel It will be the next to be serviced. 98130803Smarcel - the last event in the queue 99130803Smarcel 100130803Smarcel Events can be inserted at the front of the queue or at the end of 101130803Smarcel the queue. Events will be extracted from the queue for processing 102130803Smarcel starting from the head. Therefore, events inserted at the head of 103130803Smarcel the queue will be processed in a last in first out fashion, while 104130803Smarcel those inserted at the tail of the queue will be processed in a first 105130803Smarcel in first out manner. All the fields are NULL if the queue is 106130803Smarcel empty. */ 107130803Smarcel 108130803Smarcelstatic struct 109130803Smarcel { 110130803Smarcel gdb_event *first_event; /* First pending event */ 111130803Smarcel gdb_event *last_event; /* Last pending event */ 112130803Smarcel } 113130803Smarcelevent_queue; 114130803Smarcel 115130803Smarcel/* Gdb_notifier is just a list of file descriptors gdb is interested in. 116130803Smarcel These are the input file descriptor, and the target file 117130803Smarcel descriptor. We have two flavors of the notifier, one for platforms 118130803Smarcel that have the POLL function, the other for those that don't, and 119130803Smarcel only support SELECT. Each of the elements in the gdb_notifier list is 120130803Smarcel basically a description of what kind of events gdb is interested 121130803Smarcel in, for each fd. */ 122130803Smarcel 123130803Smarcel/* As of 1999-04-30 only the input file descriptor is registered with the 124130803Smarcel event loop. */ 125130803Smarcel 126130803Smarcel/* Do we use poll or select ? */ 127130803Smarcel#ifdef HAVE_POLL 128130803Smarcel#define USE_POLL 1 129130803Smarcel#else 130130803Smarcel#define USE_POLL 0 131130803Smarcel#endif /* HAVE_POLL */ 132130803Smarcel 133130803Smarcelstatic unsigned char use_poll = USE_POLL; 134130803Smarcel 135130803Smarcelstatic struct 136130803Smarcel { 137130803Smarcel /* Ptr to head of file handler list. */ 138130803Smarcel file_handler *first_file_handler; 139130803Smarcel 140130803Smarcel#ifdef HAVE_POLL 141130803Smarcel /* Ptr to array of pollfd structures. */ 142130803Smarcel struct pollfd *poll_fds; 143130803Smarcel 144130803Smarcel /* Timeout in milliseconds for calls to poll(). */ 145130803Smarcel int poll_timeout; 146130803Smarcel#endif 147130803Smarcel 148130803Smarcel /* Masks to be used in the next call to select. 149130803Smarcel Bits are set in response to calls to create_file_handler. */ 150130803Smarcel fd_set check_masks[3]; 151130803Smarcel 152130803Smarcel /* What file descriptors were found ready by select. */ 153130803Smarcel fd_set ready_masks[3]; 154130803Smarcel 155130803Smarcel /* Number of file descriptors to monitor. (for poll) */ 156130803Smarcel /* Number of valid bits (highest fd value + 1). (for select) */ 157130803Smarcel int num_fds; 158130803Smarcel 159130803Smarcel /* Time structure for calls to select(). */ 160130803Smarcel struct timeval select_timeout; 161130803Smarcel 162130803Smarcel /* Flag to tell whether the timeout should be used. */ 163130803Smarcel int timeout_valid; 164130803Smarcel } 165130803Smarcelgdb_notifier; 166130803Smarcel 167130803Smarcel/* Structure associated with a timer. PROC will be executed at the 168130803Smarcel first occasion after WHEN. */ 169130803Smarcelstruct gdb_timer 170130803Smarcel { 171130803Smarcel struct timeval when; 172130803Smarcel int timer_id; 173130803Smarcel struct gdb_timer *next; 174130803Smarcel timer_handler_func *proc; /* Function to call to do the work */ 175130803Smarcel gdb_client_data client_data; /* Argument to async_handler_func */ 176130803Smarcel } 177130803Smarcelgdb_timer; 178130803Smarcel 179130803Smarcel/* List of currently active timers. It is sorted in order of 180130803Smarcel increasing timers. */ 181130803Smarcelstatic struct 182130803Smarcel { 183130803Smarcel /* Pointer to first in timer list. */ 184130803Smarcel struct gdb_timer *first_timer; 185130803Smarcel 186130803Smarcel /* Id of the last timer created. */ 187130803Smarcel int num_timers; 188130803Smarcel } 189130803Smarceltimer_list; 190130803Smarcel 191130803Smarcel/* All the async_signal_handlers gdb is interested in are kept onto 192130803Smarcel this list. */ 193130803Smarcelstatic struct 194130803Smarcel { 195130803Smarcel /* Pointer to first in handler list. */ 196130803Smarcel async_signal_handler *first_handler; 197130803Smarcel 198130803Smarcel /* Pointer to last in handler list. */ 199130803Smarcel async_signal_handler *last_handler; 200130803Smarcel } 201130803Smarcelsighandler_list; 202130803Smarcel 203130803Smarcel/* Are any of the handlers ready? Check this variable using 204130803Smarcel check_async_ready. This is used by process_event, to determine 205130803Smarcel whether or not to invoke the invoke_async_signal_handler 206130803Smarcel function. */ 207130803Smarcelstatic int async_handler_ready = 0; 208130803Smarcel 209130803Smarcelstatic void create_file_handler (int fd, int mask, handler_func * proc, gdb_client_data client_data); 210130803Smarcelstatic void invoke_async_signal_handler (void); 211130803Smarcelstatic void handle_file_event (int event_file_desc); 212130803Smarcelstatic int gdb_wait_for_event (void); 213130803Smarcelstatic int check_async_ready (void); 214130803Smarcelstatic void async_queue_event (gdb_event * event_ptr, queue_position position); 215130803Smarcelstatic gdb_event *create_file_event (int fd); 216130803Smarcelstatic int process_event (void); 217130803Smarcelstatic void handle_timer_event (int dummy); 218130803Smarcelstatic void poll_timers (void); 219130803Smarcel 220130803Smarcel 221130803Smarcel/* Insert an event object into the gdb event queue at 222130803Smarcel the specified position. 223130803Smarcel POSITION can be head or tail, with values TAIL, HEAD. 224130803Smarcel EVENT_PTR points to the event to be inserted into the queue. 225130803Smarcel The caller must allocate memory for the event. It is freed 226130803Smarcel after the event has ben handled. 227130803Smarcel Events in the queue will be processed head to tail, therefore, 228130803Smarcel events inserted at the head of the queue will be processed 229130803Smarcel as last in first out. Event appended at the tail of the queue 230130803Smarcel will be processed first in first out. */ 231130803Smarcelstatic void 232130803Smarcelasync_queue_event (gdb_event * event_ptr, queue_position position) 233130803Smarcel{ 234130803Smarcel if (position == TAIL) 235130803Smarcel { 236130803Smarcel /* The event will become the new last_event. */ 237130803Smarcel 238130803Smarcel event_ptr->next_event = NULL; 239130803Smarcel if (event_queue.first_event == NULL) 240130803Smarcel event_queue.first_event = event_ptr; 241130803Smarcel else 242130803Smarcel event_queue.last_event->next_event = event_ptr; 243130803Smarcel event_queue.last_event = event_ptr; 244130803Smarcel } 245130803Smarcel else if (position == HEAD) 246130803Smarcel { 247130803Smarcel /* The event becomes the new first_event. */ 248130803Smarcel 249130803Smarcel event_ptr->next_event = event_queue.first_event; 250130803Smarcel if (event_queue.first_event == NULL) 251130803Smarcel event_queue.last_event = event_ptr; 252130803Smarcel event_queue.first_event = event_ptr; 253130803Smarcel } 254130803Smarcel} 255130803Smarcel 256130803Smarcel/* Create a file event, to be enqueued in the event queue for 257130803Smarcel processing. The procedure associated to this event is always 258130803Smarcel handle_file_event, which will in turn invoke the one that was 259130803Smarcel associated to FD when it was registered with the event loop. */ 260130803Smarcelstatic gdb_event * 261130803Smarcelcreate_file_event (int fd) 262130803Smarcel{ 263130803Smarcel gdb_event *file_event_ptr; 264130803Smarcel 265130803Smarcel file_event_ptr = (gdb_event *) xmalloc (sizeof (gdb_event)); 266130803Smarcel file_event_ptr->proc = handle_file_event; 267130803Smarcel file_event_ptr->fd = fd; 268130803Smarcel return (file_event_ptr); 269130803Smarcel} 270130803Smarcel 271130803Smarcel/* Process one event. 272130803Smarcel The event can be the next one to be serviced in the event queue, 273130803Smarcel or an asynchronous event handler can be invoked in response to 274130803Smarcel the reception of a signal. 275130803Smarcel If an event was processed (either way), 1 is returned otherwise 276130803Smarcel 0 is returned. 277130803Smarcel Scan the queue from head to tail, processing therefore the high 278130803Smarcel priority events first, by invoking the associated event handler 279130803Smarcel procedure. */ 280130803Smarcelstatic int 281130803Smarcelprocess_event (void) 282130803Smarcel{ 283130803Smarcel gdb_event *event_ptr, *prev_ptr; 284130803Smarcel event_handler_func *proc; 285130803Smarcel int fd; 286130803Smarcel 287130803Smarcel /* First let's see if there are any asynchronous event handlers that 288130803Smarcel are ready. These would be the result of invoking any of the 289130803Smarcel signal handlers. */ 290130803Smarcel 291130803Smarcel if (check_async_ready ()) 292130803Smarcel { 293130803Smarcel invoke_async_signal_handler (); 294130803Smarcel return 1; 295130803Smarcel } 296130803Smarcel 297130803Smarcel /* Look in the event queue to find an event that is ready 298130803Smarcel to be processed. */ 299130803Smarcel 300130803Smarcel for (event_ptr = event_queue.first_event; event_ptr != NULL; 301130803Smarcel event_ptr = event_ptr->next_event) 302130803Smarcel { 303130803Smarcel /* Call the handler for the event. */ 304130803Smarcel 305130803Smarcel proc = event_ptr->proc; 306130803Smarcel fd = event_ptr->fd; 307130803Smarcel 308130803Smarcel /* Let's get rid of the event from the event queue. We need to 309130803Smarcel do this now because while processing the event, the proc 310130803Smarcel function could end up calling 'error' and therefore jump out 311130803Smarcel to the caller of this function, gdb_do_one_event. In that 312130803Smarcel case, we would have on the event queue an event wich has been 313130803Smarcel processed, but not deleted. */ 314130803Smarcel 315130803Smarcel if (event_queue.first_event == event_ptr) 316130803Smarcel { 317130803Smarcel event_queue.first_event = event_ptr->next_event; 318130803Smarcel if (event_ptr->next_event == NULL) 319130803Smarcel event_queue.last_event = NULL; 320130803Smarcel } 321130803Smarcel else 322130803Smarcel { 323130803Smarcel prev_ptr = event_queue.first_event; 324130803Smarcel while (prev_ptr->next_event != event_ptr) 325130803Smarcel prev_ptr = prev_ptr->next_event; 326130803Smarcel 327130803Smarcel prev_ptr->next_event = event_ptr->next_event; 328130803Smarcel if (event_ptr->next_event == NULL) 329130803Smarcel event_queue.last_event = prev_ptr; 330130803Smarcel } 331130803Smarcel xfree (event_ptr); 332130803Smarcel 333130803Smarcel /* Now call the procedure associated with the event. */ 334130803Smarcel (*proc) (fd); 335130803Smarcel return 1; 336130803Smarcel } 337130803Smarcel 338130803Smarcel /* this is the case if there are no event on the event queue. */ 339130803Smarcel return 0; 340130803Smarcel} 341130803Smarcel 342130803Smarcel/* Process one high level event. If nothing is ready at this time, 343130803Smarcel wait for something to happen (via gdb_wait_for_event), then process 344130803Smarcel it. Returns >0 if something was done otherwise returns <0 (this 345130803Smarcel can happen if there are no event sources to wait for). If an error 346130803Smarcel occurs catch_errors() which calls this function returns zero. */ 347130803Smarcel 348130803Smarcelint 349130803Smarcelgdb_do_one_event (void *data) 350130803Smarcel{ 351130803Smarcel /* Any events already waiting in the queue? */ 352130803Smarcel if (process_event ()) 353130803Smarcel { 354130803Smarcel return 1; 355130803Smarcel } 356130803Smarcel 357130803Smarcel /* Are any timers that are ready? If so, put an event on the queue. */ 358130803Smarcel poll_timers (); 359130803Smarcel 360130803Smarcel /* Wait for a new event. If gdb_wait_for_event returns -1, 361130803Smarcel we should get out because this means that there are no 362130803Smarcel event sources left. This will make the event loop stop, 363130803Smarcel and the application exit. */ 364130803Smarcel 365130803Smarcel if (gdb_wait_for_event () < 0) 366130803Smarcel { 367130803Smarcel return -1; 368130803Smarcel } 369130803Smarcel 370130803Smarcel /* Handle any new events occurred while waiting. */ 371130803Smarcel if (process_event ()) 372130803Smarcel { 373130803Smarcel return 1; 374130803Smarcel } 375130803Smarcel 376130803Smarcel /* If gdb_wait_for_event has returned 1, it means that one 377130803Smarcel event has been handled. We break out of the loop. */ 378130803Smarcel return 1; 379130803Smarcel} 380130803Smarcel 381130803Smarcel/* Start up the event loop. This is the entry point to the event loop 382130803Smarcel from the command loop. */ 383130803Smarcel 384130803Smarcelvoid 385130803Smarcelstart_event_loop (void) 386130803Smarcel{ 387130803Smarcel /* Loop until there is nothing to do. This is the entry point to the 388130803Smarcel event loop engine. gdb_do_one_event, called via catch_errors() 389130803Smarcel will process one event for each invocation. It blocks waits for 390130803Smarcel an event and then processes it. >0 when an event is processed, 0 391130803Smarcel when catch_errors() caught an error and <0 when there are no 392130803Smarcel longer any event sources registered. */ 393130803Smarcel while (1) 394130803Smarcel { 395130803Smarcel int gdb_result; 396130803Smarcel 397130803Smarcel gdb_result = catch_errors (gdb_do_one_event, 0, "", RETURN_MASK_ALL); 398130803Smarcel if (gdb_result < 0) 399130803Smarcel break; 400130803Smarcel 401130803Smarcel /* If we long-jumped out of do_one_event, we probably 402130803Smarcel didn't get around to resetting the prompt, which leaves 403130803Smarcel readline in a messed-up state. Reset it here. */ 404130803Smarcel 405130803Smarcel if (gdb_result == 0) 406130803Smarcel { 407130803Smarcel /* FIXME: this should really be a call to a hook that is 408130803Smarcel interface specific, because interfaces can display the 409130803Smarcel prompt in their own way. */ 410130803Smarcel display_gdb_prompt (0); 411130803Smarcel /* This call looks bizarre, but it is required. If the user 412130803Smarcel entered a command that caused an error, 413130803Smarcel after_char_processing_hook won't be called from 414130803Smarcel rl_callback_read_char_wrapper. Using a cleanup there 415130803Smarcel won't work, since we want this function to be called 416130803Smarcel after a new prompt is printed. */ 417130803Smarcel if (after_char_processing_hook) 418130803Smarcel (*after_char_processing_hook) (); 419130803Smarcel /* Maybe better to set a flag to be checked somewhere as to 420130803Smarcel whether display the prompt or not. */ 421130803Smarcel } 422130803Smarcel } 423130803Smarcel 424130803Smarcel /* We are done with the event loop. There are no more event sources 425130803Smarcel to listen to. So we exit GDB. */ 426130803Smarcel return; 427130803Smarcel} 428130803Smarcel 429130803Smarcel 430130803Smarcel/* Wrapper function for create_file_handler, so that the caller 431130803Smarcel doesn't have to know implementation details about the use of poll 432130803Smarcel vs. select. */ 433130803Smarcelvoid 434130803Smarceladd_file_handler (int fd, handler_func * proc, gdb_client_data client_data) 435130803Smarcel{ 436130803Smarcel#ifdef HAVE_POLL 437130803Smarcel struct pollfd fds; 438130803Smarcel#endif 439130803Smarcel 440130803Smarcel if (use_poll) 441130803Smarcel { 442130803Smarcel#ifdef HAVE_POLL 443130803Smarcel /* Check to see if poll () is usable. If not, we'll switch to 444130803Smarcel use select. This can happen on systems like 445130803Smarcel m68k-motorola-sys, `poll' cannot be used to wait for `stdin'. 446130803Smarcel On m68k-motorola-sysv, tty's are not stream-based and not 447130803Smarcel `poll'able. */ 448130803Smarcel fds.fd = fd; 449130803Smarcel fds.events = POLLIN; 450130803Smarcel if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL)) 451130803Smarcel use_poll = 0; 452130803Smarcel#else 453130803Smarcel internal_error (__FILE__, __LINE__, 454130803Smarcel "use_poll without HAVE_POLL"); 455130803Smarcel#endif /* HAVE_POLL */ 456130803Smarcel } 457130803Smarcel if (use_poll) 458130803Smarcel { 459130803Smarcel#ifdef HAVE_POLL 460130803Smarcel create_file_handler (fd, POLLIN, proc, client_data); 461130803Smarcel#else 462130803Smarcel internal_error (__FILE__, __LINE__, 463130803Smarcel "use_poll without HAVE_POLL"); 464130803Smarcel#endif 465130803Smarcel } 466130803Smarcel else 467130803Smarcel create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION, proc, client_data); 468130803Smarcel} 469130803Smarcel 470130803Smarcel/* Add a file handler/descriptor to the list of descriptors we are 471130803Smarcel interested in. 472130803Smarcel FD is the file descriptor for the file/stream to be listened to. 473130803Smarcel For the poll case, MASK is a combination (OR) of 474130803Smarcel POLLIN, POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, 475130803Smarcel POLLWRBAND: these are the events we are interested in. If any of them 476130803Smarcel occurs, proc should be called. 477130803Smarcel For the select case, MASK is a combination of READABLE, WRITABLE, EXCEPTION. 478130803Smarcel PROC is the procedure that will be called when an event occurs for 479130803Smarcel FD. CLIENT_DATA is the argument to pass to PROC. */ 480130803Smarcelstatic void 481130803Smarcelcreate_file_handler (int fd, int mask, handler_func * proc, gdb_client_data client_data) 482130803Smarcel{ 483130803Smarcel file_handler *file_ptr; 484130803Smarcel 485130803Smarcel /* Do we already have a file handler for this file? (We may be 486130803Smarcel changing its associated procedure). */ 487130803Smarcel for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL; 488130803Smarcel file_ptr = file_ptr->next_file) 489130803Smarcel { 490130803Smarcel if (file_ptr->fd == fd) 491130803Smarcel break; 492130803Smarcel } 493130803Smarcel 494130803Smarcel /* It is a new file descriptor. Add it to the list. Otherwise, just 495130803Smarcel change the data associated with it. */ 496130803Smarcel if (file_ptr == NULL) 497130803Smarcel { 498130803Smarcel file_ptr = (file_handler *) xmalloc (sizeof (file_handler)); 499130803Smarcel file_ptr->fd = fd; 500130803Smarcel file_ptr->ready_mask = 0; 501130803Smarcel file_ptr->next_file = gdb_notifier.first_file_handler; 502130803Smarcel gdb_notifier.first_file_handler = file_ptr; 503130803Smarcel 504130803Smarcel if (use_poll) 505130803Smarcel { 506130803Smarcel#ifdef HAVE_POLL 507130803Smarcel gdb_notifier.num_fds++; 508130803Smarcel if (gdb_notifier.poll_fds) 509130803Smarcel gdb_notifier.poll_fds = 510130803Smarcel (struct pollfd *) xrealloc (gdb_notifier.poll_fds, 511130803Smarcel (gdb_notifier.num_fds 512130803Smarcel * sizeof (struct pollfd))); 513130803Smarcel else 514130803Smarcel gdb_notifier.poll_fds = 515130803Smarcel (struct pollfd *) xmalloc (sizeof (struct pollfd)); 516130803Smarcel (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd = fd; 517130803Smarcel (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events = mask; 518130803Smarcel (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents = 0; 519130803Smarcel#else 520130803Smarcel internal_error (__FILE__, __LINE__, 521130803Smarcel "use_poll without HAVE_POLL"); 522130803Smarcel#endif /* HAVE_POLL */ 523130803Smarcel } 524130803Smarcel else 525130803Smarcel { 526130803Smarcel if (mask & GDB_READABLE) 527130803Smarcel FD_SET (fd, &gdb_notifier.check_masks[0]); 528130803Smarcel else 529130803Smarcel FD_CLR (fd, &gdb_notifier.check_masks[0]); 530130803Smarcel 531130803Smarcel if (mask & GDB_WRITABLE) 532130803Smarcel FD_SET (fd, &gdb_notifier.check_masks[1]); 533130803Smarcel else 534130803Smarcel FD_CLR (fd, &gdb_notifier.check_masks[1]); 535130803Smarcel 536130803Smarcel if (mask & GDB_EXCEPTION) 537130803Smarcel FD_SET (fd, &gdb_notifier.check_masks[2]); 538130803Smarcel else 539130803Smarcel FD_CLR (fd, &gdb_notifier.check_masks[2]); 540130803Smarcel 541130803Smarcel if (gdb_notifier.num_fds <= fd) 542130803Smarcel gdb_notifier.num_fds = fd + 1; 543130803Smarcel } 544130803Smarcel } 545130803Smarcel 546130803Smarcel file_ptr->proc = proc; 547130803Smarcel file_ptr->client_data = client_data; 548130803Smarcel file_ptr->mask = mask; 549130803Smarcel} 550130803Smarcel 551130803Smarcel/* Remove the file descriptor FD from the list of monitored fd's: 552130803Smarcel i.e. we don't care anymore about events on the FD. */ 553130803Smarcelvoid 554130803Smarceldelete_file_handler (int fd) 555130803Smarcel{ 556130803Smarcel file_handler *file_ptr, *prev_ptr = NULL; 557130803Smarcel int i; 558130803Smarcel#ifdef HAVE_POLL 559130803Smarcel int j; 560130803Smarcel struct pollfd *new_poll_fds; 561130803Smarcel#endif 562130803Smarcel 563130803Smarcel /* Find the entry for the given file. */ 564130803Smarcel 565130803Smarcel for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL; 566130803Smarcel file_ptr = file_ptr->next_file) 567130803Smarcel { 568130803Smarcel if (file_ptr->fd == fd) 569130803Smarcel break; 570130803Smarcel } 571130803Smarcel 572130803Smarcel if (file_ptr == NULL) 573130803Smarcel return; 574130803Smarcel 575130803Smarcel if (use_poll) 576130803Smarcel { 577130803Smarcel#ifdef HAVE_POLL 578130803Smarcel /* Create a new poll_fds array by copying every fd's information but the 579130803Smarcel one we want to get rid of. */ 580130803Smarcel 581130803Smarcel new_poll_fds = 582130803Smarcel (struct pollfd *) xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd)); 583130803Smarcel 584130803Smarcel for (i = 0, j = 0; i < gdb_notifier.num_fds; i++) 585130803Smarcel { 586130803Smarcel if ((gdb_notifier.poll_fds + i)->fd != fd) 587130803Smarcel { 588130803Smarcel (new_poll_fds + j)->fd = (gdb_notifier.poll_fds + i)->fd; 589130803Smarcel (new_poll_fds + j)->events = (gdb_notifier.poll_fds + i)->events; 590130803Smarcel (new_poll_fds + j)->revents = (gdb_notifier.poll_fds + i)->revents; 591130803Smarcel j++; 592130803Smarcel } 593130803Smarcel } 594130803Smarcel xfree (gdb_notifier.poll_fds); 595130803Smarcel gdb_notifier.poll_fds = new_poll_fds; 596130803Smarcel gdb_notifier.num_fds--; 597130803Smarcel#else 598130803Smarcel internal_error (__FILE__, __LINE__, 599130803Smarcel "use_poll without HAVE_POLL"); 600130803Smarcel#endif /* HAVE_POLL */ 601130803Smarcel } 602130803Smarcel else 603130803Smarcel { 604130803Smarcel if (file_ptr->mask & GDB_READABLE) 605130803Smarcel FD_CLR (fd, &gdb_notifier.check_masks[0]); 606130803Smarcel if (file_ptr->mask & GDB_WRITABLE) 607130803Smarcel FD_CLR (fd, &gdb_notifier.check_masks[1]); 608130803Smarcel if (file_ptr->mask & GDB_EXCEPTION) 609130803Smarcel FD_CLR (fd, &gdb_notifier.check_masks[2]); 610130803Smarcel 611130803Smarcel /* Find current max fd. */ 612130803Smarcel 613130803Smarcel if ((fd + 1) == gdb_notifier.num_fds) 614130803Smarcel { 615130803Smarcel gdb_notifier.num_fds--; 616130803Smarcel for (i = gdb_notifier.num_fds; i; i--) 617130803Smarcel { 618130803Smarcel if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0]) 619130803Smarcel || FD_ISSET (i - 1, &gdb_notifier.check_masks[1]) 620130803Smarcel || FD_ISSET (i - 1, &gdb_notifier.check_masks[2])) 621130803Smarcel break; 622130803Smarcel } 623130803Smarcel gdb_notifier.num_fds = i; 624130803Smarcel } 625130803Smarcel } 626130803Smarcel 627130803Smarcel /* Deactivate the file descriptor, by clearing its mask, 628130803Smarcel so that it will not fire again. */ 629130803Smarcel 630130803Smarcel file_ptr->mask = 0; 631130803Smarcel 632130803Smarcel /* Get rid of the file handler in the file handler list. */ 633130803Smarcel if (file_ptr == gdb_notifier.first_file_handler) 634130803Smarcel gdb_notifier.first_file_handler = file_ptr->next_file; 635130803Smarcel else 636130803Smarcel { 637130803Smarcel for (prev_ptr = gdb_notifier.first_file_handler; 638130803Smarcel prev_ptr->next_file != file_ptr; 639130803Smarcel prev_ptr = prev_ptr->next_file) 640130803Smarcel ; 641130803Smarcel prev_ptr->next_file = file_ptr->next_file; 642130803Smarcel } 643130803Smarcel xfree (file_ptr); 644130803Smarcel} 645130803Smarcel 646130803Smarcel/* Handle the given event by calling the procedure associated to the 647130803Smarcel corresponding file handler. Called by process_event indirectly, 648130803Smarcel through event_ptr->proc. EVENT_FILE_DESC is file descriptor of the 649130803Smarcel event in the front of the event queue. */ 650130803Smarcelstatic void 651130803Smarcelhandle_file_event (int event_file_desc) 652130803Smarcel{ 653130803Smarcel file_handler *file_ptr; 654130803Smarcel int mask; 655130803Smarcel#ifdef HAVE_POLL 656130803Smarcel int error_mask; 657130803Smarcel int error_mask_returned; 658130803Smarcel#endif 659130803Smarcel 660130803Smarcel /* Search the file handler list to find one that matches the fd in 661130803Smarcel the event. */ 662130803Smarcel for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL; 663130803Smarcel file_ptr = file_ptr->next_file) 664130803Smarcel { 665130803Smarcel if (file_ptr->fd == event_file_desc) 666130803Smarcel { 667130803Smarcel /* With poll, the ready_mask could have any of three events 668130803Smarcel set to 1: POLLHUP, POLLERR, POLLNVAL. These events cannot 669130803Smarcel be used in the requested event mask (events), but they 670130803Smarcel can be returned in the return mask (revents). We need to 671130803Smarcel check for those event too, and add them to the mask which 672130803Smarcel will be passed to the handler. */ 673130803Smarcel 674130803Smarcel /* See if the desired events (mask) match the received 675130803Smarcel events (ready_mask). */ 676130803Smarcel 677130803Smarcel if (use_poll) 678130803Smarcel { 679130803Smarcel#ifdef HAVE_POLL 680130803Smarcel error_mask = POLLHUP | POLLERR | POLLNVAL; 681130803Smarcel mask = (file_ptr->ready_mask & file_ptr->mask) | 682130803Smarcel (file_ptr->ready_mask & error_mask); 683130803Smarcel error_mask_returned = mask & error_mask; 684130803Smarcel 685130803Smarcel if (error_mask_returned != 0) 686130803Smarcel { 687130803Smarcel /* Work in progress. We may need to tell somebody what 688130803Smarcel kind of error we had. */ 689130803Smarcel if (error_mask_returned & POLLHUP) 690130803Smarcel printf_unfiltered ("Hangup detected on fd %d\n", file_ptr->fd); 691130803Smarcel if (error_mask_returned & POLLERR) 692130803Smarcel printf_unfiltered ("Error detected on fd %d\n", file_ptr->fd); 693130803Smarcel if (error_mask_returned & POLLNVAL) 694130803Smarcel printf_unfiltered ("Invalid or non-`poll'able fd %d\n", file_ptr->fd); 695130803Smarcel file_ptr->error = 1; 696130803Smarcel } 697130803Smarcel else 698130803Smarcel file_ptr->error = 0; 699130803Smarcel#else 700130803Smarcel internal_error (__FILE__, __LINE__, 701130803Smarcel "use_poll without HAVE_POLL"); 702130803Smarcel#endif /* HAVE_POLL */ 703130803Smarcel } 704130803Smarcel else 705130803Smarcel { 706130803Smarcel if (file_ptr->ready_mask & GDB_EXCEPTION) 707130803Smarcel { 708130803Smarcel printf_unfiltered ("Exception condition detected on fd %d\n", file_ptr->fd); 709130803Smarcel file_ptr->error = 1; 710130803Smarcel } 711130803Smarcel else 712130803Smarcel file_ptr->error = 0; 713130803Smarcel mask = file_ptr->ready_mask & file_ptr->mask; 714130803Smarcel } 715130803Smarcel 716130803Smarcel /* Clear the received events for next time around. */ 717130803Smarcel file_ptr->ready_mask = 0; 718130803Smarcel 719130803Smarcel /* If there was a match, then call the handler. */ 720130803Smarcel if (mask != 0) 721130803Smarcel (*file_ptr->proc) (file_ptr->error, file_ptr->client_data); 722130803Smarcel break; 723130803Smarcel } 724130803Smarcel } 725130803Smarcel} 726130803Smarcel 727130803Smarcel/* Called by gdb_do_one_event to wait for new events on the 728130803Smarcel monitored file descriptors. Queue file events as they are 729130803Smarcel detected by the poll. 730130803Smarcel If there are no events, this function will block in the 731130803Smarcel call to poll. 732130803Smarcel Return -1 if there are no files descriptors to monitor, 733130803Smarcel otherwise return 0. */ 734130803Smarcelstatic int 735130803Smarcelgdb_wait_for_event (void) 736130803Smarcel{ 737130803Smarcel file_handler *file_ptr; 738130803Smarcel gdb_event *file_event_ptr; 739130803Smarcel int num_found = 0; 740130803Smarcel int i; 741130803Smarcel 742130803Smarcel /* Make sure all output is done before getting another event. */ 743130803Smarcel gdb_flush (gdb_stdout); 744130803Smarcel gdb_flush (gdb_stderr); 745130803Smarcel 746130803Smarcel if (gdb_notifier.num_fds == 0) 747130803Smarcel return -1; 748130803Smarcel 749130803Smarcel if (use_poll) 750130803Smarcel { 751130803Smarcel#ifdef HAVE_POLL 752130803Smarcel num_found = 753130803Smarcel poll (gdb_notifier.poll_fds, 754130803Smarcel (unsigned long) gdb_notifier.num_fds, 755130803Smarcel gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1); 756130803Smarcel 757130803Smarcel /* Don't print anything if we get out of poll because of a 758130803Smarcel signal. */ 759130803Smarcel if (num_found == -1 && errno != EINTR) 760130803Smarcel perror_with_name ("Poll"); 761130803Smarcel#else 762130803Smarcel internal_error (__FILE__, __LINE__, 763130803Smarcel "use_poll without HAVE_POLL"); 764130803Smarcel#endif /* HAVE_POLL */ 765130803Smarcel } 766130803Smarcel else 767130803Smarcel { 768130803Smarcel gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0]; 769130803Smarcel gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1]; 770130803Smarcel gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2]; 771130803Smarcel num_found = select (gdb_notifier.num_fds, 772130803Smarcel &gdb_notifier.ready_masks[0], 773130803Smarcel &gdb_notifier.ready_masks[1], 774130803Smarcel &gdb_notifier.ready_masks[2], 775130803Smarcel gdb_notifier.timeout_valid 776130803Smarcel ? &gdb_notifier.select_timeout : NULL); 777130803Smarcel 778130803Smarcel /* Clear the masks after an error from select. */ 779130803Smarcel if (num_found == -1) 780130803Smarcel { 781130803Smarcel FD_ZERO (&gdb_notifier.ready_masks[0]); 782130803Smarcel FD_ZERO (&gdb_notifier.ready_masks[1]); 783130803Smarcel FD_ZERO (&gdb_notifier.ready_masks[2]); 784130803Smarcel /* Dont print anything is we got a signal, let gdb handle it. */ 785130803Smarcel if (errno != EINTR) 786130803Smarcel perror_with_name ("Select"); 787130803Smarcel } 788130803Smarcel } 789130803Smarcel 790130803Smarcel /* Enqueue all detected file events. */ 791130803Smarcel 792130803Smarcel if (use_poll) 793130803Smarcel { 794130803Smarcel#ifdef HAVE_POLL 795130803Smarcel for (i = 0; (i < gdb_notifier.num_fds) && (num_found > 0); i++) 796130803Smarcel { 797130803Smarcel if ((gdb_notifier.poll_fds + i)->revents) 798130803Smarcel num_found--; 799130803Smarcel else 800130803Smarcel continue; 801130803Smarcel 802130803Smarcel for (file_ptr = gdb_notifier.first_file_handler; 803130803Smarcel file_ptr != NULL; 804130803Smarcel file_ptr = file_ptr->next_file) 805130803Smarcel { 806130803Smarcel if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd) 807130803Smarcel break; 808130803Smarcel } 809130803Smarcel 810130803Smarcel if (file_ptr) 811130803Smarcel { 812130803Smarcel /* Enqueue an event only if this is still a new event for 813130803Smarcel this fd. */ 814130803Smarcel if (file_ptr->ready_mask == 0) 815130803Smarcel { 816130803Smarcel file_event_ptr = create_file_event (file_ptr->fd); 817130803Smarcel async_queue_event (file_event_ptr, TAIL); 818130803Smarcel } 819130803Smarcel } 820130803Smarcel 821130803Smarcel file_ptr->ready_mask = (gdb_notifier.poll_fds + i)->revents; 822130803Smarcel } 823130803Smarcel#else 824130803Smarcel internal_error (__FILE__, __LINE__, 825130803Smarcel "use_poll without HAVE_POLL"); 826130803Smarcel#endif /* HAVE_POLL */ 827130803Smarcel } 828130803Smarcel else 829130803Smarcel { 830130803Smarcel for (file_ptr = gdb_notifier.first_file_handler; 831130803Smarcel (file_ptr != NULL) && (num_found > 0); 832130803Smarcel file_ptr = file_ptr->next_file) 833130803Smarcel { 834130803Smarcel int mask = 0; 835130803Smarcel 836130803Smarcel if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0])) 837130803Smarcel mask |= GDB_READABLE; 838130803Smarcel if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1])) 839130803Smarcel mask |= GDB_WRITABLE; 840130803Smarcel if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2])) 841130803Smarcel mask |= GDB_EXCEPTION; 842130803Smarcel 843130803Smarcel if (!mask) 844130803Smarcel continue; 845130803Smarcel else 846130803Smarcel num_found--; 847130803Smarcel 848130803Smarcel /* Enqueue an event only if this is still a new event for 849130803Smarcel this fd. */ 850130803Smarcel 851130803Smarcel if (file_ptr->ready_mask == 0) 852130803Smarcel { 853130803Smarcel file_event_ptr = create_file_event (file_ptr->fd); 854130803Smarcel async_queue_event (file_event_ptr, TAIL); 855130803Smarcel } 856130803Smarcel file_ptr->ready_mask = mask; 857130803Smarcel } 858130803Smarcel } 859130803Smarcel return 0; 860130803Smarcel} 861130803Smarcel 862130803Smarcel 863130803Smarcel/* Create an asynchronous handler, allocating memory for it. 864130803Smarcel Return a pointer to the newly created handler. 865130803Smarcel This pointer will be used to invoke the handler by 866130803Smarcel invoke_async_signal_handler. 867130803Smarcel PROC is the function to call with CLIENT_DATA argument 868130803Smarcel whenever the handler is invoked. */ 869130803Smarcelasync_signal_handler * 870130803Smarcelcreate_async_signal_handler (sig_handler_func * proc, gdb_client_data client_data) 871130803Smarcel{ 872130803Smarcel async_signal_handler *async_handler_ptr; 873130803Smarcel 874130803Smarcel async_handler_ptr = 875130803Smarcel (async_signal_handler *) xmalloc (sizeof (async_signal_handler)); 876130803Smarcel async_handler_ptr->ready = 0; 877130803Smarcel async_handler_ptr->next_handler = NULL; 878130803Smarcel async_handler_ptr->proc = proc; 879130803Smarcel async_handler_ptr->client_data = client_data; 880130803Smarcel if (sighandler_list.first_handler == NULL) 881130803Smarcel sighandler_list.first_handler = async_handler_ptr; 882130803Smarcel else 883130803Smarcel sighandler_list.last_handler->next_handler = async_handler_ptr; 884130803Smarcel sighandler_list.last_handler = async_handler_ptr; 885130803Smarcel return async_handler_ptr; 886130803Smarcel} 887130803Smarcel 888130803Smarcel/* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information will 889130803Smarcel be used when the handlers are invoked, after we have waited for 890130803Smarcel some event. The caller of this function is the interrupt handler 891130803Smarcel associated with a signal. */ 892130803Smarcelvoid 893130803Smarcelmark_async_signal_handler (async_signal_handler * async_handler_ptr) 894130803Smarcel{ 895130803Smarcel ((async_signal_handler *) async_handler_ptr)->ready = 1; 896130803Smarcel async_handler_ready = 1; 897130803Smarcel} 898130803Smarcel 899130803Smarcel/* Call all the handlers that are ready. */ 900130803Smarcelstatic void 901130803Smarcelinvoke_async_signal_handler (void) 902130803Smarcel{ 903130803Smarcel async_signal_handler *async_handler_ptr; 904130803Smarcel 905130803Smarcel if (async_handler_ready == 0) 906130803Smarcel return; 907130803Smarcel async_handler_ready = 0; 908130803Smarcel 909130803Smarcel /* Invoke ready handlers. */ 910130803Smarcel 911130803Smarcel while (1) 912130803Smarcel { 913130803Smarcel for (async_handler_ptr = sighandler_list.first_handler; 914130803Smarcel async_handler_ptr != NULL; 915130803Smarcel async_handler_ptr = async_handler_ptr->next_handler) 916130803Smarcel { 917130803Smarcel if (async_handler_ptr->ready) 918130803Smarcel break; 919130803Smarcel } 920130803Smarcel if (async_handler_ptr == NULL) 921130803Smarcel break; 922130803Smarcel async_handler_ptr->ready = 0; 923130803Smarcel (*async_handler_ptr->proc) (async_handler_ptr->client_data); 924130803Smarcel } 925130803Smarcel 926130803Smarcel return; 927130803Smarcel} 928130803Smarcel 929130803Smarcel/* Delete an asynchronous handler (ASYNC_HANDLER_PTR). 930130803Smarcel Free the space allocated for it. */ 931130803Smarcelvoid 932130803Smarceldelete_async_signal_handler (async_signal_handler ** async_handler_ptr) 933130803Smarcel{ 934130803Smarcel async_signal_handler *prev_ptr; 935130803Smarcel 936130803Smarcel if (sighandler_list.first_handler == (*async_handler_ptr)) 937130803Smarcel { 938130803Smarcel sighandler_list.first_handler = (*async_handler_ptr)->next_handler; 939130803Smarcel if (sighandler_list.first_handler == NULL) 940130803Smarcel sighandler_list.last_handler = NULL; 941130803Smarcel } 942130803Smarcel else 943130803Smarcel { 944130803Smarcel prev_ptr = sighandler_list.first_handler; 945130803Smarcel while (prev_ptr->next_handler != (*async_handler_ptr) && prev_ptr) 946130803Smarcel prev_ptr = prev_ptr->next_handler; 947130803Smarcel prev_ptr->next_handler = (*async_handler_ptr)->next_handler; 948130803Smarcel if (sighandler_list.last_handler == (*async_handler_ptr)) 949130803Smarcel sighandler_list.last_handler = prev_ptr; 950130803Smarcel } 951130803Smarcel xfree ((*async_handler_ptr)); 952130803Smarcel (*async_handler_ptr) = NULL; 953130803Smarcel} 954130803Smarcel 955130803Smarcel/* Is it necessary to call invoke_async_signal_handler? */ 956130803Smarcelstatic int 957130803Smarcelcheck_async_ready (void) 958130803Smarcel{ 959130803Smarcel return async_handler_ready; 960130803Smarcel} 961130803Smarcel 962130803Smarcel/* Create a timer that will expire in MILLISECONDS from now. When the 963130803Smarcel timer is ready, PROC will be executed. At creation, the timer is 964130803Smarcel aded to the timers queue. This queue is kept sorted in order of 965130803Smarcel increasing timers. Return a handle to the timer struct. */ 966130803Smarcelint 967130803Smarcelcreate_timer (int milliseconds, timer_handler_func * proc, gdb_client_data client_data) 968130803Smarcel{ 969130803Smarcel struct gdb_timer *timer_ptr, *timer_index, *prev_timer; 970130803Smarcel struct timeval time_now, delta; 971130803Smarcel 972130803Smarcel /* compute seconds */ 973130803Smarcel delta.tv_sec = milliseconds / 1000; 974130803Smarcel /* compute microseconds */ 975130803Smarcel delta.tv_usec = (milliseconds % 1000) * 1000; 976130803Smarcel 977130803Smarcel gettimeofday (&time_now, NULL); 978130803Smarcel 979130803Smarcel timer_ptr = (struct gdb_timer *) xmalloc (sizeof (gdb_timer)); 980130803Smarcel timer_ptr->when.tv_sec = time_now.tv_sec + delta.tv_sec; 981130803Smarcel timer_ptr->when.tv_usec = time_now.tv_usec + delta.tv_usec; 982130803Smarcel /* carry? */ 983130803Smarcel if (timer_ptr->when.tv_usec >= 1000000) 984130803Smarcel { 985130803Smarcel timer_ptr->when.tv_sec += 1; 986130803Smarcel timer_ptr->when.tv_usec -= 1000000; 987130803Smarcel } 988130803Smarcel timer_ptr->proc = proc; 989130803Smarcel timer_ptr->client_data = client_data; 990130803Smarcel timer_list.num_timers++; 991130803Smarcel timer_ptr->timer_id = timer_list.num_timers; 992130803Smarcel 993130803Smarcel /* Now add the timer to the timer queue, making sure it is sorted in 994130803Smarcel increasing order of expiration. */ 995130803Smarcel 996130803Smarcel for (timer_index = timer_list.first_timer; 997130803Smarcel timer_index != NULL; 998130803Smarcel timer_index = timer_index->next) 999130803Smarcel { 1000130803Smarcel /* If the seconds field is greater or if it is the same, but the 1001130803Smarcel microsecond field is greater. */ 1002130803Smarcel if ((timer_index->when.tv_sec > timer_ptr->when.tv_sec) || 1003130803Smarcel ((timer_index->when.tv_sec == timer_ptr->when.tv_sec) 1004130803Smarcel && (timer_index->when.tv_usec > timer_ptr->when.tv_usec))) 1005130803Smarcel break; 1006130803Smarcel } 1007130803Smarcel 1008130803Smarcel if (timer_index == timer_list.first_timer) 1009130803Smarcel { 1010130803Smarcel timer_ptr->next = timer_list.first_timer; 1011130803Smarcel timer_list.first_timer = timer_ptr; 1012130803Smarcel 1013130803Smarcel } 1014130803Smarcel else 1015130803Smarcel { 1016130803Smarcel for (prev_timer = timer_list.first_timer; 1017130803Smarcel prev_timer->next != timer_index; 1018130803Smarcel prev_timer = prev_timer->next) 1019130803Smarcel ; 1020130803Smarcel 1021130803Smarcel prev_timer->next = timer_ptr; 1022130803Smarcel timer_ptr->next = timer_index; 1023130803Smarcel } 1024130803Smarcel 1025130803Smarcel gdb_notifier.timeout_valid = 0; 1026130803Smarcel return timer_ptr->timer_id; 1027130803Smarcel} 1028130803Smarcel 1029130803Smarcel/* There is a chance that the creator of the timer wants to get rid of 1030130803Smarcel it before it expires. */ 1031130803Smarcelvoid 1032130803Smarceldelete_timer (int id) 1033130803Smarcel{ 1034130803Smarcel struct gdb_timer *timer_ptr, *prev_timer = NULL; 1035130803Smarcel 1036130803Smarcel /* Find the entry for the given timer. */ 1037130803Smarcel 1038130803Smarcel for (timer_ptr = timer_list.first_timer; timer_ptr != NULL; 1039130803Smarcel timer_ptr = timer_ptr->next) 1040130803Smarcel { 1041130803Smarcel if (timer_ptr->timer_id == id) 1042130803Smarcel break; 1043130803Smarcel } 1044130803Smarcel 1045130803Smarcel if (timer_ptr == NULL) 1046130803Smarcel return; 1047130803Smarcel /* Get rid of the timer in the timer list. */ 1048130803Smarcel if (timer_ptr == timer_list.first_timer) 1049130803Smarcel timer_list.first_timer = timer_ptr->next; 1050130803Smarcel else 1051130803Smarcel { 1052130803Smarcel for (prev_timer = timer_list.first_timer; 1053130803Smarcel prev_timer->next != timer_ptr; 1054130803Smarcel prev_timer = prev_timer->next) 1055130803Smarcel ; 1056130803Smarcel prev_timer->next = timer_ptr->next; 1057130803Smarcel } 1058130803Smarcel xfree (timer_ptr); 1059130803Smarcel 1060130803Smarcel gdb_notifier.timeout_valid = 0; 1061130803Smarcel} 1062130803Smarcel 1063130803Smarcel/* When a timer event is put on the event queue, it will be handled by 1064130803Smarcel this function. Just call the assiciated procedure and delete the 1065130803Smarcel timer event from the event queue. Repeat this for each timer that 1066130803Smarcel has expired. */ 1067130803Smarcelstatic void 1068130803Smarcelhandle_timer_event (int dummy) 1069130803Smarcel{ 1070130803Smarcel struct timeval time_now; 1071130803Smarcel struct gdb_timer *timer_ptr, *saved_timer; 1072130803Smarcel 1073130803Smarcel gettimeofday (&time_now, NULL); 1074130803Smarcel timer_ptr = timer_list.first_timer; 1075130803Smarcel 1076130803Smarcel while (timer_ptr != NULL) 1077130803Smarcel { 1078130803Smarcel if ((timer_ptr->when.tv_sec > time_now.tv_sec) || 1079130803Smarcel ((timer_ptr->when.tv_sec == time_now.tv_sec) && 1080130803Smarcel (timer_ptr->when.tv_usec > time_now.tv_usec))) 1081130803Smarcel break; 1082130803Smarcel 1083130803Smarcel /* Get rid of the timer from the beginning of the list. */ 1084130803Smarcel timer_list.first_timer = timer_ptr->next; 1085130803Smarcel saved_timer = timer_ptr; 1086130803Smarcel timer_ptr = timer_ptr->next; 1087130803Smarcel /* Call the procedure associated with that timer. */ 1088130803Smarcel (*saved_timer->proc) (saved_timer->client_data); 1089130803Smarcel xfree (saved_timer); 1090130803Smarcel } 1091130803Smarcel 1092130803Smarcel gdb_notifier.timeout_valid = 0; 1093130803Smarcel} 1094130803Smarcel 1095130803Smarcel/* Check whether any timers in the timers queue are ready. If at least 1096130803Smarcel one timer is ready, stick an event onto the event queue. Even in 1097130803Smarcel case more than one timer is ready, one event is enough, because the 1098130803Smarcel handle_timer_event() will go through the timers list and call the 1099130803Smarcel procedures associated with all that have expired. Update the 1100130803Smarcel timeout for the select() or poll() as well. */ 1101130803Smarcelstatic void 1102130803Smarcelpoll_timers (void) 1103130803Smarcel{ 1104130803Smarcel struct timeval time_now, delta; 1105130803Smarcel gdb_event *event_ptr; 1106130803Smarcel 1107130803Smarcel if (timer_list.first_timer != NULL) 1108130803Smarcel { 1109130803Smarcel gettimeofday (&time_now, NULL); 1110130803Smarcel delta.tv_sec = timer_list.first_timer->when.tv_sec - time_now.tv_sec; 1111130803Smarcel delta.tv_usec = timer_list.first_timer->when.tv_usec - time_now.tv_usec; 1112130803Smarcel /* borrow? */ 1113130803Smarcel if (delta.tv_usec < 0) 1114130803Smarcel { 1115130803Smarcel delta.tv_sec -= 1; 1116130803Smarcel delta.tv_usec += 1000000; 1117130803Smarcel } 1118130803Smarcel 1119130803Smarcel /* Oops it expired already. Tell select / poll to return 1120130803Smarcel immediately. (Cannot simply test if delta.tv_sec is negative 1121130803Smarcel because time_t might be unsigned.) */ 1122130803Smarcel if (timer_list.first_timer->when.tv_sec < time_now.tv_sec 1123130803Smarcel || (timer_list.first_timer->when.tv_sec == time_now.tv_sec 1124130803Smarcel && timer_list.first_timer->when.tv_usec < time_now.tv_usec)) 1125130803Smarcel { 1126130803Smarcel delta.tv_sec = 0; 1127130803Smarcel delta.tv_usec = 0; 1128130803Smarcel } 1129130803Smarcel 1130130803Smarcel if (delta.tv_sec == 0 && delta.tv_usec == 0) 1131130803Smarcel { 1132130803Smarcel event_ptr = (gdb_event *) xmalloc (sizeof (gdb_event)); 1133130803Smarcel event_ptr->proc = handle_timer_event; 1134130803Smarcel event_ptr->fd = timer_list.first_timer->timer_id; 1135130803Smarcel async_queue_event (event_ptr, TAIL); 1136130803Smarcel } 1137130803Smarcel 1138130803Smarcel /* Now we need to update the timeout for select/ poll, because we 1139130803Smarcel don't want to sit there while this timer is expiring. */ 1140130803Smarcel if (use_poll) 1141130803Smarcel { 1142130803Smarcel#ifdef HAVE_POLL 1143130803Smarcel gdb_notifier.poll_timeout = delta.tv_sec * 1000; 1144130803Smarcel#else 1145130803Smarcel internal_error (__FILE__, __LINE__, 1146130803Smarcel "use_poll without HAVE_POLL"); 1147130803Smarcel#endif /* HAVE_POLL */ 1148130803Smarcel } 1149130803Smarcel else 1150130803Smarcel { 1151130803Smarcel gdb_notifier.select_timeout.tv_sec = delta.tv_sec; 1152130803Smarcel gdb_notifier.select_timeout.tv_usec = delta.tv_usec; 1153130803Smarcel } 1154130803Smarcel gdb_notifier.timeout_valid = 1; 1155130803Smarcel } 1156130803Smarcel else 1157130803Smarcel gdb_notifier.timeout_valid = 0; 1158130803Smarcel} 1159130803Smarcel