1 /* Event loop machinery for GDB, the GNU debugger.
2 Copyright (C) 1999-2022 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/>. */
20 #include "gdbsupport/common-defs.h"
21 #include "gdbsupport/event-loop.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 "gdbsupport/gdb_select.h"
37 /* See event-loop.h. */
39 debug_event_loop_kind debug_event_loop
;
41 /* Tell create_file_handler what events we are interested in.
42 This is used by the select version of the event loop. */
44 #define GDB_READABLE (1<<1)
45 #define GDB_WRITABLE (1<<2)
46 #define GDB_EXCEPTION (1<<3)
48 /* Information about each file descriptor we register with the event
53 /* File descriptor. */
56 /* Events we want to monitor: POLLIN, etc. */
59 /* Events that have been seen since the last time. */
62 /* Procedure to call when fd is ready. */
65 /* Argument to pass to proc. */
66 gdb_client_data client_data
;
68 /* User-friendly name of this handler. */
71 /* If set, this file descriptor is used for a user interface. */
74 /* Was an error detected on this fd? */
77 /* Next registered file descriptor. */
78 struct file_handler
*next_file
;
82 /* Do we use poll or select? Some systems have poll, but then it's
83 not useable with all kinds of files. We probe that whenever a new
84 file handler is added. */
85 static bool use_poll
= true;
93 /* Gdb_notifier is just a list of file descriptors gdb is interested in.
94 These are the input file descriptor, and the target file
95 descriptor. We have two flavors of the notifier, one for platforms
96 that have the POLL function, the other for those that don't, and
97 only support SELECT. Each of the elements in the gdb_notifier list is
98 basically a description of what kind of events gdb is interested
103 /* Ptr to head of file handler list. */
104 file_handler
*first_file_handler
;
106 /* Next file handler to handle, for the select variant. To level
107 the fairness across event sources, we serve file handlers in a
108 round-robin-like fashion. The number and order of the polled
109 file handlers may change between invocations, but this is good
111 file_handler
*next_file_handler
;
114 /* Ptr to array of pollfd structures. */
115 struct pollfd
*poll_fds
;
117 /* Next file descriptor to handle, for the poll variant. To level
118 the fairness across event sources, we poll the file descriptors
119 in a round-robin-like fashion. The number and order of the
120 polled file descriptors may change between invocations, but
121 this is good enough. */
122 int next_poll_fds_index
;
124 /* Timeout in milliseconds for calls to poll(). */
128 /* Masks to be used in the next call to select.
129 Bits are set in response to calls to create_file_handler. */
130 fd_set check_masks
[3];
132 /* What file descriptors were found ready by select. */
133 fd_set ready_masks
[3];
135 /* Number of file descriptors to monitor (for poll). */
136 /* Number of valid bits (highest fd value + 1) (for select). */
139 /* Time structure for calls to select(). */
140 struct timeval select_timeout
;
142 /* Flag to tell whether the timeout should be used. */
147 /* Structure associated with a timer. PROC will be executed at the
148 first occasion after WHEN. */
151 std::chrono::steady_clock::time_point when
;
153 struct gdb_timer
*next
;
154 timer_handler_func
*proc
; /* Function to call to do the work. */
155 gdb_client_data client_data
; /* Argument to async_handler_func. */
158 /* List of currently active timers. It is sorted in order of
159 increasing timers. */
162 /* Pointer to first in timer list. */
163 struct gdb_timer
*first_timer
;
165 /* Id of the last timer created. */
170 static void create_file_handler (int fd
, int mask
, handler_func
*proc
,
171 gdb_client_data client_data
,
172 std::string
&&name
, bool is_ui
);
173 static int gdb_wait_for_event (int);
174 static int update_wait_timeout (void);
175 static int poll_timers (void);
177 /* Process one high level event. If nothing is ready at this time,
178 wait for something to happen (via gdb_wait_for_event), then process
179 it. Returns >0 if something was done otherwise returns <0 (this
180 can happen if there are no event sources to wait for). */
183 gdb_do_one_event (void)
185 static int event_source_head
= 0;
186 const int number_of_sources
= 3;
189 /* First let's see if there are any asynchronous signal handlers
190 that are ready. These would be the result of invoking any of the
192 if (invoke_async_signal_handlers ())
195 /* To level the fairness across event sources, we poll them in a
196 round-robin fashion. */
197 for (current
= 0; current
< number_of_sources
; current
++)
201 switch (event_source_head
)
204 /* Are any timers that are ready? */
205 res
= poll_timers ();
208 /* Are there events already waiting to be collected on the
209 monitored file descriptors? */
210 res
= gdb_wait_for_event (0);
213 /* Are there any asynchronous event handlers ready? */
214 res
= check_async_event_handlers ();
217 internal_error (__FILE__
, __LINE__
,
218 "unexpected event_source_head %d",
223 if (event_source_head
== number_of_sources
)
224 event_source_head
= 0;
230 /* Block waiting for a new event. If gdb_wait_for_event returns -1,
231 we should get out because this means that there are no event
232 sources left. This will make the event loop stop, and the
235 if (gdb_wait_for_event (1) < 0)
238 /* If gdb_wait_for_event has returned 1, it means that one event has
239 been handled. We break out of the loop. */
243 /* See event-loop.h */
246 add_file_handler (int fd
, handler_func
*proc
, gdb_client_data client_data
,
247 std::string
&&name
, bool is_ui
)
254 /* Check to see if poll () is usable. If not, we'll switch to
255 use select. This can happen on systems like
256 m68k-motorola-sys, `poll' cannot be used to wait for `stdin'.
257 On m68k-motorola-sysv, tty's are not stream-based and not
261 if (poll (&fds
, 1, 0) == 1 && (fds
.revents
& POLLNVAL
))
266 create_file_handler (fd
, POLLIN
, proc
, client_data
, std::move (name
),
270 #endif /* HAVE_POLL */
271 create_file_handler (fd
, GDB_READABLE
| GDB_EXCEPTION
,
272 proc
, client_data
, std::move (name
), is_ui
);
275 /* Helper for add_file_handler.
277 For the poll case, MASK is a combination (OR) of POLLIN,
278 POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND:
279 these are the events we are interested in. If any of them occurs,
280 proc should be called.
282 For the select case, MASK is a combination of READABLE, WRITABLE,
283 EXCEPTION. PROC is the procedure that will be called when an event
284 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
287 create_file_handler (int fd
, int mask
, handler_func
* proc
,
288 gdb_client_data client_data
, std::string
&&name
,
291 file_handler
*file_ptr
;
293 /* Do we already have a file handler for this file? (We may be
294 changing its associated procedure). */
295 for (file_ptr
= gdb_notifier
.first_file_handler
; file_ptr
!= NULL
;
296 file_ptr
= file_ptr
->next_file
)
298 if (file_ptr
->fd
== fd
)
302 /* It is a new file descriptor. Add it to the list. Otherwise, just
303 change the data associated with it. */
304 if (file_ptr
== NULL
)
306 file_ptr
= new file_handler
;
308 file_ptr
->ready_mask
= 0;
309 file_ptr
->next_file
= gdb_notifier
.first_file_handler
;
310 gdb_notifier
.first_file_handler
= file_ptr
;
315 gdb_notifier
.num_fds
++;
316 if (gdb_notifier
.poll_fds
)
317 gdb_notifier
.poll_fds
=
318 (struct pollfd
*) xrealloc (gdb_notifier
.poll_fds
,
319 (gdb_notifier
.num_fds
320 * sizeof (struct pollfd
)));
322 gdb_notifier
.poll_fds
=
323 XNEW (struct pollfd
);
324 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->fd
= fd
;
325 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->events
= mask
;
326 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->revents
= 0;
329 #endif /* HAVE_POLL */
331 if (mask
& GDB_READABLE
)
332 FD_SET (fd
, &gdb_notifier
.check_masks
[0]);
334 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
336 if (mask
& GDB_WRITABLE
)
337 FD_SET (fd
, &gdb_notifier
.check_masks
[1]);
339 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
341 if (mask
& GDB_EXCEPTION
)
342 FD_SET (fd
, &gdb_notifier
.check_masks
[2]);
344 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
346 if (gdb_notifier
.num_fds
<= fd
)
347 gdb_notifier
.num_fds
= fd
+ 1;
351 file_ptr
->proc
= proc
;
352 file_ptr
->client_data
= client_data
;
353 file_ptr
->mask
= mask
;
354 file_ptr
->name
= std::move (name
);
355 file_ptr
->is_ui
= is_ui
;
358 /* Return the next file handler to handle, and advance to the next
359 file handler, wrapping around if the end of the list is
362 static file_handler
*
363 get_next_file_handler_to_handle_and_advance (void)
365 file_handler
*curr_next
;
367 /* The first time around, this is still NULL. */
368 if (gdb_notifier
.next_file_handler
== NULL
)
369 gdb_notifier
.next_file_handler
= gdb_notifier
.first_file_handler
;
371 curr_next
= gdb_notifier
.next_file_handler
;
372 gdb_assert (curr_next
!= NULL
);
375 gdb_notifier
.next_file_handler
= curr_next
->next_file
;
376 /* Wrap around, if necessary. */
377 if (gdb_notifier
.next_file_handler
== NULL
)
378 gdb_notifier
.next_file_handler
= gdb_notifier
.first_file_handler
;
383 /* Remove the file descriptor FD from the list of monitored fd's:
384 i.e. we don't care anymore about events on the FD. */
386 delete_file_handler (int fd
)
388 file_handler
*file_ptr
, *prev_ptr
= NULL
;
391 /* Find the entry for the given file. */
393 for (file_ptr
= gdb_notifier
.first_file_handler
; file_ptr
!= NULL
;
394 file_ptr
= file_ptr
->next_file
)
396 if (file_ptr
->fd
== fd
)
400 if (file_ptr
== NULL
)
407 struct pollfd
*new_poll_fds
;
409 /* Create a new poll_fds array by copying every fd's information
410 but the one we want to get rid of. */
412 new_poll_fds
= (struct pollfd
*)
413 xmalloc ((gdb_notifier
.num_fds
- 1) * sizeof (struct pollfd
));
415 for (i
= 0, j
= 0; i
< gdb_notifier
.num_fds
; i
++)
417 if ((gdb_notifier
.poll_fds
+ i
)->fd
!= fd
)
419 (new_poll_fds
+ j
)->fd
= (gdb_notifier
.poll_fds
+ i
)->fd
;
420 (new_poll_fds
+ j
)->events
= (gdb_notifier
.poll_fds
+ i
)->events
;
421 (new_poll_fds
+ j
)->revents
422 = (gdb_notifier
.poll_fds
+ i
)->revents
;
426 xfree (gdb_notifier
.poll_fds
);
427 gdb_notifier
.poll_fds
= new_poll_fds
;
428 gdb_notifier
.num_fds
--;
431 #endif /* HAVE_POLL */
433 if (file_ptr
->mask
& GDB_READABLE
)
434 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
435 if (file_ptr
->mask
& GDB_WRITABLE
)
436 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
437 if (file_ptr
->mask
& GDB_EXCEPTION
)
438 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
440 /* Find current max fd. */
442 if ((fd
+ 1) == gdb_notifier
.num_fds
)
444 gdb_notifier
.num_fds
--;
445 for (i
= gdb_notifier
.num_fds
; i
; i
--)
447 if (FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[0])
448 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[1])
449 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[2]))
452 gdb_notifier
.num_fds
= i
;
456 /* Deactivate the file descriptor, by clearing its mask,
457 so that it will not fire again. */
461 /* If this file handler was going to be the next one to be handled,
462 advance to the next's next, if any. */
463 if (gdb_notifier
.next_file_handler
== file_ptr
)
465 if (file_ptr
->next_file
== NULL
466 && file_ptr
== gdb_notifier
.first_file_handler
)
467 gdb_notifier
.next_file_handler
= NULL
;
469 get_next_file_handler_to_handle_and_advance ();
472 /* Get rid of the file handler in the file handler list. */
473 if (file_ptr
== gdb_notifier
.first_file_handler
)
474 gdb_notifier
.first_file_handler
= file_ptr
->next_file
;
477 for (prev_ptr
= gdb_notifier
.first_file_handler
;
478 prev_ptr
->next_file
!= file_ptr
;
479 prev_ptr
= prev_ptr
->next_file
)
481 prev_ptr
->next_file
= file_ptr
->next_file
;
487 /* Handle the given event by calling the procedure associated to the
488 corresponding file handler. */
491 handle_file_event (file_handler
*file_ptr
, int ready_mask
)
495 /* See if the desired events (mask) match the received events
503 /* With poll, the ready_mask could have any of three events set
504 to 1: POLLHUP, POLLERR, POLLNVAL. These events cannot be
505 used in the requested event mask (events), but they can be
506 returned in the return mask (revents). We need to check for
507 those event too, and add them to the mask which will be
508 passed to the handler. */
510 /* POLLHUP means EOF, but can be combined with POLLIN to
511 signal more data to read. */
512 error_mask
= POLLHUP
| POLLERR
| POLLNVAL
;
513 mask
= ready_mask
& (file_ptr
->mask
| error_mask
);
515 if ((mask
& (POLLERR
| POLLNVAL
)) != 0)
517 /* Work in progress. We may need to tell somebody
518 what kind of error we had. */
520 warning (_("Error detected on fd %d"), file_ptr
->fd
);
522 warning (_("Invalid or non-`poll'able fd %d"),
530 #endif /* HAVE_POLL */
532 if (ready_mask
& GDB_EXCEPTION
)
534 warning (_("Exception condition detected on fd %d"),
540 mask
= ready_mask
& file_ptr
->mask
;
543 /* If there was a match, then call the handler. */
546 event_loop_ui_debug_printf (file_ptr
->is_ui
,
547 "invoking fd file handler `%s`",
548 file_ptr
->name
.c_str ());
549 file_ptr
->proc (file_ptr
->error
, file_ptr
->client_data
);
553 /* Wait for new events on the monitored file descriptors. Run the
554 event handler if the first descriptor that is detected by the poll.
555 If BLOCK and if there are no events, this function will block in
556 the call to poll. Return 1 if an event was handled. Return -1 if
557 there are no file descriptors to monitor. Return 1 if an event was
558 handled, otherwise returns 0. */
561 gdb_wait_for_event (int block
)
563 file_handler
*file_ptr
;
566 /* Make sure all output is done before getting another event. */
569 if (gdb_notifier
.num_fds
== 0)
573 update_wait_timeout ();
581 timeout
= gdb_notifier
.timeout_valid
? gdb_notifier
.poll_timeout
: -1;
585 num_found
= poll (gdb_notifier
.poll_fds
,
586 (unsigned long) gdb_notifier
.num_fds
, timeout
);
588 /* Don't print anything if we get out of poll because of a
590 if (num_found
== -1 && errno
!= EINTR
)
591 perror_with_name (("poll"));
594 #endif /* HAVE_POLL */
596 struct timeval select_timeout
;
597 struct timeval
*timeout_p
;
600 timeout_p
= gdb_notifier
.timeout_valid
601 ? &gdb_notifier
.select_timeout
: NULL
;
604 memset (&select_timeout
, 0, sizeof (select_timeout
));
605 timeout_p
= &select_timeout
;
608 gdb_notifier
.ready_masks
[0] = gdb_notifier
.check_masks
[0];
609 gdb_notifier
.ready_masks
[1] = gdb_notifier
.check_masks
[1];
610 gdb_notifier
.ready_masks
[2] = gdb_notifier
.check_masks
[2];
611 num_found
= gdb_select (gdb_notifier
.num_fds
,
612 &gdb_notifier
.ready_masks
[0],
613 &gdb_notifier
.ready_masks
[1],
614 &gdb_notifier
.ready_masks
[2],
617 /* Clear the masks after an error from select. */
620 FD_ZERO (&gdb_notifier
.ready_masks
[0]);
621 FD_ZERO (&gdb_notifier
.ready_masks
[1]);
622 FD_ZERO (&gdb_notifier
.ready_masks
[2]);
624 /* Dont print anything if we got a signal, let gdb handle
627 perror_with_name (("select"));
631 /* Avoid looking at poll_fds[i]->revents if no event fired. */
635 /* Run event handlers. We always run just one handler and go back
636 to polling, in case a handler changes the notifier list. Since
637 events for sources we haven't consumed yet wake poll/select
638 immediately, no event is lost. */
640 /* To level the fairness across event descriptors, we handle them in
641 a round-robin-like fashion. The number and order of descriptors
642 may change between invocations, but this is good enough. */
651 if (gdb_notifier
.next_poll_fds_index
>= gdb_notifier
.num_fds
)
652 gdb_notifier
.next_poll_fds_index
= 0;
653 i
= gdb_notifier
.next_poll_fds_index
++;
655 gdb_assert (i
< gdb_notifier
.num_fds
);
656 if ((gdb_notifier
.poll_fds
+ i
)->revents
)
660 for (file_ptr
= gdb_notifier
.first_file_handler
;
662 file_ptr
= file_ptr
->next_file
)
664 if (file_ptr
->fd
== (gdb_notifier
.poll_fds
+ i
)->fd
)
667 gdb_assert (file_ptr
!= NULL
);
669 mask
= (gdb_notifier
.poll_fds
+ i
)->revents
;
670 handle_file_event (file_ptr
, mask
);
674 #endif /* HAVE_POLL */
676 /* See comment about even source fairness above. */
681 file_ptr
= get_next_file_handler_to_handle_and_advance ();
683 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[0]))
684 mask
|= GDB_READABLE
;
685 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[1]))
686 mask
|= GDB_WRITABLE
;
687 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[2]))
688 mask
|= GDB_EXCEPTION
;
692 handle_file_event (file_ptr
, mask
);
698 /* Create a timer that will expire in MS milliseconds from now. When
699 the timer is ready, PROC will be executed. At creation, the timer
700 is added to the timers queue. This queue is kept sorted in order
701 of increasing timers. Return a handle to the timer struct. */
704 create_timer (int ms
, timer_handler_func
*proc
,
705 gdb_client_data client_data
)
707 using namespace std::chrono
;
708 struct gdb_timer
*timer_ptr
, *timer_index
, *prev_timer
;
710 steady_clock::time_point time_now
= steady_clock::now ();
712 timer_ptr
= new gdb_timer ();
713 timer_ptr
->when
= time_now
+ milliseconds (ms
);
714 timer_ptr
->proc
= proc
;
715 timer_ptr
->client_data
= client_data
;
716 timer_list
.num_timers
++;
717 timer_ptr
->timer_id
= timer_list
.num_timers
;
719 /* Now add the timer to the timer queue, making sure it is sorted in
720 increasing order of expiration. */
722 for (timer_index
= timer_list
.first_timer
;
724 timer_index
= timer_index
->next
)
726 if (timer_index
->when
> timer_ptr
->when
)
730 if (timer_index
== timer_list
.first_timer
)
732 timer_ptr
->next
= timer_list
.first_timer
;
733 timer_list
.first_timer
= timer_ptr
;
738 for (prev_timer
= timer_list
.first_timer
;
739 prev_timer
->next
!= timer_index
;
740 prev_timer
= prev_timer
->next
)
743 prev_timer
->next
= timer_ptr
;
744 timer_ptr
->next
= timer_index
;
747 gdb_notifier
.timeout_valid
= 0;
748 return timer_ptr
->timer_id
;
751 /* There is a chance that the creator of the timer wants to get rid of
752 it before it expires. */
754 delete_timer (int id
)
756 struct gdb_timer
*timer_ptr
, *prev_timer
= NULL
;
758 /* Find the entry for the given timer. */
760 for (timer_ptr
= timer_list
.first_timer
; timer_ptr
!= NULL
;
761 timer_ptr
= timer_ptr
->next
)
763 if (timer_ptr
->timer_id
== id
)
767 if (timer_ptr
== NULL
)
769 /* Get rid of the timer in the timer list. */
770 if (timer_ptr
== timer_list
.first_timer
)
771 timer_list
.first_timer
= timer_ptr
->next
;
774 for (prev_timer
= timer_list
.first_timer
;
775 prev_timer
->next
!= timer_ptr
;
776 prev_timer
= prev_timer
->next
)
778 prev_timer
->next
= timer_ptr
->next
;
782 gdb_notifier
.timeout_valid
= 0;
785 /* Convert a std::chrono duration to a struct timeval. */
787 template<typename Duration
>
788 static struct timeval
789 duration_cast_timeval (const Duration
&d
)
791 using namespace std::chrono
;
792 seconds sec
= duration_cast
<seconds
> (d
);
793 microseconds msec
= duration_cast
<microseconds
> (d
- sec
);
796 tv
.tv_sec
= sec
.count ();
797 tv
.tv_usec
= msec
.count ();
801 /* Update the timeout for the select() or poll(). Returns true if the
802 timer has already expired, false otherwise. */
805 update_wait_timeout (void)
807 if (timer_list
.first_timer
!= NULL
)
809 using namespace std::chrono
;
810 steady_clock::time_point time_now
= steady_clock::now ();
811 struct timeval timeout
;
813 if (timer_list
.first_timer
->when
< time_now
)
815 /* It expired already. */
821 steady_clock::duration d
= timer_list
.first_timer
->when
- time_now
;
822 timeout
= duration_cast_timeval (d
);
825 /* Update the timeout for select/ poll. */
828 gdb_notifier
.poll_timeout
= timeout
.tv_sec
* 1000;
830 #endif /* HAVE_POLL */
832 gdb_notifier
.select_timeout
.tv_sec
= timeout
.tv_sec
;
833 gdb_notifier
.select_timeout
.tv_usec
= timeout
.tv_usec
;
835 gdb_notifier
.timeout_valid
= 1;
837 if (timer_list
.first_timer
->when
< time_now
)
841 gdb_notifier
.timeout_valid
= 0;
846 /* Check whether a timer in the timers queue is ready. If a timer is
847 ready, call its handler and return. Update the timeout for the
848 select() or poll() as well. Return 1 if an event was handled,
849 otherwise returns 0.*/
854 if (update_wait_timeout ())
856 struct gdb_timer
*timer_ptr
= timer_list
.first_timer
;
857 timer_handler_func
*proc
= timer_ptr
->proc
;
858 gdb_client_data client_data
= timer_ptr
->client_data
;
860 /* Get rid of the timer from the beginning of the list. */
861 timer_list
.first_timer
= timer_ptr
->next
;
863 /* Delete the timer before calling the callback, not after, in
864 case the callback itself decides to try deleting the timer
868 /* Call the procedure associated with that timer. */
869 (proc
) (client_data
);