From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27904 invoked by alias); 22 Aug 2013 06:14:50 -0000 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 Received: (qmail 27884 invoked by uid 89); 22 Aug 2013 06:14:49 -0000 X-Spam-SWARE-Status: No, score=-4.6 required=5.0 tests=AWL,BAYES_00,KHOP_RCVD_UNTRUST,KHOP_THREADED,RCVD_IN_HOSTKARMA_W,RCVD_IN_HOSTKARMA_WL autolearn=ham version=3.3.2 Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Thu, 22 Aug 2013 06:14:49 +0000 Received: from svr-orw-fem-01.mgc.mentorg.com ([147.34.98.93]) by relay1.mentorg.com with esmtp id 1VCOAQ-0000Uc-Dw from Yao_Qi@mentor.com ; Wed, 21 Aug 2013 23:14:46 -0700 Received: from SVR-ORW-FEM-02.mgc.mentorg.com ([147.34.96.206]) by svr-orw-fem-01.mgc.mentorg.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.4675); Wed, 21 Aug 2013 23:14:46 -0700 Received: from qiyao.dyndns.org (147.34.91.1) by svr-orw-fem-02.mgc.mentorg.com (147.34.96.168) with Microsoft SMTP Server id 14.2.247.3; Wed, 21 Aug 2013 23:14:45 -0700 Message-ID: <5215AC28.3030506@codesourcery.com> Date: Thu, 22 Aug 2013 06:14:00 -0000 From: Yao Qi User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130110 Thunderbird/17.0.2 MIME-Version: 1.0 To: Pedro Alves CC: Eli Zaretskii , , Subject: Re: [PATCH] Unbuffer stdout and stderr on windows References: <51EE23F8.1070905@codesourcery.com> <83wqohw4ee.fsf@gnu.org> <20130729192559.GA5348@ednor.casa.cgf.cx> <83d2q1xiyv.fsf@gnu.org> <51F6C7B2.3020400@redhat.com> <20130731034045.GA5565@ednor.casa.cgf.cx> <20130812211105.GA11128@adacore.com> <8361v9piop.fsf@gnu.org> <20130815173618.GA6955@ednor.casa.cgf.cx> <83eh9uonlg.fsf@gnu.org> <20130815175940.GD6955@ednor.casa.cgf.cx> <520E1109.7000304@redhat.com> <520E1C34.2000907@codesourcery.com> <520E2B13.8020706@redhat.com> <83r4dtn35q.fsf@gnu.org> <520E357E.6080803@redhat.com> <83mwohn0nj.fsf@gnu.org> <520E40CD.7080604@redhat.com> In-Reply-To: <520E40CD.7080604@redhat.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-SW-Source: 2013-08/txt/msg00619.txt.bz2 On 08/16/2013 11:10 PM, Pedro Alves wrote: > - First, make stderr always unbuffered. That's what you get on > most platforms anyway. > - Then, somewhere along fputs_unfiltered or some central output routine, > keep track of which was the last to be used between stdout and stderr. > If outputting to stderr, flush stdout first. Don't need to do the > opposite, since stderr will always be unbuffered. This patch below generally implements what we discussed here... In this patch, we firstly set stderr unbuffered. Then, we overwrite gdb_stderr's methods 'to_write' and 'to_fputs', so that gdb_stdout is flushed when something is written to gdb_stderr. Regression tested on mingw32 native gdb with a local patch to set stdout/stderr to binary mode. -- Yao (齐尧) gdb: 2013-08-22 Yao Qi * main.c (captured_main) [__MINGW32__]: Set stderr unbuffered. and all update_stderr_ui_file. * ui-file.c (stderr_file_write): New function. (stderr_file_fputs): New function. (update_stderr_ui_file): New function. * ui-file.h (update_stderr_ui_file): Declare. --- gdb/main.c | 19 +++++++++++++++++++ gdb/ui-file.c | 26 ++++++++++++++++++++++++++ gdb/ui-file.h | 3 +++ 3 files changed, 48 insertions(+), 0 deletions(-) diff --git a/gdb/main.c b/gdb/main.c index 1c240e4..8e00c27 100644 --- a/gdb/main.c +++ b/gdb/main.c @@ -375,8 +375,27 @@ captured_main (void *data) saved_command_line[0] = '\0'; instream = stdin; +#ifdef __MINGW32__ + /* Ensure stderr is unbuffered. */ + setvbuf (stderr, NULL, _IONBF, BUFSIZ); +#endif + gdb_stdout = stdio_fileopen (stdout); gdb_stderr = stdio_fileopen (stderr); +#ifdef __MINGW32__ + /* There is no real line-buffering on Windows, see + http://msdn.microsoft.com/en-us/library/86cebhfs%28v=vs.71%29.aspx + so the stdout is either fully-buffered or non-buffered. We can't + make stdout non-buffered, because of two concerns, + 1. non-buffering hurts performance, + 2. non-buffering may change GDB's behavior when it is interacting + with front-end, such as Emacs. + + We decide to leave stdout as fully buffered, but flush it first + when something is written to stderr. */ + update_stderr_ui_file (gdb_stderr); +#endif + gdb_stdlog = gdb_stderr; /* for moment */ gdb_stdtarg = gdb_stderr; /* for moment */ gdb_stdin = stdio_fileopen (stdin); diff --git a/gdb/ui-file.c b/gdb/ui-file.c index cf5a86d..649d892 100644 --- a/gdb/ui-file.c +++ b/gdb/ui-file.c @@ -654,6 +654,32 @@ stdio_file_fseek (struct ui_file *file, long offset, int whence) return fseek (stdio->file, offset, whence); } +static void +stderr_file_write (struct ui_file *file, const char *buf, long length_buf) +{ + gdb_flush (gdb_stdout); + stdio_file_write (file, buf, length_buf); +} + +static void +stderr_file_fputs (const char *linebuffer, struct ui_file *file) +{ + gdb_flush (gdb_stdout); + stdio_file_fputs (linebuffer, file); +} + +/* Overwrite UI_FILE's methods 'to_write' and 'to_fputs' by function + in which gdb_stdout is flushed. */ + +void +update_stderr_ui_file (struct ui_file *ui_file) +{ + /* Method 'to_write_async_safe' is not overwritten, because it is + only used on linux host. */ + set_ui_file_write (ui_file, stderr_file_write); + set_ui_file_fputs (ui_file, stderr_file_fputs); +} + /* Like fdopen(). Create a ui_file from a previously opened FILE. */ struct ui_file * diff --git a/gdb/ui-file.h b/gdb/ui-file.h index 9fef68c..6d96c0b 100644 --- a/gdb/ui-file.h +++ b/gdb/ui-file.h @@ -129,6 +129,9 @@ extern struct ui_file *mem_fileopen (void); /* Open/create a STDIO based UI_FILE using the already open FILE. */ extern struct ui_file *stdio_fileopen (FILE *file); +extern void update_stderr_ui_file (struct ui_file *ui_file); + + /* Open NAME returning an STDIO based UI_FILE. */ extern struct ui_file *gdb_fopen (const char *name, const char *mode); -- 1.7.7.6