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