Message ID | 1484560977-8693-2-git-send-email-yao.qi@linaro.org |
---|---|
State | New, archived |
Headers |
Received: (qmail 53248 invoked by alias); 16 Jan 2017 10:03:25 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: <gdb-patches.sourceware.org> List-Unsubscribe: <mailto:gdb-patches-unsubscribe-##L=##H@sourceware.org> List-Subscribe: <mailto:gdb-patches-subscribe@sourceware.org> List-Archive: <http://sourceware.org/ml/gdb-patches/> List-Post: <mailto:gdb-patches@sourceware.org> List-Help: <mailto:gdb-patches-help@sourceware.org>, <http://sourceware.org/ml/#faqs> Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 53075 invoked by uid 89); 16 Jan 2017 10:03:23 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.5 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=no version=3.3.2 spammy=Hx-spam-relays-external:74.125.83.68, H*RU:74.125.83.68, ui_file_delete, sk:f419501 X-HELO: mail-pg0-f68.google.com Received: from mail-pg0-f68.google.com (HELO mail-pg0-f68.google.com) (74.125.83.68) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 16 Jan 2017 10:03:13 +0000 Received: by mail-pg0-f68.google.com with SMTP id 75so5045380pgf.3 for <gdb-patches@sourceware.org>; Mon, 16 Jan 2017 02:03:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=g08eS8KDy3KA6Ba9ugWYswwedXVX9L04lNwsbasGWTs=; b=WQnwWYh17eMG49PuExYdUd476VgSdkoIIV+EU8rH8YQnX4zlSv7VpkatX8jZIPSfLO gREANPQZtq77Pvd5wn25sgdOLbDc3jtSLMsQR9C2ER4P+vIrvCmqLDbFxfNDcUJe+O3Y x8jzUQL6oX8yjZS1sh4npp87qsMBfX+c3OIzknv7LqQaPhdH4eVXoTKJiomTtbkSTaJa BmUqwInm03ZAiZpUvTDENHWCNo/3GuuXHK/g42fw2RKvekeZ5yPWW038JSBgz0IrVvLc /TXyKkLiZtiaE++SIsBruVh/zVVoOGe+YWkbJvJnujbGyop81xtkCfiwKyvdNHPSYNC+ oSHw== X-Gm-Message-State: AIkVDXKV2kpJBXnfqFKS6n6S0VctdJ4J3Ifu4yBXTQGt4wYHe+/fRsD5nc3mtSJdSMHiYw== X-Received: by 10.84.232.197 with SMTP id x5mr47599402plm.111.1484560991572; Mon, 16 Jan 2017 02:03:11 -0800 (PST) Received: from E107787-LIN.cambridge.arm.com (gcc1-power7.osuosl.org. [140.211.15.137]) by smtp.gmail.com with ESMTPSA id g28sm9695206pgn.3.2017.01.16.02.03.10 for <gdb-patches@sourceware.org> (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 16 Jan 2017 02:03:11 -0800 (PST) From: Yao Qi <qiyaoltc@gmail.com> X-Google-Original-From: Yao Qi <yao.qi@linaro.org> To: gdb-patches@sourceware.org Subject: [PATCH 1/6] New function null_stream Date: Mon, 16 Jan 2017 10:02:52 +0000 Message-Id: <1484560977-8693-2-git-send-email-yao.qi@linaro.org> In-Reply-To: <1484560977-8693-1-git-send-email-yao.qi@linaro.org> References: <1484051178-16013-1-git-send-email-yao.qi@linaro.org> <1484560977-8693-1-git-send-email-yao.qi@linaro.org> X-IsSubscribed: yes |
Commit Message
Yao Qi
Jan. 16, 2017, 10:02 a.m. UTC
This patch adds a new function null_stream, which returns a null stream. The null stream can be used in multiple places. It is used in gdb_insn_length, and the following patches will use it too. gdb: 2017-01-13 Yao Qi <yao.qi@linaro.org> * disasm.c (do_ui_file_delete): Delete. (gdb_insn_length): Move code creating stream to ... * utils.c (null_stream): ... here. New function. * utils.h (null_stream): Declare. --- gdb/disasm.c | 17 +---------------- gdb/utils.c | 13 +++++++++++++ gdb/utils.h | 4 ++++ 3 files changed, 18 insertions(+), 16 deletions(-)
Comments
On 01/16/2017 04:02 AM, Yao Qi wrote: > This patch adds a new function null_stream, which returns a null > stream. The null stream can be used in multiple places. It is > used in gdb_insn_length, and the following patches will use it too. > > gdb: > > 2017-01-13 Yao Qi <yao.qi@linaro.org> > > * disasm.c (do_ui_file_delete): Delete. > (gdb_insn_length): Move code creating stream to ... > * utils.c (null_stream): ... here. New function. > * utils.h (null_stream): Declare. > --- > gdb/disasm.c | 17 +---------------- > gdb/utils.c | 13 +++++++++++++ > gdb/utils.h | 4 ++++ > 3 files changed, 18 insertions(+), 16 deletions(-) > > diff --git a/gdb/disasm.c b/gdb/disasm.c > index f419501..ae3a2f1 100644 > --- a/gdb/disasm.c > +++ b/gdb/disasm.c > @@ -838,28 +838,13 @@ gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr, > return length; > } > > -static void > -do_ui_file_delete (void *arg) > -{ > - ui_file_delete ((struct ui_file *) arg); > -} > - > /* Return the length in bytes of the instruction at address MEMADDR in > debugged memory. */ > > int > gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR addr) > { > - static struct ui_file *null_stream = NULL; > - > - /* Dummy file descriptor for the disassembler. */ > - if (!null_stream) > - { > - null_stream = ui_file_new (); > - make_final_cleanup (do_ui_file_delete, null_stream); > - } > - > - return gdb_print_insn (gdbarch, addr, null_stream, NULL); > + return gdb_print_insn (gdbarch, addr, null_stream (), NULL); > } > > /* fprintf-function for gdb_buffered_insn_length. This function is a > diff --git a/gdb/utils.c b/gdb/utils.c > index f142ffe..7bad193 100644 > --- a/gdb/utils.c > +++ b/gdb/utils.c > @@ -199,6 +199,19 @@ make_cleanup_ui_file_delete (struct ui_file *arg) > return make_cleanup (do_ui_file_delete, arg); > } > > +struct ui_file * > +null_stream (void) > +{ > + static struct ui_file *stream = NULL; > + > + if (stream == NULL) > + { > + stream = ui_file_new (); > + make_final_cleanup (do_ui_file_delete, stream); > + } Since we're explicitly setting stream to NULL, we will always execute the conditional block, so it is not needed. Unless this is supposed to reuse a stream, but in that case the code would be incorrect. > + return stream; > +} > + > /* Helper function for make_cleanup_ui_out_redirect_pop. */ > > static void > diff --git a/gdb/utils.h b/gdb/utils.h > index c548a50..26e60af 100644 > --- a/gdb/utils.h > +++ b/gdb/utils.h > @@ -189,6 +189,10 @@ extern struct ui_file *gdb_stdtarg; > extern struct ui_file *gdb_stdtargerr; > extern struct ui_file *gdb_stdtargin; > > +/* Return a null stream. */ > + Spurious newline between comment and prototype. https://sourceware.org/gdb/wiki/Internals%20GDB-C-Coding-Standards#Empty_line_between_subprogram_description_and_the_subprogram_implementation > +extern struct ui_file *null_stream (void); > + > /* Set the screen dimensions to WIDTH and HEIGHT. */ > > extern void set_screen_width_and_height (int width, int height); > Otherwise looks fine.
On 17-01-17 07:49:07, Luis Machado wrote: > > > >+struct ui_file * > >+null_stream (void) > >+{ > >+ static struct ui_file *stream = NULL; > >+ > >+ if (stream == NULL) > >+ { > >+ stream = ui_file_new (); > >+ make_final_cleanup (do_ui_file_delete, stream); > >+ } > > Since we're explicitly setting stream to NULL, we will always > execute the conditional block, so it is not needed. Unless this is > supposed to reuse a stream, but in that case the code would be > incorrect. > It is a reused null stream. Different parts of gdb can use it. Why is it incorrect?
On 01/18/2017 08:45 AM, Yao Qi wrote: > On 17-01-17 07:49:07, Luis Machado wrote: >>> >>> +struct ui_file * >>> +null_stream (void) >>> +{ >>> + static struct ui_file *stream = NULL; >>> + >>> + if (stream == NULL) >>> + { >>> + stream = ui_file_new (); >>> + make_final_cleanup (do_ui_file_delete, stream); >>> + } >> >> Since we're explicitly setting stream to NULL, we will always >> execute the conditional block, so it is not needed. Unless this is >> supposed to reuse a stream, but in that case the code would be >> incorrect. >> > > It is a reused null stream. Different parts of gdb can use it. Why > is it incorrect? > We're setting stream to NULL at the top of the function and then checking if it is NULL (it obviously is) right after that? Am i missing something?
On 2017-01-18 09:53, Luis Machado wrote: > We're setting stream to NULL at the top of the function and then > checking if it is NULL (it obviously is) right after that? Am i > missing something? The "static" :).
On 01/18/2017 08:57 AM, Simon Marchi wrote: > On 2017-01-18 09:53, Luis Machado wrote: >> We're setting stream to NULL at the top of the function and then >> checking if it is NULL (it obviously is) right after that? Am i >> missing something? > > The "static" :). Oh, that's what i was missing! Can't we have a clearer way of reusing this stream? It looks cryptic this way (at least i think it looks cryptic).
On 2017-01-18 10:01, Luis Machado wrote: > On 01/18/2017 08:57 AM, Simon Marchi wrote: >> On 2017-01-18 09:53, Luis Machado wrote: >>> We're setting stream to NULL at the top of the function and then >>> checking if it is NULL (it obviously is) right after that? Am i >>> missing something? >> >> The "static" :). > > Oh, that's what i was missing! > > Can't we have a clearer way of reusing this stream? It looks cryptic > this way (at least i think it looks cryptic). With Pedro's ui_file c++ification series, there is a null_file specialization of ui_file, and it's just a global object that you can reference. Search for "null_file null_stream" in https://sourceware.org/ml/gdb-patches/2017-01/msg00312.html
On 01/18/2017 09:18 AM, Simon Marchi wrote: > On 2017-01-18 10:01, Luis Machado wrote: >> On 01/18/2017 08:57 AM, Simon Marchi wrote: >>> On 2017-01-18 09:53, Luis Machado wrote: >>>> We're setting stream to NULL at the top of the function and then >>>> checking if it is NULL (it obviously is) right after that? Am i >>>> missing something? >>> >>> The "static" :). >> >> Oh, that's what i was missing! >> >> Can't we have a clearer way of reusing this stream? It looks cryptic >> this way (at least i think it looks cryptic). > > With Pedro's ui_file c++ification series, there is a null_file > specialization of ui_file, and it's just a global object that you can > reference. > > Search for "null_file null_stream" in > > https://sourceware.org/ml/gdb-patches/2017-01/msg00312.html That is perfectly fine. The cryptic bit i was referring to was declaring/initializing a static variable inside this particular function. It ought to be possible to initialize the static variable somewhere else and only do the null check/allocation in the function? Compilers will often zero these out too, so initializing to NULL may not even be needed? The fact that the initialization only happens once but the source line is there forever can cause some confusion.
On 2017-01-18 10:28, Luis Machado wrote: > That is perfectly fine. The cryptic bit i was referring to was > declaring/initializing a static variable inside this particular > function. Indeed, and I pointed to Pedro's patch because it removes that. The global object is statically allocated and is constructed at startup, so there's no more checking if it's initialized nor static variable inside a function. > It ought to be possible to initialize the static variable somewhere > else and only do the null check/allocation in the function? Compilers > will often zero these out too, so initializing to NULL may not even be > needed? I think this is a common pattern when you want to have a singleton with lazy initialization, but I agree that it can be confusing. The variables with static storage duration are put in .bss (since they are not in .data), so yes they'd be initialized to 0 automatically, I believe. I prefer to see the = NULL though. Of course we could move the variable outside the scope of the function, but then it would be possible to reference it from other functions. The goal of declaring it in the function is that the only way to reach it is by calling the function it's in. > The fact that the initialization only happens once but the source line > is there forever can cause some confusion. Well, you can thank Mr Ritchie & friends I suppose :).
On 01/18/2017 09:54 AM, Simon Marchi wrote: > On 2017-01-18 10:28, Luis Machado wrote: >> That is perfectly fine. The cryptic bit i was referring to was >> declaring/initializing a static variable inside this particular >> function. > > Indeed, and I pointed to Pedro's patch because it removes that. The > global object is statically allocated and is constructed at startup, so > there's no more checking if it's initialized nor static variable inside > a function. > >> It ought to be possible to initialize the static variable somewhere >> else and only do the null check/allocation in the function? Compilers >> will often zero these out too, so initializing to NULL may not even be >> needed? > > I think this is a common pattern when you want to have a singleton with > lazy initialization, but I agree that it can be confusing. The > variables with static storage duration are put in .bss (since they are > not in .data), so yes they'd be initialized to 0 automatically, I > believe. I prefer to see the = NULL though. > > Of course we could move the variable outside the scope of the function, > but then it would be possible to reference it from other functions. The > goal of declaring it in the function is that the only way to reach it is > by calling the function it's in. > >> The fact that the initialization only happens once but the source line >> is there forever can cause some confusion. > > Well, you can thank Mr Ritchie & friends I suppose :). > That's fine. If we're sticking with it, i'd at least add a comment for the sake of making it more obvious what the intentions for such a variable are and the fact it will outlive the function itself.
diff --git a/gdb/disasm.c b/gdb/disasm.c index f419501..ae3a2f1 100644 --- a/gdb/disasm.c +++ b/gdb/disasm.c @@ -838,28 +838,13 @@ gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr, return length; } -static void -do_ui_file_delete (void *arg) -{ - ui_file_delete ((struct ui_file *) arg); -} - /* Return the length in bytes of the instruction at address MEMADDR in debugged memory. */ int gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR addr) { - static struct ui_file *null_stream = NULL; - - /* Dummy file descriptor for the disassembler. */ - if (!null_stream) - { - null_stream = ui_file_new (); - make_final_cleanup (do_ui_file_delete, null_stream); - } - - return gdb_print_insn (gdbarch, addr, null_stream, NULL); + return gdb_print_insn (gdbarch, addr, null_stream (), NULL); } /* fprintf-function for gdb_buffered_insn_length. This function is a diff --git a/gdb/utils.c b/gdb/utils.c index f142ffe..7bad193 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -199,6 +199,19 @@ make_cleanup_ui_file_delete (struct ui_file *arg) return make_cleanup (do_ui_file_delete, arg); } +struct ui_file * +null_stream (void) +{ + static struct ui_file *stream = NULL; + + if (stream == NULL) + { + stream = ui_file_new (); + make_final_cleanup (do_ui_file_delete, stream); + } + return stream; +} + /* Helper function for make_cleanup_ui_out_redirect_pop. */ static void diff --git a/gdb/utils.h b/gdb/utils.h index c548a50..26e60af 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -189,6 +189,10 @@ extern struct ui_file *gdb_stdtarg; extern struct ui_file *gdb_stdtargerr; extern struct ui_file *gdb_stdtargin; +/* Return a null stream. */ + +extern struct ui_file *null_stream (void); + /* Set the screen dimensions to WIDTH and HEIGHT. */ extern void set_screen_width_and_height (int width, int height);