1 /* Event loop machinery for GDB, the GNU debugger.
2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
3 Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "event-loop.h"
22 #include "event-top.h"
26 #if defined (HAVE_POLL_H)
28 #elif defined (HAVE_SYS_POLL_H)
33 #include <sys/types.h>
35 #include "gdb_select.h"
38 /* Tell create_file_handler what events we are interested in.
39 This is used by the select version of the event loop. */
41 #define GDB_READABLE (1<<1)
42 #define GDB_WRITABLE (1<<2)
43 #define GDB_EXCEPTION (1<<3)
45 /* Data point to pass to the event handler. */
46 typedef union event_data
52 typedef struct gdb_event gdb_event
;
53 typedef void (event_handler_func
) (event_data
);
55 /* Event for the GDB event system. Events are queued by calling
56 async_queue_event and serviced later on by gdb_do_one_event. An
57 event can be, for instance, a file descriptor becoming ready to be
58 read. Servicing an event simply means that the procedure PROC will
59 be called. We have 2 queues, one for file handlers that we listen
60 to in the event loop, and one for the file handlers+events that are
61 ready. The procedure PROC associated with each event is dependant
62 of the event source. In the case of monitored file descriptors, it
63 is always the same (handle_file_event). Its duty is to invoke the
64 handler associated with the file descriptor whose state change
65 generated the event, plus doing other cleanups and such. In the
66 case of async signal handlers, it is
67 invoke_async_signal_handler. */
69 typedef struct gdb_event
71 /* Procedure to call to service this event. */
72 event_handler_func
*proc
;
74 /* Data to pass to the event handler. */
78 /* Information about each file descriptor we register with the event
81 typedef struct file_handler
83 int fd
; /* File descriptor. */
84 int mask
; /* Events we want to monitor: POLLIN, etc. */
85 int ready_mask
; /* Events that have been seen since
87 handler_func
*proc
; /* Procedure to call when fd is ready. */
88 gdb_client_data client_data
; /* Argument to pass to proc. */
89 int error
; /* Was an error detected on this fd? */
90 struct file_handler
*next_file
; /* Next registered file descriptor. */
94 /* PROC is a function to be invoked when the READY flag is set. This
95 happens when there has been a signal and the corresponding signal
96 handler has 'triggered' this async_signal_handler for execution.
97 The actual work to be done in response to a signal will be carried
98 out by PROC at a later time, within process_event. This provides a
99 deferred execution of signal handlers.
101 Async_init_signals takes care of setting up such an
102 async_signal_handler for each interesting signal. */
104 typedef struct async_signal_handler
106 int ready
; /* If ready, call this handler
107 from the main event loop, using
108 invoke_async_handler. */
109 struct async_signal_handler
*next_handler
; /* Ptr to next handler. */
110 sig_handler_func
*proc
; /* Function to call to do the work. */
111 gdb_client_data client_data
; /* Argument to async_handler_func. */
113 async_signal_handler
;
115 /* PROC is a function to be invoked when the READY flag is set. This
116 happens when the event has been marked with
117 MARK_ASYNC_EVENT_HANDLER. The actual work to be done in response
118 to an event will be carried out by PROC at a later time, within
119 process_event. This provides a deferred execution of event
121 typedef struct async_event_handler
123 /* If ready, call this handler from the main event loop, using
124 invoke_event_handler. */
127 /* Point to next handler. */
128 struct async_event_handler
*next_handler
;
130 /* Function to call to do the work. */
131 async_event_handler_func
*proc
;
133 /* Argument to PROC. */
134 gdb_client_data client_data
;
138 /* Gdb_notifier is just a list of file descriptors gdb is interested in.
139 These are the input file descriptor, and the target file
140 descriptor. We have two flavors of the notifier, one for platforms
141 that have the POLL function, the other for those that don't, and
142 only support SELECT. Each of the elements in the gdb_notifier list is
143 basically a description of what kind of events gdb is interested
146 /* As of 1999-04-30 only the input file descriptor is registered with the
149 /* Do we use poll or select ? */
154 #endif /* HAVE_POLL */
156 static unsigned char use_poll
= USE_POLL
;
165 /* Ptr to head of file handler list. */
166 file_handler
*first_file_handler
;
168 /* Next file handler to handle, for the select variant. To level
169 the fairness across event sources, we serve file handlers in a
170 round-robin-like fashion. The number and order of the polled
171 file handlers may change between invocations, but this is good
173 file_handler
*next_file_handler
;
176 /* Ptr to array of pollfd structures. */
177 struct pollfd
*poll_fds
;
179 /* Next file descriptor to handle, for the poll variant. To level
180 the fairness across event sources, we poll the file descriptors
181 in a round-robin-like fashion. The number and order of the
182 polled file descriptors may change between invocations, but
183 this is good enough. */
184 int next_poll_fds_index
;
186 /* Timeout in milliseconds for calls to poll(). */
190 /* Masks to be used in the next call to select.
191 Bits are set in response to calls to create_file_handler. */
192 fd_set check_masks
[3];
194 /* What file descriptors were found ready by select. */
195 fd_set ready_masks
[3];
197 /* Number of file descriptors to monitor (for poll). */
198 /* Number of valid bits (highest fd value + 1) (for select). */
201 /* Time structure for calls to select(). */
202 struct timeval select_timeout
;
204 /* Flag to tell whether the timeout should be used. */
209 /* Structure associated with a timer. PROC will be executed at the
210 first occasion after WHEN. */
215 struct gdb_timer
*next
;
216 timer_handler_func
*proc
; /* Function to call to do the work. */
217 gdb_client_data client_data
; /* Argument to async_handler_func. */
220 /* List of currently active timers. It is sorted in order of
221 increasing timers. */
224 /* Pointer to first in timer list. */
225 struct gdb_timer
*first_timer
;
227 /* Id of the last timer created. */
232 /* All the async_signal_handlers gdb is interested in are kept onto
236 /* Pointer to first in handler list. */
237 async_signal_handler
*first_handler
;
239 /* Pointer to last in handler list. */
240 async_signal_handler
*last_handler
;
244 /* All the async_event_handlers gdb is interested in are kept onto
248 /* Pointer to first in handler list. */
249 async_event_handler
*first_handler
;
251 /* Pointer to last in handler list. */
252 async_event_handler
*last_handler
;
254 async_event_handler_list
;
256 static int invoke_async_signal_handlers (void);
257 static void create_file_handler (int fd
, int mask
, handler_func
*proc
,
258 gdb_client_data client_data
);
259 static int check_async_event_handlers (void);
260 static int gdb_wait_for_event (int);
261 static int update_wait_timeout (void);
262 static int poll_timers (void);
265 /* Process one high level event. If nothing is ready at this time,
266 wait for something to happen (via gdb_wait_for_event), then process
267 it. Returns >0 if something was done otherwise returns <0 (this
268 can happen if there are no event sources to wait for). */
271 gdb_do_one_event (void)
273 static int event_source_head
= 0;
274 const int number_of_sources
= 3;
277 /* First let's see if there are any asynchronous signal handlers
278 that are ready. These would be the result of invoking any of the
280 if (invoke_async_signal_handlers ())
283 /* To level the fairness across event sources, we poll them in a
284 round-robin fashion. */
285 for (current
= 0; current
< number_of_sources
; current
++)
289 switch (event_source_head
)
292 /* Are any timers that are ready? */
293 res
= poll_timers ();
296 /* Are there events already waiting to be collected on the
297 monitored file descriptors? */
298 res
= gdb_wait_for_event (0);
301 /* Are there any asynchronous event handlers ready? */
302 res
= check_async_event_handlers ();
305 internal_error (__FILE__
, __LINE__
,
306 "unexpected event_source_head %d",
311 if (event_source_head
== number_of_sources
)
312 event_source_head
= 0;
318 /* Block waiting for a new event. If gdb_wait_for_event returns -1,
319 we should get out because this means that there are no event
320 sources left. This will make the event loop stop, and the
323 if (gdb_wait_for_event (1) < 0)
326 /* If gdb_wait_for_event has returned 1, it means that one event has
327 been handled. We break out of the loop. */
331 /* Start up the event loop. This is the entry point to the event loop
332 from the command loop. */
335 start_event_loop (void)
337 /* Loop until there is nothing to do. This is the entry point to
338 the event loop engine. gdb_do_one_event will process one event
339 for each invocation. It blocks waiting for an event and then
347 result
= gdb_do_one_event ();
349 CATCH (ex
, RETURN_MASK_ALL
)
351 exception_print (gdb_stderr
, ex
);
353 /* If any exception escaped to here, we better enable
354 stdin. Otherwise, any command that calls async_disable_stdin,
355 and then throws, will leave stdin inoperable. */
356 async_enable_stdin ();
357 /* If we long-jumped out of do_one_event, we probably didn't
358 get around to resetting the prompt, which leaves readline
359 in a messed-up state. Reset it here. */
360 observer_notify_command_error ();
361 /* This call looks bizarre, but it is required. If the user
362 entered a command that caused an error,
363 after_char_processing_hook won't be called from
364 rl_callback_read_char_wrapper. Using a cleanup there
365 won't work, since we want this function to be called
366 after a new prompt is printed. */
367 if (after_char_processing_hook
)
368 (*after_char_processing_hook
) ();
369 /* Maybe better to set a flag to be checked somewhere as to
370 whether display the prompt or not. */
378 /* We are done with the event loop. There are no more event sources
379 to listen to. So we exit GDB. */
384 /* Wrapper function for create_file_handler, so that the caller
385 doesn't have to know implementation details about the use of poll
388 add_file_handler (int fd
, handler_func
* proc
, gdb_client_data client_data
)
397 /* Check to see if poll () is usable. If not, we'll switch to
398 use select. This can happen on systems like
399 m68k-motorola-sys, `poll' cannot be used to wait for `stdin'.
400 On m68k-motorola-sysv, tty's are not stream-based and not
404 if (poll (&fds
, 1, 0) == 1 && (fds
.revents
& POLLNVAL
))
407 internal_error (__FILE__
, __LINE__
,
408 _("use_poll without HAVE_POLL"));
409 #endif /* HAVE_POLL */
414 create_file_handler (fd
, POLLIN
, proc
, client_data
);
416 internal_error (__FILE__
, __LINE__
,
417 _("use_poll without HAVE_POLL"));
421 create_file_handler (fd
, GDB_READABLE
| GDB_EXCEPTION
,
425 /* Add a file handler/descriptor to the list of descriptors we are
428 FD is the file descriptor for the file/stream to be listened to.
430 For the poll case, MASK is a combination (OR) of POLLIN,
431 POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND:
432 these are the events we are interested in. If any of them occurs,
433 proc should be called.
435 For the select case, MASK is a combination of READABLE, WRITABLE,
436 EXCEPTION. PROC is the procedure that will be called when an event
437 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
440 create_file_handler (int fd
, int mask
, handler_func
* proc
,
441 gdb_client_data client_data
)
443 file_handler
*file_ptr
;
445 /* Do we already have a file handler for this file? (We may be
446 changing its associated procedure). */
447 for (file_ptr
= gdb_notifier
.first_file_handler
; file_ptr
!= NULL
;
448 file_ptr
= file_ptr
->next_file
)
450 if (file_ptr
->fd
== fd
)
454 /* It is a new file descriptor. Add it to the list. Otherwise, just
455 change the data associated with it. */
456 if (file_ptr
== NULL
)
458 file_ptr
= (file_handler
*) xmalloc (sizeof (file_handler
));
460 file_ptr
->ready_mask
= 0;
461 file_ptr
->next_file
= gdb_notifier
.first_file_handler
;
462 gdb_notifier
.first_file_handler
= file_ptr
;
467 gdb_notifier
.num_fds
++;
468 if (gdb_notifier
.poll_fds
)
469 gdb_notifier
.poll_fds
=
470 (struct pollfd
*) xrealloc (gdb_notifier
.poll_fds
,
471 (gdb_notifier
.num_fds
472 * sizeof (struct pollfd
)));
474 gdb_notifier
.poll_fds
=
475 (struct pollfd
*) xmalloc (sizeof (struct pollfd
));
476 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->fd
= fd
;
477 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->events
= mask
;
478 (gdb_notifier
.poll_fds
+ gdb_notifier
.num_fds
- 1)->revents
= 0;
480 internal_error (__FILE__
, __LINE__
,
481 _("use_poll without HAVE_POLL"));
482 #endif /* HAVE_POLL */
486 if (mask
& GDB_READABLE
)
487 FD_SET (fd
, &gdb_notifier
.check_masks
[0]);
489 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
491 if (mask
& GDB_WRITABLE
)
492 FD_SET (fd
, &gdb_notifier
.check_masks
[1]);
494 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
496 if (mask
& GDB_EXCEPTION
)
497 FD_SET (fd
, &gdb_notifier
.check_masks
[2]);
499 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
501 if (gdb_notifier
.num_fds
<= fd
)
502 gdb_notifier
.num_fds
= fd
+ 1;
506 file_ptr
->proc
= proc
;
507 file_ptr
->client_data
= client_data
;
508 file_ptr
->mask
= mask
;
511 /* Return the next file handler to handle, and advance to the next
512 file handler, wrapping around if the end of the list is
515 static file_handler
*
516 get_next_file_handler_to_handle_and_advance (void)
518 file_handler
*curr_next
;
520 /* The first time around, this is still NULL. */
521 if (gdb_notifier
.next_file_handler
== NULL
)
522 gdb_notifier
.next_file_handler
= gdb_notifier
.first_file_handler
;
524 curr_next
= gdb_notifier
.next_file_handler
;
525 gdb_assert (curr_next
!= NULL
);
528 gdb_notifier
.next_file_handler
= curr_next
->next_file
;
529 /* Wrap around, if necessary. */
530 if (gdb_notifier
.next_file_handler
== NULL
)
531 gdb_notifier
.next_file_handler
= gdb_notifier
.first_file_handler
;
536 /* Remove the file descriptor FD from the list of monitored fd's:
537 i.e. we don't care anymore about events on the FD. */
539 delete_file_handler (int fd
)
541 file_handler
*file_ptr
, *prev_ptr
= NULL
;
545 struct pollfd
*new_poll_fds
;
548 /* Find the entry for the given file. */
550 for (file_ptr
= gdb_notifier
.first_file_handler
; file_ptr
!= NULL
;
551 file_ptr
= file_ptr
->next_file
)
553 if (file_ptr
->fd
== fd
)
557 if (file_ptr
== NULL
)
563 /* Create a new poll_fds array by copying every fd's information
564 but the one we want to get rid of. */
566 new_poll_fds
= (struct pollfd
*)
567 xmalloc ((gdb_notifier
.num_fds
- 1) * sizeof (struct pollfd
));
569 for (i
= 0, j
= 0; i
< gdb_notifier
.num_fds
; i
++)
571 if ((gdb_notifier
.poll_fds
+ i
)->fd
!= fd
)
573 (new_poll_fds
+ j
)->fd
= (gdb_notifier
.poll_fds
+ i
)->fd
;
574 (new_poll_fds
+ j
)->events
= (gdb_notifier
.poll_fds
+ i
)->events
;
575 (new_poll_fds
+ j
)->revents
576 = (gdb_notifier
.poll_fds
+ i
)->revents
;
580 xfree (gdb_notifier
.poll_fds
);
581 gdb_notifier
.poll_fds
= new_poll_fds
;
582 gdb_notifier
.num_fds
--;
584 internal_error (__FILE__
, __LINE__
,
585 _("use_poll without HAVE_POLL"));
586 #endif /* HAVE_POLL */
590 if (file_ptr
->mask
& GDB_READABLE
)
591 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
592 if (file_ptr
->mask
& GDB_WRITABLE
)
593 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
594 if (file_ptr
->mask
& GDB_EXCEPTION
)
595 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
597 /* Find current max fd. */
599 if ((fd
+ 1) == gdb_notifier
.num_fds
)
601 gdb_notifier
.num_fds
--;
602 for (i
= gdb_notifier
.num_fds
; i
; i
--)
604 if (FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[0])
605 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[1])
606 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[2]))
609 gdb_notifier
.num_fds
= i
;
613 /* Deactivate the file descriptor, by clearing its mask,
614 so that it will not fire again. */
618 /* If this file handler was going to be the next one to be handled,
619 advance to the next's next, if any. */
620 if (gdb_notifier
.next_file_handler
== file_ptr
)
622 if (file_ptr
->next_file
== NULL
623 && file_ptr
== gdb_notifier
.first_file_handler
)
624 gdb_notifier
.next_file_handler
= NULL
;
626 get_next_file_handler_to_handle_and_advance ();
629 /* Get rid of the file handler in the file handler list. */
630 if (file_ptr
== gdb_notifier
.first_file_handler
)
631 gdb_notifier
.first_file_handler
= file_ptr
->next_file
;
634 for (prev_ptr
= gdb_notifier
.first_file_handler
;
635 prev_ptr
->next_file
!= file_ptr
;
636 prev_ptr
= prev_ptr
->next_file
)
638 prev_ptr
->next_file
= file_ptr
->next_file
;
643 /* Handle the given event by calling the procedure associated to the
644 corresponding file handler. */
647 handle_file_event (file_handler
*file_ptr
, int ready_mask
)
656 /* With poll, the ready_mask could have any of three events
657 set to 1: POLLHUP, POLLERR, POLLNVAL. These events
658 cannot be used in the requested event mask (events), but
659 they can be returned in the return mask (revents). We
660 need to check for those event too, and add them to the
661 mask which will be passed to the handler. */
663 /* See if the desired events (mask) match the received
664 events (ready_mask). */
669 /* POLLHUP means EOF, but can be combined with POLLIN to
670 signal more data to read. */
671 error_mask
= POLLHUP
| POLLERR
| POLLNVAL
;
672 mask
= ready_mask
& (file_ptr
->mask
| error_mask
);
674 if ((mask
& (POLLERR
| POLLNVAL
)) != 0)
676 /* Work in progress. We may need to tell somebody
677 what kind of error we had. */
679 printf_unfiltered (_("Error detected on fd %d\n"),
682 printf_unfiltered (_("Invalid or non-`poll'able fd %d\n"),
689 internal_error (__FILE__
, __LINE__
,
690 _("use_poll without HAVE_POLL"));
691 #endif /* HAVE_POLL */
695 if (ready_mask
& GDB_EXCEPTION
)
697 printf_unfiltered (_("Exception condition detected "
698 "on fd %d\n"), file_ptr
->fd
);
703 mask
= ready_mask
& file_ptr
->mask
;
706 /* If there was a match, then call the handler. */
708 (*file_ptr
->proc
) (file_ptr
->error
, file_ptr
->client_data
);
713 /* Wait for new events on the monitored file descriptors. Run the
714 event handler if the first descriptor that is detected by the poll.
715 If BLOCK and if there are no events, this function will block in
716 the call to poll. Return 1 if an event was handled. Return -1 if
717 there are no file descriptors to monitor. Return 1 if an event was
718 handled, otherwise returns 0. */
721 gdb_wait_for_event (int block
)
723 file_handler
*file_ptr
;
726 /* Make sure all output is done before getting another event. */
727 gdb_flush (gdb_stdout
);
728 gdb_flush (gdb_stderr
);
730 if (gdb_notifier
.num_fds
== 0)
734 update_wait_timeout ();
742 timeout
= gdb_notifier
.timeout_valid
? gdb_notifier
.poll_timeout
: -1;
746 num_found
= poll (gdb_notifier
.poll_fds
,
747 (unsigned long) gdb_notifier
.num_fds
, timeout
);
749 /* Don't print anything if we get out of poll because of a
751 if (num_found
== -1 && errno
!= EINTR
)
752 perror_with_name (("poll"));
754 internal_error (__FILE__
, __LINE__
,
755 _("use_poll without HAVE_POLL"));
756 #endif /* HAVE_POLL */
760 struct timeval select_timeout
;
761 struct timeval
*timeout_p
;
764 timeout_p
= gdb_notifier
.timeout_valid
765 ? &gdb_notifier
.select_timeout
: NULL
;
768 memset (&select_timeout
, 0, sizeof (select_timeout
));
769 timeout_p
= &select_timeout
;
772 gdb_notifier
.ready_masks
[0] = gdb_notifier
.check_masks
[0];
773 gdb_notifier
.ready_masks
[1] = gdb_notifier
.check_masks
[1];
774 gdb_notifier
.ready_masks
[2] = gdb_notifier
.check_masks
[2];
775 num_found
= gdb_select (gdb_notifier
.num_fds
,
776 &gdb_notifier
.ready_masks
[0],
777 &gdb_notifier
.ready_masks
[1],
778 &gdb_notifier
.ready_masks
[2],
781 /* Clear the masks after an error from select. */
784 FD_ZERO (&gdb_notifier
.ready_masks
[0]);
785 FD_ZERO (&gdb_notifier
.ready_masks
[1]);
786 FD_ZERO (&gdb_notifier
.ready_masks
[2]);
788 /* Dont print anything if we got a signal, let gdb handle
791 perror_with_name (("select"));
795 /* Avoid looking at poll_fds[i]->revents if no event fired. */
799 /* Run event handlers. We always run just one handler and go back
800 to polling, in case a handler changes the notifier list. Since
801 events for sources we haven't consumed yet wake poll/select
802 immediately, no event is lost. */
804 /* To level the fairness across event descriptors, we handle them in
805 a round-robin-like fashion. The number and order of descriptors
806 may change between invocations, but this is good enough. */
815 if (gdb_notifier
.next_poll_fds_index
>= gdb_notifier
.num_fds
)
816 gdb_notifier
.next_poll_fds_index
= 0;
817 i
= gdb_notifier
.next_poll_fds_index
++;
819 gdb_assert (i
< gdb_notifier
.num_fds
);
820 if ((gdb_notifier
.poll_fds
+ i
)->revents
)
824 for (file_ptr
= gdb_notifier
.first_file_handler
;
826 file_ptr
= file_ptr
->next_file
)
828 if (file_ptr
->fd
== (gdb_notifier
.poll_fds
+ i
)->fd
)
831 gdb_assert (file_ptr
!= NULL
);
833 mask
= (gdb_notifier
.poll_fds
+ i
)->revents
;
834 handle_file_event (file_ptr
, mask
);
837 internal_error (__FILE__
, __LINE__
,
838 _("use_poll without HAVE_POLL"));
839 #endif /* HAVE_POLL */
843 /* See comment about even source fairness above. */
848 file_ptr
= get_next_file_handler_to_handle_and_advance ();
850 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[0]))
851 mask
|= GDB_READABLE
;
852 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[1]))
853 mask
|= GDB_WRITABLE
;
854 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[2]))
855 mask
|= GDB_EXCEPTION
;
859 handle_file_event (file_ptr
, mask
);
866 /* Create an asynchronous handler, allocating memory for it.
867 Return a pointer to the newly created handler.
868 This pointer will be used to invoke the handler by
869 invoke_async_signal_handler.
870 PROC is the function to call with CLIENT_DATA argument
871 whenever the handler is invoked. */
872 async_signal_handler
*
873 create_async_signal_handler (sig_handler_func
* proc
,
874 gdb_client_data client_data
)
876 async_signal_handler
*async_handler_ptr
;
879 (async_signal_handler
*) xmalloc (sizeof (async_signal_handler
));
880 async_handler_ptr
->ready
= 0;
881 async_handler_ptr
->next_handler
= NULL
;
882 async_handler_ptr
->proc
= proc
;
883 async_handler_ptr
->client_data
= client_data
;
884 if (sighandler_list
.first_handler
== NULL
)
885 sighandler_list
.first_handler
= async_handler_ptr
;
887 sighandler_list
.last_handler
->next_handler
= async_handler_ptr
;
888 sighandler_list
.last_handler
= async_handler_ptr
;
889 return async_handler_ptr
;
892 /* Call the handler from HANDLER immediately. This function runs
893 signal handlers when returning to the event loop would be too
896 call_async_signal_handler (struct async_signal_handler
*handler
)
898 (*handler
->proc
) (handler
->client_data
);
901 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
902 will be used when the handlers are invoked, after we have waited
903 for some event. The caller of this function is the interrupt
904 handler associated with a signal. */
906 mark_async_signal_handler (async_signal_handler
* async_handler_ptr
)
908 async_handler_ptr
->ready
= 1;
911 /* Call all the handlers that are ready. Returns true if any was
914 invoke_async_signal_handlers (void)
916 async_signal_handler
*async_handler_ptr
;
919 /* Invoke ready handlers. */
923 for (async_handler_ptr
= sighandler_list
.first_handler
;
924 async_handler_ptr
!= NULL
;
925 async_handler_ptr
= async_handler_ptr
->next_handler
)
927 if (async_handler_ptr
->ready
)
930 if (async_handler_ptr
== NULL
)
933 async_handler_ptr
->ready
= 0;
934 (*async_handler_ptr
->proc
) (async_handler_ptr
->client_data
);
940 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
941 Free the space allocated for it. */
943 delete_async_signal_handler (async_signal_handler
** async_handler_ptr
)
945 async_signal_handler
*prev_ptr
;
947 if (sighandler_list
.first_handler
== (*async_handler_ptr
))
949 sighandler_list
.first_handler
= (*async_handler_ptr
)->next_handler
;
950 if (sighandler_list
.first_handler
== NULL
)
951 sighandler_list
.last_handler
= NULL
;
955 prev_ptr
= sighandler_list
.first_handler
;
956 while (prev_ptr
&& prev_ptr
->next_handler
!= (*async_handler_ptr
))
957 prev_ptr
= prev_ptr
->next_handler
;
958 gdb_assert (prev_ptr
);
959 prev_ptr
->next_handler
= (*async_handler_ptr
)->next_handler
;
960 if (sighandler_list
.last_handler
== (*async_handler_ptr
))
961 sighandler_list
.last_handler
= prev_ptr
;
963 xfree ((*async_handler_ptr
));
964 (*async_handler_ptr
) = NULL
;
967 /* Create an asynchronous event handler, allocating memory for it.
968 Return a pointer to the newly created handler. PROC is the
969 function to call with CLIENT_DATA argument whenever the handler is
971 async_event_handler
*
972 create_async_event_handler (async_event_handler_func
*proc
,
973 gdb_client_data client_data
)
975 async_event_handler
*h
;
977 h
= xmalloc (sizeof (*h
));
979 h
->next_handler
= NULL
;
981 h
->client_data
= client_data
;
982 if (async_event_handler_list
.first_handler
== NULL
)
983 async_event_handler_list
.first_handler
= h
;
985 async_event_handler_list
.last_handler
->next_handler
= h
;
986 async_event_handler_list
.last_handler
= h
;
990 /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
991 will be used by gdb_do_one_event. The caller will be whoever
992 created the event source, and wants to signal that the event is
993 ready to be handled. */
995 mark_async_event_handler (async_event_handler
*async_handler_ptr
)
997 async_handler_ptr
->ready
= 1;
1000 /* See event-loop.h. */
1003 clear_async_event_handler (async_event_handler
*async_handler_ptr
)
1005 async_handler_ptr
->ready
= 0;
1008 /* Check if asynchronous event handlers are ready, and call the
1009 handler function for one that is. */
1012 check_async_event_handlers (void)
1014 async_event_handler
*async_handler_ptr
;
1016 for (async_handler_ptr
= async_event_handler_list
.first_handler
;
1017 async_handler_ptr
!= NULL
;
1018 async_handler_ptr
= async_handler_ptr
->next_handler
)
1020 if (async_handler_ptr
->ready
)
1022 async_handler_ptr
->ready
= 0;
1023 (*async_handler_ptr
->proc
) (async_handler_ptr
->client_data
);
1031 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
1032 Free the space allocated for it. */
1034 delete_async_event_handler (async_event_handler
**async_handler_ptr
)
1036 async_event_handler
*prev_ptr
;
1038 if (async_event_handler_list
.first_handler
== *async_handler_ptr
)
1040 async_event_handler_list
.first_handler
1041 = (*async_handler_ptr
)->next_handler
;
1042 if (async_event_handler_list
.first_handler
== NULL
)
1043 async_event_handler_list
.last_handler
= NULL
;
1047 prev_ptr
= async_event_handler_list
.first_handler
;
1048 while (prev_ptr
&& prev_ptr
->next_handler
!= *async_handler_ptr
)
1049 prev_ptr
= prev_ptr
->next_handler
;
1050 gdb_assert (prev_ptr
);
1051 prev_ptr
->next_handler
= (*async_handler_ptr
)->next_handler
;
1052 if (async_event_handler_list
.last_handler
== (*async_handler_ptr
))
1053 async_event_handler_list
.last_handler
= prev_ptr
;
1055 xfree (*async_handler_ptr
);
1056 *async_handler_ptr
= NULL
;
1059 /* Create a timer that will expire in MILLISECONDS from now. When the
1060 timer is ready, PROC will be executed. At creation, the timer is
1061 aded to the timers queue. This queue is kept sorted in order of
1062 increasing timers. Return a handle to the timer struct. */
1064 create_timer (int milliseconds
, timer_handler_func
* proc
,
1065 gdb_client_data client_data
)
1067 struct gdb_timer
*timer_ptr
, *timer_index
, *prev_timer
;
1068 struct timeval time_now
, delta
;
1070 /* Compute seconds. */
1071 delta
.tv_sec
= milliseconds
/ 1000;
1072 /* Compute microseconds. */
1073 delta
.tv_usec
= (milliseconds
% 1000) * 1000;
1075 gettimeofday (&time_now
, NULL
);
1077 timer_ptr
= (struct gdb_timer
*) xmalloc (sizeof (*timer_ptr
));
1078 timer_ptr
->when
.tv_sec
= time_now
.tv_sec
+ delta
.tv_sec
;
1079 timer_ptr
->when
.tv_usec
= time_now
.tv_usec
+ delta
.tv_usec
;
1081 if (timer_ptr
->when
.tv_usec
>= 1000000)
1083 timer_ptr
->when
.tv_sec
+= 1;
1084 timer_ptr
->when
.tv_usec
-= 1000000;
1086 timer_ptr
->proc
= proc
;
1087 timer_ptr
->client_data
= client_data
;
1088 timer_list
.num_timers
++;
1089 timer_ptr
->timer_id
= timer_list
.num_timers
;
1091 /* Now add the timer to the timer queue, making sure it is sorted in
1092 increasing order of expiration. */
1094 for (timer_index
= timer_list
.first_timer
;
1095 timer_index
!= NULL
;
1096 timer_index
= timer_index
->next
)
1098 /* If the seconds field is greater or if it is the same, but the
1099 microsecond field is greater. */
1100 if ((timer_index
->when
.tv_sec
> timer_ptr
->when
.tv_sec
)
1101 || ((timer_index
->when
.tv_sec
== timer_ptr
->when
.tv_sec
)
1102 && (timer_index
->when
.tv_usec
> timer_ptr
->when
.tv_usec
)))
1106 if (timer_index
== timer_list
.first_timer
)
1108 timer_ptr
->next
= timer_list
.first_timer
;
1109 timer_list
.first_timer
= timer_ptr
;
1114 for (prev_timer
= timer_list
.first_timer
;
1115 prev_timer
->next
!= timer_index
;
1116 prev_timer
= prev_timer
->next
)
1119 prev_timer
->next
= timer_ptr
;
1120 timer_ptr
->next
= timer_index
;
1123 gdb_notifier
.timeout_valid
= 0;
1124 return timer_ptr
->timer_id
;
1127 /* There is a chance that the creator of the timer wants to get rid of
1128 it before it expires. */
1130 delete_timer (int id
)
1132 struct gdb_timer
*timer_ptr
, *prev_timer
= NULL
;
1134 /* Find the entry for the given timer. */
1136 for (timer_ptr
= timer_list
.first_timer
; timer_ptr
!= NULL
;
1137 timer_ptr
= timer_ptr
->next
)
1139 if (timer_ptr
->timer_id
== id
)
1143 if (timer_ptr
== NULL
)
1145 /* Get rid of the timer in the timer list. */
1146 if (timer_ptr
== timer_list
.first_timer
)
1147 timer_list
.first_timer
= timer_ptr
->next
;
1150 for (prev_timer
= timer_list
.first_timer
;
1151 prev_timer
->next
!= timer_ptr
;
1152 prev_timer
= prev_timer
->next
)
1154 prev_timer
->next
= timer_ptr
->next
;
1158 gdb_notifier
.timeout_valid
= 0;
1161 /* Update the timeout for the select() or poll(). Returns true if the
1162 timer has already expired, false otherwise. */
1165 update_wait_timeout (void)
1167 struct timeval time_now
, delta
;
1169 if (timer_list
.first_timer
!= NULL
)
1171 gettimeofday (&time_now
, NULL
);
1172 delta
.tv_sec
= timer_list
.first_timer
->when
.tv_sec
- time_now
.tv_sec
;
1173 delta
.tv_usec
= timer_list
.first_timer
->when
.tv_usec
- time_now
.tv_usec
;
1175 if (delta
.tv_usec
< 0)
1178 delta
.tv_usec
+= 1000000;
1181 /* Cannot simply test if delta.tv_sec is negative because time_t
1182 might be unsigned. */
1183 if (timer_list
.first_timer
->when
.tv_sec
< time_now
.tv_sec
1184 || (timer_list
.first_timer
->when
.tv_sec
== time_now
.tv_sec
1185 && timer_list
.first_timer
->when
.tv_usec
< time_now
.tv_usec
))
1187 /* It expired already. */
1192 /* Update the timeout for select/ poll. */
1196 gdb_notifier
.poll_timeout
= delta
.tv_sec
* 1000;
1198 internal_error (__FILE__
, __LINE__
,
1199 _("use_poll without HAVE_POLL"));
1200 #endif /* HAVE_POLL */
1204 gdb_notifier
.select_timeout
.tv_sec
= delta
.tv_sec
;
1205 gdb_notifier
.select_timeout
.tv_usec
= delta
.tv_usec
;
1207 gdb_notifier
.timeout_valid
= 1;
1209 if (delta
.tv_sec
== 0 && delta
.tv_usec
== 0)
1213 gdb_notifier
.timeout_valid
= 0;
1218 /* Check whether a timer in the timers queue is ready. If a timer is
1219 ready, call its handler and return. Update the timeout for the
1220 select() or poll() as well. Return 1 if an event was handled,
1221 otherwise returns 0.*/
1226 if (update_wait_timeout ())
1228 struct gdb_timer
*timer_ptr
= timer_list
.first_timer
;
1229 timer_handler_func
*proc
= timer_ptr
->proc
;
1230 gdb_client_data client_data
= timer_ptr
->client_data
;
1232 /* Get rid of the timer from the beginning of the list. */
1233 timer_list
.first_timer
= timer_ptr
->next
;
1235 /* Delete the timer before calling the callback, not after, in
1236 case the callback itself decides to try deleting the timer
1240 /* Call the procedure associated with that timer. */
1241 (proc
) (client_data
);