Home | History | Annotate | Line # | Download | only in gdbsupport
event-loop.cc revision 1.1
      1  1.1  christos /* Event loop machinery for GDB, the GNU debugger.
      2  1.1  christos    Copyright (C) 1999-2020 Free Software Foundation, Inc.
      3  1.1  christos    Written by Elena Zannoni <ezannoni (at) cygnus.com> of Cygnus Solutions.
      4  1.1  christos 
      5  1.1  christos    This file is part of GDB.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19  1.1  christos 
     20  1.1  christos #include "gdbsupport/common-defs.h"
     21  1.1  christos #include "gdbsupport/event-loop.h"
     22  1.1  christos 
     23  1.1  christos #include <chrono>
     24  1.1  christos 
     25  1.1  christos #ifdef HAVE_POLL
     26  1.1  christos #if defined (HAVE_POLL_H)
     27  1.1  christos #include <poll.h>
     28  1.1  christos #elif defined (HAVE_SYS_POLL_H)
     29  1.1  christos #include <sys/poll.h>
     30  1.1  christos #endif
     31  1.1  christos #endif
     32  1.1  christos 
     33  1.1  christos #include <sys/types.h>
     34  1.1  christos #include "gdbsupport/gdb_sys_time.h"
     35  1.1  christos #include "gdbsupport/gdb_select.h"
     36  1.1  christos 
     37  1.1  christos /* Tell create_file_handler what events we are interested in.
     38  1.1  christos    This is used by the select version of the event loop.  */
     39  1.1  christos 
     40  1.1  christos #define GDB_READABLE	(1<<1)
     41  1.1  christos #define GDB_WRITABLE	(1<<2)
     42  1.1  christos #define GDB_EXCEPTION	(1<<3)
     43  1.1  christos 
     44  1.1  christos /* Information about each file descriptor we register with the event
     45  1.1  christos    loop.  */
     46  1.1  christos 
     47  1.1  christos typedef struct file_handler
     48  1.1  christos   {
     49  1.1  christos     int fd;			/* File descriptor.  */
     50  1.1  christos     int mask;			/* Events we want to monitor: POLLIN, etc.  */
     51  1.1  christos     int ready_mask;		/* Events that have been seen since
     52  1.1  christos 				   the last time.  */
     53  1.1  christos     handler_func *proc;		/* Procedure to call when fd is ready.  */
     54  1.1  christos     gdb_client_data client_data;	/* Argument to pass to proc.  */
     55  1.1  christos     int error;			/* Was an error detected on this fd?  */
     56  1.1  christos     struct file_handler *next_file;	/* Next registered file descriptor.  */
     57  1.1  christos   }
     58  1.1  christos file_handler;
     59  1.1  christos 
     60  1.1  christos /* Do we use poll or select ? */
     61  1.1  christos #ifdef HAVE_POLL
     62  1.1  christos #define USE_POLL 1
     63  1.1  christos #else
     64  1.1  christos #define USE_POLL 0
     65  1.1  christos #endif /* HAVE_POLL */
     66  1.1  christos 
     67  1.1  christos static unsigned char use_poll = USE_POLL;
     68  1.1  christos 
     69  1.1  christos #ifdef USE_WIN32API
     70  1.1  christos #include <windows.h>
     71  1.1  christos #include <io.h>
     72  1.1  christos #endif
     73  1.1  christos 
     74  1.1  christos /* Gdb_notifier is just a list of file descriptors gdb is interested in.
     75  1.1  christos    These are the input file descriptor, and the target file
     76  1.1  christos    descriptor.  We have two flavors of the notifier, one for platforms
     77  1.1  christos    that have the POLL function, the other for those that don't, and
     78  1.1  christos    only support SELECT.  Each of the elements in the gdb_notifier list is
     79  1.1  christos    basically a description of what kind of events gdb is interested
     80  1.1  christos    in, for each fd.  */
     81  1.1  christos 
     82  1.1  christos static struct
     83  1.1  christos   {
     84  1.1  christos     /* Ptr to head of file handler list.  */
     85  1.1  christos     file_handler *first_file_handler;
     86  1.1  christos 
     87  1.1  christos     /* Next file handler to handle, for the select variant.  To level
     88  1.1  christos        the fairness across event sources, we serve file handlers in a
     89  1.1  christos        round-robin-like fashion.  The number and order of the polled
     90  1.1  christos        file handlers may change between invocations, but this is good
     91  1.1  christos        enough.  */
     92  1.1  christos     file_handler *next_file_handler;
     93  1.1  christos 
     94  1.1  christos #ifdef HAVE_POLL
     95  1.1  christos     /* Ptr to array of pollfd structures.  */
     96  1.1  christos     struct pollfd *poll_fds;
     97  1.1  christos 
     98  1.1  christos     /* Next file descriptor to handle, for the poll variant.  To level
     99  1.1  christos        the fairness across event sources, we poll the file descriptors
    100  1.1  christos        in a round-robin-like fashion.  The number and order of the
    101  1.1  christos        polled file descriptors may change between invocations, but
    102  1.1  christos        this is good enough.  */
    103  1.1  christos     int next_poll_fds_index;
    104  1.1  christos 
    105  1.1  christos     /* Timeout in milliseconds for calls to poll().  */
    106  1.1  christos     int poll_timeout;
    107  1.1  christos #endif
    108  1.1  christos 
    109  1.1  christos     /* Masks to be used in the next call to select.
    110  1.1  christos        Bits are set in response to calls to create_file_handler.  */
    111  1.1  christos     fd_set check_masks[3];
    112  1.1  christos 
    113  1.1  christos     /* What file descriptors were found ready by select.  */
    114  1.1  christos     fd_set ready_masks[3];
    115  1.1  christos 
    116  1.1  christos     /* Number of file descriptors to monitor (for poll).  */
    117  1.1  christos     /* Number of valid bits (highest fd value + 1) (for select).  */
    118  1.1  christos     int num_fds;
    119  1.1  christos 
    120  1.1  christos     /* Time structure for calls to select().  */
    121  1.1  christos     struct timeval select_timeout;
    122  1.1  christos 
    123  1.1  christos     /* Flag to tell whether the timeout should be used.  */
    124  1.1  christos     int timeout_valid;
    125  1.1  christos   }
    126  1.1  christos gdb_notifier;
    127  1.1  christos 
    128  1.1  christos /* Structure associated with a timer.  PROC will be executed at the
    129  1.1  christos    first occasion after WHEN.  */
    130  1.1  christos struct gdb_timer
    131  1.1  christos   {
    132  1.1  christos     std::chrono::steady_clock::time_point when;
    133  1.1  christos     int timer_id;
    134  1.1  christos     struct gdb_timer *next;
    135  1.1  christos     timer_handler_func *proc;	    /* Function to call to do the work.  */
    136  1.1  christos     gdb_client_data client_data;    /* Argument to async_handler_func.  */
    137  1.1  christos   };
    138  1.1  christos 
    139  1.1  christos /* List of currently active timers.  It is sorted in order of
    140  1.1  christos    increasing timers.  */
    141  1.1  christos static struct
    142  1.1  christos   {
    143  1.1  christos     /* Pointer to first in timer list.  */
    144  1.1  christos     struct gdb_timer *first_timer;
    145  1.1  christos 
    146  1.1  christos     /* Id of the last timer created.  */
    147  1.1  christos     int num_timers;
    148  1.1  christos   }
    149  1.1  christos timer_list;
    150  1.1  christos 
    151  1.1  christos static void create_file_handler (int fd, int mask, handler_func *proc,
    152  1.1  christos 				 gdb_client_data client_data);
    153  1.1  christos static int gdb_wait_for_event (int);
    154  1.1  christos static int update_wait_timeout (void);
    155  1.1  christos static int poll_timers (void);
    156  1.1  christos 
    157  1.1  christos /* Process one high level event.  If nothing is ready at this time,
    159  1.1  christos    wait for something to happen (via gdb_wait_for_event), then process
    160  1.1  christos    it.  Returns >0 if something was done otherwise returns <0 (this
    161  1.1  christos    can happen if there are no event sources to wait for).  */
    162  1.1  christos 
    163  1.1  christos int
    164  1.1  christos gdb_do_one_event (void)
    165  1.1  christos {
    166  1.1  christos   static int event_source_head = 0;
    167  1.1  christos   const int number_of_sources = 3;
    168  1.1  christos   int current = 0;
    169  1.1  christos 
    170  1.1  christos   /* First let's see if there are any asynchronous signal handlers
    171  1.1  christos      that are ready.  These would be the result of invoking any of the
    172  1.1  christos      signal handlers.  */
    173  1.1  christos   if (invoke_async_signal_handlers ())
    174  1.1  christos     return 1;
    175  1.1  christos 
    176  1.1  christos   /* To level the fairness across event sources, we poll them in a
    177  1.1  christos      round-robin fashion.  */
    178  1.1  christos   for (current = 0; current < number_of_sources; current++)
    179  1.1  christos     {
    180  1.1  christos       int res;
    181  1.1  christos 
    182  1.1  christos       switch (event_source_head)
    183  1.1  christos 	{
    184  1.1  christos 	case 0:
    185  1.1  christos 	  /* Are any timers that are ready?  */
    186  1.1  christos 	  res = poll_timers ();
    187  1.1  christos 	  break;
    188  1.1  christos 	case 1:
    189  1.1  christos 	  /* Are there events already waiting to be collected on the
    190  1.1  christos 	     monitored file descriptors?  */
    191  1.1  christos 	  res = gdb_wait_for_event (0);
    192  1.1  christos 	  break;
    193  1.1  christos 	case 2:
    194  1.1  christos 	  /* Are there any asynchronous event handlers ready?  */
    195  1.1  christos 	  res = check_async_event_handlers ();
    196  1.1  christos 	  break;
    197  1.1  christos 	default:
    198  1.1  christos 	  internal_error (__FILE__, __LINE__,
    199  1.1  christos 			  "unexpected event_source_head %d",
    200  1.1  christos 			  event_source_head);
    201  1.1  christos 	}
    202  1.1  christos 
    203  1.1  christos       event_source_head++;
    204  1.1  christos       if (event_source_head == number_of_sources)
    205  1.1  christos 	event_source_head = 0;
    206  1.1  christos 
    207  1.1  christos       if (res > 0)
    208  1.1  christos 	return 1;
    209  1.1  christos     }
    210  1.1  christos 
    211  1.1  christos   /* Block waiting for a new event.  If gdb_wait_for_event returns -1,
    212  1.1  christos      we should get out because this means that there are no event
    213  1.1  christos      sources left.  This will make the event loop stop, and the
    214  1.1  christos      application exit.  */
    215  1.1  christos 
    216  1.1  christos   if (gdb_wait_for_event (1) < 0)
    217  1.1  christos     return -1;
    218  1.1  christos 
    219  1.1  christos   /* If gdb_wait_for_event has returned 1, it means that one event has
    220  1.1  christos      been handled.  We break out of the loop.  */
    221  1.1  christos   return 1;
    222  1.1  christos }
    223  1.1  christos 
    224  1.1  christos 
    225  1.1  christos 
    227  1.1  christos /* Wrapper function for create_file_handler, so that the caller
    228  1.1  christos    doesn't have to know implementation details about the use of poll
    229  1.1  christos    vs. select.  */
    230  1.1  christos void
    231  1.1  christos add_file_handler (int fd, handler_func * proc, gdb_client_data client_data)
    232  1.1  christos {
    233  1.1  christos #ifdef HAVE_POLL
    234  1.1  christos   struct pollfd fds;
    235  1.1  christos #endif
    236  1.1  christos 
    237  1.1  christos   if (use_poll)
    238  1.1  christos     {
    239  1.1  christos #ifdef HAVE_POLL
    240  1.1  christos       /* Check to see if poll () is usable.  If not, we'll switch to
    241  1.1  christos          use select.  This can happen on systems like
    242  1.1  christos          m68k-motorola-sys, `poll' cannot be used to wait for `stdin'.
    243  1.1  christos          On m68k-motorola-sysv, tty's are not stream-based and not
    244  1.1  christos          `poll'able.  */
    245  1.1  christos       fds.fd = fd;
    246  1.1  christos       fds.events = POLLIN;
    247  1.1  christos       if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL))
    248  1.1  christos 	use_poll = 0;
    249  1.1  christos #else
    250  1.1  christos       internal_error (__FILE__, __LINE__,
    251  1.1  christos 		      _("use_poll without HAVE_POLL"));
    252  1.1  christos #endif /* HAVE_POLL */
    253  1.1  christos     }
    254  1.1  christos   if (use_poll)
    255  1.1  christos     {
    256  1.1  christos #ifdef HAVE_POLL
    257  1.1  christos       create_file_handler (fd, POLLIN, proc, client_data);
    258  1.1  christos #else
    259  1.1  christos       internal_error (__FILE__, __LINE__,
    260  1.1  christos 		      _("use_poll without HAVE_POLL"));
    261  1.1  christos #endif
    262  1.1  christos     }
    263  1.1  christos   else
    264  1.1  christos     create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION,
    265  1.1  christos 			 proc, client_data);
    266  1.1  christos }
    267  1.1  christos 
    268  1.1  christos /* Add a file handler/descriptor to the list of descriptors we are
    269  1.1  christos    interested in.
    270  1.1  christos 
    271  1.1  christos    FD is the file descriptor for the file/stream to be listened to.
    272  1.1  christos 
    273  1.1  christos    For the poll case, MASK is a combination (OR) of POLLIN,
    274  1.1  christos    POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND:
    275  1.1  christos    these are the events we are interested in.  If any of them occurs,
    276  1.1  christos    proc should be called.
    277  1.1  christos 
    278  1.1  christos    For the select case, MASK is a combination of READABLE, WRITABLE,
    279  1.1  christos    EXCEPTION.  PROC is the procedure that will be called when an event
    280  1.1  christos    occurs for FD.  CLIENT_DATA is the argument to pass to PROC.  */
    281  1.1  christos 
    282  1.1  christos static void
    283  1.1  christos create_file_handler (int fd, int mask, handler_func * proc,
    284  1.1  christos 		     gdb_client_data client_data)
    285  1.1  christos {
    286  1.1  christos   file_handler *file_ptr;
    287  1.1  christos 
    288  1.1  christos   /* Do we already have a file handler for this file?  (We may be
    289  1.1  christos      changing its associated procedure).  */
    290  1.1  christos   for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
    291  1.1  christos        file_ptr = file_ptr->next_file)
    292  1.1  christos     {
    293  1.1  christos       if (file_ptr->fd == fd)
    294  1.1  christos 	break;
    295  1.1  christos     }
    296  1.1  christos 
    297  1.1  christos   /* It is a new file descriptor.  Add it to the list.  Otherwise, just
    298  1.1  christos      change the data associated with it.  */
    299  1.1  christos   if (file_ptr == NULL)
    300  1.1  christos     {
    301  1.1  christos       file_ptr = XNEW (file_handler);
    302  1.1  christos       file_ptr->fd = fd;
    303  1.1  christos       file_ptr->ready_mask = 0;
    304  1.1  christos       file_ptr->next_file = gdb_notifier.first_file_handler;
    305  1.1  christos       gdb_notifier.first_file_handler = file_ptr;
    306  1.1  christos 
    307  1.1  christos       if (use_poll)
    308  1.1  christos 	{
    309  1.1  christos #ifdef HAVE_POLL
    310  1.1  christos 	  gdb_notifier.num_fds++;
    311  1.1  christos 	  if (gdb_notifier.poll_fds)
    312  1.1  christos 	    gdb_notifier.poll_fds =
    313  1.1  christos 	      (struct pollfd *) xrealloc (gdb_notifier.poll_fds,
    314  1.1  christos 					  (gdb_notifier.num_fds
    315  1.1  christos 					   * sizeof (struct pollfd)));
    316  1.1  christos 	  else
    317  1.1  christos 	    gdb_notifier.poll_fds =
    318  1.1  christos 	      XNEW (struct pollfd);
    319  1.1  christos 	  (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd = fd;
    320  1.1  christos 	  (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events = mask;
    321  1.1  christos 	  (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents = 0;
    322  1.1  christos #else
    323  1.1  christos 	  internal_error (__FILE__, __LINE__,
    324  1.1  christos 			  _("use_poll without HAVE_POLL"));
    325  1.1  christos #endif /* HAVE_POLL */
    326  1.1  christos 	}
    327  1.1  christos       else
    328  1.1  christos 	{
    329  1.1  christos 	  if (mask & GDB_READABLE)
    330  1.1  christos 	    FD_SET (fd, &gdb_notifier.check_masks[0]);
    331  1.1  christos 	  else
    332  1.1  christos 	    FD_CLR (fd, &gdb_notifier.check_masks[0]);
    333  1.1  christos 
    334  1.1  christos 	  if (mask & GDB_WRITABLE)
    335  1.1  christos 	    FD_SET (fd, &gdb_notifier.check_masks[1]);
    336  1.1  christos 	  else
    337  1.1  christos 	    FD_CLR (fd, &gdb_notifier.check_masks[1]);
    338  1.1  christos 
    339  1.1  christos 	  if (mask & GDB_EXCEPTION)
    340  1.1  christos 	    FD_SET (fd, &gdb_notifier.check_masks[2]);
    341  1.1  christos 	  else
    342  1.1  christos 	    FD_CLR (fd, &gdb_notifier.check_masks[2]);
    343  1.1  christos 
    344  1.1  christos 	  if (gdb_notifier.num_fds <= fd)
    345  1.1  christos 	    gdb_notifier.num_fds = fd + 1;
    346  1.1  christos 	}
    347  1.1  christos     }
    348  1.1  christos 
    349  1.1  christos   file_ptr->proc = proc;
    350  1.1  christos   file_ptr->client_data = client_data;
    351  1.1  christos   file_ptr->mask = mask;
    352  1.1  christos }
    353  1.1  christos 
    354  1.1  christos /* Return the next file handler to handle, and advance to the next
    355  1.1  christos    file handler, wrapping around if the end of the list is
    356  1.1  christos    reached.  */
    357  1.1  christos 
    358  1.1  christos static file_handler *
    359  1.1  christos get_next_file_handler_to_handle_and_advance (void)
    360  1.1  christos {
    361  1.1  christos   file_handler *curr_next;
    362  1.1  christos 
    363  1.1  christos   /* The first time around, this is still NULL.  */
    364  1.1  christos   if (gdb_notifier.next_file_handler == NULL)
    365  1.1  christos     gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
    366  1.1  christos 
    367  1.1  christos   curr_next = gdb_notifier.next_file_handler;
    368  1.1  christos   gdb_assert (curr_next != NULL);
    369  1.1  christos 
    370  1.1  christos   /* Advance.  */
    371  1.1  christos   gdb_notifier.next_file_handler = curr_next->next_file;
    372  1.1  christos   /* Wrap around, if necessary.  */
    373  1.1  christos   if (gdb_notifier.next_file_handler == NULL)
    374  1.1  christos     gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
    375  1.1  christos 
    376  1.1  christos   return curr_next;
    377  1.1  christos }
    378  1.1  christos 
    379  1.1  christos /* Remove the file descriptor FD from the list of monitored fd's:
    380  1.1  christos    i.e. we don't care anymore about events on the FD.  */
    381  1.1  christos void
    382  1.1  christos delete_file_handler (int fd)
    383  1.1  christos {
    384  1.1  christos   file_handler *file_ptr, *prev_ptr = NULL;
    385  1.1  christos   int i;
    386  1.1  christos #ifdef HAVE_POLL
    387  1.1  christos   int j;
    388  1.1  christos   struct pollfd *new_poll_fds;
    389  1.1  christos #endif
    390  1.1  christos 
    391  1.1  christos   /* Find the entry for the given file.  */
    392  1.1  christos 
    393  1.1  christos   for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
    394  1.1  christos        file_ptr = file_ptr->next_file)
    395  1.1  christos     {
    396  1.1  christos       if (file_ptr->fd == fd)
    397  1.1  christos 	break;
    398  1.1  christos     }
    399  1.1  christos 
    400  1.1  christos   if (file_ptr == NULL)
    401  1.1  christos     return;
    402  1.1  christos 
    403  1.1  christos   if (use_poll)
    404  1.1  christos     {
    405  1.1  christos #ifdef HAVE_POLL
    406  1.1  christos       /* Create a new poll_fds array by copying every fd's information
    407  1.1  christos          but the one we want to get rid of.  */
    408  1.1  christos 
    409  1.1  christos       new_poll_fds = (struct pollfd *)
    410  1.1  christos 	xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd));
    411  1.1  christos 
    412  1.1  christos       for (i = 0, j = 0; i < gdb_notifier.num_fds; i++)
    413  1.1  christos 	{
    414  1.1  christos 	  if ((gdb_notifier.poll_fds + i)->fd != fd)
    415  1.1  christos 	    {
    416  1.1  christos 	      (new_poll_fds + j)->fd = (gdb_notifier.poll_fds + i)->fd;
    417  1.1  christos 	      (new_poll_fds + j)->events = (gdb_notifier.poll_fds + i)->events;
    418  1.1  christos 	      (new_poll_fds + j)->revents
    419  1.1  christos 		= (gdb_notifier.poll_fds + i)->revents;
    420  1.1  christos 	      j++;
    421  1.1  christos 	    }
    422  1.1  christos 	}
    423  1.1  christos       xfree (gdb_notifier.poll_fds);
    424  1.1  christos       gdb_notifier.poll_fds = new_poll_fds;
    425  1.1  christos       gdb_notifier.num_fds--;
    426  1.1  christos #else
    427  1.1  christos       internal_error (__FILE__, __LINE__,
    428  1.1  christos 		      _("use_poll without HAVE_POLL"));
    429  1.1  christos #endif /* HAVE_POLL */
    430  1.1  christos     }
    431  1.1  christos   else
    432  1.1  christos     {
    433  1.1  christos       if (file_ptr->mask & GDB_READABLE)
    434  1.1  christos 	FD_CLR (fd, &gdb_notifier.check_masks[0]);
    435  1.1  christos       if (file_ptr->mask & GDB_WRITABLE)
    436  1.1  christos 	FD_CLR (fd, &gdb_notifier.check_masks[1]);
    437  1.1  christos       if (file_ptr->mask & GDB_EXCEPTION)
    438  1.1  christos 	FD_CLR (fd, &gdb_notifier.check_masks[2]);
    439  1.1  christos 
    440  1.1  christos       /* Find current max fd.  */
    441  1.1  christos 
    442  1.1  christos       if ((fd + 1) == gdb_notifier.num_fds)
    443  1.1  christos 	{
    444  1.1  christos 	  gdb_notifier.num_fds--;
    445  1.1  christos 	  for (i = gdb_notifier.num_fds; i; i--)
    446  1.1  christos 	    {
    447  1.1  christos 	      if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0])
    448  1.1  christos 		  || FD_ISSET (i - 1, &gdb_notifier.check_masks[1])
    449  1.1  christos 		  || FD_ISSET (i - 1, &gdb_notifier.check_masks[2]))
    450  1.1  christos 		break;
    451  1.1  christos 	    }
    452  1.1  christos 	  gdb_notifier.num_fds = i;
    453  1.1  christos 	}
    454  1.1  christos     }
    455  1.1  christos 
    456  1.1  christos   /* Deactivate the file descriptor, by clearing its mask,
    457  1.1  christos      so that it will not fire again.  */
    458  1.1  christos 
    459  1.1  christos   file_ptr->mask = 0;
    460  1.1  christos 
    461  1.1  christos   /* If this file handler was going to be the next one to be handled,
    462  1.1  christos      advance to the next's next, if any.  */
    463  1.1  christos   if (gdb_notifier.next_file_handler == file_ptr)
    464  1.1  christos     {
    465  1.1  christos       if (file_ptr->next_file == NULL
    466  1.1  christos 	  && file_ptr == gdb_notifier.first_file_handler)
    467  1.1  christos 	gdb_notifier.next_file_handler = NULL;
    468  1.1  christos       else
    469  1.1  christos 	get_next_file_handler_to_handle_and_advance ();
    470  1.1  christos     }
    471  1.1  christos 
    472  1.1  christos   /* Get rid of the file handler in the file handler list.  */
    473  1.1  christos   if (file_ptr == gdb_notifier.first_file_handler)
    474  1.1  christos     gdb_notifier.first_file_handler = file_ptr->next_file;
    475  1.1  christos   else
    476  1.1  christos     {
    477  1.1  christos       for (prev_ptr = gdb_notifier.first_file_handler;
    478  1.1  christos 	   prev_ptr->next_file != file_ptr;
    479  1.1  christos 	   prev_ptr = prev_ptr->next_file)
    480  1.1  christos 	;
    481  1.1  christos       prev_ptr->next_file = file_ptr->next_file;
    482  1.1  christos     }
    483  1.1  christos   xfree (file_ptr);
    484  1.1  christos }
    485  1.1  christos 
    486  1.1  christos /* Handle the given event by calling the procedure associated to the
    487  1.1  christos    corresponding file handler.  */
    488  1.1  christos 
    489  1.1  christos static void
    490  1.1  christos handle_file_event (file_handler *file_ptr, int ready_mask)
    491  1.1  christos {
    492  1.1  christos   int mask;
    493  1.1  christos #ifdef HAVE_POLL
    494  1.1  christos   int error_mask;
    495  1.1  christos #endif
    496  1.1  christos 
    497  1.1  christos     {
    498  1.1  christos 	{
    499  1.1  christos 	  /* With poll, the ready_mask could have any of three events
    500  1.1  christos 	     set to 1: POLLHUP, POLLERR, POLLNVAL.  These events
    501  1.1  christos 	     cannot be used in the requested event mask (events), but
    502  1.1  christos 	     they can be returned in the return mask (revents).  We
    503  1.1  christos 	     need to check for those event too, and add them to the
    504  1.1  christos 	     mask which will be passed to the handler.  */
    505  1.1  christos 
    506  1.1  christos 	  /* See if the desired events (mask) match the received
    507  1.1  christos 	     events (ready_mask).  */
    508  1.1  christos 
    509  1.1  christos 	  if (use_poll)
    510  1.1  christos 	    {
    511  1.1  christos #ifdef HAVE_POLL
    512  1.1  christos 	      /* POLLHUP means EOF, but can be combined with POLLIN to
    513  1.1  christos 		 signal more data to read.  */
    514  1.1  christos 	      error_mask = POLLHUP | POLLERR | POLLNVAL;
    515  1.1  christos 	      mask = ready_mask & (file_ptr->mask | error_mask);
    516  1.1  christos 
    517  1.1  christos 	      if ((mask & (POLLERR | POLLNVAL)) != 0)
    518  1.1  christos 		{
    519  1.1  christos 		  /* Work in progress.  We may need to tell somebody
    520  1.1  christos 		     what kind of error we had.  */
    521  1.1  christos 		  if (mask & POLLERR)
    522  1.1  christos 		    warning (_("Error detected on fd %d"), file_ptr->fd);
    523  1.1  christos 		  if (mask & POLLNVAL)
    524  1.1  christos 		    warning (_("Invalid or non-`poll'able fd %d"),
    525  1.1  christos 			     file_ptr->fd);
    526  1.1  christos 		  file_ptr->error = 1;
    527  1.1  christos 		}
    528  1.1  christos 	      else
    529  1.1  christos 		file_ptr->error = 0;
    530  1.1  christos #else
    531  1.1  christos 	      internal_error (__FILE__, __LINE__,
    532  1.1  christos 			      _("use_poll without HAVE_POLL"));
    533  1.1  christos #endif /* HAVE_POLL */
    534  1.1  christos 	    }
    535  1.1  christos 	  else
    536  1.1  christos 	    {
    537  1.1  christos 	      if (ready_mask & GDB_EXCEPTION)
    538  1.1  christos 		{
    539  1.1  christos 		  warning (_("Exception condition detected on fd %d"),
    540  1.1  christos 			   file_ptr->fd);
    541  1.1  christos 		  file_ptr->error = 1;
    542  1.1  christos 		}
    543  1.1  christos 	      else
    544  1.1  christos 		file_ptr->error = 0;
    545  1.1  christos 	      mask = ready_mask & file_ptr->mask;
    546  1.1  christos 	    }
    547  1.1  christos 
    548  1.1  christos 	  /* If there was a match, then call the handler.  */
    549  1.1  christos 	  if (mask != 0)
    550  1.1  christos 	    (*file_ptr->proc) (file_ptr->error, file_ptr->client_data);
    551  1.1  christos 	}
    552  1.1  christos     }
    553  1.1  christos }
    554  1.1  christos 
    555  1.1  christos /* Wait for new events on the monitored file descriptors.  Run the
    556  1.1  christos    event handler if the first descriptor that is detected by the poll.
    557  1.1  christos    If BLOCK and if there are no events, this function will block in
    558  1.1  christos    the call to poll.  Return 1 if an event was handled.  Return -1 if
    559  1.1  christos    there are no file descriptors to monitor.  Return 1 if an event was
    560  1.1  christos    handled, otherwise returns 0.  */
    561  1.1  christos 
    562  1.1  christos static int
    563  1.1  christos gdb_wait_for_event (int block)
    564  1.1  christos {
    565  1.1  christos   file_handler *file_ptr;
    566  1.1  christos   int num_found = 0;
    567  1.1  christos 
    568  1.1  christos   /* Make sure all output is done before getting another event.  */
    569  1.1  christos   flush_streams ();
    570  1.1  christos 
    571  1.1  christos   if (gdb_notifier.num_fds == 0)
    572  1.1  christos     return -1;
    573  1.1  christos 
    574  1.1  christos   if (block)
    575  1.1  christos     update_wait_timeout ();
    576  1.1  christos 
    577  1.1  christos   if (use_poll)
    578  1.1  christos     {
    579  1.1  christos #ifdef HAVE_POLL
    580  1.1  christos       int timeout;
    581  1.1  christos 
    582  1.1  christos       if (block)
    583  1.1  christos 	timeout = gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1;
    584  1.1  christos       else
    585  1.1  christos 	timeout = 0;
    586  1.1  christos 
    587  1.1  christos       num_found = poll (gdb_notifier.poll_fds,
    588  1.1  christos 			(unsigned long) gdb_notifier.num_fds, timeout);
    589  1.1  christos 
    590  1.1  christos       /* Don't print anything if we get out of poll because of a
    591  1.1  christos 	 signal.  */
    592  1.1  christos       if (num_found == -1 && errno != EINTR)
    593  1.1  christos 	perror_with_name (("poll"));
    594  1.1  christos #else
    595  1.1  christos       internal_error (__FILE__, __LINE__,
    596  1.1  christos 		      _("use_poll without HAVE_POLL"));
    597  1.1  christos #endif /* HAVE_POLL */
    598  1.1  christos     }
    599  1.1  christos   else
    600  1.1  christos     {
    601  1.1  christos       struct timeval select_timeout;
    602  1.1  christos       struct timeval *timeout_p;
    603  1.1  christos 
    604  1.1  christos       if (block)
    605  1.1  christos 	timeout_p = gdb_notifier.timeout_valid
    606  1.1  christos 	  ? &gdb_notifier.select_timeout : NULL;
    607  1.1  christos       else
    608  1.1  christos 	{
    609  1.1  christos 	  memset (&select_timeout, 0, sizeof (select_timeout));
    610  1.1  christos 	  timeout_p = &select_timeout;
    611  1.1  christos 	}
    612  1.1  christos 
    613  1.1  christos       gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0];
    614  1.1  christos       gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1];
    615  1.1  christos       gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2];
    616  1.1  christos       num_found = gdb_select (gdb_notifier.num_fds,
    617  1.1  christos 			      &gdb_notifier.ready_masks[0],
    618  1.1  christos 			      &gdb_notifier.ready_masks[1],
    619  1.1  christos 			      &gdb_notifier.ready_masks[2],
    620  1.1  christos 			      timeout_p);
    621  1.1  christos 
    622  1.1  christos       /* Clear the masks after an error from select.  */
    623  1.1  christos       if (num_found == -1)
    624  1.1  christos 	{
    625  1.1  christos 	  FD_ZERO (&gdb_notifier.ready_masks[0]);
    626  1.1  christos 	  FD_ZERO (&gdb_notifier.ready_masks[1]);
    627  1.1  christos 	  FD_ZERO (&gdb_notifier.ready_masks[2]);
    628  1.1  christos 
    629  1.1  christos 	  /* Dont print anything if we got a signal, let gdb handle
    630  1.1  christos 	     it.  */
    631  1.1  christos 	  if (errno != EINTR)
    632  1.1  christos 	    perror_with_name (("select"));
    633  1.1  christos 	}
    634  1.1  christos     }
    635  1.1  christos 
    636  1.1  christos   /* Avoid looking at poll_fds[i]->revents if no event fired.  */
    637  1.1  christos   if (num_found <= 0)
    638  1.1  christos     return 0;
    639  1.1  christos 
    640  1.1  christos   /* Run event handlers.  We always run just one handler and go back
    641  1.1  christos      to polling, in case a handler changes the notifier list.  Since
    642  1.1  christos      events for sources we haven't consumed yet wake poll/select
    643  1.1  christos      immediately, no event is lost.  */
    644  1.1  christos 
    645  1.1  christos   /* To level the fairness across event descriptors, we handle them in
    646  1.1  christos      a round-robin-like fashion.  The number and order of descriptors
    647  1.1  christos      may change between invocations, but this is good enough.  */
    648  1.1  christos   if (use_poll)
    649  1.1  christos     {
    650  1.1  christos #ifdef HAVE_POLL
    651  1.1  christos       int i;
    652  1.1  christos       int mask;
    653  1.1  christos 
    654  1.1  christos       while (1)
    655  1.1  christos 	{
    656  1.1  christos 	  if (gdb_notifier.next_poll_fds_index >= gdb_notifier.num_fds)
    657  1.1  christos 	    gdb_notifier.next_poll_fds_index = 0;
    658  1.1  christos 	  i = gdb_notifier.next_poll_fds_index++;
    659  1.1  christos 
    660  1.1  christos 	  gdb_assert (i < gdb_notifier.num_fds);
    661  1.1  christos 	  if ((gdb_notifier.poll_fds + i)->revents)
    662  1.1  christos 	    break;
    663  1.1  christos 	}
    664  1.1  christos 
    665  1.1  christos       for (file_ptr = gdb_notifier.first_file_handler;
    666  1.1  christos 	   file_ptr != NULL;
    667  1.1  christos 	   file_ptr = file_ptr->next_file)
    668  1.1  christos 	{
    669  1.1  christos 	  if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd)
    670  1.1  christos 	    break;
    671  1.1  christos 	}
    672  1.1  christos       gdb_assert (file_ptr != NULL);
    673  1.1  christos 
    674  1.1  christos       mask = (gdb_notifier.poll_fds + i)->revents;
    675  1.1  christos       handle_file_event (file_ptr, mask);
    676  1.1  christos       return 1;
    677  1.1  christos #else
    678  1.1  christos       internal_error (__FILE__, __LINE__,
    679  1.1  christos 		      _("use_poll without HAVE_POLL"));
    680  1.1  christos #endif /* HAVE_POLL */
    681  1.1  christos     }
    682  1.1  christos   else
    683  1.1  christos     {
    684  1.1  christos       /* See comment about even source fairness above.  */
    685  1.1  christos       int mask = 0;
    686  1.1  christos 
    687  1.1  christos       do
    688  1.1  christos 	{
    689  1.1  christos 	  file_ptr = get_next_file_handler_to_handle_and_advance ();
    690  1.1  christos 
    691  1.1  christos 	  if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0]))
    692  1.1  christos 	    mask |= GDB_READABLE;
    693  1.1  christos 	  if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1]))
    694  1.1  christos 	    mask |= GDB_WRITABLE;
    695  1.1  christos 	  if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2]))
    696  1.1  christos 	    mask |= GDB_EXCEPTION;
    697  1.1  christos 	}
    698  1.1  christos       while (mask == 0);
    699  1.1  christos 
    700  1.1  christos       handle_file_event (file_ptr, mask);
    701  1.1  christos       return 1;
    702  1.1  christos     }
    703  1.1  christos   return 0;
    704  1.1  christos }
    705  1.1  christos 
    706  1.1  christos /* Create a timer that will expire in MS milliseconds from now.  When
    708  1.1  christos    the timer is ready, PROC will be executed.  At creation, the timer
    709  1.1  christos    is added to the timers queue.  This queue is kept sorted in order
    710  1.1  christos    of increasing timers.  Return a handle to the timer struct.  */
    711  1.1  christos 
    712  1.1  christos int
    713  1.1  christos create_timer (int ms, timer_handler_func *proc,
    714  1.1  christos 	      gdb_client_data client_data)
    715  1.1  christos {
    716  1.1  christos   using namespace std::chrono;
    717  1.1  christos   struct gdb_timer *timer_ptr, *timer_index, *prev_timer;
    718  1.1  christos 
    719  1.1  christos   steady_clock::time_point time_now = steady_clock::now ();
    720  1.1  christos 
    721  1.1  christos   timer_ptr = new gdb_timer ();
    722  1.1  christos   timer_ptr->when = time_now + milliseconds (ms);
    723  1.1  christos   timer_ptr->proc = proc;
    724  1.1  christos   timer_ptr->client_data = client_data;
    725  1.1  christos   timer_list.num_timers++;
    726  1.1  christos   timer_ptr->timer_id = timer_list.num_timers;
    727  1.1  christos 
    728  1.1  christos   /* Now add the timer to the timer queue, making sure it is sorted in
    729  1.1  christos      increasing order of expiration.  */
    730  1.1  christos 
    731  1.1  christos   for (timer_index = timer_list.first_timer;
    732  1.1  christos        timer_index != NULL;
    733  1.1  christos        timer_index = timer_index->next)
    734  1.1  christos     {
    735  1.1  christos       if (timer_index->when > timer_ptr->when)
    736  1.1  christos 	break;
    737  1.1  christos     }
    738  1.1  christos 
    739  1.1  christos   if (timer_index == timer_list.first_timer)
    740  1.1  christos     {
    741  1.1  christos       timer_ptr->next = timer_list.first_timer;
    742  1.1  christos       timer_list.first_timer = timer_ptr;
    743  1.1  christos 
    744  1.1  christos     }
    745  1.1  christos   else
    746  1.1  christos     {
    747  1.1  christos       for (prev_timer = timer_list.first_timer;
    748  1.1  christos 	   prev_timer->next != timer_index;
    749  1.1  christos 	   prev_timer = prev_timer->next)
    750  1.1  christos 	;
    751  1.1  christos 
    752  1.1  christos       prev_timer->next = timer_ptr;
    753  1.1  christos       timer_ptr->next = timer_index;
    754  1.1  christos     }
    755  1.1  christos 
    756  1.1  christos   gdb_notifier.timeout_valid = 0;
    757  1.1  christos   return timer_ptr->timer_id;
    758  1.1  christos }
    759  1.1  christos 
    760  1.1  christos /* There is a chance that the creator of the timer wants to get rid of
    761  1.1  christos    it before it expires.  */
    762  1.1  christos void
    763  1.1  christos delete_timer (int id)
    764  1.1  christos {
    765  1.1  christos   struct gdb_timer *timer_ptr, *prev_timer = NULL;
    766  1.1  christos 
    767  1.1  christos   /* Find the entry for the given timer.  */
    768  1.1  christos 
    769  1.1  christos   for (timer_ptr = timer_list.first_timer; timer_ptr != NULL;
    770  1.1  christos        timer_ptr = timer_ptr->next)
    771  1.1  christos     {
    772  1.1  christos       if (timer_ptr->timer_id == id)
    773  1.1  christos 	break;
    774  1.1  christos     }
    775  1.1  christos 
    776  1.1  christos   if (timer_ptr == NULL)
    777  1.1  christos     return;
    778  1.1  christos   /* Get rid of the timer in the timer list.  */
    779  1.1  christos   if (timer_ptr == timer_list.first_timer)
    780  1.1  christos     timer_list.first_timer = timer_ptr->next;
    781  1.1  christos   else
    782  1.1  christos     {
    783  1.1  christos       for (prev_timer = timer_list.first_timer;
    784  1.1  christos 	   prev_timer->next != timer_ptr;
    785  1.1  christos 	   prev_timer = prev_timer->next)
    786  1.1  christos 	;
    787  1.1  christos       prev_timer->next = timer_ptr->next;
    788  1.1  christos     }
    789  1.1  christos   delete timer_ptr;
    790  1.1  christos 
    791  1.1  christos   gdb_notifier.timeout_valid = 0;
    792  1.1  christos }
    793  1.1  christos 
    794  1.1  christos /* Convert a std::chrono duration to a struct timeval.  */
    795  1.1  christos 
    796  1.1  christos template<typename Duration>
    797  1.1  christos static struct timeval
    798  1.1  christos duration_cast_timeval (const Duration &d)
    799  1.1  christos {
    800  1.1  christos   using namespace std::chrono;
    801  1.1  christos   seconds sec = duration_cast<seconds> (d);
    802  1.1  christos   microseconds msec = duration_cast<microseconds> (d - sec);
    803  1.1  christos 
    804  1.1  christos   struct timeval tv;
    805  1.1  christos   tv.tv_sec = sec.count ();
    806  1.1  christos   tv.tv_usec = msec.count ();
    807  1.1  christos   return tv;
    808  1.1  christos }
    809  1.1  christos 
    810  1.1  christos /* Update the timeout for the select() or poll().  Returns true if the
    811  1.1  christos    timer has already expired, false otherwise.  */
    812  1.1  christos 
    813  1.1  christos static int
    814  1.1  christos update_wait_timeout (void)
    815  1.1  christos {
    816  1.1  christos   if (timer_list.first_timer != NULL)
    817  1.1  christos     {
    818  1.1  christos       using namespace std::chrono;
    819  1.1  christos       steady_clock::time_point time_now = steady_clock::now ();
    820  1.1  christos       struct timeval timeout;
    821  1.1  christos 
    822  1.1  christos       if (timer_list.first_timer->when < time_now)
    823  1.1  christos 	{
    824  1.1  christos 	  /* It expired already.  */
    825  1.1  christos 	  timeout.tv_sec = 0;
    826  1.1  christos 	  timeout.tv_usec = 0;
    827  1.1  christos 	}
    828  1.1  christos       else
    829  1.1  christos 	{
    830  1.1  christos 	  steady_clock::duration d = timer_list.first_timer->when - time_now;
    831  1.1  christos 	  timeout = duration_cast_timeval (d);
    832  1.1  christos 	}
    833  1.1  christos 
    834  1.1  christos       /* Update the timeout for select/ poll.  */
    835  1.1  christos       if (use_poll)
    836  1.1  christos 	{
    837  1.1  christos #ifdef HAVE_POLL
    838  1.1  christos 	  gdb_notifier.poll_timeout = timeout.tv_sec * 1000;
    839  1.1  christos #else
    840  1.1  christos 	  internal_error (__FILE__, __LINE__,
    841  1.1  christos 			  _("use_poll without HAVE_POLL"));
    842  1.1  christos #endif /* HAVE_POLL */
    843  1.1  christos 	}
    844  1.1  christos       else
    845  1.1  christos 	{
    846  1.1  christos 	  gdb_notifier.select_timeout.tv_sec = timeout.tv_sec;
    847  1.1  christos 	  gdb_notifier.select_timeout.tv_usec = timeout.tv_usec;
    848  1.1  christos 	}
    849  1.1  christos       gdb_notifier.timeout_valid = 1;
    850  1.1  christos 
    851  1.1  christos       if (timer_list.first_timer->when < time_now)
    852  1.1  christos 	return 1;
    853  1.1  christos     }
    854  1.1  christos   else
    855  1.1  christos     gdb_notifier.timeout_valid = 0;
    856  1.1  christos 
    857  1.1  christos   return 0;
    858  1.1  christos }
    859  1.1  christos 
    860  1.1  christos /* Check whether a timer in the timers queue is ready.  If a timer is
    861  1.1  christos    ready, call its handler and return.  Update the timeout for the
    862  1.1  christos    select() or poll() as well.  Return 1 if an event was handled,
    863  1.1  christos    otherwise returns 0.*/
    864  1.1  christos 
    865  1.1  christos static int
    866  1.1  christos poll_timers (void)
    867  1.1  christos {
    868  1.1  christos   if (update_wait_timeout ())
    869  1.1  christos     {
    870  1.1  christos       struct gdb_timer *timer_ptr = timer_list.first_timer;
    871  1.1  christos       timer_handler_func *proc = timer_ptr->proc;
    872  1.1  christos       gdb_client_data client_data = timer_ptr->client_data;
    873  1.1  christos 
    874  1.1  christos       /* Get rid of the timer from the beginning of the list.  */
    875  1.1  christos       timer_list.first_timer = timer_ptr->next;
    876  1.1  christos 
    877  1.1  christos       /* Delete the timer before calling the callback, not after, in
    878  1.1  christos 	 case the callback itself decides to try deleting the timer
    879  1.1  christos 	 too.  */
    880  1.1  christos       delete timer_ptr;
    881  1.1  christos 
    882  1.1  christos       /* Call the procedure associated with that timer.  */
    883  1.1  christos       (proc) (client_data);
    884  1.1  christos 
    885  1.1  christos       return 1;
    886  1.1  christos     }
    887                
    888                  return 0;
    889                }
    890