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