GDBserver
|
00001 /* Event loop machinery for the remote server for GDB. 00002 Copyright (C) 1999-2013 Free Software Foundation, Inc. 00003 00004 This file is part of GDB. 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00018 00019 /* Based on src/gdb/event-loop.c. */ 00020 00021 #include "server.h" 00022 #include "queue.h" 00023 00024 #include <sys/types.h> 00025 #include <string.h> 00026 #include <sys/time.h> 00027 00028 #ifdef USE_WIN32API 00029 #include <windows.h> 00030 #include <io.h> 00031 #endif 00032 00033 #ifdef HAVE_ERRNO_H 00034 #include <errno.h> 00035 #endif 00036 00037 #include <unistd.h> 00038 00039 typedef struct gdb_event gdb_event; 00040 typedef int (event_handler_func) (gdb_fildes_t); 00041 00042 /* Tell create_file_handler what events we are interested in. */ 00043 00044 #define GDB_READABLE (1<<1) 00045 #define GDB_WRITABLE (1<<2) 00046 #define GDB_EXCEPTION (1<<3) 00047 00048 /* Events are queued by calling 'QUEUE_enque (gdb_event_p, event_queue, 00049 file_event_ptr)' and serviced later 00050 on by do_one_event. An event can be, for instance, a file 00051 descriptor becoming ready to be read. Servicing an event simply 00052 means that the procedure PROC will be called. We have 2 queues, 00053 one for file handlers that we listen to in the event loop, and one 00054 for the file handlers+events that are ready. The procedure PROC 00055 associated with each event is always the same (handle_file_event). 00056 Its duty is to invoke the handler associated with the file 00057 descriptor whose state change generated the event, plus doing other 00058 cleanups and such. */ 00059 00060 typedef struct gdb_event 00061 { 00062 /* Procedure to call to service this event. */ 00063 event_handler_func *proc; 00064 00065 /* File descriptor that is ready. */ 00066 gdb_fildes_t fd; 00067 } *gdb_event_p; 00068 00069 /* Information about each file descriptor we register with the event 00070 loop. */ 00071 00072 typedef struct file_handler 00073 { 00074 /* File descriptor. */ 00075 gdb_fildes_t fd; 00076 00077 /* Events we want to monitor. */ 00078 int mask; 00079 00080 /* Events that have been seen since the last time. */ 00081 int ready_mask; 00082 00083 /* Procedure to call when fd is ready. */ 00084 handler_func *proc; 00085 00086 /* Argument to pass to proc. */ 00087 gdb_client_data client_data; 00088 00089 /* Was an error detected on this fd? */ 00090 int error; 00091 00092 /* Next registered file descriptor. */ 00093 struct file_handler *next_file; 00094 } 00095 file_handler; 00096 00097 DECLARE_QUEUE_P(gdb_event_p); 00098 static QUEUE(gdb_event_p) *event_queue = NULL; 00099 DEFINE_QUEUE_P(gdb_event_p); 00100 00101 /* Gdb_notifier is just a list of file descriptors gdb is interested 00102 in. These are the input file descriptor, and the target file 00103 descriptor. Each of the elements in the gdb_notifier list is 00104 basically a description of what kind of events gdb is interested 00105 in, for each fd. */ 00106 00107 static struct 00108 { 00109 /* Ptr to head of file handler list. */ 00110 file_handler *first_file_handler; 00111 00112 /* Masks to be used in the next call to select. Bits are set in 00113 response to calls to create_file_handler. */ 00114 fd_set check_masks[3]; 00115 00116 /* What file descriptors were found ready by select. */ 00117 fd_set ready_masks[3]; 00118 00119 /* Number of valid bits (highest fd value + 1). (for select) */ 00120 int num_fds; 00121 } 00122 gdb_notifier; 00123 00124 /* Callbacks are just routines that are executed before waiting for the 00125 next event. In GDB this is struct gdb_timer. We don't need timers 00126 so rather than copy all that complexity in gdbserver, we provide what 00127 we need, but we do so in a way that if/when the day comes that we need 00128 that complexity, it'll be easier to add - replace callbacks with timers 00129 and use a delta of zero (which is all gdb currently uses timers for anyway). 00130 00131 PROC will be executed before gdbserver goes to sleep to wait for the 00132 next event. */ 00133 00134 struct callback_event 00135 { 00136 int id; 00137 callback_handler_func *proc; 00138 gdb_client_data *data; 00139 struct callback_event *next; 00140 }; 00141 00142 /* Table of registered callbacks. */ 00143 00144 static struct 00145 { 00146 struct callback_event *first; 00147 struct callback_event *last; 00148 00149 /* Id of the last callback created. */ 00150 int num_callbacks; 00151 } 00152 callback_list; 00153 00154 /* Free EVENT. */ 00155 00156 static void 00157 gdb_event_xfree (struct gdb_event *event) 00158 { 00159 xfree (event); 00160 } 00161 00162 void 00163 initialize_event_loop (void) 00164 { 00165 event_queue = QUEUE_alloc (gdb_event_p, gdb_event_xfree); 00166 } 00167 00168 /* Process one event. If an event was processed, 1 is returned 00169 otherwise 0 is returned. Scan the queue from head to tail, 00170 processing therefore the high priority events first, by invoking 00171 the associated event handler procedure. */ 00172 00173 static int 00174 process_event (void) 00175 { 00176 /* Let's get rid of the event from the event queue. We need to 00177 do this now because while processing the event, since the 00178 proc function could end up jumping out to the caller of this 00179 function. In that case, we would have on the event queue an 00180 event which has been processed, but not deleted. */ 00181 if (!QUEUE_is_empty (gdb_event_p, event_queue)) 00182 { 00183 gdb_event *event_ptr = QUEUE_deque (gdb_event_p, event_queue); 00184 event_handler_func *proc = event_ptr->proc; 00185 gdb_fildes_t fd = event_ptr->fd; 00186 00187 gdb_event_xfree (event_ptr); 00188 /* Now call the procedure associated with the event. */ 00189 if ((*proc) (fd)) 00190 return -1; 00191 return 1; 00192 } 00193 00194 /* This is the case if there are no event on the event queue. */ 00195 return 0; 00196 } 00197 00198 /* Append PROC to the callback list. 00199 The result is the "id" of the callback that can be passed back to 00200 delete_callback_event. */ 00201 00202 int 00203 append_callback_event (callback_handler_func *proc, gdb_client_data data) 00204 { 00205 struct callback_event *event_ptr; 00206 00207 event_ptr = xmalloc (sizeof (*event_ptr)); 00208 event_ptr->id = callback_list.num_callbacks++; 00209 event_ptr->proc = proc; 00210 event_ptr->data = data; 00211 event_ptr->next = NULL; 00212 if (callback_list.first == NULL) 00213 callback_list.first = event_ptr; 00214 if (callback_list.last != NULL) 00215 callback_list.last->next = event_ptr; 00216 callback_list.last = event_ptr; 00217 return event_ptr->id; 00218 } 00219 00220 /* Delete callback ID. 00221 It is not an error callback ID doesn't exist. */ 00222 00223 void 00224 delete_callback_event (int id) 00225 { 00226 struct callback_event **p; 00227 00228 for (p = &callback_list.first; *p != NULL; p = &(*p)->next) 00229 { 00230 struct callback_event *event_ptr = *p; 00231 00232 if (event_ptr->id == id) 00233 { 00234 *p = event_ptr->next; 00235 if (event_ptr == callback_list.last) 00236 callback_list.last = NULL; 00237 free (event_ptr); 00238 break; 00239 } 00240 } 00241 } 00242 00243 /* Run the next callback. 00244 The result is 1 if a callback was called and event processing 00245 should continue, -1 if the callback wants the event loop to exit, 00246 and 0 if there are no more callbacks. */ 00247 00248 static int 00249 process_callback (void) 00250 { 00251 struct callback_event *event_ptr; 00252 00253 event_ptr = callback_list.first; 00254 if (event_ptr != NULL) 00255 { 00256 callback_handler_func *proc = event_ptr->proc; 00257 gdb_client_data *data = event_ptr->data; 00258 00259 /* Remove the event before calling PROC, 00260 more events may get added by PROC. */ 00261 callback_list.first = event_ptr->next; 00262 if (callback_list.first == NULL) 00263 callback_list.last = NULL; 00264 free (event_ptr); 00265 if ((*proc) (data)) 00266 return -1; 00267 return 1; 00268 } 00269 00270 return 0; 00271 } 00272 00273 /* Add a file handler/descriptor to the list of descriptors we are 00274 interested in. FD is the file descriptor for the file/stream to be 00275 listened to. MASK is a combination of READABLE, WRITABLE, 00276 EXCEPTION. PROC is the procedure that will be called when an event 00277 occurs for FD. CLIENT_DATA is the argument to pass to PROC. */ 00278 00279 static void 00280 create_file_handler (gdb_fildes_t fd, int mask, handler_func *proc, 00281 gdb_client_data client_data) 00282 { 00283 file_handler *file_ptr; 00284 00285 /* Do we already have a file handler for this file? (We may be 00286 changing its associated procedure). */ 00287 for (file_ptr = gdb_notifier.first_file_handler; 00288 file_ptr != NULL; 00289 file_ptr = file_ptr->next_file) 00290 if (file_ptr->fd == fd) 00291 break; 00292 00293 /* It is a new file descriptor. Add it to the list. Otherwise, 00294 just change the data associated with it. */ 00295 if (file_ptr == NULL) 00296 { 00297 file_ptr = xmalloc (sizeof (*file_ptr)); 00298 file_ptr->fd = fd; 00299 file_ptr->ready_mask = 0; 00300 file_ptr->next_file = gdb_notifier.first_file_handler; 00301 gdb_notifier.first_file_handler = file_ptr; 00302 00303 if (mask & GDB_READABLE) 00304 FD_SET (fd, &gdb_notifier.check_masks[0]); 00305 else 00306 FD_CLR (fd, &gdb_notifier.check_masks[0]); 00307 00308 if (mask & GDB_WRITABLE) 00309 FD_SET (fd, &gdb_notifier.check_masks[1]); 00310 else 00311 FD_CLR (fd, &gdb_notifier.check_masks[1]); 00312 00313 if (mask & GDB_EXCEPTION) 00314 FD_SET (fd, &gdb_notifier.check_masks[2]); 00315 else 00316 FD_CLR (fd, &gdb_notifier.check_masks[2]); 00317 00318 if (gdb_notifier.num_fds <= fd) 00319 gdb_notifier.num_fds = fd + 1; 00320 } 00321 00322 file_ptr->proc = proc; 00323 file_ptr->client_data = client_data; 00324 file_ptr->mask = mask; 00325 } 00326 00327 /* Wrapper function for create_file_handler. */ 00328 00329 void 00330 add_file_handler (gdb_fildes_t fd, 00331 handler_func *proc, gdb_client_data client_data) 00332 { 00333 create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION, proc, client_data); 00334 } 00335 00336 /* Remove the file descriptor FD from the list of monitored fd's: 00337 i.e. we don't care anymore about events on the FD. */ 00338 00339 void 00340 delete_file_handler (gdb_fildes_t fd) 00341 { 00342 file_handler *file_ptr, *prev_ptr = NULL; 00343 int i; 00344 00345 /* Find the entry for the given file. */ 00346 00347 for (file_ptr = gdb_notifier.first_file_handler; 00348 file_ptr != NULL; 00349 file_ptr = file_ptr->next_file) 00350 if (file_ptr->fd == fd) 00351 break; 00352 00353 if (file_ptr == NULL) 00354 return; 00355 00356 if (file_ptr->mask & GDB_READABLE) 00357 FD_CLR (fd, &gdb_notifier.check_masks[0]); 00358 if (file_ptr->mask & GDB_WRITABLE) 00359 FD_CLR (fd, &gdb_notifier.check_masks[1]); 00360 if (file_ptr->mask & GDB_EXCEPTION) 00361 FD_CLR (fd, &gdb_notifier.check_masks[2]); 00362 00363 /* Find current max fd. */ 00364 00365 if ((fd + 1) == gdb_notifier.num_fds) 00366 { 00367 gdb_notifier.num_fds--; 00368 for (i = gdb_notifier.num_fds; i; i--) 00369 { 00370 if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0]) 00371 || FD_ISSET (i - 1, &gdb_notifier.check_masks[1]) 00372 || FD_ISSET (i - 1, &gdb_notifier.check_masks[2])) 00373 break; 00374 } 00375 gdb_notifier.num_fds = i; 00376 } 00377 00378 /* Deactivate the file descriptor, by clearing its mask, so that it 00379 will not fire again. */ 00380 00381 file_ptr->mask = 0; 00382 00383 /* Get rid of the file handler in the file handler list. */ 00384 if (file_ptr == gdb_notifier.first_file_handler) 00385 gdb_notifier.first_file_handler = file_ptr->next_file; 00386 else 00387 { 00388 for (prev_ptr = gdb_notifier.first_file_handler; 00389 prev_ptr->next_file != file_ptr; 00390 prev_ptr = prev_ptr->next_file) 00391 ; 00392 prev_ptr->next_file = file_ptr->next_file; 00393 } 00394 free (file_ptr); 00395 } 00396 00397 /* Handle the given event by calling the procedure associated to the 00398 corresponding file handler. Called by process_event indirectly, 00399 through event_ptr->proc. EVENT_FILE_DESC is file descriptor of the 00400 event in the front of the event queue. */ 00401 00402 static int 00403 handle_file_event (gdb_fildes_t event_file_desc) 00404 { 00405 file_handler *file_ptr; 00406 int mask; 00407 00408 /* Search the file handler list to find one that matches the fd in 00409 the event. */ 00410 for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL; 00411 file_ptr = file_ptr->next_file) 00412 { 00413 if (file_ptr->fd == event_file_desc) 00414 { 00415 /* See if the desired events (mask) match the received 00416 events (ready_mask). */ 00417 00418 if (file_ptr->ready_mask & GDB_EXCEPTION) 00419 { 00420 fprintf (stderr, "Exception condition detected on fd %s\n", 00421 pfildes (file_ptr->fd)); 00422 file_ptr->error = 1; 00423 } 00424 else 00425 file_ptr->error = 0; 00426 mask = file_ptr->ready_mask & file_ptr->mask; 00427 00428 /* Clear the received events for next time around. */ 00429 file_ptr->ready_mask = 0; 00430 00431 /* If there was a match, then call the handler. */ 00432 if (mask != 0) 00433 { 00434 if ((*file_ptr->proc) (file_ptr->error, 00435 file_ptr->client_data) < 0) 00436 return -1; 00437 } 00438 break; 00439 } 00440 } 00441 00442 return 0; 00443 } 00444 00445 /* Create a file event, to be enqueued in the event queue for 00446 processing. The procedure associated to this event is always 00447 handle_file_event, which will in turn invoke the one that was 00448 associated to FD when it was registered with the event loop. */ 00449 00450 static gdb_event * 00451 create_file_event (gdb_fildes_t fd) 00452 { 00453 gdb_event *file_event_ptr; 00454 00455 file_event_ptr = xmalloc (sizeof (gdb_event)); 00456 file_event_ptr->proc = handle_file_event; 00457 file_event_ptr->fd = fd; 00458 return file_event_ptr; 00459 } 00460 00461 /* Called by do_one_event to wait for new events on the monitored file 00462 descriptors. Queue file events as they are detected by the poll. 00463 If there are no events, this function will block in the call to 00464 select. Return -1 if there are no files descriptors to monitor, 00465 otherwise return 0. */ 00466 00467 static int 00468 wait_for_event (void) 00469 { 00470 file_handler *file_ptr; 00471 int num_found = 0; 00472 00473 /* Make sure all output is done before getting another event. */ 00474 fflush (stdout); 00475 fflush (stderr); 00476 00477 if (gdb_notifier.num_fds == 0) 00478 return -1; 00479 00480 gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0]; 00481 gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1]; 00482 gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2]; 00483 num_found = select (gdb_notifier.num_fds, 00484 &gdb_notifier.ready_masks[0], 00485 &gdb_notifier.ready_masks[1], 00486 &gdb_notifier.ready_masks[2], 00487 NULL); 00488 00489 /* Clear the masks after an error from select. */ 00490 if (num_found == -1) 00491 { 00492 FD_ZERO (&gdb_notifier.ready_masks[0]); 00493 FD_ZERO (&gdb_notifier.ready_masks[1]); 00494 FD_ZERO (&gdb_notifier.ready_masks[2]); 00495 #ifdef EINTR 00496 /* Dont print anything if we got a signal, let gdb handle 00497 it. */ 00498 if (errno != EINTR) 00499 perror_with_name ("select"); 00500 #endif 00501 } 00502 00503 /* Enqueue all detected file events. */ 00504 00505 for (file_ptr = gdb_notifier.first_file_handler; 00506 file_ptr != NULL && num_found > 0; 00507 file_ptr = file_ptr->next_file) 00508 { 00509 int mask = 0; 00510 00511 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0])) 00512 mask |= GDB_READABLE; 00513 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1])) 00514 mask |= GDB_WRITABLE; 00515 if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2])) 00516 mask |= GDB_EXCEPTION; 00517 00518 if (!mask) 00519 continue; 00520 else 00521 num_found--; 00522 00523 /* Enqueue an event only if this is still a new event for this 00524 fd. */ 00525 00526 if (file_ptr->ready_mask == 0) 00527 { 00528 gdb_event *file_event_ptr = create_file_event (file_ptr->fd); 00529 00530 QUEUE_enque (gdb_event_p, event_queue, file_event_ptr); 00531 } 00532 file_ptr->ready_mask = mask; 00533 } 00534 00535 return 0; 00536 } 00537 00538 /* Start up the event loop. This is the entry point to the event 00539 loop. */ 00540 00541 void 00542 start_event_loop (void) 00543 { 00544 /* Loop until there is nothing to do. This is the entry point to 00545 the event loop engine. If nothing is ready at this time, wait 00546 for something to happen (via wait_for_event), then process it. 00547 Return when there are no longer event sources to wait for. */ 00548 00549 while (1) 00550 { 00551 /* Any events already waiting in the queue? */ 00552 int res = process_event (); 00553 00554 /* Did the event handler want the event loop to stop? */ 00555 if (res == -1) 00556 return; 00557 00558 if (res) 00559 continue; 00560 00561 /* Process any queued callbacks before we go to sleep. */ 00562 res = process_callback (); 00563 00564 /* Did the callback want the event loop to stop? */ 00565 if (res == -1) 00566 return; 00567 00568 if (res) 00569 continue; 00570 00571 /* Wait for a new event. If wait_for_event returns -1, we 00572 should get out because this means that there are no event 00573 sources left. This will make the event loop stop, and the 00574 application exit. */ 00575 00576 if (wait_for_event () < 0) 00577 return; 00578 } 00579 00580 /* We are done with the event loop. There are no more event sources 00581 to listen to. So we exit gdbserver. */ 00582 }