public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* Filtering shadowed variables in info local
@ 2023-11-10 17:54 Sargsyan, Eduard
  2023-11-11 22:53 ` Kevin Buettner
  0 siblings, 1 reply; 2+ messages in thread
From: Sargsyan, Eduard @ 2023-11-10 17:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aktemur, Tankut Baris, Metzger, Markus T, Schimpe, Christina

[-- Attachment #1: Type: text/plain, Size: 2602 bytes --]

Hi,

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?

- Eduard

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Filtering shadowed variables in info local
  2023-11-10 17:54 Filtering shadowed variables in info local Sargsyan, Eduard
@ 2023-11-11 22:53 ` Kevin Buettner
  0 siblings, 0 replies; 2+ messages in thread
From: Kevin Buettner @ 2023-11-11 22:53 UTC (permalink / raw)
  To: Sargsyan, Eduard
  Cc: gdb-patches, Aktemur, Tankut Baris, Metzger, Markus T, Schimpe,
	Christina

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-11-11 22:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-10 17:54 Filtering shadowed variables in info local Sargsyan, Eduard
2023-11-11 22:53 ` Kevin Buettner

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).