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