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/>. */
22 /* Local non-gdb includes. */
23 #include "common/queue.h"
24 #include "event-loop.h"
25 #include "event-top.h"
26 #include "ser-event.h"
29 #if defined (HAVE_POLL_H)
31 #elif defined (HAVE_SYS_POLL_H)
36 #include <sys/types.h>
37 #include "common/gdb_sys_time.h"
38 #include "gdb_select.h"
39 #include "observable.h"
42 /* Tell create_file_handler what events we are interested in.
43 This is used by the select version of the event loop. */
45 #define GDB_READABLE (1<<1)
46 #define GDB_WRITABLE (1<<2)
47 #define GDB_EXCEPTION (1<<3)
49 /* Data point to pass to the event handler. */
50 typedef union event_data
56 typedef struct gdb_event gdb_event
;
57 typedef void (event_handler_func
) (event_data
);
59 /* Event for the GDB event system. Events are queued by calling
60 async_queue_event and serviced later on by gdb_do_one_event. An
61 event can be, for instance, a file descriptor becoming ready to be
62 read. Servicing an event simply means that the procedure PROC will
63 be called. We have 2 queues, one for file handlers that we listen
64 to in the event loop, and one for the file handlers+events that are
65 ready. The procedure PROC associated with each event is dependant
66 of the event source. In the case of monitored file descriptors, it
67 is always the same (handle_file_event). Its duty is to invoke the
68 handler associated with the file descriptor whose state change
69 generated the event, plus doing other cleanups and such. In the
70 case of async signal handlers, it is
71 invoke_async_signal_handler. */
73 typedef struct gdb_event
75 /* Procedure to call to service this event. */
76 event_handler_func
*proc
;
78 /* Data to pass to the event handler. */
82 /* Information about each file descriptor we register with the event
85 typedef struct file_handler
87 int fd
; /* File descriptor. */
88 int mask
; /* Events we want to monitor: POLLIN, etc. */
89 int ready_mask
; /* Events that have been seen since
91 handler_func
*proc
; /* Procedure to call when fd is ready. */
92 gdb_client_data client_data
; /* Argument to pass to proc. */
93 int error
; /* Was an error detected on this fd? */
94 struct file_handler
*next_file
; /* Next registered file descriptor. */
98 /* PROC is a function to be invoked when the READY flag is set. This
99 happens when there has been a signal and the corresponding signal
100 handler has 'triggered' this async_signal_handler for execution.
101 The actual work to be done in response to a signal will be carried
102 out by PROC at a later time, within process_event. This provides a
103 deferred execution of signal handlers.
105 Async_init_signals takes care of setting up such an
106 async_signal_handler for each interesting signal. */
108 typedef struct async_signal_handler
110 int ready
; /* If ready, call this handler
111 from the main event loop, using
112 invoke_async_handler. */
113 struct async_signal_handler
*next_handler
; /* Ptr to next handler. */
114 sig_handler_func
*proc
; /* Function to call to do the work. */
115 gdb_client_data client_data
; /* Argument to async_handler_func. */
117 async_signal_handler
;
119 /* PROC is a function to be invoked when the READY flag is set. This
120 happens when the event has been marked with
121 MARK_ASYNC_EVENT_HANDLER. The actual work to be done in response
122 to an event will be carried out by PROC at a later time, within
123 process_event. This provides a deferred execution of event
125 typedef struct async_event_handler
127 /* If ready, call this handler from the main event loop, using
128 invoke_event_handler. */
131 /* Point to next handler. */
132 struct async_event_handler
*next_handler
;
134 /* Function to call to do the work. */
135 async_event_handler_func
*proc
;
137 /* Argument to PROC. */
138 gdb_client_data client_data
;
142 /* Gdb_notifier is just a list of file descriptors gdb is interested in.
143 These are the input file descriptor, and the target file
144 descriptor. We have two flavors of the notifier, one for platforms
145 that have the POLL function, the other for those that don't, and
146 only support SELECT. Each of the elements in the gdb_notifier list is
147 basically a description of what kind of events gdb is interested
150 /* As of 1999-04-30 only the input file descriptor is registered with the
153 /* Do we use poll or select ? */
158 #endif /* HAVE_POLL */
160 static unsigned char use_poll
= USE_POLL
;
169 /* Ptr to head of file handler list. */
170 file_handler
*first_file_handler
;
172 /* Next file handler to handle, for the select variant. To level
173 the fairness across event sources, we serve file handlers in a
174 round-robin-like fashion. The number and order of the polled
175 file handlers may change between invocations, but this is good
177 file_handler
*next_file_handler
;
180 /* Ptr to array of pollfd structures. */
181 struct pollfd
*poll_fds
;
183 /* Next file descriptor to handle, for the poll variant. To level
184 the fairness across event sources, we poll the file descriptors
185 in a round-robin-like fashion. The number and order of the
186 polled file descriptors may change between invocations, but
187 this is good enough. */
188 int next_poll_fds_index
;
190 /* Timeout in milliseconds for calls to poll(). */
194 /* Masks to be used in the next call to select.
195 Bits are set in response to calls to create_file_handler. */
196 fd_set check_masks
[3];
198 /* What file descriptors were found ready by select. */
199 fd_set ready_masks
[3];
201 /* Number of file descriptors to monitor (for poll). */
202 /* Number of valid bits (highest fd value + 1) (for select). */
205 /* Time structure for calls to select(). */
206 struct timeval select_timeout
;
208 /* Flag to tell whether the timeout should be used. */
213 /* Structure associated with a timer. PROC will be executed at the
214 first occasion after WHEN. */
217 std::chrono::steady_clock::time_point when
;
219 struct gdb_timer
*next
;
220 timer_handler_func
*proc
; /* Function to call to do the work. */
221 gdb_client_data client_data
; /* Argument to async_handler_func. */
224 /* List of currently active timers. It is sorted in order of
225 increasing timers. */
228 /* Pointer to first in timer list. */
229 struct gdb_timer
*first_timer
;
231 /* Id of the last timer created. */
236 /* All the async_signal_handlers gdb is interested in are kept onto
240 /* Pointer to first in handler list. */
241 async_signal_handler
*first_handler
;
243 /* Pointer to last in handler list. */
244 async_signal_handler
*last_handler
;
248 /* All the async_event_handlers gdb is interested in are kept onto
252 /* Pointer to first in handler list. */
253 async_event_handler
*first_handler
;
255 /* Pointer to last in handler list. */
256 async_event_handler
*last_handler
;
258 async_event_handler_list
;
260 static int invoke_async_signal_handlers (void);
261 static void create_file_handler (int fd
, int mask
, handler_func
*proc
,
262 gdb_client_data client_data
);
263 static int check_async_event_handlers (void);
264 static int gdb_wait_for_event (int);
265 static int update_wait_timeout (void);
266 static int poll_timers (void);
269 /* This event is signalled whenever an asynchronous handler needs to
270 defer an action to the event loop. */
271 static struct serial_event
*async_signal_handlers_serial_event
;
273 /* Callback registered with ASYNC_SIGNAL_HANDLERS_SERIAL_EVENT. */
276 async_signals_handler (int error
, gdb_client_data client_data
)
278 /* Do nothing. Handlers are run by invoke_async_signal_handlers
283 initialize_async_signal_handlers (void)
285 async_signal_handlers_serial_event
= make_serial_event ();
287 add_file_handler (serial_event_fd (async_signal_handlers_serial_event
),
288 async_signals_handler
, NULL
);
291 /* Process one high level event. If nothing is ready at this time,
292 wait for something to happen (via gdb_wait_for_event), then process
293 it. Returns >0 if something was done otherwise returns <0 (this
294 can happen if there are no event sources to wait for). */
297 gdb_do_one_event (void)
299 static int event_source_head
= 0;
300 const int number_of_sources
= 3;
303 /* First let's see if there are any asynchronous signal handlers
304 that are ready. These would be the result of invoking any of the
306 if (invoke_async_signal_handlers ())
309 /* To level the fairness across event sources, we poll them in a
310 round-robin fashion. */
311 for (current
= 0; current
< number_of_sources
; current
++)
315 switch (event_source_head
)
318 /* Are any timers that are ready? */
319 res
= poll_timers ();
322 /* Are there events already waiting to be collected on the
323 monitored file descriptors? */
324 res
= gdb_wait_for_event (0);
327 /* Are there any asynchronous event handlers ready? */
328 res
= check_async_event_handlers ();
331 internal_error (__FILE__
, __LINE__
,
332 "unexpected event_source_head %d",
337 if (event_source_head
== number_of_sources
)
338 event_source_head
= 0;
344 /* Block waiting for a new event. If gdb_wait_for_event returns -1,
345 we should get out because this means that there are no event
346 sources left. This will make the event loop stop, and the
349 if (gdb_wait_for_event (1) < 0)
352 /* If gdb_wait_for_event has returned 1, it means that one event has
353 been handled. We break out of the loop. */
357 /* Start up the event loop. This is the entry point to the event loop
358 from the command loop. */
361 start_event_loop (void)
363 /* Loop until there is nothing to do. This is the entry point to
364 the event loop engine. gdb_do_one_event will process one event
365 for each invocation. It blocks waiting for an event and then
373 result
= gdb_do_one_event ();
375 CATCH (ex
, RETURN_MASK_ALL
)
377 exception_print (gdb_stderr
, ex
);
379 /* If any exception escaped to here, we better enable
380 stdin. Otherwise, any command that calls async_disable_stdin,
381 and then throws, will leave stdin inoperable. */
382 async_enable_stdin ();
383 /* If we long-jumped out of do_one_event, we probably didn't
384 get around to resetting the prompt, which leaves readline
385 in a messed-up state. Reset it here. */
386 current_ui
->prompt_state
= PROMPT_NEEDED
;
387 gdb::observers::command_error
.notify ();
388 /* This call looks bizarre, but it is required. If the user
389 entered a command that caused an error,
390 after_char_processing_hook won't be called from
391 rl_callback_read_char_wrapper. Using a cleanup there
392 won't work, since we want this function to be called
393 after a new prompt is printed. */
394 if (after_char_processing_hook
)
395 (*after_char_processing_hook
) ();
396 /* Maybe better to set a flag to be checked somewhere as to
397 whether display the prompt or not. */
405 /* We are done with the event loop. There are no more event sources
406 to listen to. So we exit GDB. */
411 /* Wrapper function for create_file_handler, so that the caller
412 doesn't have to know implementation details about the use of poll
415 add_file_handler (int fd
, handler_func
* proc
, gdb_client_data client_data
)
424 /* Check to see if poll () is usable. If not, we'll switch to
425 use select. This can happen on systems like
426 m68k-motorola-sys, `poll' cannot be used to wait for `stdin'.
427 On m68k-motorola-sysv, tty's are not stream-based and not
431 if (poll (&fds
, 1, 0) == 1 && (fds
.revents
& POLLNVAL
))
434 internal_error (__FILE__
, __LINE__
,
435 _("use_poll without HAVE_POLL"));
436 #endif /* HAVE_POLL */
441 create_file_handler (fd
, POLLIN
, proc
, client_data
);
443 internal_error (__FILE__
, __LINE__
,
444 _("use_poll without HAVE_POLL"));
448 create_file_handler (fd
, GDB_READABLE
| GDB_EXCEPTION
,
452 /* Add a file handler/descriptor to the list of descriptors we are
455 FD is the file descriptor for the file/stream to be listened to.
457 For the poll case, MASK is a combination (OR) of POLLIN,
458 POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND:
459 these are the events we are interested in. If any of them occurs,
460 proc should be called.
462 For the select case, MASK is a combination of READABLE, WRITABLE,
463 EXCEPTION. PROC is the procedure that will be called when an event
464 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
467 create_file_handler (int fd
, int mask
, handler_func
* proc
,
468 gdb_client_data client_data
)
470 file_handler
*file_ptr
;
472 /* Do we already have a file handler for this file? (We may be
473 changing its associated procedure). */
474 for (file_ptr
= gdb_notifier
.first_file_handler
; file_ptr
!= NULL
;
475 file_ptr
= file_ptr
->next_file
)
477 if (file_ptr
->fd
== fd
)
481 /* It is a new file descriptor. Add it to the list. Otherwise, just
482 change the data associated with it. */
483 if (file_ptr
== NULL
)
485 file_ptr
= XNEW (file_handler
);
487 file_ptr
->ready_mask
= 0;
488 file_ptr
->next_file
= gdb_notifier
.first_file_handler
;
489 gdb_notifier
.first_file_handler
= file_ptr
;
494 gdb_notifier
.num_fds
++;
495 if (gdb_notifier
.poll_fds
)
496 gdb_notifier
.poll_fds
=
497 (struct pollfd
*) xrealloc (gdb_notifier
.poll_fds
,
498 (gdb_notifier
.num_fds
499 * sizeof (struct pollfd
)));
501 gdb_notifier
.poll_fds
=
502 XNEW (struct pollfd
);
503 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->fd
= fd
;
504 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->events
= mask
;
505 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->revents
= 0;
507 internal_error (__FILE__
, __LINE__
,
508 _("use_poll without HAVE_POLL"));
509 #endif /* HAVE_POLL */
513 if (mask
& GDB_READABLE
)
514 FD_SET (fd
, &gdb_notifier
.check_masks
[0]);
516 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
518 if (mask
& GDB_WRITABLE
)
519 FD_SET (fd
, &gdb_notifier
.check_masks
[1]);
521 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
523 if (mask
& GDB_EXCEPTION
)
524 FD_SET (fd
, &gdb_notifier
.check_masks
[2]);
526 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
528 if (gdb_notifier
.num_fds
<= fd
)
529 gdb_notifier
.num_fds
= fd
+ 1;
533 file_ptr
->proc
= proc
;
534 file_ptr
->client_data
= client_data
;
535 file_ptr
->mask
= mask
;
538 /* Return the next file handler to handle, and advance to the next
539 file handler, wrapping around if the end of the list is
542 static file_handler
*
543 get_next_file_handler_to_handle_and_advance (void)
545 file_handler
*curr_next
;
547 /* The first time around, this is still NULL. */
548 if (gdb_notifier
.next_file_handler
== NULL
)
549 gdb_notifier
.next_file_handler
= gdb_notifier
.first_file_handler
;
551 curr_next
= gdb_notifier
.next_file_handler
;
552 gdb_assert (curr_next
!= NULL
);
555 gdb_notifier
.next_file_handler
= curr_next
->next_file
;
556 /* Wrap around, if necessary. */
557 if (gdb_notifier
.next_file_handler
== NULL
)
558 gdb_notifier
.next_file_handler
= gdb_notifier
.first_file_handler
;
563 /* Remove the file descriptor FD from the list of monitored fd's:
564 i.e. we don't care anymore about events on the FD. */
566 delete_file_handler (int fd
)
568 file_handler
*file_ptr
, *prev_ptr
= NULL
;
572 struct pollfd
*new_poll_fds
;
575 /* Find the entry for the given file. */
577 for (file_ptr
= gdb_notifier
.first_file_handler
; file_ptr
!= NULL
;
578 file_ptr
= file_ptr
->next_file
)
580 if (file_ptr
->fd
== fd
)
584 if (file_ptr
== NULL
)
590 /* Create a new poll_fds array by copying every fd's information
591 but the one we want to get rid of. */
593 new_poll_fds
= (struct pollfd
*)
594 xmalloc ((gdb_notifier
.num_fds
- 1) * sizeof (struct pollfd
));
596 for (i
= 0, j
= 0; i
< gdb_notifier
.num_fds
; i
++)
598 if ((gdb_notifier
.poll_fds
+ i
)->fd
!= fd
)
600 (new_poll_fds
+ j
)->fd
= (gdb_notifier
.poll_fds
+ i
)->fd
;
601 (new_poll_fds
+ j
)->events
= (gdb_notifier
.poll_fds
+ i
)->events
;
602 (new_poll_fds
+ j
)->revents
603 = (gdb_notifier
.poll_fds
+ i
)->revents
;
607 xfree (gdb_notifier
.poll_fds
);
608 gdb_notifier
.poll_fds
= new_poll_fds
;
609 gdb_notifier
.num_fds
--;
611 internal_error (__FILE__
, __LINE__
,
612 _("use_poll without HAVE_POLL"));
613 #endif /* HAVE_POLL */
617 if (file_ptr
->mask
& GDB_READABLE
)
618 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
619 if (file_ptr
->mask
& GDB_WRITABLE
)
620 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
621 if (file_ptr
->mask
& GDB_EXCEPTION
)
622 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
624 /* Find current max fd. */
626 if ((fd
+ 1) == gdb_notifier
.num_fds
)
628 gdb_notifier
.num_fds
--;
629 for (i
= gdb_notifier
.num_fds
; i
; i
--)
631 if (FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[0])
632 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[1])
633 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[2]))
636 gdb_notifier
.num_fds
= i
;
640 /* Deactivate the file descriptor, by clearing its mask,
641 so that it will not fire again. */
645 /* If this file handler was going to be the next one to be handled,
646 advance to the next's next, if any. */
647 if (gdb_notifier
.next_file_handler
== file_ptr
)
649 if (file_ptr
->next_file
== NULL
650 && file_ptr
== gdb_notifier
.first_file_handler
)
651 gdb_notifier
.next_file_handler
= NULL
;
653 get_next_file_handler_to_handle_and_advance ();
656 /* Get rid of the file handler in the file handler list. */
657 if (file_ptr
== gdb_notifier
.first_file_handler
)
658 gdb_notifier
.first_file_handler
= file_ptr
->next_file
;
661 for (prev_ptr
= gdb_notifier
.first_file_handler
;
662 prev_ptr
->next_file
!= file_ptr
;
663 prev_ptr
= prev_ptr
->next_file
)
665 prev_ptr
->next_file
= file_ptr
->next_file
;
670 /* Handle the given event by calling the procedure associated to the
671 corresponding file handler. */
674 handle_file_event (file_handler
*file_ptr
, int ready_mask
)
683 /* With poll, the ready_mask could have any of three events
684 set to 1: POLLHUP, POLLERR, POLLNVAL. These events
685 cannot be used in the requested event mask (events), but
686 they can be returned in the return mask (revents). We
687 need to check for those event too, and add them to the
688 mask which will be passed to the handler. */
690 /* See if the desired events (mask) match the received
691 events (ready_mask). */
696 /* POLLHUP means EOF, but can be combined with POLLIN to
697 signal more data to read. */
698 error_mask
= POLLHUP
| POLLERR
| POLLNVAL
;
699 mask
= ready_mask
& (file_ptr
->mask
| error_mask
);
701 if ((mask
& (POLLERR
| POLLNVAL
)) != 0)
703 /* Work in progress. We may need to tell somebody
704 what kind of error we had. */
706 printf_unfiltered (_("Error detected on fd %d\n"),
709 printf_unfiltered (_("Invalid or non-`poll'able fd %d\n"),
716 internal_error (__FILE__
, __LINE__
,
717 _("use_poll without HAVE_POLL"));
718 #endif /* HAVE_POLL */
722 if (ready_mask
& GDB_EXCEPTION
)
724 printf_unfiltered (_("Exception condition detected "
725 "on fd %d\n"), file_ptr
->fd
);
730 mask
= ready_mask
& file_ptr
->mask
;
733 /* If there was a match, then call the handler. */
735 (*file_ptr
->proc
) (file_ptr
->error
, file_ptr
->client_data
);
740 /* Wait for new events on the monitored file descriptors. Run the
741 event handler if the first descriptor that is detected by the poll.
742 If BLOCK and if there are no events, this function will block in
743 the call to poll. Return 1 if an event was handled. Return -1 if
744 there are no file descriptors to monitor. Return 1 if an event was
745 handled, otherwise returns 0. */
748 gdb_wait_for_event (int block
)
750 file_handler
*file_ptr
;
753 /* Make sure all output is done before getting another event. */
754 gdb_flush (gdb_stdout
);
755 gdb_flush (gdb_stderr
);
757 if (gdb_notifier
.num_fds
== 0)
761 update_wait_timeout ();
769 timeout
= gdb_notifier
.timeout_valid
? gdb_notifier
.poll_timeout
: -1;
773 num_found
= poll (gdb_notifier
.poll_fds
,
774 (unsigned long) gdb_notifier
.num_fds
, timeout
);
776 /* Don't print anything if we get out of poll because of a
778 if (num_found
== -1 && errno
!= EINTR
)
779 perror_with_name (("poll"));
781 internal_error (__FILE__
, __LINE__
,
782 _("use_poll without HAVE_POLL"));
783 #endif /* HAVE_POLL */
787 struct timeval select_timeout
;
788 struct timeval
*timeout_p
;
791 timeout_p
= gdb_notifier
.timeout_valid
792 ? &gdb_notifier
.select_timeout
: NULL
;
795 memset (&select_timeout
, 0, sizeof (select_timeout
));
796 timeout_p
= &select_timeout
;
799 gdb_notifier
.ready_masks
[0] = gdb_notifier
.check_masks
[0];
800 gdb_notifier
.ready_masks
[1] = gdb_notifier
.check_masks
[1];
801 gdb_notifier
.ready_masks
[2] = gdb_notifier
.check_masks
[2];
802 num_found
= gdb_select (gdb_notifier
.num_fds
,
803 &gdb_notifier
.ready_masks
[0],
804 &gdb_notifier
.ready_masks
[1],
805 &gdb_notifier
.ready_masks
[2],
808 /* Clear the masks after an error from select. */
811 FD_ZERO (&gdb_notifier
.ready_masks
[0]);
812 FD_ZERO (&gdb_notifier
.ready_masks
[1]);
813 FD_ZERO (&gdb_notifier
.ready_masks
[2]);
815 /* Dont print anything if we got a signal, let gdb handle
818 perror_with_name (("select"));
822 /* Avoid looking at poll_fds[i]->revents if no event fired. */
826 /* Run event handlers. We always run just one handler and go back
827 to polling, in case a handler changes the notifier list. Since
828 events for sources we haven't consumed yet wake poll/select
829 immediately, no event is lost. */
831 /* To level the fairness across event descriptors, we handle them in
832 a round-robin-like fashion. The number and order of descriptors
833 may change between invocations, but this is good enough. */
842 if (gdb_notifier
.next_poll_fds_index
>= gdb_notifier
.num_fds
)
843 gdb_notifier
.next_poll_fds_index
= 0;
844 i
= gdb_notifier
.next_poll_fds_index
++;
846 gdb_assert (i
< gdb_notifier
.num_fds
);
847 if ((gdb_notifier
.poll_fds
+ i
)->revents
)
851 for (file_ptr
= gdb_notifier
.first_file_handler
;
853 file_ptr
= file_ptr
->next_file
)
855 if (file_ptr
->fd
== (gdb_notifier
.poll_fds
+ i
)->fd
)
858 gdb_assert (file_ptr
!= NULL
);
860 mask
= (gdb_notifier
.poll_fds
+ i
)->revents
;
861 handle_file_event (file_ptr
, mask
);
864 internal_error (__FILE__
, __LINE__
,
865 _("use_poll without HAVE_POLL"));
866 #endif /* HAVE_POLL */
870 /* See comment about even source fairness above. */
875 file_ptr
= get_next_file_handler_to_handle_and_advance ();
877 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[0]))
878 mask
|= GDB_READABLE
;
879 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[1]))
880 mask
|= GDB_WRITABLE
;
881 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[2]))
882 mask
|= GDB_EXCEPTION
;
886 handle_file_event (file_ptr
, mask
);
893 /* Create an asynchronous handler, allocating memory for it.
894 Return a pointer to the newly created handler.
895 This pointer will be used to invoke the handler by
896 invoke_async_signal_handler.
897 PROC is the function to call with CLIENT_DATA argument
898 whenever the handler is invoked. */
899 async_signal_handler
*
900 create_async_signal_handler (sig_handler_func
* proc
,
901 gdb_client_data client_data
)
903 async_signal_handler
*async_handler_ptr
;
905 async_handler_ptr
= XNEW (async_signal_handler
);
906 async_handler_ptr
->ready
= 0;
907 async_handler_ptr
->next_handler
= NULL
;
908 async_handler_ptr
->proc
= proc
;
909 async_handler_ptr
->client_data
= client_data
;
910 if (sighandler_list
.first_handler
== NULL
)
911 sighandler_list
.first_handler
= async_handler_ptr
;
913 sighandler_list
.last_handler
->next_handler
= async_handler_ptr
;
914 sighandler_list
.last_handler
= async_handler_ptr
;
915 return async_handler_ptr
;
918 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
919 will be used when the handlers are invoked, after we have waited
920 for some event. The caller of this function is the interrupt
921 handler associated with a signal. */
923 mark_async_signal_handler (async_signal_handler
* async_handler_ptr
)
925 async_handler_ptr
->ready
= 1;
926 serial_event_set (async_signal_handlers_serial_event
);
929 /* See event-loop.h. */
932 clear_async_signal_handler (async_signal_handler
*async_handler_ptr
)
934 async_handler_ptr
->ready
= 0;
937 /* See event-loop.h. */
940 async_signal_handler_is_marked (async_signal_handler
*async_handler_ptr
)
942 return async_handler_ptr
->ready
;
945 /* Call all the handlers that are ready. Returns true if any was
949 invoke_async_signal_handlers (void)
951 async_signal_handler
*async_handler_ptr
;
954 /* We're going to handle all pending signals, so no need to wake up
955 the event loop again the next time around. Note this must be
956 cleared _before_ calling the callbacks, to avoid races. */
957 serial_event_clear (async_signal_handlers_serial_event
);
959 /* Invoke all ready handlers. */
963 for (async_handler_ptr
= sighandler_list
.first_handler
;
964 async_handler_ptr
!= NULL
;
965 async_handler_ptr
= async_handler_ptr
->next_handler
)
967 if (async_handler_ptr
->ready
)
970 if (async_handler_ptr
== NULL
)
973 async_handler_ptr
->ready
= 0;
974 /* Async signal handlers have no connection to whichever was the
975 current UI, and thus always run on the main one. */
976 current_ui
= main_ui
;
977 (*async_handler_ptr
->proc
) (async_handler_ptr
->client_data
);
983 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
984 Free the space allocated for it. */
986 delete_async_signal_handler (async_signal_handler
** async_handler_ptr
)
988 async_signal_handler
*prev_ptr
;
990 if (sighandler_list
.first_handler
== (*async_handler_ptr
))
992 sighandler_list
.first_handler
= (*async_handler_ptr
)->next_handler
;
993 if (sighandler_list
.first_handler
== NULL
)
994 sighandler_list
.last_handler
= NULL
;
998 prev_ptr
= sighandler_list
.first_handler
;
999 while (prev_ptr
&& prev_ptr
->next_handler
!= (*async_handler_ptr
))
1000 prev_ptr
= prev_ptr
->next_handler
;
1001 gdb_assert (prev_ptr
);
1002 prev_ptr
->next_handler
= (*async_handler_ptr
)->next_handler
;
1003 if (sighandler_list
.last_handler
== (*async_handler_ptr
))
1004 sighandler_list
.last_handler
= prev_ptr
;
1006 xfree ((*async_handler_ptr
));
1007 (*async_handler_ptr
) = NULL
;
1010 /* Create an asynchronous event handler, allocating memory for it.
1011 Return a pointer to the newly created handler. PROC is the
1012 function to call with CLIENT_DATA argument whenever the handler is
1014 async_event_handler
*
1015 create_async_event_handler (async_event_handler_func
*proc
,
1016 gdb_client_data client_data
)
1018 async_event_handler
*h
;
1020 h
= XNEW (struct async_event_handler
);
1022 h
->next_handler
= NULL
;
1024 h
->client_data
= client_data
;
1025 if (async_event_handler_list
.first_handler
== NULL
)
1026 async_event_handler_list
.first_handler
= h
;
1028 async_event_handler_list
.last_handler
->next_handler
= h
;
1029 async_event_handler_list
.last_handler
= h
;
1033 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
1034 will be used by gdb_do_one_event. The caller will be whoever
1035 created the event source, and wants to signal that the event is
1036 ready to be handled. */
1038 mark_async_event_handler (async_event_handler
*async_handler_ptr
)
1040 async_handler_ptr
->ready
= 1;
1043 /* See event-loop.h. */
1046 clear_async_event_handler (async_event_handler
*async_handler_ptr
)
1048 async_handler_ptr
->ready
= 0;
1051 /* Check if asynchronous event handlers are ready, and call the
1052 handler function for one that is. */
1055 check_async_event_handlers (void)
1057 async_event_handler
*async_handler_ptr
;
1059 for (async_handler_ptr
= async_event_handler_list
.first_handler
;
1060 async_handler_ptr
!= NULL
;
1061 async_handler_ptr
= async_handler_ptr
->next_handler
)
1063 if (async_handler_ptr
->ready
)
1065 async_handler_ptr
->ready
= 0;
1066 (*async_handler_ptr
->proc
) (async_handler_ptr
->client_data
);
1074 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
1075 Free the space allocated for it. */
1077 delete_async_event_handler (async_event_handler
**async_handler_ptr
)
1079 async_event_handler
*prev_ptr
;
1081 if (async_event_handler_list
.first_handler
== *async_handler_ptr
)
1083 async_event_handler_list
.first_handler
1084 = (*async_handler_ptr
)->next_handler
;
1085 if (async_event_handler_list
.first_handler
== NULL
)
1086 async_event_handler_list
.last_handler
= NULL
;
1090 prev_ptr
= async_event_handler_list
.first_handler
;
1091 while (prev_ptr
&& prev_ptr
->next_handler
!= *async_handler_ptr
)
1092 prev_ptr
= prev_ptr
->next_handler
;
1093 gdb_assert (prev_ptr
);
1094 prev_ptr
->next_handler
= (*async_handler_ptr
)->next_handler
;
1095 if (async_event_handler_list
.last_handler
== (*async_handler_ptr
))
1096 async_event_handler_list
.last_handler
= prev_ptr
;
1098 xfree (*async_handler_ptr
);
1099 *async_handler_ptr
= NULL
;
1102 /* Create a timer that will expire in MS milliseconds from now. When
1103 the timer is ready, PROC will be executed. At creation, the timer
1104 is added to the timers queue. This queue is kept sorted in order
1105 of increasing timers. Return a handle to the timer struct. */
1108 create_timer (int ms
, timer_handler_func
*proc
,
1109 gdb_client_data client_data
)
1111 using namespace std::chrono
;
1112 struct gdb_timer
*timer_ptr
, *timer_index
, *prev_timer
;
1114 steady_clock::time_point time_now
= steady_clock::now ();
1116 timer_ptr
= new gdb_timer ();
1117 timer_ptr
->when
= time_now
+ milliseconds (ms
);
1118 timer_ptr
->proc
= proc
;
1119 timer_ptr
->client_data
= client_data
;
1120 timer_list
.num_timers
++;
1121 timer_ptr
->timer_id
= timer_list
.num_timers
;
1123 /* Now add the timer to the timer queue, making sure it is sorted in
1124 increasing order of expiration. */
1126 for (timer_index
= timer_list
.first_timer
;
1127 timer_index
!= NULL
;
1128 timer_index
= timer_index
->next
)
1130 if (timer_index
->when
> timer_ptr
->when
)
1134 if (timer_index
== timer_list
.first_timer
)
1136 timer_ptr
->next
= timer_list
.first_timer
;
1137 timer_list
.first_timer
= timer_ptr
;
1142 for (prev_timer
= timer_list
.first_timer
;
1143 prev_timer
->next
!= timer_index
;
1144 prev_timer
= prev_timer
->next
)
1147 prev_timer
->next
= timer_ptr
;
1148 timer_ptr
->next
= timer_index
;
1151 gdb_notifier
.timeout_valid
= 0;
1152 return timer_ptr
->timer_id
;
1155 /* There is a chance that the creator of the timer wants to get rid of
1156 it before it expires. */
1158 delete_timer (int id
)
1160 struct gdb_timer
*timer_ptr
, *prev_timer
= NULL
;
1162 /* Find the entry for the given timer. */
1164 for (timer_ptr
= timer_list
.first_timer
; timer_ptr
!= NULL
;
1165 timer_ptr
= timer_ptr
->next
)
1167 if (timer_ptr
->timer_id
== id
)
1171 if (timer_ptr
== NULL
)
1173 /* Get rid of the timer in the timer list. */
1174 if (timer_ptr
== timer_list
.first_timer
)
1175 timer_list
.first_timer
= timer_ptr
->next
;
1178 for (prev_timer
= timer_list
.first_timer
;
1179 prev_timer
->next
!= timer_ptr
;
1180 prev_timer
= prev_timer
->next
)
1182 prev_timer
->next
= timer_ptr
->next
;
1186 gdb_notifier
.timeout_valid
= 0;
1189 /* Convert a std::chrono duration to a struct timeval. */
1191 template<typename Duration
>
1192 static struct timeval
1193 duration_cast_timeval (const Duration
&d
)
1195 using namespace std::chrono
;
1196 seconds sec
= duration_cast
<seconds
> (d
);
1197 microseconds msec
= duration_cast
<microseconds
> (d
- sec
);
1200 tv
.tv_sec
= sec
.count ();
1201 tv
.tv_usec
= msec
.count ();
1205 /* Update the timeout for the select() or poll(). Returns true if the
1206 timer has already expired, false otherwise. */
1209 update_wait_timeout (void)
1211 if (timer_list
.first_timer
!= NULL
)
1213 using namespace std::chrono
;
1214 steady_clock::time_point time_now
= steady_clock::now ();
1215 struct timeval timeout
;
1217 if (timer_list
.first_timer
->when
< time_now
)
1219 /* It expired already. */
1221 timeout
.tv_usec
= 0;
1225 steady_clock::duration d
= timer_list
.first_timer
->when
- time_now
;
1226 timeout
= duration_cast_timeval (d
);
1229 /* Update the timeout for select/ poll. */
1233 gdb_notifier
.poll_timeout
= timeout
.tv_sec
* 1000;
1235 internal_error (__FILE__
, __LINE__
,
1236 _("use_poll without HAVE_POLL"));
1237 #endif /* HAVE_POLL */
1241 gdb_notifier
.select_timeout
.tv_sec
= timeout
.tv_sec
;
1242 gdb_notifier
.select_timeout
.tv_usec
= timeout
.tv_usec
;
1244 gdb_notifier
.timeout_valid
= 1;
1246 if (timer_list
.first_timer
->when
< time_now
)
1250 gdb_notifier
.timeout_valid
= 0;
1255 /* Check whether a timer in the timers queue is ready. If a timer is
1256 ready, call its handler and return. Update the timeout for the
1257 select() or poll() as well. Return 1 if an event was handled,
1258 otherwise returns 0.*/
1263 if (update_wait_timeout ())
1265 struct gdb_timer
*timer_ptr
= timer_list
.first_timer
;
1266 timer_handler_func
*proc
= timer_ptr
->proc
;
1267 gdb_client_data client_data
= timer_ptr
->client_data
;
1269 /* Get rid of the timer from the beginning of the list. */
1270 timer_list
.first_timer
= timer_ptr
->next
;
1272 /* Delete the timer before calling the callback, not after, in
1273 case the callback itself decides to try deleting the timer
1277 /* Call the procedure associated with that timer. */
1278 (proc
) (client_data
);