2007-10-02 Mark Mitchell <mark@codesourcery.com>
[platform/upstream/binutils.git] / gdb / mingw-hdep.c
1 /* Host support routines for MinGW, for GDB, the GNU debugger.
2
3    Copyright (C) 2006, 2007 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "serial.h"
22
23 #include "gdb_assert.h"
24 #include "gdb_select.h"
25 #include "gdb_string.h"
26
27 #include <windows.h>
28
29 /* The strerror() function can return NULL for errno values that are
30    out of range.  Provide a "safe" version that always returns a
31    printable string.
32
33    The Windows runtime implementation of strerror never returns NULL,
34    but does return a useless string for anything above sys_nerr;
35    unfortunately this includes all socket-related error codes.
36    This replacement tries to find a system-provided error message.  */
37
38 char *
39 safe_strerror (int errnum)
40 {
41   static char *buffer;
42   int len;
43
44   if (errnum >= 0 && errnum < sys_nerr)
45     return strerror (errnum);
46
47   if (buffer)
48     {
49       LocalFree (buffer);
50       buffer = NULL;
51     }
52
53   if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
54                      | FORMAT_MESSAGE_FROM_SYSTEM,
55                      NULL, errnum,
56                      MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
57                      (LPTSTR) &buffer, 0, NULL) == 0)
58     {
59       static char buf[32];
60       xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum);
61       return buf;
62     }
63
64   /* Windows error messages end with a period and a CR-LF; strip that
65      out.  */
66   len = strlen (buffer);
67   if (len > 3 && strcmp (buffer + len - 3, ".\r\n") == 0)
68     buffer[len - 3] = '\0';
69
70   return buffer;
71 }
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
77    The arguments to this function are the same as the traditional
78    arguments to select on POSIX platforms.  */
79
80 int
81 gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
82             struct timeval *timeout)
83 {
84   static HANDLE never_handle;
85   HANDLE handles[MAXIMUM_WAIT_OBJECTS];
86   HANDLE h;
87   DWORD event;
88   DWORD num_handles;
89   /* SCBS contains serial control objects corresponding to file
90      descriptors in READFDS and WRITEFDS.  */
91   struct serial *scbs[MAXIMUM_WAIT_OBJECTS];
92   /* The number of valid entries in SCBS.  */
93   size_t num_scbs;
94   int fd;
95   int num_ready;
96   size_t indx;
97
98   num_ready = 0;
99   num_handles = 0;
100   num_scbs = 0;
101   for (fd = 0; fd < n; ++fd)
102     {
103       HANDLE read = NULL, except = NULL;
104       struct serial *scb;
105
106       /* There is no support yet for WRITEFDS.  At present, this isn't
107          used by GDB -- but we do not want to silently ignore WRITEFDS
108          if something starts using it.  */
109       gdb_assert (!writefds || !FD_ISSET (fd, writefds));
110
111       if ((!readfds || !FD_ISSET (fd, readfds))
112           && (!exceptfds || !FD_ISSET (fd, exceptfds)))
113         continue;
114
115       scb = serial_for_fd (fd);
116       if (scb)
117         {
118           serial_wait_handle (scb, &read, &except);
119           scbs[num_scbs++] = scb;
120         }
121
122       if (read == NULL)
123         read = (HANDLE) _get_osfhandle (fd);
124       if (except == NULL)
125         {
126           if (!never_handle)
127             never_handle = CreateEvent (0, FALSE, FALSE, 0);
128
129           except = never_handle;
130         }
131
132       if (readfds && FD_ISSET (fd, readfds))
133         {
134           gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
135           handles[num_handles++] = read;
136         }
137
138       if (exceptfds && FD_ISSET (fd, exceptfds))
139         {
140           gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
141           handles[num_handles++] = except;
142         }
143     }
144   /* If we don't need to wait for any handles, we are done.  */
145   if (!num_handles)
146     {
147       if (timeout)
148         Sleep (timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
149
150       return 0;
151     }
152
153   event = WaitForMultipleObjects (num_handles,
154                                   handles,
155                                   FALSE,
156                                   timeout
157                                   ? (timeout->tv_sec * 1000
158                                      + timeout->tv_usec / 1000)
159                                   : INFINITE);
160   /* EVENT can only be a value in the WAIT_ABANDONED_0 range if the
161      HANDLES included an abandoned mutex.  Since GDB doesn't use
162      mutexes, that should never occur.  */
163   gdb_assert (!(WAIT_ABANDONED_0 <= event
164                 && event < WAIT_ABANDONED_0 + num_handles));
165   /* We no longer need the helper threads to check for activity.  */
166   for (indx = 0; indx < num_scbs; ++indx)
167     serial_done_wait_handle (scbs[indx]);
168   if (event == WAIT_FAILED)
169     return -1;
170   if (event == WAIT_TIMEOUT)
171     return 0;
172   /* Run through the READFDS, clearing bits corresponding to descriptors
173      for which input is unavailable.  */
174   h = handles[event - WAIT_OBJECT_0];
175   for (fd = 0, indx = 0; fd < n; ++fd)
176     {
177       HANDLE fd_h;
178
179       if ((!readfds || !FD_ISSET (fd, readfds))
180           && (!exceptfds || !FD_ISSET (fd, exceptfds)))
181         continue;
182
183       if (readfds && FD_ISSET (fd, readfds))
184         {
185           fd_h = handles[indx++];
186           /* This handle might be ready, even though it wasn't the handle
187              returned by WaitForMultipleObjects.  */
188           if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
189             FD_CLR (fd, readfds);
190           else
191             num_ready++;
192         }
193
194       if (exceptfds && FD_ISSET (fd, exceptfds))
195         {
196           fd_h = handles[indx++];
197           /* This handle might be ready, even though it wasn't the handle
198              returned by WaitForMultipleObjects.  */
199           if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
200             FD_CLR (fd, exceptfds);
201           else
202             num_ready++;
203         }
204     }
205
206   return num_ready;
207 }