1 /* Serial interface for local (hardwired) serial ports on Windows systems
3 Copyright (C) 2006-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include <sys/types.h>
34 void _initialize_ser_windows (void);
36 struct ser_windows_state
44 /* CancelIo is not available for Windows 95 OS, so we need to use
45 LoadLibrary/GetProcAddress to avoid a startup failure. */
46 #define CancelIo dyn_CancelIo
47 typedef BOOL
WINAPI (CancelIo_ftype
) (HANDLE
);
48 static CancelIo_ftype
*CancelIo
;
50 /* Open up a real live device for serial I/O. */
53 ser_windows_open (struct serial
*scb
, const char *name
)
56 struct ser_windows_state
*state
;
57 COMMTIMEOUTS timeouts
;
59 h
= CreateFile (name
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
60 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, NULL
);
61 if (h
== INVALID_HANDLE_VALUE
)
67 scb
->fd
= _open_osfhandle ((intptr_t) h
, O_RDWR
);
74 if (!SetCommMask (h
, EV_RXCHAR
))
80 timeouts
.ReadIntervalTimeout
= MAXDWORD
;
81 timeouts
.ReadTotalTimeoutConstant
= 0;
82 timeouts
.ReadTotalTimeoutMultiplier
= 0;
83 timeouts
.WriteTotalTimeoutConstant
= 0;
84 timeouts
.WriteTotalTimeoutMultiplier
= 0;
85 if (!SetCommTimeouts (h
, &timeouts
))
91 state
= XCNEW (struct ser_windows_state
);
94 /* Create a manual reset event to watch the input buffer. */
95 state
->ov
.hEvent
= CreateEvent (0, TRUE
, FALSE
, 0);
97 /* Create a (currently unused) handle to record exceptions. */
98 state
->except_event
= CreateEvent (0, TRUE
, FALSE
, 0);
103 /* Wait for the output to drain away, as opposed to flushing (discarding)
107 ser_windows_drain_output (struct serial
*scb
)
109 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
111 return (FlushFileBuffers (h
) != 0) ? 0 : -1;
115 ser_windows_flush_output (struct serial
*scb
)
117 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
119 return (PurgeComm (h
, PURGE_TXCLEAR
) != 0) ? 0 : -1;
123 ser_windows_flush_input (struct serial
*scb
)
125 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
127 return (PurgeComm (h
, PURGE_RXCLEAR
) != 0) ? 0 : -1;
131 ser_windows_send_break (struct serial
*scb
)
133 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
135 if (SetCommBreak (h
) == 0)
138 /* Delay for 250 milliseconds. */
141 if (ClearCommBreak (h
))
148 ser_windows_raw (struct serial
*scb
)
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
;
166 if (SetCommState (h
, &state
) == 0)
167 warning (_("SetCommState failed"));
171 ser_windows_setstopbits (struct serial
*scb
, int num
)
173 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
176 if (GetCommState (h
, &state
) == 0)
181 case SERIAL_1_STOPBITS
:
182 state
.StopBits
= ONESTOPBIT
;
184 case SERIAL_1_AND_A_HALF_STOPBITS
:
185 state
.StopBits
= ONE5STOPBITS
;
187 case SERIAL_2_STOPBITS
:
188 state
.StopBits
= TWOSTOPBITS
;
194 return (SetCommState (h
, &state
) != 0) ? 0 : -1;
197 /* Implement the "setparity" serial_ops callback. */
200 ser_windows_setparity (struct serial
*scb
, int parity
)
202 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
205 if (GetCommState (h
, &state
) == 0)
211 state
.Parity
= NOPARITY
;
212 state
.fParity
= FALSE
;
215 state
.Parity
= ODDPARITY
;
216 state
.fParity
= TRUE
;
219 state
.Parity
= EVENPARITY
;
220 state
.fParity
= TRUE
;
223 internal_warning (__FILE__
, __LINE__
,
224 "Incorrect parity value: %d", parity
);
228 return (SetCommState (h
, &state
) != 0) ? 0 : -1;
232 ser_windows_setbaudrate (struct serial
*scb
, int rate
)
234 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
237 if (GetCommState (h
, &state
) == 0)
240 state
.BaudRate
= rate
;
242 return (SetCommState (h
, &state
) != 0) ? 0 : -1;
246 ser_windows_close (struct serial
*scb
)
248 struct ser_windows_state
*state
;
250 /* Stop any pending selects. On Windows 95 OS, CancelIo function does
251 not exist. In that case, it can be replaced by a call to CloseHandle,
252 but this is not necessary here as we do close the Windows handle
253 by calling close (scb->fd) below. */
255 CancelIo ((HANDLE
) _get_osfhandle (scb
->fd
));
256 state
= (struct ser_windows_state
*) scb
->state
;
257 CloseHandle (state
->ov
.hEvent
);
258 CloseHandle (state
->except_event
);
270 ser_windows_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
272 struct ser_windows_state
*state
;
275 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
277 state
= (struct ser_windows_state
*) scb
->state
;
279 *except
= state
->except_event
;
280 *read
= state
->ov
.hEvent
;
282 if (state
->in_progress
)
285 /* Reset the mask - we are only interested in any characters which
286 arrive after this point, not characters which might have arrived
287 and already been read. */
289 /* This really, really shouldn't be necessary - just the second one.
290 But otherwise an internal flag for EV_RXCHAR does not get
291 cleared, and we get a duplicated event, if the last batch
292 of characters included at least two arriving close together. */
293 if (!SetCommMask (h
, 0))
294 warning (_("ser_windows_wait_handle: reseting mask failed"));
296 if (!SetCommMask (h
, EV_RXCHAR
))
297 warning (_("ser_windows_wait_handle: reseting mask failed (2)"));
299 /* There's a potential race condition here; we must check cbInQue
300 and not wait if that's nonzero. */
302 ClearCommError (h
, &errors
, &status
);
303 if (status
.cbInQue
> 0)
305 SetEvent (state
->ov
.hEvent
);
309 state
->in_progress
= 1;
310 ResetEvent (state
->ov
.hEvent
);
311 state
->lastCommMask
= -2;
312 if (WaitCommEvent (h
, &state
->lastCommMask
, &state
->ov
))
314 gdb_assert (state
->lastCommMask
& EV_RXCHAR
);
315 SetEvent (state
->ov
.hEvent
);
318 gdb_assert (GetLastError () == ERROR_IO_PENDING
);
322 ser_windows_read_prim (struct serial
*scb
, size_t count
)
324 struct ser_windows_state
*state
;
326 DWORD bytes_read
, bytes_read_tmp
;
330 state
= (struct ser_windows_state
*) scb
->state
;
331 if (state
->in_progress
)
333 WaitForSingleObject (state
->ov
.hEvent
, INFINITE
);
334 state
->in_progress
= 0;
335 ResetEvent (state
->ov
.hEvent
);
338 memset (&ov
, 0, sizeof (OVERLAPPED
));
339 ov
.hEvent
= CreateEvent (0, FALSE
, FALSE
, 0);
340 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
342 if (!ReadFile (h
, scb
->buf
, /* count */ 1, &bytes_read
, &ov
))
344 if (GetLastError () != ERROR_IO_PENDING
345 || !GetOverlappedResult (h
, &ov
, &bytes_read
, TRUE
))
349 CloseHandle (ov
.hEvent
);
354 ser_windows_write_prim (struct serial
*scb
, const void *buf
, size_t len
)
356 struct ser_windows_state
*state
;
361 memset (&ov
, 0, sizeof (OVERLAPPED
));
362 ov
.hEvent
= CreateEvent (0, FALSE
, FALSE
, 0);
363 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
364 if (!WriteFile (h
, buf
, len
, &bytes_written
, &ov
))
366 if (GetLastError () != ERROR_IO_PENDING
367 || !GetOverlappedResult (h
, &ov
, &bytes_written
, TRUE
))
371 CloseHandle (ov
.hEvent
);
372 return bytes_written
;
375 /* On Windows, gdb_select is implemented using WaitForMulpleObjects.
376 A "select thread" is created for each file descriptor. These
377 threads looks for activity on the corresponding descriptor, using
378 whatever techniques are appropriate for the descriptor type. When
379 that activity occurs, the thread signals an appropriate event,
380 which wakes up WaitForMultipleObjects.
382 Each select thread is in one of two states: stopped or started.
383 Select threads begin in the stopped state. When gdb_select is
384 called, threads corresponding to the descriptors of interest are
385 started by calling a wait_handle function. Each thread that
386 notices activity signals the appropriate event and then reenters
387 the stopped state. Before gdb_select returns it calls the
388 wait_handle_done functions, which return the threads to the stopped
391 enum select_thread_state
{
396 struct ser_console_state
398 /* Signaled by the select thread to indicate that data is available
399 on the file descriptor. */
401 /* Signaled by the select thread to indicate that an exception has
402 occurred on the file descriptor. */
404 /* Signaled by the select thread to indicate that it has entered the
405 started state. HAVE_STARTED and HAVE_STOPPED are never signaled
408 /* Signaled by the select thread to indicate that it has stopped,
409 either because data is available (and READ_EVENT is signaled),
410 because an exception has occurred (and EXCEPT_EVENT is signaled),
411 or because STOP_SELECT was signaled. */
414 /* Signaled by the main program to tell the select thread to enter
415 the started state. */
417 /* Signaled by the main program to tell the select thread to enter
418 the stopped state. */
420 /* Signaled by the main program to tell the select thread to
424 /* The handle for the select thread. */
426 /* The state of the select thread. This field is only accessed in
427 the main program, never by the select thread itself. */
428 enum select_thread_state thread_state
;
431 /* Called by a select thread to enter the stopped state. This
432 function does not return until the thread has re-entered the
435 select_thread_wait (struct ser_console_state
*state
)
437 HANDLE wait_events
[2];
439 /* There are two things that can wake us up: a request that we enter
440 the started state, or that we exit this thread. */
441 wait_events
[0] = state
->start_select
;
442 wait_events
[1] = state
->exit_select
;
443 if (WaitForMultipleObjects (2, wait_events
, FALSE
, INFINITE
)
445 /* Either the EXIT_SELECT event was signaled (requesting that the
446 thread exit) or an error has occurred. In either case, we exit
450 /* We are now in the started state. */
451 SetEvent (state
->have_started
);
454 typedef DWORD
WINAPI (*thread_fn_type
)(void *);
456 /* Create a new select thread for SCB executing THREAD_FN. The STATE
457 will be filled in by this function before return. */
459 create_select_thread (thread_fn_type thread_fn
,
461 struct ser_console_state
*state
)
465 /* Create all of the events. These are all auto-reset events. */
466 state
->read_event
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
467 state
->except_event
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
468 state
->have_started
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
469 state
->have_stopped
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
470 state
->start_select
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
471 state
->stop_select
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
472 state
->exit_select
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
474 state
->thread
= CreateThread (NULL
, 0, thread_fn
, scb
, 0, &threadId
);
475 /* The thread begins in the stopped state. */
476 state
->thread_state
= STS_STOPPED
;
479 /* Destroy the select thread indicated by STATE. */
481 destroy_select_thread (struct ser_console_state
*state
)
483 /* Ask the thread to exit. */
484 SetEvent (state
->exit_select
);
485 /* Wait until it does. */
486 WaitForSingleObject (state
->thread
, INFINITE
);
488 /* Destroy the events. */
489 CloseHandle (state
->read_event
);
490 CloseHandle (state
->except_event
);
491 CloseHandle (state
->have_started
);
492 CloseHandle (state
->have_stopped
);
493 CloseHandle (state
->start_select
);
494 CloseHandle (state
->stop_select
);
495 CloseHandle (state
->exit_select
);
498 /* Called by gdb_select to start the select thread indicated by STATE.
499 This function does not return until the thread has started. */
501 start_select_thread (struct ser_console_state
*state
)
503 /* Ask the thread to start. */
504 SetEvent (state
->start_select
);
505 /* Wait until it does. */
506 WaitForSingleObject (state
->have_started
, INFINITE
);
507 /* The thread is now started. */
508 state
->thread_state
= STS_STARTED
;
511 /* Called by gdb_select to stop the select thread indicated by STATE.
512 This function does not return until the thread has stopped. */
514 stop_select_thread (struct ser_console_state
*state
)
516 /* If the thread is already in the stopped state, we have nothing to
517 do. Some of the wait_handle functions avoid calling
518 start_select_thread if they notice activity on the relevant file
519 descriptors. The wait_handle_done functions still call
520 stop_select_thread -- but it is already stopped. */
521 if (state
->thread_state
!= STS_STARTED
)
523 /* Ask the thread to stop. */
524 SetEvent (state
->stop_select
);
525 /* Wait until it does. */
526 WaitForSingleObject (state
->have_stopped
, INFINITE
);
527 /* The thread is now stopped. */
528 state
->thread_state
= STS_STOPPED
;
532 console_select_thread (void *arg
)
534 struct serial
*scb
= (struct serial
*) arg
;
535 struct ser_console_state
*state
;
539 state
= (struct ser_console_state
*) scb
->state
;
540 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
544 HANDLE wait_events
[2];
548 select_thread_wait (state
);
552 wait_events
[0] = state
->stop_select
;
555 event_index
= WaitForMultipleObjects (2, wait_events
,
558 if (event_index
== WAIT_OBJECT_0
559 || WaitForSingleObject (state
->stop_select
, 0) == WAIT_OBJECT_0
)
562 if (event_index
!= WAIT_OBJECT_0
+ 1)
564 /* Wait must have failed; assume an error has occured, e.g.
565 the handle has been closed. */
566 SetEvent (state
->except_event
);
570 /* We've got a pending event on the console. See if it's
572 if (!PeekConsoleInput (h
, &record
, 1, &n_records
) || n_records
!= 1)
574 /* Something went wrong. Maybe the console is gone. */
575 SetEvent (state
->except_event
);
579 if (record
.EventType
== KEY_EVENT
&& record
.Event
.KeyEvent
.bKeyDown
)
581 WORD keycode
= record
.Event
.KeyEvent
.wVirtualKeyCode
;
583 /* Ignore events containing only control keys. We must
584 recognize "enhanced" keys which we are interested in
585 reading via getch, if they do not map to ASCII. But we
586 do not want to report input available for e.g. the
587 control key alone. */
589 if (record
.Event
.KeyEvent
.uChar
.AsciiChar
!= 0
590 || keycode
== VK_PRIOR
591 || keycode
== VK_NEXT
593 || keycode
== VK_HOME
594 || keycode
== VK_LEFT
596 || keycode
== VK_RIGHT
597 || keycode
== VK_DOWN
598 || keycode
== VK_INSERT
599 || keycode
== VK_DELETE
)
601 /* This is really a keypress. */
602 SetEvent (state
->read_event
);
607 /* Otherwise discard it and wait again. */
608 ReadConsoleInput (h
, &record
, 1, &n_records
);
611 SetEvent(state
->have_stopped
);
619 if (PeekNamedPipe ((HANDLE
) _get_osfhandle (fd
), NULL
, 0, NULL
, NULL
, NULL
))
628 if (GetFileType ((HANDLE
) _get_osfhandle (fd
)) == FILE_TYPE_DISK
)
635 pipe_select_thread (void *arg
)
637 struct serial
*scb
= (struct serial
*) arg
;
638 struct ser_console_state
*state
;
642 state
= (struct ser_console_state
*) scb
->state
;
643 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
649 select_thread_wait (state
);
651 /* Wait for something to happen on the pipe. */
654 if (!PeekNamedPipe (h
, NULL
, 0, NULL
, &n_avail
, NULL
))
656 SetEvent (state
->except_event
);
662 SetEvent (state
->read_event
);
666 /* Delay 10ms before checking again, but allow the stop
668 if (WaitForSingleObject (state
->stop_select
, 10) == WAIT_OBJECT_0
)
672 SetEvent (state
->have_stopped
);
678 file_select_thread (void *arg
)
680 struct serial
*scb
= (struct serial
*) arg
;
681 struct ser_console_state
*state
;
685 state
= (struct ser_console_state
*) scb
->state
;
686 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
690 select_thread_wait (state
);
692 if (SetFilePointer (h
, 0, NULL
, FILE_CURRENT
)
693 == INVALID_SET_FILE_POINTER
)
694 SetEvent (state
->except_event
);
696 SetEvent (state
->read_event
);
698 SetEvent (state
->have_stopped
);
704 ser_console_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
706 struct ser_console_state
*state
= (struct ser_console_state
*) scb
->state
;
710 thread_fn_type thread_fn
;
713 is_tty
= isatty (scb
->fd
);
714 if (!is_tty
&& !fd_is_file (scb
->fd
) && !fd_is_pipe (scb
->fd
))
721 state
= XCNEW (struct ser_console_state
);
725 thread_fn
= console_select_thread
;
726 else if (fd_is_pipe (scb
->fd
))
727 thread_fn
= pipe_select_thread
;
729 thread_fn
= file_select_thread
;
731 create_select_thread (thread_fn
, scb
, state
);
734 *read
= state
->read_event
;
735 *except
= state
->except_event
;
737 /* Start from a blank state. */
738 ResetEvent (state
->read_event
);
739 ResetEvent (state
->except_event
);
740 ResetEvent (state
->stop_select
);
742 /* First check for a key already in the buffer. If there is one,
743 we don't need a thread. This also catches the second key of
744 multi-character returns from getch, for instance for arrow
745 keys. The second half is in a C library internal buffer,
746 and PeekConsoleInput will not find it. */
749 SetEvent (state
->read_event
);
753 /* Otherwise, start the select thread. */
754 start_select_thread (state
);
758 ser_console_done_wait_handle (struct serial
*scb
)
760 struct ser_console_state
*state
= (struct ser_console_state
*) scb
->state
;
765 stop_select_thread (state
);
769 ser_console_close (struct serial
*scb
)
771 struct ser_console_state
*state
= (struct ser_console_state
*) scb
->state
;
775 destroy_select_thread (state
);
780 struct ser_console_ttystate
785 static serial_ttystate
786 ser_console_get_tty_state (struct serial
*scb
)
788 if (isatty (scb
->fd
))
790 struct ser_console_ttystate
*state
;
792 state
= XNEW (struct ser_console_ttystate
);
802 /* Since we use the pipe_select_thread for our select emulation,
803 we need to place the state structure it requires at the front
805 struct ser_console_state wait
;
807 /* The pex obj for our (one-stage) pipeline. */
810 /* Streams for the pipeline's input and output. */
811 FILE *input
, *output
;
814 static struct pipe_state
*
815 make_pipe_state (void)
817 struct pipe_state
*ps
= XCNEW (struct pipe_state
);
819 ps
->wait
.read_event
= INVALID_HANDLE_VALUE
;
820 ps
->wait
.except_event
= INVALID_HANDLE_VALUE
;
821 ps
->wait
.start_select
= INVALID_HANDLE_VALUE
;
822 ps
->wait
.stop_select
= INVALID_HANDLE_VALUE
;
828 free_pipe_state (struct pipe_state
*ps
)
830 int saved_errno
= errno
;
832 if (ps
->wait
.read_event
!= INVALID_HANDLE_VALUE
)
833 destroy_select_thread (&ps
->wait
);
835 /* Close the pipe to the child. We must close the pipe before
836 calling pex_free because pex_free will wait for the child to exit
837 and the child will not exit until the pipe is closed. */
843 /* pex_free closes ps->output. */
854 cleanup_pipe_state (void *untyped
)
856 struct pipe_state
*ps
= (struct pipe_state
*) untyped
;
858 free_pipe_state (ps
);
862 pipe_windows_open (struct serial
*scb
, const char *name
)
864 struct pipe_state
*ps
;
866 struct cleanup
*back_to
;
869 error_no_arg (_("child command"));
871 gdb_argv
argv (name
);
873 if (! argv
[0] || argv
[0][0] == '\0')
874 error (_("missing child command"));
876 ps
= make_pipe_state ();
877 back_to
= make_cleanup (cleanup_pipe_state
, ps
);
879 ps
->pex
= pex_init (PEX_USE_PIPES
, "target remote pipe", NULL
);
882 ps
->input
= pex_input_pipe (ps
->pex
, 1);
889 = pex_run (ps
->pex
, PEX_SEARCH
| PEX_BINARY_INPUT
| PEX_BINARY_OUTPUT
890 | PEX_STDERR_TO_PIPE
,
891 argv
[0], argv
.get (), NULL
, NULL
,
896 /* Our caller expects us to return -1, but all they'll do with
897 it generally is print the message based on errno. We have
898 all the same information here, plus err_msg provided by
899 pex_run, so we just raise the error here. */
901 error (_("error starting child process '%s': %s: %s"),
902 name
, err_msg
, safe_strerror (err
));
904 error (_("error starting child process '%s': %s"),
909 ps
->output
= pex_read_output (ps
->pex
, 1);
912 scb
->fd
= fileno (ps
->output
);
914 pex_stderr
= pex_read_err (ps
->pex
, 1);
917 scb
->error_fd
= fileno (pex_stderr
);
919 scb
->state
= (void *) ps
;
922 discard_cleanups (back_to
);
926 do_cleanups (back_to
);
931 pipe_windows_fdopen (struct serial
*scb
, int fd
)
933 struct pipe_state
*ps
;
935 ps
= make_pipe_state ();
937 ps
->input
= fdopen (fd
, "r+");
941 ps
->output
= fdopen (fd
, "r+");
946 scb
->state
= (void *) ps
;
951 free_pipe_state (ps
);
956 pipe_windows_close (struct serial
*scb
)
958 struct pipe_state
*ps
= (struct pipe_state
*) scb
->state
;
960 /* In theory, we should try to kill the subprocess here, but the pex
961 interface doesn't give us enough information to do that. Usually
962 closing the input pipe will get the message across. */
964 free_pipe_state (ps
);
969 pipe_windows_read (struct serial
*scb
, size_t count
)
971 HANDLE pipeline_out
= (HANDLE
) _get_osfhandle (scb
->fd
);
975 if (pipeline_out
== INVALID_HANDLE_VALUE
)
978 if (! PeekNamedPipe (pipeline_out
, NULL
, 0, NULL
, &available
, NULL
))
981 if (count
> available
)
984 if (! ReadFile (pipeline_out
, scb
->buf
, count
, &bytes_read
, NULL
))
992 pipe_windows_write (struct serial
*scb
, const void *buf
, size_t count
)
994 struct pipe_state
*ps
= (struct pipe_state
*) scb
->state
;
998 int pipeline_in_fd
= fileno (ps
->input
);
999 if (pipeline_in_fd
< 0)
1002 pipeline_in
= (HANDLE
) _get_osfhandle (pipeline_in_fd
);
1003 if (pipeline_in
== INVALID_HANDLE_VALUE
)
1006 if (! WriteFile (pipeline_in
, buf
, count
, &written
, NULL
))
1014 pipe_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
1016 struct pipe_state
*ps
= (struct pipe_state
*) scb
->state
;
1018 /* Have we allocated our events yet? */
1019 if (ps
->wait
.read_event
== INVALID_HANDLE_VALUE
)
1020 /* Start the thread. */
1021 create_select_thread (pipe_select_thread
, scb
, &ps
->wait
);
1023 *read
= ps
->wait
.read_event
;
1024 *except
= ps
->wait
.except_event
;
1026 /* Start from a blank state. */
1027 ResetEvent (ps
->wait
.read_event
);
1028 ResetEvent (ps
->wait
.except_event
);
1029 ResetEvent (ps
->wait
.stop_select
);
1031 start_select_thread (&ps
->wait
);
1035 pipe_done_wait_handle (struct serial
*scb
)
1037 struct pipe_state
*ps
= (struct pipe_state
*) scb
->state
;
1039 /* Have we allocated our events yet? */
1040 if (ps
->wait
.read_event
== INVALID_HANDLE_VALUE
)
1043 stop_select_thread (&ps
->wait
);
1047 pipe_avail (struct serial
*scb
, int fd
)
1049 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
1051 BOOL r
= PeekNamedPipe (h
, NULL
, 0, NULL
, &numBytes
, NULL
);
1059 gdb_pipe (int pdes
[2])
1061 if (_pipe (pdes
, 512, _O_BINARY
| _O_NOINHERIT
) == -1)
1066 struct net_windows_state
1068 struct ser_console_state base
;
1073 /* Check whether the socket has any pending data to be read. If so,
1074 set the select thread's read event. On error, set the select
1075 thread's except event. If any event was set, return true,
1076 otherwise return false. */
1079 net_windows_socket_check_pending (struct serial
*scb
)
1081 struct net_windows_state
*state
= (struct net_windows_state
*) scb
->state
;
1082 unsigned long available
;
1084 if (ioctlsocket (scb
->fd
, FIONREAD
, &available
) != 0)
1086 /* The socket closed, or some other error. */
1087 SetEvent (state
->base
.except_event
);
1090 else if (available
> 0)
1092 SetEvent (state
->base
.read_event
);
1100 net_windows_select_thread (void *arg
)
1102 struct serial
*scb
= (struct serial
*) arg
;
1103 struct net_windows_state
*state
;
1106 state
= (struct net_windows_state
*) scb
->state
;
1110 HANDLE wait_events
[2];
1111 WSANETWORKEVENTS events
;
1113 select_thread_wait (&state
->base
);
1115 wait_events
[0] = state
->base
.stop_select
;
1116 wait_events
[1] = state
->sock_event
;
1118 /* Wait for something to happen on the socket. */
1121 event_index
= WaitForMultipleObjects (2, wait_events
, FALSE
, INFINITE
);
1123 if (event_index
== WAIT_OBJECT_0
1124 || WaitForSingleObject (state
->base
.stop_select
, 0) == WAIT_OBJECT_0
)
1126 /* We have been requested to stop. */
1130 if (event_index
!= WAIT_OBJECT_0
+ 1)
1132 /* Some error has occured. Assume that this is an error
1134 SetEvent (state
->base
.except_event
);
1138 /* Enumerate the internal network events, and reset the
1139 object that signalled us to catch the next event. */
1140 if (WSAEnumNetworkEvents (scb
->fd
, state
->sock_event
, &events
) != 0)
1142 /* Something went wrong. Maybe the socket is gone. */
1143 SetEvent (state
->base
.except_event
);
1147 if (events
.lNetworkEvents
& FD_READ
)
1149 if (net_windows_socket_check_pending (scb
))
1152 /* Spurious wakeup. That is, the socket's event was
1153 signalled before we last called recv. */
1156 if (events
.lNetworkEvents
& FD_CLOSE
)
1158 SetEvent (state
->base
.except_event
);
1163 SetEvent (state
->base
.have_stopped
);
1169 net_windows_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
1171 struct net_windows_state
*state
= (struct net_windows_state
*) scb
->state
;
1173 /* Start from a clean slate. */
1174 ResetEvent (state
->base
.read_event
);
1175 ResetEvent (state
->base
.except_event
);
1176 ResetEvent (state
->base
.stop_select
);
1178 *read
= state
->base
.read_event
;
1179 *except
= state
->base
.except_event
;
1181 /* Check any pending events. Otherwise, start the select
1183 if (!net_windows_socket_check_pending (scb
))
1184 start_select_thread (&state
->base
);
1188 net_windows_done_wait_handle (struct serial
*scb
)
1190 struct net_windows_state
*state
= (struct net_windows_state
*) scb
->state
;
1192 stop_select_thread (&state
->base
);
1196 net_windows_open (struct serial
*scb
, const char *name
)
1198 struct net_windows_state
*state
;
1202 ret
= net_open (scb
, name
);
1206 state
= XCNEW (struct net_windows_state
);
1209 /* Associate an event with the socket. */
1210 state
->sock_event
= CreateEvent (0, TRUE
, FALSE
, 0);
1211 WSAEventSelect (scb
->fd
, state
->sock_event
, FD_READ
| FD_CLOSE
);
1213 /* Start the thread. */
1214 create_select_thread (net_windows_select_thread
, scb
, &state
->base
);
1221 net_windows_close (struct serial
*scb
)
1223 struct net_windows_state
*state
= (struct net_windows_state
*) scb
->state
;
1225 destroy_select_thread (&state
->base
);
1226 CloseHandle (state
->sock_event
);
1233 /* The serial port driver. */
1235 static const struct serial_ops hardwire_ops
=
1243 ser_windows_flush_output
,
1244 ser_windows_flush_input
,
1245 ser_windows_send_break
,
1247 /* These are only used for stdin; we do not need them for serial
1248 ports, so supply the standard dummies. */
1249 ser_base_get_tty_state
,
1250 ser_base_copy_tty_state
,
1251 ser_base_set_tty_state
,
1252 ser_base_print_tty_state
,
1253 ser_base_noflush_set_tty_state
,
1254 ser_windows_setbaudrate
,
1255 ser_windows_setstopbits
,
1256 ser_windows_setparity
,
1257 ser_windows_drain_output
,
1259 ser_windows_read_prim
,
1260 ser_windows_write_prim
,
1262 ser_windows_wait_handle
1265 /* The dummy serial driver used for terminals. We only provide the
1266 TTY-related methods. */
1268 static const struct serial_ops tty_ops
=
1280 ser_console_get_tty_state
,
1281 ser_base_copy_tty_state
,
1282 ser_base_set_tty_state
,
1283 ser_base_print_tty_state
,
1284 ser_base_noflush_set_tty_state
,
1288 ser_base_drain_output
,
1293 ser_console_wait_handle
,
1294 ser_console_done_wait_handle
1297 /* The pipe interface. */
1299 static const struct serial_ops pipe_ops
=
1304 pipe_windows_fdopen
,
1307 ser_base_flush_output
,
1308 ser_base_flush_input
,
1309 ser_base_send_break
,
1311 ser_base_get_tty_state
,
1312 ser_base_copy_tty_state
,
1313 ser_base_set_tty_state
,
1314 ser_base_print_tty_state
,
1315 ser_base_noflush_set_tty_state
,
1316 ser_base_setbaudrate
,
1317 ser_base_setstopbits
,
1319 ser_base_drain_output
,
1325 pipe_done_wait_handle
1328 /* The TCP/UDP socket driver. */
1330 static const struct serial_ops tcp_ops
=
1338 ser_base_flush_output
,
1339 ser_base_flush_input
,
1342 ser_base_get_tty_state
,
1343 ser_base_copy_tty_state
,
1344 ser_base_set_tty_state
,
1345 ser_base_print_tty_state
,
1346 ser_base_noflush_set_tty_state
,
1347 ser_base_setbaudrate
,
1348 ser_base_setstopbits
,
1350 ser_base_drain_output
,
1355 net_windows_wait_handle
,
1356 net_windows_done_wait_handle
1360 _initialize_ser_windows (void)
1363 struct serial_ops
*ops
;
1367 /* First find out if kernel32 exports CancelIo function. */
1368 hm
= LoadLibrary ("kernel32.dll");
1371 CancelIo
= (CancelIo_ftype
*) GetProcAddress (hm
, "CancelIo");
1377 serial_add_interface (&hardwire_ops
);
1378 serial_add_interface (&tty_ops
);
1379 serial_add_interface (&pipe_ops
);
1381 /* If WinSock works, register the TCP/UDP socket driver. */
1383 if (WSAStartup (MAKEWORD (1, 0), &wsa_data
) != 0)
1384 /* WinSock is unavailable. */
1387 serial_add_interface (&tcp_ops
);