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