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 2 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, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
25 #include "gdb_assert.h"
26 #include "gdb_select.h"
27 #include "gdb_string.h"
31 /* The strerror() function can return NULL for errno values that are
32 out of range. Provide a "safe" version that always returns a
35 The Windows runtime implementation of strerror never returns NULL,
36 but does return a useless string for anything above sys_nerr;
37 unfortunately this includes all socket-related error codes.
38 This replacement tries to find a system-provided error message. */
41 safe_strerror (int errnum
)
46 if (errnum
>= 0 && errnum
< sys_nerr
)
47 return strerror (errnum
);
55 if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
56 | FORMAT_MESSAGE_FROM_SYSTEM
,
58 MAKELANGID (LANG_NEUTRAL
, SUBLANG_DEFAULT
),
59 (LPTSTR
) &buffer
, 0, NULL
) == 0)
62 xsnprintf (buf
, sizeof buf
, "(undocumented errno %d)", errnum
);
66 /* Windows error messages end with a period and a CR-LF; strip that
68 len
= strlen (buffer
);
69 if (len
> 3 && strcmp (buffer
+ len
- 3, ".\r\n") == 0)
70 buffer
[len
- 3] = '\0';
75 /* Wrapper for select. On Windows systems, where the select interface
76 only works for sockets, this uses the GDB serial abstraction to
77 handle sockets, consoles, pipes, and serial ports.
79 The arguments to this function are the same as the traditional
80 arguments to select on POSIX platforms. */
83 gdb_select (int n
, fd_set
*readfds
, fd_set
*writefds
, fd_set
*exceptfds
,
84 struct timeval
*timeout
)
86 static HANDLE never_handle
;
87 HANDLE handles
[MAXIMUM_WAIT_OBJECTS
];
97 for (fd
= 0; fd
< n
; ++fd
)
99 HANDLE read
= NULL
, except
= NULL
;
102 /* There is no support yet for WRITEFDS. At present, this isn't
103 used by GDB -- but we do not want to silently ignore WRITEFDS
104 if something starts using it. */
105 gdb_assert (!writefds
|| !FD_ISSET (fd
, writefds
));
107 if ((!readfds
|| !FD_ISSET (fd
, readfds
))
108 && (!exceptfds
|| !FD_ISSET (fd
, exceptfds
)))
110 h
= (HANDLE
) _get_osfhandle (fd
);
112 scb
= serial_for_fd (fd
);
114 serial_wait_handle (scb
, &read
, &except
);
121 never_handle
= CreateEvent (0, FALSE
, FALSE
, 0);
123 except
= never_handle
;
126 if (readfds
&& FD_ISSET (fd
, readfds
))
128 gdb_assert (num_handles
< MAXIMUM_WAIT_OBJECTS
);
129 handles
[num_handles
++] = read
;
132 if (exceptfds
&& FD_ISSET (fd
, exceptfds
))
134 gdb_assert (num_handles
< MAXIMUM_WAIT_OBJECTS
);
135 handles
[num_handles
++] = except
;
138 /* If we don't need to wait for any handles, we are done. */
142 Sleep (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000);
147 event
= WaitForMultipleObjects (num_handles
,
151 ? (timeout
->tv_sec
* 1000
152 + timeout
->tv_usec
/ 1000)
154 /* EVENT can only be a value in the WAIT_ABANDONED_0 range if the
155 HANDLES included an abandoned mutex. Since GDB doesn't use
156 mutexes, that should never occur. */
157 gdb_assert (!(WAIT_ABANDONED_0
<= event
158 && event
< WAIT_ABANDONED_0
+ num_handles
));
159 if (event
== WAIT_FAILED
)
161 if (event
== WAIT_TIMEOUT
)
163 /* Run through the READFDS, clearing bits corresponding to descriptors
164 for which input is unavailable. */
165 h
= handles
[event
- WAIT_OBJECT_0
];
166 for (fd
= 0, indx
= 0; fd
< n
; ++fd
)
171 if ((!readfds
|| !FD_ISSET (fd
, readfds
))
172 && (!exceptfds
|| !FD_ISSET (fd
, exceptfds
)))
175 if (readfds
&& FD_ISSET (fd
, readfds
))
177 fd_h
= handles
[indx
++];
178 /* This handle might be ready, even though it wasn't the handle
179 returned by WaitForMultipleObjects. */
180 if (fd_h
!= h
&& WaitForSingleObject (fd_h
, 0) != WAIT_OBJECT_0
)
181 FD_CLR (fd
, readfds
);
186 if (exceptfds
&& FD_ISSET (fd
, exceptfds
))
188 fd_h
= handles
[indx
++];
189 /* This handle might be ready, even though it wasn't the handle
190 returned by WaitForMultipleObjects. */
191 if (fd_h
!= h
&& WaitForSingleObject (fd_h
, 0) != WAIT_OBJECT_0
)
192 FD_CLR (fd
, exceptfds
);
197 /* We created at least one event handle for this fd. Let the
198 device know we are finished with it. */
199 scb
= serial_for_fd (fd
);
201 serial_done_wait_handle (scb
);