ser-event.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Serial interface for a selectable event.
  2. Copyright (C) 2016-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 "ser-event.h"
  16. #include "serial.h"
  17. #include "gdbsupport/filestuff.h"
  18. /* On POSIX hosts, a serial_event is basically an abstraction for the
  19. classical self-pipe trick.
  20. On Windows, a serial_event is a wrapper around a native Windows
  21. event object. Because we want to interface with gdb_select, which
  22. takes file descriptors, we need to wrap that Windows event object
  23. in a file descriptor. As _open_osfhandle can not be used with
  24. event objects, we instead create a dummy file wrap that in a file
  25. descriptor with _open_osfhandle, and pass that as selectable
  26. descriptor to callers. As Windows' gdb_select converts file
  27. descriptors back to Windows handles by calling serial->wait_handle,
  28. nothing ever actually waits on that file descriptor. */
  29. struct serial_event_state
  30. {
  31. #ifdef USE_WIN32API
  32. /* The Windows event object, created with CreateEvent. */
  33. HANDLE event;
  34. #else
  35. /* The write side of the pipe. The read side is in
  36. serial->fd. */
  37. int write_fd;
  38. #endif
  39. };
  40. /* Open a new serial event. */
  41. static int
  42. serial_event_open (struct serial *scb, const char *name)
  43. {
  44. struct serial_event_state *state;
  45. state = XNEW (struct serial_event_state);
  46. scb->state = state;
  47. #ifndef USE_WIN32API
  48. {
  49. int fds[2];
  50. if (gdb_pipe_cloexec (fds) == -1)
  51. internal_error (__FILE__, __LINE__,
  52. "creating serial event pipe failed.");
  53. fcntl (fds[0], F_SETFL, O_NONBLOCK);
  54. fcntl (fds[1], F_SETFL, O_NONBLOCK);
  55. scb->fd = fds[0];
  56. state->write_fd = fds[1];
  57. }
  58. #else
  59. {
  60. /* A dummy file object that can be wrapped in a file descriptor.
  61. We don't need to store this handle because closing the file
  62. descriptor automatically closes this. */
  63. HANDLE dummy_file;
  64. /* A manual-reset event. */
  65. state->event = CreateEvent (0, TRUE, FALSE, 0);
  66. /* The dummy file handle. Created just so we have something
  67. wrappable in a file descriptor. */
  68. dummy_file = CreateFile ("nul", 0, 0, NULL, OPEN_EXISTING, 0, NULL);
  69. scb->fd = _open_osfhandle ((intptr_t) dummy_file, 0);
  70. }
  71. #endif
  72. return 0;
  73. }
  74. static void
  75. serial_event_close (struct serial *scb)
  76. {
  77. struct serial_event_state *state = (struct serial_event_state *) scb->state;
  78. close (scb->fd);
  79. #ifndef USE_WIN32API
  80. close (state->write_fd);
  81. #else
  82. CloseHandle (state->event);
  83. #endif
  84. scb->fd = -1;
  85. xfree (state);
  86. scb->state = NULL;
  87. }
  88. #ifdef USE_WIN32API
  89. /* Implementation of the wait_handle method. Returns the native
  90. Windows event object handle. */
  91. static void
  92. serial_event_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
  93. {
  94. struct serial_event_state *state = (struct serial_event_state *) scb->state;
  95. *read = state->event;
  96. }
  97. #endif
  98. /* The serial_ops for struct serial_event objects. Note we never
  99. register this serial type with serial_add_interface, because this
  100. is internal implementation detail never to be used by remote
  101. targets for protocol transport. */
  102. static const struct serial_ops serial_event_ops =
  103. {
  104. "event",
  105. serial_event_open,
  106. serial_event_close,
  107. NULL, /* fdopen */
  108. NULL, /* readchar */
  109. NULL, /* write */
  110. NULL, /* flush_output */
  111. NULL, /* flush_input */
  112. NULL, /* send_break */
  113. NULL, /* go_raw */
  114. NULL, /* get_tty_state */
  115. NULL, /* copy_tty_state */
  116. NULL, /* set_tty_state */
  117. NULL, /* print_tty_state */
  118. NULL, /* setbaudrate */
  119. NULL, /* setstopbits */
  120. NULL, /* setparity */
  121. NULL, /* drain_output */
  122. NULL, /* async */
  123. NULL, /* read_prim */
  124. NULL, /* write_prim */
  125. NULL, /* avail */
  126. #ifdef USE_WIN32API
  127. serial_event_wait_handle,
  128. #endif
  129. };
  130. /* See ser-event.h. */
  131. struct serial_event *
  132. make_serial_event (void)
  133. {
  134. return (struct serial_event *) serial_open_ops (&serial_event_ops);
  135. }
  136. /* See ser-event.h. */
  137. int
  138. serial_event_fd (struct serial_event *event)
  139. {
  140. struct serial *ser = (struct serial *) event;
  141. return ser->fd;
  142. }
  143. /* See ser-event.h. */
  144. void
  145. serial_event_set (struct serial_event *event)
  146. {
  147. struct serial *ser = (struct serial *) event;
  148. struct serial_event_state *state = (struct serial_event_state *) ser->state;
  149. #ifndef USE_WIN32API
  150. int r;
  151. char c = '+'; /* Anything. */
  152. do
  153. {
  154. r = write (state->write_fd, &c, 1);
  155. }
  156. while (r < 0 && errno == EINTR);
  157. #else
  158. SetEvent (state->event);
  159. #endif
  160. }
  161. /* See ser-event.h. */
  162. void
  163. serial_event_clear (struct serial_event *event)
  164. {
  165. struct serial *ser = (struct serial *) event;
  166. #ifndef USE_WIN32API
  167. int r;
  168. do
  169. {
  170. char c;
  171. r = read (ser->fd, &c, 1);
  172. }
  173. while (r > 0 || (r < 0 && errno == EINTR));
  174. #else
  175. struct serial_event_state *state = (struct serial_event_state *) ser->state;
  176. ResetEvent (state->event);
  177. #endif
  178. }