]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/mingw-hdep.c
2006-06-10 H.J. Lu <hongjiu.lu@intel.com>
[thirdparty/binutils-gdb.git] / gdb / mingw-hdep.c
CommitLineData
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
41char *
42safe_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
83int
84gdb_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
108 if (!FD_ISSET (fd, readfds)
109 && !FD_ISSET (fd, exceptfds))
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
127 if (FD_ISSET (fd, readfds))
128 {
129 gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
130 handles[num_handles++] = read;
131 }
132
133 if (FD_ISSET (fd, exceptfds))
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
172 if (!FD_ISSET (fd, readfds) && !FD_ISSET (fd, writefds))
173 continue;
0ea3f30e
DJ
174
175 if (FD_ISSET (fd, readfds))
176 {
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);
182 else
183 num_ready++;
184 }
185
186 if (FD_ISSET (fd, exceptfds))
187 {
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);
193 else
194 num_ready++;
195 }
c3e2b812
DJ
196
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);
200 if (scb)
201 serial_done_wait_handle (scb);
0ea3f30e
DJ
202 }
203
204 return num_ready;
205}