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