30 #include <sys/types.h>
46 #define CancelIo dyn_CancelIo
56 COMMTIMEOUTS timeouts;
58 h = CreateFile (name, GENERIC_READ | GENERIC_WRITE, 0, NULL,
59 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
60 if (h == INVALID_HANDLE_VALUE)
66 scb->
fd = _open_osfhandle ((intptr_t) h, O_RDWR);
73 if (!SetCommMask (h, EV_RXCHAR))
79 timeouts.ReadIntervalTimeout = MAXDWORD;
80 timeouts.ReadTotalTimeoutConstant = 0;
81 timeouts.ReadTotalTimeoutMultiplier = 0;
82 timeouts.WriteTotalTimeoutConstant = 0;
83 timeouts.WriteTotalTimeoutMultiplier = 0;
84 if (!SetCommTimeouts (h, &timeouts))
95 state->
ov.hEvent = CreateEvent (0, TRUE, FALSE, 0);
109 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
111 return (FlushFileBuffers (h) != 0) ? 0 : -1;
117 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
119 return (PurgeComm (h, PURGE_TXCLEAR) != 0) ? 0 : -1;
125 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
127 return (PurgeComm (h, PURGE_RXCLEAR) != 0) ? 0 : -1;
133 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
135 if (SetCommBreak (h) == 0)
141 if (ClearCommBreak (h))
150 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
153 if (GetCommState (h, &state) == 0)
156 state.fOutxCtsFlow = FALSE;
157 state.fOutxDsrFlow = FALSE;
158 state.fDtrControl = DTR_CONTROL_ENABLE;
159 state.fDsrSensitivity = FALSE;
163 state.fAbortOnError = FALSE;
168 if (SetCommState (h, &state) == 0)
175 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
178 if (GetCommState (h, &state) == 0)
184 state.StopBits = ONESTOPBIT;
187 state.StopBits = ONE5STOPBITS;
190 state.StopBits = TWOSTOPBITS;
196 return (SetCommState (h, &state) != 0) ? 0 : -1;
204 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
207 if (GetCommState (h, &state) == 0)
213 state.Parity = NOPARITY;
214 state.fParity = FALSE;
217 state.Parity = ODDPARITY;
218 state.fParity = TRUE;
221 state.Parity = EVENPARITY;
222 state.fParity = TRUE;
226 "Incorrect parity value: %d", parity);
230 return (SetCommState (h, &state) != 0) ? 0 : -1;
236 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
239 if (GetCommState (h, &state) == 0)
242 state.BaudRate =
rate;
244 return (SetCommState (h, &state) != 0) ? 0 : -1;
259 CloseHandle (state->
ov.hEvent);
277 HANDLE h = (HANDLE) _get_osfhandle (scb->
fd);
282 *read = state->
ov.hEvent;
295 if (!SetCommMask (h, 0))
296 warning (
_(
"ser_windows_wait_handle: reseting mask failed"));
298 if (!SetCommMask (h, EV_RXCHAR))
299 warning (
_(
"ser_windows_wait_handle: reseting mask failed (2)"));
304 ClearCommError (h, &errors, &status);
305 if (status.cbInQue > 0)
307 SetEvent (state->
ov.hEvent);
312 ResetEvent (state->
ov.hEvent);
317 SetEvent (state->
ov.hEvent);
320 gdb_assert (GetLastError () == ERROR_IO_PENDING);
328 DWORD bytes_read, bytes_read_tmp;
335 WaitForSingleObject (state->
ov.hEvent, INFINITE);
337 ResetEvent (state->
ov.hEvent);
340 memset (&ov, 0,
sizeof (OVERLAPPED));
341 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
342 h = (HANDLE) _get_osfhandle (scb->
fd);
344 if (!ReadFile (h, scb->
buf, 1, &bytes_read, &ov))
346 if (GetLastError () != ERROR_IO_PENDING
347 || !GetOverlappedResult (h, &ov, &bytes_read, TRUE))
351 CloseHandle (ov.hEvent);
363 memset (&ov, 0,
sizeof (OVERLAPPED));
364 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
365 h = (HANDLE) _get_osfhandle (scb->
fd);
366 if (!WriteFile (h, buf, len, &bytes_written, &ov))
368 if (GetLastError () != ERROR_IO_PENDING
369 || !GetOverlappedResult (h, &ov, &bytes_written, TRUE))
373 CloseHandle (ov.hEvent);
374 return bytes_written;
439 HANDLE wait_events[2];
445 if (WaitForMultipleObjects (2, wait_events, FALSE, INFINITE)
468 state->
read_event = CreateEvent (NULL, FALSE, FALSE, NULL);
469 state->
except_event = CreateEvent (NULL, FALSE, FALSE, NULL);
470 state->
have_started = CreateEvent (NULL, FALSE, FALSE, NULL);
471 state->
have_stopped = CreateEvent (NULL, FALSE, FALSE, NULL);
472 state->
start_select = CreateEvent (NULL, FALSE, FALSE, NULL);
473 state->
stop_select = CreateEvent (NULL, FALSE, FALSE, NULL);
474 state->
exit_select = CreateEvent (NULL, FALSE, FALSE, NULL);
476 state->
thread = CreateThread (NULL, 0, thread_fn, scb, 0, &threadId);
488 WaitForSingleObject (state->
thread, INFINITE);
542 h = (HANDLE) _get_osfhandle (scb->
fd);
546 HANDLE wait_events[2];
557 event_index = WaitForMultipleObjects (2, wait_events,
560 if (event_index == WAIT_OBJECT_0
561 || WaitForSingleObject (state->
stop_select, 0) == WAIT_OBJECT_0)
564 if (event_index != WAIT_OBJECT_0 + 1)
574 if (!PeekConsoleInput (h, &record, 1, &n_records) || n_records != 1)
581 if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
583 WORD keycode = record.Event.KeyEvent.wVirtualKeyCode;
591 if (record.Event.KeyEvent.uChar.AsciiChar != 0
592 || keycode == VK_PRIOR
593 || keycode == VK_NEXT
595 || keycode == VK_HOME
596 || keycode == VK_LEFT
598 || keycode == VK_RIGHT
599 || keycode == VK_DOWN
600 || keycode == VK_INSERT
601 || keycode == VK_DELETE)
610 ReadConsoleInput (h, &record, 1, &n_records);
621 if (PeekNamedPipe ((HANDLE) _get_osfhandle (fd), NULL, 0, NULL, NULL, NULL))
630 if (GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_DISK)
645 h = (HANDLE) _get_osfhandle (scb->
fd);
656 if (!PeekNamedPipe (h, NULL, 0, NULL, &n_avail, NULL))
670 if (WaitForSingleObject (state->
stop_select, 10) == WAIT_OBJECT_0)
688 h = (HANDLE) _get_osfhandle (scb->
fd);
694 if (SetFilePointer (h, 0, NULL, FILE_CURRENT)
695 == INVALID_SET_FILE_POINTER)
715 is_tty = isatty (scb->
fd);
791 if (isatty (scb->
fd))
822 memset (ps, 0,
sizeof (*ps));
834 int saved_errno = errno;
879 if (! argv[0] || argv[0][0] ==
'\0')
880 error (
_(
"missing child command"));
885 ps->
pex = pex_init (PEX_USE_PIPES,
"target remote pipe", NULL);
888 ps->
input = pex_input_pipe (ps->
pex, 1);
895 = pex_run (ps->
pex, PEX_SEARCH | PEX_BINARY_INPUT | PEX_BINARY_OUTPUT
896 | PEX_STDERR_TO_PIPE,
897 argv[0], argv, NULL, NULL,
907 error (
_(
"error starting child process '%s': %s: %s"),
910 error (
_(
"error starting child process '%s': %s"),
915 ps->
output = pex_read_output (ps->
pex, 1);
920 pex_stderr = pex_read_err (ps->
pex, 1);
923 scb->
error_fd = fileno (pex_stderr);
925 scb->
state = (
void *) ps;
942 ps->
input = fdopen (fd,
"r+");
946 ps->
output = fdopen (fd,
"r+");
951 scb->
state = (
void *) ps;
976 HANDLE pipeline_out = (HANDLE) _get_osfhandle (scb->
fd);
980 if (pipeline_out == INVALID_HANDLE_VALUE)
983 if (! PeekNamedPipe (pipeline_out, NULL, 0, NULL, &available, NULL))
986 if (count > available)
989 if (! ReadFile (pipeline_out, scb->
buf, count, &bytes_read, NULL))
1003 int pipeline_in_fd = fileno (ps->
input);
1004 if (pipeline_in_fd < 0)
1007 pipeline_in = (HANDLE) _get_osfhandle (pipeline_in_fd);
1008 if (pipeline_in == INVALID_HANDLE_VALUE)
1011 if (! WriteFile (pipeline_in, buf, count, &written, NULL))
1054 HANDLE h = (HANDLE) _get_osfhandle (fd);
1056 BOOL r = PeekNamedPipe (h, NULL, 0, NULL, &numBytes, NULL);
1066 if (_pipe (pdes, 512, _O_BINARY | _O_NOINHERIT) == -1)
1089 if (ioctlsocket (scb->
fd, FIONREAD, &available) != 0)
1095 else if (available > 0)
1107 struct serial *scb = arg;
1115 HANDLE wait_events[2];
1116 WSANETWORKEVENTS events;
1126 event_index = WaitForMultipleObjects (2, wait_events, FALSE, INFINITE);
1128 if (event_index == WAIT_OBJECT_0
1135 if (event_index != WAIT_OBJECT_0 + 1)
1145 if (WSAEnumNetworkEvents (scb->
fd, state->
sock_event, &events) != 0)
1152 if (events.lNetworkEvents & FD_READ)
1161 if (events.lNetworkEvents & FD_CLOSE)
1216 state->
sock_event = CreateEvent (0, TRUE, FALSE, 0);
1217 WSAEventSelect (scb->
fd, state->
sock_event, FD_READ | FD_CLOSE);
1374 hm = LoadLibrary (
"kernel32.dll");
1377 CancelIo = (
void *) GetProcAddress (hm,
"CancelIo");
1389 if (WSAStartup (MAKEWORD (1, 0), &wsa_data) != 0)
void error_no_arg(const char *why)
struct cleanup * make_cleanup_freeargv(char **arg)
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 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)
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 *)
mach_port_t mach_port_t name mach_port_t mach_port_t name error_t err
struct ser_console_state base
mach_port_t mach_port_t name mach_port_t mach_port_t name error_t int status
static void ser_console_close(struct serial *scb)
int ser_base_noflush_set_tty_state(struct serial *scb, serial_ttystate new_ttystate, serial_ttystate old_ttystate)
struct cleanup * make_cleanup(make_cleanup_ftype *function, void *arg)
static const char * parity
static void net_windows_close(struct serial *scb)
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 struct pipe_state * make_pipe_state(void)
#define SERIAL_1_STOPBITS
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)
char ** gdb_buildargv(const char *s)
void net_close(struct serial *scb)
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)
const ULONGEST const LONGEST len
int net_open(struct serial *scb, const char *name)