1 /* Host support routines for MinGW, for GDB, the GNU debugger.
3 Copyright (C) 2006-2016 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 /* This event is signalled whenever an asynchronous SIGINT handler
31 needs to perform an action in the main thread. */
32 static HANDLE sigint_event;
34 /* When SIGINT_EVENT is signalled, gdb_select will call this
36 struct async_signal_handler *sigint_handler;
38 /* Return an absolute file name of the running GDB, if possible, or
39 ARGV0 if not. The return value is in malloc'ed storage. */
42 windows_get_absolute_argv0 (const char *argv0)
44 char full_name[PATH_MAX];
46 if (GetModuleFileName (NULL, full_name, PATH_MAX))
47 return xstrdup (full_name);
48 return xstrdup (argv0);
51 /* Wrapper for select. On Windows systems, where the select interface
52 only works for sockets, this uses the GDB serial abstraction to
53 handle sockets, consoles, pipes, and serial ports.
55 The arguments to this function are the same as the traditional
56 arguments to select on POSIX platforms. */
59 gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
60 struct timeval *timeout)
62 static HANDLE never_handle;
63 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
67 /* SCBS contains serial control objects corresponding to file
68 descriptors in READFDS and WRITEFDS. */
69 struct serial *scbs[MAXIMUM_WAIT_OBJECTS];
70 /* The number of valid entries in SCBS. */
79 for (fd = 0; fd < n; ++fd)
81 HANDLE read = NULL, except = NULL;
84 /* There is no support yet for WRITEFDS. At present, this isn't
85 used by GDB -- but we do not want to silently ignore WRITEFDS
86 if something starts using it. */
87 gdb_assert (!writefds || !FD_ISSET (fd, writefds));
89 if ((!readfds || !FD_ISSET (fd, readfds))
90 && (!exceptfds || !FD_ISSET (fd, exceptfds)))
93 scb = serial_for_fd (fd);
96 serial_wait_handle (scb, &read, &except);
97 scbs[num_scbs++] = scb;
101 read = (HANDLE) _get_osfhandle (fd);
105 never_handle = CreateEvent (0, FALSE, FALSE, 0);
107 except = never_handle;
110 if (readfds && FD_ISSET (fd, readfds))
112 gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
113 handles[num_handles++] = read;
116 if (exceptfds && FD_ISSET (fd, exceptfds))
118 gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
119 handles[num_handles++] = except;
123 gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
124 handles[num_handles++] = sigint_event;
126 event = WaitForMultipleObjects (num_handles,
130 ? (timeout->tv_sec * 1000
131 + timeout->tv_usec / 1000)
133 /* EVENT can only be a value in the WAIT_ABANDONED_0 range if the
134 HANDLES included an abandoned mutex. Since GDB doesn't use
135 mutexes, that should never occur. */
136 gdb_assert (!(WAIT_ABANDONED_0 <= event
137 && event < WAIT_ABANDONED_0 + num_handles));
138 /* We no longer need the helper threads to check for activity. */
139 for (indx = 0; indx < num_scbs; ++indx)
140 serial_done_wait_handle (scbs[indx]);
141 if (event == WAIT_FAILED)
143 if (event == WAIT_TIMEOUT)
145 /* Run through the READFDS, clearing bits corresponding to descriptors
146 for which input is unavailable. */
147 h = handles[event - WAIT_OBJECT_0];
148 for (fd = 0, indx = 0; fd < n; ++fd)
152 if ((!readfds || !FD_ISSET (fd, readfds))
153 && (!exceptfds || !FD_ISSET (fd, exceptfds)))
156 if (readfds && FD_ISSET (fd, readfds))
158 fd_h = handles[indx++];
159 /* This handle might be ready, even though it wasn't the handle
160 returned by WaitForMultipleObjects. */
161 if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
162 FD_CLR (fd, readfds);
167 if (exceptfds && FD_ISSET (fd, exceptfds))
169 fd_h = handles[indx++];
170 /* This handle might be ready, even though it wasn't the handle
171 returned by WaitForMultipleObjects. */
172 if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
173 FD_CLR (fd, exceptfds);
179 /* With multi-threaded SIGINT handling, there is a race between the
180 readline signal handler and GDB. It may still be in
181 rl_prep_terminal in another thread. Do not return until it is
182 done; we can check the state here because we never longjmp from
183 signal handlers on Windows. */
184 while (RL_ISSTATE (RL_STATE_SIGHANDLER))
187 if (h == sigint_event
188 || WaitForSingleObject (sigint_event, 0) == WAIT_OBJECT_0)
190 if (sigint_handler != NULL)
191 call_async_signal_handler (sigint_handler);
203 /* Wrapper for the body of signal handlers. On Windows systems, a
204 SIGINT handler runs in its own thread. We can't longjmp from
205 there, and we shouldn't even prompt the user. Delay HANDLER
206 until the main thread is next in gdb_select. */
209 gdb_call_async_signal_handler (struct async_signal_handler *handler,
213 sigint_handler = handler;
216 mark_async_signal_handler (handler);
217 sigint_handler = NULL;
219 SetEvent (sigint_event);
222 /* -Wmissing-prototypes */
223 extern initialize_file_ftype _initialize_mingw_hdep;
226 _initialize_mingw_hdep (void)
228 sigint_event = CreateEvent (0, FALSE, FALSE, 0);