You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

320 lines
8.5 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2006 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2008 by Spencer Oliver *
  9. * spen@spen-soft.co.uk *
  10. * *
  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. *
  15. * *
  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. *
  20. * *
  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!!!! */
  25. #include <stdlib.h>
  26. #include <string.h>
  27. /*
  28. * clear_malloc
  29. *
  30. * will alloc memory and clear it
  31. */
  32. void *clear_malloc(size_t size)
  33. {
  34. void *t = malloc(size);
  35. if (t)
  36. memset(t, 0x00, size);
  37. return t;
  38. }
  39. void *fill_malloc(size_t size)
  40. {
  41. void *t = malloc(size);
  42. if (t) {
  43. /* We want to initialize memory to some known bad state.
  44. * 0 and 0xff yields 0 and -1 as integers, which often
  45. * have meaningful values. 0x5555... is not often a valid
  46. * integer and is quite easily spotted in the debugger
  47. * also it is almost certainly an invalid address */
  48. memset(t, 0x55, size);
  49. }
  50. return t;
  51. }
  52. #define IN_REPLACEMENTS_C
  53. #ifdef HAVE_CONFIG_H
  54. #include "config.h"
  55. #endif
  56. #ifdef HAVE_STRINGS_H
  57. #include <strings.h>
  58. #endif
  59. #ifdef _WIN32
  60. #include <io.h>
  61. #include <winsock2.h>
  62. #endif
  63. /* replacements for gettimeofday */
  64. #ifndef HAVE_GETTIMEOFDAY
  65. /* Windows */
  66. #ifdef _WIN32
  67. #ifndef __GNUC__
  68. #define EPOCHFILETIME (116444736000000000i64)
  69. #else
  70. #define EPOCHFILETIME (116444736000000000LL)
  71. #endif
  72. int gettimeofday(struct timeval *tv, struct timezone *tz)
  73. {
  74. FILETIME ft;
  75. LARGE_INTEGER li;
  76. __int64 t;
  77. static int tzflag;
  78. if (tv) {
  79. GetSystemTimeAsFileTime(&ft);
  80. li.LowPart = ft.dwLowDateTime;
  81. li.HighPart = ft.dwHighDateTime;
  82. t = li.QuadPart; /* In 100-nanosecond intervals */
  83. t -= EPOCHFILETIME; /* Offset to the Epoch time */
  84. t /= 10; /* In microseconds */
  85. tv->tv_sec = (long)(t / 1000000);
  86. tv->tv_usec = (long)(t % 1000000);
  87. }
  88. if (tz) {
  89. if (!tzflag) {
  90. _tzset();
  91. tzflag++;
  92. }
  93. tz->tz_minuteswest = _timezone / 60;
  94. tz->tz_dsttime = _daylight;
  95. }
  96. return 0;
  97. }
  98. #endif /* _WIN32 */
  99. #endif /* HAVE_GETTIMEOFDAY */
  100. #ifndef HAVE_STRNLEN
  101. size_t strnlen(const char *s, size_t maxlen)
  102. {
  103. const char *end = (const char *)memchr(s, '\0', maxlen);
  104. return end ? (size_t) (end - s) : maxlen;
  105. }
  106. #endif
  107. #ifndef HAVE_STRNDUP
  108. char *strndup(const char *s, size_t n)
  109. {
  110. size_t len = strnlen(s, n);
  111. char *new = malloc(len + 1);
  112. if (!new)
  113. return NULL;
  114. new[len] = '\0';
  115. return (char *) memcpy(new, s, len);
  116. }
  117. #endif
  118. #ifdef _WIN32
  119. int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
  120. {
  121. DWORD ms_total, limit;
  122. HANDLE handles[MAXIMUM_WAIT_OBJECTS];
  123. int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
  124. int n_handles = 0, i;
  125. fd_set sock_read, sock_write, sock_except;
  126. fd_set aread, awrite, aexcept;
  127. int sock_max_fd = -1;
  128. struct timeval tvslice;
  129. int retcode;
  130. #define SAFE_FD_ISSET(fd, set) (set && FD_ISSET(fd, set))
  131. /* calculate how long we need to wait in milliseconds */
  132. if (!tv)
  133. ms_total = INFINITE;
  134. else {
  135. ms_total = tv->tv_sec * 1000;
  136. ms_total += tv->tv_usec / 1000;
  137. }
  138. FD_ZERO(&sock_read);
  139. FD_ZERO(&sock_write);
  140. FD_ZERO(&sock_except);
  141. /* build an array of handles for non-sockets */
  142. for (i = 0; i < max_fd; i++) {
  143. if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {
  144. intptr_t handle = (intptr_t) _get_osfhandle(i);
  145. handles[n_handles] = (HANDLE)handle;
  146. if (handles[n_handles] == INVALID_HANDLE_VALUE) {
  147. /* socket */
  148. if (SAFE_FD_ISSET(i, rfds))
  149. FD_SET(i, &sock_read);
  150. if (SAFE_FD_ISSET(i, wfds))
  151. FD_SET(i, &sock_write);
  152. if (SAFE_FD_ISSET(i, efds))
  153. FD_SET(i, &sock_except);
  154. if (i > sock_max_fd)
  155. sock_max_fd = i;
  156. } else {
  157. handle_slot_to_fd[n_handles] = i;
  158. n_handles++;
  159. }
  160. }
  161. }
  162. if (n_handles == 0) {
  163. /* plain sockets only - let winsock handle the whole thing */
  164. return select(max_fd, rfds, wfds, efds, tv);
  165. }
  166. /* mixture of handles and sockets; lets multiplex between
  167. * winsock and waiting on the handles */
  168. FD_ZERO(&aread);
  169. FD_ZERO(&awrite);
  170. FD_ZERO(&aexcept);
  171. limit = GetTickCount() + ms_total;
  172. do {
  173. retcode = 0;
  174. if (sock_max_fd >= 0) {
  175. /* overwrite the zero'd sets here; the select call
  176. * will clear those that are not active */
  177. aread = sock_read;
  178. awrite = sock_write;
  179. aexcept = sock_except;
  180. tvslice.tv_sec = 0;
  181. tvslice.tv_usec = 1000;
  182. retcode = select(sock_max_fd + 1, &aread, &awrite, &aexcept, &tvslice);
  183. }
  184. if (n_handles > 0) {
  185. /* check handles */
  186. DWORD wret;
  187. wret = MsgWaitForMultipleObjects(n_handles,
  188. handles,
  189. FALSE,
  190. retcode > 0 ? 0 : 1,
  191. QS_ALLEVENTS);
  192. if (wret == WAIT_TIMEOUT) {
  193. /* set retcode to 0; this is the default.
  194. * select() may have set it to something else,
  195. * in which case we leave it alone, so this branch
  196. * does nothing */
  197. ;
  198. } else if (wret == WAIT_FAILED) {
  199. if (retcode == 0)
  200. retcode = -1;
  201. } else {
  202. if (retcode < 0)
  203. retcode = 0;
  204. for (i = 0; i < n_handles; i++) {
  205. if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
  206. if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
  207. DWORD bytes;
  208. intptr_t handle = (intptr_t) _get_osfhandle(
  209. handle_slot_to_fd[i]);
  210. if (PeekNamedPipe((HANDLE)handle, NULL, 0,
  211. NULL, &bytes, NULL)) {
  212. /* check to see if gdb pipe has data available */
  213. if (bytes) {
  214. FD_SET(handle_slot_to_fd[i], &aread);
  215. retcode++;
  216. }
  217. } else {
  218. FD_SET(handle_slot_to_fd[i], &aread);
  219. retcode++;
  220. }
  221. }
  222. if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
  223. FD_SET(handle_slot_to_fd[i], &awrite);
  224. retcode++;
  225. }
  226. if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
  227. FD_SET(handle_slot_to_fd[i], &aexcept);
  228. retcode++;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. } while (retcode == 0 && (ms_total == INFINITE || GetTickCount() < limit));
  235. if (rfds)
  236. *rfds = aread;
  237. if (wfds)
  238. *wfds = awrite;
  239. if (efds)
  240. *efds = aexcept;
  241. return retcode;
  242. }
  243. #endif
  244. #if defined HAVE_LIBUSB1 && !defined HAVE_LIBUSB_ERROR_NAME
  245. #include <libusb.h>
  246. /* Verbatim from git://git.libusb.org/libusb.git tag 1.0.9
  247. * The libusb_error enum is compatible down to v0.9.1
  248. */
  249. const char *libusb_error_name(int error_code)
  250. {
  251. enum libusb_error error = error_code;
  252. switch (error) {
  253. case LIBUSB_SUCCESS:
  254. return "LIBUSB_SUCCESS";
  255. case LIBUSB_ERROR_IO:
  256. return "LIBUSB_ERROR_IO";
  257. case LIBUSB_ERROR_INVALID_PARAM:
  258. return "LIBUSB_ERROR_INVALID_PARAM";
  259. case LIBUSB_ERROR_ACCESS:
  260. return "LIBUSB_ERROR_ACCESS";
  261. case LIBUSB_ERROR_NO_DEVICE:
  262. return "LIBUSB_ERROR_NO_DEVICE";
  263. case LIBUSB_ERROR_NOT_FOUND:
  264. return "LIBUSB_ERROR_NOT_FOUND";
  265. case LIBUSB_ERROR_BUSY:
  266. return "LIBUSB_ERROR_BUSY";
  267. case LIBUSB_ERROR_TIMEOUT:
  268. return "LIBUSB_ERROR_TIMEOUT";
  269. case LIBUSB_ERROR_OVERFLOW:
  270. return "LIBUSB_ERROR_OVERFLOW";
  271. case LIBUSB_ERROR_PIPE:
  272. return "LIBUSB_ERROR_PIPE";
  273. case LIBUSB_ERROR_INTERRUPTED:
  274. return "LIBUSB_ERROR_INTERRUPTED";
  275. case LIBUSB_ERROR_NO_MEM:
  276. return "LIBUSB_ERROR_NO_MEM";
  277. case LIBUSB_ERROR_NOT_SUPPORTED:
  278. return "LIBUSB_ERROR_NOT_SUPPORTED";
  279. case LIBUSB_ERROR_OTHER:
  280. return "LIBUSB_ERROR_OTHER";
  281. }
  282. return "**UNKNOWN**";
  283. }
  284. #endif