GDB (API)
/home/stan/gdb/src/gdb/event-loop.c
Go to the documentation of this file.
00001 /* Event loop machinery for GDB, the GNU debugger.
00002    Copyright (C) 1999-2013 Free Software Foundation, Inc.
00003    Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "event-loop.h"
00022 #include "event-top.h"
00023 #include "queue.h"
00024 
00025 #ifdef HAVE_POLL
00026 #if defined (HAVE_POLL_H)
00027 #include <poll.h>
00028 #elif defined (HAVE_SYS_POLL_H)
00029 #include <sys/poll.h>
00030 #endif
00031 #endif
00032 
00033 #include <sys/types.h>
00034 #include "gdb_string.h"
00035 #include <errno.h>
00036 #include <sys/time.h>
00037 #include "exceptions.h"
00038 #include "gdb_assert.h"
00039 #include "gdb_select.h"
00040 
00041 /* Tell create_file_handler what events we are interested in.
00042    This is used by the select version of the event loop.  */
00043 
00044 #define GDB_READABLE    (1<<1)
00045 #define GDB_WRITABLE    (1<<2)
00046 #define GDB_EXCEPTION   (1<<3)
00047 
00048 /* Data point to pass to the event handler.  */
00049 typedef union event_data
00050 {
00051   void *ptr;
00052   int integer;
00053 } event_data;
00054 
00055 typedef struct gdb_event gdb_event;
00056 typedef void (event_handler_func) (event_data);
00057 
00058 /* Event for the GDB event system.  Events are queued by calling
00059    async_queue_event and serviced later on by gdb_do_one_event.  An
00060    event can be, for instance, a file descriptor becoming ready to be
00061    read.  Servicing an event simply means that the procedure PROC will
00062    be called.  We have 2 queues, one for file handlers that we listen
00063    to in the event loop, and one for the file handlers+events that are
00064    ready.  The procedure PROC associated with each event is dependant
00065    of the event source.  In the case of monitored file descriptors, it
00066    is always the same (handle_file_event).  Its duty is to invoke the
00067    handler associated with the file descriptor whose state change
00068    generated the event, plus doing other cleanups and such.  In the
00069    case of async signal handlers, it is
00070    invoke_async_signal_handler.  */
00071 
00072 typedef struct gdb_event
00073   {
00074     /* Procedure to call to service this event.  */
00075     event_handler_func *proc;
00076 
00077     /* Data to pass to the event handler.  */
00078     event_data data;
00079   } *gdb_event_p;
00080 
00081 /* Information about each file descriptor we register with the event
00082    loop.  */
00083 
00084 typedef struct file_handler
00085   {
00086     int fd;                     /* File descriptor.  */
00087     int mask;                   /* Events we want to monitor: POLLIN, etc.  */
00088     int ready_mask;             /* Events that have been seen since
00089                                    the last time.  */
00090     handler_func *proc;         /* Procedure to call when fd is ready.  */
00091     gdb_client_data client_data;        /* Argument to pass to proc.  */
00092     int error;                  /* Was an error detected on this fd?  */
00093     struct file_handler *next_file;     /* Next registered file descriptor.  */
00094   }
00095 file_handler;
00096 
00097 /* PROC is a function to be invoked when the READY flag is set.  This
00098    happens when there has been a signal and the corresponding signal
00099    handler has 'triggered' this async_signal_handler for execution.
00100    The actual work to be done in response to a signal will be carried
00101    out by PROC at a later time, within process_event.  This provides a
00102    deferred execution of signal handlers.
00103 
00104    Async_init_signals takes care of setting up such an
00105    async_signal_handler for each interesting signal.  */
00106 
00107 typedef struct async_signal_handler
00108   {
00109     int ready;                      /* If ready, call this handler
00110                                        from the main event loop, using
00111                                        invoke_async_handler.  */
00112     struct async_signal_handler *next_handler;  /* Ptr to next handler.  */
00113     sig_handler_func *proc;         /* Function to call to do the work.  */
00114     gdb_client_data client_data;    /* Argument to async_handler_func.  */
00115   }
00116 async_signal_handler;
00117 
00118 /* PROC is a function to be invoked when the READY flag is set.  This
00119    happens when the event has been marked with
00120    MARK_ASYNC_EVENT_HANDLER.  The actual work to be done in response
00121    to an event will be carried out by PROC at a later time, within
00122    process_event.  This provides a deferred execution of event
00123    handlers.  */
00124 typedef struct async_event_handler
00125   {
00126     /* If ready, call this handler from the main event loop, using
00127        invoke_event_handler.  */
00128     int ready;
00129 
00130     /* Point to next handler.  */
00131     struct async_event_handler *next_handler;
00132 
00133     /* Function to call to do the work.  */
00134     async_event_handler_func *proc;
00135 
00136     /* Argument to PROC.  */
00137     gdb_client_data client_data;
00138   }
00139 async_event_handler;
00140 
00141 DECLARE_QUEUE_P(gdb_event_p);
00142 DEFINE_QUEUE_P(gdb_event_p);
00143 static QUEUE(gdb_event_p) *event_queue = NULL;
00144 
00145 /* Gdb_notifier is just a list of file descriptors gdb is interested in.
00146    These are the input file descriptor, and the target file
00147    descriptor.  We have two flavors of the notifier, one for platforms
00148    that have the POLL function, the other for those that don't, and
00149    only support SELECT.  Each of the elements in the gdb_notifier list is
00150    basically a description of what kind of events gdb is interested
00151    in, for each fd.  */
00152 
00153 /* As of 1999-04-30 only the input file descriptor is registered with the
00154    event loop.  */
00155 
00156 /* Do we use poll or select ? */
00157 #ifdef HAVE_POLL
00158 #define USE_POLL 1
00159 #else
00160 #define USE_POLL 0
00161 #endif /* HAVE_POLL */
00162 
00163 static unsigned char use_poll = USE_POLL;
00164 
00165 #ifdef USE_WIN32API
00166 #include <windows.h>
00167 #include <io.h>
00168 #endif
00169 
00170 static struct
00171   {
00172     /* Ptr to head of file handler list.  */
00173     file_handler *first_file_handler;
00174 
00175 #ifdef HAVE_POLL
00176     /* Ptr to array of pollfd structures.  */
00177     struct pollfd *poll_fds;
00178 
00179     /* Timeout in milliseconds for calls to poll().  */
00180     int poll_timeout;
00181 #endif
00182 
00183     /* Masks to be used in the next call to select.
00184        Bits are set in response to calls to create_file_handler.  */
00185     fd_set check_masks[3];
00186 
00187     /* What file descriptors were found ready by select.  */
00188     fd_set ready_masks[3];
00189 
00190     /* Number of file descriptors to monitor (for poll).  */
00191     /* Number of valid bits (highest fd value + 1) (for select).  */
00192     int num_fds;
00193 
00194     /* Time structure for calls to select().  */
00195     struct timeval select_timeout;
00196 
00197     /* Flag to tell whether the timeout should be used.  */
00198     int timeout_valid;
00199   }
00200 gdb_notifier;
00201 
00202 /* Structure associated with a timer.  PROC will be executed at the
00203    first occasion after WHEN.  */
00204 struct gdb_timer
00205   {
00206     struct timeval when;
00207     int timer_id;
00208     struct gdb_timer *next;
00209     timer_handler_func *proc;       /* Function to call to do the work.  */
00210     gdb_client_data client_data;    /* Argument to async_handler_func.  */
00211   };
00212 
00213 /* List of currently active timers.  It is sorted in order of
00214    increasing timers.  */
00215 static struct
00216   {
00217     /* Pointer to first in timer list.  */
00218     struct gdb_timer *first_timer;
00219 
00220     /* Id of the last timer created.  */
00221     int num_timers;
00222   }
00223 timer_list;
00224 
00225 /* All the async_signal_handlers gdb is interested in are kept onto
00226    this list.  */
00227 static struct
00228   {
00229     /* Pointer to first in handler list.  */
00230     async_signal_handler *first_handler;
00231 
00232     /* Pointer to last in handler list.  */
00233     async_signal_handler *last_handler;
00234   }
00235 sighandler_list;
00236 
00237 /* All the async_event_handlers gdb is interested in are kept onto
00238    this list.  */
00239 static struct
00240   {
00241     /* Pointer to first in handler list.  */
00242     async_event_handler *first_handler;
00243 
00244     /* Pointer to last in handler list.  */
00245     async_event_handler *last_handler;
00246   }
00247 async_event_handler_list;
00248 
00249 static int invoke_async_signal_handlers (void);
00250 static void create_file_handler (int fd, int mask, handler_func *proc,
00251                                  gdb_client_data client_data);
00252 static void handle_file_event (event_data data);
00253 static void check_async_event_handlers (void);
00254 static int gdb_wait_for_event (int);
00255 static void poll_timers (void);
00256 
00257 
00258 /* Create a generic event, to be enqueued in the event queue for
00259    processing.  PROC is the procedure associated to the event.  DATA
00260    is passed to PROC upon PROC invocation.  */
00261 
00262 static gdb_event *
00263 create_event (event_handler_func proc, event_data data)
00264 {
00265   gdb_event *event;
00266 
00267   event = xmalloc (sizeof (*event));
00268   event->proc = proc;
00269   event->data = data;
00270 
00271   return event;
00272 }
00273 
00274 /* Create a file event, to be enqueued in the event queue for
00275    processing.  The procedure associated to this event is always
00276    handle_file_event, which will in turn invoke the one that was
00277    associated to FD when it was registered with the event loop.  */
00278 static gdb_event *
00279 create_file_event (int fd)
00280 {
00281   event_data data;
00282 
00283   data.integer = fd;
00284   return create_event (handle_file_event, data);
00285 }
00286 
00287 
00288 /* Free EVENT.  */
00289 
00290 static void
00291 gdb_event_xfree (struct gdb_event *event)
00292 {
00293   xfree (event);
00294 }
00295 
00296 /* Initialize the event queue.  */
00297 
00298 void
00299 initialize_event_loop (void)
00300 {
00301   event_queue = QUEUE_alloc (gdb_event_p, gdb_event_xfree);
00302 }
00303 
00304 /* Process one event.
00305    The event can be the next one to be serviced in the event queue,
00306    or an asynchronous event handler can be invoked in response to
00307    the reception of a signal.
00308    If an event was processed (either way), 1 is returned otherwise
00309    0 is returned.
00310    Scan the queue from head to tail, processing therefore the high
00311    priority events first, by invoking the associated event handler
00312    procedure.  */
00313 static int
00314 process_event (void)
00315 {
00316   /* First let's see if there are any asynchronous event handlers that
00317      are ready.  These would be the result of invoking any of the
00318      signal handlers.  */
00319 
00320   if (invoke_async_signal_handlers ())
00321     return 1;
00322 
00323   /* Look in the event queue to find an event that is ready
00324      to be processed.  */
00325 
00326   if (!QUEUE_is_empty (gdb_event_p, event_queue))
00327     {
00328       /* Let's get rid of the event from the event queue.  We need to
00329          do this now because while processing the event, the proc
00330          function could end up calling 'error' and therefore jump out
00331          to the caller of this function, gdb_do_one_event.  In that
00332          case, we would have on the event queue an event wich has been
00333          processed, but not deleted.  */
00334       gdb_event *event_ptr = QUEUE_deque (gdb_event_p, event_queue);
00335       /* Call the handler for the event.  */
00336       event_handler_func *proc = event_ptr->proc;
00337       event_data data = event_ptr->data;
00338 
00339       gdb_event_xfree (event_ptr);
00340 
00341       /* Now call the procedure associated with the event.  */
00342       (*proc) (data);
00343       return 1;
00344     }
00345 
00346   /* This is the case if there are no event on the event queue.  */
00347   return 0;
00348 }
00349 
00350 /* Process one high level event.  If nothing is ready at this time,
00351    wait for something to happen (via gdb_wait_for_event), then process
00352    it.  Returns >0 if something was done otherwise returns <0 (this
00353    can happen if there are no event sources to wait for).  */
00354 
00355 int
00356 gdb_do_one_event (void)
00357 {
00358   static int event_source_head = 0;
00359   const int number_of_sources = 3;
00360   int current = 0;
00361 
00362   /* Any events already waiting in the queue?  */
00363   if (process_event ())
00364     return 1;
00365 
00366   /* To level the fairness across event sources, we poll them in a
00367      round-robin fashion.  */
00368   for (current = 0; current < number_of_sources; current++)
00369     {
00370       switch (event_source_head)
00371         {
00372         case 0:
00373           /* Are any timers that are ready? If so, put an event on the
00374              queue.  */
00375           poll_timers ();
00376           break;
00377         case 1:
00378           /* Are there events already waiting to be collected on the
00379              monitored file descriptors?  */
00380           gdb_wait_for_event (0);
00381           break;
00382         case 2:
00383           /* Are there any asynchronous event handlers ready?  */
00384           check_async_event_handlers ();
00385           break;
00386         }
00387 
00388       event_source_head++;
00389       if (event_source_head == number_of_sources)
00390         event_source_head = 0;
00391     }
00392 
00393   /* Handle any new events collected.  */
00394   if (process_event ())
00395     return 1;
00396 
00397   /* Block waiting for a new event.  If gdb_wait_for_event returns -1,
00398      we should get out because this means that there are no event
00399      sources left.  This will make the event loop stop, and the
00400      application exit.  */
00401 
00402   if (gdb_wait_for_event (1) < 0)
00403     return -1;
00404 
00405   /* Handle any new events occurred while waiting.  */
00406   if (process_event ())
00407     return 1;
00408 
00409   /* If gdb_wait_for_event has returned 1, it means that one event has
00410      been handled.  We break out of the loop.  */
00411   return 1;
00412 }
00413 
00414 /* Start up the event loop.  This is the entry point to the event loop
00415    from the command loop.  */
00416 
00417 void
00418 start_event_loop (void)
00419 {
00420   /* Loop until there is nothing to do.  This is the entry point to
00421      the event loop engine.  gdb_do_one_event will process one event
00422      for each invocation.  It blocks waiting for an event and then
00423      processes it.  */
00424   while (1)
00425     {
00426       volatile struct gdb_exception ex;
00427       int result = 0;
00428 
00429       TRY_CATCH (ex, RETURN_MASK_ALL)
00430         {
00431           result = gdb_do_one_event ();
00432         }
00433       if (ex.reason < 0)
00434         {
00435           exception_print (gdb_stderr, ex);
00436 
00437           /* If any exception escaped to here, we better enable
00438              stdin.  Otherwise, any command that calls async_disable_stdin,
00439              and then throws, will leave stdin inoperable.  */
00440           async_enable_stdin ();
00441           /* If we long-jumped out of do_one_event, we probably didn't
00442              get around to resetting the prompt, which leaves readline
00443              in a messed-up state.  Reset it here.  */
00444           /* FIXME: this should really be a call to a hook that is
00445              interface specific, because interfaces can display the
00446              prompt in their own way.  */
00447           display_gdb_prompt (0);
00448           /* This call looks bizarre, but it is required.  If the user
00449              entered a command that caused an error,
00450              after_char_processing_hook won't be called from
00451              rl_callback_read_char_wrapper.  Using a cleanup there
00452              won't work, since we want this function to be called
00453              after a new prompt is printed.  */
00454           if (after_char_processing_hook)
00455             (*after_char_processing_hook) ();
00456           /* Maybe better to set a flag to be checked somewhere as to
00457              whether display the prompt or not.  */
00458         }
00459       if (result < 0)
00460         break;
00461     }
00462 
00463   /* We are done with the event loop.  There are no more event sources
00464      to listen to.  So we exit GDB.  */
00465   return;
00466 }
00467 
00468 
00469 /* Wrapper function for create_file_handler, so that the caller
00470    doesn't have to know implementation details about the use of poll
00471    vs. select.  */
00472 void
00473 add_file_handler (int fd, handler_func * proc, gdb_client_data client_data)
00474 {
00475 #ifdef HAVE_POLL
00476   struct pollfd fds;
00477 #endif
00478 
00479   if (use_poll)
00480     {
00481 #ifdef HAVE_POLL
00482       /* Check to see if poll () is usable.  If not, we'll switch to
00483          use select.  This can happen on systems like
00484          m68k-motorola-sys, `poll' cannot be used to wait for `stdin'.
00485          On m68k-motorola-sysv, tty's are not stream-based and not
00486          `poll'able.  */
00487       fds.fd = fd;
00488       fds.events = POLLIN;
00489       if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL))
00490         use_poll = 0;
00491 #else
00492       internal_error (__FILE__, __LINE__,
00493                       _("use_poll without HAVE_POLL"));
00494 #endif /* HAVE_POLL */
00495     }
00496   if (use_poll)
00497     {
00498 #ifdef HAVE_POLL
00499       create_file_handler (fd, POLLIN, proc, client_data);
00500 #else
00501       internal_error (__FILE__, __LINE__,
00502                       _("use_poll without HAVE_POLL"));
00503 #endif
00504     }
00505   else
00506     create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION, 
00507                          proc, client_data);
00508 }
00509 
00510 /* Add a file handler/descriptor to the list of descriptors we are
00511    interested in.
00512 
00513    FD is the file descriptor for the file/stream to be listened to.
00514 
00515    For the poll case, MASK is a combination (OR) of POLLIN,
00516    POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND:
00517    these are the events we are interested in.  If any of them occurs,
00518    proc should be called.
00519 
00520    For the select case, MASK is a combination of READABLE, WRITABLE,
00521    EXCEPTION.  PROC is the procedure that will be called when an event
00522    occurs for FD.  CLIENT_DATA is the argument to pass to PROC.  */
00523 
00524 static void
00525 create_file_handler (int fd, int mask, handler_func * proc, 
00526                      gdb_client_data client_data)
00527 {
00528   file_handler *file_ptr;
00529 
00530   /* Do we already have a file handler for this file?  (We may be
00531      changing its associated procedure).  */
00532   for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
00533        file_ptr = file_ptr->next_file)
00534     {
00535       if (file_ptr->fd == fd)
00536         break;
00537     }
00538 
00539   /* It is a new file descriptor.  Add it to the list.  Otherwise, just
00540      change the data associated with it.  */
00541   if (file_ptr == NULL)
00542     {
00543       file_ptr = (file_handler *) xmalloc (sizeof (file_handler));
00544       file_ptr->fd = fd;
00545       file_ptr->ready_mask = 0;
00546       file_ptr->next_file = gdb_notifier.first_file_handler;
00547       gdb_notifier.first_file_handler = file_ptr;
00548 
00549       if (use_poll)
00550         {
00551 #ifdef HAVE_POLL
00552           gdb_notifier.num_fds++;
00553           if (gdb_notifier.poll_fds)
00554             gdb_notifier.poll_fds =
00555               (struct pollfd *) xrealloc (gdb_notifier.poll_fds,
00556                                           (gdb_notifier.num_fds
00557                                            * sizeof (struct pollfd)));
00558           else
00559             gdb_notifier.poll_fds =
00560               (struct pollfd *) xmalloc (sizeof (struct pollfd));
00561           (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd = fd;
00562           (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events = mask;
00563           (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents = 0;
00564 #else
00565           internal_error (__FILE__, __LINE__,
00566                           _("use_poll without HAVE_POLL"));
00567 #endif /* HAVE_POLL */
00568         }
00569       else
00570         {
00571           if (mask & GDB_READABLE)
00572             FD_SET (fd, &gdb_notifier.check_masks[0]);
00573           else
00574             FD_CLR (fd, &gdb_notifier.check_masks[0]);
00575 
00576           if (mask & GDB_WRITABLE)
00577             FD_SET (fd, &gdb_notifier.check_masks[1]);
00578           else
00579             FD_CLR (fd, &gdb_notifier.check_masks[1]);
00580 
00581           if (mask & GDB_EXCEPTION)
00582             FD_SET (fd, &gdb_notifier.check_masks[2]);
00583           else
00584             FD_CLR (fd, &gdb_notifier.check_masks[2]);
00585 
00586           if (gdb_notifier.num_fds <= fd)
00587             gdb_notifier.num_fds = fd + 1;
00588         }
00589     }
00590 
00591   file_ptr->proc = proc;
00592   file_ptr->client_data = client_data;
00593   file_ptr->mask = mask;
00594 }
00595 
00596 /* Remove the file descriptor FD from the list of monitored fd's: 
00597    i.e. we don't care anymore about events on the FD.  */
00598 void
00599 delete_file_handler (int fd)
00600 {
00601   file_handler *file_ptr, *prev_ptr = NULL;
00602   int i;
00603 #ifdef HAVE_POLL
00604   int j;
00605   struct pollfd *new_poll_fds;
00606 #endif
00607 
00608   /* Find the entry for the given file.  */
00609 
00610   for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
00611        file_ptr = file_ptr->next_file)
00612     {
00613       if (file_ptr->fd == fd)
00614         break;
00615     }
00616 
00617   if (file_ptr == NULL)
00618     return;
00619 
00620   if (use_poll)
00621     {
00622 #ifdef HAVE_POLL
00623       /* Create a new poll_fds array by copying every fd's information
00624          but the one we want to get rid of.  */
00625 
00626       new_poll_fds = (struct pollfd *) 
00627         xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd));
00628 
00629       for (i = 0, j = 0; i < gdb_notifier.num_fds; i++)
00630         {
00631           if ((gdb_notifier.poll_fds + i)->fd != fd)
00632             {
00633               (new_poll_fds + j)->fd = (gdb_notifier.poll_fds + i)->fd;
00634               (new_poll_fds + j)->events = (gdb_notifier.poll_fds + i)->events;
00635               (new_poll_fds + j)->revents
00636                 = (gdb_notifier.poll_fds + i)->revents;
00637               j++;
00638             }
00639         }
00640       xfree (gdb_notifier.poll_fds);
00641       gdb_notifier.poll_fds = new_poll_fds;
00642       gdb_notifier.num_fds--;
00643 #else
00644       internal_error (__FILE__, __LINE__,
00645                       _("use_poll without HAVE_POLL"));
00646 #endif /* HAVE_POLL */
00647     }
00648   else
00649     {
00650       if (file_ptr->mask & GDB_READABLE)
00651         FD_CLR (fd, &gdb_notifier.check_masks[0]);
00652       if (file_ptr->mask & GDB_WRITABLE)
00653         FD_CLR (fd, &gdb_notifier.check_masks[1]);
00654       if (file_ptr->mask & GDB_EXCEPTION)
00655         FD_CLR (fd, &gdb_notifier.check_masks[2]);
00656 
00657       /* Find current max fd.  */
00658 
00659       if ((fd + 1) == gdb_notifier.num_fds)
00660         {
00661           gdb_notifier.num_fds--;
00662           for (i = gdb_notifier.num_fds; i; i--)
00663             {
00664               if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0])
00665                   || FD_ISSET (i - 1, &gdb_notifier.check_masks[1])
00666                   || FD_ISSET (i - 1, &gdb_notifier.check_masks[2]))
00667                 break;
00668             }
00669           gdb_notifier.num_fds = i;
00670         }
00671     }
00672 
00673   /* Deactivate the file descriptor, by clearing its mask, 
00674      so that it will not fire again.  */
00675 
00676   file_ptr->mask = 0;
00677 
00678   /* Get rid of the file handler in the file handler list.  */
00679   if (file_ptr == gdb_notifier.first_file_handler)
00680     gdb_notifier.first_file_handler = file_ptr->next_file;
00681   else
00682     {
00683       for (prev_ptr = gdb_notifier.first_file_handler;
00684            prev_ptr->next_file != file_ptr;
00685            prev_ptr = prev_ptr->next_file)
00686         ;
00687       prev_ptr->next_file = file_ptr->next_file;
00688     }
00689   xfree (file_ptr);
00690 }
00691 
00692 /* Handle the given event by calling the procedure associated to the
00693    corresponding file handler.  Called by process_event indirectly,
00694    through event_ptr->proc.  EVENT_FILE_DESC is file descriptor of the
00695    event in the front of the event queue.  */
00696 static void
00697 handle_file_event (event_data data)
00698 {
00699   file_handler *file_ptr;
00700   int mask;
00701 #ifdef HAVE_POLL
00702   int error_mask;
00703 #endif
00704   int event_file_desc = data.integer;
00705 
00706   /* Search the file handler list to find one that matches the fd in
00707      the event.  */
00708   for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
00709        file_ptr = file_ptr->next_file)
00710     {
00711       if (file_ptr->fd == event_file_desc)
00712         {
00713           /* With poll, the ready_mask could have any of three events
00714              set to 1: POLLHUP, POLLERR, POLLNVAL.  These events
00715              cannot be used in the requested event mask (events), but
00716              they can be returned in the return mask (revents).  We
00717              need to check for those event too, and add them to the
00718              mask which will be passed to the handler.  */
00719 
00720           /* See if the desired events (mask) match the received
00721              events (ready_mask).  */
00722 
00723           if (use_poll)
00724             {
00725 #ifdef HAVE_POLL
00726               /* POLLHUP means EOF, but can be combined with POLLIN to
00727                  signal more data to read.  */
00728               error_mask = POLLHUP | POLLERR | POLLNVAL;
00729               mask = file_ptr->ready_mask & (file_ptr->mask | error_mask);
00730 
00731               if ((mask & (POLLERR | POLLNVAL)) != 0)
00732                 {
00733                   /* Work in progress.  We may need to tell somebody
00734                      what kind of error we had.  */
00735                   if (mask & POLLERR)
00736                     printf_unfiltered (_("Error detected on fd %d\n"),
00737                                        file_ptr->fd);
00738                   if (mask & POLLNVAL)
00739                     printf_unfiltered (_("Invalid or non-`poll'able fd %d\n"),
00740                                        file_ptr->fd);
00741                   file_ptr->error = 1;
00742                 }
00743               else
00744                 file_ptr->error = 0;
00745 #else
00746               internal_error (__FILE__, __LINE__,
00747                               _("use_poll without HAVE_POLL"));
00748 #endif /* HAVE_POLL */
00749             }
00750           else
00751             {
00752               if (file_ptr->ready_mask & GDB_EXCEPTION)
00753                 {
00754                   printf_unfiltered (_("Exception condition detected "
00755                                        "on fd %d\n"), file_ptr->fd);
00756                   file_ptr->error = 1;
00757                 }
00758               else
00759                 file_ptr->error = 0;
00760               mask = file_ptr->ready_mask & file_ptr->mask;
00761             }
00762 
00763           /* Clear the received events for next time around.  */
00764           file_ptr->ready_mask = 0;
00765 
00766           /* If there was a match, then call the handler.  */
00767           if (mask != 0)
00768             (*file_ptr->proc) (file_ptr->error, file_ptr->client_data);
00769           break;
00770         }
00771     }
00772 }
00773 
00774 /* Called by gdb_do_one_event to wait for new events on the monitored
00775    file descriptors.  Queue file events as they are detected by the
00776    poll.  If BLOCK and if there are no events, this function will
00777    block in the call to poll.  Return -1 if there are no file
00778    descriptors to monitor, otherwise return 0.  */
00779 static int
00780 gdb_wait_for_event (int block)
00781 {
00782   file_handler *file_ptr;
00783   gdb_event *file_event_ptr;
00784   int num_found = 0;
00785   int i;
00786 
00787   /* Make sure all output is done before getting another event.  */
00788   gdb_flush (gdb_stdout);
00789   gdb_flush (gdb_stderr);
00790 
00791   if (gdb_notifier.num_fds == 0)
00792     return -1;
00793 
00794   if (use_poll)
00795     {
00796 #ifdef HAVE_POLL
00797       int timeout;
00798 
00799       if (block)
00800         timeout = gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1;
00801       else
00802         timeout = 0;
00803 
00804       num_found = poll (gdb_notifier.poll_fds,
00805                         (unsigned long) gdb_notifier.num_fds, timeout);
00806 
00807       /* Don't print anything if we get out of poll because of a
00808          signal.  */
00809       if (num_found == -1 && errno != EINTR)
00810         perror_with_name (("poll"));
00811 #else
00812       internal_error (__FILE__, __LINE__,
00813                       _("use_poll without HAVE_POLL"));
00814 #endif /* HAVE_POLL */
00815     }
00816   else
00817     {
00818       struct timeval select_timeout;
00819       struct timeval *timeout_p;
00820 
00821       if (block)
00822         timeout_p = gdb_notifier.timeout_valid
00823           ? &gdb_notifier.select_timeout : NULL;
00824       else
00825         {
00826           memset (&select_timeout, 0, sizeof (select_timeout));
00827           timeout_p = &select_timeout;
00828         }
00829 
00830       gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0];
00831       gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1];
00832       gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2];
00833       num_found = gdb_select (gdb_notifier.num_fds,
00834                               &gdb_notifier.ready_masks[0],
00835                               &gdb_notifier.ready_masks[1],
00836                               &gdb_notifier.ready_masks[2],
00837                               timeout_p);
00838 
00839       /* Clear the masks after an error from select.  */
00840       if (num_found == -1)
00841         {
00842           FD_ZERO (&gdb_notifier.ready_masks[0]);
00843           FD_ZERO (&gdb_notifier.ready_masks[1]);
00844           FD_ZERO (&gdb_notifier.ready_masks[2]);
00845 
00846           /* Dont print anything if we got a signal, let gdb handle
00847              it.  */
00848           if (errno != EINTR)
00849             perror_with_name (("select"));
00850         }
00851     }
00852 
00853   /* Enqueue all detected file events.  */
00854 
00855   if (use_poll)
00856     {
00857 #ifdef HAVE_POLL
00858       for (i = 0; (i < gdb_notifier.num_fds) && (num_found > 0); i++)
00859         {
00860           if ((gdb_notifier.poll_fds + i)->revents)
00861             num_found--;
00862           else
00863             continue;
00864 
00865           for (file_ptr = gdb_notifier.first_file_handler;
00866                file_ptr != NULL;
00867                file_ptr = file_ptr->next_file)
00868             {
00869               if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd)
00870                 break;
00871             }
00872 
00873           if (file_ptr)
00874             {
00875               /* Enqueue an event only if this is still a new event for
00876                  this fd.  */
00877               if (file_ptr->ready_mask == 0)
00878                 {
00879                   file_event_ptr = create_file_event (file_ptr->fd);
00880                   QUEUE_enque (gdb_event_p, event_queue, file_event_ptr);
00881                 }
00882               file_ptr->ready_mask = (gdb_notifier.poll_fds + i)->revents;
00883             }
00884         }
00885 #else
00886       internal_error (__FILE__, __LINE__,
00887                       _("use_poll without HAVE_POLL"));
00888 #endif /* HAVE_POLL */
00889     }
00890   else
00891     {
00892       for (file_ptr = gdb_notifier.first_file_handler;
00893            (file_ptr != NULL) && (num_found > 0);
00894            file_ptr = file_ptr->next_file)
00895         {
00896           int mask = 0;
00897 
00898           if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0]))
00899             mask |= GDB_READABLE;
00900           if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1]))
00901             mask |= GDB_WRITABLE;
00902           if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2]))
00903             mask |= GDB_EXCEPTION;
00904 
00905           if (!mask)
00906             continue;
00907           else
00908             num_found--;
00909 
00910           /* Enqueue an event only if this is still a new event for
00911              this fd.  */
00912 
00913           if (file_ptr->ready_mask == 0)
00914             {
00915               file_event_ptr = create_file_event (file_ptr->fd);
00916               QUEUE_enque (gdb_event_p, event_queue, file_event_ptr);
00917             }
00918           file_ptr->ready_mask = mask;
00919         }
00920     }
00921   return 0;
00922 }
00923 
00924 
00925 /* Create an asynchronous handler, allocating memory for it.
00926    Return a pointer to the newly created handler.
00927    This pointer will be used to invoke the handler by 
00928    invoke_async_signal_handler.
00929    PROC is the function to call with CLIENT_DATA argument 
00930    whenever the handler is invoked.  */
00931 async_signal_handler *
00932 create_async_signal_handler (sig_handler_func * proc,
00933                              gdb_client_data client_data)
00934 {
00935   async_signal_handler *async_handler_ptr;
00936 
00937   async_handler_ptr =
00938     (async_signal_handler *) xmalloc (sizeof (async_signal_handler));
00939   async_handler_ptr->ready = 0;
00940   async_handler_ptr->next_handler = NULL;
00941   async_handler_ptr->proc = proc;
00942   async_handler_ptr->client_data = client_data;
00943   if (sighandler_list.first_handler == NULL)
00944     sighandler_list.first_handler = async_handler_ptr;
00945   else
00946     sighandler_list.last_handler->next_handler = async_handler_ptr;
00947   sighandler_list.last_handler = async_handler_ptr;
00948   return async_handler_ptr;
00949 }
00950 
00951 /* Call the handler from HANDLER immediately.  This function runs
00952    signal handlers when returning to the event loop would be too
00953    slow.  */
00954 void
00955 call_async_signal_handler (struct async_signal_handler *handler)
00956 {
00957   (*handler->proc) (handler->client_data);
00958 }
00959 
00960 /* Mark the handler (ASYNC_HANDLER_PTR) as ready.  This information
00961    will be used when the handlers are invoked, after we have waited
00962    for some event.  The caller of this function is the interrupt
00963    handler associated with a signal.  */
00964 void
00965 mark_async_signal_handler (async_signal_handler * async_handler_ptr)
00966 {
00967   async_handler_ptr->ready = 1;
00968 }
00969 
00970 /* Call all the handlers that are ready.  Returns true if any was
00971    indeed ready.  */
00972 static int
00973 invoke_async_signal_handlers (void)
00974 {
00975   async_signal_handler *async_handler_ptr;
00976   int any_ready = 0;
00977 
00978   /* Invoke ready handlers.  */
00979 
00980   while (1)
00981     {
00982       for (async_handler_ptr = sighandler_list.first_handler;
00983            async_handler_ptr != NULL;
00984            async_handler_ptr = async_handler_ptr->next_handler)
00985         {
00986           if (async_handler_ptr->ready)
00987             break;
00988         }
00989       if (async_handler_ptr == NULL)
00990         break;
00991       any_ready = 1;
00992       async_handler_ptr->ready = 0;
00993       (*async_handler_ptr->proc) (async_handler_ptr->client_data);
00994     }
00995 
00996   return any_ready;
00997 }
00998 
00999 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
01000    Free the space allocated for it.  */
01001 void
01002 delete_async_signal_handler (async_signal_handler ** async_handler_ptr)
01003 {
01004   async_signal_handler *prev_ptr;
01005 
01006   if (sighandler_list.first_handler == (*async_handler_ptr))
01007     {
01008       sighandler_list.first_handler = (*async_handler_ptr)->next_handler;
01009       if (sighandler_list.first_handler == NULL)
01010         sighandler_list.last_handler = NULL;
01011     }
01012   else
01013     {
01014       prev_ptr = sighandler_list.first_handler;
01015       while (prev_ptr && prev_ptr->next_handler != (*async_handler_ptr))
01016         prev_ptr = prev_ptr->next_handler;
01017       gdb_assert (prev_ptr);
01018       prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
01019       if (sighandler_list.last_handler == (*async_handler_ptr))
01020         sighandler_list.last_handler = prev_ptr;
01021     }
01022   xfree ((*async_handler_ptr));
01023   (*async_handler_ptr) = NULL;
01024 }
01025 
01026 /* Create an asynchronous event handler, allocating memory for it.
01027    Return a pointer to the newly created handler.  PROC is the
01028    function to call with CLIENT_DATA argument whenever the handler is
01029    invoked.  */
01030 async_event_handler *
01031 create_async_event_handler (async_event_handler_func *proc,
01032                             gdb_client_data client_data)
01033 {
01034   async_event_handler *h;
01035 
01036   h = xmalloc (sizeof (*h));
01037   h->ready = 0;
01038   h->next_handler = NULL;
01039   h->proc = proc;
01040   h->client_data = client_data;
01041   if (async_event_handler_list.first_handler == NULL)
01042     async_event_handler_list.first_handler = h;
01043   else
01044     async_event_handler_list.last_handler->next_handler = h;
01045   async_event_handler_list.last_handler = h;
01046   return h;
01047 }
01048 
01049 /* Mark the handler (ASYNC_HANDLER_PTR) as ready.  This information
01050    will be used by gdb_do_one_event.  The caller will be whoever
01051    created the event source, and wants to signal that the event is
01052    ready to be handled.  */
01053 void
01054 mark_async_event_handler (async_event_handler *async_handler_ptr)
01055 {
01056   async_handler_ptr->ready = 1;
01057 }
01058 
01059 struct async_event_handler_data
01060 {
01061   async_event_handler_func* proc;
01062   gdb_client_data client_data;
01063 };
01064 
01065 static void
01066 invoke_async_event_handler (event_data data)
01067 {
01068   struct async_event_handler_data *hdata = data.ptr;
01069   async_event_handler_func* proc = hdata->proc;
01070   gdb_client_data client_data = hdata->client_data;
01071 
01072   xfree (hdata);
01073   (*proc) (client_data);
01074 }
01075 
01076 /* Check if any asynchronous event handlers are ready, and queue
01077    events in the ready queue for any that are.  */
01078 static void
01079 check_async_event_handlers (void)
01080 {
01081   async_event_handler *async_handler_ptr;
01082   struct async_event_handler_data *hdata;
01083   struct gdb_event *event_ptr;
01084   event_data data;
01085 
01086   for (async_handler_ptr = async_event_handler_list.first_handler;
01087        async_handler_ptr != NULL;
01088        async_handler_ptr = async_handler_ptr->next_handler)
01089     {
01090       if (async_handler_ptr->ready)
01091         {
01092           async_handler_ptr->ready = 0;
01093 
01094           hdata = xmalloc (sizeof (*hdata));
01095 
01096           hdata->proc = async_handler_ptr->proc;
01097           hdata->client_data = async_handler_ptr->client_data;
01098 
01099           data.ptr = hdata;
01100 
01101           event_ptr = create_event (invoke_async_event_handler, data);
01102           QUEUE_enque (gdb_event_p, event_queue, event_ptr);
01103         }
01104     }
01105 }
01106 
01107 /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
01108    Free the space allocated for it.  */
01109 void
01110 delete_async_event_handler (async_event_handler **async_handler_ptr)
01111 {
01112   async_event_handler *prev_ptr;
01113 
01114   if (async_event_handler_list.first_handler == *async_handler_ptr)
01115     {
01116       async_event_handler_list.first_handler
01117         = (*async_handler_ptr)->next_handler;
01118       if (async_event_handler_list.first_handler == NULL)
01119         async_event_handler_list.last_handler = NULL;
01120     }
01121   else
01122     {
01123       prev_ptr = async_event_handler_list.first_handler;
01124       while (prev_ptr && prev_ptr->next_handler != *async_handler_ptr)
01125         prev_ptr = prev_ptr->next_handler;
01126       gdb_assert (prev_ptr);
01127       prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
01128       if (async_event_handler_list.last_handler == (*async_handler_ptr))
01129         async_event_handler_list.last_handler = prev_ptr;
01130     }
01131   xfree (*async_handler_ptr);
01132   *async_handler_ptr = NULL;
01133 }
01134 
01135 /* Create a timer that will expire in MILLISECONDS from now.  When the
01136    timer is ready, PROC will be executed.  At creation, the timer is
01137    aded to the timers queue.  This queue is kept sorted in order of
01138    increasing timers.  Return a handle to the timer struct.  */
01139 int
01140 create_timer (int milliseconds, timer_handler_func * proc, 
01141               gdb_client_data client_data)
01142 {
01143   struct gdb_timer *timer_ptr, *timer_index, *prev_timer;
01144   struct timeval time_now, delta;
01145 
01146   /* Compute seconds.  */
01147   delta.tv_sec = milliseconds / 1000;
01148   /* Compute microseconds.  */
01149   delta.tv_usec = (milliseconds % 1000) * 1000;
01150 
01151   gettimeofday (&time_now, NULL);
01152 
01153   timer_ptr = (struct gdb_timer *) xmalloc (sizeof (*timer_ptr));
01154   timer_ptr->when.tv_sec = time_now.tv_sec + delta.tv_sec;
01155   timer_ptr->when.tv_usec = time_now.tv_usec + delta.tv_usec;
01156   /* Carry?  */
01157   if (timer_ptr->when.tv_usec >= 1000000)
01158     {
01159       timer_ptr->when.tv_sec += 1;
01160       timer_ptr->when.tv_usec -= 1000000;
01161     }
01162   timer_ptr->proc = proc;
01163   timer_ptr->client_data = client_data;
01164   timer_list.num_timers++;
01165   timer_ptr->timer_id = timer_list.num_timers;
01166 
01167   /* Now add the timer to the timer queue, making sure it is sorted in
01168      increasing order of expiration.  */
01169 
01170   for (timer_index = timer_list.first_timer;
01171        timer_index != NULL;
01172        timer_index = timer_index->next)
01173     {
01174       /* If the seconds field is greater or if it is the same, but the
01175          microsecond field is greater.  */
01176       if ((timer_index->when.tv_sec > timer_ptr->when.tv_sec)
01177           || ((timer_index->when.tv_sec == timer_ptr->when.tv_sec)
01178               && (timer_index->when.tv_usec > timer_ptr->when.tv_usec)))
01179         break;
01180     }
01181 
01182   if (timer_index == timer_list.first_timer)
01183     {
01184       timer_ptr->next = timer_list.first_timer;
01185       timer_list.first_timer = timer_ptr;
01186 
01187     }
01188   else
01189     {
01190       for (prev_timer = timer_list.first_timer;
01191            prev_timer->next != timer_index;
01192            prev_timer = prev_timer->next)
01193         ;
01194 
01195       prev_timer->next = timer_ptr;
01196       timer_ptr->next = timer_index;
01197     }
01198 
01199   gdb_notifier.timeout_valid = 0;
01200   return timer_ptr->timer_id;
01201 }
01202 
01203 /* There is a chance that the creator of the timer wants to get rid of
01204    it before it expires.  */
01205 void
01206 delete_timer (int id)
01207 {
01208   struct gdb_timer *timer_ptr, *prev_timer = NULL;
01209 
01210   /* Find the entry for the given timer.  */
01211 
01212   for (timer_ptr = timer_list.first_timer; timer_ptr != NULL;
01213        timer_ptr = timer_ptr->next)
01214     {
01215       if (timer_ptr->timer_id == id)
01216         break;
01217     }
01218 
01219   if (timer_ptr == NULL)
01220     return;
01221   /* Get rid of the timer in the timer list.  */
01222   if (timer_ptr == timer_list.first_timer)
01223     timer_list.first_timer = timer_ptr->next;
01224   else
01225     {
01226       for (prev_timer = timer_list.first_timer;
01227            prev_timer->next != timer_ptr;
01228            prev_timer = prev_timer->next)
01229         ;
01230       prev_timer->next = timer_ptr->next;
01231     }
01232   xfree (timer_ptr);
01233 
01234   gdb_notifier.timeout_valid = 0;
01235 }
01236 
01237 /* When a timer event is put on the event queue, it will be handled by
01238    this function.  Just call the associated procedure and delete the
01239    timer event from the event queue.  Repeat this for each timer that
01240    has expired.  */
01241 static void
01242 handle_timer_event (event_data dummy)
01243 {
01244   struct timeval time_now;
01245   struct gdb_timer *timer_ptr, *saved_timer;
01246 
01247   gettimeofday (&time_now, NULL);
01248   timer_ptr = timer_list.first_timer;
01249 
01250   while (timer_ptr != NULL)
01251     {
01252       if ((timer_ptr->when.tv_sec > time_now.tv_sec)
01253           || ((timer_ptr->when.tv_sec == time_now.tv_sec)
01254               && (timer_ptr->when.tv_usec > time_now.tv_usec)))
01255         break;
01256 
01257       /* Get rid of the timer from the beginning of the list.  */
01258       timer_list.first_timer = timer_ptr->next;
01259       saved_timer = timer_ptr;
01260       timer_ptr = timer_ptr->next;
01261       /* Call the procedure associated with that timer.  */
01262       (*saved_timer->proc) (saved_timer->client_data);
01263       xfree (saved_timer);
01264     }
01265 
01266   gdb_notifier.timeout_valid = 0;
01267 }
01268 
01269 /* Check whether any timers in the timers queue are ready.  If at least
01270    one timer is ready, stick an event onto the event queue.  Even in
01271    case more than one timer is ready, one event is enough, because the
01272    handle_timer_event() will go through the timers list and call the
01273    procedures associated with all that have expired.l Update the
01274    timeout for the select() or poll() as well.  */
01275 static void
01276 poll_timers (void)
01277 {
01278   struct timeval time_now, delta;
01279   gdb_event *event_ptr;
01280 
01281   if (timer_list.first_timer != NULL)
01282     {
01283       gettimeofday (&time_now, NULL);
01284       delta.tv_sec = timer_list.first_timer->when.tv_sec - time_now.tv_sec;
01285       delta.tv_usec = timer_list.first_timer->when.tv_usec - time_now.tv_usec;
01286       /* Borrow?  */
01287       if (delta.tv_usec < 0)
01288         {
01289           delta.tv_sec -= 1;
01290           delta.tv_usec += 1000000;
01291         }
01292 
01293       /* Oops it expired already.  Tell select / poll to return
01294          immediately.  (Cannot simply test if delta.tv_sec is negative
01295          because time_t might be unsigned.)  */
01296       if (timer_list.first_timer->when.tv_sec < time_now.tv_sec
01297           || (timer_list.first_timer->when.tv_sec == time_now.tv_sec
01298               && timer_list.first_timer->when.tv_usec < time_now.tv_usec))
01299         {
01300           delta.tv_sec = 0;
01301           delta.tv_usec = 0;
01302         }
01303 
01304       if (delta.tv_sec == 0 && delta.tv_usec == 0)
01305         {
01306           event_ptr = (gdb_event *) xmalloc (sizeof (gdb_event));
01307           event_ptr->proc = handle_timer_event;
01308           event_ptr->data.integer = timer_list.first_timer->timer_id;
01309           QUEUE_enque (gdb_event_p, event_queue, event_ptr);
01310         }
01311 
01312       /* Now we need to update the timeout for select/ poll, because
01313          we don't want to sit there while this timer is expiring.  */
01314       if (use_poll)
01315         {
01316 #ifdef HAVE_POLL
01317           gdb_notifier.poll_timeout = delta.tv_sec * 1000;
01318 #else
01319           internal_error (__FILE__, __LINE__,
01320                           _("use_poll without HAVE_POLL"));
01321 #endif /* HAVE_POLL */
01322         }
01323       else
01324         {
01325           gdb_notifier.select_timeout.tv_sec = delta.tv_sec;
01326           gdb_notifier.select_timeout.tv_usec = delta.tv_usec;
01327         }
01328       gdb_notifier.timeout_valid = 1;
01329     }
01330   else
01331     gdb_notifier.timeout_valid = 0;
01332 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines