gdb/mingw-hdep.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Host support routines for MinGW, for GDB, the GNU debugger.

  2.    Copyright (C) 2006-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. #include "defs.h"
  15. #include "main.h"
  16. #include "serial.h"
  17. #include "event-loop.h"

  18. #include "gdb_select.h"
  19. #include "readline/readline.h"

  20. #include <windows.h>

  21. /* This event is signalled whenever an asynchronous SIGINT handler
  22.    needs to perform an action in the main thread.  */
  23. static HANDLE sigint_event;

  24. /* When SIGINT_EVENT is signalled, gdb_select will call this
  25.    function.  */
  26. struct async_signal_handler *sigint_handler;

  27. /* The strerror() function can return NULL for errno values that are
  28.    out of range.  Provide a "safe" version that always returns a
  29.    printable string.

  30.    The Windows runtime implementation of strerror never returns NULL,
  31.    but does return a useless string for anything above sys_nerr;
  32.    unfortunately this includes all socket-related error codes.
  33.    This replacement tries to find a system-provided error message.  */

  34. char *
  35. safe_strerror (int errnum)
  36. {
  37.   static char *buffer;
  38.   int len;

  39.   if (errnum >= 0 && errnum < sys_nerr)
  40.     return strerror (errnum);

  41.   if (buffer)
  42.     {
  43.       LocalFree (buffer);
  44.       buffer = NULL;
  45.     }

  46.   if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
  47.                      | FORMAT_MESSAGE_FROM_SYSTEM,
  48.                      NULL, errnum,
  49.                      MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
  50.                      (LPTSTR) &buffer, 0, NULL) == 0)
  51.     {
  52.       static char buf[32];
  53.       xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum);
  54.       return buf;
  55.     }

  56.   /* Windows error messages end with a period and a CR-LF; strip that
  57.      out.  */
  58.   len = strlen (buffer);
  59.   if (len > 3 && strcmp (buffer + len - 3, ".\r\n") == 0)
  60.     buffer[len - 3] = '\0';

  61.   return buffer;
  62. }

  63. /* Return an absolute file name of the running GDB, if possible, or
  64.    ARGV0 if not.  The return value is in malloc'ed storage.  */

  65. char *
  66. windows_get_absolute_argv0 (const char *argv0)
  67. {
  68.   char full_name[PATH_MAX];

  69.   if (GetModuleFileName (NULL, full_name, PATH_MAX))
  70.     return xstrdup (full_name);
  71.   return xstrdup (argv0);
  72. }

  73. /* Wrapper for select.  On Windows systems, where the select interface
  74.    only works for sockets, this uses the GDB serial abstraction to
  75.    handle sockets, consoles, pipes, and serial ports.

  76.    The arguments to this function are the same as the traditional
  77.    arguments to select on POSIX platforms.  */

  78. int
  79. gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  80.             struct timeval *timeout)
  81. {
  82.   static HANDLE never_handle;
  83.   HANDLE handles[MAXIMUM_WAIT_OBJECTS];
  84.   HANDLE h;
  85.   DWORD event;
  86.   DWORD num_handles;
  87.   /* SCBS contains serial control objects corresponding to file
  88.      descriptors in READFDS and WRITEFDS.  */
  89.   struct serial *scbs[MAXIMUM_WAIT_OBJECTS];
  90.   /* The number of valid entries in SCBS.  */
  91.   size_t num_scbs;
  92.   int fd;
  93.   int num_ready;
  94.   size_t indx;

  95.   num_ready = 0;
  96.   num_handles = 0;
  97.   num_scbs = 0;
  98.   for (fd = 0; fd < n; ++fd)
  99.     {
  100.       HANDLE read = NULL, except = NULL;
  101.       struct serial *scb;

  102.       /* There is no support yet for WRITEFDS.  At present, this isn't
  103.          used by GDB -- but we do not want to silently ignore WRITEFDS
  104.          if something starts using it.  */
  105.       gdb_assert (!writefds || !FD_ISSET (fd, writefds));

  106.       if ((!readfds || !FD_ISSET (fd, readfds))
  107.           && (!exceptfds || !FD_ISSET (fd, exceptfds)))
  108.         continue;

  109.       scb = serial_for_fd (fd);
  110.       if (scb)
  111.         {
  112.           serial_wait_handle (scb, &read, &except);
  113.           scbs[num_scbs++] = scb;
  114.         }

  115.       if (read == NULL)
  116.         read = (HANDLE) _get_osfhandle (fd);
  117.       if (except == NULL)
  118.         {
  119.           if (!never_handle)
  120.             never_handle = CreateEvent (0, FALSE, FALSE, 0);

  121.           except = never_handle;
  122.         }

  123.       if (readfds && FD_ISSET (fd, readfds))
  124.         {
  125.           gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
  126.           handles[num_handles++] = read;
  127.         }

  128.       if (exceptfds && FD_ISSET (fd, exceptfds))
  129.         {
  130.           gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
  131.           handles[num_handles++] = except;
  132.         }
  133.     }

  134.   gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
  135.   handles[num_handles++] = sigint_event;

  136.   event = WaitForMultipleObjects (num_handles,
  137.                                   handles,
  138.                                   FALSE,
  139.                                   timeout
  140.                                   ? (timeout->tv_sec * 1000
  141.                                      + timeout->tv_usec / 1000)
  142.                                   : INFINITE);
  143.   /* EVENT can only be a value in the WAIT_ABANDONED_0 range if the
  144.      HANDLES included an abandoned mutex.  Since GDB doesn't use
  145.      mutexes, that should never occur.  */
  146.   gdb_assert (!(WAIT_ABANDONED_0 <= event
  147.                 && event < WAIT_ABANDONED_0 + num_handles));
  148.   /* We no longer need the helper threads to check for activity.  */
  149.   for (indx = 0; indx < num_scbs; ++indx)
  150.     serial_done_wait_handle (scbs[indx]);
  151.   if (event == WAIT_FAILED)
  152.     return -1;
  153.   if (event == WAIT_TIMEOUT)
  154.     return 0;
  155.   /* Run through the READFDS, clearing bits corresponding to descriptors
  156.      for which input is unavailable.  */
  157.   h = handles[event - WAIT_OBJECT_0];
  158.   for (fd = 0, indx = 0; fd < n; ++fd)
  159.     {
  160.       HANDLE fd_h;

  161.       if ((!readfds || !FD_ISSET (fd, readfds))
  162.           && (!exceptfds || !FD_ISSET (fd, exceptfds)))
  163.         continue;

  164.       if (readfds && FD_ISSET (fd, readfds))
  165.         {
  166.           fd_h = handles[indx++];
  167.           /* This handle might be ready, even though it wasn't the handle
  168.              returned by WaitForMultipleObjects.  */
  169.           if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
  170.             FD_CLR (fd, readfds);
  171.           else
  172.             num_ready++;
  173.         }

  174.       if (exceptfds && FD_ISSET (fd, exceptfds))
  175.         {
  176.           fd_h = handles[indx++];
  177.           /* This handle might be ready, even though it wasn't the handle
  178.              returned by WaitForMultipleObjects.  */
  179.           if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
  180.             FD_CLR (fd, exceptfds);
  181.           else
  182.             num_ready++;
  183.         }
  184.     }

  185.   /* With multi-threaded SIGINT handling, there is a race between the
  186.      readline signal handler and GDB.  It may still be in
  187.      rl_prep_terminal in another thread.  Do not return until it is
  188.      done; we can check the state here because we never longjmp from
  189.      signal handlers on Windows.  */
  190.   while (RL_ISSTATE (RL_STATE_SIGHANDLER))
  191.     Sleep (1);

  192.   if (h == sigint_event
  193.       || WaitForSingleObject (sigint_event, 0) == WAIT_OBJECT_0)
  194.     {
  195.       if (sigint_handler != NULL)
  196.         call_async_signal_handler (sigint_handler);

  197.       if (num_ready == 0)
  198.         {
  199.           errno = EINTR;
  200.           return -1;
  201.         }
  202.     }

  203.   return num_ready;
  204. }

  205. /* Wrapper for the body of signal handlers.  On Windows systems, a
  206.    SIGINT handler runs in its own thread.  We can't longjmp from
  207.    there, and we shouldn't even prompt the user.  Delay HANDLER
  208.    until the main thread is next in gdb_select.  */

  209. void
  210. gdb_call_async_signal_handler (struct async_signal_handler *handler,
  211.                                int immediate_p)
  212. {
  213.   if (immediate_p)
  214.     sigint_handler = handler;
  215.   else
  216.     {
  217.       mark_async_signal_handler (handler);
  218.       sigint_handler = NULL;
  219.     }
  220.   SetEvent (sigint_event);
  221. }

  222. /* -Wmissing-prototypes */
  223. extern initialize_file_ftype _initialize_mingw_hdep;

  224. void
  225. _initialize_mingw_hdep (void)
  226. {
  227.   sigint_event = CreateEvent (0, FALSE, FALSE, 0);
  228. }