It turns out that 1.2 wasn't botched after all. (I was failing to take
[deliverable/binutils-gdb.git] / gdb / event-loop.c
index 8668fccaf17a37c137508c119d661aa3f6dbbde4..223baf79eb24f5eed33b64a17eb9d72182f719bc 100644 (file)
 #include "top.h"
 #include "event-loop.h"
 #include "event-top.h"
-#include "inferior.h"          /* For fetch_inferior_event. */
 #ifdef HAVE_POLL
 #include <poll.h>
 #else
 #include <sys/types.h>
+#include <string.h>
 #endif
 #include <errno.h>
 #include <setjmp.h>
 
 /* Type of the mask arguments to select. */
 
-#ifndef NO_FD_SET
-#define SELECT_MASK fd_set
-#else
+#ifndef HAVE_POLL
+#ifdef NO_FD_SET
+/* All this stuff below is not required if select is used as God(tm)
+   intended, with the FD_* macros.  Are there any implementations of
+   select which don't have FD_SET and other standard FD_* macros?  I
+   don't think there are, but if I'm wrong, we need to catch them.  */
+#error FD_SET must be defined if select function is to be used!
+
 #ifndef _AIX
 typedef long fd_mask;
 #endif
@@ -45,8 +50,7 @@ typedef long fd_mask;
 #define SELECT_MASK void
 #else
 #define SELECT_MASK int
-#endif
-#endif
+#endif /* !_IBMR2 */
 
 /* Define "NBBY" (number of bits per byte) if it's not already defined. */
 
@@ -54,7 +58,6 @@ typedef long fd_mask;
 #define NBBY 8
 #endif
 
-
 /* Define the number of fd_masks in an fd_set */
 
 #ifndef FD_SETSIZE
@@ -72,6 +75,9 @@ typedef long fd_mask;
 #endif
 #define MASK_SIZE howmany(FD_SETSIZE, NFDBITS)
 
+#endif /* NO_FD_SET */
+#endif /* !HAVE_POLL */
+
 
 typedef struct gdb_event gdb_event;
 typedef void (event_handler_func) (int);
@@ -193,10 +199,10 @@ static struct
 
     /* Masks to be used in the next call to select.
        Bits are set in response to calls to create_file_handler. */
-    fd_mask check_masks[3 * MASK_SIZE];
+    fd_set check_masks[3];
 
     /* What file descriptors were found ready by select. */
-    fd_mask ready_masks[3 * MASK_SIZE];
+    fd_set ready_masks[3];
 
     /* Number of valid bits (highest fd value + 1). */
     int num_fds;
@@ -230,7 +236,7 @@ static struct
     /* Pointer to first in timer list. */
     struct gdb_timer *first_timer;
 
-    /* Length of timer list. */
+    /* Id of the last timer created. */
     int num_timers;
   }
 timer_list;
