| OLD | NEW |
| 1 /* Event loop machinery for the remote server for GDB. | 1 /* Event loop machinery for the remote server for GDB. |
| 2 Copyright (C) 1999-2002, 2005-2008, 2010-2012 Free Software | 2 Copyright (C) 1999-2013 Free Software Foundation, Inc. |
| 3 Foundation, Inc. | |
| 4 | 3 |
| 5 This file is part of GDB. | 4 This file is part of GDB. |
| 6 | 5 |
| 7 This program is free software; you can redistribute it and/or modify | 6 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 | 7 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 | 8 the Free Software Foundation; either version 3 of the License, or |
| 10 (at your option) any later version. | 9 (at your option) any later version. |
| 11 | 10 |
| 12 This program is distributed in the hope that it will be useful, | 11 This program is distributed in the hope that it will be useful, |
| 13 but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 GNU General Public License for more details. | 14 GNU General Public License for more details. |
| 16 | 15 |
| 17 You should have received a copy of the GNU General Public License | 16 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/>. */ | 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | 18 |
| 20 /* Based on src/gdb/event-loop.c. */ | 19 /* Based on src/gdb/event-loop.c. */ |
| 21 | 20 |
| 22 #include "server.h" | 21 #include "server.h" |
| 22 #include "queue.h" |
| 23 | 23 |
| 24 #include <sys/types.h> | 24 #include <sys/types.h> |
| 25 #include <string.h> | 25 #include <string.h> |
| 26 #include <sys/time.h> | 26 #include <sys/time.h> |
| 27 | 27 |
| 28 #ifdef USE_WIN32API | 28 #ifdef USE_WIN32API |
| 29 #include <windows.h> | 29 #include <windows.h> |
| 30 #include <io.h> | 30 #include <io.h> |
| 31 #endif | 31 #endif |
| 32 | 32 |
| 33 #ifdef HAVE_ERRNO_H | 33 #ifdef HAVE_ERRNO_H |
| 34 #include <errno.h> | 34 #include <errno.h> |
| 35 #endif | 35 #endif |
| 36 | 36 |
| 37 #ifdef HAVE_UNISTD_H | |
| 38 #include <unistd.h> | 37 #include <unistd.h> |
| 39 #endif | |
| 40 | 38 |
| 41 typedef struct gdb_event gdb_event; | 39 typedef struct gdb_event gdb_event; |
| 42 typedef int (event_handler_func) (gdb_fildes_t); | 40 typedef int (event_handler_func) (gdb_fildes_t); |
| 43 | 41 |
| 44 /* Tell create_file_handler what events we are interested in. */ | 42 /* Tell create_file_handler what events we are interested in. */ |
| 45 | 43 |
| 46 #define GDB_READABLE (1<<1) | 44 #define GDB_READABLE (1<<1) |
| 47 #define GDB_WRITABLE (1<<2) | 45 #define GDB_WRITABLE (1<<2) |
| 48 #define GDB_EXCEPTION (1<<3) | 46 #define GDB_EXCEPTION (1<<3) |
| 49 | 47 |
| 50 /* Events are queued by calling async_queue_event and serviced later | 48 /* Events are queued by calling 'QUEUE_enque (gdb_event_p, event_queue, |
| 49 file_event_ptr)' and serviced later |
| 51 on by do_one_event. An event can be, for instance, a file | 50 on by do_one_event. An event can be, for instance, a file |
| 52 descriptor becoming ready to be read. Servicing an event simply | 51 descriptor becoming ready to be read. Servicing an event simply |
| 53 means that the procedure PROC will be called. We have 2 queues, | 52 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 | 53 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 | 54 for the file handlers+events that are ready. The procedure PROC |
| 56 associated with each event is always the same (handle_file_event). | 55 associated with each event is always the same (handle_file_event). |
| 57 Its duty is to invoke the handler associated with the file | 56 Its duty is to invoke the handler associated with the file |
| 58 descriptor whose state change generated the event, plus doing other | 57 descriptor whose state change generated the event, plus doing other |
| 59 cleanups and such. */ | 58 cleanups and such. */ |
| 60 | 59 |
| 61 struct gdb_event | 60 typedef struct gdb_event |
| 62 { | 61 { |
| 63 /* Procedure to call to service this event. */ | 62 /* Procedure to call to service this event. */ |
| 64 event_handler_func *proc; | 63 event_handler_func *proc; |
| 65 | 64 |
| 66 /* File descriptor that is ready. */ | 65 /* File descriptor that is ready. */ |
| 67 gdb_fildes_t fd; | 66 gdb_fildes_t fd; |
| 68 | 67 } *gdb_event_p; |
| 69 /* Next in list of events or NULL. */ | |
| 70 struct gdb_event *next_event; | |
| 71 }; | |
| 72 | 68 |
| 73 /* Information about each file descriptor we register with the event | 69 /* Information about each file descriptor we register with the event |
| 74 loop. */ | 70 loop. */ |
| 75 | 71 |
| 76 typedef struct file_handler | 72 typedef struct file_handler |
| 77 { | 73 { |
| 78 /* File descriptor. */ | 74 /* File descriptor. */ |
| 79 gdb_fildes_t fd; | 75 gdb_fildes_t fd; |
| 80 | 76 |
| 81 /* Events we want to monitor. */ | 77 /* Events we want to monitor. */ |
| 82 int mask; | 78 int mask; |
| 83 | 79 |
| 84 /* Events that have been seen since the last time. */ | 80 /* Events that have been seen since the last time. */ |
| 85 int ready_mask; | 81 int ready_mask; |
| 86 | 82 |
| 87 /* Procedure to call when fd is ready. */ | 83 /* Procedure to call when fd is ready. */ |
| 88 handler_func *proc; | 84 handler_func *proc; |
| 89 | 85 |
| 90 /* Argument to pass to proc. */ | 86 /* Argument to pass to proc. */ |
| 91 gdb_client_data client_data; | 87 gdb_client_data client_data; |
| 92 | 88 |
| 93 /* Was an error detected on this fd? */ | 89 /* Was an error detected on this fd? */ |
| 94 int error; | 90 int error; |
| 95 | 91 |
| 96 /* Next registered file descriptor. */ | 92 /* Next registered file descriptor. */ |
| 97 struct file_handler *next_file; | 93 struct file_handler *next_file; |
| 98 } | 94 } |
| 99 file_handler; | 95 file_handler; |
| 100 | 96 |
| 101 /* Event queue: | 97 DECLARE_QUEUE_P(gdb_event_p); |
| 102 | 98 static QUEUE(gdb_event_p) *event_queue = NULL; |
| 103 Events can be inserted at the front of the queue or at the end of | 99 DEFINE_QUEUE_P(gdb_event_p); |
| 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 | |
| 111 static struct | |
| 112 { | |
| 113 /* The first pending event. */ | |
| 114 gdb_event *first_event; | |
| 115 | |
| 116 /* The last pending event. */ | |
| 117 gdb_event *last_event; | |
| 118 } | |
| 119 event_queue; | |
| 120 | 100 |
| 121 /* Gdb_notifier is just a list of file descriptors gdb is interested | 101 /* Gdb_notifier is just a list of file descriptors gdb is interested |
| 122 in. These are the input file descriptor, and the target file | 102 in. These are the input file descriptor, and the target file |
| 123 descriptor. Each of the elements in the gdb_notifier list is | 103 descriptor. Each of the elements in the gdb_notifier list is |
| 124 basically a description of what kind of events gdb is interested | 104 basically a description of what kind of events gdb is interested |
| 125 in, for each fd. */ | 105 in, for each fd. */ |
| 126 | 106 |
| 127 static struct | 107 static struct |
| 128 { | 108 { |
| 129 /* Ptr to head of file handler list. */ | 109 /* Ptr to head of file handler list. */ |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 static struct | 144 static struct |
| 165 { | 145 { |
| 166 struct callback_event *first; | 146 struct callback_event *first; |
| 167 struct callback_event *last; | 147 struct callback_event *last; |
| 168 | 148 |
| 169 /* Id of the last callback created. */ | 149 /* Id of the last callback created. */ |
| 170 int num_callbacks; | 150 int num_callbacks; |
| 171 } | 151 } |
| 172 callback_list; | 152 callback_list; |
| 173 | 153 |
| 174 /* Insert an event object into the gdb event queue. | 154 /* Free EVENT. */ |
| 175 | |
| 176 EVENT_PTR points to the event to be inserted into the queue. The | |
| 177 caller must allocate memory for the event. It is freed after the | |
| 178 event has ben handled. Events in the queue will be processed head | |
| 179 to tail, therefore, events will be processed first in first | |
| 180 out. */ | |
| 181 | 155 |
| 182 static void | 156 static void |
| 183 async_queue_event (gdb_event *event_ptr) | 157 gdb_event_xfree (struct gdb_event *event) |
| 184 { | 158 { |
| 185 /* The event will become the new last_event. */ | 159 xfree (event); |
| 160 } |
| 186 | 161 |
| 187 event_ptr->next_event = NULL; | 162 void |
| 188 if (event_queue.first_event == NULL) | 163 initialize_event_loop (void) |
| 189 event_queue.first_event = event_ptr; | 164 { |
| 190 else | 165 event_queue = QUEUE_alloc (gdb_event_p, gdb_event_xfree); |
| 191 event_queue.last_event->next_event = event_ptr; | |
| 192 event_queue.last_event = event_ptr; | |
| 193 } | 166 } |
| 194 | 167 |
| 195 /* Process one event. If an event was processed, 1 is returned | 168 /* Process one event. If an event was processed, 1 is returned |
| 196 otherwise 0 is returned. Scan the queue from head to tail, | 169 otherwise 0 is returned. Scan the queue from head to tail, |
| 197 processing therefore the high priority events first, by invoking | 170 processing therefore the high priority events first, by invoking |
| 198 the associated event handler procedure. */ | 171 the associated event handler procedure. */ |
| 199 | 172 |
| 200 static int | 173 static int |
| 201 process_event (void) | 174 process_event (void) |
| 202 { | 175 { |
| 203 gdb_event *event_ptr, *prev_ptr; | 176 /* Let's get rid of the event from the event queue. We need to |
| 204 event_handler_func *proc; | 177 do this now because while processing the event, since the |
| 205 gdb_fildes_t fd; | 178 proc function could end up jumping out to the caller of this |
| 179 function. In that case, we would have on the event queue an |
| 180 event which has been processed, but not deleted. */ |
| 181 if (!QUEUE_is_empty (gdb_event_p, event_queue)) |
| 182 { |
| 183 gdb_event *event_ptr = QUEUE_deque (gdb_event_p, event_queue); |
| 184 event_handler_func *proc = event_ptr->proc; |
| 185 gdb_fildes_t fd = event_ptr->fd; |
| 206 | 186 |
| 207 /* Look in the event queue to find an event that is ready | 187 gdb_event_xfree (event_ptr); |
| 208 to be processed. */ | |
| 209 | |
| 210 for (event_ptr = event_queue.first_event; | |
| 211 event_ptr != NULL; | |
| 212 event_ptr = event_ptr->next_event) | |
| 213 { | |
| 214 /* Call the handler for the event. */ | |
| 215 | |
| 216 proc = event_ptr->proc; | |
| 217 fd = event_ptr->fd; | |
| 218 | |
| 219 /* Let's get rid of the event from the event queue. We need to | |
| 220 do this now because while processing the event, since the | |
| 221 proc function could end up jumping out to the caller of this | |
| 222 function. In that case, we would have on the event queue an | |
| 223 event which has been processed, but not deleted. */ | |
| 224 | |
| 225 if (event_queue.first_event == event_ptr) | |
| 226 » { | |
| 227 » event_queue.first_event = event_ptr->next_event; | |
| 228 » if (event_ptr->next_event == NULL) | |
| 229 » event_queue.last_event = NULL; | |
| 230 » } | |
| 231 else | |
| 232 » { | |
| 233 » prev_ptr = event_queue.first_event; | |
| 234 » while (prev_ptr->next_event != event_ptr) | |
| 235 » prev_ptr = prev_ptr->next_event; | |
| 236 | |
| 237 » prev_ptr->next_event = event_ptr->next_event; | |
| 238 » if (event_ptr->next_event == NULL) | |
| 239 » event_queue.last_event = prev_ptr; | |
| 240 » } | |
| 241 free (event_ptr); | |
| 242 | |
| 243 /* Now call the procedure associated with the event. */ | 188 /* Now call the procedure associated with the event. */ |
| 244 if ((*proc) (fd)) | 189 if ((*proc) (fd)) |
| 245 return -1; | 190 return -1; |
| 246 return 1; | 191 return 1; |
| 247 } | 192 } |
| 248 | 193 |
| 249 /* This is the case if there are no event on the event queue. */ | 194 /* This is the case if there are no event on the event queue. */ |
| 250 return 0; | 195 return 0; |
| 251 } | 196 } |
| 252 | 197 |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 /* Called by do_one_event to wait for new events on the monitored file | 461 /* Called by do_one_event to wait for new events on the monitored file |
| 517 descriptors. Queue file events as they are detected by the poll. | 462 descriptors. Queue file events as they are detected by the poll. |
| 518 If there are no events, this function will block in the call to | 463 If there are no events, this function will block in the call to |
| 519 select. Return -1 if there are no files descriptors to monitor, | 464 select. Return -1 if there are no files descriptors to monitor, |
| 520 otherwise return 0. */ | 465 otherwise return 0. */ |
| 521 | 466 |
| 522 static int | 467 static int |
| 523 wait_for_event (void) | 468 wait_for_event (void) |
| 524 { | 469 { |
| 525 file_handler *file_ptr; | 470 file_handler *file_ptr; |
| 526 gdb_event *file_event_ptr; | |
| 527 int num_found = 0; | 471 int num_found = 0; |
| 528 | 472 |
| 529 /* Make sure all output is done before getting another event. */ | 473 /* Make sure all output is done before getting another event. */ |
| 530 fflush (stdout); | 474 fflush (stdout); |
| 531 fflush (stderr); | 475 fflush (stderr); |
| 532 | 476 |
| 533 if (gdb_notifier.num_fds == 0) | 477 if (gdb_notifier.num_fds == 0) |
| 534 return -1; | 478 return -1; |
| 535 | 479 |
| 536 gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0]; | 480 gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0]; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 if (!mask) | 518 if (!mask) |
| 575 continue; | 519 continue; |
| 576 else | 520 else |
| 577 num_found--; | 521 num_found--; |
| 578 | 522 |
| 579 /* Enqueue an event only if this is still a new event for this | 523 /* Enqueue an event only if this is still a new event for this |
| 580 fd. */ | 524 fd. */ |
| 581 | 525 |
| 582 if (file_ptr->ready_mask == 0) | 526 if (file_ptr->ready_mask == 0) |
| 583 { | 527 { |
| 584 » file_event_ptr = create_file_event (file_ptr->fd); | 528 » gdb_event *file_event_ptr = create_file_event (file_ptr->fd); |
| 585 » async_queue_event (file_event_ptr); | 529 |
| 530 » QUEUE_enque (gdb_event_p, event_queue, file_event_ptr); |
| 586 } | 531 } |
| 587 file_ptr->ready_mask = mask; | 532 file_ptr->ready_mask = mask; |
| 588 } | 533 } |
| 589 | 534 |
| 590 return 0; | 535 return 0; |
| 591 } | 536 } |
| 592 | 537 |
| 593 /* Start up the event loop. This is the entry point to the event | 538 /* Start up the event loop. This is the entry point to the event |
| 594 loop. */ | 539 loop. */ |
| 595 | 540 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 sources left. This will make the event loop stop, and the | 573 sources left. This will make the event loop stop, and the |
| 629 application exit. */ | 574 application exit. */ |
| 630 | 575 |
| 631 if (wait_for_event () < 0) | 576 if (wait_for_event () < 0) |
| 632 return; | 577 return; |
| 633 } | 578 } |
| 634 | 579 |
| 635 /* We are done with the event loop. There are no more event sources | 580 /* We are done with the event loop. There are no more event sources |
| 636 to listen to. So we exit gdbserver. */ | 581 to listen to. So we exit gdbserver. */ |
| 637 } | 582 } |
| OLD | NEW |