1 /* Event loop machinery for GDB, the GNU debugger.
2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
3 Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "event-loop.h"
22 #include "event-top.h"
23 #include "common/queue.h"
24 #include "ser-event.h"
27 #if defined (HAVE_POLL_H)
29 #elif defined (HAVE_SYS_POLL_H)
34 #include <sys/types.h>
35 #include "common/gdb_sys_time.h"
36 #include "gdb_select.h"
37 #include "observable.h"
40 /* Tell create_file_handler what events we are interested in.
41 This is used by the select version of the event loop. */
43 #define GDB_READABLE (1<<1)
44 #define GDB_WRITABLE (1<<2)
45 #define GDB_EXCEPTION (1<<3)
47 /* Data point to pass to the event handler. */
48 typedef union event_data
54 typedef struct gdb_event gdb_event;
55 typedef void (event_handler_func) (event_data);
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. */
71 typedef struct gdb_event
73 /* Procedure to call to service this event. */
74 event_handler_func *proc;
76 /* Data to pass to the event handler. */
80 /* Information about each file descriptor we register with the event
83 typedef struct file_handler
85 int fd; /* File descriptor. */
86 int mask; /* Events we want to monitor: POLLIN, etc. */
87 int ready_mask; /* Events that have been seen since
89 handler_func *proc; /* Procedure to call when fd is ready. */
90 gdb_client_data client_data; /* Argument to pass to proc. */
91 int error; /* Was an error detected on this fd? */
92 struct file_handler *next_file; /* Next registered file descriptor. */
96 /* PROC is a function to be invoked when the READY flag is set. This
97 happens when there has been a signal and the corresponding signal
98 handler has 'triggered' this async_signal_handler for execution.
99 The actual work to be done in response to a signal will be carried
100 out by PROC at a later time, within process_event. This provides a
101 deferred execution of signal handlers.
103 Async_init_signals takes care of setting up such an
104 async_signal_handler for each interesting signal. */
106 typedef struct async_signal_handler
108 int ready; /* If ready, call this handler
109 from the main event loop, using
110 invoke_async_handler. */
111 struct async_signal_handler *next_handler; /* Ptr to next handler. */
112 sig_handler_func *proc; /* Function to call to do the work. */
113 gdb_client_data client_data; /* Argument to async_handler_func. */
115 async_signal_handler;
117 /* PROC is a function to be invoked when the READY flag is set. This
118 happens when the event has been marked with
119 MARK_ASYNC_EVENT_HANDLER. The actual work to be done in response
120 to an event will be carried out by PROC at a later time, within
121 process_event. This provides a deferred execution of event
123 typedef struct async_event_handler
125 /* If ready, call this handler from the main event loop, using
126 invoke_event_handler. */
129 /* Point to next handler. */
130 struct async_event_handler *next_handler;
132 /* Function to call to do the work. */
133 async_event_handler_func *proc;
135 /* Argument to PROC. */
136 gdb_client_data client_data;
140 /* Gdb_notifier is just a list of file descriptors gdb is interested in.
141 These are the input file descriptor, and the target file
142 descriptor. We have two flavors of the notifier, one for platforms
143 that have the POLL function, the other for those that don't, and
144 only support SELECT. Each of the elements in the gdb_notifier list is
145 basically a description of what kind of events gdb is interested
148 /* As of 1999-04-30 only the input file descriptor is registered with the
151 /* Do we use poll or select ? */
156 #endif /* HAVE_POLL */
158 static unsigned char use_poll = USE_POLL;
167 /* Ptr to head of file handler list. */
168 file_handler *first_file_handler;
170 /* Next file handler to handle, for the select variant. To level
171 the fairness across event sources, we serve file handlers in a
172 round-robin-like fashion. The number and order of the polled
173 file handlers may change between invocations, but this is good
175 file_handler *next_file_handler;
178 /* Ptr to array of pollfd structures. */
179 struct pollfd *poll_fds;
181 /* Next file descriptor to handle, for the poll variant. To level
182 the fairness across event sources, we poll the file descriptors
183 in a round-robin-like fashion. The number and order of the
184 polled file descriptors may change between invocations, but
185 this is good enough. */
186 int next_poll_fds_index;
188 /* Timeout in milliseconds for calls to poll(). */
192 /* Masks to be used in the next call to select.
193 Bits are set in response to calls to create_file_handler. */
194 fd_set check_masks[3];
196 /* What file descriptors were found ready by select. */
197 fd_set ready_masks[3];
199 /* Number of file descriptors to monitor (for poll). */
200 /* Number of valid bits (highest fd value + 1) (for select). */
203 /* Time structure for calls to select(). */
204 struct timeval select_timeout;
206 /* Flag to tell whether the timeout should be used. */
211 /* Structure associated with a timer. PROC will be executed at the
212 first occasion after WHEN. */
215 std::chrono::steady_clock::time_point when;
217 struct gdb_timer *next;
218 timer_handler_func *proc; /* Function to call to do the work. */
219 gdb_client_data client_data; /* Argument to async_handler_func. */
222 /* List of currently active timers. It is sorted in order of
223 increasing timers. */
226 /* Pointer to first in timer list. */
227 struct gdb_timer *first_timer;
229 /* Id of the last timer created. */
234 /* All the async_signal_handlers gdb is interested in are kept onto
238 /* Pointer to first in handler list. */
239 async_signal_handler *first_handler;
241 /* Pointer to last in handler list. */
242 async_signal_handler *last_handler;
246 /* All the async_event_handlers gdb is interested in are kept onto
250 /* Pointer to first in handler list. */
251 async_event_handler *first_handler;
253 /* Pointer to last in handler list. */
254 async_event_handler *last_handler;
256 async_event_handler_list;
258 static int invoke_async_signal_handlers (void);
259 static void create_file_handler (int fd, int mask, handler_func *proc,
260 gdb_client_data client_data);
261 static int check_async_event_handlers (void);
262 static int gdb_wait_for_event (int);
263 static int update_wait_timeout (void);
264 static int poll_timers (void);
267 /* This event is signalled whenever an asynchronous handler needs to
268 defer an action to the event loop. */
269 static struct serial_event *async_signal_handlers_serial_event;
271 /* Callback registered with ASYNC_SIGNAL_HANDLERS_SERIAL_EVENT. */
274 async_signals_handler (int error, gdb_client_data client_data)
276 /* Do nothing. Handlers are run by invoke_async_signal_handlers
281 initialize_async_signal_handlers (void)
283 async_signal_handlers_serial_event = make_serial_event ();
285 add_file_handler (serial_event_fd (async_signal_handlers_serial_event),
286 async_signals_handler, NULL);
289 /* Process one high level event. If nothing is ready at this time,
290 wait for something to happen (via gdb_wait_for_event), then process
291 it. Returns >0 if something was done otherwise returns <0 (this
292 can happen if there are no event sources to wait for). */
295 gdb_do_one_event (void)
297 static int event_source_head = 0;
298 const int number_of_sources = 3;
301 /* First let's see if there are any asynchronous signal handlers
302 that are ready. These would be the result of invoking any of the
304 if (invoke_async_signal_handlers ())
307 /* To level the fairness across event sources, we poll them in a
308 round-robin fashion. */
309 for (current = 0; current < number_of_sources; current++)
313 switch (event_source_head)
316 /* Are any timers that are ready? */
317 res = poll_timers ();
320 /* Are there events already waiting to be collected on the
321 monitored file descriptors? */
322 res = gdb_wait_for_event (0);
325 /* Are there any asynchronous event handlers ready? */
326 res = check_async_event_handlers ();
329 internal_error (__FILE__, __LINE__,
330 "unexpected event_source_head %d",
335 if (event_source_head == number_of_sources)
336 event_source_head = 0;
342 /* Block waiting for a new event. If gdb_wait_for_event returns -1,
343 we should get out because this means that there are no event
344 sources left. This will make the event loop stop, and the
347 if (gdb_wait_for_event (1) < 0)
350 /* If gdb_wait_for_event has returned 1, it means that one event has
351 been handled. We break out of the loop. */
355 /* Start up the event loop. This is the entry point to the event loop
356 from the command loop. */
359 start_event_loop (void)
361 /* Loop until there is nothing to do. This is the entry point to
362 the event loop engine. gdb_do_one_event will process one event
363 for each invocation. It blocks waiting for an event and then
371 result = gdb_do_one_event ();
373 CATCH (ex, RETURN_MASK_ALL)
375 exception_print (gdb_stderr, ex);
377 /* If any exception escaped to here, we better enable
378 stdin. Otherwise, any command that calls async_disable_stdin,
379 and then throws, will leave stdin inoperable. */
380 async_enable_stdin ();
381 /* If we long-jumped out of do_one_event, we probably didn't
382 get around to resetting the prompt, which leaves readline
383 in a messed-up state. Reset it here. */
384 current_ui->prompt_state = PROMPT_NEEDED;
385 gdb::observers::command_error.notify ();
386 /* This call looks bizarre, but it is required. If the user
387 entered a command that caused an error,
388 after_char_processing_hook won't be called from
389 rl_callback_read_char_wrapper. Using a cleanup there
390 won't work, since we want this function to be called
391 after a new prompt is printed. */
392 if (after_char_processing_hook)
393 (*after_char_processing_hook) ();
394 /* Maybe better to set a flag to be checked somewhere as to
395 whether display the prompt or not. */
403 /* We are done with the event loop. There are no more event sources
404 to listen to. So we exit GDB. */
409 /* Wrapper function for create_file_handler, so that the caller
410 doesn't have to know implementation details about the use of poll
413 add_file_handler (int fd, handler_func * proc, gdb_client_data client_data)
422 /* Check to see if poll () is usable. If not, we'll switch to
423 use select. This can happen on systems like
424 m68k-motorola-sys, `poll' cannot be used to wait for `stdin'.
425 On m68k-motorola-sysv, tty's are not stream-based and not
429 if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL))
432 internal_error (__FILE__, __LINE__,
433 _("use_poll without HAVE_POLL"));
434 #endif /* HAVE_POLL */
439 create_file_handler (fd, POLLIN, proc, client_data);
441 internal_error (__FILE__, __LINE__,
442 _("use_poll without HAVE_POLL"));
446 create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION,
450 /* Add a file handler/descriptor to the list of descriptors we are
453 FD is the file descriptor for the file/stream to be listened to.
455 For the poll case, MASK is a combination (OR) of POLLIN,
456 POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND:
457 these are the events we are interested in. If any of them occurs,
458 proc should be called.
460 For the select case, MASK is a combination of READABLE, WRITABLE,
461 EXCEPTION. PROC is the procedure that will be called when an event
462 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
465 create_file_handler (int fd, int mask, handler_func * proc,
466 gdb_client_data client_data)
468 file_handler *file_ptr;
470 /* Do we already have a file handler for this file? (We may be
471 changing its associated procedure). */
472 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
473 file_ptr = file_ptr->next_file)
475 if (file_ptr->fd == fd)
479 /* It is a new file descriptor. Add it to the list. Otherwise, just
480 change the data associated with it. */
481 if (file_ptr == NULL)
483 file_ptr = XNEW (file_handler);
485 file_ptr->ready_mask = 0;
486 file_ptr->next_file = gdb_notifier.first_file_handler;
487 gdb_notifier.first_file_handler = file_ptr;
492 gdb_notifier.num_fds++;
493 if (gdb_notifier.poll_fds)
494 gdb_notifier.poll_fds =
495 (struct pollfd *) xrealloc (gdb_notifier.poll_fds,
496 (gdb_notifier.num_fds
497 * sizeof (struct pollfd)));
499 gdb_notifier.poll_fds =
500 XNEW (struct pollfd);
501 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd = fd;
502 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events = mask;
503 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents = 0;
505 internal_error (__FILE__, __LINE__,
506 _("use_poll without HAVE_POLL"));
507 #endif /* HAVE_POLL */
511 if (mask & GDB_READABLE)
512 FD_SET (fd, &gdb_notifier.check_masks[0]);
514 FD_CLR (fd, &gdb_notifier.check_masks[0]);
516 if (mask & GDB_WRITABLE)
517 FD_SET (fd, &gdb_notifier.check_masks[1]);
519 FD_CLR (fd, &gdb_notifier.check_masks[1]);
521 if (mask & GDB_EXCEPTION)
522 FD_SET (fd, &gdb_notifier.check_masks[2]);
524 FD_CLR (fd, &gdb_notifier.check_masks[2]);
526 if (gdb_notifier.num_fds <= fd)
527 gdb_notifier.num_fds = fd + 1;
531 file_ptr->proc = proc;
532 file_ptr->client_data = client_data;
533 file_ptr->mask = mask;
536 /* Return the next file handler to handle, and advance to the next
537 file handler, wrapping around if the end of the list is
540 static file_handler *
541 get_next_file_handler_to_handle_and_advance (void)
543 file_handler *curr_next;
545 /* The first time around, this is still NULL. */
546 if (gdb_notifier.next_file_handler == NULL)
547 gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
549 curr_next = gdb_notifier.next_file_handler;
550 gdb_assert (curr_next != NULL);
553 gdb_notifier.next_file_handler = curr_next->next_file;
554 /* Wrap around, if necessary. */
555 if (gdb_notifier.next_file_handler == NULL)
556 gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
561 /* Remove the file descriptor FD from the list of monitored fd's:
562 i.e. we don't care anymore about events on the FD. */
564 delete_file_handler (int fd)
566 file_handler *file_ptr, *prev_ptr = NULL;
570 struct pollfd *new_poll_fds;
573 /* Find the entry for the given file. */
575 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
576 file_ptr = file_ptr->next_file)
578 if (file_ptr->fd == fd)
582 if (file_ptr == NULL)
588 /* Create a new poll_fds array by copying every fd's information
589 but the one we want to get rid of. */
591 new_poll_fds = (struct pollfd *)
592 xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd));
594 for (i = 0, j = 0; i < gdb_notifier.num_fds; i++)
596 if ((gdb_notifier.poll_fds + i)->fd != fd)
598 (new_poll_fds + j)->fd = (gdb_notifier.poll_fds + i)->fd;
599 (new_poll_fds + j)->events = (gdb_notifier.poll_fds + i)->events;
600 (new_poll_fds + j)->revents
601 = (gdb_notifier.poll_fds + i)->revents;
605 xfree (gdb_notifier.poll_fds);
606 gdb_notifier.poll_fds = new_poll_fds;
607 gdb_notifier.num_fds--;
609 internal_error (__FILE__, __LINE__,
610 _("use_poll without HAVE_POLL"));
611 #endif /* HAVE_POLL */
615 if (file_ptr->mask & GDB_READABLE)
616 FD_CLR (fd, &gdb_notifier.check_masks[0]);
617 if (file_ptr->mask & GDB_WRITABLE)
618 FD_CLR (fd, &gdb_notifier.check_masks[1]);
619 if (file_ptr->mask & GDB_EXCEPTION)
620 FD_CLR (fd, &gdb_notifier.check_masks[2]);
622 /* Find current max fd. */
624 if ((fd + 1) == gdb_notifier.num_fds)
626 gdb_notifier.num_fds--;
627 for (i = gdb_notifier.num_fds; i; i--)
629 if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0])
630 || FD_ISSET (i - 1, &gdb_notifier.check_masks[1])
631 || FD_ISSET (i - 1, &gdb_notifier.check_masks[2]))
634 gdb_notifier.num_fds = i;
638 /* Deactivate the file descriptor, by clearing its mask,
639 so that it will not fire again. */
643 /* If this file handler was going to be the next one to be handled,
644 advance to the next's next, if any. */
645 if (gdb_notifier.next_file_handler == file_ptr)
647 if (file_ptr->next_file == NULL
648 && file_ptr == gdb_notifier.first_file_handler)
649 gdb_notifier.next_file_handler = NULL;
651 get_next_file_handler_to_handle_and_advance ();
654 /* Get rid of the file handler in the file handler list. */
655 if (file_ptr == gdb_notifier.first_file_handler)
656 gdb_notifier.first_file_handler = file_ptr->next_file;
659 for (prev_ptr = gdb_notifier.first_file_handler;
660 prev_ptr->next_file != file_ptr;
661 prev_ptr = prev_ptr->next_file)
663 prev_ptr->next_file = file_ptr->next_file;
668 /* Handle the given event by calling the procedure associated to the
669 corresponding file handler. */
672 handle_file_event (file_handler *file_ptr, int ready_mask)
681 /* With poll, the ready_mask could have any of three events
682 set to 1: POLLHUP, POLLERR, POLLNVAL. These events
683 cannot be used in the requested event mask (events), but
684 they can be returned in the return mask (revents). We
685 need to check for those event too, and add them to the
686 mask which will be passed to the handler. */
688 /* See if the desired events (mask) match the received
689 events (ready_mask). */
694 /* POLLHUP means EOF, but can be combined with POLLIN to
695 signal more data to read. */
696 error_mask = POLLHUP | POLLERR | POLLNVAL;
697 mask = ready_mask & (file_ptr->mask | error_mask);
699 if ((mask & (POLLERR | POLLNVAL)) != 0)
701 /* Work in progress. We may need to tell somebody
702 what kind of error we had. */
704 printf_unfiltered (_("Error detected on fd %d\n"),
707 printf_unfiltered (_("Invalid or non-`poll'able fd %d\n"),
714 internal_error (__FILE__, __LINE__,
715 _("use_poll without HAVE_POLL"));
716 #endif /* HAVE_POLL */
720 if (ready_mask & GDB_EXCEPTION)
722 printf_unfiltered (_("Exception condition detected "
723 "on fd %d\n"), file_ptr->fd);
728 mask = ready_mask & file_ptr->mask;
731 /* If there was a match, then call the handler. */
733 (*file_ptr->proc) (file_ptr->error, file_ptr->client_data);
738 /* Wait for new events on the monitored file descriptors. Run the
739 event handler if the first descriptor that is detected by the poll.
740 If BLOCK and if there are no events, this function will block in
741 the call to poll. Return 1 if an event was handled. Return -1 if
742 there are no file descriptors to monitor. Return 1 if an event was
743 handled, otherwise returns 0. */
746 gdb_wait_for_event (int block)
748 file_handler *file_ptr;
751 /* Make sure all output is done before getting another event. */
752 gdb_flush (gdb_stdout);
753 gdb_flush (gdb_stderr);
755 if (gdb_notifier.num_fds == 0)
759 update_wait_timeout ();
767 timeout = gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1;
771 num_found = poll (gdb_notifier.poll_fds,
772 (unsigned long) gdb_notifier.num_fds, timeout);
774 /* Don't print anything if we get out of poll because of a
776 if (num_found == -1 && errno != EINTR)
777 perror_with_name (("poll"));
779 internal_error (__FILE__, __LINE__,
780 _("use_poll without HAVE_POLL"));
781 #endif /* HAVE_POLL */
785 struct timeval select_timeout;
786 struct timeval *timeout_p;
789 timeout_p = gdb_notifier.timeout_valid
790 ? &gdb_notifier.select_timeout : NULL;
793 memset (&select_timeout, 0, sizeof (select_timeout));
794 timeout_p = &select_timeout;
797 gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0];
798 gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1];
799 gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2];
800 num_found = gdb_select (gdb_notifier.num_fds,
801 &gdb_notifier.ready_masks[0],
802 &gdb_notifier.ready_masks[1],
803 &gdb_notifier.ready_masks[2],
806 /* Clear the masks after an error from select. */
809 FD_ZERO (&gdb_notifier.ready_masks[0]);
810 FD_ZERO (&gdb_notifier.ready_masks[1]);
811 FD_ZERO (&gdb_notifier.ready_masks[2]);
813 /* Dont print anything if we got a signal, let gdb handle
816 perror_with_name (("select"));
820 /* Avoid looking at poll_fds[i]->revents if no event fired. */
824 /* Run event handlers. We always run just one handler and go back
825 to polling, in case a handler changes the notifier list. Since
826 events for sources we haven't consumed yet wake poll/select
827 immediately, no event is lost. */
829 /* To level the fairness across event descriptors, we handle them in
830 a round-robin-like fashion. The number and order of descriptors
831 may change between invocations, but this is good enough. */
840 if (gdb_notifier.next_poll_fds_index >= gdb_notifier.num_fds)
841 gdb_notifier.next_poll_fds_index = 0;
842 i = gdb_notifier.next_poll_fds_index++;
844 gdb_assert (i < gdb_notifier.num_fds);
845 if ((gdb_notifier.poll_fds + i)->revents)
849 for (file_ptr = gdb_notifier.first_file_handler;
851 file_ptr = file_ptr->next_file)
853 if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd)
856 gdb_assert (file_ptr != NULL);
858 mask = (gdb_notifier.poll_fds + i)->revents;
859 handle_file_event (file_ptr, mask);
862 internal_error (__FILE__, __LINE__,
863 _("use_poll without HAVE_POLL"));
864 #endif /* HAVE_POLL */
868 /* See comment about even source fairness above. */
873 file_ptr = get_next_file_handler_to_handle_and_advance ();
875 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0]))
876 mask |= GDB_READABLE;
877 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1]))
878 mask |= GDB_WRITABLE;
879 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2]))
880 mask |= GDB_EXCEPTION;
884 handle_file_event (file_ptr, mask);
891 /* Create an asynchronous handler, allocating memory for it.
892 Return a pointer to the newly created handler.
893 This pointer will be used to invoke the handler by
894 invoke_async_signal_handler.
895 PROC is the function to call with CLIENT_DATA argument
896 whenever the handler is invoked. */
897 async_signal_handler *
898 create_async_signal_handler (sig_handler_func * proc,
899 gdb_client_data client_data)
901 async_signal_handler *async_handler_ptr;
903 async_handler_ptr = XNEW (async_signal_handler);
904 async_handler_ptr->ready = 0;
905 async_handler_ptr->next_handler = NULL;
906 async_handler_ptr->proc = proc;
907 async_handler_ptr->client_data = client_data;
908 if (sighandler_list.first_handler == NULL)
909 sighandler_list.first_handler = async_handler_ptr;
911 sighandler_list.last_handler->next_handler = async_handler_ptr;
912 sighandler_list.last_handler = async_handler_ptr;
913 return async_handler_ptr;
916 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
917 will be used when the handlers are invoked, after we have waited
918 for some event. The caller of this function is the interrupt
919 handler associated with a signal. */
921 mark_async_signal_handler (async_signal_handler * async_handler_ptr)
923 async_handler_ptr->ready = 1;
924 serial_event_set (async_signal_handlers_serial_event);
927 /* See event-loop.h. */
930 clear_async_signal_handler (async_signal_handler *async_handler_ptr)
932 async_handler_ptr->ready = 0;
935 /* See event-loop.h. */
938 async_signal_handler_is_marked (async_signal_handler *async_handler_ptr)
940 return async_handler_ptr->ready;
943 /* Call all the handlers that are ready. Returns true if any was
947 invoke_async_signal_handlers (void)
949 async_signal_handler *async_handler_ptr;
952 /* We're going to handle all pending signals, so no need to wake up
953 the event loop again the next time around. Note this must be
954 cleared _before_ calling the callbacks, to avoid races. */
955 serial_event_clear (async_signal_handlers_serial_event);
957 /* Invoke all ready handlers. */
961 for (async_handler_ptr = sighandler_list.first_handler;
962 async_handler_ptr != NULL;
963 async_handler_ptr = async_handler_ptr->next_handler)
965 if (async_handler_ptr->ready)
968 if (async_handler_ptr == NULL)
971 async_handler_ptr->ready = 0;
972 /* Async signal handlers have no connection to whichever was the
973 current UI, and thus always run on the main one. */
974 current_ui = main_ui;
975 (*async_handler_ptr->proc) (async_handler_ptr->client_data);
981 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
982 Free the space allocated for it. */
984 delete_async_signal_handler (async_signal_handler ** async_handler_ptr)
986 async_signal_handler *prev_ptr;
988 if (sighandler_list.first_handler == (*async_handler_ptr))
990 sighandler_list.first_handler = (*async_handler_ptr)->next_handler;
991 if (sighandler_list.first_handler == NULL)
992 sighandler_list.last_handler = NULL;
996 prev_ptr = sighandler_list.first_handler;
997 while (prev_ptr && prev_ptr->next_handler != (*async_handler_ptr))
998 prev_ptr = prev_ptr->next_handler;
999 gdb_assert (prev_ptr);
1000 prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
1001 if (sighandler_list.last_handler == (*async_handler_ptr))
1002 sighandler_list.last_handler = prev_ptr;
1004 xfree ((*async_handler_ptr));
1005 (*async_handler_ptr) = NULL;
1008 /* Create an asynchronous event handler, allocating memory for it.
1009 Return a pointer to the newly created handler. PROC is the
1010 function to call with CLIENT_DATA argument whenever the handler is
1012 async_event_handler *
1013 create_async_event_handler (async_event_handler_func *proc,
1014 gdb_client_data client_data)
1016 async_event_handler *h;
1018 h = XNEW (struct async_event_handler);
1020 h->next_handler = NULL;
1022 h->client_data = client_data;
1023 if (async_event_handler_list.first_handler == NULL)
1024 async_event_handler_list.first_handler = h;
1026 async_event_handler_list.last_handler->next_handler = h;
1027 async_event_handler_list.last_handler = h;
1031 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
1032 will be used by gdb_do_one_event. The caller will be whoever
1033 created the event source, and wants to signal that the event is
1034 ready to be handled. */
1036 mark_async_event_handler (async_event_handler *async_handler_ptr)
1038 async_handler_ptr->ready = 1;
1041 /* See event-loop.h. */
1044 clear_async_event_handler (async_event_handler *async_handler_ptr)
1046 async_handler_ptr->ready = 0;
1049 /* Check if asynchronous event handlers are ready, and call the
1050 handler function for one that is. */
1053 check_async_event_handlers (void)
1055 async_event_handler *async_handler_ptr;
1057 for (async_handler_ptr = async_event_handler_list.first_handler;
1058 async_handler_ptr != NULL;
1059 async_handler_ptr = async_handler_ptr->next_handler)
1061 if (async_handler_ptr->ready)
1063 async_handler_ptr->ready = 0;
1064 (*async_handler_ptr->proc) (async_handler_ptr->client_data);
1072 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
1073 Free the space allocated for it. */
1075 delete_async_event_handler (async_event_handler **async_handler_ptr)
1077 async_event_handler *prev_ptr;
1079 if (async_event_handler_list.first_handler == *async_handler_ptr)
1081 async_event_handler_list.first_handler
1082 = (*async_handler_ptr)->next_handler;
1083 if (async_event_handler_list.first_handler == NULL)
1084 async_event_handler_list.last_handler = NULL;
1088 prev_ptr = async_event_handler_list.first_handler;
1089 while (prev_ptr && prev_ptr->next_handler != *async_handler_ptr)
1090 prev_ptr = prev_ptr->next_handler;
1091 gdb_assert (prev_ptr);
1092 prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
1093 if (async_event_handler_list.last_handler == (*async_handler_ptr))
1094 async_event_handler_list.last_handler = prev_ptr;
1096 xfree (*async_handler_ptr);
1097 *async_handler_ptr = NULL;
1100 /* Create a timer that will expire in MS milliseconds from now. When
1101 the timer is ready, PROC will be executed. At creation, the timer
1102 is added to the timers queue. This queue is kept sorted in order
1103 of increasing timers. Return a handle to the timer struct. */
1106 create_timer (int ms, timer_handler_func *proc,
1107 gdb_client_data client_data)
1109 using namespace std::chrono;
1110 struct gdb_timer *timer_ptr, *timer_index, *prev_timer;
1112 steady_clock::time_point time_now = steady_clock::now ();
1114 timer_ptr = new gdb_timer ();
1115 timer_ptr->when = time_now + milliseconds (ms);
1116 timer_ptr->proc = proc;
1117 timer_ptr->client_data = client_data;
1118 timer_list.num_timers++;
1119 timer_ptr->timer_id = timer_list.num_timers;
1121 /* Now add the timer to the timer queue, making sure it is sorted in
1122 increasing order of expiration. */
1124 for (timer_index = timer_list.first_timer;
1125 timer_index != NULL;
1126 timer_index = timer_index->next)
1128 if (timer_index->when > timer_ptr->when)
1132 if (timer_index == timer_list.first_timer)
1134 timer_ptr->next = timer_list.first_timer;
1135 timer_list.first_timer = timer_ptr;
1140 for (prev_timer = timer_list.first_timer;
1141 prev_timer->next != timer_index;
1142 prev_timer = prev_timer->next)
1145 prev_timer->next = timer_ptr;
1146 timer_ptr->next = timer_index;
1149 gdb_notifier.timeout_valid = 0;
1150 return timer_ptr->timer_id;
1153 /* There is a chance that the creator of the timer wants to get rid of
1154 it before it expires. */
1156 delete_timer (int id)
1158 struct gdb_timer *timer_ptr, *prev_timer = NULL;
1160 /* Find the entry for the given timer. */
1162 for (timer_ptr = timer_list.first_timer; timer_ptr != NULL;
1163 timer_ptr = timer_ptr->next)
1165 if (timer_ptr->timer_id == id)
1169 if (timer_ptr == NULL)
1171 /* Get rid of the timer in the timer list. */
1172 if (timer_ptr == timer_list.first_timer)
1173 timer_list.first_timer = timer_ptr->next;
1176 for (prev_timer = timer_list.first_timer;
1177 prev_timer->next != timer_ptr;
1178 prev_timer = prev_timer->next)
1180 prev_timer->next = timer_ptr->next;
1184 gdb_notifier.timeout_valid = 0;
1187 /* Convert a std::chrono duration to a struct timeval. */
1189 template<typename Duration>
1190 static struct timeval
1191 duration_cast_timeval (const Duration &d)
1193 using namespace std::chrono;
1194 seconds sec = duration_cast<seconds> (d);
1195 microseconds msec = duration_cast<microseconds> (d - sec);
1198 tv.tv_sec = sec.count ();
1199 tv.tv_usec = msec.count ();
1203 /* Update the timeout for the select() or poll(). Returns true if the
1204 timer has already expired, false otherwise. */
1207 update_wait_timeout (void)
1209 if (timer_list.first_timer != NULL)
1211 using namespace std::chrono;
1212 steady_clock::time_point time_now = steady_clock::now ();
1213 struct timeval timeout;
1215 if (timer_list.first_timer->when < time_now)
1217 /* It expired already. */
1219 timeout.tv_usec = 0;
1223 steady_clock::duration d = timer_list.first_timer->when - time_now;
1224 timeout = duration_cast_timeval (d);
1227 /* Update the timeout for select/ poll. */
1231 gdb_notifier.poll_timeout = timeout.tv_sec * 1000;
1233 internal_error (__FILE__, __LINE__,
1234 _("use_poll without HAVE_POLL"));
1235 #endif /* HAVE_POLL */
1239 gdb_notifier.select_timeout.tv_sec = timeout.tv_sec;
1240 gdb_notifier.select_timeout.tv_usec = timeout.tv_usec;
1242 gdb_notifier.timeout_valid = 1;
1244 if (timer_list.first_timer->when < time_now)
1248 gdb_notifier.timeout_valid = 0;
1253 /* Check whether a timer in the timers queue is ready. If a timer is
1254 ready, call its handler and return. Update the timeout for the
1255 select() or poll() as well. Return 1 if an event was handled,
1256 otherwise returns 0.*/
1261 if (update_wait_timeout ())
1263 struct gdb_timer *timer_ptr = timer_list.first_timer;
1264 timer_handler_func *proc = timer_ptr->proc;
1265 gdb_client_data client_data = timer_ptr->client_data;
1267 /* Get rid of the timer from the beginning of the list. */
1268 timer_list.first_timer = timer_ptr->next;
1270 /* Delete the timer before calling the callback, not after, in
1271 case the callback itself decides to try deleting the timer
1275 /* Call the procedure associated with that timer. */
1276 (proc) (client_data);