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