1 /* Host support routines for MinGW, for GDB, the GNU debugger.
3 Copyright (C) 2006-2017 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"
26 #include "readline/readline.h"
30 /* Return an absolute file name of the running GDB, if possible, or
31 ARGV0 if not. The return value is in malloc'ed storage. */
34 windows_get_absolute_argv0 (const char *argv0)
36 char full_name[PATH_MAX];
38 if (GetModuleFileName (NULL, full_name, PATH_MAX))
39 return xstrdup (full_name);
40 return xstrdup (argv0);
43 /* Wrapper for select. On Windows systems, where the select interface
44 only works for sockets, this uses the GDB serial abstraction to
45 handle sockets, consoles, pipes, and serial ports.
47 The arguments to this function are the same as the traditional
48 arguments to select on POSIX platforms. */
51 gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
52 struct timeval *timeout)
54 static HANDLE never_handle;
55 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
59 /* SCBS contains serial control objects corresponding to file
60 descriptors in READFDS and WRITEFDS. */
61 struct serial *scbs[MAXIMUM_WAIT_OBJECTS];
62 /* The number of valid entries in SCBS. */
71 for (fd = 0; fd < n; ++fd)
73 HANDLE read = NULL, except = NULL;
76 /* There is no support yet for WRITEFDS. At present, this isn't
77 used by GDB -- but we do not want to silently ignore WRITEFDS
78 if something starts using it. */
79 gdb_assert (!writefds || !FD_ISSET (fd, writefds));
81 if ((!readfds || !FD_ISSET (fd, readfds))
82 && (!exceptfds || !FD_ISSET (fd, exceptfds)))
85 scb = serial_for_fd (fd);
88 serial_wait_handle (scb, &read, &except);
89 scbs[num_scbs++] = scb;
93 read = (HANDLE) _get_osfhandle (fd);
97 never_handle = CreateEvent (0, FALSE, FALSE, 0);
99 except = never_handle;
102 if (readfds && FD_ISSET (fd, readfds))
104 gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
105 handles[num_handles++] = read;
108 if (exceptfds && FD_ISSET (fd, exceptfds))
110 gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
111 handles[num_handles++] = except;
115 gdb_assert (num_handles <= MAXIMUM_WAIT_OBJECTS);
117 event = WaitForMultipleObjects (num_handles,
121 ? (timeout->tv_sec * 1000
122 + timeout->tv_usec / 1000)
124 /* EVENT can only be a value in the WAIT_ABANDONED_0 range if the
125 HANDLES included an abandoned mutex. Since GDB doesn't use
126 mutexes, that should never occur. */
127 gdb_assert (!(WAIT_ABANDONED_0 <= event
128 && event < WAIT_ABANDONED_0 + num_handles));
129 /* We no longer need the helper threads to check for activity. */
130 for (indx = 0; indx < num_scbs; ++indx)
131 serial_done_wait_handle (scbs[indx]);
132 if (event == WAIT_FAILED)
134 if (event == WAIT_TIMEOUT)
136 /* Run through the READFDS, clearing bits corresponding to descriptors
137 for which input is unavailable. */
138 h = handles[event - WAIT_OBJECT_0];
139 for (fd = 0, indx = 0; fd < n; ++fd)
143 if ((!readfds || !FD_ISSET (fd, readfds))
144 && (!exceptfds || !FD_ISSET (fd, exceptfds)))
147 if (readfds && FD_ISSET (fd, readfds))
149 fd_h = handles[indx++];
150 /* This handle might be ready, even though it wasn't the handle
151 returned by WaitForMultipleObjects. */
152 if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
153 FD_CLR (fd, readfds);
158 if (exceptfds && FD_ISSET (fd, exceptfds))
160 fd_h = handles[indx++];
161 /* This handle might be ready, even though it wasn't the handle
162 returned by WaitForMultipleObjects. */
163 if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
164 FD_CLR (fd, exceptfds);
170 /* With multi-threaded SIGINT handling, there is a race between the
171 readline signal handler and GDB. It may still be in
172 rl_prep_terminal in another thread. Do not return until it is
173 done; we can check the state here because we never longjmp from
174 signal handlers on Windows. */
175 while (RL_ISSTATE (RL_STATE_SIGHANDLER))