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