]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/event-loop.c
Move savestring to common/common-utils.c, make gdbserver use it.
[thirdparty/binutils-gdb.git] / gdb / gdbserver / event-loop.c
1 /* Event loop machinery for the remote server for GDB.
2 Copyright (C) 1999-2013 Free Software Foundation, Inc.
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 #include "queue.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
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 typedef struct gdb_event gdb_event;
42 typedef int (event_handler_func) (gdb_fildes_t);
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 '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
60 cleanups and such. */
61
62 typedef struct gdb_event
63 {
64 /* Procedure to call to service this event. */
65 event_handler_func *proc;
66
67 /* File descriptor that is ready. */
68 gdb_fildes_t fd;
69 } *gdb_event_p;
70
71 /* Information about each file descriptor we register with the event
72 loop. */
73
74 typedef struct file_handler
75 {
76 /* File descriptor. */
77 gdb_fildes_t fd;
78
79 /* Events we want to monitor. */
80 int mask;
81
82 /* Events that have been seen since the last time. */
83 int ready_mask;
84
85 /* Procedure to call when fd is ready. */
86 handler_func *proc;
87
88 /* Argument to pass to proc. */
89 gdb_client_data client_data;
90
91 /* Was an error detected on this fd? */
92 int error;
93
94 /* Next registered file descriptor. */
95 struct file_handler *next_file;
96 }
97 file_handler;
98
99 DECLARE_QUEUE_P(gdb_event_p);
100 static QUEUE(gdb_event_p) *event_queue = NULL;
101 DEFINE_QUEUE_P(gdb_event_p);
102
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
107 in, for each fd. */
108
109 static struct
110 {
111 /* Ptr to head of file handler list. */
112 file_handler *first_file_handler;
113
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];
117
118 /* What file descriptors were found ready by select. */
119 fd_set ready_masks[3];
120
121 /* Number of valid bits (highest fd value + 1). (for select) */
122 int num_fds;
123 }
124 gdb_notifier;
125
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).
132
133 PROC will be executed before gdbserver goes to sleep to wait for the
134 next event. */
135
136 struct callback_event
137 {
138 int id;
139 callback_handler_func *proc;
140 gdb_client_data *data;
141 struct callback_event *next;
142 };
143
144 /* Table of registered callbacks. */
145
146 static struct
147 {
148 struct callback_event *first;
149 struct callback_event *last;
150
151 /* Id of the last callback created. */
152 int num_callbacks;
153 }
154 callback_list;
155
156 /* Free EVENT. */
157
158 static void
159 gdb_event_xfree (struct gdb_event *event)
160 {
161 xfree (event);
162 }
163
164 void
165 initialize_event_loop (void)
166 {
167 event_queue = QUEUE_alloc (gdb_event_p, gdb_event_xfree);
168 }
169
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. */
174
175 static int
176 process_event (void)
177 {
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))
184 {
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;
188
189 gdb_event_xfree (event_ptr);
190 /* Now call the procedure associated with the event. */
191 if ((*proc) (fd))
192 return -1;
193 return 1;
194 }
195
196 /* This is the case if there are no event on the event queue. */
197 return 0;
198 }
199
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. */
203
204 int
205 append_callback_event (callback_handler_func *proc, gdb_client_data data)
206 {
207 struct callback_event *event_ptr;
208
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;
220 }
221
222 /* Delete callback ID.
223 It is not an error callback ID doesn't exist. */
224
225 void
226 delete_callback_event (int id)
227 {
228 struct callback_event **p;
229
230 for (p = &callback_list.first; *p != NULL; p = &(*p)->next)
231 {
232 struct callback_event *event_ptr = *p;
233
234 if (event_ptr->id == id)
235 {
236 *p = event_ptr->next;
237 if (event_ptr == callback_list.last)
238 callback_list.last = NULL;
239 free (event_ptr);
240 break;
241 }
242 }
243 }
244
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. */
249
250 static int
251 process_callback (void)
252 {
253 struct callback_event *event_ptr;
254
255 event_ptr = callback_list.first;
256 if (event_ptr != NULL)
257 {
258 callback_handler_func *proc = event_ptr->proc;
259 gdb_client_data *data = event_ptr->data;
260
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;
266 free (event_ptr);
267 if ((*proc) (data))
268 return -1;
269 return 1;
270 }
271
272 return 0;
273 }
274
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. */
280
281 static void
282 create_file_handler (gdb_fildes_t fd, int mask, handler_func *proc,
283 gdb_client_data client_data)
284 {
285 file_handler *file_ptr;
286
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;
290 file_ptr != NULL;
291 file_ptr = file_ptr->next_file)
292 if (file_ptr->fd == fd)
293 break;
294
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)
298 {
299 file_ptr = xmalloc (sizeof (*file_ptr));
300 file_ptr->fd = fd;
301 file_ptr->ready_mask = 0;
302 file_ptr->next_file = gdb_notifier.first_file_handler;
303 gdb_notifier.first_file_handler = file_ptr;
304
305 if (mask & GDB_READABLE)
306 FD_SET (fd, &gdb_notifier.check_masks[0]);
307 else
308 FD_CLR (fd, &gdb_notifier.check_masks[0]);
309
310 if (mask & GDB_WRITABLE)
311 FD_SET (fd, &gdb_notifier.check_masks[1]);
312 else
313 FD_CLR (fd, &gdb_notifier.check_masks[1]);
314
315 if (mask & GDB_EXCEPTION)
316 FD_SET (fd, &gdb_notifier.check_masks[2]);
317 else
318 FD_CLR (fd, &gdb_notifier.check_masks[2]);
319
320 if (gdb_notifier.num_fds <= fd)
321 gdb_notifier.num_fds = fd + 1;
322 }
323
324 file_ptr->proc = proc;
325 file_ptr->client_data = client_data;
326 file_ptr->mask = mask;
327 }
328
329 /* Wrapper function for create_file_handler. */
330
331 void
332 add_file_handler (gdb_fildes_t fd,
333 handler_func *proc, gdb_client_data client_data)
334 {
335 create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION, proc, client_data);
336 }
337
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. */
340
341 void
342 delete_file_handler (gdb_fildes_t fd)
343 {
344 file_handler *file_ptr, *prev_ptr = NULL;
345 int i;
346
347 /* Find the entry for the given file. */
348
349 for (file_ptr = gdb_notifier.first_file_handler;
350 file_ptr != NULL;
351 file_ptr = file_ptr->next_file)
352 if (file_ptr->fd == fd)
353 break;
354
355 if (file_ptr == NULL)
356 return;
357
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]);
364
365 /* Find current max fd. */
366
367 if ((fd + 1) == gdb_notifier.num_fds)
368 {
369 gdb_notifier.num_fds--;
370 for (i = gdb_notifier.num_fds; i; i--)
371 {
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]))
375 break;
376 }
377 gdb_notifier.num_fds = i;
378 }
379
380 /* Deactivate the file descriptor, by clearing its mask, so that it
381 will not fire again. */
382
383 file_ptr->mask = 0;
384
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;
388 else
389 {
390 for (prev_ptr = gdb_notifier.first_file_handler;
391 prev_ptr->next_file != file_ptr;
392 prev_ptr = prev_ptr->next_file)
393 ;
394 prev_ptr->next_file = file_ptr->next_file;
395 }
396 free (file_ptr);
397 }
398
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. */
403
404 static int
405 handle_file_event (gdb_fildes_t event_file_desc)
406 {
407 file_handler *file_ptr;
408 int mask;
409
410 /* Search the file handler list to find one that matches the fd in
411 the event. */
412 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
413 file_ptr = file_ptr->next_file)
414 {
415 if (file_ptr->fd == event_file_desc)
416 {
417 /* See if the desired events (mask) match the received
418 events (ready_mask). */
419
420 if (file_ptr->ready_mask & GDB_EXCEPTION)
421 {
422 fprintf (stderr, "Exception condition detected on fd %s\n",
423 pfildes (file_ptr->fd));
424 file_ptr->error = 1;
425 }
426 else
427 file_ptr->error = 0;
428 mask = file_ptr->ready_mask & file_ptr->mask;
429
430 /* Clear the received events for next time around. */
431 file_ptr->ready_mask = 0;
432
433 /* If there was a match, then call the handler. */
434 if (mask != 0)
435 {
436 if ((*file_ptr->proc) (file_ptr->error,
437 file_ptr->client_data) < 0)
438 return -1;
439 }
440 break;
441 }
442 }
443
444 return 0;
445 }
446
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. */
451
452 static gdb_event *
453 create_file_event (gdb_fildes_t fd)
454 {
455 gdb_event *file_event_ptr;
456
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;
461 }
462
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. */
468
469 static int
470 wait_for_event (void)
471 {
472 file_handler *file_ptr;
473 int num_found = 0;
474
475 /* Make sure all output is done before getting another event. */
476 fflush (stdout);
477 fflush (stderr);
478
479 if (gdb_notifier.num_fds == 0)
480 return -1;
481
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],
489 NULL);
490
491 /* Clear the masks after an error from select. */
492 if (num_found == -1)
493 {
494 FD_ZERO (&gdb_notifier.ready_masks[0]);
495 FD_ZERO (&gdb_notifier.ready_masks[1]);
496 FD_ZERO (&gdb_notifier.ready_masks[2]);
497 #ifdef EINTR
498 /* Dont print anything if we got a signal, let gdb handle
499 it. */
500 if (errno != EINTR)
501 perror_with_name ("select");
502 #endif
503 }
504
505 /* Enqueue all detected file events. */
506
507 for (file_ptr = gdb_notifier.first_file_handler;
508 file_ptr != NULL && num_found > 0;
509 file_ptr = file_ptr->next_file)
510 {
511 int mask = 0;
512
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;
519
520 if (!mask)
521 continue;
522 else
523 num_found--;
524
525 /* Enqueue an event only if this is still a new event for this
526 fd. */
527
528 if (file_ptr->ready_mask == 0)
529 {
530 gdb_event *file_event_ptr = create_file_event (file_ptr->fd);
531
532 QUEUE_enque (gdb_event_p, event_queue, file_event_ptr);
533 }
534 file_ptr->ready_mask = mask;
535 }
536
537 return 0;
538 }
539
540 /* Start up the event loop. This is the entry point to the event
541 loop. */
542
543 void
544 start_event_loop (void)
545 {
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. */
550
551 while (1)
552 {
553 /* Any events already waiting in the queue? */
554 int res = process_event ();
555
556 /* Did the event handler want the event loop to stop? */
557 if (res == -1)
558 return;
559
560 if (res)
561 continue;
562
563 /* Process any queued callbacks before we go to sleep. */
564 res = process_callback ();
565
566 /* Did the callback want the event loop to stop? */
567 if (res == -1)
568 return;
569
570 if (res)
571 continue;
572
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
576 application exit. */
577
578 if (wait_for_event () < 0)
579 return;
580 }
581
582 /* We are done with the event loop. There are no more event sources
583 to listen to. So we exit gdbserver. */
584 }