1 /* Host support routines for MinGW, for GDB, the GNU debugger.
3 Copyright (C) 2006-2014 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 "event-loop.h"
25 #include "gdb_select.h"
27 #include "readline/readline.h"
31 /* This event is signalled whenever an asynchronous SIGINT handler
32 needs to perform an action in the main thread. */
33 static HANDLE sigint_event;
35 /* When SIGINT_EVENT is signalled, gdb_select will call this
37 struct async_signal_handler *sigint_handler;
39 /* The strerror() function can return NULL for errno values that are
40 out of range. Provide a "safe" version that always returns a
43 The Windows runtime implementation of strerror never returns NULL,
44 but does return a useless string for anything above sys_nerr;
45 unfortunately this includes all socket-related error codes.
46 This replacement tries to find a system-provided error message. */
49 safe_strerror (int errnum)
54 if (errnum >= 0 && errnum < sys_nerr)
55 return strerror (errnum);
63 if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
64 | FORMAT_MESSAGE_FROM_SYSTEM,
66 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
67 (LPTSTR) &buffer, 0, NULL) == 0)
70 xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum);
74 /* Windows error messages end with a period and a CR-LF; strip that
76 len = strlen (buffer);
77 if (len > 3 && strcmp (buffer + len - 3, ".\r\n") == 0)
78 buffer[len - 3] = '\0';
83 /* Return an absolute file name of the running GDB, if possible, or
84 ARGV0 if not. The return value is in malloc'ed storage. */
87 windows_get_absolute_argv0 (const char *argv0)
89 char full_name[PATH_MAX];
91 if (GetModuleFileName (NULL, full_name, PATH_MAX))
92 return xstrdup (full_name);
93 return xstrdup (argv0);
96 /* Wrapper for select. On Windows systems, where the select interface
97 only works for sockets, this uses the GDB serial abstraction to
98 handle sockets, consoles, pipes, and serial ports.
100 The arguments to this function are the same as the traditional
101 arguments to select on POSIX platforms. */
104 gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
105 struct timeval *timeout)
107 static HANDLE never_handle;
108 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
112 /* SCBS contains serial control objects corresponding to file
113 descriptors in READFDS and WRITEFDS. */
114 struct serial *scbs[MAXIMUM_WAIT_OBJECTS];
115 /* The number of valid entries in SCBS. */
124 for (fd = 0; fd < n; ++fd)
126 HANDLE read = NULL, except = NULL;
129 /* There is no support yet for WRITEFDS. At present, this isn't
130 used by GDB -- but we do not want to silently ignore WRITEFDS
131 if something starts using it. */
132 gdb_assert (!writefds || !FD_ISSET (fd, writefds));
134 if ((!readfds || !FD_ISSET (fd, readfds))
135 && (!exceptfds || !FD_ISSET (fd, exceptfds)))
138 scb = serial_for_fd (fd);
141 serial_wait_handle (scb, &read, &except);
142 scbs[num_scbs++] = scb;
146 read = (HANDLE) _get_osfhandle (fd);
150 never_handle = CreateEvent (0, FALSE, FALSE, 0);
152 except = never_handle;
155 if (readfds && FD_ISSET (fd, readfds))
157 gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
158 handles[num_handles++] = read;
161 if (exceptfds && FD_ISSET (fd, exceptfds))
163 gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
164 handles[num_handles++] = except;
168 gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
169 handles[num_handles++] = sigint_event;
171 event = WaitForMultipleObjects (num_handles,
175 ? (timeout->tv_sec * 1000
176 + timeout->tv_usec / 1000)
178 /* EVENT can only be a value in the WAIT_ABANDONED_0 range if the
179 HANDLES included an abandoned mutex. Since GDB doesn't use
180 mutexes, that should never occur. */
181 gdb_assert (!(WAIT_ABANDONED_0 <= event
182 && event < WAIT_ABANDONED_0 + num_handles));
183 /* We no longer need the helper threads to check for activity. */
184 for (indx = 0; indx < num_scbs; ++indx)
185 serial_done_wait_handle (scbs[indx]);
186 if (event == WAIT_FAILED)
188 if (event == WAIT_TIMEOUT)
190 /* Run through the READFDS, clearing bits corresponding to descriptors
191 for which input is unavailable. */
192 h = handles[event - WAIT_OBJECT_0];
193 for (fd = 0, indx = 0; fd < n; ++fd)
197 if ((!readfds || !FD_ISSET (fd, readfds))
198 && (!exceptfds || !FD_ISSET (fd, exceptfds)))
201 if (readfds && FD_ISSET (fd, readfds))
203 fd_h = handles[indx++];
204 /* This handle might be ready, even though it wasn't the handle
205 returned by WaitForMultipleObjects. */
206 if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
207 FD_CLR (fd, readfds);
212 if (exceptfds && FD_ISSET (fd, exceptfds))
214 fd_h = handles[indx++];
215 /* This handle might be ready, even though it wasn't the handle
216 returned by WaitForMultipleObjects. */
217 if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
218 FD_CLR (fd, exceptfds);
224 /* With multi-threaded SIGINT handling, there is a race between the
225 readline signal handler and GDB. It may still be in
226 rl_prep_terminal in another thread. Do not return until it is
227 done; we can check the state here because we never longjmp from
228 signal handlers on Windows. */
229 while (RL_ISSTATE (RL_STATE_SIGHANDLER))
232 if (h == sigint_event
233 || WaitForSingleObject (sigint_event, 0) == WAIT_OBJECT_0)
235 if (sigint_handler != NULL)
236 call_async_signal_handler (sigint_handler);
248 /* Wrapper for the body of signal handlers. On Windows systems, a
249 SIGINT handler runs in its own thread. We can't longjmp from
250 there, and we shouldn't even prompt the user. Delay HANDLER
251 until the main thread is next in gdb_select. */
254 gdb_call_async_signal_handler (struct async_signal_handler *handler,
258 sigint_handler = handler;
261 mark_async_signal_handler (handler);
262 sigint_handler = NULL;
264 SetEvent (sigint_event);
267 /* -Wmissing-prototypes */
268 extern initialize_file_ftype _initialize_mingw_hdep;
271 _initialize_mingw_hdep (void)
273 sigint_event = CreateEvent (0, FALSE, FALSE, 0);