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