Update copyright year in gdbarch.sh doc/gdb.texinfo and doc/refcard.tex
[binutils-gdb.git] / gdb / event-loop.c
blob32f4ccaae741edd9926bad461c319f3d7f87a042
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/>. */
20 #include "defs.h"
21 #include "event-loop.h"
22 #include "event-top.h"
23 #include "ser-event.h"
25 #ifdef HAVE_POLL
26 #if defined (HAVE_POLL_H)
27 #include <poll.h>
28 #elif defined (HAVE_SYS_POLL_H)
29 #include <sys/poll.h>
30 #endif
31 #endif
33 #include <sys/types.h>
34 #include "gdbsupport/gdb_sys_time.h"
35 #include "gdb_select.h"
36 #include "observable.h"
37 #include "top.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 /* Data point to pass to the event handler. */
47 typedef union event_data
49 void *ptr;
50 int integer;
51 } event_data;
53 typedef struct gdb_event gdb_event;
54 typedef void (event_handler_func) (event_data);
56 /* Event for the GDB event system. Events are queued by calling
57 async_queue_event and serviced later on by gdb_do_one_event. An
58 event can be, for instance, a file descriptor becoming ready to be
59 read. Servicing an event simply means that the procedure PROC will
60 be called. We have 2 queues, one for file handlers that we listen
61 to in the event loop, and one for the file handlers+events that are
62 ready. The procedure PROC associated with each event is dependant
63 of the event source. In the case of monitored file descriptors, it
64 is always the same (handle_file_event). Its duty is to invoke the
65 handler associated with the file descriptor whose state change
66 generated the event, plus doing other cleanups and such. In the
67 case of async signal handlers, it is
68 invoke_async_signal_handler. */
70 typedef struct gdb_event
72 /* Procedure to call to service this event. */
73 event_handler_func *proc;
75 /* Data to pass to the event handler. */
76 event_data data;
77 } *gdb_event_p;
79 /* Information about each file descriptor we register with the event
80 loop. */
82 typedef struct file_handler
84 int fd; /* File descriptor. */
85 int mask; /* Events we want to monitor: POLLIN, etc. */
86 int ready_mask; /* Events that have been seen since
87 the last time. */
88 handler_func *proc; /* Procedure to call when fd is ready. */
89 gdb_client_data client_data; /* Argument to pass to proc. */
90 int error; /* Was an error detected on this fd? */
91 struct file_handler *next_file; /* Next registered file descriptor. */
93 file_handler;
95 /* PROC is a function to be invoked when the READY flag is set. This
96 happens when there has been a signal and the corresponding signal
97 handler has 'triggered' this async_signal_handler for execution.
98 The actual work to be done in response to a signal will be carried
99 out by PROC at a later time, within process_event. This provides a
100 deferred execution of signal handlers.
102 Async_init_signals takes care of setting up such an
103 async_signal_handler for each interesting signal. */
105 typedef struct async_signal_handler
107 int ready; /* If ready, call this handler
108 from the main event loop, using
109 invoke_async_handler. */
110 struct async_signal_handler *next_handler; /* Ptr to next handler. */
111 sig_handler_func *proc; /* Function to call to do the work. */
112 gdb_client_data client_data; /* Argument to async_handler_func. */
114 async_signal_handler;
116 /* PROC is a function to be invoked when the READY flag is set. This
117 happens when the event has been marked with
118 MARK_ASYNC_EVENT_HANDLER. The actual work to be done in response
119 to an event will be carried out by PROC at a later time, within
120 process_event. This provides a deferred execution of event
121 handlers. */
122 typedef struct async_event_handler
124 /* If ready, call this handler from the main event loop, using
125 invoke_event_handler. */
126 int ready;
128 /* Point to next handler. */
129 struct async_event_handler *next_handler;
131 /* Function to call to do the work. */
132 async_event_handler_func *proc;
134 /* Argument to PROC. */
135 gdb_client_data client_data;
137 async_event_handler;
139 /* Gdb_notifier is just a list of file descriptors gdb is interested in.
140 These are the input file descriptor, and the target file
141 descriptor. We have two flavors of the notifier, one for platforms
142 that have the POLL function, the other for those that don't, and
143 only support SELECT. Each of the elements in the gdb_notifier list is
144 basically a description of what kind of events gdb is interested
145 in, for each fd. */
147 /* As of 1999-04-30 only the input file descriptor is registered with the
148 event loop. */
150 /* Do we use poll or select ? */
151 #ifdef HAVE_POLL
152 #define USE_POLL 1
153 #else
154 #define USE_POLL 0
155 #endif /* HAVE_POLL */
157 static unsigned char use_poll = USE_POLL;
159 #ifdef USE_WIN32API
160 #include <windows.h>
161 #include <io.h>
162 #endif
164 static struct
166 /* Ptr to head of file handler list. */
167 file_handler *first_file_handler;
169 /* Next file handler to handle, for the select variant. To level
170 the fairness across event sources, we serve file handlers in a
171 round-robin-like fashion. The number and order of the polled
172 file handlers may change between invocations, but this is good
173 enough. */
174 file_handler *next_file_handler;
176 #ifdef HAVE_POLL
177 /* Ptr to array of pollfd structures. */
178 struct pollfd *poll_fds;
180 /* Next file descriptor to handle, for the poll variant. To level
181 the fairness across event sources, we poll the file descriptors
182 in a round-robin-like fashion. The number and order of the
183 polled file descriptors may change between invocations, but
184 this is good enough. */
185 int next_poll_fds_index;
187 /* Timeout in milliseconds for calls to poll(). */
188 int poll_timeout;
189 #endif
191 /* Masks to be used in the next call to select.
192 Bits are set in response to calls to create_file_handler. */
193 fd_set check_masks[3];
195 /* What file descriptors were found ready by select. */
196 fd_set ready_masks[3];
198 /* Number of file descriptors to monitor (for poll). */
199 /* Number of valid bits (highest fd value + 1) (for select). */
200 int num_fds;
202 /* Time structure for calls to select(). */
203 struct timeval select_timeout;
205 /* Flag to tell whether the timeout should be used. */
206 int timeout_valid;
208 gdb_notifier;
210 /* Structure associated with a timer. PROC will be executed at the
211 first occasion after WHEN. */
212 struct gdb_timer
214 std::chrono::steady_clock::time_point when;
215 int timer_id;
216 struct gdb_timer *next;
217 timer_handler_func *proc; /* Function to call to do the work. */
218 gdb_client_data client_data; /* Argument to async_handler_func. */
221 /* List of currently active timers. It is sorted in order of
222 increasing timers. */
223 static struct
225 /* Pointer to first in timer list. */
226 struct gdb_timer *first_timer;
228 /* Id of the last timer created. */
229 int num_timers;
231 timer_list;
233 /* All the async_signal_handlers gdb is interested in are kept onto
234 this list. */
235 static struct
237 /* Pointer to first in handler list. */
238 async_signal_handler *first_handler;
240 /* Pointer to last in handler list. */
241 async_signal_handler *last_handler;
243 sighandler_list;
245 /* All the async_event_handlers gdb is interested in are kept onto
246 this list. */
247 static struct
249 /* Pointer to first in handler list. */
250 async_event_handler *first_handler;
252 /* Pointer to last in handler list. */
253 async_event_handler *last_handler;
255 async_event_handler_list;
257 static int invoke_async_signal_handlers (void);
258 static void create_file_handler (int fd, int mask, handler_func *proc,
259 gdb_client_data client_data);
260 static int check_async_event_handlers (void);
261 static int gdb_wait_for_event (int);
262 static int update_wait_timeout (void);
263 static int poll_timers (void);
266 /* This event is signalled whenever an asynchronous handler needs to
267 defer an action to the event loop. */
268 static struct serial_event *async_signal_handlers_serial_event;
270 /* Callback registered with ASYNC_SIGNAL_HANDLERS_SERIAL_EVENT. */
272 static void
273 async_signals_handler (int error, gdb_client_data client_data)
275 /* Do nothing. Handlers are run by invoke_async_signal_handlers
276 from instead. */
279 void
280 initialize_async_signal_handlers (void)
282 async_signal_handlers_serial_event = make_serial_event ();
284 add_file_handler (serial_event_fd (async_signal_handlers_serial_event),
285 async_signals_handler, NULL);
288 /* Process one high level event. If nothing is ready at this time,
289 wait for something to happen (via gdb_wait_for_event), then process
290 it. Returns >0 if something was done otherwise returns <0 (this
291 can happen if there are no event sources to wait for). */
294 gdb_do_one_event (void)
296 static int event_source_head = 0;
297 const int number_of_sources = 3;
298 int current = 0;
300 /* First let's see if there are any asynchronous signal handlers
301 that are ready. These would be the result of invoking any of the
302 signal handlers. */
303 if (invoke_async_signal_handlers ())
304 return 1;
306 /* To level the fairness across event sources, we poll them in a
307 round-robin fashion. */
308 for (current = 0; current < number_of_sources; current++)
310 int res;
312 switch (event_source_head)
314 case 0:
315 /* Are any timers that are ready? */
316 res = poll_timers ();
317 break;
318 case 1:
319 /* Are there events already waiting to be collected on the
320 monitored file descriptors? */
321 res = gdb_wait_for_event (0);
322 break;
323 case 2:
324 /* Are there any asynchronous event handlers ready? */
325 res = check_async_event_handlers ();
326 break;
327 default:
328 internal_error (__FILE__, __LINE__,
329 "unexpected event_source_head %d",
330 event_source_head);
333 event_source_head++;
334 if (event_source_head == number_of_sources)
335 event_source_head = 0;
337 if (res > 0)
338 return 1;
341 /* Block waiting for a new event. If gdb_wait_for_event returns -1,
342 we should get out because this means that there are no event
343 sources left. This will make the event loop stop, and the
344 application exit. */
346 if (gdb_wait_for_event (1) < 0)
347 return -1;
349 /* If gdb_wait_for_event has returned 1, it means that one event has
350 been handled. We break out of the loop. */
351 return 1;
354 /* Start up the event loop. This is the entry point to the event loop
355 from the command loop. */
357 void
358 start_event_loop (void)
360 /* Loop until there is nothing to do. This is the entry point to
361 the event loop engine. gdb_do_one_event will process one event
362 for each invocation. It blocks waiting for an event and then
363 processes it. */
364 while (1)
366 int result = 0;
370 result = gdb_do_one_event ();
372 catch (const gdb_exception &ex)
374 exception_print (gdb_stderr, ex);
376 /* If any exception escaped to here, we better enable
377 stdin. Otherwise, any command that calls async_disable_stdin,
378 and then throws, will leave stdin inoperable. */
379 async_enable_stdin ();
380 /* If we long-jumped out of do_one_event, we probably didn't
381 get around to resetting the prompt, which leaves readline
382 in a messed-up state. Reset it here. */
383 current_ui->prompt_state = PROMPT_NEEDED;
384 gdb::observers::command_error.notify ();
385 /* This call looks bizarre, but it is required. If the user
386 entered a command that caused an error,
387 after_char_processing_hook won't be called from
388 rl_callback_read_char_wrapper. Using a cleanup there
389 won't work, since we want this function to be called
390 after a new prompt is printed. */
391 if (after_char_processing_hook)
392 (*after_char_processing_hook) ();
393 /* Maybe better to set a flag to be checked somewhere as to
394 whether display the prompt or not. */
397 if (result < 0)
398 break;
401 /* We are done with the event loop. There are no more event sources
402 to listen to. So we exit GDB. */
403 return;
407 /* Wrapper function for create_file_handler, so that the caller
408 doesn't have to know implementation details about the use of poll
409 vs. select. */
410 void
411 add_file_handler (int fd, handler_func * proc, gdb_client_data client_data)
413 #ifdef HAVE_POLL
414 struct pollfd fds;
415 #endif
417 if (use_poll)
419 #ifdef HAVE_POLL
420 /* Check to see if poll () is usable. If not, we'll switch to
421 use select. This can happen on systems like
422 m68k-motorola-sys, `poll' cannot be used to wait for `stdin'.
423 On m68k-motorola-sysv, tty's are not stream-based and not
424 `poll'able. */
425 fds.fd = fd;
426 fds.events = POLLIN;
427 if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL))
428 use_poll = 0;
429 #else
430 internal_error (__FILE__, __LINE__,
431 _("use_poll without HAVE_POLL"));
432 #endif /* HAVE_POLL */
434 if (use_poll)
436 #ifdef HAVE_POLL
437 create_file_handler (fd, POLLIN, proc, client_data);
438 #else
439 internal_error (__FILE__, __LINE__,
440 _("use_poll without HAVE_POLL"));
441 #endif
443 else
444 create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION,
445 proc, client_data);
448 /* Add a file handler/descriptor to the list of descriptors we are
449 interested in.
451 FD is the file descriptor for the file/stream to be listened to.
453 For the poll case, MASK is a combination (OR) of POLLIN,
454 POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND:
455 these are the events we are interested in. If any of them occurs,
456 proc should be called.
458 For the select case, MASK is a combination of READABLE, WRITABLE,
459 EXCEPTION. PROC is the procedure that will be called when an event
460 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
462 static void
463 create_file_handler (int fd, int mask, handler_func * proc,
464 gdb_client_data client_data)
466 file_handler *file_ptr;
468 /* Do we already have a file handler for this file? (We may be
469 changing its associated procedure). */
470 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
471 file_ptr = file_ptr->next_file)
473 if (file_ptr->fd == fd)
474 break;
477 /* It is a new file descriptor. Add it to the list. Otherwise, just
478 change the data associated with it. */
479 if (file_ptr == NULL)
481 file_ptr = XNEW (file_handler);
482 file_ptr->fd = fd;
483 file_ptr->ready_mask = 0;
484 file_ptr->next_file = gdb_notifier.first_file_handler;
485 gdb_notifier.first_file_handler = file_ptr;
487 if (use_poll)
489 #ifdef HAVE_POLL
490 gdb_notifier.num_fds++;
491 if (gdb_notifier.poll_fds)
492 gdb_notifier.poll_fds =
493 (struct pollfd *) xrealloc (gdb_notifier.poll_fds,
494 (gdb_notifier.num_fds
495 * sizeof (struct pollfd)));
496 else
497 gdb_notifier.poll_fds =
498 XNEW (struct pollfd);
499 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd = fd;
500 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events = mask;
501 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents = 0;
502 #else
503 internal_error (__FILE__, __LINE__,
504 _("use_poll without HAVE_POLL"));
505 #endif /* HAVE_POLL */
507 else
509 if (mask & GDB_READABLE)
510 FD_SET (fd, &gdb_notifier.check_masks[0]);
511 else
512 FD_CLR (fd, &gdb_notifier.check_masks[0]);
514 if (mask & GDB_WRITABLE)
515 FD_SET (fd, &gdb_notifier.check_masks[1]);
516 else
517 FD_CLR (fd, &gdb_notifier.check_masks[1]);
519 if (mask & GDB_EXCEPTION)
520 FD_SET (fd, &gdb_notifier.check_masks[2]);
521 else
522 FD_CLR (fd, &gdb_notifier.check_masks[2]);
524 if (gdb_notifier.num_fds <= fd)
525 gdb_notifier.num_fds = fd + 1;
529 file_ptr->proc = proc;
530 file_ptr->client_data = client_data;
531 file_ptr->mask = mask;
534 /* Return the next file handler to handle, and advance to the next
535 file handler, wrapping around if the end of the list is
536 reached. */
538 static file_handler *
539 get_next_file_handler_to_handle_and_advance (void)
541 file_handler *curr_next;
543 /* The first time around, this is still NULL. */
544 if (gdb_notifier.next_file_handler == NULL)
545 gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
547 curr_next = gdb_notifier.next_file_handler;
548 gdb_assert (curr_next != NULL);
550 /* Advance. */
551 gdb_notifier.next_file_handler = curr_next->next_file;
552 /* Wrap around, if necessary. */
553 if (gdb_notifier.next_file_handler == NULL)
554 gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
556 return curr_next;
559 /* Remove the file descriptor FD from the list of monitored fd's:
560 i.e. we don't care anymore about events on the FD. */
561 void
562 delete_file_handler (int fd)
564 file_handler *file_ptr, *prev_ptr = NULL;
565 int i;
566 #ifdef HAVE_POLL
567 int j;
568 struct pollfd *new_poll_fds;
569 #endif
571 /* Find the entry for the given file. */
573 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
574 file_ptr = file_ptr->next_file)
576 if (file_ptr->fd == fd)
577 break;
580 if (file_ptr == NULL)
581 return;
583 if (use_poll)
585 #ifdef HAVE_POLL
586 /* Create a new poll_fds array by copying every fd's information
587 but the one we want to get rid of. */
589 new_poll_fds = (struct pollfd *)
590 xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd));
592 for (i = 0, j = 0; i < gdb_notifier.num_fds; i++)
594 if ((gdb_notifier.poll_fds + i)->fd != fd)
596 (new_poll_fds + j)->fd = (gdb_notifier.poll_fds + i)->fd;
597 (new_poll_fds + j)->events = (gdb_notifier.poll_fds + i)->events;
598 (new_poll_fds + j)->revents
599 = (gdb_notifier.poll_fds + i)->revents;
600 j++;
603 xfree (gdb_notifier.poll_fds);
604 gdb_notifier.poll_fds = new_poll_fds;
605 gdb_notifier.num_fds--;
606 #else
607 internal_error (__FILE__, __LINE__,
608 _("use_poll without HAVE_POLL"));
609 #endif /* HAVE_POLL */
611 else
613 if (file_ptr->mask & GDB_READABLE)
614 FD_CLR (fd, &gdb_notifier.check_masks[0]);
615 if (file_ptr->mask & GDB_WRITABLE)
616 FD_CLR (fd, &gdb_notifier.check_masks[1]);
617 if (file_ptr->mask & GDB_EXCEPTION)
618 FD_CLR (fd, &gdb_notifier.check_masks[2]);
620 /* Find current max fd. */
622 if ((fd + 1) == gdb_notifier.num_fds)
624 gdb_notifier.num_fds--;
625 for (i = gdb_notifier.num_fds; i; i--)
627 if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0])
628 || FD_ISSET (i - 1, &gdb_notifier.check_masks[1])
629 || FD_ISSET (i - 1, &gdb_notifier.check_masks[2]))
630 break;
632 gdb_notifier.num_fds = i;
636 /* Deactivate the file descriptor, by clearing its mask,
637 so that it will not fire again. */
639 file_ptr->mask = 0;
641 /* If this file handler was going to be the next one to be handled,
642 advance to the next's next, if any. */
643 if (gdb_notifier.next_file_handler == file_ptr)
645 if (file_ptr->next_file == NULL
646 && file_ptr == gdb_notifier.first_file_handler)
647 gdb_notifier.next_file_handler = NULL;
648 else
649 get_next_file_handler_to_handle_and_advance ();
652 /* Get rid of the file handler in the file handler list. */
653 if (file_ptr == gdb_notifier.first_file_handler)
654 gdb_notifier.first_file_handler = file_ptr->next_file;
655 else
657 for (prev_ptr = gdb_notifier.first_file_handler;
658 prev_ptr->next_file != file_ptr;
659 prev_ptr = prev_ptr->next_file)
661 prev_ptr->next_file = file_ptr->next_file;
663 xfree (file_ptr);
666 /* Handle the given event by calling the procedure associated to the
667 corresponding file handler. */
669 static void
670 handle_file_event (file_handler *file_ptr, int ready_mask)
672 int mask;
673 #ifdef HAVE_POLL
674 int error_mask;
675 #endif
679 /* With poll, the ready_mask could have any of three events
680 set to 1: POLLHUP, POLLERR, POLLNVAL. These events
681 cannot be used in the requested event mask (events), but
682 they can be returned in the return mask (revents). We
683 need to check for those event too, and add them to the
684 mask which will be passed to the handler. */
686 /* See if the desired events (mask) match the received
687 events (ready_mask). */
689 if (use_poll)
691 #ifdef HAVE_POLL
692 /* POLLHUP means EOF, but can be combined with POLLIN to
693 signal more data to read. */
694 error_mask = POLLHUP | POLLERR | POLLNVAL;
695 mask = ready_mask & (file_ptr->mask | error_mask);
697 if ((mask & (POLLERR | POLLNVAL)) != 0)
699 /* Work in progress. We may need to tell somebody
700 what kind of error we had. */
701 if (mask & POLLERR)
702 printf_unfiltered (_("Error detected on fd %d\n"),
703 file_ptr->fd);
704 if (mask & POLLNVAL)
705 printf_unfiltered (_("Invalid or non-`poll'able fd %d\n"),
706 file_ptr->fd);
707 file_ptr->error = 1;
709 else
710 file_ptr->error = 0;
711 #else
712 internal_error (__FILE__, __LINE__,
713 _("use_poll without HAVE_POLL"));
714 #endif /* HAVE_POLL */
716 else
718 if (ready_mask & GDB_EXCEPTION)
720 printf_unfiltered (_("Exception condition detected "
721 "on fd %d\n"), file_ptr->fd);
722 file_ptr->error = 1;
724 else
725 file_ptr->error = 0;
726 mask = ready_mask & file_ptr->mask;
729 /* If there was a match, then call the handler. */
730 if (mask != 0)
731 (*file_ptr->proc) (file_ptr->error, file_ptr->client_data);
736 /* Wait for new events on the monitored file descriptors. Run the
737 event handler if the first descriptor that is detected by the poll.
738 If BLOCK and if there are no events, this function will block in
739 the call to poll. Return 1 if an event was handled. Return -1 if
740 there are no file descriptors to monitor. Return 1 if an event was
741 handled, otherwise returns 0. */
743 static int
744 gdb_wait_for_event (int block)
746 file_handler *file_ptr;
747 int num_found = 0;
749 /* Make sure all output is done before getting another event. */
750 gdb_flush (gdb_stdout);
751 gdb_flush (gdb_stderr);
753 if (gdb_notifier.num_fds == 0)
754 return -1;
756 if (block)
757 update_wait_timeout ();
759 if (use_poll)
761 #ifdef HAVE_POLL
762 int timeout;
764 if (block)
765 timeout = gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1;
766 else
767 timeout = 0;
769 num_found = poll (gdb_notifier.poll_fds,
770 (unsigned long) gdb_notifier.num_fds, timeout);
772 /* Don't print anything if we get out of poll because of a
773 signal. */
774 if (num_found == -1 && errno != EINTR)
775 perror_with_name (("poll"));
776 #else
777 internal_error (__FILE__, __LINE__,
778 _("use_poll without HAVE_POLL"));
779 #endif /* HAVE_POLL */
781 else
783 struct timeval select_timeout;
784 struct timeval *timeout_p;
786 if (block)
787 timeout_p = gdb_notifier.timeout_valid
788 ? &gdb_notifier.select_timeout : NULL;
789 else
791 memset (&select_timeout, 0, sizeof (select_timeout));
792 timeout_p = &select_timeout;
795 gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0];
796 gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1];
797 gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2];
798 num_found = gdb_select (gdb_notifier.num_fds,
799 &gdb_notifier.ready_masks[0],
800 &gdb_notifier.ready_masks[1],
801 &gdb_notifier.ready_masks[2],
802 timeout_p);
804 /* Clear the masks after an error from select. */
805 if (num_found == -1)
807 FD_ZERO (&gdb_notifier.ready_masks[0]);
808 FD_ZERO (&gdb_notifier.ready_masks[1]);
809 FD_ZERO (&gdb_notifier.ready_masks[2]);
811 /* Dont print anything if we got a signal, let gdb handle
812 it. */
813 if (errno != EINTR)
814 perror_with_name (("select"));
818 /* Avoid looking at poll_fds[i]->revents if no event fired. */
819 if (num_found <= 0)
820 return 0;
822 /* Run event handlers. We always run just one handler and go back
823 to polling, in case a handler changes the notifier list. Since
824 events for sources we haven't consumed yet wake poll/select
825 immediately, no event is lost. */
827 /* To level the fairness across event descriptors, we handle them in
828 a round-robin-like fashion. The number and order of descriptors
829 may change between invocations, but this is good enough. */
830 if (use_poll)
832 #ifdef HAVE_POLL
833 int i;
834 int mask;
836 while (1)
838 if (gdb_notifier.next_poll_fds_index >= gdb_notifier.num_fds)
839 gdb_notifier.next_poll_fds_index = 0;
840 i = gdb_notifier.next_poll_fds_index++;
842 gdb_assert (i < gdb_notifier.num_fds);
843 if ((gdb_notifier.poll_fds + i)->revents)
844 break;
847 for (file_ptr = gdb_notifier.first_file_handler;
848 file_ptr != NULL;
849 file_ptr = file_ptr->next_file)
851 if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd)
852 break;
854 gdb_assert (file_ptr != NULL);
856 mask = (gdb_notifier.poll_fds + i)->revents;
857 handle_file_event (file_ptr, mask);
858 return 1;
859 #else
860 internal_error (__FILE__, __LINE__,
861 _("use_poll without HAVE_POLL"));
862 #endif /* HAVE_POLL */
864 else
866 /* See comment about even source fairness above. */
867 int mask = 0;
871 file_ptr = get_next_file_handler_to_handle_and_advance ();
873 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0]))
874 mask |= GDB_READABLE;
875 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1]))
876 mask |= GDB_WRITABLE;
877 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2]))
878 mask |= GDB_EXCEPTION;
880 while (mask == 0);
882 handle_file_event (file_ptr, mask);
883 return 1;
885 return 0;
889 /* Create an asynchronous handler, allocating memory for it.
890 Return a pointer to the newly created handler.
891 This pointer will be used to invoke the handler by
892 invoke_async_signal_handler.
893 PROC is the function to call with CLIENT_DATA argument
894 whenever the handler is invoked. */
895 async_signal_handler *
896 create_async_signal_handler (sig_handler_func * proc,
897 gdb_client_data client_data)
899 async_signal_handler *async_handler_ptr;
901 async_handler_ptr = XNEW (async_signal_handler);
902 async_handler_ptr->ready = 0;
903 async_handler_ptr->next_handler = NULL;
904 async_handler_ptr->proc = proc;
905 async_handler_ptr->client_data = client_data;
906 if (sighandler_list.first_handler == NULL)
907 sighandler_list.first_handler = async_handler_ptr;
908 else
909 sighandler_list.last_handler->next_handler = async_handler_ptr;
910 sighandler_list.last_handler = async_handler_ptr;
911 return async_handler_ptr;
914 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
915 will be used when the handlers are invoked, after we have waited
916 for some event. The caller of this function is the interrupt
917 handler associated with a signal. */
918 void
919 mark_async_signal_handler (async_signal_handler * async_handler_ptr)
921 async_handler_ptr->ready = 1;
922 serial_event_set (async_signal_handlers_serial_event);
925 /* See event-loop.h. */
927 void
928 clear_async_signal_handler (async_signal_handler *async_handler_ptr)
930 async_handler_ptr->ready = 0;
933 /* See event-loop.h. */
936 async_signal_handler_is_marked (async_signal_handler *async_handler_ptr)
938 return async_handler_ptr->ready;
941 /* Call all the handlers that are ready. Returns true if any was
942 indeed ready. */
944 static int
945 invoke_async_signal_handlers (void)
947 async_signal_handler *async_handler_ptr;
948 int any_ready = 0;
950 /* We're going to handle all pending signals, so no need to wake up
951 the event loop again the next time around. Note this must be
952 cleared _before_ calling the callbacks, to avoid races. */
953 serial_event_clear (async_signal_handlers_serial_event);
955 /* Invoke all ready handlers. */
957 while (1)
959 for (async_handler_ptr = sighandler_list.first_handler;
960 async_handler_ptr != NULL;
961 async_handler_ptr = async_handler_ptr->next_handler)
963 if (async_handler_ptr->ready)
964 break;
966 if (async_handler_ptr == NULL)
967 break;
968 any_ready = 1;
969 async_handler_ptr->ready = 0;
970 /* Async signal handlers have no connection to whichever was the
971 current UI, and thus always run on the main one. */
972 current_ui = main_ui;
973 (*async_handler_ptr->proc) (async_handler_ptr->client_data);
976 return any_ready;
979 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
980 Free the space allocated for it. */
981 void
982 delete_async_signal_handler (async_signal_handler ** async_handler_ptr)
984 async_signal_handler *prev_ptr;
986 if (sighandler_list.first_handler == (*async_handler_ptr))
988 sighandler_list.first_handler = (*async_handler_ptr)->next_handler;
989 if (sighandler_list.first_handler == NULL)
990 sighandler_list.last_handler = NULL;
992 else
994 prev_ptr = sighandler_list.first_handler;
995 while (prev_ptr && prev_ptr->next_handler != (*async_handler_ptr))
996 prev_ptr = prev_ptr->next_handler;
997 gdb_assert (prev_ptr);
998 prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
999 if (sighandler_list.last_handler == (*async_handler_ptr))
1000 sighandler_list.last_handler = prev_ptr;
1002 xfree ((*async_handler_ptr));
1003 (*async_handler_ptr) = NULL;
1006 /* Create an asynchronous event handler, allocating memory for it.
1007 Return a pointer to the newly created handler. PROC is the
1008 function to call with CLIENT_DATA argument whenever the handler is
1009 invoked. */
1010 async_event_handler *
1011 create_async_event_handler (async_event_handler_func *proc,
1012 gdb_client_data client_data)
1014 async_event_handler *h;
1016 h = XNEW (struct async_event_handler);
1017 h->ready = 0;
1018 h->next_handler = NULL;
1019 h->proc = proc;
1020 h->client_data = client_data;
1021 if (async_event_handler_list.first_handler == NULL)
1022 async_event_handler_list.first_handler = h;
1023 else
1024 async_event_handler_list.last_handler->next_handler = h;
1025 async_event_handler_list.last_handler = h;
1026 return h;
1029 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
1030 will be used by gdb_do_one_event. The caller will be whoever
1031 created the event source, and wants to signal that the event is
1032 ready to be handled. */
1033 void
1034 mark_async_event_handler (async_event_handler *async_handler_ptr)
1036 async_handler_ptr->ready = 1;
1039 /* See event-loop.h. */
1041 void
1042 clear_async_event_handler (async_event_handler *async_handler_ptr)
1044 async_handler_ptr->ready = 0;
1047 /* Check if asynchronous event handlers are ready, and call the
1048 handler function for one that is. */
1050 static int
1051 check_async_event_handlers (void)
1053 async_event_handler *async_handler_ptr;
1055 for (async_handler_ptr = async_event_handler_list.first_handler;
1056 async_handler_ptr != NULL;
1057 async_handler_ptr = async_handler_ptr->next_handler)
1059 if (async_handler_ptr->ready)
1061 async_handler_ptr->ready = 0;
1062 (*async_handler_ptr->proc) (async_handler_ptr->client_data);
1063 return 1;
1067 return 0;
1070 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
1071 Free the space allocated for it. */
1072 void
1073 delete_async_event_handler (async_event_handler **async_handler_ptr)
1075 async_event_handler *prev_ptr;
1077 if (async_event_handler_list.first_handler == *async_handler_ptr)
1079 async_event_handler_list.first_handler
1080 = (*async_handler_ptr)->next_handler;
1081 if (async_event_handler_list.first_handler == NULL)
1082 async_event_handler_list.last_handler = NULL;
1084 else
1086 prev_ptr = async_event_handler_list.first_handler;
1087 while (prev_ptr && prev_ptr->next_handler != *async_handler_ptr)
1088 prev_ptr = prev_ptr->next_handler;
1089 gdb_assert (prev_ptr);
1090 prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
1091 if (async_event_handler_list.last_handler == (*async_handler_ptr))
1092 async_event_handler_list.last_handler = prev_ptr;
1094 xfree (*async_handler_ptr);
1095 *async_handler_ptr = NULL;
1098 /* Create a timer that will expire in MS milliseconds from now. When
1099 the timer is ready, PROC will be executed. At creation, the timer
1100 is added to the timers queue. This queue is kept sorted in order
1101 of increasing timers. Return a handle to the timer struct. */
1104 create_timer (int ms, timer_handler_func *proc,
1105 gdb_client_data client_data)
1107 using namespace std::chrono;
1108 struct gdb_timer *timer_ptr, *timer_index, *prev_timer;
1110 steady_clock::time_point time_now = steady_clock::now ();
1112 timer_ptr = new gdb_timer ();
1113 timer_ptr->when = time_now + milliseconds (ms);
1114 timer_ptr->proc = proc;
1115 timer_ptr->client_data = client_data;
1116 timer_list.num_timers++;
1117 timer_ptr->timer_id = timer_list.num_timers;
1119 /* Now add the timer to the timer queue, making sure it is sorted in
1120 increasing order of expiration. */
1122 for (timer_index = timer_list.first_timer;
1123 timer_index != NULL;
1124 timer_index = timer_index->next)
1126 if (timer_index->when > timer_ptr->when)
1127 break;
1130 if (timer_index == timer_list.first_timer)
1132 timer_ptr->next = timer_list.first_timer;
1133 timer_list.first_timer = timer_ptr;
1136 else
1138 for (prev_timer = timer_list.first_timer;
1139 prev_timer->next != timer_index;
1140 prev_timer = prev_timer->next)
1143 prev_timer->next = timer_ptr;
1144 timer_ptr->next = timer_index;
1147 gdb_notifier.timeout_valid = 0;
1148 return timer_ptr->timer_id;
1151 /* There is a chance that the creator of the timer wants to get rid of
1152 it before it expires. */
1153 void
1154 delete_timer (int id)
1156 struct gdb_timer *timer_ptr, *prev_timer = NULL;
1158 /* Find the entry for the given timer. */
1160 for (timer_ptr = timer_list.first_timer; timer_ptr != NULL;
1161 timer_ptr = timer_ptr->next)
1163 if (timer_ptr->timer_id == id)
1164 break;
1167 if (timer_ptr == NULL)
1168 return;
1169 /* Get rid of the timer in the timer list. */
1170 if (timer_ptr == timer_list.first_timer)
1171 timer_list.first_timer = timer_ptr->next;
1172 else
1174 for (prev_timer = timer_list.first_timer;
1175 prev_timer->next != timer_ptr;
1176 prev_timer = prev_timer->next)
1178 prev_timer->next = timer_ptr->next;
1180 delete timer_ptr;
1182 gdb_notifier.timeout_valid = 0;
1185 /* Convert a std::chrono duration to a struct timeval. */
1187 template<typename Duration>
1188 static struct timeval
1189 duration_cast_timeval (const Duration &d)
1191 using namespace std::chrono;
1192 seconds sec = duration_cast<seconds> (d);
1193 microseconds msec = duration_cast<microseconds> (d - sec);
1195 struct timeval tv;
1196 tv.tv_sec = sec.count ();
1197 tv.tv_usec = msec.count ();
1198 return tv;
1201 /* Update the timeout for the select() or poll(). Returns true if the
1202 timer has already expired, false otherwise. */
1204 static int
1205 update_wait_timeout (void)
1207 if (timer_list.first_timer != NULL)
1209 using namespace std::chrono;
1210 steady_clock::time_point time_now = steady_clock::now ();
1211 struct timeval timeout;
1213 if (timer_list.first_timer->when < time_now)
1215 /* It expired already. */
1216 timeout.tv_sec = 0;
1217 timeout.tv_usec = 0;
1219 else
1221 steady_clock::duration d = timer_list.first_timer->when - time_now;
1222 timeout = duration_cast_timeval (d);
1225 /* Update the timeout for select/ poll. */
1226 if (use_poll)
1228 #ifdef HAVE_POLL
1229 gdb_notifier.poll_timeout = timeout.tv_sec * 1000;
1230 #else
1231 internal_error (__FILE__, __LINE__,
1232 _("use_poll without HAVE_POLL"));
1233 #endif /* HAVE_POLL */
1235 else
1237 gdb_notifier.select_timeout.tv_sec = timeout.tv_sec;
1238 gdb_notifier.select_timeout.tv_usec = timeout.tv_usec;
1240 gdb_notifier.timeout_valid = 1;
1242 if (timer_list.first_timer->when < time_now)
1243 return 1;
1245 else
1246 gdb_notifier.timeout_valid = 0;
1248 return 0;
1251 /* Check whether a timer in the timers queue is ready. If a timer is
1252 ready, call its handler and return. Update the timeout for the
1253 select() or poll() as well. Return 1 if an event was handled,
1254 otherwise returns 0.*/
1256 static int
1257 poll_timers (void)
1259 if (update_wait_timeout ())
1261 struct gdb_timer *timer_ptr = timer_list.first_timer;
1262 timer_handler_func *proc = timer_ptr->proc;
1263 gdb_client_data client_data = timer_ptr->client_data;
1265 /* Get rid of the timer from the beginning of the list. */
1266 timer_list.first_timer = timer_ptr->next;
1268 /* Delete the timer before calling the callback, not after, in
1269 case the callback itself decides to try deleting the timer
1270 too. */
1271 delete timer_ptr;
1273 /* Call the procedure associated with that timer. */
1274 (proc) (client_data);
1276 return 1;
1279 return 0;