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