From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26727 invoked by alias); 28 Aug 2013 07:23:39 -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 26708 invoked by uid 89); 28 Aug 2013 07:23:38 -0000 Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 28 Aug 2013 07:23:38 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00,KHOP_THREADED,RDNS_NONE,SPF_HELO_FAIL autolearn=no version=3.3.2 X-HELO: relay1.mentorg.com Received: from svr-orw-fem-01.mgc.mentorg.com ([147.34.98.93]) by relay1.mentorg.com with esmtp id 1VEa6G-00040Y-D8 from Yao_Qi@mentor.com ; Wed, 28 Aug 2013 00:23:32 -0700 Received: from SVR-ORW-FEM-03.mgc.mentorg.com ([147.34.97.39]) by svr-orw-fem-01.mgc.mentorg.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.4675); Wed, 28 Aug 2013 00:23:31 -0700 Received: from qiyao.dyndns.org (147.34.91.1) by svr-orw-fem-03.mgc.mentorg.com (147.34.97.39) with Microsoft SMTP Server id 14.2.247.3; Wed, 28 Aug 2013 00:23:31 -0700 Message-ID: <521DA52C.9020003@codesourcery.com> Date: Wed, 28 Aug 2013 07:23: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: Joel Brobecker , Eli Zaretskii , Subject: Re: [PATCH] Unbuffer stdout and stderr on windows References: <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> <5215AC28.3030506@codesourcery.com> <20130822141756.GB5221@adacore.com> <5216C6B0.90906@codesourcery.com> <521D0E8A.80303@redhat.com> In-Reply-To: <521D0E8A.80303@redhat.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-SW-Source: 2013-08/txt/msg00810.txt.bz2 On 08/28/2013 04:39 AM, Pedro Alves wrote: > Better say that "it is not used on Windows hosts". But I think > it'd be better even to say: > > /* Method 'to_write_async_safe' is not overwritten, because > there's no way to flushing a stream in an async-safe manner. > Fortunately, it doesn't really matter, because: > - that method is only used for printing internal debug output > from signal handlers. > - Windows hosts don't have a concept of async-safeness. Signal > handlers run in a separate thread, so they can call > the regular non-async-safe output routines freely. */ OK. [...] > hide that in ui-file.c itself. So e.g., we'd have a new stderr_fileopen > method, and then both places that create gdb_stderr would use it, like: > > -gdb_stderr = stdio_fileopen (stderr); > +gdb_stderr = stderr_fileopen (); > > We could also that whole set of Windows-specific comments there > too. Right. Function stderr_fileopen is added in the updated patch. What do you think? -- Yao (齐尧) gdb: 2013-08-28 Yao Qi Pedro Alves * event-top.c (gdb_setup_readline): Call stderr_fileopen instead of stdio_fileopen. * main.c (captured_main) [__MINGW32__]: Set stderr unbuffered. Call stderr_fileopen instead of stdio_fileopen. * ui-file.c (stderr_file_write): New function. (stderr_file_fputs): New function. (stderr_fileopen): New function. * ui-file.h (stderr_fileopen): Declare. --- gdb/event-top.c | 2 +- gdb/main.c | 9 ++++++++- gdb/ui-file.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ gdb/ui-file.h | 3 +++ 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/gdb/event-top.c b/gdb/event-top.c index f00ab7d..f1d55b3 100644 --- a/gdb/event-top.c +++ b/gdb/event-top.c @@ -955,7 +955,7 @@ gdb_setup_readline (void) time. */ if (!batch_silent) gdb_stdout = stdio_fileopen (stdout); - gdb_stderr = stdio_fileopen (stderr); + gdb_stderr = stderr_fileopen (); gdb_stdlog = gdb_stderr; /* for moment */ gdb_stdtarg = gdb_stderr; /* for moment */ gdb_stdtargerr = gdb_stderr; /* for moment */ diff --git a/gdb/main.c b/gdb/main.c index 1c240e4..11f4b03 100644 --- a/gdb/main.c +++ b/gdb/main.c @@ -375,8 +375,15 @@ captured_main (void *data) saved_command_line[0] = '\0'; instream = stdin; +#ifdef __MINGW32__ + /* Ensure stderr is unbuffered. A Cygwin pty or pipe is implemented + as a Windows pipe, and Windows buffers on pipes. */ + setvbuf (stderr, NULL, _IONBF, BUFSIZ); +#endif + gdb_stdout = stdio_fileopen (stdout); - gdb_stderr = stdio_fileopen (stderr); + gdb_stderr = stderr_fileopen (); + 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..d29b8da 100644 --- a/gdb/ui-file.c +++ b/gdb/ui-file.c @@ -654,6 +654,60 @@ stdio_file_fseek (struct ui_file *file, long offset, int whence) return fseek (stdio->file, offset, whence); } +/* This is the implementation of ui_file method to_write for stderr. + gdb_stdout is flushed before writing to gdb_stderr. */ + +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); +} + +/* This is the implementation of ui_file method to_fputs for stderr. + gdb_stdout is flushed before writing to gdb_stderr. */ + +static void +stderr_file_fputs (const char *linebuffer, struct ui_file *file) +{ + gdb_flush (gdb_stdout); + stdio_file_fputs (linebuffer, file); +} + +/* Create a ui_file from stderr. */ + +struct ui_file * +stderr_fileopen (void) +{ + struct ui_file *ui_file = 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 decided to leave stdout as fully buffered, but flush it first + when something is written to stderr. */ + + /* Method 'to_write_async_safe' is not overwritten, because there's + no way to flushing a stream in an async-safe manner. + Fortunately, it doesn't really matter, because: + - that method is only used for printing internal debug output + from signal handlers. + - Windows hosts don't have a concept of async-safeness. Signal + handlers run in a separate thread, so they can call + the regular non-async-safe output routines freely. */ + set_ui_file_write (ui_file, stderr_file_write); + set_ui_file_fputs (ui_file, stderr_file_fputs); +#endif + + return ui_file; +} + /* 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..dca800f 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 struct ui_file *stderr_fileopen (void); + + /* Open NAME returning an STDIO based UI_FILE. */ extern struct ui_file *gdb_fopen (const char *name, const char *mode); -- 1.7.7.6