* python/py-utils.c (gdb_pymodule_addobject): Cast away const.
[deliverable/binutils-gdb.git] / gdb / mingw-hdep.c
1 /* Host support routines for MinGW, for GDB, the GNU debugger.
2
3 Copyright (C) 2006-2013 Free Software Foundation, Inc.
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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "main.h"
22 #include "serial.h"
23 #include "event-loop.h"
24
25 #include "gdb_assert.h"
26 #include "gdb_select.h"
27 #include "gdb_string.h"
28 #include "readline/readline.h"
29
30 #include <windows.h>
31
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
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 }
83
84 /* Return an absolute file name of the running GDB, if possible, or
85 ARGV0 if not. The return value is in malloc'ed storage. */
86
87 char *
88 windows_get_absolute_argv0 (const char *argv0)
89 {
90 char full_name[PATH_MAX];
91
92 if (GetModuleFileName (NULL, full_name, PATH_MAX))
93 return xstrdup (full_name);
94 return xstrdup (argv0);
95 }
96
97 /* Wrapper for select. On Windows systems, where the select interface
98 only works for sockets, this uses the GDB serial abstraction to
99 handle sockets, consoles, pipes, and serial ports.
100
101 The arguments to this function are the same as the traditional
102 arguments to select on POSIX platforms. */
103
104 int
105 gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
106 struct timeval *timeout)
107 {
108 static HANDLE never_handle;
109 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
110 HANDLE h;
111 DWORD event;
112 DWORD num_handles;
113 /* SCBS contains serial control objects corresponding to file
114 descriptors in READFDS and WRITEFDS. */
115 struct serial *scbs[MAXIMUM_WAIT_OBJECTS];
116 /* The number of valid entries in SCBS. */
117 size_t num_scbs;
118 int fd;
119 int num_ready;
120 size_t indx;
121
122 num_ready = 0;
123 num_handles = 0;
124 num_scbs = 0;
125 for (fd = 0; fd < n; ++fd)
126 {
127 HANDLE read = NULL, except = NULL;
128 struct serial *scb;
129
130 /* There is no support yet for WRITEFDS. At present, this isn't
131 used by GDB -- but we do not want to silently ignore WRITEFDS
132 if something starts using it. */
133 gdb_assert (!writefds || !FD_ISSET (fd, writefds));
134
135 if ((!readfds || !FD_ISSET (fd, readfds))
136 && (!exceptfds || !FD_ISSET (fd, exceptfds)))
137 continue;
138
139 scb = serial_for_fd (fd);
140 if (scb)
141 {
142 serial_wait_handle (scb, &read, &except);
143 scbs[num_scbs++] = scb;
144 }
145
146 if (read == NULL)
147 read = (HANDLE) _get_osfhandle (fd);
148 if (except == NULL)
149 {
150 if (!never_handle)
151 never_handle = CreateEvent (0, FALSE, FALSE, 0);
152
153 except = never_handle;
154 }
155
156 if (readfds && FD_ISSET (fd, readfds))
157 {
158 gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
159 handles[num_handles++] = read;
160 }
161
162 if (exceptfds && FD_ISSET (fd, exceptfds))
163 {
164 gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
165 handles[num_handles++] = except;
166 }
167 }
168
169 gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
170 handles[num_handles++] = sigint_event;
171
172 event = WaitForMultipleObjects (num_handles,
173 handles,
174 FALSE,
175 timeout
176 ? (timeout->tv_sec * 1000
177 + timeout->tv_usec / 1000)
178 : INFINITE);
179 /* EVENT can only be a value in the WAIT_ABANDONED_0 range if the
180 HANDLES included an abandoned mutex. Since GDB doesn't use
181 mutexes, that should never occur. */
182 gdb_assert (!(WAIT_ABANDONED_0 <= event
183 && event < WAIT_ABANDONED_0 + num_handles));
184 /* We no longer need the helper threads to check for activity. */
185 for (indx = 0; indx < num_scbs; ++indx)
186 serial_done_wait_handle (scbs[indx]);
187 if (event == WAIT_FAILED)
188 return -1;
189 if (event == WAIT_TIMEOUT)
190 return 0;
191 /* Run through the READFDS, clearing bits corresponding to descriptors
192 for which input is unavailable. */
193 h = handles[event - WAIT_OBJECT_0];
194 for (fd = 0, indx = 0; fd < n; ++fd)
195 {
196 HANDLE fd_h;
197
198 if ((!readfds || !FD_ISSET (fd, readfds))
199 && (!exceptfds || !FD_ISSET (fd, exceptfds)))
200 continue;
201
202 if (readfds && FD_ISSET (fd, readfds))
203 {
204 fd_h = handles[indx++];
205 /* This handle might be ready, even though it wasn't the handle
206 returned by WaitForMultipleObjects. */
207 if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
208 FD_CLR (fd, readfds);
209 else
210 num_ready++;
211 }
212
213 if (exceptfds && FD_ISSET (fd, exceptfds))
214 {
215 fd_h = handles[indx++];
216 /* This handle might be ready, even though it wasn't the handle
217 returned by WaitForMultipleObjects. */
218 if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
219 FD_CLR (fd, exceptfds);
220 else
221 num_ready++;
222 }
223 }
224
225 /* With multi-threaded SIGINT handling, there is a race between the
226 readline signal handler and GDB. It may still be in
227 rl_prep_terminal in another thread. Do not return until it is
228 done; we can check the state here because we never longjmp from
229 signal handlers on Windows. */
230 while (RL_ISSTATE (RL_STATE_SIGHANDLER))
231 Sleep (1);
232
233 if (h == sigint_event
234 || WaitForSingleObject (sigint_event, 0) == WAIT_OBJECT_0)
235 {
236 if (sigint_handler != NULL)
237 call_async_signal_handler (sigint_handler);
238
239 if (num_ready == 0)
240 {
241 errno = EINTR;
242 return -1;
243 }
244 }
245
246 return num_ready;
247 }
248
249 /* Wrapper for the body of signal handlers. On Windows systems, a
250 SIGINT handler runs in its own thread. We can't longjmp from
251 there, and we shouldn't even prompt the user. Delay HANDLER
252 until the main thread is next in gdb_select. */
253
254 void
255 gdb_call_async_signal_handler (struct async_signal_handler *handler,
256 int immediate_p)
257 {
258 if (immediate_p)
259 sigint_handler = handler;
260 else
261 {
262 mark_async_signal_handler (handler);
263 sigint_handler = NULL;
264 }
265 SetEvent (sigint_event);
266 }
267
268 /* -Wmissing-prototypes */
269 extern initialize_file_ftype _initialize_mingw_hdep;
270
271 void
272 _initialize_mingw_hdep (void)
273 {
274 sigint_event = CreateEvent (0, FALSE, FALSE, 0);
275 }
This page took 0.039392 seconds and 4 git commands to generate.