GDB (xrefs)
Loading...
Searching...
No Matches
ser-mingw.c
Go to the documentation of this file.
1/* Serial interface for local (hardwired) serial ports on Windows systems
2
3 Copyright (C) 2006-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21#include "serial.h"
22#include "ser-base.h"
23#include "ser-tcp.h"
24
25#include <windows.h>
26#include <conio.h>
27
28#include <fcntl.h>
29#include <unistd.h>
30#include <sys/types.h>
31
32#include "command.h"
33#include "gdbsupport/buildargv.h"
34
36{
38 OVERLAPPED ov;
41};
42
43/* CancelIo is not available for Windows 95 OS, so we need to use
44 LoadLibrary/GetProcAddress to avoid a startup failure. */
45#define CancelIo dyn_CancelIo
46typedef BOOL WINAPI (CancelIo_ftype) (HANDLE);
48
49/* Open up a real live device for serial I/O. */
50
51static int
52ser_windows_open (struct serial *scb, const char *name)
53{
54 HANDLE h;
55 struct ser_windows_state *state;
56 COMMTIMEOUTS timeouts;
57
58 h = CreateFile (name, GENERIC_READ | GENERIC_WRITE, 0, NULL,
59 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
60 if (h == INVALID_HANDLE_VALUE)
61 {
62 errno = ENOENT;
63 return -1;
64 }
65
66 scb->fd = _open_osfhandle ((intptr_t) h, O_RDWR);
67 if (scb->fd < 0)
68 {
69 errno = ENOENT;
70 return -1;
71 }
72
73 if (!SetCommMask (h, EV_RXCHAR))
74 {
75 errno = EINVAL;
76 return -1;
77 }
78
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))
85 {
86 errno = EINVAL;
87 return -1;
88 }
89
90 state = XCNEW (struct ser_windows_state);
91 scb->state = state;
92
93 /* Create a manual reset event to watch the input buffer. */
94 state->ov.hEvent = CreateEvent (0, TRUE, FALSE, 0);
95
96 /* Create a (currently unused) handle to record exceptions. */
97 state->except_event = CreateEvent (0, TRUE, FALSE, 0);
98
99 return 0;
100}
101
102/* Wait for the output to drain away, as opposed to flushing (discarding)
103 it. */
104
105static int
107{
108 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
109
110 return (FlushFileBuffers (h) != 0) ? 0 : -1;
111}
112
113static int
115{
116 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
117
118 return (PurgeComm (h, PURGE_TXCLEAR) != 0) ? 0 : -1;
119}
120
121static int
123{
124 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
125
126 return (PurgeComm (h, PURGE_RXCLEAR) != 0) ? 0 : -1;
127}
128
129static int
131{
132 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
133
134 if (SetCommBreak (h) == 0)
135 return -1;
136
137 /* Delay for 250 milliseconds. */
138 Sleep (250);
139
140 if (ClearCommBreak (h))
141 return -1;
142
143 return 0;
144}
145
146static void
148{
149 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
150 DCB state;
151
152 if (GetCommState (h, &state) == 0)
153 return;
154
155 state.fOutxCtsFlow = FALSE;
156 state.fOutxDsrFlow = FALSE;
157 state.fDtrControl = DTR_CONTROL_ENABLE;
158 state.fDsrSensitivity = FALSE;
159 state.fOutX = FALSE;
160 state.fInX = FALSE;
161 state.fNull = FALSE;
162 state.fAbortOnError = FALSE;
163 state.ByteSize = 8;
164
165 if (SetCommState (h, &state) == 0)
166 warning (_("SetCommState failed"));
167}
168
169static int
170ser_windows_setstopbits (struct serial *scb, int num)
171{
172 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
173 DCB state;
174
175 if (GetCommState (h, &state) == 0)
176 return -1;
177
178 switch (num)
179 {
181 state.StopBits = ONESTOPBIT;
182 break;
184 state.StopBits = ONE5STOPBITS;
185 break;
187 state.StopBits = TWOSTOPBITS;
188 break;
189 default:
190 return 1;
191 }
192
193 return (SetCommState (h, &state) != 0) ? 0 : -1;
194}
195
196/* Implement the "setparity" serial_ops callback. */
197
198static int
200{
201 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
202 DCB state;
203
204 if (GetCommState (h, &state) == 0)
205 return -1;
206
207 switch (parity)
208 {
209 case GDBPARITY_NONE:
210 state.Parity = NOPARITY;
211 state.fParity = FALSE;
212 break;
213 case GDBPARITY_ODD:
214 state.Parity = ODDPARITY;
215 state.fParity = TRUE;
216 break;
217 case GDBPARITY_EVEN:
218 state.Parity = EVENPARITY;
219 state.fParity = TRUE;
220 break;
221 default:
222 internal_warning ("Incorrect parity value: %d", parity);
223 return -1;
224 }
225
226 return (SetCommState (h, &state) != 0) ? 0 : -1;
227}
228
229static int
231{
232 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
233 DCB state;
234
235 if (GetCommState (h, &state) == 0)
236 return -1;
237
238 state.BaudRate = rate;
239
240 return (SetCommState (h, &state) != 0) ? 0 : -1;
241}
242
243static void
245{
246 struct ser_windows_state *state;
247
248 /* Stop any pending selects. On Windows 95 OS, CancelIo function does
249 not exist. In that case, it can be replaced by a call to CloseHandle,
250 but this is not necessary here as we do close the Windows handle
251 by calling close (scb->fd) below. */
252 if (CancelIo)
253 CancelIo ((HANDLE) _get_osfhandle (scb->fd));
254 state = (struct ser_windows_state *) scb->state;
255 CloseHandle (state->ov.hEvent);
256 CloseHandle (state->except_event);
257
258 if (scb->fd < 0)
259 return;
260
261 close (scb->fd);
262 scb->fd = -1;
263
264 xfree (scb->state);
265}
266
267static void
268ser_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
269{
270 struct ser_windows_state *state;
271 COMSTAT status;
272 DWORD errors;
273 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
274
275 state = (struct ser_windows_state *) scb->state;
276
277 *except = state->except_event;
278 *read = state->ov.hEvent;
279
280 if (state->in_progress)
281 return;
282
283 /* Reset the mask - we are only interested in any characters which
284 arrive after this point, not characters which might have arrived
285 and already been read. */
286
287 /* This really, really shouldn't be necessary - just the second one.
288 But otherwise an internal flag for EV_RXCHAR does not get
289 cleared, and we get a duplicated event, if the last batch
290 of characters included at least two arriving close together. */
291 if (!SetCommMask (h, 0))
292 warning (_("ser_windows_wait_handle: reseting mask failed"));
293
294 if (!SetCommMask (h, EV_RXCHAR))
295 warning (_("ser_windows_wait_handle: reseting mask failed (2)"));
296
297 /* There's a potential race condition here; we must check cbInQue
298 and not wait if that's nonzero. */
299
300 ClearCommError (h, &errors, &status);
301 if (status.cbInQue > 0)
302 {
303 SetEvent (state->ov.hEvent);
304 return;
305 }
306
307 state->in_progress = 1;
308 ResetEvent (state->ov.hEvent);
309 state->lastCommMask = -2;
310 if (WaitCommEvent (h, &state->lastCommMask, &state->ov))
311 {
312 gdb_assert (state->lastCommMask & EV_RXCHAR);
313 SetEvent (state->ov.hEvent);
314 }
315 else
316 gdb_assert (GetLastError () == ERROR_IO_PENDING);
317}
318
319static int
320ser_windows_read_prim (struct serial *scb, size_t count)
321{
322 struct ser_windows_state *state;
323 OVERLAPPED ov;
324 DWORD bytes_read;
325 HANDLE h;
326
327 state = (struct ser_windows_state *) scb->state;
328 if (state->in_progress)
329 {
330 WaitForSingleObject (state->ov.hEvent, INFINITE);
331 state->in_progress = 0;
332 ResetEvent (state->ov.hEvent);
333 }
334
335 memset (&ov, 0, sizeof (OVERLAPPED));
336 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
337 h = (HANDLE) _get_osfhandle (scb->fd);
338
339 if (!ReadFile (h, scb->buf, /* count */ 1, &bytes_read, &ov))
340 {
341 if (GetLastError () != ERROR_IO_PENDING
342 || !GetOverlappedResult (h, &ov, &bytes_read, TRUE))
343 bytes_read = -1;
344 }
345
346 CloseHandle (ov.hEvent);
347 return bytes_read;
348}
349
350static int
351ser_windows_write_prim (struct serial *scb, const void *buf, size_t len)
352{
353 OVERLAPPED ov;
354 DWORD bytes_written;
355 HANDLE h;
356
357 memset (&ov, 0, sizeof (OVERLAPPED));
358 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
359 h = (HANDLE) _get_osfhandle (scb->fd);
360 if (!WriteFile (h, buf, len, &bytes_written, &ov))
361 {
362 if (GetLastError () != ERROR_IO_PENDING
363 || !GetOverlappedResult (h, &ov, &bytes_written, TRUE))
364 bytes_written = -1;
365 }
366
367 CloseHandle (ov.hEvent);
368 return bytes_written;
369}
370
371/* On Windows, gdb_select is implemented using WaitForMulpleObjects.
372 A "select thread" is created for each file descriptor. These
373 threads looks for activity on the corresponding descriptor, using
374 whatever techniques are appropriate for the descriptor type. When
375 that activity occurs, the thread signals an appropriate event,
376 which wakes up WaitForMultipleObjects.
377
378 Each select thread is in one of two states: stopped or started.
379 Select threads begin in the stopped state. When gdb_select is
380 called, threads corresponding to the descriptors of interest are
381 started by calling a wait_handle function. Each thread that
382 notices activity signals the appropriate event and then reenters
383 the stopped state. Before gdb_select returns it calls the
384 wait_handle_done functions, which return the threads to the stopped
385 state. */
386
391
393{
394 /* Signaled by the select thread to indicate that data is available
395 on the file descriptor. */
397 /* Signaled by the select thread to indicate that an exception has
398 occurred on the file descriptor. */
400 /* Signaled by the select thread to indicate that it has entered the
401 started state. HAVE_STARTED and HAVE_STOPPED are never signaled
402 simultaneously. */
404 /* Signaled by the select thread to indicate that it has stopped,
405 either because data is available (and READ_EVENT is signaled),
406 because an exception has occurred (and EXCEPT_EVENT is signaled),
407 or because STOP_SELECT was signaled. */
409
410 /* Signaled by the main program to tell the select thread to enter
411 the started state. */
413 /* Signaled by the main program to tell the select thread to enter
414 the stopped state. */
416 /* Signaled by the main program to tell the select thread to
417 exit. */
419
420 /* The handle for the select thread. */
421 HANDLE thread;
422 /* The state of the select thread. This field is only accessed in
423 the main program, never by the select thread itself. */
425};
426
427/* Called by a select thread to enter the stopped state. This
428 function does not return until the thread has re-entered the
429 started state. */
430static void
432{
433 HANDLE wait_events[2];
434
435 /* There are two things that can wake us up: a request that we enter
436 the started state, or that we exit this thread. */
437 wait_events[0] = state->start_select;
438 wait_events[1] = state->exit_select;
439 if (WaitForMultipleObjects (2, wait_events, FALSE, INFINITE)
440 != WAIT_OBJECT_0)
441 /* Either the EXIT_SELECT event was signaled (requesting that the
442 thread exit) or an error has occurred. In either case, we exit
443 the thread. */
444 ExitThread (0);
445
446 /* We are now in the started state. */
447 SetEvent (state->have_started);
448}
449
450typedef DWORD WINAPI (*thread_fn_type)(void *);
451
452/* Create a new select thread for SCB executing THREAD_FN. The STATE
453 will be filled in by this function before return. */
454static void
456 struct serial *scb,
457 struct ser_console_state *state)
458{
459 DWORD threadId;
460
461 /* Create all of the events. These are all auto-reset events. */
462 state->read_event = CreateEvent (NULL, FALSE, FALSE, NULL);
463 state->except_event = CreateEvent (NULL, FALSE, FALSE, NULL);
464 state->have_started = CreateEvent (NULL, FALSE, FALSE, NULL);
465 state->have_stopped = CreateEvent (NULL, FALSE, FALSE, NULL);
466 state->start_select = CreateEvent (NULL, FALSE, FALSE, NULL);
467 state->stop_select = CreateEvent (NULL, FALSE, FALSE, NULL);
468 state->exit_select = CreateEvent (NULL, FALSE, FALSE, NULL);
469
470 state->thread = CreateThread (NULL, 0, thread_fn, scb, 0, &threadId);
471 /* The thread begins in the stopped state. */
472 state->thread_state = STS_STOPPED;
473}
474
475/* Destroy the select thread indicated by STATE. */
476static void
478{
479 /* Ask the thread to exit. */
480 SetEvent (state->exit_select);
481 /* Wait until it does. */
482 WaitForSingleObject (state->thread, INFINITE);
483
484 /* Destroy the events. */
485 CloseHandle (state->read_event);
486 CloseHandle (state->except_event);
487 CloseHandle (state->have_started);
488 CloseHandle (state->have_stopped);
489 CloseHandle (state->start_select);
490 CloseHandle (state->stop_select);
491 CloseHandle (state->exit_select);
492}
493
494/* Called by gdb_select to start the select thread indicated by STATE.
495 This function does not return until the thread has started. */
496static void
498{
499 /* Ask the thread to start. */
500 SetEvent (state->start_select);
501 /* Wait until it does. */
502 WaitForSingleObject (state->have_started, INFINITE);
503 /* The thread is now started. */
504 state->thread_state = STS_STARTED;
505}
506
507/* Called by gdb_select to stop the select thread indicated by STATE.
508 This function does not return until the thread has stopped. */
509static void
511{
512 /* If the thread is already in the stopped state, we have nothing to
513 do. Some of the wait_handle functions avoid calling
514 start_select_thread if they notice activity on the relevant file
515 descriptors. The wait_handle_done functions still call
516 stop_select_thread -- but it is already stopped. */
517 if (state->thread_state != STS_STARTED)
518 return;
519 /* Ask the thread to stop. */
520 SetEvent (state->stop_select);
521 /* Wait until it does. */
522 WaitForSingleObject (state->have_stopped, INFINITE);
523 /* The thread is now stopped. */
524 state->thread_state = STS_STOPPED;
525}
526
527static DWORD WINAPI
529{
530 struct serial *scb = (struct serial *) arg;
531 struct ser_console_state *state;
532 int event_index;
533 HANDLE h;
534
535 state = (struct ser_console_state *) scb->state;
536 h = (HANDLE) _get_osfhandle (scb->fd);
537
538 while (1)
539 {
540 HANDLE wait_events[2];
541 INPUT_RECORD record;
542 DWORD n_records;
543
544 select_thread_wait (state);
545
546 while (1)
547 {
548 wait_events[0] = state->stop_select;
549 wait_events[1] = h;
550
551 event_index = WaitForMultipleObjects (2, wait_events,
552 FALSE, INFINITE);
553
554 if (event_index == WAIT_OBJECT_0
555 || WaitForSingleObject (state->stop_select, 0) == WAIT_OBJECT_0)
556 break;
557
558 if (event_index != WAIT_OBJECT_0 + 1)
559 {
560 /* Wait must have failed; assume an error has occurred, e.g.
561 the handle has been closed. */
562 SetEvent (state->except_event);
563 break;
564 }
565
566 /* We've got a pending event on the console. See if it's
567 of interest. */
568 if (!PeekConsoleInput (h, &record, 1, &n_records) || n_records != 1)
569 {
570 /* Something went wrong. Maybe the console is gone. */
571 SetEvent (state->except_event);
572 break;
573 }
574
575 if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
576 {
577 WORD keycode = record.Event.KeyEvent.wVirtualKeyCode;
578
579 /* Ignore events containing only control keys. We must
580 recognize "enhanced" keys which we are interested in
581 reading via getch, if they do not map to ASCII. But we
582 do not want to report input available for e.g. the
583 control key alone. */
584
585 if (record.Event.KeyEvent.uChar.AsciiChar != 0
586 || keycode == VK_PRIOR
587 || keycode == VK_NEXT
588 || keycode == VK_END
589 || keycode == VK_HOME
590 || keycode == VK_LEFT
591 || keycode == VK_UP
592 || keycode == VK_RIGHT
593 || keycode == VK_DOWN
594 || keycode == VK_INSERT
595 || keycode == VK_DELETE)
596 {
597 /* This is really a keypress. */
598 SetEvent (state->read_event);
599 break;
600 }
601 }
602 else if (record.EventType == MOUSE_EVENT)
603 {
604 SetEvent (state->read_event);
605 break;
606 }
607
608 /* Otherwise discard it and wait again. */
609 ReadConsoleInput (h, &record, 1, &n_records);
610 }
611
612 SetEvent(state->have_stopped);
613 }
614 return 0;
615}
616
617static int
619{
620 if (PeekNamedPipe ((HANDLE) _get_osfhandle (fd), NULL, 0, NULL, NULL, NULL))
621 return 1;
622 else
623 return 0;
624}
625
626static int
628{
629 if (GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_DISK)
630 return 1;
631 else
632 return 0;
633}
634
635static DWORD WINAPI
637{
638 struct serial *scb = (struct serial *) arg;
639 struct ser_console_state *state;
640 HANDLE h;
641
642 state = (struct ser_console_state *) scb->state;
643 h = (HANDLE) _get_osfhandle (scb->fd);
644
645 while (1)
646 {
647 DWORD n_avail;
648
649 select_thread_wait (state);
650
651 /* Wait for something to happen on the pipe. */
652 while (1)
653 {
654 if (!PeekNamedPipe (h, NULL, 0, NULL, &n_avail, NULL))
655 {
656 SetEvent (state->except_event);
657 break;
658 }
659
660 if (n_avail > 0)
661 {
662 SetEvent (state->read_event);
663 break;
664 }
665
666 /* Delay 10ms before checking again, but allow the stop
667 event to wake us. */
668 if (WaitForSingleObject (state->stop_select, 10) == WAIT_OBJECT_0)
669 break;
670 }
671
672 SetEvent (state->have_stopped);
673 }
674 return 0;
675}
676
677static DWORD WINAPI
679{
680 struct serial *scb = (struct serial *) arg;
681 struct ser_console_state *state;
682 HANDLE h;
683
684 state = (struct ser_console_state *) scb->state;
685 h = (HANDLE) _get_osfhandle (scb->fd);
686
687 while (1)
688 {
689 select_thread_wait (state);
690
691 if (SetFilePointer (h, 0, NULL, FILE_CURRENT)
692 == INVALID_SET_FILE_POINTER)
693 SetEvent (state->except_event);
694 else
695 SetEvent (state->read_event);
696
697 SetEvent (state->have_stopped);
698 }
699 return 0;
700}
701
702static void
703ser_console_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
704{
705 struct ser_console_state *state = (struct ser_console_state *) scb->state;
706
707 if (state == NULL)
708 {
709 thread_fn_type thread_fn;
710 int is_tty;
711
712 is_tty = isatty (scb->fd);
713 if (!is_tty && !fd_is_file (scb->fd) && !fd_is_pipe (scb->fd))
714 {
715 *read = NULL;
716 *except = NULL;
717 return;
718 }
719
720 state = XCNEW (struct ser_console_state);
721 scb->state = state;
722
723 if (is_tty)
724 thread_fn = console_select_thread;
725 else if (fd_is_pipe (scb->fd))
726 thread_fn = pipe_select_thread;
727 else
728 thread_fn = file_select_thread;
729
730 create_select_thread (thread_fn, scb, state);
731 }
732
733 *read = state->read_event;
734 *except = state->except_event;
735
736 /* Start from a blank state. */
737 ResetEvent (state->read_event);
738 ResetEvent (state->except_event);
739 ResetEvent (state->stop_select);
740
741 /* First check for a key already in the buffer. If there is one,
742 we don't need a thread. This also catches the second key of
743 multi-character returns from getch, for instance for arrow
744 keys. The second half is in a C library internal buffer,
745 and PeekConsoleInput will not find it. */
746 if (_kbhit ())
747 {
748 SetEvent (state->read_event);
749 return;
750 }
751
752 /* Otherwise, start the select thread. */
753 start_select_thread (state);
754}
755
756static void
758{
759 struct ser_console_state *state = (struct ser_console_state *) scb->state;
760
761 if (state == NULL)
762 return;
763
764 stop_select_thread (state);
765}
766
767static void
769{
770 struct ser_console_state *state = (struct ser_console_state *) scb->state;
771
772 if (scb->state)
773 {
774 destroy_select_thread (state);
775 xfree (scb->state);
776 }
777}
778
780{
782};
783
784static serial_ttystate
786{
787 if (isatty (scb->fd))
788 {
789 struct ser_console_ttystate *state;
790
791 state = XNEW (struct ser_console_ttystate);
792 state->is_a_tty = 1;
793 return state;
794 }
795 else
796 return NULL;
797}
798
800{
801 /* Since we use the pipe_select_thread for our select emulation,
802 we need to place the state structure it requires at the front
803 of our state. */
805
806 /* The pex obj for our (one-stage) pipeline. */
807 struct pex_obj *pex;
808
809 /* Streams for the pipeline's input and output. */
810 FILE *input, *output;
811};
812
813static struct pipe_state *
815{
816 struct pipe_state *ps = XCNEW (struct pipe_state);
817
818 ps->wait.read_event = INVALID_HANDLE_VALUE;
819 ps->wait.except_event = INVALID_HANDLE_VALUE;
820 ps->wait.start_select = INVALID_HANDLE_VALUE;
821 ps->wait.stop_select = INVALID_HANDLE_VALUE;
822
823 return ps;
824}
825
826static void
828{
829 int saved_errno = errno;
830
831 if (ps->wait.read_event != INVALID_HANDLE_VALUE)
833
834 /* Close the pipe to the child. We must close the pipe before
835 calling pex_free because pex_free will wait for the child to exit
836 and the child will not exit until the pipe is closed. */
837 if (ps->input)
838 fclose (ps->input);
839 if (ps->pex)
840 {
841 pex_free (ps->pex);
842 /* pex_free closes ps->output. */
843 }
844 else if (ps->output)
845 fclose (ps->output);
846
847 xfree (ps);
848
849 errno = saved_errno;
850}
851
853{
854 void operator() (pipe_state *ps) const
855 {
856 free_pipe_state (ps);
857 }
858};
859
860typedef std::unique_ptr<pipe_state, pipe_state_destroyer> pipe_state_up;
861
862static int
863pipe_windows_open (struct serial *scb, const char *name)
864{
865 FILE *pex_stderr;
866
867 if (name == NULL)
868 error_no_arg (_("child command"));
869
870 if (*name == '|')
871 {
872 name++;
873 name = skip_spaces (name);
874 }
875
876 gdb_argv argv (name);
877
878 if (! argv[0] || argv[0][0] == '\0')
879 error (_("missing child command"));
880
882
883 ps->pex = pex_init (PEX_USE_PIPES, "target remote pipe", NULL);
884 if (! ps->pex)
885 return -1;
886 ps->input = pex_input_pipe (ps->pex, 1);
887 if (! ps->input)
888 return -1;
889
890 {
891 int err;
892 const char *err_msg
893 = pex_run (ps->pex, PEX_SEARCH | PEX_BINARY_INPUT | PEX_BINARY_OUTPUT
894 | PEX_STDERR_TO_PIPE,
895 argv[0], argv.get (), NULL, NULL,
896 &err);
897
898 if (err_msg)
899 {
900 /* Our caller expects us to return -1, but all they'll do with
901 it generally is print the message based on errno. We have
902 all the same information here, plus err_msg provided by
903 pex_run, so we just raise the error here. */
904 if (err)
905 error (_("error starting child process '%s': %s: %s"),
906 name, err_msg, safe_strerror (err));
907 else
908 error (_("error starting child process '%s': %s"),
909 name, err_msg);
910 }
911 }
912
913 ps->output = pex_read_output (ps->pex, 1);
914 if (! ps->output)
915 return -1;
916 scb->fd = fileno (ps->output);
917
918 pex_stderr = pex_read_err (ps->pex, 1);
919 if (! pex_stderr)
920 return -1;
921 scb->error_fd = fileno (pex_stderr);
922
923 scb->state = ps.release ();
924
925 return 0;
926}
927
928static int
929pipe_windows_fdopen (struct serial *scb, int fd)
930{
931 struct pipe_state *ps;
932
933 ps = make_pipe_state ();
934
935 ps->input = fdopen (fd, "r+");
936 if (! ps->input)
937 goto fail;
938
939 ps->output = fdopen (fd, "r+");
940 if (! ps->output)
941 goto fail;
942
943 scb->fd = fd;
944 scb->state = (void *) ps;
945
946 return 0;
947
948 fail:
949 free_pipe_state (ps);
950 return -1;
951}
952
953static void
955{
956 struct pipe_state *ps = (struct pipe_state *) scb->state;
957
958 /* In theory, we should try to kill the subprocess here, but the pex
959 interface doesn't give us enough information to do that. Usually
960 closing the input pipe will get the message across. */
961
962 free_pipe_state (ps);
963}
964
965
966static int
967pipe_windows_read (struct serial *scb, size_t count)
968{
969 HANDLE pipeline_out = (HANDLE) _get_osfhandle (scb->fd);
970 DWORD available;
971 DWORD bytes_read;
972
973 if (pipeline_out == INVALID_HANDLE_VALUE)
974 return -1;
975
976 if (! PeekNamedPipe (pipeline_out, NULL, 0, NULL, &available, NULL))
977 return -1;
978
979 if (count > available)
980 count = available;
981
982 if (! ReadFile (pipeline_out, scb->buf, count, &bytes_read, NULL))
983 return -1;
984
985 return bytes_read;
986}
987
988
989static int
990pipe_windows_write (struct serial *scb, const void *buf, size_t count)
991{
992 struct pipe_state *ps = (struct pipe_state *) scb->state;
993 HANDLE pipeline_in;
994 DWORD written;
995
996 int pipeline_in_fd = fileno (ps->input);
997 if (pipeline_in_fd < 0)
998 return -1;
999
1000 pipeline_in = (HANDLE) _get_osfhandle (pipeline_in_fd);
1001 if (pipeline_in == INVALID_HANDLE_VALUE)
1002 return -1;
1003
1004 if (! WriteFile (pipeline_in, buf, count, &written, NULL))
1005 return -1;
1006
1007 return written;
1008}
1009
1010
1011static void
1012pipe_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
1013{
1014 struct pipe_state *ps = (struct pipe_state *) scb->state;
1015
1016 /* Have we allocated our events yet? */
1017 if (ps->wait.read_event == INVALID_HANDLE_VALUE)
1018 /* Start the thread. */
1020
1021 *read = ps->wait.read_event;
1022 *except = ps->wait.except_event;
1023
1024 /* Start from a blank state. */
1025 ResetEvent (ps->wait.read_event);
1026 ResetEvent (ps->wait.except_event);
1027 ResetEvent (ps->wait.stop_select);
1028
1030}
1031
1032static void
1034{
1035 struct pipe_state *ps = (struct pipe_state *) scb->state;
1036
1037 /* Have we allocated our events yet? */
1038 if (ps->wait.read_event == INVALID_HANDLE_VALUE)
1039 return;
1040
1041 stop_select_thread (&ps->wait);
1042}
1043
1044static int
1045pipe_avail (struct serial *scb, int fd)
1046{
1047 HANDLE h = (HANDLE) _get_osfhandle (fd);
1048 DWORD numBytes;
1049 BOOL r = PeekNamedPipe (h, NULL, 0, NULL, &numBytes, NULL);
1050
1051 if (r == FALSE)
1052 numBytes = 0;
1053 return numBytes;
1054}
1055
1056int
1057gdb_pipe (int pdes[2])
1058{
1059 if (_pipe (pdes, 512, _O_BINARY | _O_NOINHERIT) == -1)
1060 return -1;
1061 return 0;
1062}
1063
1065{
1067
1069};
1070
1071/* Check whether the socket has any pending data to be read. If so,
1072 set the select thread's read event. On error, set the select
1073 thread's except event. If any event was set, return true,
1074 otherwise return false. */
1075
1076static int
1078{
1079 struct net_windows_state *state = (struct net_windows_state *) scb->state;
1080 unsigned long available;
1081
1082 if (ioctlsocket (scb->fd, FIONREAD, &available) != 0)
1083 {
1084 /* The socket closed, or some other error. */
1085 SetEvent (state->base.except_event);
1086 return 1;
1087 }
1088 else if (available > 0)
1089 {
1090 SetEvent (state->base.read_event);
1091 return 1;
1092 }
1093
1094 return 0;
1095}
1096
1097static DWORD WINAPI
1099{
1100 struct serial *scb = (struct serial *) arg;
1101 struct net_windows_state *state;
1102 int event_index;
1103
1104 state = (struct net_windows_state *) scb->state;
1105
1106 while (1)
1107 {
1108 HANDLE wait_events[2];
1109 WSANETWORKEVENTS events;
1110
1111 select_thread_wait (&state->base);
1112
1113 wait_events[0] = state->base.stop_select;
1114 wait_events[1] = state->sock_event;
1115
1116 /* Wait for something to happen on the socket. */
1117 while (1)
1118 {
1119 event_index = WaitForMultipleObjects (2, wait_events, FALSE, INFINITE);
1120
1121 if (event_index == WAIT_OBJECT_0
1122 || WaitForSingleObject (state->base.stop_select, 0) == WAIT_OBJECT_0)
1123 {
1124 /* We have been requested to stop. */
1125 break;
1126 }
1127
1128 if (event_index != WAIT_OBJECT_0 + 1)
1129 {
1130 /* Some error has occurred. Assume that this is an error
1131 condition. */
1132 SetEvent (state->base.except_event);
1133 break;
1134 }
1135
1136 /* Enumerate the internal network events, and reset the
1137 object that signalled us to catch the next event. */
1138 if (WSAEnumNetworkEvents (scb->fd, state->sock_event, &events) != 0)
1139 {
1140 /* Something went wrong. Maybe the socket is gone. */
1141 SetEvent (state->base.except_event);
1142 break;
1143 }
1144
1145 if (events.lNetworkEvents & FD_READ)
1146 {
1148 break;
1149
1150 /* Spurious wakeup. That is, the socket's event was
1151 signalled before we last called recv. */
1152 }
1153
1154 if (events.lNetworkEvents & FD_CLOSE)
1155 {
1156 SetEvent (state->base.except_event);
1157 break;
1158 }
1159 }
1160
1161 SetEvent (state->base.have_stopped);
1162 }
1163 return 0;
1164}
1165
1166static void
1167net_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
1168{
1169 struct net_windows_state *state = (struct net_windows_state *) scb->state;
1170
1171 /* Start from a clean slate. */
1172 ResetEvent (state->base.read_event);
1173 ResetEvent (state->base.except_event);
1174 ResetEvent (state->base.stop_select);
1175
1176 *read = state->base.read_event;
1177 *except = state->base.except_event;
1178
1179 /* Check any pending events. Otherwise, start the select
1180 thread. */
1182 start_select_thread (&state->base);
1183}
1184
1185static void
1187{
1188 struct net_windows_state *state = (struct net_windows_state *) scb->state;
1189
1190 stop_select_thread (&state->base);
1191}
1192
1193static int
1194net_windows_open (struct serial *scb, const char *name)
1195{
1196 struct net_windows_state *state;
1197 int ret;
1198
1199 ret = net_open (scb, name);
1200 if (ret != 0)
1201 return ret;
1202
1203 state = XCNEW (struct net_windows_state);
1204 scb->state = state;
1205
1206 /* Associate an event with the socket. */
1207 state->sock_event = CreateEvent (0, TRUE, FALSE, 0);
1208 WSAEventSelect (scb->fd, state->sock_event, FD_READ | FD_CLOSE);
1209
1210 /* Start the thread. */
1212
1213 return 0;
1214}
1215
1216
1217static void
1219{
1220 struct net_windows_state *state = (struct net_windows_state *) scb->state;
1221
1222 destroy_select_thread (&state->base);
1223 CloseHandle (state->sock_event);
1224
1225 xfree (scb->state);
1226
1227 net_close (scb);
1228}
1229
1230/* The serial port driver. */
1231
1232static const struct serial_ops hardwire_ops =
1233{
1234 "hardwire",
1237 NULL,
1244 /* These are only used for stdin; we do not need them for serial
1245 ports, so supply the standard dummies. */
1257 NULL,
1259};
1260
1261/* The dummy serial driver used for terminals. We only provide the
1262 TTY-related methods. */
1263
1264static const struct serial_ops tty_ops =
1265{
1266 "terminal",
1267 NULL,
1269 NULL,
1270 NULL,
1271 NULL,
1272 NULL,
1273 NULL,
1274 NULL,
1275 NULL,
1280 NULL,
1281 NULL,
1282 NULL,
1284 NULL,
1285 NULL,
1286 NULL,
1287 NULL,
1290};
1291
1292/* The pipe interface. */
1293
1321
1322/* The TCP/UDP socket driver. */
1323
1351
1353void
1355{
1356 WSADATA wsa_data;
1357
1358 HMODULE hm = NULL;
1359
1360 /* First find out if kernel32 exports CancelIo function. */
1361 hm = LoadLibrary ("kernel32.dll");
1362 if (hm)
1363 {
1364 CancelIo = (CancelIo_ftype *) GetProcAddress (hm, "CancelIo");
1365 FreeLibrary (hm);
1366 }
1367 else
1368 CancelIo = NULL;
1369
1373
1374 /* If WinSock works, register the TCP/UDP socket driver. */
1375
1376 if (WSAStartup (MAKEWORD (1, 0), &wsa_data) != 0)
1377 /* WinSock is unavailable. */
1378 return;
1379
1381}
const char *const name
void xfree(void *)
void error_no_arg(const char *why)
Definition cli-cmds.c:206
#define BOOL
ssize_t read(int fd, void *buf, size_t count)
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t err
Definition gnu-nat.c:1789
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int status
Definition gnu-nat.c:1790
unsigned available
Definition go32-nat.c:7
void ser_base_async(struct serial *scb, int async_p)
Definition ser-base.c:587
void ser_base_print_tty_state(struct serial *scb, serial_ttystate ttystate, struct ui_file *stream)
Definition ser-base.c:556
void ser_base_raw(struct serial *scb)
Definition ser-base.c:530
int ser_base_set_tty_state(struct serial *scb, serial_ttystate ttystate)
Definition ser-base.c:550
serial_ttystate ser_base_get_tty_state(struct serial *scb)
Definition ser-base.c:536
int ser_base_drain_output(struct serial *scb)
Definition ser-base.c:524
int ser_base_send_break(struct serial *scb)
Definition ser-base.c:518
int ser_base_setbaudrate(struct serial *scb, int rate)
Definition ser-base.c:565
serial_ttystate ser_base_copy_tty_state(struct serial *scb, serial_ttystate ttystate)
Definition ser-base.c:543
int ser_base_write(struct serial *scb, const void *buf, size_t count)
Definition ser-base.c:475
int ser_base_setstopbits(struct serial *scb, int num)
Definition ser-base.c:571
int ser_base_flush_output(struct serial *scb)
Definition ser-base.c:499
int ser_base_flush_input(struct serial *scb)
Definition ser-base.c:505
int ser_base_readchar(struct serial *scb, int timeout)
Definition ser-base.c:469
int ser_base_setparity(struct serial *scb, int parity)
Definition ser-base.c:579
static void ser_windows_raw(struct serial *scb)
Definition ser-mingw.c:147
static int ser_windows_send_break(struct serial *scb)
Definition ser-mingw.c:130
static serial_ttystate ser_console_get_tty_state(struct serial *scb)
Definition ser-mingw.c:785
static int pipe_windows_write(struct serial *scb, const void *buf, size_t count)
Definition ser-mingw.c:990
static int ser_windows_flush_input(struct serial *scb)
Definition ser-mingw.c:122
static int ser_windows_setbaudrate(struct serial *scb, int rate)
Definition ser-mingw.c:230
static const struct serial_ops tcp_ops
Definition ser-mingw.c:1324
static void destroy_select_thread(struct ser_console_state *state)
Definition ser-mingw.c:477
static int pipe_windows_open(struct serial *scb, const char *name)
Definition ser-mingw.c:863
static void pipe_windows_close(struct serial *scb)
Definition ser-mingw.c:954
static const struct serial_ops hardwire_ops
Definition ser-mingw.c:1232
static int fd_is_file(int fd)
Definition ser-mingw.c:627
static int ser_windows_flush_output(struct serial *scb)
Definition ser-mingw.c:114
DWORD WINAPI(* thread_fn_type)(void *)
Definition ser-mingw.c:450
static int pipe_windows_fdopen(struct serial *scb, int fd)
Definition ser-mingw.c:929
static int ser_windows_open(struct serial *scb, const char *name)
Definition ser-mingw.c:52
int gdb_pipe(int pdes[2])
Definition ser-mingw.c:1057
static void stop_select_thread(struct ser_console_state *state)
Definition ser-mingw.c:510
static DWORD WINAPI net_windows_select_thread(void *arg)
Definition ser-mingw.c:1098
static int fd_is_pipe(int fd)
Definition ser-mingw.c:618
static struct pipe_state * make_pipe_state(void)
Definition ser-mingw.c:814
static void ser_windows_wait_handle(struct serial *scb, HANDLE *read, HANDLE *except)
Definition ser-mingw.c:268
void _initialize_ser_windows()
Definition ser-mingw.c:1354
static void ser_console_done_wait_handle(struct serial *scb)
Definition ser-mingw.c:757
static int ser_windows_drain_output(struct serial *scb)
Definition ser-mingw.c:106
static void select_thread_wait(struct ser_console_state *state)
Definition ser-mingw.c:431
select_thread_state
Definition ser-mingw.c:387
@ STS_STOPPED
Definition ser-mingw.c:389
@ STS_STARTED
Definition ser-mingw.c:388
static void ser_console_wait_handle(struct serial *scb, HANDLE *read, HANDLE *except)
Definition ser-mingw.c:703
static int net_windows_socket_check_pending(struct serial *scb)
Definition ser-mingw.c:1077
BOOL WINAPI CancelIo_ftype(HANDLE)
Definition ser-mingw.c:46
static DWORD WINAPI pipe_select_thread(void *arg)
Definition ser-mingw.c:636
#define CancelIo
Definition ser-mingw.c:45
static int ser_windows_read_prim(struct serial *scb, size_t count)
Definition ser-mingw.c:320
static void create_select_thread(thread_fn_type thread_fn, struct serial *scb, struct ser_console_state *state)
Definition ser-mingw.c:455
static void free_pipe_state(struct pipe_state *ps)
Definition ser-mingw.c:827
std::unique_ptr< pipe_state, pipe_state_destroyer > pipe_state_up
Definition ser-mingw.c:860
static void ser_console_close(struct serial *scb)
Definition ser-mingw.c:768
static DWORD WINAPI console_select_thread(void *arg)
Definition ser-mingw.c:528
static void pipe_done_wait_handle(struct serial *scb)
Definition ser-mingw.c:1033
static int ser_windows_setparity(struct serial *scb, int parity)
Definition ser-mingw.c:199
static void start_select_thread(struct ser_console_state *state)
Definition ser-mingw.c:497
static void net_windows_close(struct serial *scb)
Definition ser-mingw.c:1218
static void net_windows_done_wait_handle(struct serial *scb)
Definition ser-mingw.c:1186
static int ser_windows_write_prim(struct serial *scb, const void *buf, size_t len)
Definition ser-mingw.c:351
static DWORD WINAPI file_select_thread(void *arg)
Definition ser-mingw.c:678
static const struct serial_ops pipe_ops
Definition ser-mingw.c:1294
static void net_windows_wait_handle(struct serial *scb, HANDLE *read, HANDLE *except)
Definition ser-mingw.c:1167
static int ser_windows_setstopbits(struct serial *scb, int num)
Definition ser-mingw.c:170
static int net_windows_open(struct serial *scb, const char *name)
Definition ser-mingw.c:1194
static int pipe_avail(struct serial *scb, int fd)
Definition ser-mingw.c:1045
static void ser_windows_close(struct serial *scb)
Definition ser-mingw.c:244
static int pipe_windows_read(struct serial *scb, size_t count)
Definition ser-mingw.c:967
static void pipe_wait_handle(struct serial *scb, HANDLE *read, HANDLE *except)
Definition ser-mingw.c:1012
static const struct serial_ops tty_ops
Definition ser-mingw.c:1264
int net_write_prim(struct serial *scb, const void *buf, size_t count)
Definition ser-tcp.c:411
int net_read_prim(struct serial *scb, size_t count)
Definition ser-tcp.c:402
int net_open(struct serial *scb, const char *name)
Definition ser-tcp.c:276
int ser_tcp_send_break(struct serial *scb)
Definition ser-tcp.c:421
void net_close(struct serial *scb)
Definition ser-tcp.c:392
int rate
Definition ser-unix.c:242
static const char * parity
Definition serial.c:641
void serial_add_interface(const struct serial_ops *optable)
Definition serial.c:154
#define GDBPARITY_NONE
Definition serial.h:194
#define GDBPARITY_EVEN
Definition serial.h:196
#define SERIAL_2_STOPBITS
Definition serial.h:190
#define SERIAL_1_AND_A_HALF_STOPBITS
Definition serial.h:189
void * serial_ttystate
Definition serial.h:35
#define SERIAL_1_STOPBITS
Definition serial.h:188
#define GDBPARITY_ODD
Definition serial.h:195
struct ser_console_state base
Definition ser-mingw.c:1066
void operator()(pipe_state *ps) const
Definition ser-mingw.c:854
struct ser_console_state wait
Definition ser-mingw.c:804
struct pex_obj * pex
Definition ser-mingw.c:807
FILE * output
Definition ser-mingw.c:810
FILE * input
Definition ser-mingw.c:810
enum select_thread_state thread_state
Definition ser-mingw.c:424
HANDLE except_event
Definition ser-mingw.c:40
OVERLAPPED ov
Definition ser-mingw.c:38
int fd
Definition serial.h:238
unsigned char buf[BUFSIZ]
Definition serial.h:250
void * state
Definition serial.h:245
int error_fd
Definition serial.h:243