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
68 /* replacements for gettimeofday */
69 #ifndef HAVE_GETTIMEOFDAY
75 #define EPOCHFILETIME (116444736000000000i64)
77 #define EPOCHFILETIME (116444736000000000LL)
80 int gettimeofday(struct timeval *tv, struct timezone *tz)
88 GetSystemTimeAsFileTime(&ft);
89 li.LowPart = ft.dwLowDateTime;
90 li.HighPart = ft.dwHighDateTime;
91 t = li.QuadPart; /* In 100-nanosecond intervals */
92 t -= EPOCHFILETIME; /* Offset to the Epoch time */
93 t /= 10; /* In microseconds */
94 tv->tv_sec = (long)(t / 1000000);
95 tv->tv_usec = (long)(t % 1000000);
103 tz->tz_minuteswest = _timezone / 60;
104 tz->tz_dsttime = _daylight;
111 #endif /* HAVE_GETTIMEOFDAY */
114 size_t strnlen(const char *s, size_t maxlen)
116 const char *end = (const char *)memchr(s, '\0', maxlen);
117 return end ? (size_t) (end - s) : maxlen;
122 char *strndup(const char *s, size_t n)
124 size_t len = strnlen(s, n);
125 char *new = malloc(len + 1);
131 return (char *) memcpy(new, s, len);
136 int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
138 DWORD ms_total, limit;
139 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
140 int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
141 int n_handles = 0, i;
142 fd_set sock_read, sock_write, sock_except;
143 fd_set aread, awrite, aexcept;
144 int sock_max_fd = -1;
145 struct timeval tvslice;
148 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
150 /* calculate how long we need to wait in milliseconds */
154 ms_total = tv->tv_sec * 1000;
155 ms_total += tv->tv_usec / 1000;
159 FD_ZERO(&sock_write);
160 FD_ZERO(&sock_except);
162 /* build an array of handles for non-sockets */
163 for (i = 0; i < max_fd; i++) {
164 if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {
165 intptr_t handle = (intptr_t) _get_osfhandle(i);
166 handles[n_handles] = (HANDLE)handle;
167 if (handles[n_handles] == INVALID_HANDLE_VALUE) {
169 if (SAFE_FD_ISSET(i, rfds))
170 FD_SET(i, &sock_read);
171 if (SAFE_FD_ISSET(i, wfds))
172 FD_SET(i, &sock_write);
173 if (SAFE_FD_ISSET(i, efds))
174 FD_SET(i, &sock_except);
178 handle_slot_to_fd[n_handles] = i;
184 if (n_handles == 0) {
185 /* plain sockets only - let winsock handle the whole thing */
186 return select(max_fd, rfds, wfds, efds, tv);
189 /* mixture of handles and sockets; lets multiplex between
190 * winsock and waiting on the handles */
196 limit = GetTickCount() + ms_total;
200 if (sock_max_fd >= 0) {
201 /* overwrite the zero'd sets here; the select call
202 * will clear those that are not active */
205 aexcept = sock_except;
208 tvslice.tv_usec = 1000;
210 retcode = select(sock_max_fd + 1, &aread, &awrite, &aexcept, &tvslice);
217 wret = MsgWaitForMultipleObjects(n_handles,
223 if (wret == WAIT_TIMEOUT) {
224 /* set retcode to 0; this is the default.
225 * select() may have set it to something else,
226 * in which case we leave it alone, so this branch
229 } else if (wret == WAIT_FAILED) {
235 for (i = 0; i < n_handles; i++) {
236 if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
237 if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
239 intptr_t handle = (intptr_t) _get_osfhandle(
240 handle_slot_to_fd[i]);
242 if (PeekNamedPipe((HANDLE)handle, NULL, 0,
243 NULL, &bytes, NULL)) {
244 /* check to see if gdb pipe has data available */
246 FD_SET(handle_slot_to_fd[i], &aread);
250 FD_SET(handle_slot_to_fd[i], &aread);
254 if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
255 FD_SET(handle_slot_to_fd[i], &awrite);
258 if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
259 FD_SET(handle_slot_to_fd[i], &aexcept);
266 } while (retcode == 0 && (ms_total == INFINITE || GetTickCount() < limit));
279 #if defined HAVE_LIBUSB1 && !defined HAVE_LIBUSB_ERROR_NAME
281 /* Verbatim from git://git.libusb.org/libusb.git tag 1.0.9
282 * The libusb_error enum is compatible down to v0.9.1
284 const char *libusb_error_name(int error_code)
286 enum libusb_error error = error_code;
289 return "LIBUSB_SUCCESS";
290 case LIBUSB_ERROR_IO:
291 return "LIBUSB_ERROR_IO";
292 case LIBUSB_ERROR_INVALID_PARAM:
293 return "LIBUSB_ERROR_INVALID_PARAM";
294 case LIBUSB_ERROR_ACCESS:
295 return "LIBUSB_ERROR_ACCESS";
296 case LIBUSB_ERROR_NO_DEVICE:
297 return "LIBUSB_ERROR_NO_DEVICE";
298 case LIBUSB_ERROR_NOT_FOUND:
299 return "LIBUSB_ERROR_NOT_FOUND";
300 case LIBUSB_ERROR_BUSY:
301 return "LIBUSB_ERROR_BUSY";
302 case LIBUSB_ERROR_TIMEOUT:
303 return "LIBUSB_ERROR_TIMEOUT";
304 case LIBUSB_ERROR_OVERFLOW:
305 return "LIBUSB_ERROR_OVERFLOW";
306 case LIBUSB_ERROR_PIPE:
307 return "LIBUSB_ERROR_PIPE";
308 case LIBUSB_ERROR_INTERRUPTED:
309 return "LIBUSB_ERROR_INTERRUPTED";
310 case LIBUSB_ERROR_NO_MEM:
311 return "LIBUSB_ERROR_NO_MEM";
312 case LIBUSB_ERROR_NOT_SUPPORTED:
313 return "LIBUSB_ERROR_NOT_SUPPORTED";
314 case LIBUSB_ERROR_OTHER:
315 return "LIBUSB_ERROR_OTHER";
317 return "**UNKNOWN**";