Include <chrono> in event-loop.c
[deliverable/binutils-gdb.git] / gdb / event-loop.c
CommitLineData
b5a0ac70 1/* Event loop machinery for GDB, the GNU debugger.
b811d2c2 2 Copyright (C) 1999-2020 Free Software Foundation, Inc.
b5a0ac70
SS
3 Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
4
5 This file is part of GDB.
6
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
b5a0ac70
SS
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
371d5dec 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
b5a0ac70 19
b5a0ac70 20#include "defs.h"
9e0b60a8 21#include "event-loop.h"
c2c6d25f 22#include "event-top.h"
5cc3ce8b 23#include "ser-event.h"
409a3f64 24
98029d02
TT
25#include <chrono>
26
b5a0ac70 27#ifdef HAVE_POLL
409a3f64 28#if defined (HAVE_POLL_H)
9e0b60a8 29#include <poll.h>
409a3f64
AC
30#elif defined (HAVE_SYS_POLL_H)
31#include <sys/poll.h>
32#endif
44f45770 33#endif
409a3f64 34
9e0b60a8 35#include <sys/types.h>
268a13a5 36#include "gdbsupport/gdb_sys_time.h"
06cc9596 37#include "gdbsupport/gdb_select.h"
7c36c34e 38#include "top.h"
c2c6d25f 39
371d5dec
MS
40/* Tell create_file_handler what events we are interested in.
41 This is used by the select version of the event loop. */
01f69b38
DE
42
43#define GDB_READABLE (1<<1)
44#define GDB_WRITABLE (1<<2)
45#define GDB_EXCEPTION (1<<3)
46
c2c6d25f 47/* Information about each file descriptor we register with the event
371d5dec 48 loop. */
c2c6d25f
JM
49
50typedef struct file_handler
51 {
371d5dec
MS
52 int fd; /* File descriptor. */
53 int mask; /* Events we want to monitor: POLLIN, etc. */
c2c6d25f 54 int ready_mask; /* Events that have been seen since
371d5dec
MS
55 the last time. */
56 handler_func *proc; /* Procedure to call when fd is ready. */
57 gdb_client_data client_data; /* Argument to pass to proc. */
58 int error; /* Was an error detected on this fd? */
59 struct file_handler *next_file; /* Next registered file descriptor. */
c2c6d25f
JM
60 }
61file_handler;
62
371d5dec 63/* PROC is a function to be invoked when the READY flag is set. This
c2c6d25f 64 happens when there has been a signal and the corresponding signal
371d5dec
MS
65 handler has 'triggered' this async_signal_handler for execution.
66 The actual work to be done in response to a signal will be carried
67 out by PROC at a later time, within process_event. This provides a
68 deferred execution of signal handlers.
69
c2c6d25f 70 Async_init_signals takes care of setting up such an
371d5dec
MS
71 async_signal_handler for each interesting signal. */
72
c2c6d25f
JM
73typedef struct async_signal_handler
74 {
371d5dec
MS
75 int ready; /* If ready, call this handler
76 from the main event loop, using
77 invoke_async_handler. */
78 struct async_signal_handler *next_handler; /* Ptr to next handler. */
79 sig_handler_func *proc; /* Function to call to do the work. */
80 gdb_client_data client_data; /* Argument to async_handler_func. */
c2c6d25f
JM
81 }
82async_signal_handler;
83
50d01748
PA
84/* PROC is a function to be invoked when the READY flag is set. This
85 happens when the event has been marked with
86 MARK_ASYNC_EVENT_HANDLER. The actual work to be done in response
87 to an event will be carried out by PROC at a later time, within
88 process_event. This provides a deferred execution of event
89 handlers. */
90typedef struct async_event_handler
91 {
92 /* If ready, call this handler from the main event loop, using
93 invoke_event_handler. */
94 int ready;
95
96 /* Point to next handler. */
97 struct async_event_handler *next_handler;
98
99 /* Function to call to do the work. */
100 async_event_handler_func *proc;
101
102 /* Argument to PROC. */
103 gdb_client_data client_data;
104 }
105async_event_handler;
106
b5a0ac70
SS
107/* Gdb_notifier is just a list of file descriptors gdb is interested in.
108 These are the input file descriptor, and the target file
371d5dec 109 descriptor. We have two flavors of the notifier, one for platforms
b5a0ac70 110 that have the POLL function, the other for those that don't, and
371d5dec 111 only support SELECT. Each of the elements in the gdb_notifier list is
b5a0ac70 112 basically a description of what kind of events gdb is interested
371d5dec 113 in, for each fd. */
b5a0ac70 114
392a587b 115/* As of 1999-04-30 only the input file descriptor is registered with the
371d5dec 116 event loop. */
b5a0ac70 117
44f45770 118/* Do we use poll or select ? */
b5a0ac70 119#ifdef HAVE_POLL
44f45770
EZ
120#define USE_POLL 1
121#else
122#define USE_POLL 0
123#endif /* HAVE_POLL */
124
125static unsigned char use_poll = USE_POLL;
b5a0ac70 126
011825f0
MM
127#ifdef USE_WIN32API
128#include <windows.h>
129#include <io.h>
130#endif
131
b5a0ac70
SS
132static struct
133 {
371d5dec 134 /* Ptr to head of file handler list. */
b5a0ac70
SS
135 file_handler *first_file_handler;
136
4e63d0ac
PA
137 /* Next file handler to handle, for the select variant. To level
138 the fairness across event sources, we serve file handlers in a
139 round-robin-like fashion. The number and order of the polled
140 file handlers may change between invocations, but this is good
141 enough. */
142 file_handler *next_file_handler;
143
44f45770 144#ifdef HAVE_POLL
371d5dec 145 /* Ptr to array of pollfd structures. */
b5a0ac70
SS
146 struct pollfd *poll_fds;
147
4e63d0ac
PA
148 /* Next file descriptor to handle, for the poll variant. To level
149 the fairness across event sources, we poll the file descriptors
150 in a round-robin-like fashion. The number and order of the
151 polled file descriptors may change between invocations, but
152 this is good enough. */
153 int next_poll_fds_index;
154
371d5dec 155 /* Timeout in milliseconds for calls to poll(). */
44f45770
EZ
156 int poll_timeout;
157#endif
b5a0ac70
SS
158
159 /* Masks to be used in the next call to select.
371d5dec 160 Bits are set in response to calls to create_file_handler. */
58a2c44a 161 fd_set check_masks[3];
b5a0ac70 162
371d5dec 163 /* What file descriptors were found ready by select. */
58a2c44a 164 fd_set ready_masks[3];
b5a0ac70 165
371d5dec
MS
166 /* Number of file descriptors to monitor (for poll). */
167 /* Number of valid bits (highest fd value + 1) (for select). */
b5a0ac70
SS
168 int num_fds;
169
371d5dec 170 /* Time structure for calls to select(). */
44f45770 171 struct timeval select_timeout;
c2c6d25f 172
371d5dec 173 /* Flag to tell whether the timeout should be used. */
c2c6d25f 174 int timeout_valid;
6426a772 175 }
b5a0ac70
SS
176gdb_notifier;
177
371d5dec
MS
178/* Structure associated with a timer. PROC will be executed at the
179 first occasion after WHEN. */
c2c6d25f
JM
180struct gdb_timer
181 {
dcb07cfa 182 std::chrono::steady_clock::time_point when;
c2c6d25f
JM
183 int timer_id;
184 struct gdb_timer *next;
371d5dec
MS
185 timer_handler_func *proc; /* Function to call to do the work. */
186 gdb_client_data client_data; /* Argument to async_handler_func. */
ae462839 187 };
c2c6d25f 188
371d5dec
MS
189/* List of currently active timers. It is sorted in order of
190 increasing timers. */
c2c6d25f
JM
191static struct
192 {
371d5dec 193 /* Pointer to first in timer list. */
c2c6d25f
JM
194 struct gdb_timer *first_timer;
195
371d5dec 196 /* Id of the last timer created. */
c2c6d25f
JM
197 int num_timers;
198 }
199timer_list;
200
b5a0ac70 201/* All the async_signal_handlers gdb is interested in are kept onto
371d5dec 202 this list. */
b5a0ac70
SS
203static struct
204 {
371d5dec 205 /* Pointer to first in handler list. */
c5aa993b
JM
206 async_signal_handler *first_handler;
207
371d5dec 208 /* Pointer to last in handler list. */
c5aa993b 209 async_signal_handler *last_handler;
b5a0ac70
SS
210 }
211sighandler_list;
212
50d01748 213/* All the async_event_handlers gdb is interested in are kept onto
371d5dec 214 this list. */
50d01748
PA
215static struct
216 {
371d5dec 217 /* Pointer to first in handler list. */
50d01748
PA
218 async_event_handler *first_handler;
219
371d5dec 220 /* Pointer to last in handler list. */
50d01748
PA
221 async_event_handler *last_handler;
222 }
223async_event_handler_list;
224
225static int invoke_async_signal_handlers (void);
226static void create_file_handler (int fd, int mask, handler_func *proc,
227 gdb_client_data client_data);
70b66289 228static int check_async_event_handlers (void);
50d01748 229static int gdb_wait_for_event (int);
70b66289
PA
230static int update_wait_timeout (void);
231static int poll_timers (void);
b5a0ac70
SS
232\f
233
5cc3ce8b
PA
234/* This event is signalled whenever an asynchronous handler needs to
235 defer an action to the event loop. */
236static struct serial_event *async_signal_handlers_serial_event;
237
238/* Callback registered with ASYNC_SIGNAL_HANDLERS_SERIAL_EVENT. */
239
240static void
241async_signals_handler (int error, gdb_client_data client_data)
242{
243 /* Do nothing. Handlers are run by invoke_async_signal_handlers
244 from instead. */
245}
246
247void
248initialize_async_signal_handlers (void)
249{
250 async_signal_handlers_serial_event = make_serial_event ();
251
252 add_file_handler (serial_event_fd (async_signal_handlers_serial_event),
253 async_signals_handler, NULL);
254}
255
b5a0ac70
SS
256/* Process one high level event. If nothing is ready at this time,
257 wait for something to happen (via gdb_wait_for_event), then process
11cf8741 258 it. Returns >0 if something was done otherwise returns <0 (this
e0dd0826 259 can happen if there are no event sources to wait for). */
11cf8741 260
99656a61 261int
e0dd0826 262gdb_do_one_event (void)
b5a0ac70 263{
50d01748
PA
264 static int event_source_head = 0;
265 const int number_of_sources = 3;
266 int current = 0;
267
70b66289
PA
268 /* First let's see if there are any asynchronous signal handlers
269 that are ready. These would be the result of invoking any of the
270 signal handlers. */
271 if (invoke_async_signal_handlers ())
50d01748
PA
272 return 1;
273
274 /* To level the fairness across event sources, we poll them in a
275 round-robin fashion. */
276 for (current = 0; current < number_of_sources; current++)
11cf8741 277 {
70b66289
PA
278 int res;
279
50d01748
PA
280 switch (event_source_head)
281 {
282 case 0:
70b66289
PA
283 /* Are any timers that are ready? */
284 res = poll_timers ();
50d01748
PA
285 break;
286 case 1:
287 /* Are there events already waiting to be collected on the
288 monitored file descriptors? */
70b66289 289 res = gdb_wait_for_event (0);
50d01748
PA
290 break;
291 case 2:
292 /* Are there any asynchronous event handlers ready? */
70b66289 293 res = check_async_event_handlers ();
50d01748 294 break;
80bd5fab
PA
295 default:
296 internal_error (__FILE__, __LINE__,
297 "unexpected event_source_head %d",
298 event_source_head);
50d01748
PA
299 }
300
301 event_source_head++;
302 if (event_source_head == number_of_sources)
303 event_source_head = 0;
7e5cd2de 304
70b66289
PA
305 if (res > 0)
306 return 1;
307 }
7e5cd2de 308
50d01748
PA
309 /* Block waiting for a new event. If gdb_wait_for_event returns -1,
310 we should get out because this means that there are no event
311 sources left. This will make the event loop stop, and the
312 application exit. */
7e5cd2de 313
50d01748
PA
314 if (gdb_wait_for_event (1) < 0)
315 return -1;
7e5cd2de 316
50d01748
PA
317 /* If gdb_wait_for_event has returned 1, it means that one event has
318 been handled. We break out of the loop. */
11cf8741
JM
319 return 1;
320}
321
b5a0ac70
SS
322\f
323
085dd6e6
JM
324/* Wrapper function for create_file_handler, so that the caller
325 doesn't have to know implementation details about the use of poll
371d5dec 326 vs. select. */
c5aa993b 327void
6426a772 328add_file_handler (int fd, handler_func * proc, gdb_client_data client_data)
085dd6e6
JM
329{
330#ifdef HAVE_POLL
44f45770
EZ
331 struct pollfd fds;
332#endif
333
334 if (use_poll)
335 {
336#ifdef HAVE_POLL
371d5dec
MS
337 /* Check to see if poll () is usable. If not, we'll switch to
338 use select. This can happen on systems like
7e5cd2de
EZ
339 m68k-motorola-sys, `poll' cannot be used to wait for `stdin'.
340 On m68k-motorola-sysv, tty's are not stream-based and not
371d5dec 341 `poll'able. */
7e5cd2de
EZ
342 fds.fd = fd;
343 fds.events = POLLIN;
344 if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL))
345 use_poll = 0;
44f45770 346#else
8e65ff28 347 internal_error (__FILE__, __LINE__,
e2e0b3e5 348 _("use_poll without HAVE_POLL"));
44f45770
EZ
349#endif /* HAVE_POLL */
350 }
351 if (use_poll)
352 {
353#ifdef HAVE_POLL
354 create_file_handler (fd, POLLIN, proc, client_data);
085dd6e6 355#else
8e65ff28 356 internal_error (__FILE__, __LINE__,
e2e0b3e5 357 _("use_poll without HAVE_POLL"));
085dd6e6 358#endif
44f45770
EZ
359 }
360 else
371d5dec
MS
361 create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION,
362 proc, client_data);
085dd6e6
JM
363}
364
b5a0ac70 365/* Add a file handler/descriptor to the list of descriptors we are
371d5dec
MS
366 interested in.
367
368 FD is the file descriptor for the file/stream to be listened to.
369
370 For the poll case, MASK is a combination (OR) of POLLIN,
371 POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND:
372 these are the events we are interested in. If any of them occurs,
373 proc should be called.
374
375 For the select case, MASK is a combination of READABLE, WRITABLE,
376 EXCEPTION. PROC is the procedure that will be called when an event
377 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
378
085dd6e6 379static void
371d5dec
MS
380create_file_handler (int fd, int mask, handler_func * proc,
381 gdb_client_data client_data)
b5a0ac70
SS
382{
383 file_handler *file_ptr;
384
371d5dec
MS
385 /* Do we already have a file handler for this file? (We may be
386 changing its associated procedure). */
b5a0ac70
SS
387 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
388 file_ptr = file_ptr->next_file)
389 {
390 if (file_ptr->fd == fd)
391 break;
392 }
393
371d5dec
MS
394 /* It is a new file descriptor. Add it to the list. Otherwise, just
395 change the data associated with it. */
b5a0ac70
SS
396 if (file_ptr == NULL)
397 {
8d749320 398 file_ptr = XNEW (file_handler);
b5a0ac70
SS
399 file_ptr->fd = fd;
400 file_ptr->ready_mask = 0;
401 file_ptr->next_file = gdb_notifier.first_file_handler;
402 gdb_notifier.first_file_handler = file_ptr;
b5a0ac70 403
05a6c72c
KS
404 if (use_poll)
405 {
b5a0ac70 406#ifdef HAVE_POLL
05a6c72c
KS
407 gdb_notifier.num_fds++;
408 if (gdb_notifier.poll_fds)
409 gdb_notifier.poll_fds =
410 (struct pollfd *) xrealloc (gdb_notifier.poll_fds,
411 (gdb_notifier.num_fds
412 * sizeof (struct pollfd)));
413 else
414 gdb_notifier.poll_fds =
8d749320 415 XNEW (struct pollfd);
05a6c72c
KS
416 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd = fd;
417 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events = mask;
418 (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents = 0;
44f45770 419#else
05a6c72c 420 internal_error (__FILE__, __LINE__,
e2e0b3e5 421 _("use_poll without HAVE_POLL"));
44f45770 422#endif /* HAVE_POLL */
05a6c72c 423 }
44f45770 424 else
05a6c72c
KS
425 {
426 if (mask & GDB_READABLE)
427 FD_SET (fd, &gdb_notifier.check_masks[0]);
428 else
429 FD_CLR (fd, &gdb_notifier.check_masks[0]);
430
431 if (mask & GDB_WRITABLE)
432 FD_SET (fd, &gdb_notifier.check_masks[1]);
433 else
434 FD_CLR (fd, &gdb_notifier.check_masks[1]);
435
436 if (mask & GDB_EXCEPTION)
437 FD_SET (fd, &gdb_notifier.check_masks[2]);
438 else
439 FD_CLR (fd, &gdb_notifier.check_masks[2]);
440
441 if (gdb_notifier.num_fds <= fd)
442 gdb_notifier.num_fds = fd + 1;
443 }
44f45770 444 }
05a6c72c
KS
445
446 file_ptr->proc = proc;
447 file_ptr->client_data = client_data;
448 file_ptr->mask = mask;
b5a0ac70
SS
449}
450
4e63d0ac
PA
451/* Return the next file handler to handle, and advance to the next
452 file handler, wrapping around if the end of the list is
453 reached. */
454
455static file_handler *
456get_next_file_handler_to_handle_and_advance (void)
457{
458 file_handler *curr_next;
459
460 /* The first time around, this is still NULL. */
461 if (gdb_notifier.next_file_handler == NULL)
462 gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
463
464 curr_next = gdb_notifier.next_file_handler;
465 gdb_assert (curr_next != NULL);
466
467 /* Advance. */
468 gdb_notifier.next_file_handler = curr_next->next_file;
469 /* Wrap around, if necessary. */
470 if (gdb_notifier.next_file_handler == NULL)
471 gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
472
473 return curr_next;
474}
475
b5a0ac70 476/* Remove the file descriptor FD from the list of monitored fd's:
371d5dec 477 i.e. we don't care anymore about events on the FD. */
b5a0ac70 478void
c2c6d25f 479delete_file_handler (int fd)
b5a0ac70
SS
480{
481 file_handler *file_ptr, *prev_ptr = NULL;
58a2c44a
EZ
482 int i;
483#ifdef HAVE_POLL
484 int j;
b5a0ac70 485 struct pollfd *new_poll_fds;
b5a0ac70
SS
486#endif
487
371d5dec 488 /* Find the entry for the given file. */
b5a0ac70
SS
489
490 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
491 file_ptr = file_ptr->next_file)
492 {
493 if (file_ptr->fd == fd)
494 break;
495 }
496
497 if (file_ptr == NULL)
498 return;
499
44f45770
EZ
500 if (use_poll)
501 {
b5a0ac70 502#ifdef HAVE_POLL
371d5dec
MS
503 /* Create a new poll_fds array by copying every fd's information
504 but the one we want to get rid of. */
b5a0ac70 505
371d5dec
MS
506 new_poll_fds = (struct pollfd *)
507 xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd));
b5a0ac70 508
44f45770 509 for (i = 0, j = 0; i < gdb_notifier.num_fds; i++)
b5a0ac70 510 {
44f45770
EZ
511 if ((gdb_notifier.poll_fds + i)->fd != fd)
512 {
513 (new_poll_fds + j)->fd = (gdb_notifier.poll_fds + i)->fd;
514 (new_poll_fds + j)->events = (gdb_notifier.poll_fds + i)->events;
3e43a32a
MS
515 (new_poll_fds + j)->revents
516 = (gdb_notifier.poll_fds + i)->revents;
44f45770
EZ
517 j++;
518 }
b5a0ac70 519 }
b8c9b27d 520 xfree (gdb_notifier.poll_fds);
44f45770
EZ
521 gdb_notifier.poll_fds = new_poll_fds;
522 gdb_notifier.num_fds--;
523#else
8e65ff28 524 internal_error (__FILE__, __LINE__,
e2e0b3e5 525 _("use_poll without HAVE_POLL"));
44f45770 526#endif /* HAVE_POLL */
b5a0ac70 527 }
44f45770
EZ
528 else
529 {
530 if (file_ptr->mask & GDB_READABLE)
531 FD_CLR (fd, &gdb_notifier.check_masks[0]);
532 if (file_ptr->mask & GDB_WRITABLE)
533 FD_CLR (fd, &gdb_notifier.check_masks[1]);
534 if (file_ptr->mask & GDB_EXCEPTION)
535 FD_CLR (fd, &gdb_notifier.check_masks[2]);
b5a0ac70 536
371d5dec 537 /* Find current max fd. */
b5a0ac70 538
44f45770 539 if ((fd + 1) == gdb_notifier.num_fds)
b5a0ac70 540 {
44f45770
EZ
541 gdb_notifier.num_fds--;
542 for (i = gdb_notifier.num_fds; i; i--)
543 {
544 if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0])
545 || FD_ISSET (i - 1, &gdb_notifier.check_masks[1])
546 || FD_ISSET (i - 1, &gdb_notifier.check_masks[2]))
547 break;
548 }
549 gdb_notifier.num_fds = i;
b5a0ac70
SS
550 }
551 }
b5a0ac70 552
cff3e48b 553 /* Deactivate the file descriptor, by clearing its mask,
371d5dec 554 so that it will not fire again. */
cff3e48b
JM
555
556 file_ptr->mask = 0;
557
4e63d0ac
PA
558 /* If this file handler was going to be the next one to be handled,
559 advance to the next's next, if any. */
560 if (gdb_notifier.next_file_handler == file_ptr)
561 {
562 if (file_ptr->next_file == NULL
563 && file_ptr == gdb_notifier.first_file_handler)
564 gdb_notifier.next_file_handler = NULL;
565 else
566 get_next_file_handler_to_handle_and_advance ();
567 }
568
371d5dec 569 /* Get rid of the file handler in the file handler list. */
b5a0ac70
SS
570 if (file_ptr == gdb_notifier.first_file_handler)
571 gdb_notifier.first_file_handler = file_ptr->next_file;
572 else
573 {
574 for (prev_ptr = gdb_notifier.first_file_handler;
9e0b60a8 575 prev_ptr->next_file != file_ptr;
b5a0ac70
SS
576 prev_ptr = prev_ptr->next_file)
577 ;
578 prev_ptr->next_file = file_ptr->next_file;
579 }
b8c9b27d 580 xfree (file_ptr);
b5a0ac70
SS
581}
582
583/* Handle the given event by calling the procedure associated to the
70b66289
PA
584 corresponding file handler. */
585
b5a0ac70 586static void
70b66289 587handle_file_event (file_handler *file_ptr, int ready_mask)
b5a0ac70 588{
c2c6d25f
JM
589 int mask;
590#ifdef HAVE_POLL
591 int error_mask;
c2c6d25f 592#endif
b5a0ac70 593
b5a0ac70 594 {
b5a0ac70
SS
595 {
596 /* With poll, the ready_mask could have any of three events
371d5dec
MS
597 set to 1: POLLHUP, POLLERR, POLLNVAL. These events
598 cannot be used in the requested event mask (events), but
599 they can be returned in the return mask (revents). We
600 need to check for those event too, and add them to the
601 mask which will be passed to the handler. */
b5a0ac70
SS
602
603 /* See if the desired events (mask) match the received
371d5dec 604 events (ready_mask). */
b5a0ac70 605
44f45770 606 if (use_poll)
c2c6d25f 607 {
44f45770 608#ifdef HAVE_POLL
652c71b4
AS
609 /* POLLHUP means EOF, but can be combined with POLLIN to
610 signal more data to read. */
44f45770 611 error_mask = POLLHUP | POLLERR | POLLNVAL;
70b66289 612 mask = ready_mask & (file_ptr->mask | error_mask);
44f45770 613
652c71b4 614 if ((mask & (POLLERR | POLLNVAL)) != 0)
44f45770 615 {
371d5dec
MS
616 /* Work in progress. We may need to tell somebody
617 what kind of error we had. */
652c71b4 618 if (mask & POLLERR)
3e43a32a
MS
619 printf_unfiltered (_("Error detected on fd %d\n"),
620 file_ptr->fd);
652c71b4 621 if (mask & POLLNVAL)
3e43a32a
MS
622 printf_unfiltered (_("Invalid or non-`poll'able fd %d\n"),
623 file_ptr->fd);
44f45770
EZ
624 file_ptr->error = 1;
625 }
626 else
627 file_ptr->error = 0;
628#else
8e65ff28 629 internal_error (__FILE__, __LINE__,
e2e0b3e5 630 _("use_poll without HAVE_POLL"));
44f45770 631#endif /* HAVE_POLL */
6426a772
JM
632 }
633 else
c2c6d25f 634 {
70b66289 635 if (ready_mask & GDB_EXCEPTION)
44f45770 636 {
3e43a32a
MS
637 printf_unfiltered (_("Exception condition detected "
638 "on fd %d\n"), file_ptr->fd);
44f45770
EZ
639 file_ptr->error = 1;
640 }
641 else
642 file_ptr->error = 0;
70b66289 643 mask = ready_mask & file_ptr->mask;
c2c6d25f 644 }
b5a0ac70 645
371d5dec 646 /* If there was a match, then call the handler. */
b5a0ac70 647 if (mask != 0)
2acceee2 648 (*file_ptr->proc) (file_ptr->error, file_ptr->client_data);
b5a0ac70
SS
649 }
650 }
651}
652
70b66289
PA
653/* Wait for new events on the monitored file descriptors. Run the
654 event handler if the first descriptor that is detected by the poll.
655 If BLOCK and if there are no events, this function will block in
656 the call to poll. Return 1 if an event was handled. Return -1 if
657 there are no file descriptors to monitor. Return 1 if an event was
658 handled, otherwise returns 0. */
659
b5a0ac70 660static int
50d01748 661gdb_wait_for_event (int block)
b5a0ac70
SS
662{
663 file_handler *file_ptr;
0f71a2f6 664 int num_found = 0;
b5a0ac70 665
371d5dec 666 /* Make sure all output is done before getting another event. */
da5bd37e
TT
667 gdb_stdout->flush ();
668 gdb_stderr->flush ();
7be570e7 669
b5a0ac70
SS
670 if (gdb_notifier.num_fds == 0)
671 return -1;
672
70b66289
PA
673 if (block)
674 update_wait_timeout ();
675
44f45770
EZ
676 if (use_poll)
677 {
b5a0ac70 678#ifdef HAVE_POLL
50d01748
PA
679 int timeout;
680
681 if (block)
682 timeout = gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1;
683 else
684 timeout = 0;
685
686 num_found = poll (gdb_notifier.poll_fds,
687 (unsigned long) gdb_notifier.num_fds, timeout);
44f45770
EZ
688
689 /* Don't print anything if we get out of poll because of a
50d01748 690 signal. */
44f45770 691 if (num_found == -1 && errno != EINTR)
e2e0b3e5 692 perror_with_name (("poll"));
44f45770 693#else
8e65ff28 694 internal_error (__FILE__, __LINE__,
e2e0b3e5 695 _("use_poll without HAVE_POLL"));
44f45770
EZ
696#endif /* HAVE_POLL */
697 }
698 else
c2c6d25f 699 {
50d01748 700 struct timeval select_timeout;
50d01748 701 struct timeval *timeout_p;
d7f9d729 702
50d01748
PA
703 if (block)
704 timeout_p = gdb_notifier.timeout_valid
705 ? &gdb_notifier.select_timeout : NULL;
706 else
707 {
708 memset (&select_timeout, 0, sizeof (select_timeout));
709 timeout_p = &select_timeout;
710 }
711
44f45770
EZ
712 gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0];
713 gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1];
714 gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2];
011825f0
MM
715 num_found = gdb_select (gdb_notifier.num_fds,
716 &gdb_notifier.ready_masks[0],
717 &gdb_notifier.ready_masks[1],
718 &gdb_notifier.ready_masks[2],
50d01748 719 timeout_p);
44f45770 720
371d5dec 721 /* Clear the masks after an error from select. */
44f45770
EZ
722 if (num_found == -1)
723 {
724 FD_ZERO (&gdb_notifier.ready_masks[0]);
725 FD_ZERO (&gdb_notifier.ready_masks[1]);
726 FD_ZERO (&gdb_notifier.ready_masks[2]);
50d01748
PA
727
728 /* Dont print anything if we got a signal, let gdb handle
729 it. */
44f45770 730 if (errno != EINTR)
e2e0b3e5 731 perror_with_name (("select"));
44f45770 732 }
c2c6d25f 733 }
b5a0ac70 734
4e63d0ac
PA
735 /* Avoid looking at poll_fds[i]->revents if no event fired. */
736 if (num_found <= 0)
737 return 0;
738
70b66289
PA
739 /* Run event handlers. We always run just one handler and go back
740 to polling, in case a handler changes the notifier list. Since
741 events for sources we haven't consumed yet wake poll/select
742 immediately, no event is lost. */
b5a0ac70 743
4e63d0ac
PA
744 /* To level the fairness across event descriptors, we handle them in
745 a round-robin-like fashion. The number and order of descriptors
746 may change between invocations, but this is good enough. */
44f45770
EZ
747 if (use_poll)
748 {
b5a0ac70 749#ifdef HAVE_POLL
4e63d0ac
PA
750 int i;
751 int mask;
b5a0ac70 752
4e63d0ac
PA
753 while (1)
754 {
755 if (gdb_notifier.next_poll_fds_index >= gdb_notifier.num_fds)
756 gdb_notifier.next_poll_fds_index = 0;
757 i = gdb_notifier.next_poll_fds_index++;
44f45770 758
4e63d0ac
PA
759 gdb_assert (i < gdb_notifier.num_fds);
760 if ((gdb_notifier.poll_fds + i)->revents)
761 break;
762 }
70b66289 763
4e63d0ac
PA
764 for (file_ptr = gdb_notifier.first_file_handler;
765 file_ptr != NULL;
766 file_ptr = file_ptr->next_file)
767 {
768 if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd)
769 break;
44f45770 770 }
4e63d0ac
PA
771 gdb_assert (file_ptr != NULL);
772
773 mask = (gdb_notifier.poll_fds + i)->revents;
774 handle_file_event (file_ptr, mask);
775 return 1;
44f45770 776#else
8e65ff28 777 internal_error (__FILE__, __LINE__,
e2e0b3e5 778 _("use_poll without HAVE_POLL"));
44f45770
EZ
779#endif /* HAVE_POLL */
780 }
781 else
782 {
4e63d0ac
PA
783 /* See comment about even source fairness above. */
784 int mask = 0;
785
786 do
b5a0ac70 787 {
4e63d0ac 788 file_ptr = get_next_file_handler_to_handle_and_advance ();
44f45770
EZ
789
790 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0]))
791 mask |= GDB_READABLE;
792 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1]))
793 mask |= GDB_WRITABLE;
794 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2]))
795 mask |= GDB_EXCEPTION;
b5a0ac70 796 }
4e63d0ac
PA
797 while (mask == 0);
798
799 handle_file_event (file_ptr, mask);
800 return 1;
b5a0ac70 801 }
b5a0ac70
SS
802 return 0;
803}
804\f
805
371d5dec 806/* Create an asynchronous handler, allocating memory for it.
b5a0ac70
SS
807 Return a pointer to the newly created handler.
808 This pointer will be used to invoke the handler by
809 invoke_async_signal_handler.
810 PROC is the function to call with CLIENT_DATA argument
371d5dec 811 whenever the handler is invoked. */
b5a0ac70 812async_signal_handler *
3e43a32a
MS
813create_async_signal_handler (sig_handler_func * proc,
814 gdb_client_data client_data)
b5a0ac70
SS
815{
816 async_signal_handler *async_handler_ptr;
817
8d749320 818 async_handler_ptr = XNEW (async_signal_handler);
b5a0ac70
SS
819 async_handler_ptr->ready = 0;
820 async_handler_ptr->next_handler = NULL;
821 async_handler_ptr->proc = proc;
822 async_handler_ptr->client_data = client_data;
823 if (sighandler_list.first_handler == NULL)
824 sighandler_list.first_handler = async_handler_ptr;
825 else
826 sighandler_list.last_handler->next_handler = async_handler_ptr;
827 sighandler_list.last_handler = async_handler_ptr;
828 return async_handler_ptr;
829}
830
371d5dec
MS
831/* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
832 will be used when the handlers are invoked, after we have waited
833 for some event. The caller of this function is the interrupt
834 handler associated with a signal. */
b5a0ac70 835void
6426a772 836mark_async_signal_handler (async_signal_handler * async_handler_ptr)
b5a0ac70 837{
50d01748 838 async_handler_ptr->ready = 1;
5cc3ce8b 839 serial_event_set (async_signal_handlers_serial_event);
b5a0ac70
SS
840}
841
abc56d60
PA
842/* See event-loop.h. */
843
844void
845clear_async_signal_handler (async_signal_handler *async_handler_ptr)
846{
847 async_handler_ptr->ready = 0;
848}
849
850/* See event-loop.h. */
851
852int
853async_signal_handler_is_marked (async_signal_handler *async_handler_ptr)
854{
855 return async_handler_ptr->ready;
856}
857
50d01748
PA
858/* Call all the handlers that are ready. Returns true if any was
859 indeed ready. */
5cc3ce8b 860
50d01748
PA
861static int
862invoke_async_signal_handlers (void)
b5a0ac70
SS
863{
864 async_signal_handler *async_handler_ptr;
50d01748 865 int any_ready = 0;
b5a0ac70 866
5cc3ce8b
PA
867 /* We're going to handle all pending signals, so no need to wake up
868 the event loop again the next time around. Note this must be
869 cleared _before_ calling the callbacks, to avoid races. */
870 serial_event_clear (async_signal_handlers_serial_event);
871
872 /* Invoke all ready handlers. */
b5a0ac70
SS
873
874 while (1)
875 {
c5aa993b 876 for (async_handler_ptr = sighandler_list.first_handler;
b5a0ac70
SS
877 async_handler_ptr != NULL;
878 async_handler_ptr = async_handler_ptr->next_handler)
879 {
880 if (async_handler_ptr->ready)
881 break;
882 }
883 if (async_handler_ptr == NULL)
884 break;
50d01748 885 any_ready = 1;
b5a0ac70 886 async_handler_ptr->ready = 0;
7c36c34e
PA
887 /* Async signal handlers have no connection to whichever was the
888 current UI, and thus always run on the main one. */
889 current_ui = main_ui;
b5a0ac70
SS
890 (*async_handler_ptr->proc) (async_handler_ptr->client_data);
891 }
892
50d01748 893 return any_ready;
b5a0ac70
SS
894}
895
371d5dec 896/* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
b5a0ac70
SS
897 Free the space allocated for it. */
898void
6426a772 899delete_async_signal_handler (async_signal_handler ** async_handler_ptr)
b5a0ac70
SS
900{
901 async_signal_handler *prev_ptr;
902
43ff13b4 903 if (sighandler_list.first_handler == (*async_handler_ptr))
b5a0ac70 904 {
43ff13b4 905 sighandler_list.first_handler = (*async_handler_ptr)->next_handler;
b5a0ac70
SS
906 if (sighandler_list.first_handler == NULL)
907 sighandler_list.last_handler = NULL;
908 }
909 else
910 {
911 prev_ptr = sighandler_list.first_handler;
32107cd5 912 while (prev_ptr && prev_ptr->next_handler != (*async_handler_ptr))
b5a0ac70 913 prev_ptr = prev_ptr->next_handler;
60bc018f 914 gdb_assert (prev_ptr);
43ff13b4
JM
915 prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
916 if (sighandler_list.last_handler == (*async_handler_ptr))
b5a0ac70
SS
917 sighandler_list.last_handler = prev_ptr;
918 }
b8c9b27d 919 xfree ((*async_handler_ptr));
43ff13b4 920 (*async_handler_ptr) = NULL;
b5a0ac70
SS
921}
922
50d01748
PA
923/* Create an asynchronous event handler, allocating memory for it.
924 Return a pointer to the newly created handler. PROC is the
925 function to call with CLIENT_DATA argument whenever the handler is
926 invoked. */
927async_event_handler *
928create_async_event_handler (async_event_handler_func *proc,
929 gdb_client_data client_data)
930{
931 async_event_handler *h;
932
8d749320 933 h = XNEW (struct async_event_handler);
50d01748
PA
934 h->ready = 0;
935 h->next_handler = NULL;
936 h->proc = proc;
937 h->client_data = client_data;
938 if (async_event_handler_list.first_handler == NULL)
939 async_event_handler_list.first_handler = h;
940 else
941 async_event_handler_list.last_handler->next_handler = h;
942 async_event_handler_list.last_handler = h;
943 return h;
944}
945
946/* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
947 will be used by gdb_do_one_event. The caller will be whoever
948 created the event source, and wants to signal that the event is
949 ready to be handled. */
950void
951mark_async_event_handler (async_event_handler *async_handler_ptr)
952{
953 async_handler_ptr->ready = 1;
954}
955
b7d2e916
PA
956/* See event-loop.h. */
957
958void
959clear_async_event_handler (async_event_handler *async_handler_ptr)
960{
961 async_handler_ptr->ready = 0;
962}
963
70b66289
PA
964/* Check if asynchronous event handlers are ready, and call the
965 handler function for one that is. */
50d01748 966
70b66289 967static int
50d01748
PA
968check_async_event_handlers (void)
969{
970 async_event_handler *async_handler_ptr;
50d01748
PA
971
972 for (async_handler_ptr = async_event_handler_list.first_handler;
973 async_handler_ptr != NULL;
974 async_handler_ptr = async_handler_ptr->next_handler)
975 {
976 if (async_handler_ptr->ready)
977 {
978 async_handler_ptr->ready = 0;
70b66289
PA
979 (*async_handler_ptr->proc) (async_handler_ptr->client_data);
980 return 1;
50d01748
PA
981 }
982 }
70b66289
PA
983
984 return 0;
50d01748
PA
985}
986
987/* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
988 Free the space allocated for it. */
989void
990delete_async_event_handler (async_event_handler **async_handler_ptr)
b5a0ac70 991{
50d01748
PA
992 async_event_handler *prev_ptr;
993
994 if (async_event_handler_list.first_handler == *async_handler_ptr)
995 {
3e43a32a
MS
996 async_event_handler_list.first_handler
997 = (*async_handler_ptr)->next_handler;
50d01748
PA
998 if (async_event_handler_list.first_handler == NULL)
999 async_event_handler_list.last_handler = NULL;
1000 }
1001 else
1002 {
1003 prev_ptr = async_event_handler_list.first_handler;
1004 while (prev_ptr && prev_ptr->next_handler != *async_handler_ptr)
1005 prev_ptr = prev_ptr->next_handler;
60bc018f 1006 gdb_assert (prev_ptr);
50d01748
PA
1007 prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
1008 if (async_event_handler_list.last_handler == (*async_handler_ptr))
1009 async_event_handler_list.last_handler = prev_ptr;
1010 }
1011 xfree (*async_handler_ptr);
1012 *async_handler_ptr = NULL;
b5a0ac70 1013}
c2c6d25f 1014
dcb07cfa
PA
1015/* Create a timer that will expire in MS milliseconds from now. When
1016 the timer is ready, PROC will be executed. At creation, the timer
1017 is added to the timers queue. This queue is kept sorted in order
1018 of increasing timers. Return a handle to the timer struct. */
1019
c2c6d25f 1020int
dcb07cfa 1021create_timer (int ms, timer_handler_func *proc,
371d5dec 1022 gdb_client_data client_data)
c2c6d25f 1023{
dcb07cfa 1024 using namespace std::chrono;
c2c6d25f 1025 struct gdb_timer *timer_ptr, *timer_index, *prev_timer;
6426a772 1026
dcb07cfa 1027 steady_clock::time_point time_now = steady_clock::now ();
c2c6d25f 1028
dcb07cfa
PA
1029 timer_ptr = new gdb_timer ();
1030 timer_ptr->when = time_now + milliseconds (ms);
c2c6d25f
JM
1031 timer_ptr->proc = proc;
1032 timer_ptr->client_data = client_data;
6426a772 1033 timer_list.num_timers++;
c2c6d25f
JM
1034 timer_ptr->timer_id = timer_list.num_timers;
1035
1036 /* Now add the timer to the timer queue, making sure it is sorted in
371d5dec 1037 increasing order of expiration. */
c2c6d25f 1038
6426a772
JM
1039 for (timer_index = timer_list.first_timer;
1040 timer_index != NULL;
c2c6d25f
JM
1041 timer_index = timer_index->next)
1042 {
dcb07cfa 1043 if (timer_index->when > timer_ptr->when)
c2c6d25f
JM
1044 break;
1045 }
6426a772 1046
c2c6d25f
JM
1047 if (timer_index == timer_list.first_timer)
1048 {
1049 timer_ptr->next = timer_list.first_timer;
1050 timer_list.first_timer = timer_ptr;
1051
1052 }
1053 else
1054 {
6426a772
JM
1055 for (prev_timer = timer_list.first_timer;
1056 prev_timer->next != timer_index;
c2c6d25f
JM
1057 prev_timer = prev_timer->next)
1058 ;
6426a772 1059
c2c6d25f
JM
1060 prev_timer->next = timer_ptr;
1061 timer_ptr->next = timer_index;
1062 }
1063
1064 gdb_notifier.timeout_valid = 0;
1065 return timer_ptr->timer_id;
1066}
1067
1068/* There is a chance that the creator of the timer wants to get rid of
371d5dec 1069 it before it expires. */
c2c6d25f
JM
1070void
1071delete_timer (int id)
1072{
1073 struct gdb_timer *timer_ptr, *prev_timer = NULL;
1074
371d5dec 1075 /* Find the entry for the given timer. */
c2c6d25f
JM
1076
1077 for (timer_ptr = timer_list.first_timer; timer_ptr != NULL;
1078 timer_ptr = timer_ptr->next)
1079 {
1080 if (timer_ptr->timer_id == id)
1081 break;
1082 }
1083
1084 if (timer_ptr == NULL)
1085 return;
371d5dec 1086 /* Get rid of the timer in the timer list. */
c2c6d25f
JM
1087 if (timer_ptr == timer_list.first_timer)
1088 timer_list.first_timer = timer_ptr->next;
1089 else
1090 {
1091 for (prev_timer = timer_list.first_timer;
1092 prev_timer->next != timer_ptr;
1093 prev_timer = prev_timer->next)
1094 ;
1095 prev_timer->next = timer_ptr->next;
1096 }
dcb07cfa 1097 delete timer_ptr;
c2c6d25f
JM
1098
1099 gdb_notifier.timeout_valid = 0;
1100}
1101
dcb07cfa
PA
1102/* Convert a std::chrono duration to a struct timeval. */
1103
1104template<typename Duration>
1105static struct timeval
1106duration_cast_timeval (const Duration &d)
1107{
1108 using namespace std::chrono;
1109 seconds sec = duration_cast<seconds> (d);
1110 microseconds msec = duration_cast<microseconds> (d - sec);
1111
1112 struct timeval tv;
1113 tv.tv_sec = sec.count ();
1114 tv.tv_usec = msec.count ();
1115 return tv;
1116}
1117
70b66289
PA
1118/* Update the timeout for the select() or poll(). Returns true if the
1119 timer has already expired, false otherwise. */
6426a772 1120
70b66289
PA
1121static int
1122update_wait_timeout (void)
c2c6d25f 1123{
2acceee2 1124 if (timer_list.first_timer != NULL)
c2c6d25f 1125 {
dcb07cfa
PA
1126 using namespace std::chrono;
1127 steady_clock::time_point time_now = steady_clock::now ();
1128 struct timeval timeout;
6426a772 1129
dcb07cfa 1130 if (timer_list.first_timer->when < time_now)
c2c6d25f 1131 {
70b66289 1132 /* It expired already. */
dcb07cfa
PA
1133 timeout.tv_sec = 0;
1134 timeout.tv_usec = 0;
1135 }
1136 else
1137 {
1138 steady_clock::duration d = timer_list.first_timer->when - time_now;
1139 timeout = duration_cast_timeval (d);
c2c6d25f
JM
1140 }
1141
70b66289 1142 /* Update the timeout for select/ poll. */
44f45770
EZ
1143 if (use_poll)
1144 {
c2c6d25f 1145#ifdef HAVE_POLL
dcb07cfa 1146 gdb_notifier.poll_timeout = timeout.tv_sec * 1000;
c2c6d25f 1147#else
8e65ff28 1148 internal_error (__FILE__, __LINE__,
e2e0b3e5 1149 _("use_poll without HAVE_POLL"));
44f45770
EZ
1150#endif /* HAVE_POLL */
1151 }
1152 else
1153 {
dcb07cfa
PA
1154 gdb_notifier.select_timeout.tv_sec = timeout.tv_sec;
1155 gdb_notifier.select_timeout.tv_usec = timeout.tv_usec;
44f45770 1156 }
c2c6d25f 1157 gdb_notifier.timeout_valid = 1;
70b66289 1158
dcb07cfa 1159 if (timer_list.first_timer->when < time_now)
70b66289 1160 return 1;
c2c6d25f 1161 }
6426a772 1162 else
c2c6d25f 1163 gdb_notifier.timeout_valid = 0;
70b66289
PA
1164
1165 return 0;
1166}
1167
1168/* Check whether a timer in the timers queue is ready. If a timer is
1169 ready, call its handler and return. Update the timeout for the
1170 select() or poll() as well. Return 1 if an event was handled,
1171 otherwise returns 0.*/
1172
1173static int
1174poll_timers (void)
1175{
1176 if (update_wait_timeout ())
1177 {
1178 struct gdb_timer *timer_ptr = timer_list.first_timer;
1179 timer_handler_func *proc = timer_ptr->proc;
1180 gdb_client_data client_data = timer_ptr->client_data;
1181
1182 /* Get rid of the timer from the beginning of the list. */
1183 timer_list.first_timer = timer_ptr->next;
1184
1185 /* Delete the timer before calling the callback, not after, in
1186 case the callback itself decides to try deleting the timer
1187 too. */
0e05cf3a 1188 delete timer_ptr;
70b66289
PA
1189
1190 /* Call the procedure associated with that timer. */
1191 (proc) (client_data);
1192
1193 return 1;
1194 }
1195
1196 return 0;
c2c6d25f 1197}
This page took 1.549285 seconds and 4 git commands to generate.