1 /* Event loop machinery for GDB, the GNU debugger.
2 Copyright (C) 1999-2020 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 "ser-event.h"
28 #if defined (HAVE_POLL_H)
30 #elif defined (HAVE_SYS_POLL_H)
35 #include <sys/types.h>
36 #include "gdbsupport/gdb_sys_time.h"
37 #include "gdbsupport/gdb_select.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 /* Information about each file descriptor we register with the event
50 typedef struct file_handler
52 int fd
; /* File descriptor. */
53 int mask
; /* Events we want to monitor: POLLIN, etc. */
54 int ready_mask
; /* Events that have been seen since
56 handler_func
*proc
; /* Procedure to call when fd is ready. */
57 gdb_client_data client_data
; /* Argument to pass to proc. */
58 int error
; /* Was an error detected on this fd? */
59 struct file_handler
*next_file
; /* Next registered file descriptor. */
63 /* PROC is a function to be invoked when the READY flag is set. This
64 happens when there has been a signal and the corresponding signal
65 handler has 'triggered' this async_signal_handler for execution.
66 The actual work to be done in response to a signal will be carried
67 out by PROC at a later time, within process_event. This provides a
68 deferred execution of signal handlers.
70 Async_init_signals takes care of setting up such an
71 async_signal_handler for each interesting signal. */
73 typedef struct async_signal_handler
75 int ready
; /* If ready, call this handler
76 from the main event loop, using
77 invoke_async_handler. */
78 struct async_signal_handler
*next_handler
; /* Ptr to next handler. */
79 sig_handler_func
*proc
; /* Function to call to do the work. */
80 gdb_client_data client_data
; /* Argument to async_handler_func. */
84 /* PROC is a function to be invoked when the READY flag is set. This
85 happens when the event has been marked with
86 MARK_ASYNC_EVENT_HANDLER. The actual work to be done in response
87 to an event will be carried out by PROC at a later time, within
88 process_event. This provides a deferred execution of event
90 typedef struct async_event_handler
92 /* If ready, call this handler from the main event loop, using
93 invoke_event_handler. */
96 /* Point to next handler. */
97 struct async_event_handler
*next_handler
;
99 /* Function to call to do the work. */
100 async_event_handler_func
*proc
;
102 /* Argument to PROC. */
103 gdb_client_data client_data
;
107 /* Gdb_notifier is just a list of file descriptors gdb is interested in.
108 These are the input file descriptor, and the target file
109 descriptor. We have two flavors of the notifier, one for platforms
110 that have the POLL function, the other for those that don't, and
111 only support SELECT. Each of the elements in the gdb_notifier list is
112 basically a description of what kind of events gdb is interested
115 /* As of 1999-04-30 only the input file descriptor is registered with the
118 /* Do we use poll or select ? */
123 #endif /* HAVE_POLL */
125 static unsigned char use_poll
= USE_POLL
;
134 /* Ptr to head of file handler list. */
135 file_handler
*first_file_handler
;
137 /* Next file handler to handle, for the select variant. To level
138 the fairness across event sources, we serve file handlers in a
139 round-robin-like fashion. The number and order of the polled
140 file handlers may change between invocations, but this is good
142 file_handler
*next_file_handler
;
145 /* Ptr to array of pollfd structures. */
146 struct pollfd
*poll_fds
;
148 /* Next file descriptor to handle, for the poll variant. To level
149 the fairness across event sources, we poll the file descriptors
150 in a round-robin-like fashion. The number and order of the
151 polled file descriptors may change between invocations, but
152 this is good enough. */
153 int next_poll_fds_index
;
155 /* Timeout in milliseconds for calls to poll(). */
159 /* Masks to be used in the next call to select.
160 Bits are set in response to calls to create_file_handler. */
161 fd_set check_masks
[3];
163 /* What file descriptors were found ready by select. */
164 fd_set ready_masks
[3];
166 /* Number of file descriptors to monitor (for poll). */
167 /* Number of valid bits (highest fd value + 1) (for select). */
170 /* Time structure for calls to select(). */
171 struct timeval select_timeout
;
173 /* Flag to tell whether the timeout should be used. */
178 /* Structure associated with a timer. PROC will be executed at the
179 first occasion after WHEN. */
182 std::chrono::steady_clock::time_point when
;
184 struct gdb_timer
*next
;
185 timer_handler_func
*proc
; /* Function to call to do the work. */
186 gdb_client_data client_data
; /* Argument to async_handler_func. */
189 /* List of currently active timers. It is sorted in order of
190 increasing timers. */
193 /* Pointer to first in timer list. */
194 struct gdb_timer
*first_timer
;
196 /* Id of the last timer created. */
201 /* All the async_signal_handlers gdb is interested in are kept onto
205 /* Pointer to first in handler list. */
206 async_signal_handler
*first_handler
;
208 /* Pointer to last in handler list. */
209 async_signal_handler
*last_handler
;
213 /* All the async_event_handlers gdb is interested in are kept onto
217 /* Pointer to first in handler list. */
218 async_event_handler
*first_handler
;
220 /* Pointer to last in handler list. */
221 async_event_handler
*last_handler
;
223 async_event_handler_list
;
225 static int invoke_async_signal_handlers (void);
226 static void create_file_handler (int fd
, int mask
, handler_func
*proc
,
227 gdb_client_data client_data
);
228 static int check_async_event_handlers (void);
229 static int gdb_wait_for_event (int);
230 static int update_wait_timeout (void);
231 static int poll_timers (void);
234 /* This event is signalled whenever an asynchronous handler needs to
235 defer an action to the event loop. */
236 static struct serial_event
*async_signal_handlers_serial_event
;
238 /* Callback registered with ASYNC_SIGNAL_HANDLERS_SERIAL_EVENT. */
241 async_signals_handler (int error
, gdb_client_data client_data
)
243 /* Do nothing. Handlers are run by invoke_async_signal_handlers
248 initialize_async_signal_handlers (void)
250 async_signal_handlers_serial_event
= make_serial_event ();
252 add_file_handler (serial_event_fd (async_signal_handlers_serial_event
),
253 async_signals_handler
, NULL
);
256 /* Process one high level event. If nothing is ready at this time,
257 wait for something to happen (via gdb_wait_for_event), then process
258 it. Returns >0 if something was done otherwise returns <0 (this
259 can happen if there are no event sources to wait for). */
262 gdb_do_one_event (void)
264 static int event_source_head
= 0;
265 const int number_of_sources
= 3;
268 /* First let's see if there are any asynchronous signal handlers
269 that are ready. These would be the result of invoking any of the
271 if (invoke_async_signal_handlers ())
274 /* To level the fairness across event sources, we poll them in a
275 round-robin fashion. */
276 for (current
= 0; current
< number_of_sources
; current
++)
280 switch (event_source_head
)
283 /* Are any timers that are ready? */
284 res
= poll_timers ();
287 /* Are there events already waiting to be collected on the
288 monitored file descriptors? */
289 res
= gdb_wait_for_event (0);
292 /* Are there any asynchronous event handlers ready? */
293 res
= check_async_event_handlers ();
296 internal_error (__FILE__
, __LINE__
,
297 "unexpected event_source_head %d",
302 if (event_source_head
== number_of_sources
)
303 event_source_head
= 0;
309 /* Block waiting for a new event. If gdb_wait_for_event returns -1,
310 we should get out because this means that there are no event
311 sources left. This will make the event loop stop, and the
314 if (gdb_wait_for_event (1) < 0)
317 /* If gdb_wait_for_event has returned 1, it means that one event has
318 been handled. We break out of the loop. */
324 /* Wrapper function for create_file_handler, so that the caller
325 doesn't have to know implementation details about the use of poll
328 add_file_handler (int fd
, handler_func
* proc
, gdb_client_data client_data
)
337 /* Check to see if poll () is usable. If not, we'll switch to
338 use select. This can happen on systems like
339 m68k-motorola-sys, `poll' cannot be used to wait for `stdin'.
340 On m68k-motorola-sysv, tty's are not stream-based and not
344 if (poll (&fds
, 1, 0) == 1 && (fds
.revents
& POLLNVAL
))
347 internal_error (__FILE__
, __LINE__
,
348 _("use_poll without HAVE_POLL"));
349 #endif /* HAVE_POLL */
354 create_file_handler (fd
, POLLIN
, proc
, client_data
);
356 internal_error (__FILE__
, __LINE__
,
357 _("use_poll without HAVE_POLL"));
361 create_file_handler (fd
, GDB_READABLE
| GDB_EXCEPTION
,
365 /* Add a file handler/descriptor to the list of descriptors we are
368 FD is the file descriptor for the file/stream to be listened to.
370 For the poll case, MASK is a combination (OR) of POLLIN,
371 POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND:
372 these are the events we are interested in. If any of them occurs,
373 proc should be called.
375 For the select case, MASK is a combination of READABLE, WRITABLE,
376 EXCEPTION. PROC is the procedure that will be called when an event
377 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
380 create_file_handler (int fd
, int mask
, handler_func
* proc
,
381 gdb_client_data client_data
)
383 file_handler
*file_ptr
;
385 /* Do we already have a file handler for this file? (We may be
386 changing its associated procedure). */
387 for (file_ptr
= gdb_notifier
.first_file_handler
; file_ptr
!= NULL
;
388 file_ptr
= file_ptr
->next_file
)
390 if (file_ptr
->fd
== fd
)
394 /* It is a new file descriptor. Add it to the list. Otherwise, just
395 change the data associated with it. */
396 if (file_ptr
== NULL
)
398 file_ptr
= XNEW (file_handler
);
400 file_ptr
->ready_mask
= 0;
401 file_ptr
->next_file
= gdb_notifier
.first_file_handler
;
402 gdb_notifier
.first_file_handler
= file_ptr
;
407 gdb_notifier
.num_fds
++;
408 if (gdb_notifier
.poll_fds
)
409 gdb_notifier
.poll_fds
=
410 (struct pollfd
*) xrealloc (gdb_notifier
.poll_fds
,
411 (gdb_notifier
.num_fds
412 * sizeof (struct pollfd
)));
414 gdb_notifier
.poll_fds
=
415 XNEW (struct pollfd
);
416 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->fd
= fd
;
417 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->events
= mask
;
418 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->revents
= 0;
420 internal_error (__FILE__
, __LINE__
,
421 _("use_poll without HAVE_POLL"));
422 #endif /* HAVE_POLL */
426 if (mask
& GDB_READABLE
)
427 FD_SET (fd
, &gdb_notifier
.check_masks
[0]);
429 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
431 if (mask
& GDB_WRITABLE
)
432 FD_SET (fd
, &gdb_notifier
.check_masks
[1]);
434 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
436 if (mask
& GDB_EXCEPTION
)
437 FD_SET (fd
, &gdb_notifier
.check_masks
[2]);
439 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
441 if (gdb_notifier
.num_fds
<= fd
)
442 gdb_notifier
.num_fds
= fd
+ 1;
446 file_ptr
->proc
= proc
;
447 file_ptr
->client_data
= client_data
;
448 file_ptr
->mask
= mask
;
451 /* Return the next file handler to handle, and advance to the next
452 file handler, wrapping around if the end of the list is
455 static file_handler
*
456 get_next_file_handler_to_handle_and_advance (void)
458 file_handler
*curr_next
;
460 /* The first time around, this is still NULL. */
461 if (gdb_notifier
.next_file_handler
== NULL
)
462 gdb_notifier
.next_file_handler
= gdb_notifier
.first_file_handler
;
464 curr_next
= gdb_notifier
.next_file_handler
;
465 gdb_assert (curr_next
!= NULL
);
468 gdb_notifier
.next_file_handler
= curr_next
->next_file
;
469 /* Wrap around, if necessary. */
470 if (gdb_notifier
.next_file_handler
== NULL
)
471 gdb_notifier
.next_file_handler
= gdb_notifier
.first_file_handler
;
476 /* Remove the file descriptor FD from the list of monitored fd's:
477 i.e. we don't care anymore about events on the FD. */
479 delete_file_handler (int fd
)
481 file_handler
*file_ptr
, *prev_ptr
= NULL
;
485 struct pollfd
*new_poll_fds
;
488 /* Find the entry for the given file. */
490 for (file_ptr
= gdb_notifier
.first_file_handler
; file_ptr
!= NULL
;
491 file_ptr
= file_ptr
->next_file
)
493 if (file_ptr
->fd
== fd
)
497 if (file_ptr
== NULL
)
503 /* Create a new poll_fds array by copying every fd's information
504 but the one we want to get rid of. */
506 new_poll_fds
= (struct pollfd
*)
507 xmalloc ((gdb_notifier
.num_fds
- 1) * sizeof (struct pollfd
));
509 for (i
= 0, j
= 0; i
< gdb_notifier
.num_fds
; i
++)
511 if ((gdb_notifier
.poll_fds
+ i
)->fd
!= fd
)
513 (new_poll_fds
+ j
)->fd
= (gdb_notifier
.poll_fds
+ i
)->fd
;
514 (new_poll_fds
+ j
)->events
= (gdb_notifier
.poll_fds
+ i
)->events
;
515 (new_poll_fds
+ j
)->revents
516 = (gdb_notifier
.poll_fds
+ i
)->revents
;
520 xfree (gdb_notifier
.poll_fds
);
521 gdb_notifier
.poll_fds
= new_poll_fds
;
522 gdb_notifier
.num_fds
--;
524 internal_error (__FILE__
, __LINE__
,
525 _("use_poll without HAVE_POLL"));
526 #endif /* HAVE_POLL */
530 if (file_ptr
->mask
& GDB_READABLE
)
531 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
532 if (file_ptr
->mask
& GDB_WRITABLE
)
533 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
534 if (file_ptr
->mask
& GDB_EXCEPTION
)
535 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
537 /* Find current max fd. */
539 if ((fd
+ 1) == gdb_notifier
.num_fds
)
541 gdb_notifier
.num_fds
--;
542 for (i
= gdb_notifier
.num_fds
; i
; i
--)
544 if (FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[0])
545 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[1])
546 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[2]))
549 gdb_notifier
.num_fds
= i
;
553 /* Deactivate the file descriptor, by clearing its mask,
554 so that it will not fire again. */
558 /* If this file handler was going to be the next one to be handled,
559 advance to the next's next, if any. */
560 if (gdb_notifier
.next_file_handler
== file_ptr
)
562 if (file_ptr
->next_file
== NULL
563 && file_ptr
== gdb_notifier
.first_file_handler
)
564 gdb_notifier
.next_file_handler
= NULL
;
566 get_next_file_handler_to_handle_and_advance ();
569 /* Get rid of the file handler in the file handler list. */
570 if (file_ptr
== gdb_notifier
.first_file_handler
)
571 gdb_notifier
.first_file_handler
= file_ptr
->next_file
;
574 for (prev_ptr
= gdb_notifier
.first_file_handler
;
575 prev_ptr
->next_file
!= file_ptr
;
576 prev_ptr
= prev_ptr
->next_file
)
578 prev_ptr
->next_file
= file_ptr
->next_file
;
583 /* Handle the given event by calling the procedure associated to the
584 corresponding file handler. */
587 handle_file_event (file_handler
*file_ptr
, int ready_mask
)
596 /* With poll, the ready_mask could have any of three events
597 set to 1: POLLHUP, POLLERR, POLLNVAL. These events
598 cannot be used in the requested event mask (events), but
599 they can be returned in the return mask (revents). We
600 need to check for those event too, and add them to the
601 mask which will be passed to the handler. */
603 /* See if the desired events (mask) match the received
604 events (ready_mask). */
609 /* POLLHUP means EOF, but can be combined with POLLIN to
610 signal more data to read. */
611 error_mask
= POLLHUP
| POLLERR
| POLLNVAL
;
612 mask
= ready_mask
& (file_ptr
->mask
| error_mask
);
614 if ((mask
& (POLLERR
| POLLNVAL
)) != 0)
616 /* Work in progress. We may need to tell somebody
617 what kind of error we had. */
619 warning (_("Error detected on fd %d"), file_ptr
->fd
);
621 warning (_("Invalid or non-`poll'able fd %d"),
628 internal_error (__FILE__
, __LINE__
,
629 _("use_poll without HAVE_POLL"));
630 #endif /* HAVE_POLL */
634 if (ready_mask
& GDB_EXCEPTION
)
636 warning (_("Exception condition detected on fd %d"),
642 mask
= ready_mask
& file_ptr
->mask
;
645 /* If there was a match, then call the handler. */
647 (*file_ptr
->proc
) (file_ptr
->error
, file_ptr
->client_data
);
652 /* Wait for new events on the monitored file descriptors. Run the
653 event handler if the first descriptor that is detected by the poll.
654 If BLOCK and if there are no events, this function will block in
655 the call to poll. Return 1 if an event was handled. Return -1 if
656 there are no file descriptors to monitor. Return 1 if an event was
657 handled, otherwise returns 0. */
660 gdb_wait_for_event (int block
)
662 file_handler
*file_ptr
;
665 /* Make sure all output is done before getting another event. */
666 gdb_stdout
->flush ();
667 gdb_stderr
->flush ();
669 if (gdb_notifier
.num_fds
== 0)
673 update_wait_timeout ();
681 timeout
= gdb_notifier
.timeout_valid
? gdb_notifier
.poll_timeout
: -1;
685 num_found
= poll (gdb_notifier
.poll_fds
,
686 (unsigned long) gdb_notifier
.num_fds
, timeout
);
688 /* Don't print anything if we get out of poll because of a
690 if (num_found
== -1 && errno
!= EINTR
)
691 perror_with_name (("poll"));
693 internal_error (__FILE__
, __LINE__
,
694 _("use_poll without HAVE_POLL"));
695 #endif /* HAVE_POLL */
699 struct timeval select_timeout
;
700 struct timeval
*timeout_p
;
703 timeout_p
= gdb_notifier
.timeout_valid
704 ? &gdb_notifier
.select_timeout
: NULL
;
707 memset (&select_timeout
, 0, sizeof (select_timeout
));
708 timeout_p
= &select_timeout
;
711 gdb_notifier
.ready_masks
[0] = gdb_notifier
.check_masks
[0];
712 gdb_notifier
.ready_masks
[1] = gdb_notifier
.check_masks
[1];
713 gdb_notifier
.ready_masks
[2] = gdb_notifier
.check_masks
[2];
714 num_found
= gdb_select (gdb_notifier
.num_fds
,
715 &gdb_notifier
.ready_masks
[0],
716 &gdb_notifier
.ready_masks
[1],
717 &gdb_notifier
.ready_masks
[2],
720 /* Clear the masks after an error from select. */
723 FD_ZERO (&gdb_notifier
.ready_masks
[0]);
724 FD_ZERO (&gdb_notifier
.ready_masks
[1]);
725 FD_ZERO (&gdb_notifier
.ready_masks
[2]);
727 /* Dont print anything if we got a signal, let gdb handle
730 perror_with_name (("select"));
734 /* Avoid looking at poll_fds[i]->revents if no event fired. */
738 /* Run event handlers. We always run just one handler and go back
739 to polling, in case a handler changes the notifier list. Since
740 events for sources we haven't consumed yet wake poll/select
741 immediately, no event is lost. */
743 /* To level the fairness across event descriptors, we handle them in
744 a round-robin-like fashion. The number and order of descriptors
745 may change between invocations, but this is good enough. */
754 if (gdb_notifier
.next_poll_fds_index
>= gdb_notifier
.num_fds
)
755 gdb_notifier
.next_poll_fds_index
= 0;
756 i
= gdb_notifier
.next_poll_fds_index
++;
758 gdb_assert (i
< gdb_notifier
.num_fds
);
759 if ((gdb_notifier
.poll_fds
+ i
)->revents
)
763 for (file_ptr
= gdb_notifier
.first_file_handler
;
765 file_ptr
= file_ptr
->next_file
)
767 if (file_ptr
->fd
== (gdb_notifier
.poll_fds
+ i
)->fd
)
770 gdb_assert (file_ptr
!= NULL
);
772 mask
= (gdb_notifier
.poll_fds
+ i
)->revents
;
773 handle_file_event (file_ptr
, mask
);
776 internal_error (__FILE__
, __LINE__
,
777 _("use_poll without HAVE_POLL"));
778 #endif /* HAVE_POLL */
782 /* See comment about even source fairness above. */
787 file_ptr
= get_next_file_handler_to_handle_and_advance ();
789 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[0]))
790 mask
|= GDB_READABLE
;
791 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[1]))
792 mask
|= GDB_WRITABLE
;
793 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[2]))
794 mask
|= GDB_EXCEPTION
;
798 handle_file_event (file_ptr
, mask
);
805 /* Create an asynchronous handler, allocating memory for it.
806 Return a pointer to the newly created handler.
807 This pointer will be used to invoke the handler by
808 invoke_async_signal_handler.
809 PROC is the function to call with CLIENT_DATA argument
810 whenever the handler is invoked. */
811 async_signal_handler
*
812 create_async_signal_handler (sig_handler_func
* proc
,
813 gdb_client_data client_data
)
815 async_signal_handler
*async_handler_ptr
;
817 async_handler_ptr
= XNEW (async_signal_handler
);
818 async_handler_ptr
->ready
= 0;
819 async_handler_ptr
->next_handler
= NULL
;
820 async_handler_ptr
->proc
= proc
;
821 async_handler_ptr
->client_data
= client_data
;
822 if (sighandler_list
.first_handler
== NULL
)
823 sighandler_list
.first_handler
= async_handler_ptr
;
825 sighandler_list
.last_handler
->next_handler
= async_handler_ptr
;
826 sighandler_list
.last_handler
= async_handler_ptr
;
827 return async_handler_ptr
;
830 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
831 will be used when the handlers are invoked, after we have waited
832 for some event. The caller of this function is the interrupt
833 handler associated with a signal. */
835 mark_async_signal_handler (async_signal_handler
* async_handler_ptr
)
837 async_handler_ptr
->ready
= 1;
838 serial_event_set (async_signal_handlers_serial_event
);
841 /* See event-loop.h. */
844 clear_async_signal_handler (async_signal_handler
*async_handler_ptr
)
846 async_handler_ptr
->ready
= 0;
849 /* See event-loop.h. */
852 async_signal_handler_is_marked (async_signal_handler
*async_handler_ptr
)
854 return async_handler_ptr
->ready
;
857 /* Call all the handlers that are ready. Returns true if any was
861 invoke_async_signal_handlers (void)
863 async_signal_handler
*async_handler_ptr
;
866 /* We're going to handle all pending signals, so no need to wake up
867 the event loop again the next time around. Note this must be
868 cleared _before_ calling the callbacks, to avoid races. */
869 serial_event_clear (async_signal_handlers_serial_event
);
871 /* Invoke all ready handlers. */
875 for (async_handler_ptr
= sighandler_list
.first_handler
;
876 async_handler_ptr
!= NULL
;
877 async_handler_ptr
= async_handler_ptr
->next_handler
)
879 if (async_handler_ptr
->ready
)
882 if (async_handler_ptr
== NULL
)
885 async_handler_ptr
->ready
= 0;
886 /* Async signal handlers have no connection to whichever was the
887 current UI, and thus always run on the main one. */
888 current_ui
= main_ui
;
889 (*async_handler_ptr
->proc
) (async_handler_ptr
->client_data
);
895 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
896 Free the space allocated for it. */
898 delete_async_signal_handler (async_signal_handler
** async_handler_ptr
)
900 async_signal_handler
*prev_ptr
;
902 if (sighandler_list
.first_handler
== (*async_handler_ptr
))
904 sighandler_list
.first_handler
= (*async_handler_ptr
)->next_handler
;
905 if (sighandler_list
.first_handler
== NULL
)
906 sighandler_list
.last_handler
= NULL
;
910 prev_ptr
= sighandler_list
.first_handler
;
911 while (prev_ptr
&& prev_ptr
->next_handler
!= (*async_handler_ptr
))
912 prev_ptr
= prev_ptr
->next_handler
;
913 gdb_assert (prev_ptr
);
914 prev_ptr
->next_handler
= (*async_handler_ptr
)->next_handler
;
915 if (sighandler_list
.last_handler
== (*async_handler_ptr
))
916 sighandler_list
.last_handler
= prev_ptr
;
918 xfree ((*async_handler_ptr
));
919 (*async_handler_ptr
) = NULL
;
922 /* Create an asynchronous event handler, allocating memory for it.
923 Return a pointer to the newly created handler. PROC is the
924 function to call with CLIENT_DATA argument whenever the handler is
926 async_event_handler
*
927 create_async_event_handler (async_event_handler_func
*proc
,
928 gdb_client_data client_data
)
930 async_event_handler
*h
;
932 h
= XNEW (struct async_event_handler
);
934 h
->next_handler
= NULL
;
936 h
->client_data
= client_data
;
937 if (async_event_handler_list
.first_handler
== NULL
)
938 async_event_handler_list
.first_handler
= h
;
940 async_event_handler_list
.last_handler
->next_handler
= h
;
941 async_event_handler_list
.last_handler
= h
;
945 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
946 will be used by gdb_do_one_event. The caller will be whoever
947 created the event source, and wants to signal that the event is
948 ready to be handled. */
950 mark_async_event_handler (async_event_handler
*async_handler_ptr
)
952 async_handler_ptr
->ready
= 1;
955 /* See event-loop.h. */
958 clear_async_event_handler (async_event_handler
*async_handler_ptr
)
960 async_handler_ptr
->ready
= 0;
963 /* Check if asynchronous event handlers are ready, and call the
964 handler function for one that is. */
967 check_async_event_handlers (void)
969 async_event_handler
*async_handler_ptr
;
971 for (async_handler_ptr
= async_event_handler_list
.first_handler
;
972 async_handler_ptr
!= NULL
;
973 async_handler_ptr
= async_handler_ptr
->next_handler
)
975 if (async_handler_ptr
->ready
)
977 async_handler_ptr
->ready
= 0;
978 (*async_handler_ptr
->proc
) (async_handler_ptr
->client_data
);
986 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
987 Free the space allocated for it. */
989 delete_async_event_handler (async_event_handler
**async_handler_ptr
)
991 async_event_handler
*prev_ptr
;
993 if (async_event_handler_list
.first_handler
== *async_handler_ptr
)
995 async_event_handler_list
.first_handler
996 = (*async_handler_ptr
)->next_handler
;
997 if (async_event_handler_list
.first_handler
== NULL
)
998 async_event_handler_list
.last_handler
= NULL
;
1002 prev_ptr
= async_event_handler_list
.first_handler
;
1003 while (prev_ptr
&& prev_ptr
->next_handler
!= *async_handler_ptr
)
1004 prev_ptr
= prev_ptr
->next_handler
;
1005 gdb_assert (prev_ptr
);
1006 prev_ptr
->next_handler
= (*async_handler_ptr
)->next_handler
;
1007 if (async_event_handler_list
.last_handler
== (*async_handler_ptr
))
1008 async_event_handler_list
.last_handler
= prev_ptr
;
1010 xfree (*async_handler_ptr
);
1011 *async_handler_ptr
= NULL
;
1014 /* Create a timer that will expire in MS milliseconds from now. When
1015 the timer is ready, PROC will be executed. At creation, the timer
1016 is added to the timers queue. This queue is kept sorted in order
1017 of increasing timers. Return a handle to the timer struct. */
1020 create_timer (int ms
, timer_handler_func
*proc
,
1021 gdb_client_data client_data
)
1023 using namespace std::chrono
;
1024 struct gdb_timer
*timer_ptr
, *timer_index
, *prev_timer
;
1026 steady_clock::time_point time_now
= steady_clock::now ();
1028 timer_ptr
= new gdb_timer ();
1029 timer_ptr
->when
= time_now
+ milliseconds (ms
);
1030 timer_ptr
->proc
= proc
;
1031 timer_ptr
->client_data
= client_data
;
1032 timer_list
.num_timers
++;
1033 timer_ptr
->timer_id
= timer_list
.num_timers
;
1035 /* Now add the timer to the timer queue, making sure it is sorted in
1036 increasing order of expiration. */
1038 for (timer_index
= timer_list
.first_timer
;
1039 timer_index
!= NULL
;
1040 timer_index
= timer_index
->next
)
1042 if (timer_index
->when
> timer_ptr
->when
)
1046 if (timer_index
== timer_list
.first_timer
)
1048 timer_ptr
->next
= timer_list
.first_timer
;
1049 timer_list
.first_timer
= timer_ptr
;
1054 for (prev_timer
= timer_list
.first_timer
;
1055 prev_timer
->next
!= timer_index
;
1056 prev_timer
= prev_timer
->next
)
1059 prev_timer
->next
= timer_ptr
;
1060 timer_ptr
->next
= timer_index
;
1063 gdb_notifier
.timeout_valid
= 0;
1064 return timer_ptr
->timer_id
;
1067 /* There is a chance that the creator of the timer wants to get rid of
1068 it before it expires. */
1070 delete_timer (int id
)
1072 struct gdb_timer
*timer_ptr
, *prev_timer
= NULL
;
1074 /* Find the entry for the given timer. */
1076 for (timer_ptr
= timer_list
.first_timer
; timer_ptr
!= NULL
;
1077 timer_ptr
= timer_ptr
->next
)
1079 if (timer_ptr
->timer_id
== id
)
1083 if (timer_ptr
== NULL
)
1085 /* Get rid of the timer in the timer list. */
1086 if (timer_ptr
== timer_list
.first_timer
)
1087 timer_list
.first_timer
= timer_ptr
->next
;
1090 for (prev_timer
= timer_list
.first_timer
;
1091 prev_timer
->next
!= timer_ptr
;
1092 prev_timer
= prev_timer
->next
)
1094 prev_timer
->next
= timer_ptr
->next
;
1098 gdb_notifier
.timeout_valid
= 0;
1101 /* Convert a std::chrono duration to a struct timeval. */
1103 template<typename Duration
>
1104 static struct timeval
1105 duration_cast_timeval (const Duration
&d
)
1107 using namespace std::chrono
;
1108 seconds sec
= duration_cast
<seconds
> (d
);
1109 microseconds msec
= duration_cast
<microseconds
> (d
- sec
);
1112 tv
.tv_sec
= sec
.count ();
1113 tv
.tv_usec
= msec
.count ();
1117 /* Update the timeout for the select() or poll(). Returns true if the
1118 timer has already expired, false otherwise. */
1121 update_wait_timeout (void)
1123 if (timer_list
.first_timer
!= NULL
)
1125 using namespace std::chrono
;
1126 steady_clock::time_point time_now
= steady_clock::now ();
1127 struct timeval timeout
;
1129 if (timer_list
.first_timer
->when
< time_now
)
1131 /* It expired already. */
1133 timeout
.tv_usec
= 0;
1137 steady_clock::duration d
= timer_list
.first_timer
->when
- time_now
;
1138 timeout
= duration_cast_timeval (d
);
1141 /* Update the timeout for select/ poll. */
1145 gdb_notifier
.poll_timeout
= timeout
.tv_sec
* 1000;
1147 internal_error (__FILE__
, __LINE__
,
1148 _("use_poll without HAVE_POLL"));
1149 #endif /* HAVE_POLL */
1153 gdb_notifier
.select_timeout
.tv_sec
= timeout
.tv_sec
;
1154 gdb_notifier
.select_timeout
.tv_usec
= timeout
.tv_usec
;
1156 gdb_notifier
.timeout_valid
= 1;
1158 if (timer_list
.first_timer
->when
< time_now
)
1162 gdb_notifier
.timeout_valid
= 0;
1167 /* Check whether a timer in the timers queue is ready. If a timer is
1168 ready, call its handler and return. Update the timeout for the
1169 select() or poll() as well. Return 1 if an event was handled,
1170 otherwise returns 0.*/
1175 if (update_wait_timeout ())
1177 struct gdb_timer
*timer_ptr
= timer_list
.first_timer
;
1178 timer_handler_func
*proc
= timer_ptr
->proc
;
1179 gdb_client_data client_data
= timer_ptr
->client_data
;
1181 /* Get rid of the timer from the beginning of the list. */
1182 timer_list
.first_timer
= timer_ptr
->next
;
1184 /* Delete the timer before calling the callback, not after, in
1185 case the callback itself decides to try deleting the timer
1189 /* Call the procedure associated with that timer. */
1190 (proc
) (client_data
);