1 /* Serial interface for local (hardwired) serial ports on Windows systems
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #include <sys/types.h>
33 #include "gdb_assert.h"
34 #include "gdb_string.h"
38 void _initialize_ser_windows (void);
40 struct ser_windows_state
48 /* Open up a real live device for serial I/O. */
51 ser_windows_open (struct serial
*scb
, const char *name
)
54 struct ser_windows_state
*state
;
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
))
89 state
= xmalloc (sizeof (struct ser_windows_state
));
90 memset (state
, 0, sizeof (struct ser_windows_state
));
93 /* Create a manual reset event to watch the input buffer. */
94 state
->ov
.hEvent
= CreateEvent (0, TRUE
, FALSE
, 0);
96 /* Create a (currently unused) handle to record exceptions. */
97 state
->except_event
= CreateEvent (0, TRUE
, FALSE
, 0);
102 /* Wait for the output to drain away, as opposed to flushing (discarding)
106 ser_windows_drain_output (struct serial
*scb
)
108 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
110 return (FlushFileBuffers (h
) != 0) ? 0 : -1;
114 ser_windows_flush_output (struct serial
*scb
)
116 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
118 return (PurgeComm (h
, PURGE_TXCLEAR
) != 0) ? 0 : -1;
122 ser_windows_flush_input (struct serial
*scb
)
124 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
126 return (PurgeComm (h
, PURGE_RXCLEAR
) != 0) ? 0 : -1;
130 ser_windows_send_break (struct serial
*scb
)
132 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
134 if (SetCommBreak (h
) == 0)
137 /* Delay for 250 milliseconds. */
140 if (ClearCommBreak (h
))
147 ser_windows_raw (struct serial
*scb
)
149 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
152 if (GetCommState (h
, &state
) == 0)
155 state
.fParity
= FALSE
;
156 state
.fOutxCtsFlow
= FALSE
;
157 state
.fOutxDsrFlow
= FALSE
;
158 state
.fDtrControl
= DTR_CONTROL_ENABLE
;
159 state
.fDsrSensitivity
= FALSE
;
163 state
.fAbortOnError
= FALSE
;
165 state
.Parity
= NOPARITY
;
167 scb
->current_timeout
= 0;
169 if (SetCommState (h
, &state
) == 0)
170 warning (_("SetCommState failed\n"));
174 ser_windows_setstopbits (struct serial
*scb
, int num
)
176 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
179 if (GetCommState (h
, &state
) == 0)
184 case SERIAL_1_STOPBITS
:
185 state
.StopBits
= ONESTOPBIT
;
187 case SERIAL_1_AND_A_HALF_STOPBITS
:
188 state
.StopBits
= ONE5STOPBITS
;
190 case SERIAL_2_STOPBITS
:
191 state
.StopBits
= TWOSTOPBITS
;
197 return (SetCommState (h
, &state
) != 0) ? 0 : -1;
201 ser_windows_setbaudrate (struct serial
*scb
, int rate
)
203 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
206 if (GetCommState (h
, &state
) == 0)
209 state
.BaudRate
= rate
;
211 return (SetCommState (h
, &state
) != 0) ? 0 : -1;
215 ser_windows_close (struct serial
*scb
)
217 struct ser_windows_state
*state
;
219 /* Stop any pending selects. */
220 CancelIo ((HANDLE
) _get_osfhandle (scb
->fd
));
222 CloseHandle (state
->ov
.hEvent
);
223 CloseHandle (state
->except_event
);
235 ser_windows_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
237 struct ser_windows_state
*state
;
240 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
244 *except
= state
->except_event
;
245 *read
= state
->ov
.hEvent
;
247 if (state
->in_progress
)
250 /* Reset the mask - we are only interested in any characters which
251 arrive after this point, not characters which might have arrived
252 and already been read. */
254 /* This really, really shouldn't be necessary - just the second one.
255 But otherwise an internal flag for EV_RXCHAR does not get
256 cleared, and we get a duplicated event, if the last batch
257 of characters included at least two arriving close together. */
258 if (!SetCommMask (h
, 0))
259 warning (_("ser_windows_wait_handle: reseting mask failed"));
261 if (!SetCommMask (h
, EV_RXCHAR
))
262 warning (_("ser_windows_wait_handle: reseting mask failed (2)"));
264 /* There's a potential race condition here; we must check cbInQue
265 and not wait if that's nonzero. */
267 ClearCommError (h
, &errors
, &status
);
268 if (status
.cbInQue
> 0)
270 SetEvent (state
->ov
.hEvent
);
274 state
->in_progress
= 1;
275 ResetEvent (state
->ov
.hEvent
);
276 state
->lastCommMask
= -2;
277 if (WaitCommEvent (h
, &state
->lastCommMask
, &state
->ov
))
279 gdb_assert (state
->lastCommMask
& EV_RXCHAR
);
280 SetEvent (state
->ov
.hEvent
);
283 gdb_assert (GetLastError () == ERROR_IO_PENDING
);
287 ser_windows_read_prim (struct serial
*scb
, size_t count
)
289 struct ser_windows_state
*state
;
291 DWORD bytes_read
, bytes_read_tmp
;
296 if (state
->in_progress
)
298 WaitForSingleObject (state
->ov
.hEvent
, INFINITE
);
299 state
->in_progress
= 0;
300 ResetEvent (state
->ov
.hEvent
);
303 memset (&ov
, 0, sizeof (OVERLAPPED
));
304 ov
.hEvent
= CreateEvent (0, FALSE
, FALSE
, 0);
305 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
307 if (!ReadFile (h
, scb
->buf
, /* count */ 1, &bytes_read
, &ov
))
309 if (GetLastError () != ERROR_IO_PENDING
310 || !GetOverlappedResult (h
, &ov
, &bytes_read
, TRUE
))
314 CloseHandle (ov
.hEvent
);
319 ser_windows_write_prim (struct serial
*scb
, const void *buf
, size_t len
)
321 struct ser_windows_state
*state
;
326 memset (&ov
, 0, sizeof (OVERLAPPED
));
327 ov
.hEvent
= CreateEvent (0, FALSE
, FALSE
, 0);
328 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
329 if (!WriteFile (h
, buf
, len
, &bytes_written
, &ov
))
331 if (GetLastError () != ERROR_IO_PENDING
332 || !GetOverlappedResult (h
, &ov
, &bytes_written
, TRUE
))
336 CloseHandle (ov
.hEvent
);
337 return bytes_written
;
340 /* On Windows, gdb_select is implemented using WaitForMulpleObjects.
341 A "select thread" is created for each file descriptor. These
342 threads looks for activity on the corresponding descriptor, using
343 whatever techniques are appropriate for the descriptor type. When
344 that activity occurs, the thread signals an appropriate event,
345 which wakes up WaitForMultipleObjects.
347 Each select thread is in one of two states: stopped or started.
348 Select threads begin in the stopped state. When gdb_select is
349 called, threads corresponding to the descriptors of interest are
350 started by calling a wait_handle function. Each thread that
351 notices activity signals the appropriate event and then reenters
352 the stopped state. Before gdb_select returns it calls the
353 wait_handle_done functions, which return the threads to the stopped
356 enum select_thread_state
{
361 struct ser_console_state
363 /* Signaled by the select thread to indicate that data is available
364 on the file descriptor. */
366 /* Signaled by the select thread to indicate that an exception has
367 occurred on the file descriptor. */
369 /* Signaled by the select thread to indicate that it has entered the
370 started state. HAVE_STARTED and HAVE_STOPPED are never signaled
373 /* Signaled by the select thread to indicate that it has stopped,
374 either because data is available (and READ_EVENT is signaled),
375 because an exception has occurred (and EXCEPT_EVENT is signaled),
376 or because STOP_SELECT was signaled. */
379 /* Signaled by the main program to tell the select thread to enter
380 the started state. */
382 /* Signaled by the main program to tell the select thread to enter
383 the stopped state. */
385 /* Signaled by the main program to tell the select thread to
389 /* The handle for the select thread. */
391 /* The state of the select thread. This field is only accessed in
392 the main program, never by the select thread itself. */
393 enum select_thread_state thread_state
;
396 /* Called by a select thread to enter the stopped state. This
397 function does not return until the thread has re-entered the
400 select_thread_wait (struct ser_console_state
*state
)
402 HANDLE wait_events
[2];
404 /* There are two things that can wake us up: a request that we enter
405 the started state, or that we exit this thread. */
406 wait_events
[0] = state
->start_select
;
407 wait_events
[1] = state
->exit_select
;
408 if (WaitForMultipleObjects (2, wait_events
, FALSE
, INFINITE
)
410 /* Either the EXIT_SELECT event was signaled (requesting that the
411 thread exit) or an error has occurred. In either case, we exit
415 /* We are now in the started state. */
416 SetEvent (state
->have_started
);
419 typedef DWORD
WINAPI (*thread_fn_type
)(void *);
421 /* Create a new select thread for SCB executing THREAD_FN. The STATE
422 will be filled in by this function before return. */
424 create_select_thread (thread_fn_type thread_fn
,
426 struct ser_console_state
*state
)
430 /* Create all of the events. These are all auto-reset events. */
431 state
->read_event
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
432 state
->except_event
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
433 state
->have_started
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
434 state
->have_stopped
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
435 state
->start_select
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
436 state
->stop_select
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
437 state
->exit_select
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
439 state
->thread
= CreateThread (NULL
, 0, thread_fn
, scb
, 0, &threadId
);
440 /* The thread begins in the stopped state. */
441 state
->thread_state
= STS_STOPPED
;
444 /* Destroy the select thread indicated by STATE. */
446 destroy_select_thread (struct ser_console_state
*state
)
448 /* Ask the thread to exit. */
449 SetEvent (state
->exit_select
);
450 /* Wait until it does. */
451 WaitForSingleObject (state
->thread
, INFINITE
);
453 /* Destroy the events. */
454 CloseHandle (state
->read_event
);
455 CloseHandle (state
->except_event
);
456 CloseHandle (state
->have_started
);
457 CloseHandle (state
->have_stopped
);
458 CloseHandle (state
->start_select
);
459 CloseHandle (state
->stop_select
);
460 CloseHandle (state
->exit_select
);
463 /* Called by gdb_select to start the select thread indicated by STATE.
464 This function does not return until the thread has started. */
466 start_select_thread (struct ser_console_state
*state
)
468 /* Ask the thread to start. */
469 SetEvent (state
->start_select
);
470 /* Wait until it does. */
471 WaitForSingleObject (state
->have_started
, INFINITE
);
472 /* The thread is now started. */
473 state
->thread_state
= STS_STARTED
;
476 /* Called by gdb_select to stop the select thread indicated by STATE.
477 This function does not return until the thread has stopped. */
479 stop_select_thread (struct ser_console_state
*state
)
481 /* If the thread is already in the stopped state, we have nothing to
482 do. Some of the wait_handle functions avoid calling
483 start_select_thread if they notice activity on the relevant file
484 descriptors. The wait_handle_done functions still call
485 stop_select_thread -- but it is already stopped. */
486 if (state
->thread_state
!= STS_STARTED
)
488 /* Ask the thread to stop. */
489 SetEvent (state
->stop_select
);
490 /* Wait until it does. */
491 WaitForSingleObject (state
->have_stopped
, INFINITE
);
492 /* The thread is now stopped. */
493 state
->thread_state
= STS_STOPPED
;
497 console_select_thread (void *arg
)
499 struct serial
*scb
= arg
;
500 struct ser_console_state
*state
;
505 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
509 HANDLE wait_events
[2];
513 select_thread_wait (state
);
517 wait_events
[0] = state
->stop_select
;
520 event_index
= WaitForMultipleObjects (2, wait_events
,
523 if (event_index
== WAIT_OBJECT_0
524 || WaitForSingleObject (state
->stop_select
, 0) == WAIT_OBJECT_0
)
527 if (event_index
!= WAIT_OBJECT_0
+ 1)
529 /* Wait must have failed; assume an error has occured, e.g.
530 the handle has been closed. */
531 SetEvent (state
->except_event
);
535 /* We've got a pending event on the console. See if it's
537 if (!PeekConsoleInput (h
, &record
, 1, &n_records
) || n_records
!= 1)
539 /* Something went wrong. Maybe the console is gone. */
540 SetEvent (state
->except_event
);
544 if (record
.EventType
== KEY_EVENT
&& record
.Event
.KeyEvent
.bKeyDown
)
546 WORD keycode
= record
.Event
.KeyEvent
.wVirtualKeyCode
;
548 /* Ignore events containing only control keys. We must
549 recognize "enhanced" keys which we are interested in
550 reading via getch, if they do not map to ASCII. But we
551 do not want to report input available for e.g. the
552 control key alone. */
554 if (record
.Event
.KeyEvent
.uChar
.AsciiChar
!= 0
555 || keycode
== VK_PRIOR
556 || keycode
== VK_NEXT
558 || keycode
== VK_HOME
559 || keycode
== VK_LEFT
561 || keycode
== VK_RIGHT
562 || keycode
== VK_DOWN
563 || keycode
== VK_INSERT
564 || keycode
== VK_DELETE
)
566 /* This is really a keypress. */
567 SetEvent (state
->read_event
);
572 /* Otherwise discard it and wait again. */
573 ReadConsoleInput (h
, &record
, 1, &n_records
);
576 SetEvent(state
->have_stopped
);
584 if (PeekNamedPipe ((HANDLE
) _get_osfhandle (fd
), NULL
, 0, NULL
, NULL
, NULL
))
593 if (GetFileType ((HANDLE
) _get_osfhandle (fd
)) == FILE_TYPE_DISK
)
600 pipe_select_thread (void *arg
)
602 struct serial
*scb
= arg
;
603 struct ser_console_state
*state
;
608 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
614 select_thread_wait (state
);
616 /* Wait for something to happen on the pipe. */
619 if (!PeekNamedPipe (h
, NULL
, 0, NULL
, &n_avail
, NULL
))
621 SetEvent (state
->except_event
);
627 SetEvent (state
->read_event
);
631 /* Delay 10ms before checking again, but allow the stop
633 if (WaitForSingleObject (state
->stop_select
, 10) == WAIT_OBJECT_0
)
637 SetEvent (state
->have_stopped
);
643 file_select_thread (void *arg
)
645 struct serial
*scb
= arg
;
646 struct ser_console_state
*state
;
651 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
655 select_thread_wait (state
);
657 if (SetFilePointer (h
, 0, NULL
, FILE_CURRENT
)
658 == INVALID_SET_FILE_POINTER
)
659 SetEvent (state
->except_event
);
661 SetEvent (state
->read_event
);
663 SetEvent (state
->have_stopped
);
669 ser_console_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
671 struct ser_console_state
*state
= scb
->state
;
675 thread_fn_type thread_fn
;
678 is_tty
= isatty (scb
->fd
);
679 if (!is_tty
&& !fd_is_file (scb
->fd
) && !fd_is_pipe (scb
->fd
))
686 state
= xmalloc (sizeof (struct ser_console_state
));
687 memset (state
, 0, sizeof (struct ser_console_state
));
691 thread_fn
= console_select_thread
;
692 else if (fd_is_pipe (scb
->fd
))
693 thread_fn
= pipe_select_thread
;
695 thread_fn
= file_select_thread
;
697 create_select_thread (thread_fn
, scb
, state
);
700 *read
= state
->read_event
;
701 *except
= state
->except_event
;
703 /* Start from a blank state. */
704 ResetEvent (state
->read_event
);
705 ResetEvent (state
->except_event
);
706 ResetEvent (state
->stop_select
);
708 /* First check for a key already in the buffer. If there is one,
709 we don't need a thread. This also catches the second key of
710 multi-character returns from getch, for instance for arrow
711 keys. The second half is in a C library internal buffer,
712 and PeekConsoleInput will not find it. */
715 SetEvent (state
->read_event
);
719 /* Otherwise, start the select thread. */
720 start_select_thread (state
);
724 ser_console_done_wait_handle (struct serial
*scb
)
726 struct ser_console_state
*state
= scb
->state
;
731 stop_select_thread (state
);
735 ser_console_close (struct serial
*scb
)
737 struct ser_console_state
*state
= scb
->state
;
741 destroy_select_thread (state
);
746 struct ser_console_ttystate
751 static serial_ttystate
752 ser_console_get_tty_state (struct serial
*scb
)
754 if (isatty (scb
->fd
))
756 struct ser_console_ttystate
*state
;
758 state
= (struct ser_console_ttystate
*) xmalloc (sizeof *state
);
768 /* Since we use the pipe_select_thread for our select emulation,
769 we need to place the state structure it requires at the front
771 struct ser_console_state wait
;
773 /* The pex obj for our (one-stage) pipeline. */
776 /* Streams for the pipeline's input and output. */
777 FILE *input
, *output
;
780 static struct pipe_state
*
781 make_pipe_state (void)
783 struct pipe_state
*ps
= XMALLOC (struct pipe_state
);
785 memset (ps
, 0, sizeof (*ps
));
786 ps
->wait
.read_event
= INVALID_HANDLE_VALUE
;
787 ps
->wait
.except_event
= INVALID_HANDLE_VALUE
;
788 ps
->wait
.start_select
= INVALID_HANDLE_VALUE
;
789 ps
->wait
.stop_select
= INVALID_HANDLE_VALUE
;
795 free_pipe_state (struct pipe_state
*ps
)
797 int saved_errno
= errno
;
799 if (ps
->wait
.read_event
!= INVALID_HANDLE_VALUE
)
800 destroy_select_thread (&ps
->wait
);
802 /* Close the pipe to the child. We must close the pipe before
803 calling pex_free because pex_free will wait for the child to exit
804 and the child will not exit until the pipe is closed. */
810 /* pex_free closes ps->output. */
821 cleanup_pipe_state (void *untyped
)
823 struct pipe_state
*ps
= untyped
;
825 free_pipe_state (ps
);
829 pipe_windows_open (struct serial
*scb
, const char *name
)
831 struct pipe_state
*ps
;
834 struct cleanup
*back_to
;
837 error_no_arg (_("child command"));
839 argv
= gdb_buildargv (name
);
840 back_to
= make_cleanup_freeargv (argv
);
842 if (! argv
[0] || argv
[0][0] == '\0')
843 error (_("missing child command"));
845 ps
= make_pipe_state ();
846 make_cleanup (cleanup_pipe_state
, ps
);
848 ps
->pex
= pex_init (PEX_USE_PIPES
, "target remote pipe", NULL
);
851 ps
->input
= pex_input_pipe (ps
->pex
, 1);
858 = pex_run (ps
->pex
, PEX_SEARCH
| PEX_BINARY_INPUT
| PEX_BINARY_OUTPUT
859 | PEX_STDERR_TO_PIPE
,
860 argv
[0], argv
, NULL
, NULL
,
865 /* Our caller expects us to return -1, but all they'll do with
866 it generally is print the message based on errno. We have
867 all the same information here, plus err_msg provided by
868 pex_run, so we just raise the error here. */
870 error (_("error starting child process '%s': %s: %s"),
871 name
, err_msg
, safe_strerror (err
));
873 error (_("error starting child process '%s': %s"),
878 ps
->output
= pex_read_output (ps
->pex
, 1);
881 scb
->fd
= fileno (ps
->output
);
883 pex_stderr
= pex_read_err (ps
->pex
, 1);
886 scb
->error_fd
= fileno (pex_stderr
);
888 scb
->state
= (void *) ps
;
890 discard_cleanups (back_to
);
894 do_cleanups (back_to
);
899 pipe_windows_fdopen (struct serial
*scb
, int fd
)
901 struct pipe_state
*ps
;
903 ps
= make_pipe_state ();
905 ps
->input
= fdopen (fd
, "r+");
909 ps
->output
= fdopen (fd
, "r+");
914 scb
->state
= (void *) ps
;
919 free_pipe_state (ps
);
924 pipe_windows_close (struct serial
*scb
)
926 struct pipe_state
*ps
= scb
->state
;
928 /* In theory, we should try to kill the subprocess here, but the pex
929 interface doesn't give us enough information to do that. Usually
930 closing the input pipe will get the message across. */
932 free_pipe_state (ps
);
937 pipe_windows_read (struct serial
*scb
, size_t count
)
939 HANDLE pipeline_out
= (HANDLE
) _get_osfhandle (scb
->fd
);
943 if (pipeline_out
== INVALID_HANDLE_VALUE
)
946 if (! PeekNamedPipe (pipeline_out
, NULL
, 0, NULL
, &available
, NULL
))
949 if (count
> available
)
952 if (! ReadFile (pipeline_out
, scb
->buf
, count
, &bytes_read
, NULL
))
960 pipe_windows_write (struct serial
*scb
, const void *buf
, size_t count
)
962 struct pipe_state
*ps
= scb
->state
;
966 int pipeline_in_fd
= fileno (ps
->input
);
967 if (pipeline_in_fd
< 0)
970 pipeline_in
= (HANDLE
) _get_osfhandle (pipeline_in_fd
);
971 if (pipeline_in
== INVALID_HANDLE_VALUE
)
974 if (! WriteFile (pipeline_in
, buf
, count
, &written
, NULL
))
982 pipe_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
984 struct pipe_state
*ps
= scb
->state
;
986 /* Have we allocated our events yet? */
987 if (ps
->wait
.read_event
== INVALID_HANDLE_VALUE
)
988 /* Start the thread. */
989 create_select_thread (pipe_select_thread
, scb
, &ps
->wait
);
991 *read
= ps
->wait
.read_event
;
992 *except
= ps
->wait
.except_event
;
994 /* Start from a blank state. */
995 ResetEvent (ps
->wait
.read_event
);
996 ResetEvent (ps
->wait
.except_event
);
997 ResetEvent (ps
->wait
.stop_select
);
999 start_select_thread (&ps
->wait
);
1003 pipe_done_wait_handle (struct serial
*scb
)
1005 struct pipe_state
*ps
= scb
->state
;
1007 /* Have we allocated our events yet? */
1008 if (ps
->wait
.read_event
== INVALID_HANDLE_VALUE
)
1011 stop_select_thread (&ps
->wait
);
1015 pipe_avail (struct serial
*scb
, int fd
)
1017 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
1019 BOOL r
= PeekNamedPipe (h
, NULL
, 0, NULL
, &numBytes
, NULL
);
1027 gdb_pipe (int pdes
[2])
1029 if (_pipe (pdes
, 512, _O_BINARY
| _O_NOINHERIT
) == -1)
1034 struct net_windows_state
1036 struct ser_console_state base
;
1042 net_windows_select_thread (void *arg
)
1044 struct serial
*scb
= arg
;
1045 struct net_windows_state
*state
;
1052 HANDLE wait_events
[2];
1053 WSANETWORKEVENTS events
;
1055 select_thread_wait (&state
->base
);
1057 wait_events
[0] = state
->base
.stop_select
;
1058 wait_events
[1] = state
->sock_event
;
1060 event_index
= WaitForMultipleObjects (2, wait_events
, FALSE
, INFINITE
);
1062 if (event_index
== WAIT_OBJECT_0
1063 || WaitForSingleObject (state
->base
.stop_select
, 0) == WAIT_OBJECT_0
)
1064 /* We have been requested to stop. */
1066 else if (event_index
!= WAIT_OBJECT_0
+ 1)
1067 /* Some error has occured. Assume that this is an error
1069 SetEvent (state
->base
.except_event
);
1072 /* Enumerate the internal network events, and reset the
1073 object that signalled us to catch the next event. */
1074 WSAEnumNetworkEvents (scb
->fd
, state
->sock_event
, &events
);
1076 gdb_assert (events
.lNetworkEvents
& (FD_READ
| FD_CLOSE
));
1078 if (events
.lNetworkEvents
& FD_READ
)
1079 SetEvent (state
->base
.read_event
);
1081 if (events
.lNetworkEvents
& FD_CLOSE
)
1082 SetEvent (state
->base
.except_event
);
1085 SetEvent (state
->base
.have_stopped
);
1090 net_windows_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
1092 struct net_windows_state
*state
= scb
->state
;
1094 /* Start from a clean slate. */
1095 ResetEvent (state
->base
.read_event
);
1096 ResetEvent (state
->base
.except_event
);
1097 ResetEvent (state
->base
.stop_select
);
1099 *read
= state
->base
.read_event
;
1100 *except
= state
->base
.except_event
;
1102 /* Check any pending events. This both avoids starting the thread
1103 unnecessarily, and handles stray FD_READ events (see below). */
1104 if (WaitForSingleObject (state
->sock_event
, 0) == WAIT_OBJECT_0
)
1106 WSANETWORKEVENTS events
;
1109 /* Enumerate the internal network events, and reset the object that
1110 signalled us to catch the next event. */
1111 WSAEnumNetworkEvents (scb
->fd
, state
->sock_event
, &events
);
1113 /* You'd think that FD_READ or FD_CLOSE would be set here. But,
1114 sometimes, neither is. I suspect that the FD_READ is set and
1115 the corresponding event signalled while recv is running, and
1116 the FD_READ is then lowered when recv consumes all the data,
1117 but there's no way to un-signal the event. This isn't a
1118 problem for the call in net_select_thread, since any new
1119 events after this point will not have been drained by recv.
1120 It just means that we can't have the obvious assert here. */
1122 /* If there is a read event, it might be still valid, or it might
1123 not be - it may have been signalled before we last called
1124 recv. Double-check that there is data. */
1125 if (events
.lNetworkEvents
& FD_READ
)
1127 unsigned long available
;
1129 if (ioctlsocket (scb
->fd
, FIONREAD
, &available
) == 0
1132 SetEvent (state
->base
.read_event
);
1136 /* Oops, no data. This call to recv will cause future
1137 data to retrigger the event, e.g. while we are
1138 in net_select_thread. */
1139 recv (scb
->fd
, NULL
, 0, 0);
1142 /* If there's a close event, then record it - it is obviously
1143 still valid, and it will not be resignalled. */
1144 if (events
.lNetworkEvents
& FD_CLOSE
)
1146 SetEvent (state
->base
.except_event
);
1150 /* If we set either handle, there's no need to wake the thread. */
1155 start_select_thread (&state
->base
);
1159 net_windows_done_wait_handle (struct serial
*scb
)
1161 struct net_windows_state
*state
= scb
->state
;
1163 stop_select_thread (&state
->base
);
1167 net_windows_open (struct serial
*scb
, const char *name
)
1169 struct net_windows_state
*state
;
1173 ret
= net_open (scb
, name
);
1177 state
= xmalloc (sizeof (struct net_windows_state
));
1178 memset (state
, 0, sizeof (struct net_windows_state
));
1181 /* Associate an event with the socket. */
1182 state
->sock_event
= CreateEvent (0, TRUE
, FALSE
, 0);
1183 WSAEventSelect (scb
->fd
, state
->sock_event
, FD_READ
| FD_CLOSE
);
1185 /* Start the thread. */
1186 create_select_thread (net_windows_select_thread
, scb
, &state
->base
);
1193 net_windows_close (struct serial
*scb
)
1195 struct net_windows_state
*state
= scb
->state
;
1197 destroy_select_thread (&state
->base
);
1198 CloseHandle (state
->sock_event
);
1206 _initialize_ser_windows (void)
1209 struct serial_ops
*ops
;
1211 /* First register the serial port driver. */
1213 ops
= XMALLOC (struct serial_ops
);
1214 memset (ops
, 0, sizeof (struct serial_ops
));
1215 ops
->name
= "hardwire";
1217 ops
->open
= ser_windows_open
;
1218 ops
->close
= ser_windows_close
;
1220 ops
->flush_output
= ser_windows_flush_output
;
1221 ops
->flush_input
= ser_windows_flush_input
;
1222 ops
->send_break
= ser_windows_send_break
;
1224 /* These are only used for stdin; we do not need them for serial
1225 ports, so supply the standard dummies. */
1226 ops
->get_tty_state
= ser_base_get_tty_state
;
1227 ops
->set_tty_state
= ser_base_set_tty_state
;
1228 ops
->print_tty_state
= ser_base_print_tty_state
;
1229 ops
->noflush_set_tty_state
= ser_base_noflush_set_tty_state
;
1231 ops
->go_raw
= ser_windows_raw
;
1232 ops
->setbaudrate
= ser_windows_setbaudrate
;
1233 ops
->setstopbits
= ser_windows_setstopbits
;
1234 ops
->drain_output
= ser_windows_drain_output
;
1235 ops
->readchar
= ser_base_readchar
;
1236 ops
->write
= ser_base_write
;
1237 ops
->async
= ser_base_async
;
1238 ops
->read_prim
= ser_windows_read_prim
;
1239 ops
->write_prim
= ser_windows_write_prim
;
1240 ops
->wait_handle
= ser_windows_wait_handle
;
1242 serial_add_interface (ops
);
1244 /* Next create the dummy serial driver used for terminals. We only
1245 provide the TTY-related methods. */
1247 ops
= XMALLOC (struct serial_ops
);
1248 memset (ops
, 0, sizeof (struct serial_ops
));
1250 ops
->name
= "terminal";
1253 ops
->close
= ser_console_close
;
1254 ops
->get_tty_state
= ser_console_get_tty_state
;
1255 ops
->set_tty_state
= ser_base_set_tty_state
;
1256 ops
->print_tty_state
= ser_base_print_tty_state
;
1257 ops
->noflush_set_tty_state
= ser_base_noflush_set_tty_state
;
1258 ops
->drain_output
= ser_base_drain_output
;
1259 ops
->wait_handle
= ser_console_wait_handle
;
1260 ops
->done_wait_handle
= ser_console_done_wait_handle
;
1262 serial_add_interface (ops
);
1264 /* The pipe interface. */
1266 ops
= XMALLOC (struct serial_ops
);
1267 memset (ops
, 0, sizeof (struct serial_ops
));
1270 ops
->open
= pipe_windows_open
;
1271 ops
->close
= pipe_windows_close
;
1272 ops
->fdopen
= pipe_windows_fdopen
;
1273 ops
->readchar
= ser_base_readchar
;
1274 ops
->write
= ser_base_write
;
1275 ops
->flush_output
= ser_base_flush_output
;
1276 ops
->flush_input
= ser_base_flush_input
;
1277 ops
->send_break
= ser_base_send_break
;
1278 ops
->go_raw
= ser_base_raw
;
1279 ops
->get_tty_state
= ser_base_get_tty_state
;
1280 ops
->set_tty_state
= ser_base_set_tty_state
;
1281 ops
->print_tty_state
= ser_base_print_tty_state
;
1282 ops
->noflush_set_tty_state
= ser_base_noflush_set_tty_state
;
1283 ops
->setbaudrate
= ser_base_setbaudrate
;
1284 ops
->setstopbits
= ser_base_setstopbits
;
1285 ops
->drain_output
= ser_base_drain_output
;
1286 ops
->async
= ser_base_async
;
1287 ops
->read_prim
= pipe_windows_read
;
1288 ops
->write_prim
= pipe_windows_write
;
1289 ops
->wait_handle
= pipe_wait_handle
;
1290 ops
->done_wait_handle
= pipe_done_wait_handle
;
1291 ops
->avail
= pipe_avail
;
1293 serial_add_interface (ops
);
1295 /* If WinSock works, register the TCP/UDP socket driver. */
1297 if (WSAStartup (MAKEWORD (1, 0), &wsa_data
) != 0)
1298 /* WinSock is unavailable. */
1301 ops
= XMALLOC (struct serial_ops
);
1302 memset (ops
, 0, sizeof (struct serial_ops
));
1305 ops
->open
= net_windows_open
;
1306 ops
->close
= net_windows_close
;
1307 ops
->readchar
= ser_base_readchar
;
1308 ops
->write
= ser_base_write
;
1309 ops
->flush_output
= ser_base_flush_output
;
1310 ops
->flush_input
= ser_base_flush_input
;
1311 ops
->send_break
= ser_tcp_send_break
;
1312 ops
->go_raw
= ser_base_raw
;
1313 ops
->get_tty_state
= ser_base_get_tty_state
;
1314 ops
->set_tty_state
= ser_base_set_tty_state
;
1315 ops
->print_tty_state
= ser_base_print_tty_state
;
1316 ops
->noflush_set_tty_state
= ser_base_noflush_set_tty_state
;
1317 ops
->setbaudrate
= ser_base_setbaudrate
;
1318 ops
->setstopbits
= ser_base_setstopbits
;
1319 ops
->drain_output
= ser_base_drain_output
;
1320 ops
->async
= ser_base_async
;
1321 ops
->read_prim
= net_read_prim
;
1322 ops
->write_prim
= net_write_prim
;
1323 ops
->wait_handle
= net_windows_wait_handle
;
1324 ops
->done_wait_handle
= net_windows_done_wait_handle
;
1325 serial_add_interface (ops
);