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