1 /* Event loop machinery for the remote server for GDB.
2 Copyright (C) 1999-2015 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. */
24 #include <sys/types.h>
25 #include "gdb_sys_time.h"
34 typedef struct gdb_event gdb_event
;
35 typedef int (event_handler_func
) (gdb_fildes_t
);
37 /* Tell create_file_handler what events we are interested in. */
39 #define GDB_READABLE (1<<1)
40 #define GDB_WRITABLE (1<<2)
41 #define GDB_EXCEPTION (1<<3)
43 /* Events are queued by calling 'QUEUE_enque (gdb_event_p, event_queue,
44 file_event_ptr)' and serviced later
45 on by do_one_event. An event can be, for instance, a file
46 descriptor becoming ready to be read. Servicing an event simply
47 means that the procedure PROC will be called. We have 2 queues,
48 one for file handlers that we listen to in the event loop, and one
49 for the file handlers+events that are ready. The procedure PROC
50 associated with each event is always the same (handle_file_event).
51 Its duty is to invoke the handler associated with the file
52 descriptor whose state change generated the event, plus doing other
55 typedef struct gdb_event
57 /* Procedure to call to service this event. */
58 event_handler_func
*proc
;
60 /* File descriptor that is ready. */
64 /* Information about each file descriptor we register with the event
67 typedef struct file_handler
69 /* File descriptor. */
72 /* Events we want to monitor. */
75 /* Events that have been seen since the last time. */
78 /* Procedure to call when fd is ready. */
81 /* Argument to pass to proc. */
82 gdb_client_data client_data
;
84 /* Was an error detected on this fd? */
87 /* Next registered file descriptor. */
88 struct file_handler
*next_file
;
92 DECLARE_QUEUE_P(gdb_event_p
);
93 static QUEUE(gdb_event_p
) *event_queue
= NULL
;
94 DEFINE_QUEUE_P(gdb_event_p
);
96 /* Gdb_notifier is just a list of file descriptors gdb is interested
97 in. These are the input file descriptor, and the target file
98 descriptor. Each of the elements in the gdb_notifier list is
99 basically a description of what kind of events gdb is interested
104 /* Ptr to head of file handler list. */
105 file_handler
*first_file_handler
;
107 /* Masks to be used in the next call to select. Bits are set in
108 response to calls to create_file_handler. */
109 fd_set check_masks
[3];
111 /* What file descriptors were found ready by select. */
112 fd_set ready_masks
[3];
114 /* Number of valid bits (highest fd value + 1). (for select) */
119 /* Callbacks are just routines that are executed before waiting for the
120 next event. In GDB this is struct gdb_timer. We don't need timers
121 so rather than copy all that complexity in gdbserver, we provide what
122 we need, but we do so in a way that if/when the day comes that we need
123 that complexity, it'll be easier to add - replace callbacks with timers
124 and use a delta of zero (which is all gdb currently uses timers for anyway).
126 PROC will be executed before gdbserver goes to sleep to wait for the
129 struct callback_event
132 callback_handler_func
*proc
;
133 gdb_client_data data
;
134 struct callback_event
*next
;
137 /* Table of registered callbacks. */
141 struct callback_event
*first
;
142 struct callback_event
*last
;
144 /* Id of the last callback created. */
152 gdb_event_xfree (struct gdb_event
*event
)
158 initialize_event_loop (void)
160 event_queue
= QUEUE_alloc (gdb_event_p
, gdb_event_xfree
);
163 /* Process one event. If an event was processed, 1 is returned
164 otherwise 0 is returned. Scan the queue from head to tail,
165 processing therefore the high priority events first, by invoking
166 the associated event handler procedure. */
171 /* Let's get rid of the event from the event queue. We need to
172 do this now because while processing the event, since the
173 proc function could end up jumping out to the caller of this
174 function. In that case, we would have on the event queue an
175 event which has been processed, but not deleted. */
176 if (!QUEUE_is_empty (gdb_event_p
, event_queue
))
178 gdb_event
*event_ptr
= QUEUE_deque (gdb_event_p
, event_queue
);
179 event_handler_func
*proc
= event_ptr
->proc
;
180 gdb_fildes_t fd
= event_ptr
->fd
;
182 gdb_event_xfree (event_ptr
);
183 /* Now call the procedure associated with the event. */
189 /* This is the case if there are no event on the event queue. */
193 /* Append PROC to the callback list.
194 The result is the "id" of the callback that can be passed back to
195 delete_callback_event. */
198 append_callback_event (callback_handler_func
*proc
, gdb_client_data data
)
200 struct callback_event
*event_ptr
;
202 event_ptr
= xmalloc (sizeof (*event_ptr
));
203 event_ptr
->id
= callback_list
.num_callbacks
++;
204 event_ptr
->proc
= proc
;
205 event_ptr
->data
= data
;
206 event_ptr
->next
= NULL
;
207 if (callback_list
.first
== NULL
)
208 callback_list
.first
= event_ptr
;
209 if (callback_list
.last
!= NULL
)
210 callback_list
.last
->next
= event_ptr
;
211 callback_list
.last
= event_ptr
;
212 return event_ptr
->id
;
215 /* Delete callback ID.
216 It is not an error callback ID doesn't exist. */
219 delete_callback_event (int id
)
221 struct callback_event
**p
;
223 for (p
= &callback_list
.first
; *p
!= NULL
; p
= &(*p
)->next
)
225 struct callback_event
*event_ptr
= *p
;
227 if (event_ptr
->id
== id
)
229 *p
= event_ptr
->next
;
230 if (event_ptr
== callback_list
.last
)
231 callback_list
.last
= NULL
;
238 /* Run the next callback.
239 The result is 1 if a callback was called and event processing
240 should continue, -1 if the callback wants the event loop to exit,
241 and 0 if there are no more callbacks. */
244 process_callback (void)
246 struct callback_event
*event_ptr
;
248 event_ptr
= callback_list
.first
;
249 if (event_ptr
!= NULL
)
251 callback_handler_func
*proc
= event_ptr
->proc
;
252 gdb_client_data data
= event_ptr
->data
;
254 /* Remove the event before calling PROC,
255 more events may get added by PROC. */
256 callback_list
.first
= event_ptr
->next
;
257 if (callback_list
.first
== NULL
)
258 callback_list
.last
= NULL
;
268 /* Add a file handler/descriptor to the list of descriptors we are
269 interested in. FD is the file descriptor for the file/stream to be
270 listened to. MASK is a combination of READABLE, WRITABLE,
271 EXCEPTION. PROC is the procedure that will be called when an event
272 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
275 create_file_handler (gdb_fildes_t fd
, int mask
, handler_func
*proc
,
276 gdb_client_data client_data
)
278 file_handler
*file_ptr
;
280 /* Do we already have a file handler for this file? (We may be
281 changing its associated procedure). */
282 for (file_ptr
= gdb_notifier
.first_file_handler
;
284 file_ptr
= file_ptr
->next_file
)
285 if (file_ptr
->fd
== fd
)
288 /* It is a new file descriptor. Add it to the list. Otherwise,
289 just change the data associated with it. */
290 if (file_ptr
== NULL
)
292 file_ptr
= xmalloc (sizeof (*file_ptr
));
294 file_ptr
->ready_mask
= 0;
295 file_ptr
->next_file
= gdb_notifier
.first_file_handler
;
296 gdb_notifier
.first_file_handler
= file_ptr
;
298 if (mask
& GDB_READABLE
)
299 FD_SET (fd
, &gdb_notifier
.check_masks
[0]);
301 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
303 if (mask
& GDB_WRITABLE
)
304 FD_SET (fd
, &gdb_notifier
.check_masks
[1]);
306 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
308 if (mask
& GDB_EXCEPTION
)
309 FD_SET (fd
, &gdb_notifier
.check_masks
[2]);
311 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
313 if (gdb_notifier
.num_fds
<= fd
)
314 gdb_notifier
.num_fds
= fd
+ 1;
317 file_ptr
->proc
= proc
;
318 file_ptr
->client_data
= client_data
;
319 file_ptr
->mask
= mask
;
322 /* Wrapper function for create_file_handler. */
325 add_file_handler (gdb_fildes_t fd
,
326 handler_func
*proc
, gdb_client_data client_data
)
328 create_file_handler (fd
, GDB_READABLE
| GDB_EXCEPTION
, proc
, client_data
);
331 /* Remove the file descriptor FD from the list of monitored fd's:
332 i.e. we don't care anymore about events on the FD. */
335 delete_file_handler (gdb_fildes_t fd
)
337 file_handler
*file_ptr
, *prev_ptr
= NULL
;
340 /* Find the entry for the given file. */
342 for (file_ptr
= gdb_notifier
.first_file_handler
;
344 file_ptr
= file_ptr
->next_file
)
345 if (file_ptr
->fd
== fd
)
348 if (file_ptr
== NULL
)
351 if (file_ptr
->mask
& GDB_READABLE
)
352 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
353 if (file_ptr
->mask
& GDB_WRITABLE
)
354 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
355 if (file_ptr
->mask
& GDB_EXCEPTION
)
356 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
358 /* Find current max fd. */
360 if ((fd
+ 1) == gdb_notifier
.num_fds
)
362 gdb_notifier
.num_fds
--;
363 for (i
= gdb_notifier
.num_fds
; i
; i
--)
365 if (FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[0])
366 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[1])
367 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[2]))
370 gdb_notifier
.num_fds
= i
;
373 /* Deactivate the file descriptor, by clearing its mask, so that it
374 will not fire again. */
378 /* Get rid of the file handler in the file handler list. */
379 if (file_ptr
== gdb_notifier
.first_file_handler
)
380 gdb_notifier
.first_file_handler
= file_ptr
->next_file
;
383 for (prev_ptr
= gdb_notifier
.first_file_handler
;
384 prev_ptr
->next_file
!= file_ptr
;
385 prev_ptr
= prev_ptr
->next_file
)
387 prev_ptr
->next_file
= file_ptr
->next_file
;
392 /* Handle the given event by calling the procedure associated to the
393 corresponding file handler. Called by process_event indirectly,
394 through event_ptr->proc. EVENT_FILE_DESC is file descriptor of the
395 event in the front of the event queue. */
398 handle_file_event (gdb_fildes_t event_file_desc
)
400 file_handler
*file_ptr
;
403 /* Search the file handler list to find one that matches the fd in
405 for (file_ptr
= gdb_notifier
.first_file_handler
; file_ptr
!= NULL
;
406 file_ptr
= file_ptr
->next_file
)
408 if (file_ptr
->fd
== event_file_desc
)
410 /* See if the desired events (mask) match the received
411 events (ready_mask). */
413 if (file_ptr
->ready_mask
& GDB_EXCEPTION
)
415 fprintf (stderr
, "Exception condition detected on fd %s\n",
416 pfildes (file_ptr
->fd
));
421 mask
= file_ptr
->ready_mask
& file_ptr
->mask
;
423 /* Clear the received events for next time around. */
424 file_ptr
->ready_mask
= 0;
426 /* If there was a match, then call the handler. */
429 if ((*file_ptr
->proc
) (file_ptr
->error
,
430 file_ptr
->client_data
) < 0)
440 /* Create a file event, to be enqueued in the event queue for
441 processing. The procedure associated to this event is always
442 handle_file_event, which will in turn invoke the one that was
443 associated to FD when it was registered with the event loop. */
446 create_file_event (gdb_fildes_t fd
)
448 gdb_event
*file_event_ptr
;
450 file_event_ptr
= xmalloc (sizeof (gdb_event
));
451 file_event_ptr
->proc
= handle_file_event
;
452 file_event_ptr
->fd
= fd
;
453 return file_event_ptr
;
456 /* Called by do_one_event to wait for new events on the monitored file
457 descriptors. Queue file events as they are detected by the poll.
458 If there are no events, this function will block in the call to
459 select. Return -1 if there are no files descriptors to monitor,
460 otherwise return 0. */
463 wait_for_event (void)
465 file_handler
*file_ptr
;
468 /* Make sure all output is done before getting another event. */
472 if (gdb_notifier
.num_fds
== 0)
475 gdb_notifier
.ready_masks
[0] = gdb_notifier
.check_masks
[0];
476 gdb_notifier
.ready_masks
[1] = gdb_notifier
.check_masks
[1];
477 gdb_notifier
.ready_masks
[2] = gdb_notifier
.check_masks
[2];
478 num_found
= select (gdb_notifier
.num_fds
,
479 &gdb_notifier
.ready_masks
[0],
480 &gdb_notifier
.ready_masks
[1],
481 &gdb_notifier
.ready_masks
[2],
484 /* Clear the masks after an error from select. */
487 FD_ZERO (&gdb_notifier
.ready_masks
[0]);
488 FD_ZERO (&gdb_notifier
.ready_masks
[1]);
489 FD_ZERO (&gdb_notifier
.ready_masks
[2]);
491 /* Dont print anything if we got a signal, let gdb handle
494 perror_with_name ("select");
498 /* Enqueue all detected file events. */
500 for (file_ptr
= gdb_notifier
.first_file_handler
;
501 file_ptr
!= NULL
&& num_found
> 0;
502 file_ptr
= file_ptr
->next_file
)
506 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[0]))
507 mask
|= GDB_READABLE
;
508 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[1]))
509 mask
|= GDB_WRITABLE
;
510 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[2]))
511 mask
|= GDB_EXCEPTION
;
518 /* Enqueue an event only if this is still a new event for this
521 if (file_ptr
->ready_mask
== 0)
523 gdb_event
*file_event_ptr
= create_file_event (file_ptr
->fd
);
525 QUEUE_enque (gdb_event_p
, event_queue
, file_event_ptr
);
527 file_ptr
->ready_mask
= mask
;
533 /* Start up the event loop. This is the entry point to the event
537 start_event_loop (void)
539 /* Loop until there is nothing to do. This is the entry point to
540 the event loop engine. If nothing is ready at this time, wait
541 for something to happen (via wait_for_event), then process it.
542 Return when there are no longer event sources to wait for. */
546 /* Any events already waiting in the queue? */
547 int res
= process_event ();
549 /* Did the event handler want the event loop to stop? */
556 /* Process any queued callbacks before we go to sleep. */
557 res
= process_callback ();
559 /* Did the callback want the event loop to stop? */
566 /* Wait for a new event. If wait_for_event returns -1, we
567 should get out because this means that there are no event
568 sources left. This will make the event loop stop, and the
571 if (wait_for_event () < 0)
575 /* We are done with the event loop. There are no more event sources
576 to listen to. So we exit gdbserver. */