Commit | Line | Data |
---|---|---|
121ce6e5 DJ |
1 | /* Host support routines for MinGW, for GDB, the GNU debugger. |
2 | ||
ecd75fc8 | 3 | Copyright (C) 2006-2014 Free Software Foundation, Inc. |
121ce6e5 DJ |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
121ce6e5 DJ |
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 | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
121ce6e5 DJ |
19 | |
20 | #include "defs.h" | |
d9ac0664 | 21 | #include "main.h" |
0ea3f30e | 22 | #include "serial.h" |
b803fb0f | 23 | #include "event-loop.h" |
121ce6e5 | 24 | |
0ea3f30e | 25 | #include "gdb_select.h" |
b803fb0f | 26 | #include "readline/readline.h" |
121ce6e5 DJ |
27 | |
28 | #include <windows.h> | |
29 | ||
b803fb0f DJ |
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; | |
33 | ||
34 | /* When SIGINT_EVENT is signalled, gdb_select will call this | |
35 | function. */ | |
36 | struct async_signal_handler *sigint_handler; | |
37 | ||
121ce6e5 DJ |
38 | /* The strerror() function can return NULL for errno values that are |
39 | out of range. Provide a "safe" version that always returns a | |
40 | printable string. | |
41 | ||
42 | The Windows runtime implementation of strerror never returns NULL, | |
43 | but does return a useless string for anything above sys_nerr; | |
44 | unfortunately this includes all socket-related error codes. | |
45 | This replacement tries to find a system-provided error message. */ | |
46 | ||
47 | char * | |
48 | safe_strerror (int errnum) | |
49 | { | |
50 | static char *buffer; | |
51 | int len; | |
52 | ||
53 | if (errnum >= 0 && errnum < sys_nerr) | |
54 | return strerror (errnum); | |
55 | ||
56 | if (buffer) | |
57 | { | |
58 | LocalFree (buffer); | |
59 | buffer = NULL; | |
60 | } | |
61 | ||
62 | if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | |
63 | | FORMAT_MESSAGE_FROM_SYSTEM, | |
64 | NULL, errnum, | |
65 | MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), | |
66 | (LPTSTR) &buffer, 0, NULL) == 0) | |
67 | { | |
68 | static char buf[32]; | |
69 | xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum); | |
70 | return buf; | |
71 | } | |
72 | ||
73 | /* Windows error messages end with a period and a CR-LF; strip that | |
74 | out. */ | |
75 | len = strlen (buffer); | |
76 | if (len > 3 && strcmp (buffer + len - 3, ".\r\n") == 0) | |
77 | buffer[len - 3] = '\0'; | |
78 | ||
79 | return buffer; | |
80 | } | |
0ea3f30e | 81 | |
d9ac0664 EZ |
82 | /* Return an absolute file name of the running GDB, if possible, or |
83 | ARGV0 if not. The return value is in malloc'ed storage. */ | |
84 | ||
85 | char * | |
86 | windows_get_absolute_argv0 (const char *argv0) | |
87 | { | |
88 | char full_name[PATH_MAX]; | |
89 | ||
90 | if (GetModuleFileName (NULL, full_name, PATH_MAX)) | |
91 | return xstrdup (full_name); | |
92 | return xstrdup (argv0); | |
93 | } | |
94 | ||
0ea3f30e DJ |
95 | /* Wrapper for select. On Windows systems, where the select interface |
96 | only works for sockets, this uses the GDB serial abstraction to | |
97 | handle sockets, consoles, pipes, and serial ports. | |
98 | ||
99 | The arguments to this function are the same as the traditional | |
100 | arguments to select on POSIX platforms. */ | |
101 | ||
102 | int | |
103 | gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, | |
104 | struct timeval *timeout) | |
105 | { | |
106 | static HANDLE never_handle; | |
107 | HANDLE handles[MAXIMUM_WAIT_OBJECTS]; | |
108 | HANDLE h; | |
109 | DWORD event; | |
110 | DWORD num_handles; | |
4577549b DJ |
111 | /* SCBS contains serial control objects corresponding to file |
112 | descriptors in READFDS and WRITEFDS. */ | |
113 | struct serial *scbs[MAXIMUM_WAIT_OBJECTS]; | |
114 | /* The number of valid entries in SCBS. */ | |
115 | size_t num_scbs; | |
0ea3f30e DJ |
116 | int fd; |
117 | int num_ready; | |
4577549b | 118 | size_t indx; |
0ea3f30e DJ |
119 | |
120 | num_ready = 0; | |
121 | num_handles = 0; | |
4577549b | 122 | num_scbs = 0; |
0ea3f30e DJ |
123 | for (fd = 0; fd < n; ++fd) |
124 | { | |
125 | HANDLE read = NULL, except = NULL; | |
126 | struct serial *scb; | |
127 | ||
128 | /* There is no support yet for WRITEFDS. At present, this isn't | |
129 | used by GDB -- but we do not want to silently ignore WRITEFDS | |
130 | if something starts using it. */ | |
131 | gdb_assert (!writefds || !FD_ISSET (fd, writefds)); | |
132 | ||
98739726 DJ |
133 | if ((!readfds || !FD_ISSET (fd, readfds)) |
134 | && (!exceptfds || !FD_ISSET (fd, exceptfds))) | |
0ea3f30e | 135 | continue; |
0ea3f30e DJ |
136 | |
137 | scb = serial_for_fd (fd); | |
138 | if (scb) | |
4577549b DJ |
139 | { |
140 | serial_wait_handle (scb, &read, &except); | |
141 | scbs[num_scbs++] = scb; | |
142 | } | |
0ea3f30e DJ |
143 | |
144 | if (read == NULL) | |
4577549b | 145 | read = (HANDLE) _get_osfhandle (fd); |
0ea3f30e DJ |
146 | if (except == NULL) |
147 | { | |
148 | if (!never_handle) | |
149 | never_handle = CreateEvent (0, FALSE, FALSE, 0); | |
150 | ||
151 | except = never_handle; | |
152 | } | |
153 | ||
98739726 | 154 | if (readfds && FD_ISSET (fd, readfds)) |
0ea3f30e DJ |
155 | { |
156 | gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS); | |
157 | handles[num_handles++] = read; | |
158 | } | |
159 | ||
98739726 | 160 | if (exceptfds && FD_ISSET (fd, exceptfds)) |
0ea3f30e DJ |
161 | { |
162 | gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS); | |
163 | handles[num_handles++] = except; | |
164 | } | |
165 | } | |
0ea3f30e | 166 | |
b803fb0f DJ |
167 | gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS); |
168 | handles[num_handles++] = sigint_event; | |
0ea3f30e DJ |
169 | |
170 | event = WaitForMultipleObjects (num_handles, | |
171 | handles, | |
172 | FALSE, | |
173 | timeout | |
174 | ? (timeout->tv_sec * 1000 | |
175 | + timeout->tv_usec / 1000) | |
176 | : INFINITE); | |
177 | /* EVENT can only be a value in the WAIT_ABANDONED_0 range if the | |
178 | HANDLES included an abandoned mutex. Since GDB doesn't use | |
179 | mutexes, that should never occur. */ | |
180 | gdb_assert (!(WAIT_ABANDONED_0 <= event | |
181 | && event < WAIT_ABANDONED_0 + num_handles)); | |
4577549b DJ |
182 | /* We no longer need the helper threads to check for activity. */ |
183 | for (indx = 0; indx < num_scbs; ++indx) | |
184 | serial_done_wait_handle (scbs[indx]); | |
0ea3f30e DJ |
185 | if (event == WAIT_FAILED) |
186 | return -1; | |
187 | if (event == WAIT_TIMEOUT) | |
188 | return 0; | |
189 | /* Run through the READFDS, clearing bits corresponding to descriptors | |
190 | for which input is unavailable. */ | |
191 | h = handles[event - WAIT_OBJECT_0]; | |
192 | for (fd = 0, indx = 0; fd < n; ++fd) | |
193 | { | |
194 | HANDLE fd_h; | |
c3e2b812 | 195 | |
98739726 DJ |
196 | if ((!readfds || !FD_ISSET (fd, readfds)) |
197 | && (!exceptfds || !FD_ISSET (fd, exceptfds))) | |
c3e2b812 | 198 | continue; |
0ea3f30e | 199 | |
98739726 | 200 | if (readfds && FD_ISSET (fd, readfds)) |
0ea3f30e DJ |
201 | { |
202 | fd_h = handles[indx++]; | |
203 | /* This handle might be ready, even though it wasn't the handle | |
204 | returned by WaitForMultipleObjects. */ | |
205 | if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0) | |
206 | FD_CLR (fd, readfds); | |
207 | else | |
208 | num_ready++; | |
209 | } | |
210 | ||
98739726 | 211 | if (exceptfds && FD_ISSET (fd, exceptfds)) |
0ea3f30e DJ |
212 | { |
213 | fd_h = handles[indx++]; | |
214 | /* This handle might be ready, even though it wasn't the handle | |
215 | returned by WaitForMultipleObjects. */ | |
216 | if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0) | |
217 | FD_CLR (fd, exceptfds); | |
218 | else | |
219 | num_ready++; | |
220 | } | |
221 | } | |
222 | ||
b803fb0f DJ |
223 | /* With multi-threaded SIGINT handling, there is a race between the |
224 | readline signal handler and GDB. It may still be in | |
225 | rl_prep_terminal in another thread. Do not return until it is | |
226 | done; we can check the state here because we never longjmp from | |
227 | signal handlers on Windows. */ | |
228 | while (RL_ISSTATE (RL_STATE_SIGHANDLER)) | |
229 | Sleep (1); | |
230 | ||
231 | if (h == sigint_event | |
232 | || WaitForSingleObject (sigint_event, 0) == WAIT_OBJECT_0) | |
233 | { | |
234 | if (sigint_handler != NULL) | |
235 | call_async_signal_handler (sigint_handler); | |
236 | ||
237 | if (num_ready == 0) | |
238 | { | |
239 | errno = EINTR; | |
240 | return -1; | |
241 | } | |
242 | } | |
243 | ||
0ea3f30e DJ |
244 | return num_ready; |
245 | } | |
b803fb0f DJ |
246 | |
247 | /* Wrapper for the body of signal handlers. On Windows systems, a | |
248 | SIGINT handler runs in its own thread. We can't longjmp from | |
249 | there, and we shouldn't even prompt the user. Delay HANDLER | |
250 | until the main thread is next in gdb_select. */ | |
251 | ||
252 | void | |
253 | gdb_call_async_signal_handler (struct async_signal_handler *handler, | |
254 | int immediate_p) | |
255 | { | |
256 | if (immediate_p) | |
257 | sigint_handler = handler; | |
258 | else | |
259 | { | |
260 | mark_async_signal_handler (handler); | |
261 | sigint_handler = NULL; | |
262 | } | |
263 | SetEvent (sigint_event); | |
264 | } | |
265 | ||
a95babbf YQ |
266 | /* -Wmissing-prototypes */ |
267 | extern initialize_file_ftype _initialize_mingw_hdep; | |
268 | ||
b803fb0f DJ |
269 | void |
270 | _initialize_mingw_hdep (void) | |
271 | { | |
272 | sigint_event = CreateEvent (0, FALSE, FALSE, 0); | |
273 | } |