Non-stop mode support.
[deliverable/binutils-gdb.git] / gdb / gdbserver / event-loop.c
CommitLineData
bd99dc85
PA
1/* Event loop machinery for the remote server for GDB.
2 Copyright (C) 1999, 2000, 2001, 2002, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20/* Based on src/gdb/event-loop.c. */
21
22#include "server.h"
23
24#include <sys/types.h>
25#include <string.h>
26#include <sys/time.h>
27
28#ifdef USE_WIN32API
29#include <windows.h>
30#include <io.h>
31#endif
32
33#ifdef HAVE_ERRNO_H
34#include <errno.h>
35#endif
36
37typedef struct gdb_event gdb_event;
38typedef void (event_handler_func) (int);
39
40/* Tell create_file_handler what events we are interested in. */
41
42#define GDB_READABLE (1<<1)
43#define GDB_WRITABLE (1<<2)
44#define GDB_EXCEPTION (1<<3)
45
46/* Events are queued by calling async_queue_event and serviced later
47 on by do_one_event. An event can be, for instance, a file
48 descriptor becoming ready to be read. Servicing an event simply
49 means that the procedure PROC will be called. We have 2 queues,
50 one for file handlers that we listen to in the event loop, and one
51 for the file handlers+events that are ready. The procedure PROC
52 associated with each event is always the same (handle_file_event).
53 Its duty is to invoke the handler associated with the file
54 descriptor whose state change generated the event, plus doing other
55 cleanups and such. */
56
57struct gdb_event
58 {
59 /* Procedure to call to service this event. */
60 event_handler_func *proc;
61
62 /* File descriptor that is ready. */
63 int fd;
64
65 /* Next in list of events or NULL. */
66 struct gdb_event *next_event;
67 };
68
69/* Information about each file descriptor we register with the event
70 loop. */
71
72typedef struct file_handler
73 {
74 /* File descriptor. */
75 int fd;
76
77 /* Events we want to monitor. */
78 int mask;
79
80 /* Events that have been seen since the last time. */
81 int ready_mask;
82
83 /* Procedure to call when fd is ready. */
84 handler_func *proc;
85
86 /* Argument to pass to proc. */
87 gdb_client_data client_data;
88
89 /* Was an error detected on this fd? */
90 int error;
91
92 /* Next registered file descriptor. */
93 struct file_handler *next_file;
94 }
95file_handler;
96
97/* Event queue:
98
99 Events can be inserted at the front of the queue or at the end of
100 the queue. Events will be extracted from the queue for processing
101 starting from the head. Therefore, events inserted at the head of
102 the queue will be processed in a last in first out fashion, while
103 those inserted at the tail of the queue will be processed in a
104 first in first out manner. All the fields are NULL if the queue is
105 empty. */
106
107static struct
108 {
109 /* The first pending event. */
110 gdb_event *first_event;
111
112 /* The last pending event. */
113 gdb_event *last_event;
114 }
115event_queue;
116
117/* Gdb_notifier is just a list of file descriptors gdb is interested
118 in. These are the input file descriptor, and the target file
119 descriptor. Each of the elements in the gdb_notifier list is
120 basically a description of what kind of events gdb is interested
121 in, for each fd. */
122
123static struct
124 {
125 /* Ptr to head of file handler list. */
126 file_handler *first_file_handler;
127
128 /* Masks to be used in the next call to select. Bits are set in
129 response to calls to create_file_handler. */
130 fd_set check_masks[3];
131
132 /* What file descriptors were found ready by select. */
133 fd_set ready_masks[3];
134
135 /* Number of valid bits (highest fd value + 1). (for select) */
136 int num_fds;
137 }
138gdb_notifier;
139
140/* Insert an event object into the gdb event queue.
141
142 EVENT_PTR points to the event to be inserted into the queue. The
143 caller must allocate memory for the event. It is freed after the
144 event has ben handled. Events in the queue will be processed head
145 to tail, therefore, events will be processed first in first
146 out. */
147
148static void
149async_queue_event (gdb_event *event_ptr)
150{
151 /* The event will become the new last_event. */
152
153 event_ptr->next_event = NULL;
154 if (event_queue.first_event == NULL)
155 event_queue.first_event = event_ptr;
156 else
157 event_queue.last_event->next_event = event_ptr;
158 event_queue.last_event = event_ptr;
159}
160
161/* Process one event. If an event was processed, 1 is returned
162 otherwise 0 is returned. Scan the queue from head to tail,
163 processing therefore the high priority events first, by invoking
164 the associated event handler procedure. */
165
166static int
167process_event (void)
168{
169 gdb_event *event_ptr, *prev_ptr;
170 event_handler_func *proc;
171 int fd;
172
173 /* Look in the event queue to find an event that is ready
174 to be processed. */
175
176 for (event_ptr = event_queue.first_event;
177 event_ptr != NULL;
178 event_ptr = event_ptr->next_event)
179 {
180 /* Call the handler for the event. */
181
182 proc = event_ptr->proc;
183 fd = event_ptr->fd;
184
185 /* Let's get rid of the event from the event queue. We need to
186 do this now because while processing the event, since the
187 proc function could end up jumping out to the caller of this
188 function. In that case, we would have on the event queue an
189 event which has been processed, but not deleted. */
190
191 if (event_queue.first_event == event_ptr)
192 {
193 event_queue.first_event = event_ptr->next_event;
194 if (event_ptr->next_event == NULL)
195 event_queue.last_event = NULL;
196 }
197 else
198 {
199 prev_ptr = event_queue.first_event;
200 while (prev_ptr->next_event != event_ptr)
201 prev_ptr = prev_ptr->next_event;
202
203 prev_ptr->next_event = event_ptr->next_event;
204 if (event_ptr->next_event == NULL)
205 event_queue.last_event = prev_ptr;
206 }
207 free (event_ptr);
208
209 /* Now call the procedure associated with the event. */
210 (*proc) (fd);
211 return 1;
212 }
213
214 /* This is the case if there are no event on the event queue. */
215 return 0;
216}
217
218/* Add a file handler/descriptor to the list of descriptors we are
219 interested in. FD is the file descriptor for the file/stream to be
220 listened to. MASK is a combination of READABLE, WRITABLE,
221 EXCEPTION. PROC is the procedure that will be called when an event
222 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
223
224static void
225create_file_handler (int fd, int mask, handler_func *proc,
226 gdb_client_data client_data)
227{
228 file_handler *file_ptr;
229
230 /* Do we already have a file handler for this file? (We may be
231 changing its associated procedure). */
232 for (file_ptr = gdb_notifier.first_file_handler;
233 file_ptr != NULL;
234 file_ptr = file_ptr->next_file)
235 if (file_ptr->fd == fd)
236 break;
237
238 /* It is a new file descriptor. Add it to the list. Otherwise,
239 just change the data associated with it. */
240 if (file_ptr == NULL)
241 {
242 file_ptr = xmalloc (sizeof (*file_ptr));
243 file_ptr->fd = fd;
244 file_ptr->ready_mask = 0;
245 file_ptr->next_file = gdb_notifier.first_file_handler;
246 gdb_notifier.first_file_handler = file_ptr;
247
248 if (mask & GDB_READABLE)
249 FD_SET (fd, &gdb_notifier.check_masks[0]);
250 else
251 FD_CLR (fd, &gdb_notifier.check_masks[0]);
252
253 if (mask & GDB_WRITABLE)
254 FD_SET (fd, &gdb_notifier.check_masks[1]);
255 else
256 FD_CLR (fd, &gdb_notifier.check_masks[1]);
257
258 if (mask & GDB_EXCEPTION)
259 FD_SET (fd, &gdb_notifier.check_masks[2]);
260 else
261 FD_CLR (fd, &gdb_notifier.check_masks[2]);
262
263 if (gdb_notifier.num_fds <= fd)
264 gdb_notifier.num_fds = fd + 1;
265 }
266
267 file_ptr->proc = proc;
268 file_ptr->client_data = client_data;
269 file_ptr->mask = mask;
270}
271
272/* Wrapper function for create_file_handler. */
273
274void
275add_file_handler (int fd, handler_func *proc, gdb_client_data client_data)
276{
277 create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION, proc, client_data);
278}
279
280/* Remove the file descriptor FD from the list of monitored fd's:
281 i.e. we don't care anymore about events on the FD. */
282
283void
284delete_file_handler (int fd)
285{
286 file_handler *file_ptr, *prev_ptr = NULL;
287 int i;
288
289 /* Find the entry for the given file. */
290
291 for (file_ptr = gdb_notifier.first_file_handler;
292 file_ptr != NULL;
293 file_ptr = file_ptr->next_file)
294 if (file_ptr->fd == fd)
295 break;
296
297 if (file_ptr == NULL)
298 return;
299
300 if (file_ptr->mask & GDB_READABLE)
301 FD_CLR (fd, &gdb_notifier.check_masks[0]);
302 if (file_ptr->mask & GDB_WRITABLE)
303 FD_CLR (fd, &gdb_notifier.check_masks[1]);
304 if (file_ptr->mask & GDB_EXCEPTION)
305 FD_CLR (fd, &gdb_notifier.check_masks[2]);
306
307 /* Find current max fd. */
308
309 if ((fd + 1) == gdb_notifier.num_fds)
310 {
311 gdb_notifier.num_fds--;
312 for (i = gdb_notifier.num_fds; i; i--)
313 {
314 if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0])
315 || FD_ISSET (i - 1, &gdb_notifier.check_masks[1])
316 || FD_ISSET (i - 1, &gdb_notifier.check_masks[2]))
317 break;
318 }
319 gdb_notifier.num_fds = i;
320 }
321
322 /* Deactivate the file descriptor, by clearing its mask, so that it
323 will not fire again. */
324
325 file_ptr->mask = 0;
326
327 /* Get rid of the file handler in the file handler list. */
328 if (file_ptr == gdb_notifier.first_file_handler)
329 gdb_notifier.first_file_handler = file_ptr->next_file;
330 else
331 {
332 for (prev_ptr = gdb_notifier.first_file_handler;
333 prev_ptr->next_file != file_ptr;
334 prev_ptr = prev_ptr->next_file)
335 ;
336 prev_ptr->next_file = file_ptr->next_file;
337 }
338 free (file_ptr);
339}
340
341/* Handle the given event by calling the procedure associated to the
342 corresponding file handler. Called by process_event indirectly,
343 through event_ptr->proc. EVENT_FILE_DESC is file descriptor of the
344 event in the front of the event queue. */
345
346static void
347handle_file_event (int event_file_desc)
348{
349 file_handler *file_ptr;
350 int mask;
351
352 /* Search the file handler list to find one that matches the fd in
353 the event. */
354 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
355 file_ptr = file_ptr->next_file)
356 {
357 if (file_ptr->fd == event_file_desc)
358 {
359 /* See if the desired events (mask) match the received
360 events (ready_mask). */
361
362 if (file_ptr->ready_mask & GDB_EXCEPTION)
363 {
364 fprintf (stderr, "Exception condition detected on fd %d\n",
365 file_ptr->fd);
366 file_ptr->error = 1;
367 }
368 else
369 file_ptr->error = 0;
370 mask = file_ptr->ready_mask & file_ptr->mask;
371
372 /* Clear the received events for next time around. */
373 file_ptr->ready_mask = 0;
374
375 /* If there was a match, then call the handler. */
376 if (mask != 0)
377 (*file_ptr->proc) (file_ptr->error, file_ptr->client_data);
378 break;
379 }
380 }
381}
382
383/* Create a file event, to be enqueued in the event queue for
384 processing. The procedure associated to this event is always
385 handle_file_event, which will in turn invoke the one that was
386 associated to FD when it was registered with the event loop. */
387
388static gdb_event *
389create_file_event (int fd)
390{
391 gdb_event *file_event_ptr;
392
393 file_event_ptr = xmalloc (sizeof (gdb_event));
394 file_event_ptr->proc = handle_file_event;
395 file_event_ptr->fd = fd;
396 return file_event_ptr;
397}
398
399/* Called by do_one_event to wait for new events on the monitored file
400 descriptors. Queue file events as they are detected by the poll.
401 If there are no events, this function will block in the call to
402 select. Return -1 if there are no files descriptors to monitor,
403 otherwise return 0. */
404
405static int
406wait_for_event (void)
407{
408 file_handler *file_ptr;
409 gdb_event *file_event_ptr;
410 int num_found = 0;
411
412 /* Make sure all output is done before getting another event. */
413 fflush (stdout);
414 fflush (stderr);
415
416 if (gdb_notifier.num_fds == 0)
417 return -1;
418
419 gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0];
420 gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1];
421 gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2];
422 num_found = select (gdb_notifier.num_fds,
423 &gdb_notifier.ready_masks[0],
424 &gdb_notifier.ready_masks[1],
425 &gdb_notifier.ready_masks[2],
426 NULL);
427
428 /* Clear the masks after an error from select. */
429 if (num_found == -1)
430 {
431 FD_ZERO (&gdb_notifier.ready_masks[0]);
432 FD_ZERO (&gdb_notifier.ready_masks[1]);
433 FD_ZERO (&gdb_notifier.ready_masks[2]);
434#ifdef EINTR
435 /* Dont print anything if we got a signal, let gdb handle
436 it. */
437 if (errno != EINTR)
438 perror_with_name ("select");
439#endif
440 }
441
442 /* Enqueue all detected file events. */
443
444 for (file_ptr = gdb_notifier.first_file_handler;
445 file_ptr != NULL && num_found > 0;
446 file_ptr = file_ptr->next_file)
447 {
448 int mask = 0;
449
450 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0]))
451 mask |= GDB_READABLE;
452 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1]))
453 mask |= GDB_WRITABLE;
454 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2]))
455 mask |= GDB_EXCEPTION;
456
457 if (!mask)
458 continue;
459 else
460 num_found--;
461
462 /* Enqueue an event only if this is still a new event for this
463 fd. */
464
465 if (file_ptr->ready_mask == 0)
466 {
467 file_event_ptr = create_file_event (file_ptr->fd);
468 async_queue_event (file_event_ptr);
469 }
470 file_ptr->ready_mask = mask;
471 }
472
473 return 0;
474}
475
476/* Start up the event loop. This is the entry point to the event
477 loop. */
478
479void
480start_event_loop (void)
481{
482 /* Loop until there is nothing to do. This is the entry point to
483 the event loop engine. If nothing is ready at this time, wait
484 for something to happen (via wait_for_event), then process it.
485 Return when there are no longer event sources to wait for. */
486
487 while (1)
488 {
489 /* Any events already waiting in the queue? */
490 if (process_event ())
491 continue;
492
493 /* Wait for a new event. If wait_for_event returns -1, we
494 should get out because this means that there are no event
495 sources left. This will make the event loop stop, and the
496 application exit. */
497
498 if (wait_for_event () < 0)
499 return;
500 }
501
502 /* We are done with the event loop. There are no more event sources
503 to listen to. So we exit gdbserver. */
504}
This page took 0.04106 seconds and 4 git commands to generate.