public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Kevin Buettner <kevinb@redhat.com>
To: "Sargsyan, Eduard" <eduard.sargsyan@intel.com>
Cc: gdb-patches@sourceware.org, "Aktemur,
	Tankut Baris" <tankut.baris.aktemur@intel.com>,
	"Metzger, Markus T" <markus.t.metzger@intel.com>,
	"Schimpe, Christina" <christina.schimpe@intel.com>
Subject: Re: Filtering shadowed variables in info local
Date: Sat, 11 Nov 2023 15:53:04 -0700	[thread overview]
Message-ID: <20231111155304.1d4077bf@f38-zws-nv> (raw)
In-Reply-To: <SA2PR11MB47795DF0FA8F02C6B75036B5E0AEA@SA2PR11MB4779.namprd11.prod.outlook.com>

On Fri, 10 Nov 2023 17:54:53 +0000
"Sargsyan, Eduard" <eduard.sargsyan@intel.com> wrote:

> After https://sourceware.org/pipermail/gdb-patches/2021-June/180478.html
> and https://sourceware.org/pipermail/gdb-patches/2022-January/184819.html
> GDB prints "<shadowed: decl at line 5>" if variable has been marked as shadowed.
> But user might not be interested in seeing any shadowed variables.
> 
> For example, this code:
> 
>      1 int num = 1;
>      2 int
>      3 main ()
>      4  {
>      5    const char *str = "main";
>      6    int num = 3;
>      7    {
>      8       const char *str = "nested";
>      9       int num = 5;
>     10      num = 0; //  break here
>     11    }
>     12    return num;
>     13  }
> 
> Brings GDB to (when stopped at line 10):
> 
>   (gdb) info locals
>   str = 0x555555556009 "nested"
>   num = 5
>   str = 0x555555556004 "main"     <shadowed: decl at line 5>
>   num = 3 <shadowed: decl at line 6>
> 
> Prints both shadowed and local variables "str" and "num", but does not print
> global accessible variable "num".
> 
>   (gdb) print num
>   $1 = 5
>   (gdb) print ::num
>   $2 = 1
> 
> Setting value to local variable "num" works as expected:
>   (gdb) set num = 7
>   (gdb) info locals
>   str = 0x555555556009 "nested"
>   num = 7
>   str = 0x555555556004 "main"     <shadowed: decl at line 5>
>   num = 3 <shadowed: decl at line 6>
> 
> In C++ shadowed variable is not accessible, to there is no way to change value
> of variable "num" declared at line 5.  So only local and global variables can
> be modified:
> 
>   (gdb) set ::num = 9
>   (gdb) print num
>   $3 = 7
>   (gdb) print ::num
>   $4 = 9
> 
> I'm not sure if printing shadowed variables "num" and "str" are useful in the
> use case described above.  Maybe adding a setting to filter out shadowed
> variables makes sense (by default the setting will be disabled).
> 
> Example:
> 
>   (gdb) info locals
>   str = 0x555555556009 "nested"
>   num = 7
>   str = 0x555555556004 "main"     <shadowed: decl at line 5>
>   num = 3 <shadowed: decl at line 6>
>   (gdb) set print shadowed off
>   (gdb) info locals
>   str = 0x555555556009 "nested"
>   num = 7
>   (gdb) set print shadowed on
>   (gdb) info locals
>   str = 0x555555556009 "nested"
>   num = 7
>   str = 0x555555556004 "main"     <shadowed: decl at line 5>
>   num = 3 <shadowed: decl at line 6>
> 
> What are your thoughts?

Nice write-up!

I think it's useful to know the values of shadowed variables even if
they're not settable.  I think it might be useful to also know about the
shadowed global, which is currently not displayed by "info locals". 
OTOH, the command suggests that only locals should be displayed, but
the fact that a global is shadowed might be the source of a bug; IMO, it
wouldn't hurt to tell the user about it.

I have no objection to the addition of a "set print shadowed" command
to control output of shadowed variables, but I'd prefer that the
default be to preserve current behavior.

Kevin


      reply	other threads:[~2023-11-11 22:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-10 17:54 Sargsyan, Eduard
2023-11-11 22:53 ` Kevin Buettner [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231111155304.1d4077bf@f38-zws-nv \
    --to=kevinb@redhat.com \
    --cc=christina.schimpe@intel.com \
    --cc=eduard.sargsyan@intel.com \
    --cc=gdb-patches@sourceware.org \
    --cc=markus.t.metzger@intel.com \
    --cc=tankut.baris.aktemur@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).