1 /***************************************************************************
2 * Copyright (C) 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
24 /* DANGER!!!! These must be defined *BEFORE* replacements.h and the malloc() macro!!!! */
31 * will alloc memory and clear it
33 void *clear_malloc(size_t size)
35 void *t = malloc(size);
37 memset(t, 0x00, size);
41 void *fill_malloc(size_t size)
43 void *t = malloc(size);
45 /* We want to initialize memory to some known bad state.
46 * 0 and 0xff yields 0 and -1 as integers, which often
47 * have meaningful values. 0x5555... is not often a valid
48 * integer and is quite easily spotted in the debugger
49 * also it is almost certainly an invalid address */
50 memset(t, 0x55, size);
55 #define IN_REPLACEMENTS_C
67 /* replacements for gettimeofday */
68 #ifndef HAVE_GETTIMEOFDAY
74 #define EPOCHFILETIME (116444736000000000i64)
76 #define EPOCHFILETIME (116444736000000000LL)
79 int gettimeofday(struct timeval *tv, struct timezone *tz)
87 GetSystemTimeAsFileTime(&ft);
88 li.LowPart = ft.dwLowDateTime;
89 li.HighPart = ft.dwHighDateTime;
90 t = li.QuadPart; /* In 100-nanosecond intervals */
91 t -= EPOCHFILETIME; /* Offset to the Epoch time */
92 t /= 10; /* In microseconds */
93 tv->tv_sec = (long)(t / 1000000);
94 tv->tv_usec = (long)(t % 1000000);
102 tz->tz_minuteswest = _timezone / 60;
103 tz->tz_dsttime = _daylight;
110 #endif /* HAVE_GETTIMEOFDAY */
113 size_t strnlen(const char *s, size_t maxlen)
115 const char *end = (const char *)memchr(s, '\0', maxlen);
116 return end ? (size_t) (end - s) : maxlen;
121 char *strndup(const char *s, size_t n)
123 size_t len = strnlen(s, n);
124 char *new = malloc(len + 1);
130 return (char *) memcpy(new, s, len);
135 int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
137 DWORD ms_total, limit;
138 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
139 int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
140 int n_handles = 0, i;
141 fd_set sock_read, sock_write, sock_except;
142 fd_set aread, awrite, aexcept;
143 int sock_max_fd = -1;
144 struct timeval tvslice;
147 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
149 /* calculate how long we need to wait in milliseconds */
153 ms_total = tv->tv_sec * 1000;
154 ms_total += tv->tv_usec / 1000;
158 FD_ZERO(&sock_write);
159 FD_ZERO(&sock_except);
161 /* build an array of handles for non-sockets */
162 for (i = 0; i < max_fd; i++) {
163 if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {
164 intptr_t handle = (intptr_t) _get_osfhandle(i);
165 handles[n_handles] = (HANDLE)handle;
166 if (handles[n_handles] == INVALID_HANDLE_VALUE) {
168 if (SAFE_FD_ISSET(i, rfds))
169 FD_SET(i, &sock_read);
170 if (SAFE_FD_ISSET(i, wfds))
171 FD_SET(i, &sock_write);
172 if (SAFE_FD_ISSET(i, efds))
173 FD_SET(i, &sock_except);
177 handle_slot_to_fd[n_handles] = i;
183 if (n_handles == 0) {
184 /* plain sockets only - let winsock handle the whole thing */
185 return select(max_fd, rfds, wfds, efds, tv);
188 /* mixture of handles and sockets; lets multiplex between
189 * winsock and waiting on the handles */
195 limit = GetTickCount() + ms_total;
199 if (sock_max_fd >= 0) {
200 /* overwrite the zero'd sets here; the select call
201 * will clear those that are not active */
204 aexcept = sock_except;
207 tvslice.tv_usec = 1000;
209 retcode = select(sock_max_fd + 1, &aread, &awrite, &aexcept, &tvslice);
216 wret = MsgWaitForMultipleObjects(n_handles,
222 if (wret == WAIT_TIMEOUT) {
223 /* set retcode to 0; this is the default.
224 * select() may have set it to something else,
225 * in which case we leave it alone, so this branch
228 } else if (wret == WAIT_FAILED) {
234 for (i = 0; i < n_handles; i++) {
235 if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
236 if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
238 intptr_t handle = (intptr_t) _get_osfhandle(
239 handle_slot_to_fd[i]);
241 if (PeekNamedPipe((HANDLE)handle, NULL, 0,
242 NULL, &dwBytes, NULL)) {
243 /* check to see if gdb pipe has data available */
245 FD_SET(handle_slot_to_fd[i], &aread);
249 FD_SET(handle_slot_to_fd[i], &aread);
253 if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
254 FD_SET(handle_slot_to_fd[i], &awrite);
257 if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
258 FD_SET(handle_slot_to_fd[i], &aexcept);
265 } while (retcode == 0 && (ms_total == INFINITE || GetTickCount() < limit));
278 #if defined HAVE_LIBUSB1 && !defined HAVE_LIBUSB_ERROR_NAME
280 /* Verbatim from git://git.libusb.org/libusb.git tag 1.0.9
281 * The libusb_error enum is compatible down to v0.9.1
283 const char *libusb_error_name(int error_code)
285 enum libusb_error error = error_code;
288 return "LIBUSB_SUCCESS";
289 case LIBUSB_ERROR_IO:
290 return "LIBUSB_ERROR_IO";
291 case LIBUSB_ERROR_INVALID_PARAM:
292 return "LIBUSB_ERROR_INVALID_PARAM";
293 case LIBUSB_ERROR_ACCESS:
294 return "LIBUSB_ERROR_ACCESS";
295 case LIBUSB_ERROR_NO_DEVICE:
296 return "LIBUSB_ERROR_NO_DEVICE";
297 case LIBUSB_ERROR_NOT_FOUND:
298 return "LIBUSB_ERROR_NOT_FOUND";
299 case LIBUSB_ERROR_BUSY:
300 return "LIBUSB_ERROR_BUSY";
301 case LIBUSB_ERROR_TIMEOUT:
302 return "LIBUSB_ERROR_TIMEOUT";
303 case LIBUSB_ERROR_OVERFLOW:
304 return "LIBUSB_ERROR_OVERFLOW";
305 case LIBUSB_ERROR_PIPE:
306 return "LIBUSB_ERROR_PIPE";
307 case LIBUSB_ERROR_INTERRUPTED:
308 return "LIBUSB_ERROR_INTERRUPTED";
309 case LIBUSB_ERROR_NO_MEM:
310 return "LIBUSB_ERROR_NO_MEM";
311 case LIBUSB_ERROR_NOT_SUPPORTED:
312 return "LIBUSB_ERROR_NOT_SUPPORTED";
313 case LIBUSB_ERROR_OTHER:
314 return "LIBUSB_ERROR_OTHER";
316 return "**UNKNOWN**";