gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gdb / mingw-hdep.c
index 79b23dc2556b65669676678069298d81cb0a69c0..43d99747659e98b6ef9cb6d842737f2454d1b767 100644 (file)
@@ -1,13 +1,12 @@
 /* Host support routines for MinGW, for GDB, the GNU debugger.
 
-   Copyright (C) 2006
-   Free Software Foundation, Inc.
+   Copyright (C) 2006-2020 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
+#include "main.h"
 #include "serial.h"
+#include "gdbsupport/event-loop.h"
 
-#include "gdb_assert.h"
-#include "gdb_select.h"
-#include "gdb_string.h"
+#include "gdbsupport/gdb_select.h"
 
 #include <windows.h>
 
-/* The strerror() function can return NULL for errno values that are
-   out of range.  Provide a "safe" version that always returns a
-   printable string.
-
-   The Windows runtime implementation of strerror never returns NULL,
-   but does return a useless string for anything above sys_nerr;
-   unfortunately this includes all socket-related error codes.
-   This replacement tries to find a system-provided error message.  */
+/* Return an absolute file name of the running GDB, if possible, or
+   ARGV0 if not.  The return value is in malloc'ed storage.  */
 
 char *
-safe_strerror (int errnum)
+windows_get_absolute_argv0 (const char *argv0)
 {
-  static char *buffer;
-  int len;
-
-  if (errnum >= 0 && errnum < sys_nerr)
-    return strerror (errnum);
-
-  if (buffer)
-    {
-      LocalFree (buffer);
-      buffer = NULL;
-    }
-
-  if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
-                    | FORMAT_MESSAGE_FROM_SYSTEM,
-                    NULL, errnum,
-                    MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
-                    (LPTSTR) &buffer, 0, NULL) == 0)
-    {
-      static char buf[32];
-      xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum);
-      return buf;
-    }
-
-  /* Windows error messages end with a period and a CR-LF; strip that
-     out.  */
-  len = strlen (buffer);
-  if (len > 3 && strcmp (buffer + len - 3, ".\r\n") == 0)
-    buffer[len - 3] = '\0';
+  char full_name[PATH_MAX];
 
-  return buffer;
+  if (GetModuleFileName (NULL, full_name, PATH_MAX))
+    return xstrdup (full_name);
+  return xstrdup (argv0);
 }
 
 /* Wrapper for select.  On Windows systems, where the select interface
@@ -89,12 +55,29 @@ gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
   HANDLE h;
   DWORD event;
   DWORD num_handles;
+  /* SCBS contains serial control objects corresponding to file
+     descriptors in READFDS and WRITEFDS.  */
+  struct serial *scbs[MAXIMUM_WAIT_OBJECTS];
+  /* The number of valid entries in SCBS.  */
+  size_t num_scbs;
   int fd;
   int num_ready;
