Update years in copyright notice for the GDB files.
[deliverable/binutils-gdb.git] / gdb / gdbserver / event-loop.c
index 6471772e16478d3a07ca661ff4933233481bff09..87487e0a46543346a54329edcf4f690ae03ca7ee 100644 (file)
@@ -1,6 +1,5 @@
 /* Event loop machinery for the remote server for GDB.
-   Copyright (C) 1999, 2000, 2001, 2002, 2005, 2006, 2007, 2008, 2010
-   Free Software Foundation, Inc.
+   Copyright (C) 1999-2013 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -39,7 +38,7 @@
 #endif
 
 typedef struct gdb_event gdb_event;
-typedef int (event_handler_func) (int);
+typedef int (event_handler_func) (gdb_fildes_t);
 
 /* Tell create_file_handler what events we are interested in.  */
 
@@ -64,7 +63,7 @@ struct gdb_event
     event_handler_func *proc;
 
     /* File descriptor that is ready.  */
-    int fd;
+    gdb_fildes_t fd;
 
     /* Next in list of events or NULL.  */
     struct gdb_event *next_event;
@@ -76,7 +75,7 @@ struct gdb_event
 typedef struct file_handler
   {
     /* File descriptor.  */
-    int fd;
+    gdb_fildes_t fd;
 
     /* Events we want to monitor.  */
     int mask;
@@ -141,6 +140,36 @@ static struct
   }
 gdb_notifier;
 
+/* Callbacks are just routines that are executed before waiting for the
+   next event.  In GDB this is struct gdb_timer.  We don't need timers
+   so rather than copy all that complexity in gdbserver, we provide what
+   we need, but we do so in a way that if/when the day comes that we need
+   that complexity, it'll be easier to add - replace callbacks with timers
+   and use a delta of zero (which is all gdb currently uses timers for anyway).
+
+   PROC will be executed before gdbserver goes to sleep to wait for the
+   next event.  */
+
+struct callback_event
+  {
+    int id;
+    callback_handler_func *proc;
+    gdb_client_data *data;
+    struct callback_event *next;
+  };
+
+/* Table of registered callbacks.  */
+
+static struct
+  {
+    struct callback_event *first;
+    struct callback_event *last;
+
+    /* Id of the last callback created.  */
+    int num_callbacks;
+  }
+callback_list;
+
 /* Insert an event object into the gdb event queue.
 
    EVENT_PTR points to the event to be inserted into the queue.  The
@@ -172,7 +201,7 @@ process_event (void)
 {
   gdb_event *event_ptr, *prev_ptr;
   event_handler_func *proc;
-  int fd;
+  gdb_fildes_t fd;
 
   /* Look in the event queue to find an event that is ready
      to be processed.  */
@@ -220,6 +249,81 @@ process_event (void)
   return 0;
 }
 
+/* Append PROC to the callback list.
+   The result is the "id" of the callback that can be passed back to
+   delete_callback_event.  */
+
+int
+append_callback_event (callback_handler_func *proc, gdb_client_data data)
+{
+  struct callback_event *event_ptr;
+
+  event_ptr = xmalloc (sizeof (*event_ptr));
+  event_ptr->id = callback_list.num_callbacks++;
+  event_ptr->proc = proc;
+  event_ptr->data = data;
+  event_ptr->next = NULL;
+  if (callback_list.first == NULL)
+    callback_list.first = event_ptr;
+  if (callback_list.last != NULL)
+    callback_list.last->next = event_ptr;
+  callback_list.last = event_ptr;
+  return event_ptr->id;
+}
+
+/* Delete callback ID.
+   It is not an error callback ID doesn't exist.  */
+
+void
+delete_callback_event (int id)
+{
+  struct callback_event **p;
+
+  for (p = &callback_list.first; *p != NULL; p = &(*p)->next)
+    {
+      struct callback_event *event_ptr = *p;
+
+      if (event_ptr->id == id)
+       {
+         *p = event_ptr->next;
+         if (event_ptr == callback_list.last)
+           callback_list.last = NULL;
+         free (event_ptr);
+         break;
+       }
+    }
+}
+
+/* Run the next callback.
+   The result is 1 if a callback was called and event processing
+   should continue, -1 if the callback wants the event loop to exit,
+   and 0 if there are no more callbacks.  */
+
+static int
+process_callback (void)
+{
+  struct callback_event *event_ptr;
+
+  event_ptr = callback_list.first;
+  if (event_ptr != NULL)
+    {
+      callback_handler_func *proc = event_ptr->proc;
+      gdb_client_data *data = event_ptr->data;
+
+      /* Remove the event before calling PROC,
+        more events may get added by PROC.  */
+      callback_list.first = event_ptr->next;
+      if (callback_list.first == NULL)
+       callback_list.last = NULL;
+      free  (event_ptr);
+      if ((*proc) (data))
+       return -1;
+      return 1;
+    }
+
+  return 0;
+}
+
 /* Add a file handler/descriptor to the list of descriptors we are
    interested in.  FD is the file descriptor for the file/stream to be
    listened to.  MASK is a combination of READABLE, WRITABLE,
@@ -227,7 +331,7 @@ process_event (void)
    occurs for FD.  CLIENT_DATA is the argument to pass to PROC.  */
 
 static void
-create_file_handler (int fd, int mask, handler_func *proc,
+create_file_handler (gdb_fildes_t fd, int mask, handler_func *proc,
                     gdb_client_data client_data)
 {
   file_handler *file_ptr;
@@ -277,7 +381,8 @@ create_file_handler (int fd, int mask, handler_func *proc,
 /* Wrapper function for create_file_handler.  */
 
 void
-add_file_handler (int fd, handler_func *proc, gdb_client_data client_data)
+add_file_handler (gdb_fildes_t fd,
+                 handler_func *proc, gdb_client_data client_data)
 {
   create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION, proc, client_data);
 }
@@ -286,7 +391,7 @@ add_file_handler (int fd, handler_func *proc, gdb_client_data client_data)
    i.e. we don't care anymore about events on the FD.  */
 
 void
-delete_file_handler (int fd)
+delete_file_handler (gdb_fildes_t fd)
 {
   file_handler *file_ptr, *prev_ptr = NULL;
   int i;
@@ -349,7 +454,7 @@ delete_file_handler (int fd)
    event in the front of the event queue.  */
 
 static int
-handle_file_event (int event_file_desc)
+handle_file_event (gdb_fildes_t event_file_desc)
 {
   file_handler *file_ptr;
   int mask;
@@ -366,8 +471,8 @@ handle_file_event (int event_file_desc)
 
          if (file_ptr->ready_mask & GDB_EXCEPTION)
            {
-             fprintf (stderr, "Exception condition detected on fd %d\n",
-                      file_ptr->fd);
+             fprintf (stderr, "Exception condition detected on fd %s\n",
+                      pfildes (file_ptr->fd));
              file_ptr->error = 1;
            }
          else
@@ -397,7 +502,7 @@ handle_file_event (int event_file_desc)
    associated to FD when it was registered with the event loop.  */
 
 static gdb_event *
-create_file_event (int fd)
+create_file_event (gdb_fildes_t fd)
 {
   gdb_event *file_event_ptr;
 
@@ -507,6 +612,16 @@ start_event_loop (void)
       if (res)
        continue;
 
+      /* Process any queued callbacks before we go to sleep.  */
+      res = process_callback ();
+
+      /* Did the callback want the event loop to stop?  */
+      if (res == -1)
+       return;
+
+      if (res)
+       continue;
+
       /* Wait for a new event.  If 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
This page took 0.027145 seconds and 4 git commands to generate.