GDB (API)
/home/stan/gdb/src/gdb/mingw-hdep.c
Go to the documentation of this file.
00001 /* Host support routines for MinGW, for GDB, the GNU debugger.
00002 
00003    Copyright (C) 2006-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "main.h"
00022 #include "serial.h"
00023 #include "event-loop.h"
00024 
00025 #include "gdb_assert.h"
00026 #include "gdb_select.h"
00027 #include "gdb_string.h"
00028 #include "readline/readline.h"
00029 
00030 #include <windows.h>
00031 
00032 /* This event is signalled whenever an asynchronous SIGINT handler
00033    needs to perform an action in the main thread.  */
00034 static HANDLE sigint_event;
00035 
00036 /* When SIGINT_EVENT is signalled, gdb_select will call this
00037    function.  */
00038 struct async_signal_handler *sigint_handler;
00039 
00040 /* The strerror() function can return NULL for errno values that are
00041    out of range.  Provide a "safe" version that always returns a
00042    printable string.
00043 
00044    The Windows runtime implementation of strerror never returns NULL,
00045    but does return a useless string for anything above sys_nerr;
00046    unfortunately this includes all socket-related error codes.
00047    This replacement tries to find a system-provided error message.  */
00048 
00049 char *
00050 safe_strerror (int errnum)
00051 {
00052   static char *buffer;
00053   int len;
00054 
00055   if (errnum >= 0 && errnum < sys_nerr)
00056     return strerror (errnum);
00057 
00058   if (buffer)
00059     {
00060       LocalFree (buffer);
00061       buffer = NULL;
00062     }
00063 
00064   if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
00065                      | FORMAT_MESSAGE_FROM_SYSTEM,
00066                      NULL, errnum,
00067                      MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
00068                      (LPTSTR) &buffer, 0, NULL) == 0)
00069     {
00070       static char buf[32];
00071       xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum);
00072       return buf;
00073     }
00074 
00075   /* Windows error messages end with a period and a CR-LF; strip that
00076      out.  */
00077   len = strlen (buffer);
00078   if (len > 3 && strcmp (buffer + len - 3, ".\r\n") == 0)
00079     buffer[len - 3] = '\0';
00080 
00081   return buffer;
00082 }
00083 
00084 /* Return an absolute file name of the running GDB, if possible, or
00085    ARGV0 if not.  The return value is in malloc'ed storage.  */
00086 
00087 char *
00088 windows_get_absolute_argv0 (const char *argv0)
00089 {
00090   char full_name[PATH_MAX];
00091 
00092   if (GetModuleFileName (NULL, full_name, PATH_MAX))
00093     return xstrdup (full_name);
00094   return xstrdup (argv0);
00095 }
00096 
00097 /* Wrapper for select.  On Windows systems, where the select interface
00098    only works for sockets, this uses the GDB serial abstraction to
00099    handle sockets, consoles, pipes, and serial ports.
00100 
00101    The arguments to this function are the same as the traditional
00102    arguments to select on POSIX platforms.  */
00103 
00104 int
00105 gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
00106             struct timeval *timeout)
00107 {
00108   static HANDLE never_handle;
00109   HANDLE handles[MAXIMUM_WAIT_OBJECTS];
00110   HANDLE h;
00111   DWORD event;
00112   DWORD num_handles;
00113   /* SCBS contains serial control objects corresponding to file
00114      descriptors in READFDS and WRITEFDS.  */
00115   struct serial *scbs[MAXIMUM_WAIT_OBJECTS];
00116   /* The number of valid entries in SCBS.  */
00117   size_t num_scbs;
00118   int fd;
00119   int num_ready;
00120   size_t indx;
00121 
00122   num_ready = 0;
00123   num_handles = 0;
00124   num_scbs = 0;
00125   for (fd = 0; fd < n; ++fd)
00126     {
00127       HANDLE read = NULL, except = NULL;
00128       struct serial *scb;
00129 
00130       /* There is no support yet for WRITEFDS.  At present, this isn't
00131          used by GDB -- but we do not want to silently ignore WRITEFDS
00132          if something starts using it.  */
00133       gdb_assert (!writefds || !FD_ISSET (fd, writefds));
00134 
00135       if ((!readfds || !FD_ISSET (fd, readfds))
00136           && (!exceptfds || !FD_ISSET (fd, exceptfds)))
00137         continue;
00138 
00139       scb = serial_for_fd (fd);
00140       if (scb)
00141         {
00142           serial_wait_handle (scb, &read, &except);
00143           scbs[num_scbs++] = scb;
00144         }
00145 
00146       if (read == NULL)
00147         read = (HANDLE) _get_osfhandle (fd);
00148       if (except == NULL)
00149         {
00150           if (!never_handle)
00151             never_handle = CreateEvent (0, FALSE, FALSE, 0);
00152 
00153           except = never_handle;
00154         }
00155 
00156       if (readfds && FD_ISSET (fd, readfds))
00157         {
00158           gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
00159           handles[num_handles++] = read;
00160         }
00161 
00162       if (exceptfds && FD_ISSET (fd, exceptfds))
00163         {
00164           gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
00165           handles[num_handles++] = except;
00166         }
00167     }
00168 
00169   gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
00170   handles[num_handles++] = sigint_event;
00171 
00172   event = WaitForMultipleObjects (num_handles,
00173                                   handles,
00174                                   FALSE,
00175                                   timeout
00176                                   ? (timeout->tv_sec * 1000
00177                                      + timeout->tv_usec / 1000)
00178                                   : INFINITE);
00179   /* EVENT can only be a value in the WAIT_ABANDONED_0 range if the
00180      HANDLES included an abandoned mutex.  Since GDB doesn't use
00181      mutexes, that should never occur.  */
00182   gdb_assert (!(WAIT_ABANDONED_0 <= event
00183                 && event < WAIT_ABANDONED_0 + num_handles));
00184   /* We no longer need the helper threads to check for activity.  */
00185   for (indx = 0; indx < num_scbs; ++indx)
00186     serial_done_wait_handle (scbs[indx]);
00187   if (event == WAIT_FAILED)
00188     return -1;
00189   if (event == WAIT_TIMEOUT)
00190     return 0;
00191   /* Run through the READFDS, clearing bits corresponding to descriptors
00192      for which input is unavailable.  */
00193   h = handles[event - WAIT_OBJECT_0];
00194   for (fd = 0, indx = 0; fd < n; ++fd)
00195     {
00196       HANDLE fd_h;
00197 
00198       if ((!readfds || !FD_ISSET (fd, readfds))
00199           && (!exceptfds || !FD_ISSET (fd, exceptfds)))
00200         continue;
00201 
00202       if (readfds && FD_ISSET (fd, readfds))
00203         {
00204           fd_h = handles[indx++];
00205           /* This handle might be ready, even though it wasn't the handle
00206              returned by WaitForMultipleObjects.  */
00207           if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
00208             FD_CLR (fd, readfds);
00209           else
00210             num_ready++;
00211         }
00212 
00213       if (exceptfds && FD_ISSET (fd, exceptfds))
00214         {
00215           fd_h = handles[indx++];
00216           /* This handle might be ready, even though it wasn't the handle
00217              returned by WaitForMultipleObjects.  */
00218           if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
00219             FD_CLR (fd, exceptfds);
00220           else
00221             num_ready++;
00222         }
00223     }
00224 
00225   /* With multi-threaded SIGINT handling, there is a race between the
00226      readline signal handler and GDB.  It may still be in
00227      rl_prep_terminal in another thread.  Do not return until it is
00228      done; we can check the state here because we never longjmp from
00229      signal handlers on Windows.  */
00230   while (RL_ISSTATE (RL_STATE_SIGHANDLER))
00231     Sleep (1);
00232 
00233   if (h == sigint_event
00234       || WaitForSingleObject (sigint_event, 0) == WAIT_OBJECT_0)
00235     {
00236       if (sigint_handler != NULL)
00237         call_async_signal_handler (sigint_handler);
00238 
00239       if (num_ready == 0)
00240         {
00241           errno = EINTR;
00242           return -1;
00243         }
00244     }
00245 
00246   return num_ready;
00247 }
00248 
00249 /* Wrapper for the body of signal handlers.  On Windows systems, a
00250    SIGINT handler runs in its own thread.  We can't longjmp from
00251    there, and we shouldn't even prompt the user.  Delay HANDLER
00252    until the main thread is next in gdb_select.  */
00253 
00254 void
00255 gdb_call_async_signal_handler (struct async_signal_handler *handler,
00256                                int immediate_p)
00257 {
00258   if (immediate_p)
00259     sigint_handler = handler;
00260   else
00261     {
00262       mark_async_signal_handler (handler);
00263       sigint_handler = NULL;
00264     }
00265   SetEvent (sigint_event);
00266 }
00267 
00268 /* -Wmissing-prototypes */
00269 extern initialize_file_ftype _initialize_mingw_hdep;
00270 
00271 void
00272 _initialize_mingw_hdep (void)
00273 {
00274   sigint_event = CreateEvent (0, FALSE, FALSE, 0);
00275 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines