1 /* Event loop machinery for the remote server for GDB.
2 Copyright (C) 1999-2020 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Based on src/gdb/event-loop.c. */
23 #include <sys/types.h>
24 #include "gdbsupport/gdb_sys_time.h"
34 typedef int (event_handler_func
) (gdb_fildes_t
);
36 /* Tell create_file_handler what events we are interested in. */
38 #define GDB_READABLE (1<<1)
39 #define GDB_WRITABLE (1<<2)
40 #define GDB_EXCEPTION (1<<3)
42 /* Events are queued by on the event_queue and serviced later
43 on by do_one_event. An event can be, for instance, a file
44 descriptor becoming ready to be read. Servicing an event simply
45 means that the procedure PROC will be called. We have 2 queues,
46 one for file handlers that we listen to in the event loop, and one
47 for the file handlers+events that are ready. The procedure PROC
48 associated with each event is always the same (handle_file_event).
49 Its duty is to invoke the handler associated with the file
50 descriptor whose state change generated the event, plus doing other
55 /* Procedure to call to service this event. */
56 event_handler_func
*proc
;
58 /* File descriptor that is ready. */
62 /* Information about each file descriptor we register with the event
65 typedef struct file_handler
67 /* File descriptor. */
70 /* Events we want to monitor. */
73 /* Events that have been seen since the last time. */
76 /* Procedure to call when fd is ready. */
79 /* Argument to pass to proc. */
80 gdb_client_data client_data
;
82 /* Was an error detected on this fd? */
85 /* Next registered file descriptor. */
86 struct file_handler
*next_file
;
90 typedef gdb::unique_xmalloc_ptr
<gdb_event
> gdb_event_up
;
92 static std::queue
<gdb_event_up
, std::list
<gdb_event_up
>> event_queue
;
94 /* Gdb_notifier is just a list of file descriptors gdb is interested
95 in. These are the input file descriptor, and the target file
96 descriptor. Each of the elements in the gdb_notifier list is
97 basically a description of what kind of events gdb is interested
102 /* Ptr to head of file handler list. */
103 file_handler
*first_file_handler
;
105 /* Masks to be used in the next call to select. Bits are set in
106 response to calls to create_file_handler. */
107 fd_set check_masks
[3];
109 /* What file descriptors were found ready by select. */
110 fd_set ready_masks
[3];
112 /* Number of valid bits (highest fd value + 1). (for select) */
117 /* Callbacks are just routines that are executed before waiting for the
118 next event. In GDB this is struct gdb_timer. We don't need timers
119 so rather than copy all that complexity in gdbserver, we provide what
120 we need, but we do so in a way that if/when the day comes that we need
121 that complexity, it'll be easier to add - replace callbacks with timers
122 and use a delta of zero (which is all gdb currently uses timers for anyway).
124 PROC will be executed before gdbserver goes to sleep to wait for the
127 struct callback_event
130 callback_handler_func
*proc
;
131 gdb_client_data data
;
132 struct callback_event
*next
;
135 /* Table of registered callbacks. */
139 struct callback_event
*first
;
140 struct callback_event
*last
;
142 /* Id of the last callback created. */
148 initialize_event_loop (void)
152 /* Process one event. If an event was processed, 1 is returned
153 otherwise 0 is returned. Scan the queue from head to tail,
154 processing therefore the high priority events first, by invoking
155 the associated event handler procedure. */
160 /* Let's get rid of the event from the event queue. We need to
161 do this now because while processing the event, since the
162 proc function could end up jumping out to the caller of this
163 function. In that case, we would have on the event queue an
164 event which has been processed, but not deleted. */
165 if (!event_queue
.empty ())
167 gdb_event_up event_ptr
= std::move (event_queue
.front ());
170 event_handler_func
*proc
= event_ptr
->proc
;
171 gdb_fildes_t fd
= event_ptr
->fd
;
173 /* Now call the procedure associated with the event. */
179 /* This is the case if there are no event on the event queue. */
183 /* Append PROC to the callback list.
184 The result is the "id" of the callback that can be passed back to
185 delete_callback_event. */
188 append_callback_event (callback_handler_func
*proc
, gdb_client_data data
)
190 struct callback_event
*event_ptr
= XNEW (struct callback_event
);
192 event_ptr
->id
= callback_list
.num_callbacks
++;
193 event_ptr
->proc
= proc
;
194 event_ptr
->data
= data
;
195 event_ptr
->next
= NULL
;
196 if (callback_list
.first
== NULL
)
197 callback_list
.first
= event_ptr
;
198 if (callback_list
.last
!= NULL
)
199 callback_list
.last
->next
= event_ptr
;
200 callback_list
.last
= event_ptr
;
201 return event_ptr
->id
;
204 /* Delete callback ID.
205 It is not an error callback ID doesn't exist. */
208 delete_callback_event (int id
)
210 struct callback_event
**p
;
212 for (p
= &callback_list
.first
; *p
!= NULL
; p
= &(*p
)->next
)
214 struct callback_event
*event_ptr
= *p
;
216 if (event_ptr
->id
== id
)
218 *p
= event_ptr
->next
;
219 if (event_ptr
== callback_list
.last
)
220 callback_list
.last
= NULL
;
227 /* Run the next callback.
228 The result is 1 if a callback was called and event processing
229 should continue, -1 if the callback wants the event loop to exit,
230 and 0 if there are no more callbacks. */
233 process_callback (void)
235 struct callback_event
*event_ptr
;
237 event_ptr
= callback_list
.first
;
238 if (event_ptr
!= NULL
)
240 callback_handler_func
*proc
= event_ptr
->proc
;
241 gdb_client_data data
= event_ptr
->data
;
243 /* Remove the event before calling PROC,
244 more events may get added by PROC. */
245 callback_list
.first
= event_ptr
->next
;
246 if (callback_list
.first
== NULL
)
247 callback_list
.last
= NULL
;
257 /* Add a file handler/descriptor to the list of descriptors we are
258 interested in. FD is the file descriptor for the file/stream to be
259 listened to. MASK is a combination of READABLE, WRITABLE,
260 EXCEPTION. PROC is the procedure that will be called when an event
261 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
264 create_file_handler (gdb_fildes_t fd
, int mask
, handler_func
*proc
,
265 gdb_client_data client_data
)
267 file_handler
*file_ptr
;
269 /* Do we already have a file handler for this file? (We may be
270 changing its associated procedure). */
271 for (file_ptr
= gdb_notifier
.first_file_handler
;
273 file_ptr
= file_ptr
->next_file
)
274 if (file_ptr
->fd
== fd
)
277 /* It is a new file descriptor. Add it to the list. Otherwise,
278 just change the data associated with it. */
279 if (file_ptr
== NULL
)
281 file_ptr
= XNEW (struct file_handler
);
283 file_ptr
->ready_mask
= 0;
284 file_ptr
->next_file
= gdb_notifier
.first_file_handler
;
285 gdb_notifier
.first_file_handler
= file_ptr
;
287 if (mask
& GDB_READABLE
)
288 FD_SET (fd
, &gdb_notifier
.check_masks
[0]);
290 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
292 if (mask
& GDB_WRITABLE
)
293 FD_SET (fd
, &gdb_notifier
.check_masks
[1]);
295 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
297 if (mask
& GDB_EXCEPTION
)
298 FD_SET (fd
, &gdb_notifier
.check_masks
[2]);
300 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
302 if (gdb_notifier
.num_fds
<= fd
)
303 gdb_notifier
.num_fds
= fd
+ 1;
306 file_ptr
->proc
= proc
;
307 file_ptr
->client_data
= client_data
;
308 file_ptr
->mask
= mask
;
311 /* Wrapper function for create_file_handler. */
314 add_file_handler (gdb_fildes_t fd
,
315 handler_func
*proc
, gdb_client_data client_data
)
317 create_file_handler (fd
, GDB_READABLE
| GDB_EXCEPTION
, proc
, client_data
);
320 /* Remove the file descriptor FD from the list of monitored fd's:
321 i.e. we don't care anymore about events on the FD. */
324 delete_file_handler (gdb_fildes_t fd
)
326 file_handler
*file_ptr
, *prev_ptr
= NULL
;
329 /* Find the entry for the given file. */
331 for (file_ptr
= gdb_notifier
.first_file_handler
;
333 file_ptr
= file_ptr
->next_file
)
334 if (file_ptr
->fd
== fd
)
337 if (file_ptr
== NULL
)
340 if (file_ptr
->mask
& GDB_READABLE
)
341 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
342 if (file_ptr
->mask
& GDB_WRITABLE
)
343 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
344 if (file_ptr
->mask
& GDB_EXCEPTION
)
345 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
347 /* Find current max fd. */
349 if ((fd
+ 1) == gdb_notifier
.num_fds
)
351 gdb_notifier
.num_fds
--;
352 for (i
= gdb_notifier
.num_fds
; i
; i
--)
354 if (FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[0])
355 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[1])
356 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[2]))
359 gdb_notifier
.num_fds
= i
;
362 /* Deactivate the file descriptor, by clearing its mask, so that it
363 will not fire again. */
367 /* Get rid of the file handler in the file handler list. */
368 if (file_ptr
== gdb_notifier
.first_file_handler
)
369 gdb_notifier
.first_file_handler
= file_ptr
->next_file
;
372 for (prev_ptr
= gdb_notifier
.first_file_handler
;
373 prev_ptr
->next_file
!= file_ptr
;
374 prev_ptr
= prev_ptr
->next_file
)
376 prev_ptr
->next_file
= file_ptr
->next_file
;
381 /* Handle the given event by calling the procedure associated to the
382 corresponding file handler. Called by process_event indirectly,
383 through event_ptr->proc. EVENT_FILE_DESC is file descriptor of the
384 event in the front of the event queue. */
387 handle_file_event (gdb_fildes_t event_file_desc
)
389 file_handler
*file_ptr
;
392 /* Search the file handler list to find one that matches the fd in
394 for (file_ptr
= gdb_notifier
.first_file_handler
; file_ptr
!= NULL
;
395 file_ptr
= file_ptr
->next_file
)
397 if (file_ptr
->fd
== event_file_desc
)
399 /* See if the desired events (mask) match the received
400 events (ready_mask). */
402 if (file_ptr
->ready_mask
& GDB_EXCEPTION
)
404 warning ("Exception condition detected on fd %s",
405 pfildes (file_ptr
->fd
));
410 mask
= file_ptr
->ready_mask
& file_ptr
->mask
;
412 /* Clear the received events for next time around. */
413 file_ptr
->ready_mask
= 0;
415 /* If there was a match, then call the handler. */
418 if ((*file_ptr
->proc
) (file_ptr
->error
,
419 file_ptr
->client_data
) < 0)
429 /* Create a file event, to be enqueued in the event queue for
430 processing. The procedure associated to this event is always
431 handle_file_event, which will in turn invoke the one that was
432 associated to FD when it was registered with the event loop. */
435 create_file_event (gdb_fildes_t fd
)
437 gdb_event
*file_event_ptr
;
439 file_event_ptr
= XNEW (gdb_event
);
440 file_event_ptr
->proc
= handle_file_event
;
441 file_event_ptr
->fd
= fd
;
443 return file_event_ptr
;
446 /* Called by do_one_event to wait for new events on the monitored file
447 descriptors. Queue file events as they are detected by the poll.
448 If there are no events, this function will block in the call to
449 select. Return -1 if there are no files descriptors to monitor,
450 otherwise return 0. */
453 wait_for_event (void)
455 file_handler
*file_ptr
;
458 /* Make sure all output is done before getting another event. */
462 if (gdb_notifier
.num_fds
== 0)
465 gdb_notifier
.ready_masks
[0] = gdb_notifier
.check_masks
[0];
466 gdb_notifier
.ready_masks
[1] = gdb_notifier
.check_masks
[1];
467 gdb_notifier
.ready_masks
[2] = gdb_notifier
.check_masks
[2];
468 num_found
= select (gdb_notifier
.num_fds
,
469 &gdb_notifier
.ready_masks
[0],
470 &gdb_notifier
.ready_masks
[1],
471 &gdb_notifier
.ready_masks
[2],
474 /* Clear the masks after an error from select. */
477 FD_ZERO (&gdb_notifier
.ready_masks
[0]);
478 FD_ZERO (&gdb_notifier
.ready_masks
[1]);
479 FD_ZERO (&gdb_notifier
.ready_masks
[2]);
481 /* Dont print anything if we got a signal, let gdb handle
484 perror_with_name ("select");
488 /* Enqueue all detected file events. */
490 for (file_ptr
= gdb_notifier
.first_file_handler
;
491 file_ptr
!= NULL
&& num_found
> 0;
492 file_ptr
= file_ptr
->next_file
)
496 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[0]))
497 mask
|= GDB_READABLE
;
498 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[1]))
499 mask
|= GDB_WRITABLE
;
500 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[2]))
501 mask
|= GDB_EXCEPTION
;
508 /* Enqueue an event only if this is still a new event for this
511 if (file_ptr
->ready_mask
== 0)
513 gdb_event
*file_event_ptr
= create_file_event (file_ptr
->fd
);
515 event_queue
.emplace (file_event_ptr
);
517 file_ptr
->ready_mask
= mask
;
523 /* Start up the event loop. This is the entry point to the event
527 start_event_loop (void)
529 /* Loop until there is nothing to do. This is the entry point to
530 the event loop engine. If nothing is ready at this time, wait
531 for something to happen (via wait_for_event), then process it.
532 Return when there are no longer event sources to wait for. */
536 /* Any events already waiting in the queue? */
537 int res
= process_event ();
539 /* Did the event handler want the event loop to stop? */
546 /* Process any queued callbacks before we go to sleep. */
547 res
= process_callback ();
549 /* Did the callback want the event loop to stop? */
556 /* Wait for a new event. If wait_for_event returns -1, we
557 should get out because this means that there are no event
558 sources left. This will make the event loop stop, and the
561 if (wait_for_event () < 0)
565 /* We are done with the event loop. There are no more event sources
566 to listen to. So we exit gdbserver. */