mingw-hdep.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* Host support routines for MinGW, for GDB, the GNU debugger.
  2. Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "main.h"
  16. #include "serial.h"
  17. #include "gdbsupport/event-loop.h"
  18. #include "gdbsupport/gdb_select.h"
  19. #include <windows.h>
  20. /* Return an absolute file name of the running GDB, if possible, or
  21. ARGV0 if not. The return value is in malloc'ed storage. */
  22. char *
  23. windows_get_absolute_argv0 (const char *argv0)
  24. {
  25. char full_name[PATH_MAX];
  26. if (GetModuleFileName (NULL, full_name, PATH_MAX))
  27. return xstrdup (full_name);
  28. return xstrdup (argv0);
  29. }
  30. /* Wrapper for select. On Windows systems, where the select interface
  31. only works for sockets, this uses the GDB serial abstraction to
  32. handle sockets, consoles, pipes, and serial ports.
  33. The arguments to this function are the same as the traditional
  34. arguments to select on POSIX platforms. */
  35. int
  36. gdb_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  37. struct timeval *timeout)
  38. {
  39. static HANDLE never_handle;
  40. HANDLE handles[MAXIMUM_WAIT_OBJECTS];
  41. HANDLE h;
  42. DWORD event;
  43. DWORD num_handles;
  44. /* SCBS contains serial control objects corresponding to file
  45. descriptors in READFDS and WRITEFDS. */
  46. struct serial *scbs[MAXIMUM_WAIT_OBJECTS];
  47. /* The number of valid entries in SCBS. */
  48. size_t num_scbs;
  49. int fd;
  50. int num_ready;
  51. size_t indx;
  52. if (n == 0)
  53. {
  54. /* The MS API says that the first argument to
  55. WaitForMultipleObjects cannot be zero. That's why we just
  56. use a regular Sleep here. */
  57. if (timeout != NULL)
  58. Sleep (timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
  59. return 0;
  60. }
  61. num_ready = 0;
  62. num_handles = 0;
  63. num_scbs = 0;
  64. for (fd = 0; fd < n; ++fd)
  65. {
  66. HANDLE read = NULL, except = NULL;
  67. struct serial *scb;
  68. /* There is no support yet for WRITEFDS. At present, this isn't
  69. used by GDB -- but we do not want to silently ignore WRITEFDS
  70. if something starts using it. */
  71. gdb_assert (!writefds || !FD_ISSET (fd, writefds));
  72. if ((!readfds || !FD_ISSET (fd, readfds))
  73. && (!exceptfds || !FD_ISSET (fd, exceptfds)))
  74. continue;
  75. scb = serial_for_fd (fd);
  76. if (scb)
  77. {
  78. serial_wait_handle (scb, &read, &except);
  79. scbs[num_scbs++] = scb;
  80. }
  81. if (read == NULL)
  82. read = (HANDLE) _get_osfhandle (fd);
  83. if (except == NULL)
  84. {
  85. if (!never_handle)
  86. never_handle = CreateEvent (0, FALSE, FALSE, 0);
  87. except = never_handle;
  88. }
  89. if (readfds && FD_ISSET (fd, readfds))
  90. {
  91. gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
  92. handles[num_handles++] = read;
  93. }
  94. if (exceptfds && FD_ISSET (fd, exceptfds))
  95. {
  96. gdb_assert (num_handles < MAXIMUM_WAIT_OBJECTS);
  97. handles[num_handles++] = except;
  98. }
  99. }
  100. gdb_assert (num_handles <= MAXIMUM_WAIT_OBJECTS);
  101. event = WaitForMultipleObjects (num_handles,
  102. handles,
  103. FALSE,
  104. timeout
  105. ? (timeout->tv_sec * 1000
  106. + timeout->tv_usec / 1000)
  107. : INFINITE);
  108. /* EVENT can only be a value in the WAIT_ABANDONED_0 range if the
  109. HANDLES included an abandoned mutex. Since GDB doesn't use
  110. mutexes, that should never occur. */
  111. gdb_assert (!(WAIT_ABANDONED_0 <= event
  112. && event < WAIT_ABANDONED_0 + num_handles));
  113. /* We no longer need the helper threads to check for activity. */
  114. for (indx = 0; indx < num_scbs; ++indx)
  115. serial_done_wait_handle (scbs[indx]);
  116. if (event == WAIT_FAILED)
  117. return -1;
  118. if (event == WAIT_TIMEOUT)
  119. return 0;
  120. /* Run through the READFDS, clearing bits corresponding to descriptors
  121. for which input is unavailable. */
  122. h = handles[event - WAIT_OBJECT_0];
  123. for (fd = 0, indx = 0; fd < n; ++fd)
  124. {
  125. HANDLE fd_h;
  126. if ((!readfds || !FD_ISSET (fd, readfds))
  127. && (!exceptfds || !FD_ISSET (fd, exceptfds)))
  128. continue;
  129. if (readfds && FD_ISSET (fd, readfds))
  130. {
  131. fd_h = handles[indx++];
  132. /* This handle might be ready, even though it wasn't the handle
  133. returned by WaitForMultipleObjects. */
  134. if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
  135. FD_CLR (fd, readfds);
  136. else
  137. num_ready++;
  138. }
  139. if (exceptfds && FD_ISSET (fd, exceptfds))
  140. {
  141. fd_h = handles[indx++];
  142. /* This handle might be ready, even though it wasn't the handle
  143. returned by WaitForMultipleObjects. */
  144. if (fd_h != h && WaitForSingleObject (fd_h, 0) != WAIT_OBJECT_0)
  145. FD_CLR (fd, exceptfds);
  146. else
  147. num_ready++;
  148. }
  149. }
  150. return num_ready;
  151. }
  152. /* Map COLOR's RGB triplet, with 8 bits per component, into 16 Windows
  153. console colors, where each component has just 1 bit, plus a single
  154. intensity bit which affects all 3 components. */
  155. static int
  156. rgb_to_16colors (const ui_file_style::color &color)
  157. {
  158. uint8_t rgb[3];
  159. color.get_rgb (rgb);
  160. int retval = 0;
  161. for (int i = 0; i < 3; i++)
  162. {
  163. /* Subdivide 256 possible values of each RGB component into 3
  164. regions: no color, normal color, bright color. 256 / 3 = 85,
  165. but ui-style.c follows xterm and uses 92 for R and G
  166. components of the bright-blue color, so we bias the divisor a
  167. bit to have the bright colors between 9 and 15 identical to
  168. what ui-style.c expects. */
  169. int bits = rgb[i] / 93;
  170. retval |= ((bits > 0) << (2 - i)) | ((bits > 1) << 3);
  171. }
  172. return retval;
  173. }
  174. /* Zero if not yet initialized, 1 if stdout is a console device, else -1. */
  175. static int mingw_console_initialized;
  176. /* Handle to stdout . */
  177. static HANDLE hstdout = INVALID_HANDLE_VALUE;
  178. /* Text attribute to use for normal text (the "none" pseudo-color). */
  179. static SHORT norm_attr;
  180. /* The most recently applied style. */
  181. static ui_file_style last_style;
  182. /* Alternative for the libc 'fputs' which handles embedded SGR
  183. sequences in support of styling. */
  184. int
  185. gdb_console_fputs (const char *linebuf, FILE *fstream)
  186. {
  187. if (!mingw_console_initialized)
  188. {
  189. hstdout = (HANDLE)_get_osfhandle (fileno (fstream));
  190. DWORD cmode;
  191. CONSOLE_SCREEN_BUFFER_INFO csbi;
  192. if (hstdout != INVALID_HANDLE_VALUE
  193. && GetConsoleMode (hstdout, &cmode) != 0
  194. && GetConsoleScreenBufferInfo (hstdout, &csbi))
  195. {
  196. norm_attr = csbi.wAttributes;
  197. mingw_console_initialized = 1;
  198. }
  199. else if (hstdout != INVALID_HANDLE_VALUE)
  200. mingw_console_initialized = -1; /* valid, but not a console device */
  201. }
  202. /* If our stdout is not a console device, let the default 'fputs'
  203. handle the task. */
  204. if (mingw_console_initialized <= 0)
  205. return 0;
  206. /* Mapping between 8 ANSI colors and Windows console attributes. */
  207. static int fg_color[] = {
  208. 0, /* black */
  209. FOREGROUND_RED, /* red */
  210. FOREGROUND_GREEN, /* green */
  211. FOREGROUND_GREEN | FOREGROUND_RED, /* yellow */
  212. FOREGROUND_BLUE, /* blue */
  213. FOREGROUND_BLUE | FOREGROUND_RED, /* magenta */
  214. FOREGROUND_BLUE | FOREGROUND_GREEN, /* cyan */
  215. FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* gray */
  216. };
  217. static int bg_color[] = {
  218. 0, /* black */
  219. BACKGROUND_RED, /* red */
  220. BACKGROUND_GREEN, /* green */
  221. BACKGROUND_GREEN | BACKGROUND_RED, /* yellow */
  222. BACKGROUND_BLUE, /* blue */
  223. BACKGROUND_BLUE | BACKGROUND_RED, /* magenta */
  224. BACKGROUND_BLUE | BACKGROUND_GREEN, /* cyan */
  225. BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE /* gray */
  226. };
  227. ui_file_style style = last_style;
  228. unsigned char c;
  229. size_t n_read;
  230. for ( ; (c = *linebuf) != 0; linebuf += n_read)
  231. {
  232. if (c == '\033')
  233. {
  234. fflush (fstream);
  235. bool parsed = style.parse (linebuf, &n_read);
  236. if (n_read <= 0) /* should never happen */
  237. n_read = 1;
  238. if (!parsed)
  239. {
  240. /* This means we silently swallow SGR sequences we
  241. cannot parse. */
  242. continue;
  243. }
  244. /* Colors. */
  245. const ui_file_style::color &fg = style.get_foreground ();
  246. const ui_file_style::color &bg = style.get_background ();
  247. int fgcolor, bgcolor, bright, inverse;
  248. if (fg.is_none ())
  249. fgcolor = norm_attr & 15;
  250. else if (fg.is_basic ())
  251. fgcolor = fg_color[fg.get_value () & 15];
  252. else
  253. fgcolor = rgb_to_16colors (fg);
  254. if (bg.is_none ())
  255. bgcolor = norm_attr & (15 << 4);
  256. else if (bg.is_basic ())
  257. bgcolor = bg_color[bg.get_value () & 15];
  258. else
  259. bgcolor = rgb_to_16colors (bg) << 4;
  260. /* Intensity. */
  261. switch (style.get_intensity ())
  262. {
  263. case ui_file_style::NORMAL:
  264. case ui_file_style::DIM:
  265. bright = 0;
  266. break;
  267. case ui_file_style::BOLD:
  268. bright = 1;
  269. break;
  270. default:
  271. gdb_assert_not_reached ("invalid intensity");
  272. }
  273. /* Inverse video. */
  274. if (style.is_reverse ())
  275. inverse = 1;
  276. else
  277. inverse = 0;
  278. /* Construct the attribute. */
  279. if (inverse)
  280. {
  281. int t = fgcolor;
  282. fgcolor = (bgcolor >> 4);
  283. bgcolor = (t << 4);
  284. }
  285. if (bright)
  286. fgcolor |= FOREGROUND_INTENSITY;
  287. SHORT attr = (bgcolor & (15 << 4)) | (fgcolor & 15);
  288. /* Apply the attribute. */
  289. SetConsoleTextAttribute (hstdout, attr);
  290. }
  291. else
  292. {
  293. /* When we are about to write newline, we need to clear to
  294. EOL with the normal attribute, to avoid spilling the
  295. colors to the next screen line. We assume here that no
  296. non-default attribute extends beyond the newline. */
  297. if (c == '\n')
  298. {
  299. DWORD nchars;
  300. COORD start_pos;
  301. DWORD written;
  302. CONSOLE_SCREEN_BUFFER_INFO csbi;
  303. fflush (fstream);
  304. GetConsoleScreenBufferInfo (hstdout, &csbi);
  305. if (csbi.wAttributes != norm_attr)
  306. {
  307. start_pos = csbi.dwCursorPosition;
  308. nchars = csbi.dwSize.X - start_pos.X;
  309. FillConsoleOutputAttribute (hstdout, norm_attr, nchars,
  310. start_pos, &written);
  311. FillConsoleOutputCharacter (hstdout, ' ', nchars,
  312. start_pos, &written);
  313. }
  314. }
  315. fputc (c, fstream);
  316. n_read = 1;
  317. }
  318. }
  319. last_style = style;
  320. return 1;
  321. }