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