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