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"
26 #if defined (HAVE_POLL_H)
28 #elif defined (HAVE_SYS_POLL_H)
33 #include <sys/types.h>
34 #include "gdbsupport/gdb_sys_time.h"
35 #include "gdb_select.h"
36 #include "observable.h"
39 /* Tell create_file_handler what events we are interested in.
40 This is used by the select version of the event loop. */
42 #define GDB_READABLE (1<<1)
43 #define GDB_WRITABLE (1<<2)
44 #define GDB_EXCEPTION (1<<3)
46 /* Information about each file descriptor we register with the event
49 typedef struct file_handler
51 int fd
; /* File descriptor. */
52 int mask
; /* Events we want to monitor: POLLIN, etc. */
53 int ready_mask
; /* Events that have been seen since
55 handler_func
*proc
; /* Procedure to call when fd is ready. */
56 gdb_client_data client_data
; /* Argument to pass to proc. */
57 int error
; /* Was an error detected on this fd? */
58 struct file_handler
*next_file
; /* Next registered file descriptor. */
62 /* PROC is a function to be invoked when the READY flag is set. This
63 happens when there has been a signal and the corresponding signal
64 handler has 'triggered' this async_signal_handler for execution.
65 The actual work to be done in response to a signal will be carried
66 out by PROC at a later time, within process_event. This provides a
67 deferred execution of signal handlers.
69 Async_init_signals takes care of setting up such an
70 async_signal_handler for each interesting signal. */
72 typedef struct async_signal_handler
74 int ready
; /* If ready, call this handler
75 from the main event loop, using
76 invoke_async_handler. */
77 struct async_signal_handler
*next_handler
; /* Ptr to next handler. */
78 sig_handler_func
*proc
; /* Function to call to do the work. */
79 gdb_client_data client_data
; /* Argument to async_handler_func. */
83 /* PROC is a function to be invoked when the READY flag is set. This
84 happens when the event has been marked with
85 MARK_ASYNC_EVENT_HANDLER. The actual work to be done in response
86 to an event will be carried out by PROC at a later time, within
87 process_event. This provides a deferred execution of event
89 typedef struct async_event_handler
91 /* If ready, call this handler from the main event loop, using
92 invoke_event_handler. */
95 /* Point to next handler. */
96 struct async_event_handler
*next_handler
;
98 /* Function to call to do the work. */
99 async_event_handler_func
*proc
;
101 /* Argument to PROC. */
102 gdb_client_data client_data
;
106 /* Gdb_notifier is just a list of file descriptors gdb is interested in.
107 These are the input file descriptor, and the target file
108 descriptor. We have two flavors of the notifier, one for platforms
109 that have the POLL function, the other for those that don't, and
110 only support SELECT. Each of the elements in the gdb_notifier list is
111 basically a description of what kind of events gdb is interested
114 /* As of 1999-04-30 only the input file descriptor is registered with the
117 /* Do we use poll or select ? */
122 #endif /* HAVE_POLL */
124 static unsigned char use_poll
= USE_POLL
;
133 /* Ptr to head of file handler list. */
134 file_handler
*first_file_handler
;
136 /* Next file handler to handle, for the select variant. To level
137 the fairness across event sources, we serve file handlers in a
138 round-robin-like fashion. The number and order of the polled
139 file handlers may change between invocations, but this is good
141 file_handler
*next_file_handler
;
144 /* Ptr to array of pollfd structures. */
145 struct pollfd
*poll_fds
;
147 /* Next file descriptor to handle, for the poll variant. To level
148 the fairness across event sources, we poll the file descriptors
149 in a round-robin-like fashion. The number and order of the
150 polled file descriptors may change between invocations, but
151 this is good enough. */
152 int next_poll_fds_index
;
154 /* Timeout in milliseconds for calls to poll(). */
158 /* Masks to be used in the next call to select.
159 Bits are set in response to calls to create_file_handler. */
160 fd_set check_masks
[3];
162 /* What file descriptors were found ready by select. */
163 fd_set ready_masks
[3];
165 /* Number of file descriptors to monitor (for poll). */
166 /* Number of valid bits (highest fd value + 1) (for select). */
169 /* Time structure for calls to select(). */
170 struct timeval select_timeout
;
172 /* Flag to tell whether the timeout should be used. */
177 /* Structure associated with a timer. PROC will be executed at the
178 first occasion after WHEN. */
181 std::chrono::steady_clock::time_point when
;
183 struct gdb_timer
*next
;
184 timer_handler_func
*proc
; /* Function to call to do the work. */
185 gdb_client_data client_data
; /* Argument to async_handler_func. */
188 /* List of currently active timers. It is sorted in order of
189 increasing timers. */
192 /* Pointer to first in timer list. */
193 struct gdb_timer
*first_timer
;
195 /* Id of the last timer created. */
200 /* All the async_signal_handlers gdb is interested in are kept onto
204 /* Pointer to first in handler list. */
205 async_signal_handler
*first_handler
;
207 /* Pointer to last in handler list. */
208 async_signal_handler
*last_handler
;
212 /* All the async_event_handlers gdb is interested in are kept onto
216 /* Pointer to first in handler list. */
217 async_event_handler
*first_handler
;
219 /* Pointer to last in handler list. */
220 async_event_handler
*last_handler
;
222 async_event_handler_list
;
224 static int invoke_async_signal_handlers (void);
225 static void create_file_handler (int fd
, int mask
, handler_func
*proc
,
226 gdb_client_data client_data
);
227 static int check_async_event_handlers (void);
228 static int gdb_wait_for_event (int);
229 static int update_wait_timeout (void);
230 static int poll_timers (void);
233 /* This event is signalled whenever an asynchronous handler needs to
234 defer an action to the event loop. */
235 static struct serial_event
*async_signal_handlers_serial_event
;
237 /* Callback registered with ASYNC_SIGNAL_HANDLERS_SERIAL_EVENT. */
240 async_signals_handler (int error
, gdb_client_data client_data
)
242 /* Do nothing. Handlers are run by invoke_async_signal_handlers
247 initialize_async_signal_handlers (void)
249 async_signal_handlers_serial_event
= make_serial_event ();
251 add_file_handler (serial_event_fd (async_signal_handlers_serial_event
),
252 async_signals_handler
, NULL
);
255 /* Process one high level event. If nothing is ready at this time,
256 wait for something to happen (via gdb_wait_for_event), then process
257 it. Returns >0 if something was done otherwise returns <0 (this
258 can happen if there are no event sources to wait for). */
261 gdb_do_one_event (void)
263 static int event_source_head
= 0;
264 const int number_of_sources
= 3;
267 /* First let's see if there are any asynchronous signal handlers
268 that are ready. These would be the result of invoking any of the
270 if (invoke_async_signal_handlers ())
273 /* To level the fairness across event sources, we poll them in a
274 round-robin fashion. */
275 for (current
= 0; current
< number_of_sources
; current
++)
279 switch (event_source_head
)
282 /* Are any timers that are ready? */
283 res
= poll_timers ();
286 /* Are there events already waiting to be collected on the
287 monitored file descriptors? */
288 res
= gdb_wait_for_event (0);
291 /* Are there any asynchronous event handlers ready? */
292 res
= check_async_event_handlers ();
295 internal_error (__FILE__
, __LINE__
,
296 "unexpected event_source_head %d",
301 if (event_source_head
== number_of_sources
)
302 event_source_head
= 0;
308 /* Block waiting for a new event. If gdb_wait_for_event returns -1,
309 we should get out because this means that there are no event
310 sources left. This will make the event loop stop, and the
313 if (gdb_wait_for_event (1) < 0)
316 /* If gdb_wait_for_event has returned 1, it means that one event has
317 been handled. We break out of the loop. */
321 /* Start up the event loop. This is the entry point to the event loop
322 from the command loop. */
325 start_event_loop (void)
327 /* Loop until there is nothing to do. This is the entry point to
328 the event loop engine. gdb_do_one_event will process one event
329 for each invocation. It blocks waiting for an event and then
337 result
= gdb_do_one_event ();
339 catch (const gdb_exception
&ex
)
341 exception_print (gdb_stderr
, ex
);
343 /* If any exception escaped to here, we better enable
344 stdin. Otherwise, any command that calls async_disable_stdin,
345 and then throws, will leave stdin inoperable. */
346 SWITCH_THRU_ALL_UIS ()
348 async_enable_stdin ();
350 /* If we long-jumped out of do_one_event, we probably didn't
351 get around to resetting the prompt, which leaves readline
352 in a messed-up state. Reset it here. */
353 current_ui
->prompt_state
= PROMPT_NEEDED
;
354 gdb::observers::command_error
.notify ();
355 /* This call looks bizarre, but it is required. If the user
356 entered a command that caused an error,
357 after_char_processing_hook won't be called from
358 rl_callback_read_char_wrapper. Using a cleanup there
359 won't work, since we want this function to be called
360 after a new prompt is printed. */
361 if (after_char_processing_hook
)
362 (*after_char_processing_hook
) ();
363 /* Maybe better to set a flag to be checked somewhere as to
364 whether display the prompt or not. */
371 /* We are done with the event loop. There are no more event sources
372 to listen to. So we exit GDB. */
377 /* Wrapper function for create_file_handler, so that the caller
378 doesn't have to know implementation details about the use of poll
381 add_file_handler (int fd
, handler_func
* proc
, gdb_client_data client_data
)
390 /* Check to see if poll () is usable. If not, we'll switch to
391 use select. This can happen on systems like
392 m68k-motorola-sys, `poll' cannot be used to wait for `stdin'.
393 On m68k-motorola-sysv, tty's are not stream-based and not
397 if (poll (&fds
, 1, 0) == 1 && (fds
.revents
& POLLNVAL
))
400 internal_error (__FILE__
, __LINE__
,
401 _("use_poll without HAVE_POLL"));
402 #endif /* HAVE_POLL */
407 create_file_handler (fd
, POLLIN
, proc
, client_data
);
409 internal_error (__FILE__
, __LINE__
,
410 _("use_poll without HAVE_POLL"));
414 create_file_handler (fd
, GDB_READABLE
| GDB_EXCEPTION
,
418 /* Add a file handler/descriptor to the list of descriptors we are
421 FD is the file descriptor for the file/stream to be listened to.
423 For the poll case, MASK is a combination (OR) of POLLIN,
424 POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND:
425 these are the events we are interested in. If any of them occurs,
426 proc should be called.
428 For the select case, MASK is a combination of READABLE, WRITABLE,
429 EXCEPTION. PROC is the procedure that will be called when an event
430 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
433 create_file_handler (int fd
, int mask
, handler_func
* proc
,
434 gdb_client_data client_data
)
436 file_handler
*file_ptr
;
438 /* Do we already have a file handler for this file? (We may be
439 changing its associated procedure). */
440 for (file_ptr
= gdb_notifier
.first_file_handler
; file_ptr
!= NULL
;
441 file_ptr
= file_ptr
->next_file
)
443 if (file_ptr
->fd
== fd
)
447 /* It is a new file descriptor. Add it to the list. Otherwise, just
448 change the data associated with it. */
449 if (file_ptr
== NULL
)
451 file_ptr
= XNEW (file_handler
);
453 file_ptr
->ready_mask
= 0;
454 file_ptr
->next_file
= gdb_notifier
.first_file_handler
;
455 gdb_notifier
.first_file_handler
= file_ptr
;
460 gdb_notifier
.num_fds
++;
461 if (gdb_notifier
.poll_fds
)
462 gdb_notifier
.poll_fds
=
463 (struct pollfd
*) xrealloc (gdb_notifier
.poll_fds
,
464 (gdb_notifier
.num_fds
465 * sizeof (struct pollfd
)));
467 gdb_notifier
.poll_fds
=
468 XNEW (struct pollfd
);
469 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->fd
= fd
;
470 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->events
= mask
;
471 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->revents
= 0;
473 internal_error (__FILE__
, __LINE__
,
474 _("use_poll without HAVE_POLL"));
475 #endif /* HAVE_POLL */
479 if (mask
& GDB_READABLE
)
480 FD_SET (fd
, &gdb_notifier
.check_masks
[0]);
482 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
484 if (mask
& GDB_WRITABLE
)
485 FD_SET (fd
, &gdb_notifier
.check_masks
[1]);
487 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
489 if (mask
& GDB_EXCEPTION
)
490 FD_SET (fd
, &gdb_notifier
.check_masks
[2]);
492 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
494 if (gdb_notifier
.num_fds
<= fd
)
495 gdb_notifier
.num_fds
= fd
+ 1;
499 file_ptr
->proc
= proc
;
500 file_ptr
->client_data
= client_data
;
501 file_ptr
->mask
= mask
;
504 /* Return the next file handler to handle, and advance to the next
505 file handler, wrapping around if the end of the list is
508 static file_handler
*
509 get_next_file_handler_to_handle_and_advance (void)
511 file_handler
*curr_next
;
513 /* The first time around, this is still NULL. */
514 if (gdb_notifier
.next_file_handler
== NULL
)
515 gdb_notifier
.next_file_handler
= gdb_notifier
.first_file_handler
;
517 curr_next
= gdb_notifier
.next_file_handler
;
518 gdb_assert (curr_next
!= NULL
);
521 gdb_notifier
.next_file_handler
= curr_next
->next_file
;
522 /* Wrap around, if necessary. */
523 if (gdb_notifier
.next_file_handler
== NULL
)
524 gdb_notifier
.next_file_handler
= gdb_notifier
.first_file_handler
;
529 /* Remove the file descriptor FD from the list of monitored fd's:
530 i.e. we don't care anymore about events on the FD. */
532 delete_file_handler (int fd
)
534 file_handler
*file_ptr
, *prev_ptr
= NULL
;
538 struct pollfd
*new_poll_fds
;
541 /* Find the entry for the given file. */
543 for (file_ptr
= gdb_notifier
.first_file_handler
; file_ptr
!= NULL
;
544 file_ptr
= file_ptr
->next_file
)
546 if (file_ptr
->fd
== fd
)
550 if (file_ptr
== NULL
)
556 /* Create a new poll_fds array by copying every fd's information
557 but the one we want to get rid of. */
559 new_poll_fds
= (struct pollfd
*)
560 xmalloc ((gdb_notifier
.num_fds
- 1) * sizeof (struct pollfd
));
562 for (i
= 0, j
= 0; i
< gdb_notifier
.num_fds
; i
++)
564 if ((gdb_notifier
.poll_fds
+ i
)->fd
!= fd
)
566 (new_poll_fds
+ j
)->fd
= (gdb_notifier
.poll_fds
+ i
)->fd
;
567 (new_poll_fds
+ j
)->events
= (gdb_notifier
.poll_fds
+ i
)->events
;
568 (new_poll_fds
+ j
)->revents
569 = (gdb_notifier
.poll_fds
+ i
)->revents
;
573 xfree (gdb_notifier
.poll_fds
);
574 gdb_notifier
.poll_fds
= new_poll_fds
;
575 gdb_notifier
.num_fds
--;
577 internal_error (__FILE__
, __LINE__
,
578 _("use_poll without HAVE_POLL"));
579 #endif /* HAVE_POLL */
583 if (file_ptr
->mask
& GDB_READABLE
)
584 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
585 if (file_ptr
->mask
& GDB_WRITABLE
)
586 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
587 if (file_ptr
->mask
& GDB_EXCEPTION
)
588 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
590 /* Find current max fd. */
592 if ((fd
+ 1) == gdb_notifier
.num_fds
)
594 gdb_notifier
.num_fds
--;
595 for (i
= gdb_notifier
.num_fds
; i
; i
--)
597 if (FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[0])
598 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[1])
599 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[2]))
602 gdb_notifier
.num_fds
= i
;
606 /* Deactivate the file descriptor, by clearing its mask,
607 so that it will not fire again. */
611 /* If this file handler was going to be the next one to be handled,
612 advance to the next's next, if any. */
613 if (gdb_notifier
.next_file_handler
== file_ptr
)
615 if (file_ptr
->next_file
== NULL
616 && file_ptr
== gdb_notifier
.first_file_handler
)
617 gdb_notifier
.next_file_handler
= NULL
;
619 get_next_file_handler_to_handle_and_advance ();
622 /* Get rid of the file handler in the file handler list. */
623 if (file_ptr
== gdb_notifier
.first_file_handler
)
624 gdb_notifier
.first_file_handler
= file_ptr
->next_file
;
627 for (prev_ptr
= gdb_notifier
.first_file_handler
;
628 prev_ptr
->next_file
!= file_ptr
;
629 prev_ptr
= prev_ptr
->next_file
)
631 prev_ptr
->next_file
= file_ptr
->next_file
;
636 /* Handle the given event by calling the procedure associated to the
637 corresponding file handler. */
640 handle_file_event (file_handler
*file_ptr
, int ready_mask
)
649 /* With poll, the ready_mask could have any of three events
650 set to 1: POLLHUP, POLLERR, POLLNVAL. These events
651 cannot be used in the requested event mask (events), but
652 they can be returned in the return mask (revents). We
653 need to check for those event too, and add them to the
654 mask which will be passed to the handler. */
656 /* See if the desired events (mask) match the received
657 events (ready_mask). */
662 /* POLLHUP means EOF, but can be combined with POLLIN to
663 signal more data to read. */
664 error_mask
= POLLHUP
| POLLERR
| POLLNVAL
;
665 mask
= ready_mask
& (file_ptr
->mask
| error_mask
);
667 if ((mask
& (POLLERR
| POLLNVAL
)) != 0)
669 /* Work in progress. We may need to tell somebody
670 what kind of error we had. */
672 printf_unfiltered (_("Error detected on fd %d\n"),
675 printf_unfiltered (_("Invalid or non-`poll'able fd %d\n"),
682 internal_error (__FILE__
, __LINE__
,
683 _("use_poll without HAVE_POLL"));
684 #endif /* HAVE_POLL */
688 if (ready_mask
& GDB_EXCEPTION
)
690 printf_unfiltered (_("Exception condition detected "
691 "on fd %d\n"), file_ptr
->fd
);
696 mask
= ready_mask
& file_ptr
->mask
;
699 /* If there was a match, then call the handler. */
701 (*file_ptr
->proc
) (file_ptr
->error
, file_ptr
->client_data
);
706 /* Wait for new events on the monitored file descriptors. Run the
707 event handler if the first descriptor that is detected by the poll.
708 If BLOCK and if there are no events, this function will block in
709 the call to poll. Return 1 if an event was handled. Return -1 if
710 there are no file descriptors to monitor. Return 1 if an event was
711 handled, otherwise returns 0. */
714 gdb_wait_for_event (int block
)
716 file_handler
*file_ptr
;
719 /* Make sure all output is done before getting another event. */
720 gdb_stdout
->flush ();
721 gdb_stderr
->flush ();
723 if (gdb_notifier
.num_fds
== 0)
727 update_wait_timeout ();
735 timeout
= gdb_notifier
.timeout_valid
? gdb_notifier
.poll_timeout
: -1;
739 num_found
= poll (gdb_notifier
.poll_fds
,
740 (unsigned long) gdb_notifier
.num_fds
, timeout
);
742 /* Don't print anything if we get out of poll because of a
744 if (num_found
== -1 && errno
!= EINTR
)
745 perror_with_name (("poll"));
747 internal_error (__FILE__
, __LINE__
,
748 _("use_poll without HAVE_POLL"));
749 #endif /* HAVE_POLL */
753 struct timeval select_timeout
;
754 struct timeval
*timeout_p
;
757 timeout_p
= gdb_notifier
.timeout_valid
758 ? &gdb_notifier
.select_timeout
: NULL
;
761 memset (&select_timeout
, 0, sizeof (select_timeout
));
762 timeout_p
= &select_timeout
;
765 gdb_notifier
.ready_masks
[0] = gdb_notifier
.check_masks
[0];
766 gdb_notifier
.ready_masks
[1] = gdb_notifier
.check_masks
[1];
767 gdb_notifier
.ready_masks
[2] = gdb_notifier
.check_masks
[2];
768 num_found
= gdb_select (gdb_notifier
.num_fds
,
769 &gdb_notifier
.ready_masks
[0],
770 &gdb_notifier
.ready_masks
[1],
771 &gdb_notifier
.ready_masks
[2],
774 /* Clear the masks after an error from select. */
777 FD_ZERO (&gdb_notifier
.ready_masks
[0]);
778 FD_ZERO (&gdb_notifier
.ready_masks
[1]);
779 FD_ZERO (&gdb_notifier
.ready_masks
[2]);
781 /* Dont print anything if we got a signal, let gdb handle
784 perror_with_name (("select"));
788 /* Avoid looking at poll_fds[i]->revents if no event fired. */
792 /* Run event handlers. We always run just one handler and go back
793 to polling, in case a handler changes the notifier list. Since
794 events for sources we haven't consumed yet wake poll/select
795 immediately, no event is lost. */
797 /* To level the fairness across event descriptors, we handle them in
798 a round-robin-like fashion. The number and order of descriptors
799 may change between invocations, but this is good enough. */
808 if (gdb_notifier
.next_poll_fds_index
>= gdb_notifier
.num_fds
)
809 gdb_notifier
.next_poll_fds_index
= 0;
810 i
= gdb_notifier
.next_poll_fds_index
++;
812 gdb_assert (i
< gdb_notifier
.num_fds
);
813 if ((gdb_notifier
.poll_fds
+ i
)->revents
)
817 for (file_ptr
= gdb_notifier
.first_file_handler
;
819 file_ptr
= file_ptr
->next_file
)
821 if (file_ptr
->fd
== (gdb_notifier
.poll_fds
+ i
)->fd
)
824 gdb_assert (file_ptr
!= NULL
);
826 mask
= (gdb_notifier
.poll_fds
+ i
)->revents
;
827 handle_file_event (file_ptr
, mask
);
830 internal_error (__FILE__
, __LINE__
,
831 _("use_poll without HAVE_POLL"));
832 #endif /* HAVE_POLL */
836 /* See comment about even source fairness above. */
841 file_ptr
= get_next_file_handler_to_handle_and_advance ();
843 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[0]))
844 mask
|= GDB_READABLE
;
845 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[1]))
846 mask
|= GDB_WRITABLE
;
847 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[2]))
848 mask
|= GDB_EXCEPTION
;
852 handle_file_event (file_ptr
, mask
);
859 /* Create an asynchronous handler, allocating memory for it.
860 Return a pointer to the newly created handler.
861 This pointer will be used to invoke the handler by
862 invoke_async_signal_handler.
863 PROC is the function to call with CLIENT_DATA argument
864 whenever the handler is invoked. */
865 async_signal_handler
*
866 create_async_signal_handler (sig_handler_func
* proc
,
867 gdb_client_data client_data
)
869 async_signal_handler
*async_handler_ptr
;
871 async_handler_ptr
= XNEW (async_signal_handler
);
872 async_handler_ptr
->ready
= 0;
873 async_handler_ptr
->next_handler
= NULL
;
874 async_handler_ptr
->proc
= proc
;
875 async_handler_ptr
->client_data
= client_data
;
876 if (sighandler_list
.first_handler
== NULL
)
877 sighandler_list
.first_handler
= async_handler_ptr
;
879 sighandler_list
.last_handler
->next_handler
= async_handler_ptr
;
880 sighandler_list
.last_handler
= async_handler_ptr
;
881 return async_handler_ptr
;
884 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
885 will be used when the handlers are invoked, after we have waited
886 for some event. The caller of this function is the interrupt
887 handler associated with a signal. */
889 mark_async_signal_handler (async_signal_handler
* async_handler_ptr
)
891 async_handler_ptr
->ready
= 1;
892 serial_event_set (async_signal_handlers_serial_event
);
895 /* See event-loop.h. */
898 clear_async_signal_handler (async_signal_handler
*async_handler_ptr
)
900 async_handler_ptr
->ready
= 0;
903 /* See event-loop.h. */
906 async_signal_handler_is_marked (async_signal_handler
*async_handler_ptr
)
908 return async_handler_ptr
->ready
;
911 /* Call all the handlers that are ready. Returns true if any was
915 invoke_async_signal_handlers (void)
917 async_signal_handler
*async_handler_ptr
;
920 /* We're going to handle all pending signals, so no need to wake up
921 the event loop again the next time around. Note this must be
922 cleared _before_ calling the callbacks, to avoid races. */
923 serial_event_clear (async_signal_handlers_serial_event
);
925 /* Invoke all ready handlers. */
929 for (async_handler_ptr
= sighandler_list
.first_handler
;
930 async_handler_ptr
!= NULL
;
931 async_handler_ptr
= async_handler_ptr
->next_handler
)
933 if (async_handler_ptr
->ready
)
936 if (async_handler_ptr
== NULL
)
939 async_handler_ptr
->ready
= 0;
940 /* Async signal handlers have no connection to whichever was the
941 current UI, and thus always run on the main one. */
942 current_ui
= main_ui
;
943 (*async_handler_ptr
->proc
) (async_handler_ptr
->client_data
);
949 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
950 Free the space allocated for it. */
952 delete_async_signal_handler (async_signal_handler
** async_handler_ptr
)
954 async_signal_handler
*prev_ptr
;
956 if (sighandler_list
.first_handler
== (*async_handler_ptr
))
958 sighandler_list
.first_handler
= (*async_handler_ptr
)->next_handler
;
959 if (sighandler_list
.first_handler
== NULL
)
960 sighandler_list
.last_handler
= NULL
;
964 prev_ptr
= sighandler_list
.first_handler
;
965 while (prev_ptr
&& prev_ptr
->next_handler
!= (*async_handler_ptr
))
966 prev_ptr
= prev_ptr
->next_handler
;
967 gdb_assert (prev_ptr
);
968 prev_ptr
->next_handler
= (*async_handler_ptr
)->next_handler
;
969 if (sighandler_list
.last_handler
== (*async_handler_ptr
))
970 sighandler_list
.last_handler
= prev_ptr
;
972 xfree ((*async_handler_ptr
));
973 (*async_handler_ptr
) = NULL
;
976 /* Create an asynchronous event handler, allocating memory for it.
977 Return a pointer to the newly created handler. PROC is the
978 function to call with CLIENT_DATA argument whenever the handler is
980 async_event_handler
*
981 create_async_event_handler (async_event_handler_func
*proc
,
982 gdb_client_data client_data
)
984 async_event_handler
*h
;
986 h
= XNEW (struct async_event_handler
);
988 h
->next_handler
= NULL
;
990 h
->client_data
= client_data
;
991 if (async_event_handler_list
.first_handler
== NULL
)
992 async_event_handler_list
.first_handler
= h
;
994 async_event_handler_list
.last_handler
->next_handler
= h
;
995 async_event_handler_list
.last_handler
= h
;
999 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
1000 will be used by gdb_do_one_event. The caller will be whoever
1001 created the event source, and wants to signal that the event is
1002 ready to be handled. */
1004 mark_async_event_handler (async_event_handler
*async_handler_ptr
)
1006 async_handler_ptr
->ready
= 1;
1009 /* See event-loop.h. */
1012 clear_async_event_handler (async_event_handler
*async_handler_ptr
)
1014 async_handler_ptr
->ready
= 0;
1017 /* Check if asynchronous event handlers are ready, and call the
1018 handler function for one that is. */
1021 check_async_event_handlers (void)
1023 async_event_handler
*async_handler_ptr
;
1025 for (async_handler_ptr
= async_event_handler_list
.first_handler
;
1026 async_handler_ptr
!= NULL
;
1027 async_handler_ptr
= async_handler_ptr
->next_handler
)
1029 if (async_handler_ptr
->ready
)
1031 async_handler_ptr
->ready
= 0;
1032 (*async_handler_ptr
->proc
) (async_handler_ptr
->client_data
);
1040 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
1041 Free the space allocated for it. */
1043 delete_async_event_handler (async_event_handler
**async_handler_ptr
)
1045 async_event_handler
*prev_ptr
;
1047 if (async_event_handler_list
.first_handler
== *async_handler_ptr
)
1049 async_event_handler_list
.first_handler
1050 = (*async_handler_ptr
)->next_handler
;
1051 if (async_event_handler_list
.first_handler
== NULL
)
1052 async_event_handler_list
.last_handler
= NULL
;
1056 prev_ptr
= async_event_handler_list
.first_handler
;
1057 while (prev_ptr
&& prev_ptr
->next_handler
!= *async_handler_ptr
)
1058 prev_ptr
= prev_ptr
->next_handler
;
1059 gdb_assert (prev_ptr
);
1060 prev_ptr
->next_handler
= (*async_handler_ptr
)->next_handler
;
1061 if (async_event_handler_list
.last_handler
== (*async_handler_ptr
))
1062 async_event_handler_list
.last_handler
= prev_ptr
;
1064 xfree (*async_handler_ptr
);
1065 *async_handler_ptr
= NULL
;
1068 /* Create a timer that will expire in MS milliseconds from now. When
1069 the timer is ready, PROC will be executed. At creation, the timer
1070 is added to the timers queue. This queue is kept sorted in order
1071 of increasing timers. Return a handle to the timer struct. */
1074 create_timer (int ms
, timer_handler_func
*proc
,
1075 gdb_client_data client_data
)
1077 using namespace std::chrono
;
1078 struct gdb_timer
*timer_ptr
, *timer_index
, *prev_timer
;
1080 steady_clock::time_point time_now
= steady_clock::now ();
1082 timer_ptr
= new gdb_timer ();
1083 timer_ptr
->when
= time_now
+ milliseconds (ms
);
1084 timer_ptr
->proc
= proc
;
1085 timer_ptr
->client_data
= client_data
;
1086 timer_list
.num_timers
++;
1087 timer_ptr
->timer_id
= timer_list
.num_timers
;
1089 /* Now add the timer to the timer queue, making sure it is sorted in
1090 increasing order of expiration. */
1092 for (timer_index
= timer_list
.first_timer
;
1093 timer_index
!= NULL
;
1094 timer_index
= timer_index
->next
)
1096 if (timer_index
->when
> timer_ptr
->when
)
1100 if (timer_index
== timer_list
.first_timer
)
1102 timer_ptr
->next
= timer_list
.first_timer
;
1103 timer_list
.first_timer
= timer_ptr
;
1108 for (prev_timer
= timer_list
.first_timer
;
1109 prev_timer
->next
!= timer_index
;
1110 prev_timer
= prev_timer
->next
)
1113 prev_timer
->next
= timer_ptr
;
1114 timer_ptr
->next
= timer_index
;
1117 gdb_notifier
.timeout_valid
= 0;
1118 return timer_ptr
->timer_id
;
1121 /* There is a chance that the creator of the timer wants to get rid of
1122 it before it expires. */
1124 delete_timer (int id
)
1126 struct gdb_timer
*timer_ptr
, *prev_timer
= NULL
;
1128 /* Find the entry for the given timer. */
1130 for (timer_ptr
= timer_list
.first_timer
; timer_ptr
!= NULL
;
1131 timer_ptr
= timer_ptr
->next
)
1133 if (timer_ptr
->timer_id
== id
)
1137 if (timer_ptr
== NULL
)
1139 /* Get rid of the timer in the timer list. */
1140 if (timer_ptr
== timer_list
.first_timer
)
1141 timer_list
.first_timer
= timer_ptr
->next
;
1144 for (prev_timer
= timer_list
.first_timer
;
1145 prev_timer
->next
!= timer_ptr
;
1146 prev_timer
= prev_timer
->next
)
1148 prev_timer
->next
= timer_ptr
->next
;
1152 gdb_notifier
.timeout_valid
= 0;
1155 /* Convert a std::chrono duration to a struct timeval. */
1157 template<typename Duration
>
1158 static struct timeval
1159 duration_cast_timeval (const Duration
&d
)
1161 using namespace std::chrono
;
1162 seconds sec
= duration_cast
<seconds
> (d
);
1163 microseconds msec
= duration_cast
<microseconds
> (d
- sec
);
1166 tv
.tv_sec
= sec
.count ();
1167 tv
.tv_usec
= msec
.count ();
1171 /* Update the timeout for the select() or poll(). Returns true if the
1172 timer has already expired, false otherwise. */
1175 update_wait_timeout (void)
1177 if (timer_list
.first_timer
!= NULL
)
1179 using namespace std::chrono
;
1180 steady_clock::time_point time_now
= steady_clock::now ();
1181 struct timeval timeout
;
1183 if (timer_list
.first_timer
->when
< time_now
)
1185 /* It expired already. */
1187 timeout
.tv_usec
= 0;
1191 steady_clock::duration d
= timer_list
.first_timer
->when
- time_now
;
1192 timeout
= duration_cast_timeval (d
);
1195 /* Update the timeout for select/ poll. */
1199 gdb_notifier
.poll_timeout
= timeout
.tv_sec
* 1000;
1201 internal_error (__FILE__
, __LINE__
,
1202 _("use_poll without HAVE_POLL"));
1203 #endif /* HAVE_POLL */
1207 gdb_notifier
.select_timeout
.tv_sec
= timeout
.tv_sec
;
1208 gdb_notifier
.select_timeout
.tv_usec
= timeout
.tv_usec
;
1210 gdb_notifier
.timeout_valid
= 1;
1212 if (timer_list
.first_timer
->when
< time_now
)
1216 gdb_notifier
.timeout_valid
= 0;
1221 /* Check whether a timer in the timers queue is ready. If a timer is
1222 ready, call its handler and return. Update the timeout for the
1223 select() or poll() as well. Return 1 if an event was handled,
1224 otherwise returns 0.*/
1229 if (update_wait_timeout ())
1231 struct gdb_timer
*timer_ptr
= timer_list
.first_timer
;
1232 timer_handler_func
*proc
= timer_ptr
->proc
;
1233 gdb_client_data client_data
= timer_ptr
->client_data
;
1235 /* Get rid of the timer from the beginning of the list. */
1236 timer_list
.first_timer
= timer_ptr
->next
;
1238 /* Delete the timer before calling the callback, not after, in
1239 case the callback itself decides to try deleting the timer
1243 /* Call the procedure associated with that timer. */
1244 (proc
) (client_data
);