1 /* Host support routines for MinGW, for GDB, the GNU debugger.
3 Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
23 #include "gdb_assert.h"
24 #include "gdb_select.h"
25 #include "gdb_string.h"
29 /* The strerror() function can return NULL for errno values that are
30 out of range. Provide a "safe" version that always returns a
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. */
39 safe_strerror (int errnum)
44 if (errnum >= 0 && errnum < sys_nerr)
45 return strerror (errnum);
53 if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
54 | FORMAT_MESSAGE_FROM_SYSTEM,
56 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
57 (LPTSTR) &buffer, 0, NULL) == 0)
60 xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum);
64 /* Windows error messages end with a period and a CR-LF; strip that
66 len = strlen (buffer);
67 if (len > 3 && strcmp (buffer + len - 3, ".\r\n") == 0)
68 buffer[len - 3] = '\0';
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.
77 The arguments to this function are the same as the traditional
78 arguments to select on POSIX platforms. */
81 gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
82 struct timeval *timeout)
84 static HANDLE never_handle;
85 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
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. */
101 for (fd = 0; fd < n; ++fd)
103 HANDLE read = NULL, except = NULL;
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));
111 if ((!readfds || !FD_ISSET (fd, readfds))
112 && (!exceptfds || !FD_ISSET (fd, exceptfds)))
115 scb = serial_for_fd (fd);
118 serial_wait_handle (scb, &read, &except);
119 scbs[num_scbs++] = scb;
123 read = (HANDLE) _get_osfhandle (fd);
127 never_handle = CreateEvent (0, FALSE, FALSE, 0);
129 except = never_handle;
132 if (readfds && FD_ISSET (fd, readfds))
134 gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
135 handles[num_handles++] = read;
138 if (exceptfds && FD_ISSET (fd, exceptfds))
140 gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
141 handles[num_handles++] = except;
144 /* If we don't need to wait for any handles, we are done. */
148 Sleep (timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
153 event = WaitForMultipleObjects (num_handles,
157 ? (timeout->tv_sec * 1000
158 + timeout->tv_usec / 1000)
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)
170 if (event == WAIT_TIMEOUT)
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)
179 if ((!readfds || !FD_ISSET (fd, readfds))
180 && (!exceptfds || !FD_ISSET (fd, exceptfds)))
183 if (readfds && FD_ISSET (fd, readfds))
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);
194 if (exceptfds && FD_ISSET (fd, exceptfds))
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);