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