Copyright updates for 2007.
[external/binutils.git] / gdb / mingw-hdep.c
1 /* Host support routines for MinGW, for GDB, the GNU debugger.
2
3    Copyright (C) 2006, 2007 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 2 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, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor,
20    Boston, MA 02110-1301, USA.  */
21
22 #include "defs.h"
23 #include "serial.h"
24
25 #include "gdb_assert.h"
26 #include "gdb_select.h"
27 #include "gdb_string.h"
28
29 #include <windows.h>
30
31 /* The strerror() function can return NULL for errno values that are
32    out of range.  Provide a "safe" version that always returns a
33    printable string.
34
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.  */
39
40 char *
41 safe_strerror (int errnum)
42 {
43   static char *buffer;
44   int len;
45
46   if (errnum >= 0 && errnum < sys_nerr)
47     return strerror (errnum);
48
49   if (buffer)
50     {
51       LocalFree (buffer);
52       buffer = NULL;
53     }
54
55   if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
56                      | FORMAT_MESSAGE_FROM_SYSTEM,
57                      NULL, errnum,
58                      MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
59                      (LPTSTR) &buffer, 0, NULL) == 0)
60     {
61       static char buf[32];
62       xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum);
63       return buf;
64     }
65
66   /* Windows error messages end with a period and a CR-LF; strip that
67      out.  */
68   len = strlen (buffer);
69   if (len > 3 && strcmp (buffer + len - 3, ".\r\n") == 0)
70     buffer[len - 3] = '\0';
71
72   return buffer;
73 }
74
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.
78
79    The arguments to this function are the same as the traditional
80    arguments to select on POSIX platforms.  */
81
82 int
83 gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
84             struct timeval *timeout)
85 {
86   static HANDLE never_handle;
87   HANDLE handles[MAXIMUM_WAIT_OBJECTS];
88   HANDLE h;
89   DWORD event;
90   DWORD num_handles;
91   int fd;
92   int num_ready;
93   int indx;
94
95   num_ready = 0;
96   num_handles = 0;
97   for (fd = 0; fd < n; ++fd)
98     {
99       HANDLE read = NULL, except = NULL;
100       struct serial *scb;
101
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));
106
107       if ((!readfds || !FD_ISSET (fd, readfds))
108           && (!exceptfds || !FD_ISSET (fd, exceptfds)))
109         continue;
110       h = (HANDLE) _get_osfhandle (fd);
111
112       scb = serial_for_fd (fd);
113       if (scb)
114         serial_wait_handle (scb, &read, &except);
115
116       if (read == NULL)
117         read = h;
118       if (except == NULL)
119         {
120           if (!never_handle)
121             never_handle = CreateEvent (0, FALSE, FALSE, 0);
122
123           except = never_handle;
124         }
125
126       if (readfds && FD_ISSET (fd, readfds))
127         {
128           gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
129           handles[num_handles++] = read;
130         }
131
132       if (exceptfds && FD_ISSET (fd, exceptfds))
133         {
134           gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
135           handles[num_handles++] = except;
136         }
137     }
138   /* If we don't need to wait for any handles, we are done.  */
139   if (!num_handles)
140     {
141       if (timeout)
142         Sleep (timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
143
144       return 0;
145     }
146
147   event = WaitForMultipleObjects (num_handles,
148                                   handles,
149                                   FALSE,
150                                   timeout
151                                   ? (timeout->tv_sec * 1000
152                                      + timeout->tv_usec / 1000)
153                                   : INFINITE);
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)
160     return -1;
161   if (event == WAIT_TIMEOUT)
162     return 0;
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)
167     {
168       HANDLE fd_h;
169       struct serial *scb;
170
171       if ((!readfds || !FD_ISSET (fd, readfds))
172           && (!exceptfds || !FD_ISSET (fd, exceptfds)))
173         continue;
174
175       if (readfds && 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 (exceptfds && 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         }
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);
202     }
203
204   return num_ready;
205 }