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