Commit | Line | Data |
---|---|---|
b5a0ac70 SS |
1 | /* Event loop machinery for GDB, the GNU debugger. |
2 | Copyright 1999 Free Software Foundation, Inc. | |
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 | |
9 | the Free Software Foundation; either version 2 of the License, or | |
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 | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
b5a0ac70 | 21 | #include "defs.h" |
9e0b60a8 JM |
22 | #include "top.h" |
23 | #include "event-loop.h" | |
b5a0ac70 | 24 | #ifdef HAVE_POLL |
9e0b60a8 JM |
25 | #include <poll.h> |
26 | #else | |
27 | #include <sys/types.h> | |
b5a0ac70 SS |
28 | #endif |
29 | #include <errno.h> | |
9e0b60a8 | 30 | #include <setjmp.h> |
b5a0ac70 SS |
31 | |
32 | /* Event queue: | |
33 | - the first event in the queue is the head of the queue. | |
34 | It will be the next to be serviced. | |
35 | - the last event in the queue | |
36 | ||
37 | Events can be inserted at the front of the queue or at the end of | |
38 | the queue. Events will be extracted from the queue for processing | |
39 | starting from the head. Therefore, events inserted at the head of | |
40 | the queue will be processed in a last in first out fashoin, while | |
41 | those inserted at the tail of the queue will be processed in a first | |
42 | in first out manner. All the fields are NULL if the queue is | |
43 | empty. */ | |
44 | ||
45 | static struct | |
46 | { | |
47 | gdb_event *first_event; /* First pending event */ | |
48 | gdb_event *last_event; /* Last pending event */ | |
49 | } | |
50 | event_queue; | |
51 | ||
52 | /* Gdb_notifier is just a list of file descriptors gdb is interested in. | |
53 | These are the input file descriptor, and the target file | |
54 | descriptor. We have two flavors of the notifier, one for platforms | |
55 | that have the POLL function, the other for those that don't, and | |
56 | only support SELECT. Each of the elements in the gdb_notifier list is | |
57 | basically a description of what kind of events gdb is interested | |
58 | in, for each fd. */ | |
59 | ||
392a587b | 60 | /* As of 1999-04-30 only the input file descriptor is registered with the |
b5a0ac70 SS |
61 | event loop. */ |
62 | ||
63 | #ifdef HAVE_POLL | |
64 | /* Poll based implementation of the notifier. */ | |
65 | ||
66 | static struct | |
67 | { | |
68 | /* Ptr to head of file handler list. */ | |
69 | file_handler *first_file_handler; | |
70 | ||
71 | /* Ptr to array of pollfd structures. */ | |
72 | struct pollfd *poll_fds; | |
73 | ||
74 | /* Number of file descriptors to monitor. */ | |
75 | int num_fds; | |
76 | ||
77 | } | |
78 | gdb_notifier; | |
79 | ||
80 | #else /* ! HAVE_POLL */ | |
81 | ||
82 | /* Select based implementation of the notifier. */ | |
83 | ||
84 | static struct | |
85 | { | |
86 | /* Ptr to head of file handler list. */ | |
87 | file_handler *first_file_handler; | |
88 | ||
89 | /* Masks to be used in the next call to select. | |
90 | Bits are set in response to calls to create_file_handler. */ | |
91 | fd_mask check_masks[3 * MASK_SIZE]; | |
92 | ||
93 | /* What file descriptors were found ready by select. */ | |
94 | fd_mask ready_masks[3 * MASK_SIZE]; | |
95 | ||
96 | /* Number of valid bits (highest fd value + 1). */ | |
97 | int num_fds; | |
98 | ||
99 | } | |
100 | gdb_notifier; | |
101 | ||
102 | #endif /* HAVE_POLL */ | |
103 | ||
104 | /* All the async_signal_handlers gdb is interested in are kept onto | |
105 | this list. */ | |
106 | static struct | |
107 | { | |
108 | /* Pointer to first in handler list. */ | |
109 | async_signal_handler *first_handler; | |
110 | ||
111 | /* Pointer to last in handler list. */ | |
112 | async_signal_handler *last_handler; | |
113 | } | |
114 | sighandler_list; | |
115 | ||
116 | /* Is any of the handlers ready? Check this variable using | |
117 | check_async_ready. This is used by process_event, to determine | |
118 | whether or not to invoke the invoke_async_signal_handler | |
119 | function. */ | |
120 | static int async_handler_ready = 0; | |
121 | ||
085dd6e6 | 122 | static void create_file_handler PARAMS ((int, int, file_handler_func *, gdb_client_data)); |
b5a0ac70 SS |
123 | static void invoke_async_signal_handler PARAMS ((void)); |
124 | static int gdb_wait_for_event PARAMS ((void)); | |
085dd6e6 | 125 | static int gdb_do_one_event PARAMS ((void)); |
b5a0ac70 | 126 | static int check_async_ready PARAMS ((void)); |
b5a0ac70 SS |
127 | \f |
128 | ||
129 | /* Insert an event object into the gdb event queue at | |
130 | the specified position. | |
131 | POSITION can be head or tail, with values TAIL, HEAD. | |
132 | EVENT_PTR points to the event to be inserted into the queue. | |
133 | The caller must allocate memory for the event. It is freed | |
134 | after the event has ben handled. | |
135 | Events in the queue will be processed head to tail, therefore, | |
136 | events inserted at the head of the queue will be processed | |
137 | as last in first out. Event appended at the tail of the queue | |
138 | will be processed first in first out. */ | |
139 | static void | |
140 | async_queue_event (event_ptr, position) | |
141 | gdb_event *event_ptr; | |
142 | queue_position position; | |
143 | { | |
144 | if (position == TAIL) | |
145 | { | |
146 | /* The event will become the new last_event. */ | |
147 | ||
148 | event_ptr->next_event = NULL; | |
149 | if (event_queue.first_event == NULL) | |
150 | event_queue.first_event = event_ptr; | |
151 | else | |
152 | event_queue.last_event->next_event = event_ptr; | |
153 | event_queue.last_event = event_ptr; | |
154 | } | |
155 | else if (position == HEAD) | |
156 | { | |
157 | /* The event becomes the new first_event. */ | |
158 | ||
159 | event_ptr->next_event = event_queue.first_event; | |
160 | if (event_queue.first_event == NULL) | |
161 | event_queue.last_event = event_ptr; | |
162 | event_queue.first_event = event_ptr; | |
163 | } | |
164 | } | |
165 | ||
166 | /* Process one event. | |
167 | The event can be the next one to be serviced in the event queue, | |
168 | or an asynchronous event handler can be invoked in response to | |
169 | the reception of a signal. | |
170 | If an event was processed (either way), 1 is returned otherwise | |
171 | 0 is returned. | |
172 | Scan the queue from head to tail, processing therefore the high | |
173 | priority events first, by invoking the associated event handler | |
174 | procedure. */ | |
175 | static int | |
176 | process_event () | |
177 | { | |
178 | gdb_event *event_ptr, *prev_ptr; | |
179 | event_handler_func *proc; | |
180 | int fd; | |
181 | ||
182 | /* First let's see if there are any asynchronous event handlers that | |
183 | are ready. These would be the result of invoking any of the | |
184 | signal handlers. */ | |
185 | ||
186 | if (check_async_ready ()) | |
187 | { | |
188 | invoke_async_signal_handler (); | |
189 | return 1; | |
190 | } | |
191 | ||
192 | /* Look in the event queue to find an event that is ready | |
193 | to be processed. */ | |
194 | ||
195 | for (event_ptr = event_queue.first_event; event_ptr != NULL; | |
196 | event_ptr = event_ptr->next_event) | |
197 | { | |
198 | /* Call the handler for the event. */ | |
199 | ||
200 | proc = event_ptr->proc; | |
201 | fd = event_ptr->fd; | |
202 | ||
203 | /* Let's get rid of the event from the event queue. We need to | |
204 | do this now because while processing the event, the proc | |
205 | function could end up calling 'error' and therefore jump out | |
206 | to the caller of this function, gdb_do_one_event. In that | |
207 | case, we would have on the event queue an event wich has been | |
208 | processed, but not deleted. */ | |
209 | ||
210 | if (event_queue.first_event == event_ptr) | |
211 | { | |
212 | event_queue.first_event = event_ptr->next_event; | |
213 | if (event_ptr->next_event == NULL) | |
214 | event_queue.last_event = NULL; | |
215 | } | |
216 | else | |
217 | { | |
218 | prev_ptr = event_queue.first_event; | |
219 | while (prev_ptr->next_event != event_ptr) | |
220 | prev_ptr = prev_ptr->next_event; | |
221 | ||
222 | prev_ptr->next_event = event_ptr->next_event; | |
223 | if (event_ptr->next_event == NULL) | |
224 | event_queue.last_event = prev_ptr; | |
225 | } | |
226 | free ((char *) event_ptr); | |
227 | ||
228 | /* Now call the procedure associted with the event. */ | |
229 | (*proc) (fd); | |
230 | return 1; | |
231 | } | |
232 | ||
233 | /* this is the case if there are no event on the event queue. */ | |
234 | return 0; | |
235 | } | |
236 | ||
237 | /* Process one high level event. If nothing is ready at this time, | |
238 | wait for something to happen (via gdb_wait_for_event), then process | |
239 | it. Returns 1 if something was done otherwise returns 0 (this can | |
240 | happen if there are no event sources to wait for). */ | |
085dd6e6 | 241 | static int |
b5a0ac70 SS |
242 | gdb_do_one_event () |
243 | { | |
244 | int result = 0; | |
245 | ||
246 | while (1) | |
247 | { | |
248 | if (!SET_TOP_LEVEL ()) | |
249 | { | |
250 | /* Any events already waiting in the queue? */ | |
251 | if (process_event ()) | |
252 | { | |
253 | result = 1; | |
254 | break; | |
255 | } | |
256 | ||
257 | /* Wait for a new event. If gdb_wait_for_event returns -1, | |
258 | we should get out because this means that there are no | |
259 | event sources left. This will make the event loop stop, | |
260 | and the application exit. */ | |
261 | ||
262 | result = gdb_wait_for_event (); | |
263 | if (result < 0) | |
264 | { | |
265 | result = 0; | |
266 | break; | |
267 | } | |
268 | ||
269 | /* Handle any new events occurred while waiting. */ | |
270 | if (process_event ()) | |
271 | { | |
272 | result = 1; | |
273 | break; | |
274 | } | |
275 | ||
276 | /* If gdb_wait_for_event has returned 1, it means that one | |
277 | event has been handled. We break out of the loop. */ | |
278 | if (result) | |
279 | break; | |
280 | } /* end of if !set_top_level */ | |
281 | else | |
282 | { | |
085dd6e6 JM |
283 | /* FIXME: this should really be a call to a hook that is |
284 | interface specific, because interfaces can display the | |
285 | prompt in their own way. */ | |
b5a0ac70 SS |
286 | display_gdb_prompt (0); |
287 | /* Maybe better to set a flag to be checked somewhere as to | |
288 | whether display the prompt or not. */ | |
289 | } | |
290 | } | |
291 | return result; | |
292 | } | |
43ff13b4 | 293 | \f |
085dd6e6 JM |
294 | |
295 | /* Start up the event loop. This is the entry point to the event loop | |
296 | from the command loop. */ | |
297 | void | |
298 | start_event_loop () | |
299 | { | |
300 | /* Loop until there is something to do. This is the entry point to | |
301 | the event loop engine. gdb_do_one_event will process one event | |
302 | for each invocation. It always returns 1, unless there are no | |
303 | more event sources registered. In this case it returns 0. */ | |
304 | while (gdb_do_one_event () != 0) | |
305 | ; | |
306 | ||
307 | /* We are done with the event loop. There are no more event sources | |
308 | to listen to. So we exit GDB. */ | |
309 | return; | |
310 | } | |
311 | ||
b5a0ac70 SS |
312 | \f |
313 | ||
085dd6e6 JM |
314 | /* Wrapper function for create_file_handler, so that the caller |
315 | doesn't have to know implementation details about the use of poll | |
316 | vs. select. */ | |
317 | void | |
318 | add_file_handler (fd, proc, client_data) | |
319 | int fd; | |
320 | file_handler_func *proc; | |
321 | gdb_client_data client_data; | |
322 | { | |
323 | #ifdef HAVE_POLL | |
324 | create_file_handler (fd, POLLIN, (file_handler_func *) proc, client_data); | |
325 | #else | |
326 | create_file_handler (fd, GDB_READABLE, (file_handler_func *) proc, client_data); | |
327 | #endif | |
328 | } | |
329 | ||
b5a0ac70 SS |
330 | /* Add a file handler/descriptor to the list of descriptors we are |
331 | interested in. | |
332 | FD is the file descriptor for the file/stream to be listened to. | |
333 | For the poll case, MASK is a combination (OR) of | |
334 | POLLIN, POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, | |
335 | POLLWRBAND: these are the events we are interested in. If any of them | |
336 | occurs, proc should be called. | |
337 | For the select case, MASK is a combination of READABLE, WRITABLE, EXCEPTION. | |
338 | PROC is the procedure that will be called when an event occurs for | |
339 | FD. CLIENT_DATA is the argument to pass to PROC. */ | |
085dd6e6 | 340 | static void |
b5a0ac70 SS |
341 | create_file_handler (fd, mask, proc, client_data) |
342 | int fd; | |
343 | int mask; | |
344 | file_handler_func *proc; | |
345 | gdb_client_data client_data; | |
346 | { | |
347 | file_handler *file_ptr; | |
348 | ||
349 | #ifndef HAVE_POLL | |
350 | int index, bit; | |
351 | #endif | |
352 | ||
353 | /* Do we already have a file handler for this file? (We may be | |
354 | changing its associated procedure). */ | |
355 | for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL; | |
356 | file_ptr = file_ptr->next_file) | |
357 | { | |
358 | if (file_ptr->fd == fd) | |
359 | break; | |
360 | } | |
361 | ||
362 | /* It is a new file descriptor. */ | |
363 | if (file_ptr == NULL) | |
364 | { | |
365 | file_ptr = (file_handler *) xmalloc (sizeof (file_handler)); | |
366 | file_ptr->fd = fd; | |
367 | file_ptr->ready_mask = 0; | |
368 | file_ptr->next_file = gdb_notifier.first_file_handler; | |
369 | gdb_notifier.first_file_handler = file_ptr; | |
370 | } | |
371 | file_ptr->proc = proc; | |
372 | file_ptr->client_data = client_data; | |
373 | file_ptr->mask = mask; | |
374 | ||
375 | #ifdef HAVE_POLL | |
376 | ||
377 | gdb_notifier.num_fds++; | |
9e0b60a8 JM |
378 | if (gdb_notifier.poll_fds) |
379 | gdb_notifier.poll_fds = | |
380 | (struct pollfd *) realloc (gdb_notifier.poll_fds, | |
381 | (gdb_notifier.num_fds) * sizeof (struct pollfd)); | |
382 | else | |
383 | gdb_notifier.poll_fds = | |
384 | (struct pollfd *) xmalloc (sizeof (struct pollfd)); | |
b5a0ac70 SS |
385 | (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd = fd; |
386 | (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events = mask; | |
387 | (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents = 0; | |
388 | ||
389 | #else /* ! HAVE_POLL */ | |
390 | ||
391 | index = fd / (NBBY * sizeof (fd_mask)); | |
392 | bit = 1 << (fd % (NBBY * sizeof (fd_mask))); | |
393 | ||
394 | if (mask & GDB_READABLE) | |
395 | gdb_notifier.check_masks[index] |= bit; | |
396 | else | |
397 | gdb_notifier.check_masks[index] &= ~bit; | |
398 | ||
399 | if (mask & GDB_WRITABLE) | |
400 | (gdb_notifier.check_masks + MASK_SIZE)[index] |= bit; | |
401 | else | |
402 | (gdb_notifier.check_masks + MASK_SIZE)[index] &= ~bit; | |
403 | ||
404 | if (mask & GDB_EXCEPTION) | |
405 | (gdb_notifier.check_masks + 2 * (MASK_SIZE))[index] |= bit; | |
406 | else | |
407 | (gdb_notifier.check_masks + 2 * (MASK_SIZE))[index] &= ~bit; | |
408 | ||
409 | if (gdb_notifier.num_fds <= fd) | |
410 | gdb_notifier.num_fds = fd + 1; | |
411 | ||
412 | #endif /* HAVE_POLL */ | |
413 | } | |
414 | ||
415 | /* Remove the file descriptor FD from the list of monitored fd's: | |
416 | i.e. we don't care anymore about events on the FD. */ | |
417 | void | |
418 | delete_file_handler (fd) | |
419 | int fd; | |
420 | { | |
421 | file_handler *file_ptr, *prev_ptr = NULL; | |
422 | int i, j; | |
423 | struct pollfd *new_poll_fds; | |
424 | #ifndef HAVE_POLL | |
425 | int index, bit; | |
426 | unsigned long flags; | |
427 | #endif | |
428 | ||
429 | /* Find the entry for the given file. */ | |
430 | ||
431 | for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL; | |
432 | file_ptr = file_ptr->next_file) | |
433 | { | |
434 | if (file_ptr->fd == fd) | |
435 | break; | |
436 | } | |
437 | ||
438 | if (file_ptr == NULL) | |
439 | return; | |
440 | ||
441 | /* Deactivate the file descriptor, by clearing its mask, | |
442 | so that it will not fire again. */ | |
443 | ||
444 | file_ptr->mask = 0; | |
445 | ||
446 | #ifdef HAVE_POLL | |
447 | /* Create a new poll_fds array by copying every fd's information but the | |
448 | one we want to get rid of. */ | |
449 | ||
450 | new_poll_fds = | |
451 | (struct pollfd *) xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd)); | |
452 | ||
453 | for (i = 0, j = 0; i < gdb_notifier.num_fds; i++) | |
454 | { | |
455 | if ((gdb_notifier.poll_fds + i)->fd != fd) | |
456 | { | |
457 | (new_poll_fds + j)->fd = (gdb_notifier.poll_fds + i)->fd; | |
458 | (new_poll_fds + j)->events = (gdb_notifier.poll_fds + i)->events; | |
459 | (new_poll_fds + j)->revents = (gdb_notifier.poll_fds + i)->revents; | |
460 | j++; | |
461 | } | |
462 | } | |
463 | free (gdb_notifier.poll_fds); | |
464 | gdb_notifier.poll_fds = new_poll_fds; | |
465 | gdb_notifier.num_fds--; | |
466 | ||
467 | #else /* ! HAVE_POLL */ | |
468 | ||
469 | index = fd / (NBBY * sizeof (fd_mask)); | |
470 | bit = 1 << (fd % (NBBY * sizeof (fd_mask))); | |
471 | ||
472 | if (file_ptr->mask & GDB_READABLE) | |
473 | gdb_notifier.check_masks[index] &= ~bit; | |
474 | if (file_ptr->mask & GDB_WRITABLE) | |
475 | (gdb_notifier.check_masks + MASK_SIZE)[index] &= ~bit; | |
476 | if (file_ptr->mask & GDB_EXCEPTION) | |
477 | (gdb_notifier.check_masks + 2 * (MASK_SIZE))[index] &= ~bit; | |
478 | ||
479 | /* Find current max fd. */ | |
480 | ||
481 | if ((fd + 1) == gdb_notifier.num_fds) | |
482 | { | |
483 | for (gdb_notifier.num_fds = 0; index >= 0; index--) | |
484 | { | |
485 | flags = gdb_notifier.check_masks[index] | |
486 | | (gdb_notifier.check_masks + MASK_SIZE)[index] | |
487 | | (gdb_notifier.check_masks + 2 * (MASK_SIZE))[index]; | |
488 | if (flags) | |
489 | { | |
490 | for (i = (NBBY * sizeof (fd_mask)); i > 0; i--) | |
491 | { | |
492 | if (flags & (((unsigned long) 1) << (i - 1))) | |
493 | break; | |
494 | } | |
495 | gdb_notifier.num_fds = index * (NBBY * sizeof (fd_mask)) + i; | |
496 | break; | |
497 | } | |
498 | } | |
499 | } | |
500 | #endif /* HAVE_POLL */ | |
501 | ||
502 | /* Get rid of the file handler in the file handler list. */ | |
503 | if (file_ptr == gdb_notifier.first_file_handler) | |
504 | gdb_notifier.first_file_handler = file_ptr->next_file; | |
505 | else | |
506 | { | |
507 | for (prev_ptr = gdb_notifier.first_file_handler; | |
9e0b60a8 | 508 | prev_ptr->next_file != file_ptr; |
b5a0ac70 SS |
509 | prev_ptr = prev_ptr->next_file) |
510 | ; | |
511 | prev_ptr->next_file = file_ptr->next_file; | |
512 | } | |
513 | free ((char *) file_ptr); | |
514 | } | |
515 | ||
516 | /* Handle the given event by calling the procedure associated to the | |
517 | corresponding file handler. Called by process_event indirectly, | |
518 | through event_ptr->proc. EVENT_FILE_DESC is file descriptor of the | |
519 | event in the front of the event queue. */ | |
520 | static void | |
521 | handle_file_event (event_file_desc) | |
522 | int event_file_desc; | |
523 | { | |
524 | file_handler *file_ptr; | |
525 | int mask, error_mask; | |
526 | ||
527 | /* Search the file handler list to find one that matches the fd in | |
528 | the event. */ | |
529 | for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL; | |
530 | file_ptr = file_ptr->next_file) | |
531 | { | |
532 | if (file_ptr->fd == event_file_desc) | |
533 | { | |
534 | /* With poll, the ready_mask could have any of three events | |
535 | set to 1: POLLHUP, POLLERR, POLLNVAL. These events cannot | |
536 | be used in the requested event mask (events), but they | |
537 | can be returned in the return mask (revents). We need to | |
538 | check for those event too, and add them to the mask which | |
539 | will be passed to the handler. */ | |
540 | ||
541 | /* See if the desired events (mask) match the received | |
542 | events (ready_mask). */ | |
543 | ||
544 | #ifdef HAVE_POLL | |
545 | error_mask = POLLHUP | POLLERR | POLLNVAL; | |
546 | mask = (file_ptr->ready_mask & file_ptr->mask) | | |
547 | (file_ptr->ready_mask & error_mask); | |
548 | ||
549 | #else /* ! HAVE_POLL */ | |
550 | mask = file_ptr->ready_mask & file_ptr->mask; | |
551 | #endif /* HAVE_POLL */ | |
552 | ||
553 | /* Clear the received events for next time around. */ | |
554 | file_ptr->ready_mask = 0; | |
555 | ||
556 | /* If there was a match, then call the handler. */ | |
557 | if (mask != 0) | |
558 | (*file_ptr->proc) (file_ptr->client_data, mask); | |
559 | break; | |
560 | } | |
561 | } | |
562 | } | |
563 | ||
564 | /* Called by gdb_do_one_event to wait for new events on the | |
565 | monitored file descriptors. Queue file events as they are | |
566 | detected by the poll. | |
567 | If there are no events, this function will block in the | |
568 | call to poll. | |
569 | Return -1 if there are no files descriptors to monitor, | |
570 | otherwise return 0. */ | |
571 | static int | |
572 | gdb_wait_for_event () | |
573 | { | |
574 | file_handler *file_ptr; | |
575 | gdb_event *file_event_ptr; | |
0f71a2f6 JM |
576 | int num_found = 0; |
577 | int i; | |
b5a0ac70 SS |
578 | |
579 | #ifndef HAVE_POLL | |
580 | int mask, bit, index; | |
581 | #endif | |
582 | ||
583 | if (gdb_notifier.num_fds == 0) | |
584 | return -1; | |
585 | ||
586 | #ifdef HAVE_POLL | |
587 | num_found = | |
588 | poll (gdb_notifier.poll_fds, (unsigned long) gdb_notifier.num_fds, -1); | |
589 | ||
590 | #else /* ! HAVE_POLL */ | |
591 | memcpy (gdb_notifier.ready_masks, | |
592 | gdb_notifier.check_masks, | |
593 | 3 * MASK_SIZE * sizeof (fd_mask)); | |
594 | num_found = select (gdb_notifier.num_fds, | |
595 | (SELECT_MASK *) & gdb_notifier.ready_masks[0], | |
596 | (SELECT_MASK *) & gdb_notifier.ready_masks[MASK_SIZE], | |
597 | (SELECT_MASK *) & gdb_notifier.ready_masks[2 * MASK_SIZE], | |
598 | NULL); | |
599 | ||
600 | /* Clear the masks after an error from select. */ | |
601 | if (num_found == -1) | |
602 | memset (gdb_notifier.ready_masks, | |
603 | 0, 3 * MASK_SIZE * sizeof (fd_mask)); | |
604 | ||
605 | #endif /* HAVE_POLL */ | |
606 | ||
607 | /* Enqueue all detected file events. */ | |
608 | ||
609 | #ifdef HAVE_POLL | |
610 | ||
611 | for (i = 0; (i < gdb_notifier.num_fds) && (num_found > 0); i++) | |
612 | { | |
613 | if ((gdb_notifier.poll_fds + i)->revents) | |
614 | num_found--; | |
615 | else | |
616 | continue; | |
617 | ||
618 | for (file_ptr = gdb_notifier.first_file_handler; | |
619 | file_ptr != NULL; | |
620 | file_ptr = file_ptr->next_file) | |
621 | { | |
622 | if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd) | |
623 | break; | |
624 | } | |
625 | ||
626 | if (file_ptr) | |
627 | { | |
628 | /* Enqueue an event only if this is still a new event for | |
629 | this fd. */ | |
630 | if (file_ptr->ready_mask == 0) | |
631 | { | |
632 | file_event_ptr = | |
633 | (gdb_event *) xmalloc (sizeof (gdb_event)); | |
634 | file_event_ptr->proc = handle_file_event; | |
635 | file_event_ptr->fd = file_ptr->fd; | |
636 | async_queue_event (file_event_ptr, TAIL); | |
637 | } | |
638 | } | |
639 | ||
640 | file_ptr->ready_mask = (gdb_notifier.poll_fds + i)->revents; | |
641 | } | |
642 | ||
643 | #else /* ! HAVE_POLL */ | |
644 | for (file_ptr = gdb_notifier.first_file_handler; | |
645 | (file_ptr != NULL) && (num_found > 0); | |
646 | file_ptr = file_ptr->next_file) | |
647 | { | |
648 | index = file_ptr->fd / (NBBY * sizeof (fd_mask)); | |
649 | bit = 1 << (file_ptr->fd % (NBBY * sizeof (fd_mask))); | |
650 | mask = 0; | |
651 | ||
652 | if (gdb_notifier.ready_masks[index] & bit) | |
653 | mask |= GDB_READABLE; | |
654 | if ((gdb_notifier.ready_masks + MASK_SIZE)[index] & bit) | |
655 | mask |= GDB_WRITABLE; | |
656 | if ((gdb_notifier.ready_masks + 2 * (MASK_SIZE))[index] & bit) | |
657 | mask |= GDB_EXCEPTION; | |
658 | ||
659 | if (!mask) | |
660 | continue; | |
661 | else | |
662 | num_found--; | |
663 | ||
664 | /* Enqueue an event only if this is still a new event for | |
665 | this fd. */ | |
666 | ||
667 | if (file_ptr->ready_mask == 0) | |
668 | { | |
669 | file_event_ptr = | |
670 | (gdb_event *) xmalloc (sizeof (gdb_event)); | |
671 | file_event_ptr->proc = handle_file_event; | |
672 | file_event_ptr->fd = file_ptr->fd; | |
673 | async_queue_event (file_event_ptr, TAIL); | |
674 | } | |
675 | file_ptr->ready_mask = mask; | |
676 | } | |
677 | #endif /* HAVE_POLL */ | |
678 | ||
679 | return 0; | |
680 | } | |
681 | \f | |
682 | ||
683 | /* Create an asynchronous handler, allocating memory for it. | |
684 | Return a pointer to the newly created handler. | |
685 | This pointer will be used to invoke the handler by | |
686 | invoke_async_signal_handler. | |
687 | PROC is the function to call with CLIENT_DATA argument | |
688 | whenever the handler is invoked. */ | |
689 | async_signal_handler * | |
690 | create_async_signal_handler (proc, client_data) | |
691 | async_handler_func *proc; | |
692 | gdb_client_data client_data; | |
693 | { | |
694 | async_signal_handler *async_handler_ptr; | |
695 | ||
696 | async_handler_ptr = | |
697 | (async_signal_handler *) xmalloc (sizeof (async_signal_handler)); | |
698 | async_handler_ptr->ready = 0; | |
699 | async_handler_ptr->next_handler = NULL; | |
700 | async_handler_ptr->proc = proc; | |
701 | async_handler_ptr->client_data = client_data; | |
702 | if (sighandler_list.first_handler == NULL) | |
703 | sighandler_list.first_handler = async_handler_ptr; | |
704 | else | |
705 | sighandler_list.last_handler->next_handler = async_handler_ptr; | |
706 | sighandler_list.last_handler = async_handler_ptr; | |
707 | return async_handler_ptr; | |
708 | } | |
709 | ||
710 | /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information will | |
711 | be used when the handlers are invoked, after we have waited for | |
712 | some event. The caller of this function is the interrupt handler | |
713 | associated with a signal. */ | |
714 | void | |
715 | mark_async_signal_handler (async_handler_ptr) | |
716 | async_signal_handler *async_handler_ptr; | |
717 | { | |
718 | ((async_signal_handler *) async_handler_ptr)->ready = 1; | |
719 | async_handler_ready = 1; | |
720 | } | |
721 | ||
722 | /* Call all the handlers that are ready. */ | |
723 | static void | |
724 | invoke_async_signal_handler () | |
725 | { | |
726 | async_signal_handler *async_handler_ptr; | |
727 | ||
728 | if (async_handler_ready == 0) | |
729 | return; | |
730 | async_handler_ready = 0; | |
731 | ||
732 | /* Invoke ready handlers. */ | |
733 | ||
734 | while (1) | |
735 | { | |
736 | for (async_handler_ptr = sighandler_list.first_handler; | |
737 | async_handler_ptr != NULL; | |
738 | async_handler_ptr = async_handler_ptr->next_handler) | |
739 | { | |
740 | if (async_handler_ptr->ready) | |
741 | break; | |
742 | } | |
743 | if (async_handler_ptr == NULL) | |
744 | break; | |
745 | async_handler_ptr->ready = 0; | |
746 | (*async_handler_ptr->proc) (async_handler_ptr->client_data); | |
747 | } | |
748 | ||
749 | return; | |
750 | } | |
751 | ||
752 | /* Delete an asynchronous handler (ASYNC_HANDLER_PTR). | |
753 | Free the space allocated for it. */ | |
754 | void | |
755 | delete_async_signal_handler (async_handler_ptr) | |
43ff13b4 | 756 | async_signal_handler **async_handler_ptr; |
b5a0ac70 SS |
757 | { |
758 | async_signal_handler *prev_ptr; | |
759 | ||
43ff13b4 | 760 | if (sighandler_list.first_handler == (*async_handler_ptr)) |
b5a0ac70 | 761 | { |
43ff13b4 | 762 | sighandler_list.first_handler = (*async_handler_ptr)->next_handler; |
b5a0ac70 SS |
763 | if (sighandler_list.first_handler == NULL) |
764 | sighandler_list.last_handler = NULL; | |
765 | } | |
766 | else | |
767 | { | |
768 | prev_ptr = sighandler_list.first_handler; | |
43ff13b4 | 769 | while (prev_ptr->next_handler != (*async_handler_ptr) && prev_ptr) |
b5a0ac70 | 770 | prev_ptr = prev_ptr->next_handler; |
43ff13b4 JM |
771 | prev_ptr->next_handler = (*async_handler_ptr)->next_handler; |
772 | if (sighandler_list.last_handler == (*async_handler_ptr)) | |
b5a0ac70 SS |
773 | sighandler_list.last_handler = prev_ptr; |
774 | } | |
43ff13b4 JM |
775 | free ((char *) (*async_handler_ptr)); |
776 | (*async_handler_ptr) = NULL; | |
b5a0ac70 SS |
777 | } |
778 | ||
779 | /* Is it necessary to call invoke_async_signal_handler? */ | |
780 | static int | |
781 | check_async_ready () | |
782 | { | |
783 | return async_handler_ready; | |
784 | } |