OSDN Git Service

Switch the license of all .c files to GPLv3.
[pf3gnuchains/pf3gnuchains3x.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 3 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, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "serial.h"
22
23 #include "gdb_assert.h"
24 #include "gdb_select.h"
25 #include "gdb_string.h"
26
27 #include <windows.h>
28
29 /* The strerror() function can return NULL for errno values that are
30    out of range.  Provide a "safe" version that always returns a
31    printable string.
32
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.  */
37
38 char *
39 safe_strerror (int errnum)
40 {
41   static char *buffer;
42   int len;
43
44   if (errnum >= 0 && errnum < sys_nerr)
45     return strerror (errnum);
46
47   if (buffer)
48     {
49       LocalFree (buffer);
50       buffer = NULL;
51     }
52
53   if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
54                      | FORMAT_MESSAGE_FROM_SYSTEM,
55                      NULL, errnum,
56                      MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
57                      (LPTSTR) &buffer, 0, NULL) == 0)
58     {
59       static char buf[32];
60       xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum);
61       return buf;
62     }
63
64   /* Windows error messages end with a period and a CR-LF; strip that
65      out.  */
66   len = strlen (buffer);
67   if (len > 3 && strcmp (buffer + len - 3, ".\r\n") == 0)
68     buffer[len - 3] = '\0';
69
70   return buffer;
71 }
72
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.
76
77    The arguments to this function are the same as the traditional
78    arguments to select on POSIX platforms.  */
79
80 int
81 gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
82             struct timeval *timeout)
83 {
84   static HANDLE never_handle;
85   HANDLE handles[MAXIMUM_WAIT_OBJECTS];
86   HANDLE h;
87   DWORD event;
88   DWORD num_handles;
89   int fd;
90   int num_ready;
91   int indx;
92
93   num_ready = 0;
94   num_handles = 0;
95   for (fd = 0; fd < n; ++fd)
96     {
97       HANDLE read = NULL, except = NULL;
98       struct serial *scb;
99
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));
104
105       if ((!readfds || !FD_ISSET (fd, readfds))
106           && (!exceptfds || !FD_ISSET (fd, exceptfds)))
107         continue;
108       h = (HANDLE) _get_osfhandle (fd);
109
110       scb = serial_for_fd (fd);
111       if (scb)
112         serial_wait_handle (scb, &read, &except);
113
114       if (read == NULL)
115         read = h;
116       if (except == NULL)
117         {
118           if (!never_handle)
119             never_handle = CreateEvent (0, FALSE, FALSE, 0);
120
121           except = never_handle;
122         }
123
124       if (readfds && FD_ISSET (fd, readfds))
125         {
126           gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
127           handles[num_handles++] = read;
128         }
129
130       if (exceptfds && FD_ISSET (fd, exceptfds))
131         {
132           gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
133           handles[num_handles++] = except;
134         }
135     }
136   /* If we don't need to wait for any handles, we are done.  */
137   if (!num_handles)
138     {
139       if (timeout)
140         Sleep (timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
141
142       return 0;
143     }
144
145   event = WaitForMultipleObjects (num_handles,
146                                   handles,
147                                   FALSE,
148                                   timeout
149                                   ? (timeout->tv_sec * 1000
150                                      + timeout->tv_usec / 1000)
151                                   : INFINITE);
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)
158     return -1;
159   if (event == WAIT_TIMEOUT)
160     return 0;
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)
165     {
166       HANDLE fd_h;
167       struct serial *scb;
168
169       if ((!readfds || !FD_ISSET (fd, readfds))
170           && (!exceptfds || !FD_ISSET (fd, exceptfds)))
171         continue;
172
173       if (readfds && FD_ISSET (fd, readfds))
174         {
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);
180           else
181             num_ready++;
182         }
183
184       if (exceptfds && FD_ISSET (fd, exceptfds))
185         {
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);
191           else
192             num_ready++;
193         }
194
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);
198       if (scb)
199         serial_done_wait_handle (scb);
200     }
201
202   return num_ready;
203 }