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