1 /* Event loop machinery for GDB, the GNU debugger.
2 Copyright (C) 1999, 2000, 2001, 2002, 2005, 2006, 2007, 2008, 2009, 2010,
3 2011 Free Software Foundation, Inc.
4 Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "event-loop.h"
23 #include "event-top.h"
26 #if defined (HAVE_POLL_H)
28 #elif defined (HAVE_SYS_POLL_H)
33 #include <sys/types.h>
34 #include "gdb_string.h"
37 #include "exceptions.h"
38 #include "gdb_assert.h"
39 #include "gdb_select.h"
41 /* Tell create_file_handler what events we are interested in.
42 This is used by the select version of the event loop. */
44 #define GDB_READABLE (1<<1)
45 #define GDB_WRITABLE (1<<2)
46 #define GDB_EXCEPTION (1<<3)
48 /* Data point to pass to the event handler. */
49 typedef union event_data
55 typedef struct gdb_event gdb_event;
56 typedef void (event_handler_func) (event_data);
58 /* Event for the GDB event system. Events are queued by calling
59 async_queue_event and serviced later on by gdb_do_one_event. An
60 event can be, for instance, a file descriptor becoming ready to be
61 read. Servicing an event simply means that the procedure PROC will
62 be called. We have 2 queues, one for file handlers that we listen
63 to in the event loop, and one for the file handlers+events that are
64 ready. The procedure PROC associated with each event is dependant
65 of the event source. In the case of monitored file descriptors, it
66 is always the same (handle_file_event). Its duty is to invoke the
67 handler associated with the file descriptor whose state change
68 generated the event, plus doing other cleanups and such. In the
69 case of async signal handlers, it is
70 invoke_async_signal_handler. */
74 /* Procedure to call to service this event. */
75 event_handler_func *proc;
77 /* Data to pass to the event handler. */
80 /* Next in list of events or NULL. */
81 struct gdb_event *next_event;
84 /* Information about each file descriptor we register with the event
87 typedef struct file_handler
89 int fd; /* File descriptor. */
90 int mask; /* Events we want to monitor: POLLIN, etc. */
91 int ready_mask; /* Events that have been seen since
93 handler_func *proc; /* Procedure to call when fd is ready. */
94 gdb_client_data client_data; /* Argument to pass to proc. */
95 int error; /* Was an error detected on this fd? */
96 struct file_handler *next_file; /* Next registered file descriptor. */
100 /* PROC is a function to be invoked when the READY flag is set. This
101 happens when there has been a signal and the corresponding signal
102 handler has 'triggered' this async_signal_handler for execution.
103 The actual work to be done in response to a signal will be carried
104 out by PROC at a later time, within process_event. This provides a
105 deferred execution of signal handlers.
107 Async_init_signals takes care of setting up such an
108 async_signal_handler for each interesting signal. */
110 typedef struct async_signal_handler
112 int ready; /* If ready, call this handler
113 from the main event loop, using
114 invoke_async_handler. */
115 struct async_signal_handler *next_handler; /* Ptr to next handler. */
116 sig_handler_func *proc; /* Function to call to do the work. */
117 gdb_client_data client_data; /* Argument to async_handler_func. */
119 async_signal_handler;
121 /* PROC is a function to be invoked when the READY flag is set. This
122 happens when the event has been marked with
123 MARK_ASYNC_EVENT_HANDLER. The actual work to be done in response
124 to an event will be carried out by PROC at a later time, within
125 process_event. This provides a deferred execution of event
127 typedef struct async_event_handler
129 /* If ready, call this handler from the main event loop, using
130 invoke_event_handler. */
133 /* Point to next handler. */
134 struct async_event_handler *next_handler;
136 /* Function to call to do the work. */
137 async_event_handler_func *proc;
139 /* Argument to PROC. */
140 gdb_client_data client_data;
146 - the first event in the queue is the head of the queue.
147 It will be the next to be serviced.
148 - the last event in the queue
150 Events can be inserted at the front of the queue or at the end of
151 the queue. Events will be extracted from the queue for processing
152 starting from the head. Therefore, events inserted at the head of
153 the queue will be processed in a last in first out fashion, while
154 those inserted at the tail of the queue will be processed in a first
155 in first out manner. All the fields are NULL if the queue is
160 gdb_event *first_event; /* First pending event. */
161 gdb_event *last_event; /* Last pending event. */
165 /* Gdb_notifier is just a list of file descriptors gdb is interested in.
166 These are the input file descriptor, and the target file
167 descriptor. We have two flavors of the notifier, one for platforms
168 that have the POLL function, the other for those that don't, and
169 only support SELECT. Each of the elements in the gdb_notifier list is
170 basically a description of what kind of events gdb is interested
173 /* As of 1999-04-30 only the input file descriptor is registered with the
176 /* Do we use poll or select ? */
181 #endif /* HAVE_POLL */
183 static unsigned char use_poll = USE_POLL;
192 /* Ptr to head of file handler list. */
193 file_handler *first_file_handler;
196 /* Ptr to array of pollfd structures. */
197 struct pollfd *poll_fds;
199 /* Timeout in milliseconds for calls to poll(). */
203 /* Masks to be used in the next call to select.
204 Bits are set in response to calls to create_file_handler. */
205 fd_set check_masks[3];
207 /* What file descriptors were found ready by select. */
208 fd_set ready_masks[3];
210 /* Number of file descriptors to monitor (for poll). */
211 /* Number of valid bits (highest fd value + 1) (for select). */
214 /* Time structure for calls to select(). */
215 struct timeval select_timeout;
217 /* Flag to tell whether the timeout should be used. */
222 /* Structure associated with a timer. PROC will be executed at the
223 first occasion after WHEN. */
228 struct gdb_timer *next;
229 timer_handler_func *proc; /* Function to call to do the work. */
230 gdb_client_data client_data; /* Argument to async_handler_func. */
233 /* List of currently active timers. It is sorted in order of
234 increasing timers. */
237 /* Pointer to first in timer list. */
238 struct gdb_timer *first_timer;
240 /* Id of the last timer created. */
245 /* All the async_signal_handlers gdb is interested in are kept onto
249 /* Pointer to first in handler list. */
250 async_signal_handler *first_handler;
252 /* Pointer to last in handler list. */
253 async_signal_handler *last_handler;
257 /* All the async_event_handlers gdb is interested in are kept onto
261 /* Pointer to first in handler list. */
262 async_event_handler *first_handler;
264 /* Pointer to last in handler list. */
265 async_event_handler *last_handler;
267 async_event_handler_list;
269 static int invoke_async_signal_handlers (void);
270 static void create_file_handler (int fd, int mask, handler_func *proc,
271 gdb_client_data client_data);
272 static void handle_file_event (event_data data);
273 static void check_async_event_handlers (void);
274 static int gdb_wait_for_event (int);
275 static void poll_timers (void);
278 /* Insert an event object into the gdb event queue at
279 the specified position.
280 POSITION can be head or tail, with values TAIL, HEAD.
281 EVENT_PTR points to the event to be inserted into the queue.
282 The caller must allocate memory for the event. It is freed
283 after the event has ben handled.
284 Events in the queue will be processed head to tail, therefore,
285 events inserted at the head of the queue will be processed
286 as last in first out. Event appended at the tail of the queue
287 will be processed first in first out. */
289 async_queue_event (gdb_event * event_ptr, queue_position position)
291 if (position == TAIL)
293 /* The event will become the new last_event. */
295 event_ptr->next_event = NULL;
296 if (event_queue.first_event == NULL)
297 event_queue.first_event = event_ptr;
299 event_queue.last_event->next_event = event_ptr;
300 event_queue.last_event = event_ptr;
302 else if (position == HEAD)
304 /* The event becomes the new first_event. */
306 event_ptr->next_event = event_queue.first_event;
307 if (event_queue.first_event == NULL)
308 event_queue.last_event = event_ptr;
309 event_queue.first_event = event_ptr;
313 /* Create a generic event, to be enqueued in the event queue for
314 processing. PROC is the procedure associated to the event. DATA
315 is passed to PROC upon PROC invocation. */
318 create_event (event_handler_func proc, event_data data)
322 event = xmalloc (sizeof (*event));
329 /* Create a file event, to be enqueued in the event queue for
330 processing. The procedure associated to this event is always
331 handle_file_event, which will in turn invoke the one that was
332 associated to FD when it was registered with the event loop. */
334 create_file_event (int fd)
339 return create_event (handle_file_event, data);
342 /* Process one event.
343 The event can be the next one to be serviced in the event queue,
344 or an asynchronous event handler can be invoked in response to
345 the reception of a signal.
346 If an event was processed (either way), 1 is returned otherwise
348 Scan the queue from head to tail, processing therefore the high
349 priority events first, by invoking the associated event handler
354 gdb_event *event_ptr, *prev_ptr;
355 event_handler_func *proc;
358 /* First let's see if there are any asynchronous event handlers that
359 are ready. These would be the result of invoking any of the
362 if (invoke_async_signal_handlers ())
365 /* Look in the event queue to find an event that is ready
368 for (event_ptr = event_queue.first_event; event_ptr != NULL;
369 event_ptr = event_ptr->next_event)
371 /* Call the handler for the event. */
373 proc = event_ptr->proc;
374 data = event_ptr->data;
376 /* Let's get rid of the event from the event queue. We need to
377 do this now because while processing the event, the proc
378 function could end up calling 'error' and therefore jump out
379 to the caller of this function, gdb_do_one_event. In that
380 case, we would have on the event queue an event wich has been
381 processed, but not deleted. */
383 if (event_queue.first_event == event_ptr)
385 event_queue.first_event = event_ptr->next_event;
386 if (event_ptr->next_event == NULL)
387 event_queue.last_event = NULL;
391 prev_ptr = event_queue.first_event;
392 while (prev_ptr->next_event != event_ptr)
393 prev_ptr = prev_ptr->next_event;
395 prev_ptr->next_event = event_ptr->next_event;
396 if (event_ptr->next_event == NULL)
397 event_queue.last_event = prev_ptr;
401 /* Now call the procedure associated with the event. */
406 /* This is the case if there are no event on the event queue. */
410 /* Process one high level event. If nothing is ready at this time,
411 wait for something to happen (via gdb_wait_for_event), then process
412 it. Returns >0 if something was done otherwise returns <0 (this
413 can happen if there are no event sources to wait for). If an error
414 occurs catch_errors() which calls this function returns zero. */
417 gdb_do_one_event (void *data)
419 static int event_source_head = 0;
420 const int number_of_sources = 3;
423 /* Any events already waiting in the queue? */
424 if (process_event ())
427 /* To level the fairness across event sources, we poll them in a
428 round-robin fashion. */
429 for (current = 0; current < number_of_sources; current++)
431 switch (event_source_head)
434 /* Are any timers that are ready? If so, put an event on the
439 /* Are there events already waiting to be collected on the
440 monitored file descriptors? */
441 gdb_wait_for_event (0);
444 /* Are there any asynchronous event handlers ready? */
445 check_async_event_handlers ();
450 if (event_source_head == number_of_sources)
451 event_source_head = 0;
454 /* Handle any new events collected. */
455 if (process_event ())
458 /* Block waiting for a new event. If gdb_wait_for_event returns -1,
459 we should get out because this means that there are no event
460 sources left. This will make the event loop stop, and the
463 if (gdb_wait_for_event (1) < 0)
466 /* Handle any new events occurred while waiting. */
467 if (process_event ())
470 /* If gdb_wait_for_event has returned 1, it means that one event has
471 been handled. We break out of the loop. */
475 /* Start up the event loop. This is the entry point to the event loop
476 from the command loop. */
479 start_event_loop (void)
481 /* Loop until there is nothing to do. This is the entry point to the
482 event loop engine. gdb_do_one_event, called via catch_errors()
483 will process one event for each invocation. It blocks waits for
484 an event and then processes it. >0 when an event is processed, 0
485 when catch_errors() caught an error and <0 when there are no
486 longer any event sources registered. */
491 gdb_result = catch_errors (gdb_do_one_event, 0, "", RETURN_MASK_ALL);
495 /* If we long-jumped out of do_one_event, we probably
496 didn't get around to resetting the prompt, which leaves
497 readline in a messed-up state. Reset it here. */
501 /* If any exception escaped to here, we better enable
502 stdin. Otherwise, any command that calls async_disable_stdin,
503 and then throws, will leave stdin inoperable. */
504 async_enable_stdin ();
505 /* FIXME: this should really be a call to a hook that is
506 interface specific, because interfaces can display the
507 prompt in their own way. */
508 display_gdb_prompt (0);
509 /* This call looks bizarre, but it is required. If the user
510 entered a command that caused an error,
511 after_char_processing_hook won't be called from
512 rl_callback_read_char_wrapper. Using a cleanup there
513 won't work, since we want this function to be called
514 after a new prompt is printed. */
515 if (after_char_processing_hook)
516 (*after_char_processing_hook) ();
517 /* Maybe better to set a flag to be checked somewhere as to
518 whether display the prompt or not. */
522 /* We are done with the event loop. There are no more event sources
523 to listen to. So we exit GDB. */
528 /* Wrapper function for create_file_handler, so that the caller
529 doesn't have to know implementation details about the use of poll
532 add_file_handler (int fd, handler_func * proc, gdb_client_data client_data)
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
548 if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL))
551 internal_error (__FILE__, __LINE__,
552 _("use_poll without HAVE_POLL"));
553 #endif /* HAVE_POLL */
558 create_file_handler (fd, POLLIN, proc, client_data);
560 internal_error (__FILE__, __LINE__,
561 _("use_poll without HAVE_POLL"));
565 create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION,
569 /* Add a file handler/descriptor to the list of descriptors we are
572 FD is the file descriptor for the file/stream to be listened to.
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.
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. */
584 create_file_handler (int fd, int mask, handler_func * proc,
585 gdb_client_data client_data)
587 file_handler *file_ptr;
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)
594 if (file_ptr->fd == fd)
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)
602 file_ptr = (file_handler *) xmalloc (sizeof (file_handler));
604 file_ptr->ready_mask = 0;
605 file_ptr->next_file = gdb_notifier.first_file_handler;
606 gdb_notifier.first_file_handler = file_ptr;
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)));
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;
624 internal_error (__FILE__, __LINE__,
625 _("use_poll without HAVE_POLL"));
626 #endif /* HAVE_POLL */
630 if (mask & GDB_READABLE)
631 FD_SET (fd, &gdb_notifier.check_masks[0]);
633 FD_CLR (fd, &gdb_notifier.check_masks[0]);
635 if (mask & GDB_WRITABLE)
636 FD_SET (fd, &gdb_notifier.check_masks[1]);
638 FD_CLR (fd, &gdb_notifier.check_masks[1]);
640 if (mask & GDB_EXCEPTION)
641 FD_SET (fd, &gdb_notifier.check_masks[2]);
643 FD_CLR (fd, &gdb_notifier.check_masks[2]);
645 if (gdb_notifier.num_fds <= fd)
646 gdb_notifier.num_fds = fd + 1;
650 file_ptr->proc = proc;
651 file_ptr->client_data = client_data;
652 file_ptr->mask = mask;
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. */
658 delete_file_handler (int fd)
660 file_handler *file_ptr, *prev_ptr = NULL;
664 struct pollfd *new_poll_fds;
667 /* Find the entry for the given file. */
669 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
670 file_ptr = file_ptr->next_file)
672 if (file_ptr->fd == fd)
676 if (file_ptr == NULL)
682 /* Create a new poll_fds array by copying every fd's information
683 but the one we want to get rid of. */
685 new_poll_fds = (struct pollfd *)
686 xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd));
688 for (i = 0, j = 0; i < gdb_notifier.num_fds; i++)
690 if ((gdb_notifier.poll_fds + i)->fd != fd)
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 = (gdb_notifier.poll_fds + i)->revents;
698 xfree (gdb_notifier.poll_fds);
699 gdb_notifier.poll_fds = new_poll_fds;
700 gdb_notifier.num_fds--;
702 internal_error (__FILE__, __LINE__,
703 _("use_poll without HAVE_POLL"));
704 #endif /* HAVE_POLL */
708 if (file_ptr->mask & GDB_READABLE)
709 FD_CLR (fd, &gdb_notifier.check_masks[0]);
710 if (file_ptr->mask & GDB_WRITABLE)
711 FD_CLR (fd, &gdb_notifier.check_masks[1]);
712 if (file_ptr->mask & GDB_EXCEPTION)
713 FD_CLR (fd, &gdb_notifier.check_masks[2]);
715 /* Find current max fd. */
717 if ((fd + 1) == gdb_notifier.num_fds)
719 gdb_notifier.num_fds--;
720 for (i = gdb_notifier.num_fds; i; i--)
722 if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0])
723 || FD_ISSET (i - 1, &gdb_notifier.check_masks[1])
724 || FD_ISSET (i - 1, &gdb_notifier.check_masks[2]))
727 gdb_notifier.num_fds = i;
731 /* Deactivate the file descriptor, by clearing its mask,
732 so that it will not fire again. */
736 /* Get rid of the file handler in the file handler list. */
737 if (file_ptr == gdb_notifier.first_file_handler)
738 gdb_notifier.first_file_handler = file_ptr->next_file;
741 for (prev_ptr = gdb_notifier.first_file_handler;
742 prev_ptr->next_file != file_ptr;
743 prev_ptr = prev_ptr->next_file)
745 prev_ptr->next_file = file_ptr->next_file;
750 /* Handle the given event by calling the procedure associated to the
751 corresponding file handler. Called by process_event indirectly,
752 through event_ptr->proc. EVENT_FILE_DESC is file descriptor of the
753 event in the front of the event queue. */
755 handle_file_event (event_data data)
757 file_handler *file_ptr;
761 int error_mask_returned;
763 int event_file_desc = data.integer;
765 /* Search the file handler list to find one that matches the fd in
767 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
768 file_ptr = file_ptr->next_file)
770 if (file_ptr->fd == event_file_desc)
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. */
779 /* See if the desired events (mask) match the received
780 events (ready_mask). */
785 error_mask = POLLHUP | POLLERR | POLLNVAL;
786 mask = (file_ptr->ready_mask & file_ptr->mask) |
787 (file_ptr->ready_mask & error_mask);
788 error_mask_returned = mask & error_mask;
790 if (error_mask_returned != 0)
792 /* Work in progress. We may need to tell somebody
793 what kind of error we had. */
794 if (error_mask_returned & POLLHUP)
795 printf_unfiltered (_("Hangup detected on fd %d\n"), file_ptr->fd);
796 if (error_mask_returned & POLLERR)
797 printf_unfiltered (_("Error detected on fd %d\n"), file_ptr->fd);
798 if (error_mask_returned & POLLNVAL)
799 printf_unfiltered (_("Invalid or non-`poll'able fd %d\n"), file_ptr->fd);
805 internal_error (__FILE__, __LINE__,
806 _("use_poll without HAVE_POLL"));
807 #endif /* HAVE_POLL */
811 if (file_ptr->ready_mask & GDB_EXCEPTION)
813 printf_unfiltered (_("Exception condition detected on fd %d\n"), file_ptr->fd);
818 mask = file_ptr->ready_mask & file_ptr->mask;
821 /* Clear the received events for next time around. */
822 file_ptr->ready_mask = 0;
824 /* If there was a match, then call the handler. */
826 (*file_ptr->proc) (file_ptr->error, file_ptr->client_data);
832 /* Called by gdb_do_one_event to wait for new events on the monitored
833 file descriptors. Queue file events as they are detected by the
834 poll. If BLOCK and if there are no events, this function will
835 block in the call to poll. Return -1 if there are no file
836 descriptors to monitor, otherwise return 0. */
838 gdb_wait_for_event (int block)
840 file_handler *file_ptr;
841 gdb_event *file_event_ptr;
845 /* Make sure all output is done before getting another event. */
846 gdb_flush (gdb_stdout);
847 gdb_flush (gdb_stderr);
849 if (gdb_notifier.num_fds == 0)
858 timeout = gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1;
862 num_found = poll (gdb_notifier.poll_fds,
863 (unsigned long) gdb_notifier.num_fds, timeout);
865 /* Don't print anything if we get out of poll because of a
867 if (num_found == -1 && errno != EINTR)
868 perror_with_name (("poll"));
870 internal_error (__FILE__, __LINE__,
871 _("use_poll without HAVE_POLL"));
872 #endif /* HAVE_POLL */
876 struct timeval select_timeout;
877 struct timeval *timeout_p;
880 timeout_p = gdb_notifier.timeout_valid
881 ? &gdb_notifier.select_timeout : NULL;
884 memset (&select_timeout, 0, sizeof (select_timeout));
885 timeout_p = &select_timeout;
888 gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0];
889 gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1];
890 gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2];
891 num_found = gdb_select (gdb_notifier.num_fds,
892 &gdb_notifier.ready_masks[0],
893 &gdb_notifier.ready_masks[1],
894 &gdb_notifier.ready_masks[2],
897 /* Clear the masks after an error from select. */
900 FD_ZERO (&gdb_notifier.ready_masks[0]);
901 FD_ZERO (&gdb_notifier.ready_masks[1]);
902 FD_ZERO (&gdb_notifier.ready_masks[2]);
904 /* Dont print anything if we got a signal, let gdb handle
907 perror_with_name (("select"));
911 /* Enqueue all detected file events. */
916 for (i = 0; (i < gdb_notifier.num_fds) && (num_found > 0); i++)
918 if ((gdb_notifier.poll_fds + i)->revents)
923 for (file_ptr = gdb_notifier.first_file_handler;
925 file_ptr = file_ptr->next_file)
927 if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd)
933 /* Enqueue an event only if this is still a new event for
935 if (file_ptr->ready_mask == 0)
937 file_event_ptr = create_file_event (file_ptr->fd);
938 async_queue_event (file_event_ptr, TAIL);
940 file_ptr->ready_mask = (gdb_notifier.poll_fds + i)->revents;
944 internal_error (__FILE__, __LINE__,
945 _("use_poll without HAVE_POLL"));
946 #endif /* HAVE_POLL */
950 for (file_ptr = gdb_notifier.first_file_handler;
951 (file_ptr != NULL) && (num_found > 0);
952 file_ptr = file_ptr->next_file)
956 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0]))
957 mask |= GDB_READABLE;
958 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1]))
959 mask |= GDB_WRITABLE;
960 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2]))
961 mask |= GDB_EXCEPTION;
968 /* Enqueue an event only if this is still a new event for
971 if (file_ptr->ready_mask == 0)
973 file_event_ptr = create_file_event (file_ptr->fd);
974 async_queue_event (file_event_ptr, TAIL);
976 file_ptr->ready_mask = mask;
983 /* Create an asynchronous handler, allocating memory for it.
984 Return a pointer to the newly created handler.
985 This pointer will be used to invoke the handler by
986 invoke_async_signal_handler.
987 PROC is the function to call with CLIENT_DATA argument
988 whenever the handler is invoked. */
989 async_signal_handler *
990 create_async_signal_handler (sig_handler_func * proc, gdb_client_data client_data)
992 async_signal_handler *async_handler_ptr;
995 (async_signal_handler *) xmalloc (sizeof (async_signal_handler));
996 async_handler_ptr->ready = 0;
997 async_handler_ptr->next_handler = NULL;
998 async_handler_ptr->proc = proc;
999 async_handler_ptr->client_data = client_data;
1000 if (sighandler_list.first_handler == NULL)
1001 sighandler_list.first_handler = async_handler_ptr;
1003 sighandler_list.last_handler->next_handler = async_handler_ptr;
1004 sighandler_list.last_handler = async_handler_ptr;
1005 return async_handler_ptr;
1008 /* Call the handler from HANDLER immediately. This function runs
1009 signal handlers when returning to the event loop would be too
1012 call_async_signal_handler (struct async_signal_handler *handler)
1014 (*handler->proc) (handler->client_data);
1017 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
1018 will be used when the handlers are invoked, after we have waited
1019 for some event. The caller of this function is the interrupt
1020 handler associated with a signal. */
1022 mark_async_signal_handler (async_signal_handler * async_handler_ptr)
1024 async_handler_ptr->ready = 1;
1027 /* Call all the handlers that are ready. Returns true if any was
1030 invoke_async_signal_handlers (void)
1032 async_signal_handler *async_handler_ptr;
1035 /* Invoke ready handlers. */
1039 for (async_handler_ptr = sighandler_list.first_handler;
1040 async_handler_ptr != NULL;
1041 async_handler_ptr = async_handler_ptr->next_handler)
1043 if (async_handler_ptr->ready)
1046 if (async_handler_ptr == NULL)
1049 async_handler_ptr->ready = 0;
1050 (*async_handler_ptr->proc) (async_handler_ptr->client_data);
1056 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
1057 Free the space allocated for it. */
1059 delete_async_signal_handler (async_signal_handler ** async_handler_ptr)
1061 async_signal_handler *prev_ptr;
1063 if (sighandler_list.first_handler == (*async_handler_ptr))
1065 sighandler_list.first_handler = (*async_handler_ptr)->next_handler;
1066 if (sighandler_list.first_handler == NULL)
1067 sighandler_list.last_handler = NULL;
1071 prev_ptr = sighandler_list.first_handler;
1072 while (prev_ptr && prev_ptr->next_handler != (*async_handler_ptr))
1073 prev_ptr = prev_ptr->next_handler;
1074 prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
1075 if (sighandler_list.last_handler == (*async_handler_ptr))
1076 sighandler_list.last_handler = prev_ptr;
1078 xfree ((*async_handler_ptr));
1079 (*async_handler_ptr) = NULL;
1082 /* Create an asynchronous event handler, allocating memory for it.
1083 Return a pointer to the newly created handler. PROC is the
1084 function to call with CLIENT_DATA argument whenever the handler is
1086 async_event_handler *
1087 create_async_event_handler (async_event_handler_func *proc,
1088 gdb_client_data client_data)
1090 async_event_handler *h;
1092 h = xmalloc (sizeof (*h));
1094 h->next_handler = NULL;
1096 h->client_data = client_data;
1097 if (async_event_handler_list.first_handler == NULL)
1098 async_event_handler_list.first_handler = h;
1100 async_event_handler_list.last_handler->next_handler = h;
1101 async_event_handler_list.last_handler = h;
1105 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
1106 will be used by gdb_do_one_event. The caller will be whoever
1107 created the event source, and wants to signal that the event is
1108 ready to be handled. */
1110 mark_async_event_handler (async_event_handler *async_handler_ptr)
1112 async_handler_ptr->ready = 1;
1115 struct async_event_handler_data
1117 async_event_handler_func* proc;
1118 gdb_client_data client_data;
1122 invoke_async_event_handler (event_data data)
1124 struct async_event_handler_data *hdata = data.ptr;
1125 async_event_handler_func* proc = hdata->proc;
1126 gdb_client_data client_data = hdata->client_data;
1129 (*proc) (client_data);
1132 /* Check if any asynchronous event handlers are ready, and queue
1133 events in the ready queue for any that are. */
1135 check_async_event_handlers (void)
1137 async_event_handler *async_handler_ptr;
1138 struct async_event_handler_data *hdata;
1139 struct gdb_event *event_ptr;
1142 for (async_handler_ptr = async_event_handler_list.first_handler;
1143 async_handler_ptr != NULL;
1144 async_handler_ptr = async_handler_ptr->next_handler)
1146 if (async_handler_ptr->ready)
1148 async_handler_ptr->ready = 0;
1150 hdata = xmalloc (sizeof (*hdata));
1152 hdata->proc = async_handler_ptr->proc;
1153 hdata->client_data = async_handler_ptr->client_data;
1157 event_ptr = create_event (invoke_async_event_handler, data);
1158 async_queue_event (event_ptr, TAIL);
1163 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
1164 Free the space allocated for it. */
1166 delete_async_event_handler (async_event_handler **async_handler_ptr)
1168 async_event_handler *prev_ptr;
1170 if (async_event_handler_list.first_handler == *async_handler_ptr)
1172 async_event_handler_list.first_handler = (*async_handler_ptr)->next_handler;
1173 if (async_event_handler_list.first_handler == NULL)
1174 async_event_handler_list.last_handler = NULL;
1178 prev_ptr = async_event_handler_list.first_handler;
1179 while (prev_ptr && prev_ptr->next_handler != *async_handler_ptr)
1180 prev_ptr = prev_ptr->next_handler;
1181 prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
1182 if (async_event_handler_list.last_handler == (*async_handler_ptr))
1183 async_event_handler_list.last_handler = prev_ptr;
1185 xfree (*async_handler_ptr);
1186 *async_handler_ptr = NULL;
1189 /* Create a timer that will expire in MILLISECONDS from now. When the
1190 timer is ready, PROC will be executed. At creation, the timer is
1191 aded to the timers queue. This queue is kept sorted in order of
1192 increasing timers. Return a handle to the timer struct. */
1194 create_timer (int milliseconds, timer_handler_func * proc,
1195 gdb_client_data client_data)
1197 struct gdb_timer *timer_ptr, *timer_index, *prev_timer;
1198 struct timeval time_now, delta;
1200 /* Compute seconds. */
1201 delta.tv_sec = milliseconds / 1000;
1202 /* Compute microseconds. */
1203 delta.tv_usec = (milliseconds % 1000) * 1000;
1205 gettimeofday (&time_now, NULL);
1207 timer_ptr = (struct gdb_timer *) xmalloc (sizeof (*timer_ptr));
1208 timer_ptr->when.tv_sec = time_now.tv_sec + delta.tv_sec;
1209 timer_ptr->when.tv_usec = time_now.tv_usec + delta.tv_usec;
1211 if (timer_ptr->when.tv_usec >= 1000000)
1213 timer_ptr->when.tv_sec += 1;
1214 timer_ptr->when.tv_usec -= 1000000;
1216 timer_ptr->proc = proc;
1217 timer_ptr->client_data = client_data;
1218 timer_list.num_timers++;
1219 timer_ptr->timer_id = timer_list.num_timers;
1221 /* Now add the timer to the timer queue, making sure it is sorted in
1222 increasing order of expiration. */
1224 for (timer_index = timer_list.first_timer;
1225 timer_index != NULL;
1226 timer_index = timer_index->next)
1228 /* If the seconds field is greater or if it is the same, but the
1229 microsecond field is greater. */
1230 if ((timer_index->when.tv_sec > timer_ptr->when.tv_sec)
1231 || ((timer_index->when.tv_sec == timer_ptr->when.tv_sec)
1232 && (timer_index->when.tv_usec > timer_ptr->when.tv_usec)))
1236 if (timer_index == timer_list.first_timer)
1238 timer_ptr->next = timer_list.first_timer;
1239 timer_list.first_timer = timer_ptr;
1244 for (prev_timer = timer_list.first_timer;
1245 prev_timer->next != timer_index;
1246 prev_timer = prev_timer->next)
1249 prev_timer->next = timer_ptr;
1250 timer_ptr->next = timer_index;
1253 gdb_notifier.timeout_valid = 0;
1254 return timer_ptr->timer_id;
1257 /* There is a chance that the creator of the timer wants to get rid of
1258 it before it expires. */
1260 delete_timer (int id)
1262 struct gdb_timer *timer_ptr, *prev_timer = NULL;
1264 /* Find the entry for the given timer. */
1266 for (timer_ptr = timer_list.first_timer; timer_ptr != NULL;
1267 timer_ptr = timer_ptr->next)
1269 if (timer_ptr->timer_id == id)
1273 if (timer_ptr == NULL)
1275 /* Get rid of the timer in the timer list. */
1276 if (timer_ptr == timer_list.first_timer)
1277 timer_list.first_timer = timer_ptr->next;
1280 for (prev_timer = timer_list.first_timer;
1281 prev_timer->next != timer_ptr;
1282 prev_timer = prev_timer->next)
1284 prev_timer->next = timer_ptr->next;
1288 gdb_notifier.timeout_valid = 0;
1291 /* When a timer event is put on the event queue, it will be handled by
1292 this function. Just call the associated procedure and delete the
1293 timer event from the event queue. Repeat this for each timer that
1296 handle_timer_event (event_data dummy)
1298 struct timeval time_now;
1299 struct gdb_timer *timer_ptr, *saved_timer;
1301 gettimeofday (&time_now, NULL);
1302 timer_ptr = timer_list.first_timer;
1304 while (timer_ptr != NULL)
1306 if ((timer_ptr->when.tv_sec > time_now.tv_sec)
1307 || ((timer_ptr->when.tv_sec == time_now.tv_sec)
1308 && (timer_ptr->when.tv_usec > time_now.tv_usec)))
1311 /* Get rid of the timer from the beginning of the list. */
1312 timer_list.first_timer = timer_ptr->next;
1313 saved_timer = timer_ptr;
1314 timer_ptr = timer_ptr->next;
1315 /* Call the procedure associated with that timer. */
1316 (*saved_timer->proc) (saved_timer->client_data);
1317 xfree (saved_timer);
1320 gdb_notifier.timeout_valid = 0;
1323 /* Check whether any timers in the timers queue are ready. If at least
1324 one timer is ready, stick an event onto the event queue. Even in
1325 case more than one timer is ready, one event is enough, because the
1326 handle_timer_event() will go through the timers list and call the
1327 procedures associated with all that have expired.l Update the
1328 timeout for the select() or poll() as well. */
1332 struct timeval time_now, delta;
1333 gdb_event *event_ptr;
1335 if (timer_list.first_timer != NULL)
1337 gettimeofday (&time_now, NULL);
1338 delta.tv_sec = timer_list.first_timer->when.tv_sec - time_now.tv_sec;
1339 delta.tv_usec = timer_list.first_timer->when.tv_usec - time_now.tv_usec;
1341 if (delta.tv_usec < 0)
1344 delta.tv_usec += 1000000;
1347 /* Oops it expired already. Tell select / poll to return
1348 immediately. (Cannot simply test if delta.tv_sec is negative
1349 because time_t might be unsigned.) */
1350 if (timer_list.first_timer->when.tv_sec < time_now.tv_sec
1351 || (timer_list.first_timer->when.tv_sec == time_now.tv_sec
1352 && timer_list.first_timer->when.tv_usec < time_now.tv_usec))
1358 if (delta.tv_sec == 0 && delta.tv_usec == 0)
1360 event_ptr = (gdb_event *) xmalloc (sizeof (gdb_event));
1361 event_ptr->proc = handle_timer_event;
1362 event_ptr->data.integer = timer_list.first_timer->timer_id;
1363 async_queue_event (event_ptr, TAIL);
1366 /* Now we need to update the timeout for select/ poll, because
1367 we don't want to sit there while this timer is expiring. */
1371 gdb_notifier.poll_timeout = delta.tv_sec * 1000;
1373 internal_error (__FILE__, __LINE__,
1374 _("use_poll without HAVE_POLL"));
1375 #endif /* HAVE_POLL */
1379 gdb_notifier.select_timeout.tv_sec = delta.tv_sec;
1380 gdb_notifier.select_timeout.tv_usec = delta.tv_usec;
1382 gdb_notifier.timeout_valid = 1;
1385 gdb_notifier.timeout_valid = 0;