@@ -257,14 +263,13 @@ static void create_file_handler (int fd, int mask, handler_func * proc, gdb_clie
 static void invoke_async_signal_handler (void);
 static void handle_file_event (int event_file_desc);
 static int gdb_wait_for_event (void);
-static int gdb_do_one_event (void);
+static int gdb_do_one_event (void *data);
 static int check_async_ready (void);
 static void async_queue_event (gdb_event * event_ptr, queue_position position);
 static gdb_event *create_file_event (int fd);
 static int process_event (void);
 static void handle_timer_event (int dummy);
 static void poll_timers (void);
-static int fetch_inferior_event_wrapper (gdb_client_data client_data);
 \f
 
 /* Insert an event object into the gdb event queue at 
@@ -390,52 +395,61 @@ process_event (void)
 
 /* Process one high level event.  If nothing is ready at this time,
    wait for something to happen (via gdb_wait_for_event), then process
-   it.  Returns 1 if something was done otherwise returns 0 (this can
-   happen if there are no event sources to wait for). */
+   it.  Returns >0 if something was done otherwise returns <0 (this
+   can happen if there are no event sources to wait for).  If an error
+   occures catch_errors() which calls this function returns zero. */
+
 static int
-gdb_do_one_event (void)
+gdb_do_one_event (void *data)
 {
-  int result = 0;
-
-  while (1)
+  /* Any events already waiting in the queue? */
+  if (process_event ())
     {
-      if (!SET_TOP_LEVEL ())
-       {
-         /* Any events already waiting in the queue? */
-         if (process_event ())
-           {
-             result = 1;
-             break;
-           }
-
-         /* Are any timers that are ready? If so, put an event on the queue. */
-         poll_timers ();
-
-         /* Wait for a new event.  If gdb_wait_for_event returns -1,
-            we should get out because this means that there are no
-            event sources left. This will make the event loop stop,
-            and the application exit. */
-
-         result = gdb_wait_for_event ();
-         if (result < 0)
-           {
-             result = 0;
-             break;
-           }
+      return 1;
+    }
+  
+  /* Are any timers that are ready? If so, put an event on the queue. */
+  poll_timers ();
+  
+  /* Wait for a new event.  If gdb_wait_for_event returns -1,
+     we should get out because this means that there are no
+     event sources left. This will make the event loop stop,
+     and the application exit. */
+  
+  if (gdb_wait_for_event () < 0)
+    {
+      return -1;
+    }
+  
+  /* Handle any new events occurred while waiting. */
+  if (process_event ())
+    {
+      return 1;
+    }
+  
+  /* If gdb_wait_for_event has returned 1, it means that one
+     event has been handled. We break out of the loop. */
+  return 1;
+}
 
-         /* Handle any new events occurred while waiting. */
-         if (process_event ())
-           {
-             result = 1;
-             break;
-           }
+/* Start up the event loop. This is the entry point to the event loop
+   from the command loop. */
 
-         /* If gdb_wait_for_event has returned 1, it means that one
-            event has been handled. We break out of the loop. */
-         if (result)
-           break;
-       }                       /* end of if !set_top_level */
-      else
+void
+start_event_loop (void)
+{
+  /* Loop until there is nothing to do. This is the entry point to the
+     event loop engine. gdb_do_one_event, called via catch_errors()
+     will process one event for each invocation.  It blocks waits for
+     an event and then processes it.  >0 when an event is processed, 0
+     when catch_errors() caught an error and <0 when there are no
+     longer any event sources registered. */
+  while (1)
+    {
+      int result = catch_errors (gdb_do_one_event, 0, "", RETURN_MASK_ALL);
+      if (result < 0)
+       break;
+      if (result == 0)
        {
          /* FIXME: this should really be a call to a hook that is
             interface specific, because interfaces can display the
@@ -445,21 +459,6 @@ gdb_do_one_event (void)
             whether display the prompt or not. */
        }
     }
-  return result;
-}
-\f
-
-/* Start up the event loop. This is the entry point to the event loop
-   from the command loop. */
-void
-start_event_loop (void)
-{
-  /* Loop until there is something to do. This is the entry point to
-     the event loop engine. gdb_do_one_event will process one event
-     for each invocation.  It always returns 1, unless there are no
-     more event sources registered. In this case it returns 0.  */
-  while (gdb_do_one_event () != 0)
-    ;
 
   /* We are done with the event loop. There are no more event sources
      to listen to.  So we exit GDB. */
@@ -495,10 +494,6 @@ create_file_handler (int fd, int mask, handler_func * proc, gdb_client_data clie
 {
   file_handler *file_ptr;
 
-#ifndef HAVE_POLL
-  int index, bit;
-#endif
-
   /* Do we already have a file handler for this file? (We may be
      changing its associated procedure). */
   for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
@@ -540,23 +535,20 @@ create_file_handler (int fd, int mask, handler_func * proc, gdb_client_data clie
 
 #else /* ! HAVE_POLL */
 
-  index = fd / (NBBY * sizeof (fd_mask));
-  bit = 1 << (fd % (NBBY * sizeof (fd_mask)));
-
   if (mask & GDB_READABLE)
-    gdb_notifier.check_masks[index] |= bit;
+    FD_SET (fd, &gdb_notifier.check_masks[0]);
   else
-    gdb_notifier.check_masks[index] &= ~bit;
+    FD_CLR (fd, &gdb_notifier.check_masks[0]);
 
   if (mask & GDB_WRITABLE)
-    (gdb_notifier.check_masks + MASK_SIZE)[index] |= bit;
+    FD_SET (fd, &gdb_notifier.check_masks[1]);
   else
-    (gdb_notifier.check_masks + MASK_SIZE)[index] &= ~bit;
+    FD_CLR (fd, &gdb_notifier.check_masks[1]);
 
   if (mask & GDB_EXCEPTION)
-    (gdb_notifier.check_masks + 2 * (MASK_SIZE))[index] |= bit;
+    FD_SET (fd, &gdb_notifier.check_masks[2]);
   else
-    (gdb_notifier.check_masks + 2 * (MASK_SIZE))[index] &= ~bit;
+    FD_CLR (fd, &gdb_notifier.check_masks[2]);
 
   if (gdb_notifier.num_fds <= fd)
     gdb_notifier.num_fds = fd + 1;
@@ -570,11 +562,10 @@ void
 delete_file_handler (int fd)
 {
   file_handler *file_ptr, *prev_ptr = NULL;
-  int i, j;
+  int i;
+#ifdef HAVE_POLL
+  int j;
   struct pollfd *new_poll_fds;
-#ifndef HAVE_POLL
-  int index, bit;
-  unsigned long flags;
 #endif
 
   /* Find the entry for the given file. */
@@ -612,36 +603,26 @@ delete_file_handler (int fd)
 
 #else /* ! HAVE_POLL */
 
-  index = fd / (NBBY * sizeof (fd_mask));
-  bit = 1 << (fd % (NBBY * sizeof (fd_mask)));
-
   if (file_ptr->mask & GDB_READABLE)
-    gdb_notifier.check_masks[index] &= ~bit;
+    FD_CLR (fd, &gdb_notifier.check_masks[0]);
   if (file_ptr->mask & GDB_WRITABLE)
-    (gdb_notifier.check_masks + MASK_SIZE)[index] &= ~bit;
+    FD_CLR (fd, &gdb_notifier.check_masks[1]);
   if (file_ptr->mask & GDB_EXCEPTION)
-    (gdb_notifier.check_masks + 2 * (MASK_SIZE))[index] &= ~bit;
+    FD_CLR (fd, &gdb_notifier.check_masks[2]);
 
   /* Find current max fd. */
 
   if ((fd + 1) == gdb_notifier.num_fds)
     {
-      for (gdb_notifier.num_fds = 0; index >= 0; index--)
+      gdb_notifier.num_fds--;
+      for (i = gdb_notifier.num_fds; i; i--)
        {
-         flags = gdb_notifier.check_masks[index]
-           | (gdb_notifier.check_masks + MASK_SIZE)[index]
-           | (gdb_notifier.check_masks + 2 * (MASK_SIZE))[index];
-         if (flags)
-           {
-             for (i = (NBBY * sizeof (fd_mask)); i > 0; i--)
-               {
-                 if (flags & (((unsigned long) 1) << (i - 1)))
-                   break;
-               }
-             gdb_notifier.num_fds = index * (NBBY * sizeof (fd_mask)) + i;
-             break;
-           }
+         if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0])
+             || FD_ISSET (i - 1, &gdb_notifier.check_masks[1])
+             || FD_ISSET (i - 1, &gdb_notifier.check_masks[2]))
+           break;
        }
+      gdb_notifier.num_fds = i;
     }
 #endif /* HAVE_POLL */
 
@@ -705,12 +686,12 @@ handle_file_event (int event_file_desc)
            {
              /* Work in progress. We may need to tell somebody what
                 kind of error we had. */
-             /*if (error_mask_returned & POLLHUP)
-                printf_unfiltered ("Hangup detected on fd %d\n", file_ptr->fd);
-                if (error_mask_returned & POLLERR)
-                printf_unfiltered ("Error detected on fd %d\n", file_ptr->fd);
-                if (error_mask_returned & POLLNVAL)
-                printf_unfiltered ("Invalid fd %d\n", file_ptr->fd); */
+             if (error_mask_returned & POLLHUP)
+               printf_unfiltered ("Hangup detected on fd %d\n", file_ptr->fd);
+             if (error_mask_returned & POLLERR)
+               printf_unfiltered ("Error detected on fd %d\n", file_ptr->fd);
+             if (error_mask_returned & POLLNVAL)
+               printf_unfiltered ("Invalid or non-`poll'able fd %d\n", file_ptr->fd);
              file_ptr->error = 1;
            }
          else
@@ -731,7 +712,7 @@ handle_file_event (int event_file_desc)
 
          /* If there was a match, then call the handler. */
          if (mask != 0)
-           (*file_ptr->proc) (file_ptr->error, file_ptr->fd, file_ptr->client_data);
+           (*file_ptr->proc) (file_ptr->error, file_ptr->client_data);
          break;
        }
     }
@@ -750,10 +731,8 @@ gdb_wait_for_event (void)
   file_handler *file_ptr;
   gdb_event *file_event_ptr;
   int num_found = 0;
+#ifdef HAVE_POLL
   int i;
-
-#ifndef HAVE_POLL
-  int mask, bit, index;
 #endif
 
   /* Make sure all output is done before getting another event. */
@@ -775,20 +754,24 @@ gdb_wait_for_event (void)
     perror_with_name ("Poll");
 
 #else /* ! HAVE_POLL */
-  memcpy (gdb_notifier.ready_masks,
-         gdb_notifier.check_masks,
-         3 * MASK_SIZE * sizeof (fd_mask));
+
+  gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0];
+  gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1];
+  gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2];
+
   num_found = select (gdb_notifier.num_fds,
-                     (SELECT_MASK *) & gdb_notifier.ready_masks[0],
-                     (SELECT_MASK *) & gdb_notifier.ready_masks[MASK_SIZE],
-                 (SELECT_MASK *) & gdb_notifier.ready_masks[2 * MASK_SIZE],
-                 gdb_notifier.timeout_valid ? &gdb_notifier.timeout : NULL);
+                     & gdb_notifier.ready_masks[0],
+                     & gdb_notifier.ready_masks[1],
+                     & gdb_notifier.ready_masks[2],
+                     gdb_notifier.timeout_valid
+                     ? &gdb_notifier.timeout : NULL);
 
   /* Clear the masks after an error from select. */
   if (num_found == -1)
     {
-      memset (gdb_notifier.ready_masks,
-             0, 3 * MASK_SIZE * sizeof (fd_mask));
+      FD_ZERO (&gdb_notifier.ready_masks[0]);
+      FD_ZERO (&gdb_notifier.ready_masks[1]);
+      FD_ZERO (&gdb_notifier.ready_masks[2]);
       /* Dont print anything is we got a signal, let gdb handle it. */
       if (errno != EINTR)
        perror_with_name ("Select");
@@ -829,19 +812,18 @@ gdb_wait_for_event (void)
     }
 
 #else /* ! HAVE_POLL */
