1 /* Event loop machinery for the remote server for GDB.
2 Copyright (C) 1999-2013 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>
41 typedef struct gdb_event gdb_event
;
42 typedef int (event_handler_func
) (gdb_fildes_t
);
44 /* Tell create_file_handler what events we are interested in. */
46 #define GDB_READABLE (1<<1)
47 #define GDB_WRITABLE (1<<2)
48 #define GDB_EXCEPTION (1<<3)
50 /* Events are queued by calling 'QUEUE_enque (gdb_event_p, event_queue,
51 file_event_ptr)' and serviced later
52 on by do_one_event. An event can be, for instance, a file
53 descriptor becoming ready to be read. Servicing an event simply
54 means that the procedure PROC will be called. We have 2 queues,
55 one for file handlers that we listen to in the event loop, and one
56 for the file handlers+events that are ready. The procedure PROC
57 associated with each event is always the same (handle_file_event).
58 Its duty is to invoke the handler associated with the file
59 descriptor whose state change generated the event, plus doing other
62 typedef struct gdb_event
64 /* Procedure to call to service this event. */
65 event_handler_func
*proc
;
67 /* File descriptor that is ready. */
71 /* Information about each file descriptor we register with the event
74 typedef struct file_handler
76 /* File descriptor. */
79 /* Events we want to monitor. */
82 /* Events that have been seen since the last time. */
85 /* Procedure to call when fd is ready. */
88 /* Argument to pass to proc. */
89 gdb_client_data client_data
;
91 /* Was an error detected on this fd? */
94 /* Next registered file descriptor. */
95 struct file_handler
*next_file
;
99 DECLARE_QUEUE_P(gdb_event_p
);
100 static QUEUE(gdb_event_p
) *event_queue
= NULL
;
101 DEFINE_QUEUE_P(gdb_event_p
);
103 /* Gdb_notifier is just a list of file descriptors gdb is interested
104 in. These are the input file descriptor, and the target file
105 descriptor. Each of the elements in the gdb_notifier list is
106 basically a description of what kind of events gdb is interested
111 /* Ptr to head of file handler list. */
112 file_handler
*first_file_handler
;
114 /* Masks to be used in the next call to select. Bits are set in
115 response to calls to create_file_handler. */
116 fd_set check_masks
[3];
118 /* What file descriptors were found ready by select. */
119 fd_set ready_masks
[3];
121 /* Number of valid bits (highest fd value + 1). (for select) */
126 /* Callbacks are just routines that are executed before waiting for the
127 next event. In GDB this is struct gdb_timer. We don't need timers
128 so rather than copy all that complexity in gdbserver, we provide what
129 we need, but we do so in a way that if/when the day comes that we need
130 that complexity, it'll be easier to add - replace callbacks with timers
131 and use a delta of zero (which is all gdb currently uses timers for anyway).
133 PROC will be executed before gdbserver goes to sleep to wait for the
136 struct callback_event
139 callback_handler_func
*proc
;
140 gdb_client_data
*data
;
141 struct callback_event
*next
;
144 /* Table of registered callbacks. */
148 struct callback_event
*first
;
149 struct callback_event
*last
;
151 /* Id of the last callback created. */
159 gdb_event_xfree (struct gdb_event
*event
)
165 initialize_event_loop (void)
167 event_queue
= QUEUE_alloc (gdb_event_p
, gdb_event_xfree
);
170 /* Process one event. If an event was processed, 1 is returned
171 otherwise 0 is returned. Scan the queue from head to tail,
172 processing therefore the high priority events first, by invoking
173 the associated event handler procedure. */
178 /* Let's get rid of the event from the event queue. We need to
179 do this now because while processing the event, since the
180 proc function could end up jumping out to the caller of this
181 function. In that case, we would have on the event queue an
182 event which has been processed, but not deleted. */
183 if (!QUEUE_is_empty (gdb_event_p
, event_queue
))
185 gdb_event
*event_ptr
= QUEUE_deque (gdb_event_p
, event_queue
);
186 event_handler_func
*proc
= event_ptr
->proc
;
187 gdb_fildes_t fd
= event_ptr
->fd
;
189 gdb_event_xfree (event_ptr
);
190 /* Now call the procedure associated with the event. */
196 /* This is the case if there are no event on the event queue. */
200 /* Append PROC to the callback list.
201 The result is the "id" of the callback that can be passed back to
202 delete_callback_event. */
205 append_callback_event (callback_handler_func
*proc
, gdb_client_data data
)
207 struct callback_event
*event_ptr
;
209 event_ptr
= xmalloc (sizeof (*event_ptr
));
210 event_ptr
->id
= callback_list
.num_callbacks
++;
211 event_ptr
->proc
= proc
;
212 event_ptr
->data
= data
;
213 event_ptr
->next
= NULL
;
214 if (callback_list
.first
== NULL
)
215 callback_list
.first
= event_ptr
;
216 if (callback_list
.last
!= NULL
)
217 callback_list
.last
->next
= event_ptr
;
218 callback_list
.last
= event_ptr
;
219 return event_ptr
->id
;
222 /* Delete callback ID.
223 It is not an error callback ID doesn't exist. */
226 delete_callback_event (int id
)
228 struct callback_event
**p
;
230 for (p
= &callback_list
.first
; *p
!= NULL
; p
= &(*p
)->next
)
232 struct callback_event
*event_ptr
= *p
;
234 if (event_ptr
->id
== id
)
236 *p
= event_ptr
->next
;
237 if (event_ptr
== callback_list
.last
)
238 callback_list
.last
= NULL
;
245 /* Run the next callback.
246 The result is 1 if a callback was called and event processing
247 should continue, -1 if the callback wants the event loop to exit,
248 and 0 if there are no more callbacks. */
251 process_callback (void)
253 struct callback_event
*event_ptr
;
255 event_ptr
= callback_list
.first
;
256 if (event_ptr
!= NULL
)
258 callback_handler_func
*proc
= event_ptr
->proc
;
259 gdb_client_data
*data
= event_ptr
->data
;
261 /* Remove the event before calling PROC,
262 more events may get added by PROC. */
263 callback_list
.first
= event_ptr
->next
;
264 if (callback_list
.first
== NULL
)
265 callback_list
.last
= NULL
;
275 /* Add a file handler/descriptor to the list of descriptors we are
276 interested in. FD is the file descriptor for the file/stream to be
277 listened to. MASK is a combination of READABLE, WRITABLE,
278 EXCEPTION. PROC is the procedure that will be called when an event
279 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
282 create_file_handler (gdb_fildes_t fd
, int mask
, handler_func
*proc
,
283 gdb_client_data client_data
)
285 file_handler
*file_ptr
;
287 /* Do we already have a file handler for this file? (We may be
288 changing its associated procedure). */
289 for (file_ptr
= gdb_notifier
.first_file_handler
;
291 file_ptr
= file_ptr
->next_file
)
292 if (file_ptr
->fd
== fd
)
295 /* It is a new file descriptor. Add it to the list. Otherwise,
296 just change the data associated with it. */
297 if (file_ptr
== NULL
)
299 file_ptr
= xmalloc (sizeof (*file_ptr
));
301 file_ptr
->ready_mask
= 0;
302 file_ptr
->next_file
= gdb_notifier
.first_file_handler
;
303 gdb_notifier
.first_file_handler
= file_ptr
;
305 if (mask
& GDB_READABLE
)
306 FD_SET (fd
, &gdb_notifier
.check_masks
[0]);
308 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
310 if (mask
& GDB_WRITABLE
)
311 FD_SET (fd
, &gdb_notifier
.check_masks
[1]);
313 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
315 if (mask
& GDB_EXCEPTION
)
316 FD_SET (fd
, &gdb_notifier
.check_masks
[2]);
318 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
320 if (gdb_notifier
.num_fds
<= fd
)
321 gdb_notifier
.num_fds
= fd
+ 1;
324 file_ptr
->proc
= proc
;
325 file_ptr
->client_data
= client_data
;
326 file_ptr
->mask
= mask
;
329 /* Wrapper function for create_file_handler. */
332 add_file_handler (gdb_fildes_t fd
,
333 handler_func
*proc
, gdb_client_data client_data
)
335 create_file_handler (fd
, GDB_READABLE
| GDB_EXCEPTION
, proc
, client_data
);
338 /* Remove the file descriptor FD from the list of monitored fd's:
339 i.e. we don't care anymore about events on the FD. */
342 delete_file_handler (gdb_fildes_t fd
)
344 file_handler
*file_ptr
, *prev_ptr
= NULL
;
347 /* Find the entry for the given file. */
349 for (file_ptr
= gdb_notifier
.first_file_handler
;
351 file_ptr
= file_ptr
->next_file
)
352 if (file_ptr
->fd
== fd
)
355 if (file_ptr
== NULL
)
358 if (file_ptr
->mask
& GDB_READABLE
)
359 FD_CLR (fd
, &gdb_notifier
.check_masks
[0]);
360 if (file_ptr
->mask
& GDB_WRITABLE
)
361 FD_CLR (fd
, &gdb_notifier
.check_masks
[1]);
362 if (file_ptr
->mask
& GDB_EXCEPTION
)
363 FD_CLR (fd
, &gdb_notifier
.check_masks
[2]);
365 /* Find current max fd. */
367 if ((fd
+ 1) == gdb_notifier
.num_fds
)
369 gdb_notifier
.num_fds
--;
370 for (i
= gdb_notifier
.num_fds
; i
; i
--)
372 if (FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[0])
373 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[1])
374 || FD_ISSET (i
- 1, &gdb_notifier
.check_masks
[2]))
377 gdb_notifier
.num_fds
= i
;
380 /* Deactivate the file descriptor, by clearing its mask, so that it
381 will not fire again. */
385 /* Get rid of the file handler in the file handler list. */
386 if (file_ptr
== gdb_notifier
.first_file_handler
)
387 gdb_notifier
.first_file_handler
= file_ptr
->next_file
;
390 for (prev_ptr
= gdb_notifier
.first_file_handler
;
391 prev_ptr
->next_file
!= file_ptr
;
392 prev_ptr
= prev_ptr
->next_file
)
394 prev_ptr
->next_file
= file_ptr
->next_file
;
399 /* Handle the given event by calling the procedure associated to the
400 corresponding file handler. Called by process_event indirectly,
401 through event_ptr->proc. EVENT_FILE_DESC is file descriptor of the
402 event in the front of the event queue. */
405 handle_file_event (gdb_fildes_t event_file_desc
)
407 file_handler
*file_ptr
;
410 /* Search the file handler list to find one that matches the fd in
412 for (file_ptr
= gdb_notifier
.first_file_handler
; file_ptr
!= NULL
;
413 file_ptr
= file_ptr
->next_file
)
415 if (file_ptr
->fd
== event_file_desc
)
417 /* See if the desired events (mask) match the received
418 events (ready_mask). */
420 if (file_ptr
->ready_mask
& GDB_EXCEPTION
)
422 fprintf (stderr
, "Exception condition detected on fd %s\n",
423 pfildes (file_ptr
->fd
));
428 mask
= file_ptr
->ready_mask
& file_ptr
->mask
;
430 /* Clear the received events for next time around. */
431 file_ptr
->ready_mask
= 0;
433 /* If there was a match, then call the handler. */
436 if ((*file_ptr
->proc
) (file_ptr
->error
,
437 file_ptr
->client_data
) < 0)
447 /* Create a file event, to be enqueued in the event queue for
448 processing. The procedure associated to this event is always
449 handle_file_event, which will in turn invoke the one that was
450 associated to FD when it was registered with the event loop. */
453 create_file_event (gdb_fildes_t fd
)
455 gdb_event
*file_event_ptr
;
457 file_event_ptr
= xmalloc (sizeof (gdb_event
));
458 file_event_ptr
->proc
= handle_file_event
;
459 file_event_ptr
->fd
= fd
;
460 return file_event_ptr
;
463 /* Called by do_one_event to wait for new events on the monitored file
464 descriptors. Queue file events as they are detected by the poll.
465 If there are no events, this function will block in the call to
466 select. Return -1 if there are no files descriptors to monitor,
467 otherwise return 0. */
470 wait_for_event (void)
472 file_handler
*file_ptr
;
475 /* Make sure all output is done before getting another event. */
479 if (gdb_notifier
.num_fds
== 0)
482 gdb_notifier
.ready_masks
[0] = gdb_notifier
.check_masks
[0];
483 gdb_notifier
.ready_masks
[1] = gdb_notifier
.check_masks
[1];
484 gdb_notifier
.ready_masks
[2] = gdb_notifier
.check_masks
[2];
485 num_found
= select (gdb_notifier
.num_fds
,
486 &gdb_notifier
.ready_masks
[0],
487 &gdb_notifier
.ready_masks
[1],
488 &gdb_notifier
.ready_masks
[2],
491 /* Clear the masks after an error from select. */
494 FD_ZERO (&gdb_notifier
.ready_masks
[0]);
495 FD_ZERO (&gdb_notifier
.ready_masks
[1]);
496 FD_ZERO (&gdb_notifier
.ready_masks
[2]);
498 /* Dont print anything if we got a signal, let gdb handle
501 perror_with_name ("select");
505 /* Enqueue all detected file events. */
507 for (file_ptr
= gdb_notifier
.first_file_handler
;
508 file_ptr
!= NULL
&& num_found
> 0;
509 file_ptr
= file_ptr
->next_file
)
513 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[0]))
514 mask
|= GDB_READABLE
;
515 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[1]))
516 mask
|= GDB_WRITABLE
;
517 if (FD_ISSET (file_ptr
->fd
, &gdb_notifier
.ready_masks
[2]))
518 mask
|= GDB_EXCEPTION
;
525 /* Enqueue an event only if this is still a new event for this
528 if (file_ptr
->ready_mask
== 0)
530 gdb_event
*file_event_ptr
= create_file_event (file_ptr
->fd
);
532 QUEUE_enque (gdb_event_p
, event_queue
, file_event_ptr
);
534 file_ptr
->ready_mask
= mask
;
540 /* Start up the event loop. This is the entry point to the event
544 start_event_loop (void)
546 /* Loop until there is nothing to do. This is the entry point to
547 the event loop engine. If nothing is ready at this time, wait
548 for something to happen (via wait_for_event), then process it.
549 Return when there are no longer event sources to wait for. */
553 /* Any events already waiting in the queue? */
554 int res
= process_event ();
556 /* Did the event handler want the event loop to stop? */
563 /* Process any queued callbacks before we go to sleep. */
564 res
= process_callback ();
566 /* Did the callback want the event loop to stop? */
573 /* Wait for a new event. If wait_for_event returns -1, we
574 should get out because this means that there are no event
575 sources left. This will make the event loop stop, and the
578 if (wait_for_event () < 0)
582 /* We are done with the event loop. There are no more event sources
583 to listen to. So we exit gdbserver. */