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