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