From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30153 invoked by alias); 26 Jan 2007 13:54:19 -0000 Received: (qmail 30143 invoked by uid 22791); 26 Jan 2007 13:54:18 -0000 X-Spam-Check-By: sourceware.org Received: from mail.codesourcery.com (HELO mail.codesourcery.com) (65.74.133.4) by sourceware.org (qpsmtpd/0.31) with ESMTP; Fri, 26 Jan 2007 13:54:14 +0000 Received: (qmail 7895 invoked from network); 26 Jan 2007 13:54:11 -0000 Received: from unknown (HELO ?172.16.64.38?) (vladimir@127.0.0.2) by mail.codesourcery.com with ESMTPA; 26 Jan 2007 13:54:11 -0000 From: Vladimir Prus To: gdb-patches@sources.redhat.com Subject: "target remote | " stderr Date: Fri, 26 Jan 2007 13:54:00 -0000 User-Agent: KMail/1.9.1 MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_xfguFUFST40uHFY" Message-Id: <200701261653.53834.vladimir@codesourcery.com> Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2007-01/txt/msg00534.txt.bz2 --Boundary-00=_xfguFUFST40uHFY Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Content-length: 735 At the moment, when handling target remote | whatever gdb does not do anything with 'whatever''s stderr. This is not good, because when using MI, frontend might not even look at stderr at all, so messages from 'whatever' get lost. This patch fixes that. The fix is only for Linux, I plan to do the same with mingw support, but only if this patch is considered OK. - Volodya Pass stderr of program run with "target remote |" via gdb_stderr. * serial.c (serial_open): Set error_fd to -1. * serial.h (struct serial): New field error_fd. * ser-pipe.c (pipe_open): Create another pair of sockets. Pass stderr to gdb. * ser-base.c (generic_readchar): Check if there's anything in stderr channel and route that to gdb_stderr. --Boundary-00=_xfguFUFST40uHFY Content-Type: text/x-diff; charset="us-ascii"; name="remote_stderr__gdb_mainline.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="remote_stderr__gdb_mainline.diff" Content-length: 3660 --- gdb/serial.c (/mirrors/gdb_mainline) (revision 3222) +++ gdb/serial.c (/patches/gdb/remote_stderr/gdb_mainline) (revision 3222) @@ -211,6 +211,7 @@ serial_open (const char *name) scb->bufcnt = 0; scb->bufp = scb->buf; + scb->error_fd = -1; if (scb->ops->open (scb, open_name)) { --- gdb/serial.h (/mirrors/gdb_mainline) (revision 3222) +++ gdb/serial.h (/patches/gdb/remote_stderr/gdb_mainline) (revision 3222) @@ -191,6 +191,12 @@ extern int serial_debug_p (struct serial struct serial { int fd; /* File descriptor */ + int error_fd; /* File descriptor for a separate + error stream that should be + immediately forwarded to gdb_stderr. + This may be -1. + If != -1, this descriptor should + be non-blocking. */ struct serial_ops *ops; /* Function vector */ void *state; /* Local context info for open FD */ serial_ttystate ttystate; /* Not used (yet) */ --- gdb/ser-pipe.c (/mirrors/gdb_mainline) (revision 3222) +++ gdb/ser-pipe.c (/patches/gdb/remote_stderr/gdb_mainline) (revision 3222) @@ -62,9 +62,12 @@ pipe_open (struct serial *scb, const cha * published in UNIX Review, Vol. 6, No. 8. */ int pdes[2]; + int err_pdes[2]; int pid; if (socketpair (AF_UNIX, SOCK_STREAM, 0, pdes) < 0) return -1; + if (socketpair (AF_UNIX, SOCK_STREAM, 0, err_pdes) < 0) + return -1; /* Create the child process to run the command in. Note that the apparent call to vfork() below *might* actually be a call to @@ -77,9 +80,18 @@ pipe_open (struct serial *scb, const cha { close (pdes[0]); close (pdes[1]); + close (err_pdes[0]); + close (err_pdes[1]); return -1; } + if (fcntl (err_pdes[0], F_SETFL, O_NONBLOCK) == -1) + { + close (err_pdes[0]); + close (err_pdes[1]); + err_pdes[0] = err_pdes[1] = -1; + } + /* Child. */ if (pid == 0) { @@ -91,6 +103,13 @@ pipe_open (struct serial *scb, const cha close (pdes[1]); } dup2 (STDOUT_FILENO, STDIN_FILENO); + + if (err_pdes[0] != -1) + { + close (err_pdes[0]); + dup2 (err_pdes[1], STDERR_FILENO); + close (err_pdes[1]); + } #if 0 /* close any stray FD's - FIXME - how? */ /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams @@ -109,6 +128,7 @@ pipe_open (struct serial *scb, const cha state = XMALLOC (struct pipe_state); state->pid = pid; scb->fd = pdes[0]; + scb->error_fd = err_pdes[0]; scb->state = state; /* If we don't do this, GDB simply exits when the remote side dies. */ --- gdb/ser-base.c (/mirrors/gdb_mainline) (revision 3222) +++ gdb/ser-base.c (/patches/gdb/remote_stderr/gdb_mainline) (revision 3222) @@ -314,6 +314,33 @@ generic_readchar (struct serial *scb, in int (do_readchar) (struct serial *scb, int timeout)) { int ch; + + /* Read any error output we might have. */ + if (scb->error_fd != -1) + { + ssize_t s; + char buf[81]; + while ((s = read (scb->error_fd, &buf, 80)) > 0) + { + char *current; + char *newline; + /* In theory, embedded newlines are not a problem. + But for MI, we want each output line to have just + one newline for legibility. So output things + in newline chunks. */ + buf[s] = '\0'; + current = buf; + while ((newline = strstr (current, "\n")) != NULL) + { + *newline = '\0'; + fputs_unfiltered (current, gdb_stderr); + fputs_unfiltered ("\n", gdb_stderr); + current = newline + 1; + } + fputs_unfiltered (current, gdb_stderr); + } + } + if (scb->bufcnt > 0) { ch = *scb->bufp; --Boundary-00=_xfguFUFST40uHFY--