This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gdb / mingw-hdep.c
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"
24 #include "serial.h"
25
26 #include "gdb_assert.h"
27 #include "gdb_select.h"
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 }
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
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;
170
171       if (FD_ISSET (fd, readfds))
172         {
173           fd_h = handles[indx++];
174           /* This handle might be ready, even though it wasn't the handle
175              returned by WaitForMultipleObjects.  */
176           if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
177             FD_CLR (fd, readfds);
178           else
179             num_ready++;
180         }
181
182       if (FD_ISSET (fd, exceptfds))
183         {
184           fd_h = handles[indx++];
185           /* This handle might be ready, even though it wasn't the handle
186              returned by WaitForMultipleObjects.  */
187           if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
188             FD_CLR (fd, exceptfds);
189           else
190             num_ready++;
191         }
192     }
193
194   return num_ready;
195 }