1 /* Host support routines for MinGW, for GDB, the GNU debugger.
3 Copyright (C) 2006, 2007 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 "gdb_assert.h"
24 #include "gdb_select.h"
25 #include "gdb_string.h"
29 /* The strerror() function can return NULL for errno values that are
30 out of range. Provide a "safe" version that always returns a
33 The Windows runtime implementation of strerror never returns NULL,
34 but does return a useless string for anything above sys_nerr;
35 unfortunately this includes all socket-related error codes.
36 This replacement tries to find a system-provided error message. */
39 safe_strerror (int errnum
)
44 if (errnum
>= 0 && errnum
< sys_nerr
)
45 return strerror (errnum
);
53 if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
54 | FORMAT_MESSAGE_FROM_SYSTEM
,
56 MAKELANGID (LANG_NEUTRAL
, SUBLANG_DEFAULT
),
57 (LPTSTR
) &buffer
, 0, NULL
) == 0)
60 xsnprintf (buf
, sizeof buf
, "(undocumented errno %d)", errnum
);
64 /* Windows error messages end with a period and a CR-LF; strip that
66 len
= strlen (buffer
);
67 if (len
> 3 && strcmp (buffer
+ len
- 3, ".\r\n") == 0)
68 buffer
[len
- 3] = '\0';
73 /* Wrapper for select. On Windows systems, where the select interface
74 only works for sockets, this uses the GDB serial abstraction to
75 handle sockets, consoles, pipes, and serial ports.
77 The arguments to this function are the same as the traditional
78 arguments to select on POSIX platforms. */
81 gdb_select (int n
, fd_set
*readfds
, fd_set
*writefds
, fd_set
*exceptfds
,
82 struct timeval
*timeout
)
84 static HANDLE never_handle
;
85 HANDLE handles
[MAXIMUM_WAIT_OBJECTS
];
95 for (fd
= 0; fd
< n
; ++fd
)
97 HANDLE read
= NULL
, except
= NULL
;
100 /* There is no support yet for WRITEFDS. At present, this isn't
101 used by GDB -- but we do not want to silently ignore WRITEFDS
102 if something starts using it. */
103 gdb_assert (!writefds
|| !FD_ISSET (fd
, writefds
));
105 if ((!readfds
|| !FD_ISSET (fd
, readfds
))
106 && (!exceptfds
|| !FD_ISSET (fd
, exceptfds
)))
108 h
= (HANDLE
) _get_osfhandle (fd
);
110 scb
= serial_for_fd (fd
);
112 serial_wait_handle (scb
, &read
, &except
);
119 never_handle
= CreateEvent (0, FALSE
, FALSE
, 0);
121 except
= never_handle
;
124 if (readfds
&& FD_ISSET (fd
, readfds
))
126 gdb_assert (num_handles
< MAXIMUM_WAIT_OBJECTS
);
127 handles
[num_handles
++] = read
;
130 if (exceptfds
&& FD_ISSET (fd
, exceptfds
))
132 gdb_assert (num_handles
< MAXIMUM_WAIT_OBJECTS
);
133 handles
[num_handles
++] = except
;
136 /* If we don't need to wait for any handles, we are done. */
140 Sleep (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000);
145 event
= WaitForMultipleObjects (num_handles
,
149 ? (timeout
->tv_sec
* 1000
150 + timeout
->tv_usec
/ 1000)
152 /* EVENT can only be a value in the WAIT_ABANDONED_0 range if the
153 HANDLES included an abandoned mutex. Since GDB doesn't use
154 mutexes, that should never occur. */
155 gdb_assert (!(WAIT_ABANDONED_0
<= event
156 && event
< WAIT_ABANDONED_0
+ num_handles
));
157 if (event
== WAIT_FAILED
)
159 if (event
== WAIT_TIMEOUT
)
161 /* Run through the READFDS, clearing bits corresponding to descriptors
162 for which input is unavailable. */
163 h
= handles
[event
- WAIT_OBJECT_0
];
164 for (fd
= 0, indx
= 0; fd
< n
; ++fd
)
169 if ((!readfds
|| !FD_ISSET (fd
, readfds
))
170 && (!exceptfds
|| !FD_ISSET (fd
, exceptfds
)))
173 if (readfds
&& FD_ISSET (fd
, readfds
))
175 fd_h
= handles
[indx
++];
176 /* This handle might be ready, even though it wasn't the handle
177 returned by WaitForMultipleObjects. */
178 if (fd_h
!= h
&& WaitForSingleObject (fd_h
, 0) != WAIT_OBJECT_0
)
179 FD_CLR (fd
, readfds
);
184 if (exceptfds
&& FD_ISSET (fd
, exceptfds
))
186 fd_h
= handles
[indx
++];
187 /* This handle might be ready, even though it wasn't the handle
188 returned by WaitForMultipleObjects. */
189 if (fd_h
!= h
&& WaitForSingleObject (fd_h
, 0) != WAIT_OBJECT_0
)
190 FD_CLR (fd
, exceptfds
);
195 /* We created at least one event handle for this fd. Let the
196 device know we are finished with it. */
197 scb
= serial_for_fd (fd
);
199 serial_done_wait_handle (scb
);