gdb/gdbserver/event-loop.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Event loop machinery for the remote server for GDB.
  2.    Copyright (C) 1999-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>. */

  14. /* Based on src/gdb/event-loop.c.  */

  15. #include "server.h"
  16. #include "queue.h"

  17. #include <sys/types.h>
  18. #include <sys/time.h>

  19. #ifdef USE_WIN32API
  20. #include <windows.h>
  21. #include <io.h>
  22. #endif

  23. #include <unistd.h>

  24. typedef struct gdb_event gdb_event;
  25. typedef int (event_handler_func) (gdb_fildes_t);

  26. /* Tell create_file_handler what events we are interested in.  */

  27. #define GDB_READABLE        (1<<1)
  28. #define GDB_WRITABLE        (1<<2)
  29. #define GDB_EXCEPTION        (1<<3)

  30. /* Events are queued by calling 'QUEUE_enque (gdb_event_p, event_queue,
  31.    file_event_ptr)' and serviced later
  32.    on by do_one_event.  An event can be, for instance, a file
  33.    descriptor becoming ready to be read.  Servicing an event simply
  34.    means that the procedure PROC will be called.  We have 2 queues,
  35.    one for file handlers that we listen to in the event loop, and one
  36.    for the file handlers+events that are ready.  The procedure PROC
  37.    associated with each event is always the same (handle_file_event).
  38.    Its duty is to invoke the handler associated with the file
  39.    descriptor whose state change generated the event, plus doing other
  40.    cleanups and such.  */

  41. typedef struct gdb_event
  42.   {
  43.     /* Procedure to call to service this event.  */
  44.     event_handler_func *proc;

  45.     /* File descriptor that is ready.  */
  46.     gdb_fildes_t fd;
  47.   } *gdb_event_p;

  48. /* Information about each file descriptor we register with the event
  49.    loop.  */

  50. typedef struct file_handler
  51.   {
  52.     /* File descriptor.  */
  53.     gdb_fildes_t fd;

  54.     /* Events we want to monitor.  */
  55.     int mask;

  56.     /* Events that have been seen since the last time.  */
  57.     int ready_mask;

  58.     /* Procedure to call when fd is ready.  */
  59.     handler_func *proc;

  60.     /* Argument to pass to proc.  */
  61.     gdb_client_data client_data;

  62.     /* Was an error detected on this fd?  */
  63.     int error;

  64.     /* Next registered file descriptor.  */
  65.     struct file_handler *next_file;
  66.   }
  67. file_handler;

  68. DECLARE_QUEUE_P(gdb_event_p);
  69. static QUEUE(gdb_event_p) *event_queue = NULL;
  70. DEFINE_QUEUE_P(gdb_event_p);

  71. /* Gdb_notifier is just a list of file descriptors gdb is interested
  72.    in.  These are the input file descriptor, and the target file
  73.    descriptor.  Each of the elements in the gdb_notifier list is
  74.    basically a description of what kind of events gdb is interested
  75.    in, for each fd.  */

  76. static struct
  77.   {
  78.     /* Ptr to head of file handler list.  */
  79.     file_handler *first_file_handler;

  80.     /* Masks to be used in the next call to select.  Bits are set in
  81.        response to calls to create_file_handler.  */
  82.     fd_set check_masks[3];

  83.     /* What file descriptors were found ready by select.  */
  84.     fd_set ready_masks[3];

  85.     /* Number of valid bits (highest fd value + 1). (for select) */
  86.     int num_fds;
  87.   }
  88. gdb_notifier;

  89. /* Callbacks are just routines that are executed before waiting for the
  90.    next event.  In GDB this is struct gdb_timer.  We don't need timers
  91.    so rather than copy all that complexity in gdbserver, we provide what
  92.    we need, but we do so in a way that if/when the day comes that we need
  93.    that complexity, it'll be easier to add - replace callbacks with timers
  94.    and use a delta of zero (which is all gdb currently uses timers for anyway).

  95.    PROC will be executed before gdbserver goes to sleep to wait for the
  96.    next event.  */

  97. struct callback_event
  98.   {
  99.     int id;
  100.     callback_handler_func *proc;
  101.     gdb_client_data *data;
  102.     struct callback_event *next;
  103.   };

  104. /* Table of registered callbacks.  */

  105. static struct
  106.   {
  107.     struct callback_event *first;
  108.     struct callback_event *last;

  109.     /* Id of the last callback created.  */
  110.     int num_callbacks;
  111.   }
  112. callback_list;

  113. /* Free EVENT.  */

  114. static void
  115. gdb_event_xfree (struct gdb_event *event)
  116. {
  117.   xfree (event);
  118. }

  119. void
  120. initialize_event_loop (void)
  121. {
  122.   event_queue = QUEUE_alloc (gdb_event_p, gdb_event_xfree);
  123. }

  124. /* Process one event.  If an event was processed, 1 is returned
  125.    otherwise 0 is returned.  Scan the queue from head to tail,
  126.    processing therefore the high priority events first, by invoking
  127.    the associated event handler procedure.  */

  128. static int
  129. process_event (void)
  130. {
  131.   /* Let's get rid of the event from the event queue.  We need to
  132.      do this now because while processing the event, since the
  133.      proc function could end up jumping out to the caller of this
  134.      function.  In that case, we would have on the event queue an
  135.      event which has been processed, but not deleted.  */
  136.   if (!QUEUE_is_empty (gdb_event_p, event_queue))
  137.     {
  138.       gdb_event *event_ptr = QUEUE_deque (gdb_event_p, event_queue);
  139.       event_handler_func *proc = event_ptr->proc;
  140.       gdb_fildes_t fd = event_ptr->fd;

  141.       gdb_event_xfree (event_ptr);
  142.       /* Now call the procedure associated with the event.  */
  143.       if ((*proc) (fd))
  144.         return -1;
  145.       return 1;
  146.     }

  147.   /* This is the case if there are no event on the event queue.  */
  148.   return 0;
  149. }

  150. /* Append PROC to the callback list.
  151.    The result is the "id" of the callback that can be passed back to
  152.    delete_callback_event.  */

  153. int
  154. append_callback_event (callback_handler_func *proc, gdb_client_data data)
  155. {
  156.   struct callback_event *event_ptr;

  157.   event_ptr = xmalloc (sizeof (*event_ptr));
  158.   event_ptr->id = callback_list.num_callbacks++;
  159.   event_ptr->proc = proc;
  160.   event_ptr->data = data;
  161.   event_ptr->next = NULL;
  162.   if (callback_list.first == NULL)
  163.     callback_list.first = event_ptr;
  164.   if (callback_list.last != NULL)
  165.     callback_list.last->next = event_ptr;
  166.   callback_list.last = event_ptr;
  167.   return event_ptr->id;
  168. }

  169. /* Delete callback ID.
  170.    It is not an error callback ID doesn't exist.  */

  171. void
  172. delete_callback_event (int id)
  173. {
  174.   struct callback_event **p;

  175.   for (p = &callback_list.first; *p != NULL; p = &(*p)->next)
  176.     {
  177.       struct callback_event *event_ptr = *p;

  178.       if (event_ptr->id == id)
  179.         {
  180.           *p = event_ptr->next;
  181.           if (event_ptr == callback_list.last)
  182.             callback_list.last = NULL;
  183.           free (event_ptr);
  184.           break;
  185.         }
  186.     }
  187. }

  188. /* Run the next callback.
  189.    The result is 1 if a callback was called and event processing
  190.    should continue, -1 if the callback wants the event loop to exit,
  191.    and 0 if there are no more callbacks.  */

  192. static int
  193. process_callback (void)
  194. {
  195.   struct callback_event *event_ptr;

  196.   event_ptr = callback_list.first;
  197.   if (event_ptr != NULL)
  198.     {
  199.       callback_handler_func *proc = event_ptr->proc;
  200.       gdb_client_data *data = event_ptr->data;

  201.       /* Remove the event before calling PROC,
  202.          more events may get added by PROC.  */
  203.       callback_list.first = event_ptr->next;
  204.       if (callback_list.first == NULL)
  205.         callback_list.last = NULL;
  206.       free  (event_ptr);
  207.       if ((*proc) (data))
  208.         return -1;
  209.       return 1;
  210.     }

  211.   return 0;
  212. }

  213. /* Add a file handler/descriptor to the list of descriptors we are
  214.    interested in.  FD is the file descriptor for the file/stream to be
  215.    listened toMASK is a combination of READABLE, WRITABLE,
  216.    EXCEPTION.  PROC is the procedure that will be called when an event
  217.    occurs for FD.  CLIENT_DATA is the argument to pass to PROC.  */

  218. static void
  219. create_file_handler (gdb_fildes_t fd, int mask, handler_func *proc,
  220.                      gdb_client_data client_data)
  221. {
  222.   file_handler *file_ptr;

  223.   /* Do we already have a file handler for this file? (We may be
  224.      changing its associated procedure).  */
  225.   for (file_ptr = gdb_notifier.first_file_handler;
  226.        file_ptr != NULL;
  227.        file_ptr = file_ptr->next_file)
  228.     if (file_ptr->fd == fd)
  229.       break;

  230.   /* It is a new file descriptor.  Add it to the list.  Otherwise,
  231.      just change the data associated with it.  */
  232.   if (file_ptr == NULL)
  233.     {
  234.       file_ptr = xmalloc (sizeof (*file_ptr));
  235.       file_ptr->fd = fd;
  236.       file_ptr->ready_mask = 0;
  237.       file_ptr->next_file = gdb_notifier.first_file_handler;
  238.       gdb_notifier.first_file_handler = file_ptr;

  239.       if (mask & GDB_READABLE)
  240.         FD_SET (fd, &gdb_notifier.check_masks[0]);
  241.       else
  242.         FD_CLR (fd, &gdb_notifier.check_masks[0]);

  243.       if (mask & GDB_WRITABLE)
  244.         FD_SET (fd, &gdb_notifier.check_masks[1]);
  245.       else
  246.         FD_CLR (fd, &gdb_notifier.check_masks[1]);

  247.       if (mask & GDB_EXCEPTION)
  248.         FD_SET (fd, &gdb_notifier.check_masks[2]);
  249.       else
  250.         FD_CLR (fd, &gdb_notifier.check_masks[2]);

  251.       if (gdb_notifier.num_fds <= fd)
  252.         gdb_notifier.num_fds = fd + 1;
  253.     }

  254.   file_ptr->proc = proc;
  255.   file_ptr->client_data = client_data;
  256.   file_ptr->mask = mask;
  257. }

  258. /* Wrapper function for create_file_handler.  */

  259. void
  260. add_file_handler (gdb_fildes_t fd,
  261.                   handler_func *proc, gdb_client_data client_data)
  262. {
  263.   create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION, proc, client_data);
  264. }

  265. /* Remove the file descriptor FD from the list of monitored fd's:
  266.    i.e. we don't care anymore about events on the FD.  */

  267. void
  268. delete_file_handler (gdb_fildes_t fd)
  269. {
  270.   file_handler *file_ptr, *prev_ptr = NULL;
  271.   int i;

  272.   /* Find the entry for the given file. */

  273.   for (file_ptr = gdb_notifier.first_file_handler;
  274.        file_ptr != NULL;
  275.        file_ptr = file_ptr->next_file)
  276.     if (file_ptr->fd == fd)
  277.       break;

  278.   if (file_ptr == NULL)
  279.     return;

  280.   if (file_ptr->mask & GDB_READABLE)
  281.     FD_CLR (fd, &gdb_notifier.check_masks[0]);
  282.   if (file_ptr->mask & GDB_WRITABLE)
  283.     FD_CLR (fd, &gdb_notifier.check_masks[1]);
  284.   if (file_ptr->mask & GDB_EXCEPTION)
  285.     FD_CLR (fd, &gdb_notifier.check_masks[2]);

  286.   /* Find current max fd.  */

  287.   if ((fd + 1) == gdb_notifier.num_fds)
  288.     {
  289.       gdb_notifier.num_fds--;
  290.       for (i = gdb_notifier.num_fds; i; i--)
  291.         {
  292.           if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0])
  293.               || FD_ISSET (i - 1, &gdb_notifier.check_masks[1])
  294.               || FD_ISSET (i - 1, &gdb_notifier.check_masks[2]))
  295.             break;
  296.         }
  297.       gdb_notifier.num_fds = i;
  298.     }

  299.   /* Deactivate the file descriptor, by clearing its mask, so that it
  300.      will not fire again.  */

  301.   file_ptr->mask = 0;

  302.   /* Get rid of the file handler in the file handler list.  */
  303.   if (file_ptr == gdb_notifier.first_file_handler)
  304.     gdb_notifier.first_file_handler = file_ptr->next_file;
  305.   else
  306.     {
  307.       for (prev_ptr = gdb_notifier.first_file_handler;
  308.            prev_ptr->next_file != file_ptr;
  309.            prev_ptr = prev_ptr->next_file)
  310.         ;
  311.       prev_ptr->next_file = file_ptr->next_file;
  312.     }
  313.   free (file_ptr);
  314. }

  315. /* Handle the given event by calling the procedure associated to the
  316.    corresponding file handler.  Called by process_event indirectly,
  317.    through event_ptr->proc.  EVENT_FILE_DESC is file descriptor of the
  318.    event in the front of the event queue.  */

  319. static int
  320. handle_file_event (gdb_fildes_t event_file_desc)
  321. {
  322.   file_handler *file_ptr;
  323.   int mask;

  324.   /* Search the file handler list to find one that matches the fd in
  325.      the event.  */
  326.   for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
  327.        file_ptr = file_ptr->next_file)
  328.     {
  329.       if (file_ptr->fd == event_file_desc)
  330.         {
  331.           /* See if the desired events (mask) match the received
  332.              events (ready_mask).  */

  333.           if (file_ptr->ready_mask & GDB_EXCEPTION)
  334.             {
  335.               fprintf (stderr, "Exception condition detected on fd %s\n",
  336.                        pfildes (file_ptr->fd));
  337.               file_ptr->error = 1;
  338.             }
  339.           else
  340.             file_ptr->error = 0;
  341.           mask = file_ptr->ready_mask & file_ptr->mask;

  342.           /* Clear the received events for next time around.  */
  343.           file_ptr->ready_mask = 0;

  344.           /* If there was a match, then call the handler.  */
  345.           if (mask != 0)
  346.             {
  347.               if ((*file_ptr->proc) (file_ptr->error,
  348.                                      file_ptr->client_data) < 0)
  349.                 return -1;
  350.             }
  351.           break;
  352.         }
  353.     }

  354.   return 0;
  355. }

  356. /* Create a file event, to be enqueued in the event queue for
  357.    processing.  The procedure associated to this event is always
  358.    handle_file_event, which will in turn invoke the one that was
  359.    associated to FD when it was registered with the event loop.  */

  360. static gdb_event *
  361. create_file_event (gdb_fildes_t fd)
  362. {
  363.   gdb_event *file_event_ptr;

  364.   file_event_ptr = xmalloc (sizeof (gdb_event));
  365.   file_event_ptr->proc = handle_file_event;
  366.   file_event_ptr->fd = fd;
  367.   return file_event_ptr;
  368. }

  369. /* Called by do_one_event to wait for new events on the monitored file
  370.    descriptors.  Queue file events as they are detected by the poll.
  371.    If there are no events, this function will block in the call to
  372.    select.  Return -1 if there are no files descriptors to monitor,
  373.    otherwise return 0.  */

  374. static int
  375. wait_for_event (void)
  376. {
  377.   file_handler *file_ptr;
  378.   int num_found = 0;

  379.   /* Make sure all output is done before getting another event.  */
  380.   fflush (stdout);
  381.   fflush (stderr);

  382.   if (gdb_notifier.num_fds == 0)
  383.     return -1;

  384.   gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0];
  385.   gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1];
  386.   gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2];
  387.   num_found = select (gdb_notifier.num_fds,
  388.                       &gdb_notifier.ready_masks[0],
  389.                       &gdb_notifier.ready_masks[1],
  390.                       &gdb_notifier.ready_masks[2],
  391.                       NULL);

  392.   /* Clear the masks after an error from select.  */
  393.   if (num_found == -1)
  394.     {
  395.       FD_ZERO (&gdb_notifier.ready_masks[0]);
  396.       FD_ZERO (&gdb_notifier.ready_masks[1]);
  397.       FD_ZERO (&gdb_notifier.ready_masks[2]);
  398. #ifdef EINTR
  399.       /* Dont print anything if we got a signal, let gdb handle
  400.          it.  */
  401.       if (errno != EINTR)
  402.         perror_with_name ("select");
  403. #endif
  404.     }

  405.   /* Enqueue all detected file events.  */

  406.   for (file_ptr = gdb_notifier.first_file_handler;
  407.        file_ptr != NULL && num_found > 0;
  408.        file_ptr = file_ptr->next_file)
  409.     {
  410.       int mask = 0;

  411.       if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0]))
  412.         mask |= GDB_READABLE;
  413.       if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1]))
  414.         mask |= GDB_WRITABLE;
  415.       if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2]))
  416.         mask |= GDB_EXCEPTION;

  417.       if (!mask)
  418.         continue;
  419.       else
  420.         num_found--;

  421.       /* Enqueue an event only if this is still a new event for this
  422.          fd.  */

  423.       if (file_ptr->ready_mask == 0)
  424.         {
  425.           gdb_event *file_event_ptr = create_file_event (file_ptr->fd);

  426.           QUEUE_enque (gdb_event_p, event_queue, file_event_ptr);
  427.         }
  428.       file_ptr->ready_mask = mask;
  429.     }

  430.   return 0;
  431. }

  432. /* Start up the event loop.  This is the entry point to the event
  433.    loop.  */

  434. void
  435. start_event_loop (void)
  436. {
  437.   /* Loop until there is nothing to do.  This is the entry point to
  438.      the event loop engine.  If nothing is ready at this time, wait
  439.      for something to happen (via wait_for_event), then process it.
  440.      Return when there are no longer event sources to wait for.  */

  441.   while (1)
  442.     {
  443.       /* Any events already waiting in the queue?  */
  444.       int res = process_event ();

  445.       /* Did the event handler want the event loop to stop?  */
  446.       if (res == -1)
  447.         return;

  448.       if (res)
  449.         continue;

  450.       /* Process any queued callbacks before we go to sleep.  */
  451.       res = process_callback ();

  452.       /* Did the callback want the event loop to stop?  */
  453.       if (res == -1)
  454.         return;

  455.       if (res)
  456.         continue;

  457.       /* Wait for a new event.  If wait_for_event returns -1, we
  458.          should get out because this means that there are no event
  459.          sources left.  This will make the event loop stop, and the
  460.          application exit.  */

  461.       if (wait_for_event () < 0)
  462.         return;
  463.     }

  464.   /* We are done with the event loop.  There are no more event sources
  465.      to listen to.  So we exit gdbserver.  */
  466. }