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