+ /* The pex obj for our (one-stage) pipeline. */
+ struct pex_obj *pex;
+
+ /* Streams for the pipeline's input and output. */
+ FILE *input, *output;
+};
+
+static struct pipe_state *
+make_pipe_state (void)
+{
+ struct pipe_state *ps = XCNEW (struct pipe_state);
+
+ ps->wait.read_event = INVALID_HANDLE_VALUE;
+ ps->wait.except_event = INVALID_HANDLE_VALUE;
+ ps->wait.start_select = INVALID_HANDLE_VALUE;
+ ps->wait.stop_select = INVALID_HANDLE_VALUE;
+
+ return ps;
+}
+
+static void
+free_pipe_state (struct pipe_state *ps)
+{
+ int saved_errno = errno;
+
+ if (ps->wait.read_event != INVALID_HANDLE_VALUE)
+ destroy_select_thread (&ps->wait);
+
+ /* Close the pipe to the child. We must close the pipe before
+ calling pex_free because pex_free will wait for the child to exit
+ and the child will not exit until the pipe is closed. */
+ if (ps->input)
+ fclose (ps->input);
+ if (ps->pex)
+ {
+ pex_free (ps->pex);
+ /* pex_free closes ps->output. */
+ }
+ else if (ps->output)
+ fclose (ps->output);
+
+ xfree (ps);
+
+ errno = saved_errno;
+}
+
+struct pipe_state_destroyer
+{
+ void operator() (pipe_state *ps) const
+ {
+ free_pipe_state (ps);
+ }
+};
+
+typedef std::unique_ptr<pipe_state, pipe_state_destroyer> pipe_state_up;
+
+static int
+pipe_windows_open (struct serial *scb, const char *name)
+{
+ FILE *pex_stderr;
+
+ if (name == NULL)
+ error_no_arg (_("child command"));
+
+ gdb_argv argv (name);
+
+ if (! argv[0] || argv[0][0] == '\0')
+ error (_("missing child command"));
+
+ pipe_state_up ps (make_pipe_state ());
+
+ ps->pex = pex_init (PEX_USE_PIPES, "target remote pipe", NULL);
+ if (! ps->pex)
+ return -1;
+ ps->input = pex_input_pipe (ps->pex, 1);
+ if (! ps->input)
+ return -1;
+
+ {
+ int err;
+ const char *err_msg
+ = pex_run (ps->pex, PEX_SEARCH | PEX_BINARY_INPUT | PEX_BINARY_OUTPUT
+ | PEX_STDERR_TO_PIPE,
+ argv[0], argv.get (), NULL, NULL,
+ &err);
+
+ if (err_msg)
+ {
+ /* Our caller expects us to return -1, but all they'll do with
+ it generally is print the message based on errno. We have
+ all the same information here, plus err_msg provided by
+ pex_run, so we just raise the error here. */
+ if (err)
+ error (_("error starting child process '%s': %s: %s"),
+ name, err_msg, safe_strerror (err));
+ else
+ error (_("error starting child process '%s': %s"),
+ name, err_msg);
+ }
+ }
+
+ ps->output = pex_read_output (ps->pex, 1);
+ if (! ps->output)
+ return -1;
+ scb->fd = fileno (ps->output);
+
+ pex_stderr = pex_read_err (ps->pex, 1);
+ if (! pex_stderr)
+ return -1;
+ scb->error_fd = fileno (pex_stderr);
+
+ scb->state = ps.release ();
+
+ return 0;
+}
+
+static int
+pipe_windows_fdopen (struct serial *scb, int fd)
+{
+ struct pipe_state *ps;
+
+ ps = make_pipe_state ();
+
+ ps->input = fdopen (fd, "r+");
+ if (! ps->input)
+ goto fail;
+
+ ps->output = fdopen (fd, "r+");
+ if (! ps->output)
+ goto fail;
+
+ scb->fd = fd;
+ scb->state = (void *) ps;
+
+ return 0;
+
+ fail:
+ free_pipe_state (ps);
+ return -1;
+}
+
+static void
+pipe_windows_close (struct serial *scb)
+{
+ struct pipe_state *ps = (struct pipe_state *) scb->state;
+
+ /* In theory, we should try to kill the subprocess here, but the pex
+ interface doesn't give us enough information to do that. Usually
+ closing the input pipe will get the message across. */
+
+ free_pipe_state (ps);
+}
+
+
+static int
+pipe_windows_read (struct serial *scb, size_t count)
+{
+ HANDLE pipeline_out = (HANDLE) _get_osfhandle (scb->fd);
+ DWORD available;
+ DWORD bytes_read;
+
+ if (pipeline_out == INVALID_HANDLE_VALUE)
+ return -1;
+
+ if (! PeekNamedPipe (pipeline_out, NULL, 0, NULL, &available, NULL))
+ return -1;
+
+ if (count > available)
+ count = available;
+
+ if (! ReadFile (pipeline_out, scb->buf, count, &bytes_read, NULL))
+ return -1;
+
+ return bytes_read;
+}
+
+
+static int
+pipe_windows_write (struct serial *scb, const void *buf, size_t count)
+{
+ struct pipe_state *ps = (struct pipe_state *) scb->state;
+ HANDLE pipeline_in;
+ DWORD written;
+
+ int pipeline_in_fd = fileno (ps->input);
+ if (pipeline_in_fd < 0)
+ return -1;
+
+ pipeline_in = (HANDLE) _get_osfhandle (pipeline_in_fd);
+ if (pipeline_in == INVALID_HANDLE_VALUE)
+ return -1;
+
+ if (! WriteFile (pipeline_in, buf, count, &written, NULL))
+ return -1;
+
+ return written;
+}
+
+
+static void
+pipe_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
+{
+ struct pipe_state *ps = (struct pipe_state *) scb->state;
+
+ /* Have we allocated our events yet? */
+ if (ps->wait.read_event == INVALID_HANDLE_VALUE)
+ /* Start the thread. */
+ create_select_thread (pipe_select_thread, scb, &ps->wait);
+
+ *read = ps->wait.read_event;
+ *except = ps->wait.except_event;
+
+ /* Start from a blank state. */
+ ResetEvent (ps->wait.read_event);
+ ResetEvent (ps->wait.except_event);
+ ResetEvent (ps->wait.stop_select);
+
+ start_select_thread (&ps->wait);
+}
+
+static void
+pipe_done_wait_handle (struct serial *scb)
+{
+ struct pipe_state *ps = (struct pipe_state *) scb->state;
+
+ /* Have we allocated our events yet? */
+ if (ps->wait.read_event == INVALID_HANDLE_VALUE)
+ return;
+
+ stop_select_thread (&ps->wait);
+}
+
+static int
+pipe_avail (struct serial *scb, int fd)
+{
+ HANDLE h = (HANDLE) _get_osfhandle (fd);
+ DWORD numBytes;
+ BOOL r = PeekNamedPipe (h, NULL, 0, NULL, &numBytes, NULL);
+
+ if (r == FALSE)
+ numBytes = 0;
+ return numBytes;
+}
+
+int
+gdb_pipe (int pdes[2])
+{
+ if (_pipe (pdes, 512, _O_BINARY | _O_NOINHERIT) == -1)
+ return -1;
+ return 0;
+}
+
+struct net_windows_state
+{
+ struct ser_console_state base;
+