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