]>
Commit | Line | Data |
---|---|---|
121ce6e5 DJ |
1 | /* Host support routines for MinGW, for GDB, the GNU debugger. |
2 | ||
3 | Copyright (C) 2006 | |
4 | Free Software Foundation, Inc. | |
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 | |
10 | the Free Software Foundation; either version 2 of the License, or | |
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 | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
21 | Boston, MA 02110-1301, USA. */ | |
22 | ||
23 | #include "defs.h" | |
0ea3f30e | 24 | #include "serial.h" |
121ce6e5 | 25 | |
0ea3f30e DJ |
26 | #include "gdb_assert.h" |
27 | #include "gdb_select.h" | |
121ce6e5 DJ |
28 | #include "gdb_string.h" |
29 | ||
30 | #include <windows.h> | |
31 | ||
32 | /* The strerror() function can return NULL for errno values that are | |
33 | out of range. Provide a "safe" version that always returns a | |
34 | printable string. | |
35 | ||
36 | The Windows runtime implementation of strerror never returns NULL, | |
37 | but does return a useless string for anything above sys_nerr; | |
38 | unfortunately this includes all socket-related error codes. | |
39 | This replacement tries to find a system-provided error message. */ | |
40 | ||
41 | char * | |
42 | safe_strerror (int errnum) | |
43 | { | |
44 | static char *buffer; | |
45 | int len; | |
46 | ||
47 | if (errnum >= 0 && errnum < sys_nerr) | |
48 | return strerror (errnum); | |
49 | ||
50 | if (buffer) | |
51 | { | |
52 | LocalFree (buffer); | |
53 | buffer = NULL; | |
54 | } | |
55 | ||
56 | if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | |
57 | | FORMAT_MESSAGE_FROM_SYSTEM, | |
58 | NULL, errnum, | |
59 | MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), | |
60 | (LPTSTR) &buffer, 0, NULL) == 0) | |
61 | { | |
62 | static char buf[32]; | |
63 | xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum); | |
64 | return buf; | |
65 | } | |
66 | ||
67 | /* Windows error messages end with a period and a CR-LF; strip that | |
68 | out. */ | |
69 | len = strlen (buffer); | |
70 | if (len > 3 && strcmp (buffer + len - 3, ".\r\n") == 0) | |
71 | buffer[len - 3] = '\0'; | |
72 | ||
73 | return buffer; | |
74 | } | |
0ea3f30e DJ |
75 | |
76 | /* Wrapper for select. On Windows systems, where the select interface | |
77 | only works for sockets, this uses the GDB serial abstraction to | |
78 | handle sockets, consoles, pipes, and serial ports. | |
79 | ||
80 | The arguments to this function are the same as the traditional | |
81 | arguments to select on POSIX platforms. */ | |
82 | ||
83 | int | |
84 | gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, | |
85 | struct timeval *timeout) | |
86 | { | |
87 | static HANDLE never_handle; | |
88 | HANDLE handles[MAXIMUM_WAIT_OBJECTS]; | |
89 | HANDLE h; | |
90 | DWORD event; | |
91 | DWORD num_handles; | |
92 | int fd; | |
93 | int num_ready; | |
94 | int indx; | |
95 | ||
96 | num_ready = 0; | |
97 | num_handles = 0; | |
98 | for (fd = 0; fd < n; ++fd) | |
99 | { | |
100 | HANDLE read = NULL, except = NULL; | |
101 | struct serial *scb; | |
102 | ||
103 | /* There is no support yet for WRITEFDS. At present, this isn't | |
104 | used by GDB -- but we do not want to silently ignore WRITEFDS | |
105 | if something starts using it. */ | |
106 | gdb_assert (!writefds || !FD_ISSET (fd, writefds)); | |
107 | ||
98739726 DJ |
108 | if ((!readfds || !FD_ISSET (fd, readfds)) |
109 | && (!exceptfds || !FD_ISSET (fd, exceptfds))) | |
0ea3f30e DJ |
110 | continue; |
111 | h = (HANDLE) _get_osfhandle (fd); | |
112 | ||
113 | scb = serial_for_fd (fd); | |
114 | if (scb) | |
115 | serial_wait_handle (scb, &read, &except); | |
116 | ||
117 | if (read == NULL) | |
118 | read = h; | |
119 | if (except == NULL) | |
120 | { | |
121 | if (!never_handle) | |
122 | never_handle = CreateEvent (0, FALSE, FALSE, 0); | |
123 | ||
124 | except = never_handle; | |
125 | } | |
126 | ||
98739726 | 127 | if (readfds && FD_ISSET (fd, readfds)) |
0ea3f30e DJ |
128 | { |
129 | gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS); | |
130 | handles[num_handles++] = read; | |
131 | } | |
132 | ||
98739726 | 133 | if (exceptfds && FD_ISSET (fd, exceptfds)) |
0ea3f30e DJ |
134 | { |
135 | gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS); | |
136 | handles[num_handles++] = except; | |
137 | } | |
138 | } | |
139 | /* If we don't need to wait for any handles, we are done. */ | |
140 | if (!num_handles) | |
141 | { | |
142 | if (timeout) | |
143 | Sleep (timeout->tv_sec * 1000 + timeout->tv_usec / 1000); | |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
148 | event = WaitForMultipleObjects (num_handles, | |
149 | handles, | |
150 | FALSE, | |
151 | timeout | |
152 | ? (timeout->tv_sec * 1000 | |
153 | + timeout->tv_usec / 1000) | |
154 | : INFINITE); | |
155 | /* EVENT can only be a value in the WAIT_ABANDONED_0 range if the | |
156 | HANDLES included an abandoned mutex. Since GDB doesn't use | |
157 | mutexes, that should never occur. */ | |
158 | gdb_assert (!(WAIT_ABANDONED_0 <= event | |
159 | && event < WAIT_ABANDONED_0 + num_handles)); | |
160 | if (event == WAIT_FAILED) | |
161 | return -1; | |
162 | if (event == WAIT_TIMEOUT) | |
163 | return 0; | |
164 | /* Run through the READFDS, clearing bits corresponding to descriptors | |
165 | for which input is unavailable. */ | |
166 | h = handles[event - WAIT_OBJECT_0]; | |
167 | for (fd = 0, indx = 0; fd < n; ++fd) | |
168 | { | |
169 | HANDLE fd_h; | |
c3e2b812 DJ |
170 | struct serial *scb; |
171 | ||
98739726 DJ |
172 | if ((!readfds || !FD_ISSET (fd, readfds)) |
173 | && (!exceptfds || !FD_ISSET (fd, exceptfds))) | |
c3e2b812 | 174 | continue; |
0ea3f30e | 175 | |
98739726 | 176 | if (readfds && FD_ISSET (fd, readfds)) |
0ea3f30e DJ |
177 | { |
178 | fd_h = handles[indx++]; | |
179 | /* This handle might be ready, even though it wasn't the handle | |
180 | returned by WaitForMultipleObjects. */ | |
181 | if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0) | |
182 | FD_CLR (fd, readfds); | |
183 | else | |
184 | num_ready++; | |
185 | } | |
186 | ||
98739726 | 187 | if (exceptfds && FD_ISSET (fd, exceptfds)) |
0ea3f30e DJ |
188 | { |
189 | fd_h = handles[indx++]; | |
190 | /* This handle might be ready, even though it wasn't the handle | |
191 | returned by WaitForMultipleObjects. */ | |
192 | if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0) | |
193 | FD_CLR (fd, exceptfds); | |
194 | else | |
195 | num_ready++; | |
196 | } | |
c3e2b812 DJ |
197 | |
198 | /* We created at least one event handle for this fd. Let the | |
199 | device know we are finished with it. */ | |
200 | scb = serial_for_fd (fd); | |
201 | if (scb) | |
202 | serial_done_wait_handle (scb); | |
0ea3f30e DJ |
203 | } |
204 | ||
205 | return num_ready; | |
206 | } |