Introduce and use flush_streams
[deliverable/binutils-gdb.git] / gdb / event-loop.c
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.
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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "event-loop.h"
22 #include "event-top.h"
23 #include "ser-event.h"
24
25 #include <chrono>
26
27 #ifdef HAVE_POLL
28 #if defined (HAVE_POLL_H)
29 #include <poll.h>
30 #elif defined (HAVE_SYS_POLL_H)
31 #include <sys/poll.h>
32 #endif
33 #endif
34
35 #include <sys/types.h>
36 #include "gdbsupport/gdb_sys_time.h"
37 #include "gdbsupport/gdb_select.h"
38 #include "top.h"
39
40 /* Tell create_file_handler what events we are interested in.
41 This is used by the select version of the event loop. */
42
43 #define GDB_READABLE (1<<1)
44 #define GDB_WRITABLE (1<<2)
45 #define GDB_EXCEPTION (1<<3)
46
47 /* Information about each file descriptor we register with the event
48 loop. */
49
50 typedef struct file_handler
51 {
52 int fd; /* File descriptor. */
53 int mask; /* Events we want to monitor: POLLIN, etc. */
54 int ready_mask; /* Events that have been seen since
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. */
60 }
61 file_handler;
62
63 /* PROC is a function to be invoked when the READY flag is set. This
64 happens when there has been a signal and the corresponding signal
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
70 Async_init_signals takes care of setting up such an
71 async_signal_handler for each interesting signal. */
72
73 typedef struct async_signal_handler
74 {
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. */
81 }
82 async_signal_handler;
83
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. */
90 typedef 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 }
105 async_event_handler;
106
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
109 descriptor. We have two flavors of the notifier, one for platforms
110 that have the POLL function, the other for those that don't, and
111 only support SELECT. Each of the elements in the gdb_notifier list is
112 basically a description of what kind of events gdb is interested
113 in, for each fd. */
114
115 /* As of 1999-04-30 only the input file descriptor is registered with the
116 event loop. */
117
118 /* Do we use poll or select ? */
119 #ifdef HAVE_POLL
120 #define USE_POLL 1
121 #else
122 #define USE_POLL 0
123 #endif /* HAVE_POLL */
124
125 static unsigned char use_poll = USE_POLL;
126
127 #ifdef USE_WIN32API
128 #include <windows.h>
129 #include <io.h>
130 #endif
131
132 static struct
133 {
134 /* Ptr to head of file handler list. */
135 file_handler *first_file_handler;
136
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
144 #ifdef HAVE_POLL
145 /* Ptr to array of pollfd structures. */
146 struct pollfd *poll_fds;
147
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
155 /* Timeout in milliseconds for calls to poll(). */
156 int poll_timeout;
157 #endif
158
159 /* Masks to be used in the next call to select.
160 Bits are set in response to calls to create_file_handler. */
161 fd_set check_masks[3];
162
163 /* What file descriptors were found ready by select. */
164 fd_set ready_masks[3];
165
166 /* Number of file descriptors to monitor (for poll). */
167 /* Number of valid bits (highest fd value + 1) (for select). */
168 int num_fds;
169
170 /* Time structure for calls to select(). */
171 struct timeval select_timeout;
172
173 /* Flag to tell whether the timeout should be used. */
174 int timeout_valid;
175 }
176 gdb_notifier;
177
178 /* Structure associated with a timer. PROC will be executed at the
179 first occasion after WHEN. */
180 struct gdb_timer
181 {
182 std::chrono::steady_clock::time_point when;
183 int timer_id;
184 struct gdb_timer *next;
185 timer_handler_func *proc; /* Function to call to do the work. */
186 gdb_client_data client_data; /* Argument to async_handler_func. */
187 };
188
189 /* List of currently active timers. It is sorted in order of
190 increasing timers. */
191 static struct
192 {
193 /* Pointer to first in timer list. */
194 struct gdb_timer *first_timer;
195
196 /* Id of the last timer created. */
197 int num_timers;
198 }
199 timer_list;
200
201 /* All the async_signal_handlers gdb is interested in are kept onto
202 this list. */
203 static struct
204 {
205 /* Pointer to first in handler list. */
206 async_signal_handler *first_handler;
207
208 /* Pointer to last in handler list. */
209 async_signal_handler *last_handler;
210 }
211 sighandler_list;
212
213 /* All the async_event_handlers gdb is interested in are kept onto
214 this list. */
215 static struct
216 {
217 /* Pointer to first in handler list. */
218 async_event_handler *first_handler;
219
220 /* Pointer to last in handler list. */
221 async_event_handler *last_handler;
222 }
223 async_event_handler_list;
224
225 static int invoke_async_signal_handlers (void);
226 static void create_file_handler (int fd, int mask, handler_func *proc,
227 gdb_client_data client_data);
228 static int check_async_event_handlers (void);
229 static int gdb_wait_for_event (int);
230 static int update_wait_timeout (void);
231 static int poll_timers (void);
232 \f
233
234 /* This event is signalled whenever an asynchronous handler needs to
235 defer an action to the event loop. */
236 static struct serial_event *async_signal_handlers_serial_event;
237
238 /* Callback registered with ASYNC_SIGNAL_HANDLERS_SERIAL_EVENT. */
239
240 static void
241 async_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
247 void
248 initialize_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
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
258 it. Returns >0 if something was done otherwise returns <0 (this
259 can happen if there are no event sources to wait for). */
260
261 int
262 gdb_do_one_event (void)
263 {
264 static int event_source_head = 0;
265 const int number_of_sources = 3;
266 int current = 0;
267
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 ())
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++)
277 {
278 int res;
279
280 switch (event_source_head)
281 {
282 case 0:
283 /* Are any timers that are ready? */
284 res = poll_timers ();
285 break;
286 case 1:
287 /* Are there events already waiting to be collected on the
288 monitored file descriptors? */
289 res = gdb_wait_for_event (0);
290 break;
291 case 2:
292 /* Are there any asynchronous event handlers ready? */
293 res = check_async_event_handlers ();
294 break;
295 default:
296 internal_error (__FILE__, __LINE__,
297 "unexpected event_source_head %d",
298 event_source_head);
299 }
300
301 event_source_head++;
302 if (event_source_head == number_of_sources)
303 event_source_head = 0;
304
305 if (res > 0)
306 return 1;
307 }
308
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. */
313
314 if (gdb_wait_for_event (1) < 0)
315 return -1;
316
317 /* If gdb_wait_for_event has returned 1, it means that one event has
318 been handled. We break out of the loop. */
319 return 1;
320 }
321
322 \f
323
324 /* Wrapper function for create_file_handler, so that the caller
325 doesn't have to know implementation details about the use of poll
326 vs. select. */
327 void
328 add_file_handler (int fd, handler_func * proc, gdb_client_data client_data)
329 {
330 #ifdef HAVE_POLL
331 struct pollfd fds;
332 #endif
333
334 if (use_poll)
335 {
336 #ifdef HAVE_POLL
337 /* Check to see if poll () is usable. If not, we'll switch to
338 use select. This can happen on systems like
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
341 `poll'able. */
342 fds.fd = fd;
343 fds.events = POLLIN;
344 if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL))
345 use_poll = 0;
346 #else
347 internal_error (__FILE__, __LINE__,
348 _("use_poll without HAVE_POLL"));
349 #endif /* HAVE_POLL */
350 }
351 if (use_poll)
352 {
353 #ifdef HAVE_POLL
354 create_file_handler (fd, POLLIN, proc, client_data);
355 #else
356 internal_error (__FILE__, __LINE__,
357 _("use_poll without HAVE_POLL"));
358 #endif
359 }
360 else
361 create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION,
362 proc, client_data);
363 }
364
365 /* Add a file handler/descriptor to the list of descriptors we are
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
379 static void
380 create_file_handler (int fd, int mask, handler_func * proc,
381 gdb_client_data client_data)
382 {
383 file_handler *file_ptr;
384
385 /* Do we already have a file handler for this file? (We may be
386 changing its associated procedure). */
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
394 /* It is a new file descriptor. Add it to the list. Otherwise, just
395 change the data associated with it. */
396 if (file_ptr == NULL)
397 {
398 file_ptr = XNEW (file_handler);
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;
403
404 if (use_poll)
405 {
406 #ifdef HAVE_POLL
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 =
415 XNEW (struct pollfd);
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;
419 #else
420 internal_error (__FILE__, __LINE__,
421 _("use_poll without HAVE_POLL"));
422 #endif /* HAVE_POLL */
423 }
424 else
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 }
444 }
445
446 file_ptr->proc = proc;
447 file_ptr->client_data = client_data;
448 file_ptr->mask = mask;
449 }
450
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
455 static file_handler *
456 get_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
476 /* Remove the file descriptor FD from the list of monitored fd's:
477 i.e. we don't care anymore about events on the FD. */
478 void
479 delete_file_handler (int fd)
480 {
481 file_handler *file_ptr, *prev_ptr = NULL;
482 int i;
483 #ifdef HAVE_POLL
484 int j;
485 struct pollfd *new_poll_fds;
486 #endif
487
488 /* Find the entry for the given file. */
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
500 if (use_poll)
501 {
502 #ifdef HAVE_POLL
503 /* Create a new poll_fds array by copying every fd's information
504 but the one we want to get rid of. */
505
506 new_poll_fds = (struct pollfd *)
507 xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd));
508
509 for (i = 0, j = 0; i < gdb_notifier.num_fds; i++)
510 {
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;
515 (new_poll_fds + j)->revents
516 = (gdb_notifier.poll_fds + i)->revents;
517 j++;
518 }
519 }
520 xfree (gdb_notifier.poll_fds);
521 gdb_notifier.poll_fds = new_poll_fds;
522 gdb_notifier.num_fds--;
523 #else
524 internal_error (__FILE__, __LINE__,
525 _("use_poll without HAVE_POLL"));
526 #endif /* HAVE_POLL */
527 }
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]);
536
537 /* Find current max fd. */
538
539 if ((fd + 1) == gdb_notifier.num_fds)
540 {
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;
550 }
551 }
552
553 /* Deactivate the file descriptor, by clearing its mask,
554 so that it will not fire again. */
555
556 file_ptr->mask = 0;
557
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
569 /* Get rid of the file handler in the file handler list. */
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;
575 prev_ptr->next_file != file_ptr;
576 prev_ptr = prev_ptr->next_file)
577 ;
578 prev_ptr->next_file = file_ptr->next_file;
579 }
580 xfree (file_ptr);
581 }
582
583 /* Handle the given event by calling the procedure associated to the
584 corresponding file handler. */
585
586 static void
587 handle_file_event (file_handler *file_ptr, int ready_mask)
588 {
589 int mask;
590 #ifdef HAVE_POLL
591 int error_mask;
592 #endif
593
594 {
595 {
596 /* With poll, the ready_mask could have any of three events
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. */
602
603 /* See if the desired events (mask) match the received
604 events (ready_mask). */
605
606 if (use_poll)
607 {
608 #ifdef HAVE_POLL
609 /* POLLHUP means EOF, but can be combined with POLLIN to
610 signal more data to read. */
611 error_mask = POLLHUP | POLLERR | POLLNVAL;
612 mask = ready_mask & (file_ptr->mask | error_mask);
613
614 if ((mask & (POLLERR | POLLNVAL)) != 0)
615 {
616 /* Work in progress. We may need to tell somebody
617 what kind of error we had. */
618 if (mask & POLLERR)
619 warning (_("Error detected on fd %d"), file_ptr->fd);
620 if (mask & POLLNVAL)
621 warning (_("Invalid or non-`poll'able fd %d"),
622 file_ptr->fd);
623 file_ptr->error = 1;
624 }
625 else
626 file_ptr->error = 0;
627 #else
628 internal_error (__FILE__, __LINE__,
629 _("use_poll without HAVE_POLL"));
630 #endif /* HAVE_POLL */
631 }
632 else
633 {
634 if (ready_mask & GDB_EXCEPTION)
635 {
636 warning (_("Exception condition detected on fd %d"),
637 file_ptr->fd);
638 file_ptr->error = 1;
639 }
640 else
641 file_ptr->error = 0;
642 mask = ready_mask & file_ptr->mask;
643 }
644
645 /* If there was a match, then call the handler. */
646 if (mask != 0)
647 (*file_ptr->proc) (file_ptr->error, file_ptr->client_data);
648 }
649 }
650 }
651
652 /* Wait for new events on the monitored file descriptors. Run the
653 event handler if the first descriptor that is detected by the poll.
654 If BLOCK and if there are no events, this function will block in
655 the call to poll. Return 1 if an event was handled. Return -1 if
656 there are no file descriptors to monitor. Return 1 if an event was
657 handled, otherwise returns 0. */
658
659 static int
660 gdb_wait_for_event (int block)
661 {
662 file_handler *file_ptr;
663 int num_found = 0;
664
665 /* Make sure all output is done before getting another event. */
666 flush_streams ();
667
668 if (gdb_notifier.num_fds == 0)
669 return -1;
670
671 if (block)
672 update_wait_timeout ();
673
674 if (use_poll)
675 {
676 #ifdef HAVE_POLL
677 int timeout;
678
679 if (block)
680 timeout = gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1;
681 else
682 timeout = 0;
683
684 num_found = poll (gdb_notifier.poll_fds,
685 (unsigned long) gdb_notifier.num_fds, timeout);
686
687 /* Don't print anything if we get out of poll because of a
688 signal. */
689 if (num_found == -1 && errno != EINTR)
690 perror_with_name (("poll"));
691 #else
692 internal_error (__FILE__, __LINE__,
693 _("use_poll without HAVE_POLL"));
694 #endif /* HAVE_POLL */
695 }
696 else
697 {
698 struct timeval select_timeout;
699 struct timeval *timeout_p;
700
701 if (block)
702 timeout_p = gdb_notifier.timeout_valid
703 ? &gdb_notifier.select_timeout : NULL;
704 else
705 {
706 memset (&select_timeout, 0, sizeof (select_timeout));
707 timeout_p = &select_timeout;
708 }
709
710 gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0];
711 gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1];
712 gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2];
713 num_found = gdb_select (gdb_notifier.num_fds,
714 &gdb_notifier.ready_masks[0],
715 &gdb_notifier.ready_masks[1],
716 &gdb_notifier.ready_masks[2],
717 timeout_p);
718
719 /* Clear the masks after an error from select. */
720 if (num_found == -1)
721 {
722 FD_ZERO (&gdb_notifier.ready_masks[0]);
723 FD_ZERO (&gdb_notifier.ready_masks[1]);
724 FD_ZERO (&gdb_notifier.ready_masks[2]);
725
726 /* Dont print anything if we got a signal, let gdb handle
727 it. */
728 if (errno != EINTR)
729 perror_with_name (("select"));
730 }
731 }
732
733 /* Avoid looking at poll_fds[i]->revents if no event fired. */
734 if (num_found <= 0)
735 return 0;
736
737 /* Run event handlers. We always run just one handler and go back
738 to polling, in case a handler changes the notifier list. Since
739 events for sources we haven't consumed yet wake poll/select
740 immediately, no event is lost. */
741
742 /* To level the fairness across event descriptors, we handle them in
743 a round-robin-like fashion. The number and order of descriptors
744 may change between invocations, but this is good enough. */
745 if (use_poll)
746 {
747 #ifdef HAVE_POLL
748 int i;
749 int mask;
750
751 while (1)
752 {
753 if (gdb_notifier.next_poll_fds_index >= gdb_notifier.num_fds)
754 gdb_notifier.next_poll_fds_index = 0;
755 i = gdb_notifier.next_poll_fds_index++;
756
757 gdb_assert (i < gdb_notifier.num_fds);
758 if ((gdb_notifier.poll_fds + i)->revents)
759 break;
760 }
761
762 for (file_ptr = gdb_notifier.first_file_handler;
763 file_ptr != NULL;
764 file_ptr = file_ptr->next_file)
765 {
766 if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd)
767 break;
768 }
769 gdb_assert (file_ptr != NULL);
770
771 mask = (gdb_notifier.poll_fds + i)->revents;
772 handle_file_event (file_ptr, mask);
773 return 1;
774 #else
775 internal_error (__FILE__, __LINE__,
776 _("use_poll without HAVE_POLL"));
777 #endif /* HAVE_POLL */
778 }
779 else
780 {
781 /* See comment about even source fairness above. */
782 int mask = 0;
783
784 do
785 {
786 file_ptr = get_next_file_handler_to_handle_and_advance ();
787
788 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0]))
789 mask |= GDB_READABLE;
790 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1]))
791 mask |= GDB_WRITABLE;
792 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2]))
793 mask |= GDB_EXCEPTION;
794 }
795 while (mask == 0);
796
797 handle_file_event (file_ptr, mask);
798 return 1;
799 }
800 return 0;
801 }
802 \f
803
804 /* Create an asynchronous handler, allocating memory for it.
805 Return a pointer to the newly created handler.
806 This pointer will be used to invoke the handler by
807 invoke_async_signal_handler.
808 PROC is the function to call with CLIENT_DATA argument
809 whenever the handler is invoked. */
810 async_signal_handler *
811 create_async_signal_handler (sig_handler_func * proc,
812 gdb_client_data client_data)
813 {
814 async_signal_handler *async_handler_ptr;
815
816 async_handler_ptr = XNEW (async_signal_handler);
817 async_handler_ptr->ready = 0;
818 async_handler_ptr->next_handler = NULL;
819 async_handler_ptr->proc = proc;
820 async_handler_ptr->client_data = client_data;
821 if (sighandler_list.first_handler == NULL)
822 sighandler_list.first_handler = async_handler_ptr;
823 else
824 sighandler_list.last_handler->next_handler = async_handler_ptr;
825 sighandler_list.last_handler = async_handler_ptr;
826 return async_handler_ptr;
827 }
828
829 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
830 will be used when the handlers are invoked, after we have waited
831 for some event. The caller of this function is the interrupt
832 handler associated with a signal. */
833 void
834 mark_async_signal_handler (async_signal_handler * async_handler_ptr)
835 {
836 async_handler_ptr->ready = 1;
837 serial_event_set (async_signal_handlers_serial_event);
838 }
839
840 /* See event-loop.h. */
841
842 void
843 clear_async_signal_handler (async_signal_handler *async_handler_ptr)
844 {
845 async_handler_ptr->ready = 0;
846 }
847
848 /* See event-loop.h. */
849
850 int
851 async_signal_handler_is_marked (async_signal_handler *async_handler_ptr)
852 {
853 return async_handler_ptr->ready;
854 }
855
856 /* Call all the handlers that are ready. Returns true if any was
857 indeed ready. */
858
859 static int
860 invoke_async_signal_handlers (void)
861 {
862 async_signal_handler *async_handler_ptr;
863 int any_ready = 0;
864
865 /* We're going to handle all pending signals, so no need to wake up
866 the event loop again the next time around. Note this must be
867 cleared _before_ calling the callbacks, to avoid races. */
868 serial_event_clear (async_signal_handlers_serial_event);
869
870 /* Invoke all ready handlers. */
871
872 while (1)
873 {
874 for (async_handler_ptr = sighandler_list.first_handler;
875 async_handler_ptr != NULL;
876 async_handler_ptr = async_handler_ptr->next_handler)
877 {
878 if (async_handler_ptr->ready)
879 break;
880 }
881 if (async_handler_ptr == NULL)
882 break;
883 any_ready = 1;
884 async_handler_ptr->ready = 0;
885 /* Async signal handlers have no connection to whichever was the
886 current UI, and thus always run on the main one. */
887 current_ui = main_ui;
888 (*async_handler_ptr->proc) (async_handler_ptr->client_data);
889 }
890
891 return any_ready;
892 }
893
894 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
895 Free the space allocated for it. */
896 void
897 delete_async_signal_handler (async_signal_handler ** async_handler_ptr)
898 {
899 async_signal_handler *prev_ptr;
900
901 if (sighandler_list.first_handler == (*async_handler_ptr))
902 {
903 sighandler_list.first_handler = (*async_handler_ptr)->next_handler;
904 if (sighandler_list.first_handler == NULL)
905 sighandler_list.last_handler = NULL;
906 }
907 else
908 {
909 prev_ptr = sighandler_list.first_handler;
910 while (prev_ptr && prev_ptr->next_handler != (*async_handler_ptr))
911 prev_ptr = prev_ptr->next_handler;
912 gdb_assert (prev_ptr);
913 prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
914 if (sighandler_list.last_handler == (*async_handler_ptr))
915 sighandler_list.last_handler = prev_ptr;
916 }
917 xfree ((*async_handler_ptr));
918 (*async_handler_ptr) = NULL;
919 }
920
921 /* Create an asynchronous event handler, allocating memory for it.
922 Return a pointer to the newly created handler. PROC is the
923 function to call with CLIENT_DATA argument whenever the handler is
924 invoked. */
925 async_event_handler *
926 create_async_event_handler (async_event_handler_func *proc,
927 gdb_client_data client_data)
928 {
929 async_event_handler *h;
930
931 h = XNEW (struct async_event_handler);
932 h->ready = 0;
933 h->next_handler = NULL;
934 h->proc = proc;
935 h->client_data = client_data;
936 if (async_event_handler_list.first_handler == NULL)
937 async_event_handler_list.first_handler = h;
938 else
939 async_event_handler_list.last_handler->next_handler = h;
940 async_event_handler_list.last_handler = h;
941 return h;
942 }
943
944 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
945 will be used by gdb_do_one_event. The caller will be whoever
946 created the event source, and wants to signal that the event is
947 ready to be handled. */
948 void
949 mark_async_event_handler (async_event_handler *async_handler_ptr)
950 {
951 async_handler_ptr->ready = 1;
952 }
953
954 /* See event-loop.h. */
955
956 void
957 clear_async_event_handler (async_event_handler *async_handler_ptr)
958 {
959 async_handler_ptr->ready = 0;
960 }
961
962 /* Check if asynchronous event handlers are ready, and call the
963 handler function for one that is. */
964
965 static int
966 check_async_event_handlers (void)
967 {
968 async_event_handler *async_handler_ptr;
969
970 for (async_handler_ptr = async_event_handler_list.first_handler;
971 async_handler_ptr != NULL;
972 async_handler_ptr = async_handler_ptr->next_handler)
973 {
974 if (async_handler_ptr->ready)
975 {
976 async_handler_ptr->ready = 0;
977 (*async_handler_ptr->proc) (async_handler_ptr->client_data);
978 return 1;
979 }
980 }
981
982 return 0;
983 }
984
985 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
986 Free the space allocated for it. */
987 void
988 delete_async_event_handler (async_event_handler **async_handler_ptr)
989 {
990 async_event_handler *prev_ptr;
991
992 if (async_event_handler_list.first_handler == *async_handler_ptr)
993 {
994 async_event_handler_list.first_handler
995 = (*async_handler_ptr)->next_handler;
996 if (async_event_handler_list.first_handler == NULL)
997 async_event_handler_list.last_handler = NULL;
998 }
999 else
1000 {
1001 prev_ptr = async_event_handler_list.first_handler;
1002 while (prev_ptr && prev_ptr->next_handler != *async_handler_ptr)
1003 prev_ptr = prev_ptr->next_handler;
1004 gdb_assert (prev_ptr);
1005 prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
1006 if (async_event_handler_list.last_handler == (*async_handler_ptr))
1007 async_event_handler_list.last_handler = prev_ptr;
1008 }
1009 xfree (*async_handler_ptr);
1010 *async_handler_ptr = NULL;
1011 }
1012
1013 /* Create a timer that will expire in MS milliseconds from now. When
1014 the timer is ready, PROC will be executed. At creation, the timer
1015 is added to the timers queue. This queue is kept sorted in order
1016 of increasing timers. Return a handle to the timer struct. */
1017
1018 int
1019 create_timer (int ms, timer_handler_func *proc,
1020 gdb_client_data client_data)
1021 {
1022 using namespace std::chrono;
1023 struct gdb_timer *timer_ptr, *timer_index, *prev_timer;
1024
1025 steady_clock::time_point time_now = steady_clock::now ();
1026
1027 timer_ptr = new gdb_timer ();
1028 timer_ptr->when = time_now + milliseconds (ms);
1029 timer_ptr->proc = proc;
1030 timer_ptr->client_data = client_data;
1031 timer_list.num_timers++;
1032 timer_ptr->timer_id = timer_list.num_timers;
1033
1034 /* Now add the timer to the timer queue, making sure it is sorted in
1035 increasing order of expiration. */
1036
1037 for (timer_index = timer_list.first_timer;
1038 timer_index != NULL;
1039 timer_index = timer_index->next)
1040 {
1041 if (timer_index->when > timer_ptr->when)
1042 break;
1043 }
1044
1045 if (timer_index == timer_list.first_timer)
1046 {
1047 timer_ptr->next = timer_list.first_timer;
1048 timer_list.first_timer = timer_ptr;
1049
1050 }
1051 else
1052 {
1053 for (prev_timer = timer_list.first_timer;
1054 prev_timer->next != timer_index;
1055 prev_timer = prev_timer->next)
1056 ;
1057
1058 prev_timer->next = timer_ptr;
1059 timer_ptr->next = timer_index;
1060 }
1061
1062 gdb_notifier.timeout_valid = 0;
1063 return timer_ptr->timer_id;
1064 }
1065
1066 /* There is a chance that the creator of the timer wants to get rid of
1067 it before it expires. */
1068 void
1069 delete_timer (int id)
1070 {
1071 struct gdb_timer *timer_ptr, *prev_timer = NULL;
1072
1073 /* Find the entry for the given timer. */
1074
1075 for (timer_ptr = timer_list.first_timer; timer_ptr != NULL;
1076 timer_ptr = timer_ptr->next)
1077 {
1078 if (timer_ptr->timer_id == id)
1079 break;
1080 }
1081
1082 if (timer_ptr == NULL)
1083 return;
1084 /* Get rid of the timer in the timer list. */
1085 if (timer_ptr == timer_list.first_timer)
1086 timer_list.first_timer = timer_ptr->next;
1087 else
1088 {
1089 for (prev_timer = timer_list.first_timer;
1090 prev_timer->next != timer_ptr;
1091 prev_timer = prev_timer->next)
1092 ;
1093 prev_timer->next = timer_ptr->next;
1094 }
1095 delete timer_ptr;
1096
1097 gdb_notifier.timeout_valid = 0;
1098 }
1099
1100 /* Convert a std::chrono duration to a struct timeval. */
1101
1102 template<typename Duration>
1103 static struct timeval
1104 duration_cast_timeval (const Duration &d)
1105 {
1106 using namespace std::chrono;
1107 seconds sec = duration_cast<seconds> (d);
1108 microseconds msec = duration_cast<microseconds> (d - sec);
1109
1110 struct timeval tv;
1111 tv.tv_sec = sec.count ();
1112 tv.tv_usec = msec.count ();
1113 return tv;
1114 }
1115
1116 /* Update the timeout for the select() or poll(). Returns true if the
1117 timer has already expired, false otherwise. */
1118
1119 static int
1120 update_wait_timeout (void)
1121 {
1122 if (timer_list.first_timer != NULL)
1123 {
1124 using namespace std::chrono;
1125 steady_clock::time_point time_now = steady_clock::now ();
1126 struct timeval timeout;
1127
1128 if (timer_list.first_timer->when < time_now)
1129 {
1130 /* It expired already. */
1131 timeout.tv_sec = 0;
1132 timeout.tv_usec = 0;
1133 }
1134 else
1135 {
1136 steady_clock::duration d = timer_list.first_timer->when - time_now;
1137 timeout = duration_cast_timeval (d);
1138 }
1139
1140 /* Update the timeout for select/ poll. */
1141 if (use_poll)
1142 {
1143 #ifdef HAVE_POLL
1144 gdb_notifier.poll_timeout = timeout.tv_sec * 1000;
1145 #else
1146 internal_error (__FILE__, __LINE__,
1147 _("use_poll without HAVE_POLL"));
1148 #endif /* HAVE_POLL */
1149 }
1150 else
1151 {
1152 gdb_notifier.select_timeout.tv_sec = timeout.tv_sec;
1153 gdb_notifier.select_timeout.tv_usec = timeout.tv_usec;
1154 }
1155 gdb_notifier.timeout_valid = 1;
1156
1157 if (timer_list.first_timer->when < time_now)
1158 return 1;
1159 }
1160 else
1161 gdb_notifier.timeout_valid = 0;
1162
1163 return 0;
1164 }
1165
1166 /* Check whether a timer in the timers queue is ready. If a timer is
1167 ready, call its handler and return. Update the timeout for the
1168 select() or poll() as well. Return 1 if an event was handled,
1169 otherwise returns 0.*/
1170
1171 static int
1172 poll_timers (void)
1173 {
1174 if (update_wait_timeout ())
1175 {
1176 struct gdb_timer *timer_ptr = timer_list.first_timer;
1177 timer_handler_func *proc = timer_ptr->proc;
1178 gdb_client_data client_data = timer_ptr->client_data;
1179
1180 /* Get rid of the timer from the beginning of the list. */
1181 timer_list.first_timer = timer_ptr->next;
1182
1183 /* Delete the timer before calling the callback, not after, in
1184 case the callback itself decides to try deleting the timer
1185 too. */
1186 delete timer_ptr;
1187
1188 /* Call the procedure associated with that timer. */
1189 (proc) (client_data);
1190
1191 return 1;
1192 }
1193
1194 return 0;
1195 }
This page took 0.52052 seconds and 4 git commands to generate.