-  int indx;
+  size_t indx;
+
+  if (n == 0)
+    {
+      /* The MS API says that the first argument to
+        WaitForMultipleObjects cannot be zero.  That's why we just
+        use a regular Sleep here.  */
+      if (timeout != NULL)
+       Sleep (timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
+
+      return 0;
+    }
 
   num_ready = 0;
   num_handles = 0;
+  num_scbs = 0;
   for (fd = 0; fd < n; ++fd)
     {
       HANDLE read = NULL, except = NULL;
@@ -105,17 +88,19 @@ gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
         if something starts using it.  */
       gdb_assert (!writefds || !FD_ISSET (fd, writefds));
 
-      if (!FD_ISSET (fd, readfds)
-         && !FD_ISSET (fd, exceptfds))
+      if ((!readfds || !FD_ISSET (fd, readfds))
+         && (!exceptfds || !FD_ISSET (fd, exceptfds)))
        continue;
-      h = (HANDLE) _get_osfhandle (fd);
 
       scb = serial_for_fd (fd);
       if (scb)
-       serial_wait_handle (scb, &read, &except);
+       {
+         serial_wait_handle (scb, &read, &except);
+         scbs[num_scbs++] = scb;
+       }
 
       if (read == NULL)
-       read = h;
+       read = (HANDLE) _get_osfhandle (fd);
       if (except == NULL)
        {
          if (!never_handle)
@@ -124,26 +109,20 @@ gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
          except = never_handle;
        }
 
-      if (FD_ISSET (fd, readfds))
+      if (readfds && FD_ISSET (fd, readfds))
        {
          gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
          handles[num_handles++] = read;
        }
 
-      if (FD_ISSET (fd, exceptfds))
+      if (exceptfds && FD_ISSET (fd, exceptfds))
        {
          gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
          handles[num_handles++] = except;
        }
     }
-  /* If we don't need to wait for any handles, we are done.  */
-  if (!num_handles)
-    {
-      if (timeout)
-       Sleep (timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
 
-      return 0;
-    }
+  gdb_assert (num_handles <= MAXIMUM_WAIT_OBJECTS);
 
   event = WaitForMultipleObjects (num_handles,
                                  handles,
@@ -157,6 +136,9 @@ gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
      mutexes, that should never occur.  */
   gdb_assert (!(WAIT_ABANDONED_0 <= event
                && event < WAIT_ABANDONED_0 + num_handles));
+  /* We no longer need the helper threads to check for activity.  */
+  for (indx = 0; indx < num_scbs; ++indx)
+    serial_done_wait_handle (scbs[indx]);
   if (event == WAIT_FAILED)
     return -1;
   if (event == WAIT_TIMEOUT)
@@ -168,7 +150,11 @@ gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
     {
       HANDLE fd_h;
 
-      if (FD_ISSET (fd, readfds))
+      if ((!readfds || !FD_ISSET (fd, readfds))
+         && (!exceptfds || !FD_ISSET (fd, exceptfds)))
+       continue;
+
+      if (readfds && FD_ISSET (fd, readfds))
        {
          fd_h = handles[indx++];
          /* This handle might be ready, even though it wasn't the handle
@@ -179,7 +165,7 @@ gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
            num_ready++;
        }
 
-      if (FD_ISSET (fd, exceptfds))
+      if (exceptfds && FD_ISSET (fd, exceptfds))
        {
          fd_h = handles[indx++];
          /* This handle might be ready, even though it wasn't the handle
@@ -193,3 +179,195 @@ gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
 
   return num_ready;
 }
+
+/* Map COLOR's RGB triplet, with 8 bits per component, into 16 Windows
+   console colors, where each component has just 1 bit, plus a single
+   intensity bit which affects all 3 components.  */
+static int
+rgb_to_16colors (const ui_file_style::color &color)
+{
+  uint8_t rgb[3];
+  color.get_rgb (rgb);
+
+  int retval = 0;
+  for (int i = 0; i < 3; i++)
+    {
+      /* Subdivide 256 possible values of each RGB component into 3
+        regions: no color, normal color, bright color.  256 / 3 = 85,
+        but ui-style.c follows xterm and uses 92 for R and G
+        components of the bright-blue color, so we bias the divisor a
+        bit to have the bright colors between 9 and 15 identical to
+        what ui-style.c expects.  */
+      int bits = rgb[i] / 93;
+      retval |= ((bits > 0) << (2 - i)) | ((bits > 1) << 3);
+    }
+
+  return retval;
+}
+
+/* Zero if not yet initialized, 1 if stdout is a console device, else -1.  */
+static int mingw_console_initialized;
+
+/* Handle to stdout . */
+static HANDLE hstdout = INVALID_HANDLE_VALUE;
+
+/* Text attribute to use for normal text (the "none" pseudo-color).  */
+static SHORT  norm_attr;
+
+/* The most recently applied style.  */
+static ui_file_style last_style;
+
+/* Alternative for the libc 'fputs' which handles embedded SGR
+   sequences in support of styling.  */
+
+int
+gdb_console_fputs (const char *linebuf, FILE *fstream)
+{
+  if (!mingw_console_initialized)
+    {
+      hstdout = (HANDLE)_get_osfhandle (fileno (fstream));
+      DWORD cmode;
+      CONSOLE_SCREEN_BUFFER_INFO csbi;
+
+      if (hstdout != INVALID_HANDLE_VALUE
+         && GetConsoleMode (hstdout, &cmode) != 0
+         && GetConsoleScreenBufferInfo (hstdout, &csbi))
+       {
+         norm_attr = csbi.wAttributes;
+         mingw_console_initialized = 1;
+       }
+      else if (hstdout != INVALID_HANDLE_VALUE)
+       mingw_console_initialized = -1; /* valid, but not a console device */
+    }
+  /* If our stdout is not a console device, let the default 'fputs'
+     handle the task. */
+  if (mingw_console_initialized <= 0)
+    return 0;
+
+  /* Mapping between 8 ANSI colors and Windows console attributes.  */
+  static int fg_color[] = {
+    0,                                 /* black */
+    FOREGROUND_RED,                    /* red */
+    FOREGROUND_GREEN,                  /* green */
+    FOREGROUND_GREEN | FOREGROUND_RED, /* yellow */
+    FOREGROUND_BLUE,                   /* blue */
+    FOREGROUND_BLUE | FOREGROUND_RED,  /* magenta */
+    FOREGROUND_BLUE | FOREGROUND_GREEN, /* cyan */
+    FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* gray */
+  };
+  static int bg_color[] = {
+    0,                                 /* black */
+    BACKGROUND_RED,                    /* red */
+    BACKGROUND_GREEN,                  /* green */
+    BACKGROUND_GREEN | BACKGROUND_RED, /* yellow */
+    BACKGROUND_BLUE,                   /* blue */
+    BACKGROUND_BLUE | BACKGROUND_RED,  /* magenta */
+    BACKGROUND_BLUE | BACKGROUND_GREEN, /* cyan */
+    BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE /* gray */
+  };
+
+  ui_file_style style = last_style;
+  unsigned char c;
+  size_t n_read;
+
+  for ( ; (c = *linebuf) != 0; linebuf += n_read)
+    {
+      if (c == '\033')
+       {
+         fflush (fstream);
+         bool parsed = style.parse (linebuf, &n_read);
+         if (n_read <= 0)      /* should never happen */
+           n_read = 1;
+         if (!parsed)
+           {
+             /* This means we silently swallow SGR sequences we
+                cannot parse.  */
+             continue;
+           }
+         /* Colors.  */
+         const ui_file_style::color &fg = style.get_foreground ();
+         const ui_file_style::color &bg = style.get_background ();
+         int fgcolor, bgcolor, bright, inverse;
+         if (fg.is_none ())
+           fgcolor = norm_attr & 15;
+         else if (fg.is_basic ())
+           fgcolor = fg_color[fg.get_value () & 15];
+         else
+           fgcolor = rgb_to_16colors (fg);
+         if (bg.is_none ())
+           bgcolor = norm_attr & (15 << 4);
+         else if (bg.is_basic ())
+           bgcolor = bg_color[bg.get_value () & 15];
+         else
+           bgcolor = rgb_to_16colors (bg) << 4;
+
+         /* Intensity.  */
+         switch (style.get_intensity ())
+           {
+           case ui_file_style::NORMAL:
+           case ui_file_style::DIM:
+             bright = 0;
+             break;
+           case ui_file_style::BOLD:
+             bright = 1;
+             break;
+           default:
+             gdb_assert_not_reached ("invalid intensity");
+           }
+
+         /* Inverse video.  */
+         if (style.is_reverse ())
+           inverse = 1;
+         else
+           inverse = 0;
+
+         /* Construct the attribute.  */
+         if (inverse)
+           {
+             int t = fgcolor;
+             fgcolor = (bgcolor >> 4);
+             bgcolor = (t << 4);
+           }
+         if (bright)
+           fgcolor |= FOREGROUND_INTENSITY;
+
+         SHORT attr = (bgcolor & (15 << 4)) | (fgcolor & 15);
+
+         /* Apply the attribute.  */
+         SetConsoleTextAttribute (hstdout, attr);
+       }
+      else
+       {
+         /* When we are about to write newline, we need to clear to
+            EOL with the normal attribute, to avoid spilling the
+            colors to the next screen line.  We assume here that no
+            non-default attribute extends beyond the newline.  */
+         if (c == '\n')
+           {
+             DWORD nchars;
+             COORD start_pos;
+             DWORD written;
+             CONSOLE_SCREEN_BUFFER_INFO csbi;
+
+             fflush (fstream);
+             GetConsoleScreenBufferInfo (hstdout, &csbi);
+
+             if (csbi.wAttributes != norm_attr)
+               {
+                 start_pos = csbi.dwCursorPosition;
+                 nchars = csbi.dwSize.X - start_pos.X;
+
+                 FillConsoleOutputAttribute (hstdout, norm_attr, nchars,
+                                             start_pos, &written);
+                 FillConsoleOutputCharacter (hstdout, ' ', nchars,
+                                             start_pos, &written);
+               }
+           }
+         fputc (c, fstream);
+         n_read = 1;
+       }
+    }
+
+  last_style = style;
+  return 1;
+}
This page took 0.027307 seconds and 4 git commands to generate.