+
   for (file_ptr = gdb_notifier.first_file_handler;
        (file_ptr != NULL) && (num_found > 0);
        file_ptr = file_ptr->next_file)
     {
-      index = file_ptr->fd / (NBBY * sizeof (fd_mask));
-      bit = 1 << (file_ptr->fd % (NBBY * sizeof (fd_mask)));
-      mask = 0;
+      int mask = 0;
 
-      if (gdb_notifier.ready_masks[index] & bit)
+      if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0]))
        mask |= GDB_READABLE;
-      if ((gdb_notifier.ready_masks + MASK_SIZE)[index] & bit)
+      if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1]))
        mask |= GDB_WRITABLE;
-      if ((gdb_notifier.ready_masks + 2 * (MASK_SIZE))[index] & bit)
+      if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2]))
        mask |= GDB_EXCEPTION;
 
       if (!mask)
@@ -859,6 +841,7 @@ gdb_wait_for_event (void)
        }
       file_ptr->ready_mask = mask;
     }
+
 #endif /* HAVE_POLL */
 
   return 0;
@@ -964,42 +947,6 @@ check_async_ready (void)
   return async_handler_ready;
 }
 
-/* FIXME: where does this function belong? */
-/* General function to handle events in the inferior. So far it just
-   takes care of detecting errors reported by select() or poll(),
-   otherwise it assumes that all is OK, and goes on reading data from
-   the fd. This however may not always be what we want to do. */
-void
-inferior_event_handler (int error, gdb_client_data client_data, int fd)
-{
-  if (error == 1)
-    {
-      printf_unfiltered ("error detected on fd %d\n", fd);
-      delete_file_handler (fd);
-      pop_target ();
-      discard_all_continuations ();
-    }
-  else
-    /* Use catch errors for now, until the inner layers of
-       fetch_inferior_event (i.e. readchar) can return meaningful
-       error status.  If an error occurs while getting an event from
-       the target, just get rid of the target. */
-    if (!catch_errors (fetch_inferior_event_wrapper, client_data, "", RETURN_MASK_ALL))
-      {
-       delete_file_handler (fd);
-       discard_all_continuations ();
-       pop_target ();
-       display_gdb_prompt (0);
-      }
-}
-
-static int 
-fetch_inferior_event_wrapper (gdb_client_data client_data)
-{
-  fetch_inferior_event (client_data);
-  return 1;
-}
-
 /* Create a timer that will expire in MILLISECONDS from now. When the
    timer is ready, PROC will be executed. At creation, the timer is
    aded to the timers queue.  This queue is kept sorted in order of
@@ -1126,7 +1073,7 @@ handle_timer_event (int dummy)
       saved_timer = timer_ptr;
       timer_ptr = timer_ptr->next;
       /* Call the procedure associated with that timer. */
-      (*saved_timer->proc) (timer_ptr->client_data);
+      (*saved_timer->proc) (saved_timer->client_data);
       free (saved_timer);
     }
 
@@ -1145,7 +1092,7 @@ poll_timers (void)
   struct timeval time_now, delta;
   gdb_event *event_ptr;
 
-  if (timer_list.num_timers)
+  if (timer_list.first_timer != NULL)
     {
       gettimeofday (&time_now, NULL);
       delta.tv_sec = timer_list.first_timer->when.tv_sec - time_now.tv_sec;
@@ -1158,8 +1105,11 @@ poll_timers (void)
        }
 
       /* Oops it expired already. Tell select / poll to return
-         immediately. */
-      if (delta.tv_sec < 0)
+         immediately. (Cannot simply test if delta.tv_sec is negative
+        because time_t might be unsigned.)  */
+      if (timer_list.first_timer->when.tv_sec < time_now.tv_sec
+         || (timer_list.first_timer->when.tv_sec == time_now.tv_sec
+             && timer_list.first_timer->when.tv_usec < time_now.tv_usec))
        {
          delta.tv_sec = 0;
          delta.tv_usec = 0;
This page took 0.029491 seconds and 4 git commands to generate.