30 #include <sys/types.h> 44 #define CancelIo dyn_CancelIo 55 COMMTIMEOUTS timeouts;
57 h = CreateFile (
name, GENERIC_READ | GENERIC_WRITE, 0, NULL,
58 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
59 if (h == INVALID_HANDLE_VALUE)
65 scb->
fd = _open_osfhandle ((intptr_t) h, O_RDWR);
72 if (!SetCommMask (h, EV_RXCHAR))
78 timeouts.ReadIntervalTimeout = MAXDWORD;
79 timeouts.ReadTotalTimeoutConstant = 0;
80 timeouts.ReadTotalTimeoutMultiplier = 0;
81 timeouts.WriteTotalTimeoutConstant = 0;
82 timeouts.WriteTotalTimeoutMultiplier = 0;
83 if (!SetCommTimeouts (h, &timeouts))
93 state->
ov.hEvent = CreateEvent (0, TRUE, FALSE, 0);
107 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
109 return (FlushFileBuffers (h) != 0) ? 0 : -1;
115 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
117 return (PurgeComm (h, PURGE_TXCLEAR) != 0) ? 0 : -1;
123 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
125 return (PurgeComm (h, PURGE_RXCLEAR) != 0) ? 0 : -1;
131 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
133 if (SetCommBreak (h) == 0)
139 if (ClearCommBreak (h))
148 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
151 if (GetCommState (h, &state) == 0)
154 state.fOutxCtsFlow = FALSE;
155 state.fOutxDsrFlow = FALSE;
156 state.fDtrControl = DTR_CONTROL_ENABLE;
157 state.fDsrSensitivity = FALSE;
161 state.fAbortOnError = FALSE;
164 if (SetCommState (h, &state) == 0)
171 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
174 if (GetCommState (h, &state) == 0)
180 state.StopBits = ONESTOPBIT;
183 state.StopBits = ONE5STOPBITS;
186 state.StopBits = TWOSTOPBITS;
192 return (SetCommState (h, &state) != 0) ? 0 : -1;
200 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
203 if (GetCommState (h, &state) == 0)
209 state.Parity = NOPARITY;
210 state.fParity = FALSE;
213 state.Parity = ODDPARITY;
214 state.fParity = TRUE;
217 state.Parity = EVENPARITY;
218 state.fParity = TRUE;
222 "Incorrect parity value: %d",
parity);
226 return (SetCommState (h, &state) != 0) ? 0 : -1;
232 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
235 if (GetCommState (h, &state) == 0)
238 state.BaudRate =
rate;
240 return (SetCommState (h, &state) != 0) ? 0 : -1;
255 CloseHandle (state->
ov.hEvent);
273 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
291 if (!SetCommMask (h, 0))
292 warning (
_(
"ser_windows_wait_handle: reseting mask failed"));
294 if (!SetCommMask (h, EV_RXCHAR))
295 warning (
_(
"ser_windows_wait_handle: reseting mask failed (2)"));
303 SetEvent (state->
ov.hEvent);
308 ResetEvent (state->
ov.hEvent);
313 SetEvent (state->
ov.hEvent);
316 gdb_assert (GetLastError () == ERROR_IO_PENDING);
324 DWORD bytes_read, bytes_read_tmp;
331 WaitForSingleObject (state->
ov.hEvent, INFINITE);
333 ResetEvent (state->
ov.hEvent);
336 memset (&
ov, 0,
sizeof (OVERLAPPED));
337 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
338 h = (HANDLE) _get_osfhandle (scb->
fd);
340 if (!ReadFile (h, scb->
buf, 1, &bytes_read, &
ov))
342 if (GetLastError () != ERROR_IO_PENDING
343 || !GetOverlappedResult (h, &
ov, &bytes_read, TRUE))
347 CloseHandle (
ov.hEvent);
359 memset (&
ov, 0,
sizeof (OVERLAPPED));
360 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
361 h = (HANDLE) _get_osfhandle (scb->
fd);
362 if (!WriteFile (h, buf, len, &bytes_written, &
ov))
364 if (GetLastError () != ERROR_IO_PENDING
365 || !GetOverlappedResult (h, &
ov, &bytes_written, TRUE))
369 CloseHandle (
ov.hEvent);
370 return bytes_written;
435 HANDLE wait_events[2];
441 if (WaitForMultipleObjects (2, wait_events, FALSE, INFINITE)
464 state->
read_event = CreateEvent (NULL, FALSE, FALSE, NULL);
465 state->
except_event = CreateEvent (NULL, FALSE, FALSE, NULL);
466 state->
have_started = CreateEvent (NULL, FALSE, FALSE, NULL);
467 state->
have_stopped = CreateEvent (NULL, FALSE, FALSE, NULL);
468 state->
start_select = CreateEvent (NULL, FALSE, FALSE, NULL);
469 state->
stop_select = CreateEvent (NULL, FALSE, FALSE, NULL);
470 state->
exit_select = CreateEvent (NULL, FALSE, FALSE, NULL);
472 state->
thread = CreateThread (NULL, 0, thread_fn, scb, 0, &threadId);
484 WaitForSingleObject (state->
thread, INFINITE);
538 h = (HANDLE) _get_osfhandle (scb->
fd);
542 HANDLE wait_events[2];
553 event_index = WaitForMultipleObjects (2, wait_events,
556 if (event_index == WAIT_OBJECT_0
557 || WaitForSingleObject (state->
stop_select, 0) == WAIT_OBJECT_0)
560 if (event_index != WAIT_OBJECT_0 + 1)
570 if (!PeekConsoleInput (h, &
record, 1, &n_records) || n_records != 1)
577 if (
record.EventType == KEY_EVENT &&
record.Event.KeyEvent.bKeyDown)
579 WORD keycode =
record.Event.KeyEvent.wVirtualKeyCode;
587 if (
record.Event.KeyEvent.uChar.AsciiChar != 0
588 || keycode == VK_PRIOR
589 || keycode == VK_NEXT
591 || keycode == VK_HOME
592 || keycode == VK_LEFT
594 || keycode == VK_RIGHT
595 || keycode == VK_DOWN
596 || keycode == VK_INSERT
597 || keycode == VK_DELETE)
606 ReadConsoleInput (h, &
record, 1, &n_records);
617 if (PeekNamedPipe ((HANDLE) _get_osfhandle (fd), NULL, 0, NULL, NULL, NULL))
626 if (GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_DISK)
641 h = (HANDLE) _get_osfhandle (scb->
fd);
652 if (!PeekNamedPipe (h, NULL, 0, NULL, &n_avail, NULL))
666 if (WaitForSingleObject (state->
stop_select, 10) == WAIT_OBJECT_0)
684 h = (HANDLE) _get_osfhandle (scb->
fd);
690 if (SetFilePointer (h, 0, NULL, FILE_CURRENT)
691 == INVALID_SET_FILE_POINTER)
711 is_tty = isatty (scb->
fd);
786 if (isatty (scb->
fd))
817 ps->wait.read_event = INVALID_HANDLE_VALUE;
818 ps->wait.except_event = INVALID_HANDLE_VALUE;
819 ps->wait.start_select = INVALID_HANDLE_VALUE;
820 ps->wait.stop_select = INVALID_HANDLE_VALUE;
828 int saved_errno = errno;
830 if (
ps->wait.read_event != INVALID_HANDLE_VALUE)
871 if (!
argv[0] ||
argv[0][0] ==
'\0')
872 error (
_(
"missing child command"));
877 ps->pex = pex_init (PEX_USE_PIPES,
"target remote pipe", NULL);
880 ps->input = pex_input_pipe (
ps->pex, 1);
887 = pex_run (
ps->pex, PEX_SEARCH | PEX_BINARY_INPUT | PEX_BINARY_OUTPUT
888 | PEX_STDERR_TO_PIPE,
899 error (
_(
"error starting child process '%s': %s: %s"),
902 error (
_(
"error starting child process '%s': %s"),
907 ps->output = pex_read_output (
ps->pex, 1);
910 scb->
fd = fileno (
ps->output);
912 pex_stderr = pex_read_err (
ps->pex, 1);
915 scb->
error_fd = fileno (pex_stderr);
935 ps->input = fdopen (fd,
"r+");
939 ps->output = fdopen (fd,
"r+");
969 HANDLE pipeline_out = (HANDLE) _get_osfhandle (scb->
fd);
973 if (pipeline_out == INVALID_HANDLE_VALUE)
976 if (! PeekNamedPipe (pipeline_out, NULL, 0, NULL, &
available, NULL))
982 if (! ReadFile (pipeline_out, scb->
buf, count, &bytes_read, NULL))
996 int pipeline_in_fd = fileno (
ps->input);
997 if (pipeline_in_fd < 0)
1000 pipeline_in = (HANDLE) _get_osfhandle (pipeline_in_fd);
1001 if (pipeline_in == INVALID_HANDLE_VALUE)
1004 if (! WriteFile (pipeline_in, buf, count, &written, NULL))
1017 if (
ps->wait.read_event == INVALID_HANDLE_VALUE)
1021 *
read =
ps->wait.read_event;
1022 *except =
ps->wait.except_event;
1025 ResetEvent (
ps->wait.read_event);
1026 ResetEvent (
ps->wait.except_event);
1027 ResetEvent (
ps->wait.stop_select);
1038 if (
ps->wait.read_event == INVALID_HANDLE_VALUE)
1047 HANDLE h = (HANDLE) _get_osfhandle (fd);
1049 BOOL r = PeekNamedPipe (h, NULL, 0, NULL, &numBytes, NULL);
1059 if (_pipe (pdes, 512, _O_BINARY | _O_NOINHERIT) == -1)
1108 HANDLE wait_events[2];
1109 WSANETWORKEVENTS events;
1119 event_index = WaitForMultipleObjects (2, wait_events, FALSE, INFINITE);
1121 if (event_index == WAIT_OBJECT_0
1128 if (event_index != WAIT_OBJECT_0 + 1)
1138 if (WSAEnumNetworkEvents (scb->
fd, state->
sock_event, &events) != 0)
1145 if (events.lNetworkEvents & FD_READ)
1154 if (events.lNetworkEvents & FD_CLOSE)
1208 state->
sock_event = CreateEvent (0, TRUE, FALSE, 0);
1209 WSAEventSelect (scb->
fd, state->
sock_event, FD_READ | FD_CLOSE);
1362 hm = LoadLibrary (
"kernel32.dll");
1377 if (WSAStartup (MAKEWORD (1, 0), &wsa_data) != 0)
void error_no_arg(const char *why)
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t err
ssize_t read(int fd, void *buf, size_t count)
static int net_windows_open(struct serial *scb, const char *name)
static int ser_windows_drain_output(struct serial *scb)
static void select_thread_wait(struct ser_console_state *state)
static DWORD WINAPI net_windows_select_thread(void *arg)
static int ser_windows_write_prim(struct serial *scb, const void *buf, size_t len)
static void ser_windows_close(struct serial *scb)
int ser_base_set_tty_state(struct serial *scb, serial_ttystate ttystate)
static serial_ttystate ser_console_get_tty_state(struct serial *scb)
static const struct serial_ops hardwire_ops
static int fd_is_pipe(int fd)
int ser_base_flush_output(struct serial *scb)
static void ser_console_done_wait_handle(struct serial *scb)
void warning(const char *fmt,...)
static int ser_windows_setparity(struct serial *scb, int parity)
static void pipe_done_wait_handle(struct serial *scb)
static void net_windows_done_wait_handle(struct serial *scb)
static void net_windows_wait_handle(struct serial *scb, HANDLE *read, HANDLE *except)
static int pipe_avail(struct serial *scb, int fd)
def record(name, number, comment=None)
void * memset(T *s, int c, size_t n)=delete
int net_write_prim(struct serial *scb, const void *buf, size_t count)
static void pipe_wait_handle(struct serial *scb, HANDLE *read, HANDLE *except)
static int fd_is_file(int fd)
static DWORD WINAPI file_select_thread(void *arg)
int ser_base_setbaudrate(struct serial *scb, int rate)
static void stop_select_thread(struct ser_console_state *state)
static int ser_windows_send_break(struct serial *scb)
int ser_base_setstopbits(struct serial *scb, int num)
void _initialize_ser_windows(void)
static DWORD WINAPI console_select_thread(void *arg)
static void ser_console_wait_handle(struct serial *scb, HANDLE *read, HANDLE *except)
void ser_base_raw(struct serial *scb)
void ser_base_async(struct serial *scb, int async_p)
int net_read_prim(struct serial *scb, size_t count)
static int net_windows_socket_check_pending(struct serial *scb)
DWORD WINAPI(* thread_fn_type)(void *)
struct ser_console_state base
static void ser_console_close(struct serial *scb)
struct cleanup * make_cleanup(make_cleanup_ftype *function, void *arg)
static const char * parity
static void net_windows_close(struct serial *scb)
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int status
static int ser_windows_open(struct serial *scb, const char *name)
int ser_base_drain_output(struct serial *scb)
unsigned char buf[BUFSIZ]
static int ser_windows_setstopbits(struct serial *scb, int num)
static DWORD WINAPI pipe_select_thread(void *arg)
static void ser_windows_raw(struct serial *scb)
static int ser_windows_flush_input(struct serial *scb)
int gdb_pipe(int pdes[2])
static int pipe_windows_read(struct serial *scb, size_t count)
#define SERIAL_1_AND_A_HALF_STOPBITS
static const struct serial_ops tcp_ops
static struct pipe_state * make_pipe_state(void)
#define SERIAL_1_STOPBITS
BOOL WINAPI() CancelIo_ftype(HANDLE)
void ser_base_print_tty_state(struct serial *scb, serial_ttystate ttystate, struct ui_file *stream)
void discard_cleanups(struct cleanup *old_chain)
int ser_base_setparity(struct serial *scb, int parity)
static int pipe_windows_write(struct serial *scb, const void *buf, size_t count)
#define SERIAL_2_STOPBITS
void serial_add_interface(const struct serial_ops *optable)
static void start_select_thread(struct ser_console_state *state)
int ser_base_flush_input(struct serial *scb)
struct ser_console_state wait
int ser_base_readchar(struct serial *scb, int timeout)
char * safe_strerror(int)
static int ser_windows_read_prim(struct serial *scb, size_t count)
static void create_select_thread(thread_fn_type thread_fn, struct serial *scb, struct ser_console_state *state)
int ser_base_send_break(struct serial *scb)
int ser_base_write(struct serial *scb, const void *buf, size_t count)
static void ser_windows_wait_handle(struct serial *scb, HANDLE *read, HANDLE *except)
void net_close(struct serial *scb)
static const struct serial_ops tty_ops
static int ser_windows_flush_output(struct serial *scb)
int ser_tcp_send_break(struct serial *scb)
static void free_pipe_state(struct pipe_state *ps)
enum select_thread_state thread_state
serial_ttystate ser_base_copy_tty_state(struct serial *scb, serial_ttystate ttystate)
static int pipe_windows_open(struct serial *scb, const char *name)
static int pipe_windows_fdopen(struct serial *scb, int fd)
void internal_warning(const char *file, int line, const char *fmt,...)
static void destroy_select_thread(struct ser_console_state *state)
void error(const char *fmt,...)
static void pipe_windows_close(struct serial *scb)
void do_cleanups(struct cleanup *old_chain)
static void cleanup_pipe_state(void *untyped)
serial_ttystate ser_base_get_tty_state(struct serial *scb)
static int ser_windows_setbaudrate(struct serial *scb, int rate)
static const struct serial_ops pipe_ops
int net_open(struct serial *scb, const char *name)