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