public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* Shadowed local variables in "info locals"
@ 2021-06-29 14:21 Aktemur, Tankut Baris
  2021-06-29 14:49 ` Simon Marchi
  2021-06-30 16:54 ` Tom Tromey
  0 siblings, 2 replies; 4+ messages in thread
From: Aktemur, Tankut Baris @ 2021-06-29 14:21 UTC (permalink / raw)
  To: gdb-patches

Hi,

Suppose we have the following program:

     1  int
     2  main ()
     3  {
     4    int x = 42;
     5    {
     6      int x = 99;
     7      x = 99;
     8    }
     9    return 0;
    10  }

The "info locals" command, when stopped at line 7, gives

  (gdb) info locals
  x = 99
  x = 42

Is this a bug or an (un)intentional feature?

From one perspective, only one "x" is available for use in our context, and
thus only one "x" should be printed by "info locals".  This is the "bug" view.

From another perspective, the outer "x" and inner "x" are two different
variables that are located in separate locations but they just happen to have
the same name.  Therefore, displaying both is giving valuable information.
This is the "feature" view.

Displaying both names could be confusing for the user in particular if the 
values of the variables come from complex execution flows and if the user cannot
tell which "x" is which.  A potential improvement to the current state is
annotating the outer scope variable for clarity.  E.g.

  (gdb) info locals
  x = 99
  x = 42 <shadowed>

What are your thoughts?

-Baris


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] 4+ messages in thread

* Re: Shadowed local variables in "info locals"
  2021-06-29 14:21 Shadowed local variables in "info locals" Aktemur, Tankut Baris
@ 2021-06-29 14:49 ` Simon Marchi
  2021-06-30 16:54 ` Tom Tromey
  1 sibling, 0 replies; 4+ messages in thread
From: Simon Marchi @ 2021-06-29 14:49 UTC (permalink / raw)
  To: Aktemur, Tankut Baris, gdb-patches

On 2021-06-29 10:21 a.m., Aktemur, Tankut Baris via Gdb-patches wrote:
> Hi,
> 
> Suppose we have the following program:
> 
>      1  int
>      2  main ()
>      3  {
>      4    int x = 42;
>      5    {
>      6      int x = 99;
>      7      x = 99;
>      8    }
>      9    return 0;
>     10  }
> 
> The "info locals" command, when stopped at line 7, gives
> 
>   (gdb) info locals
>   x = 99
>   x = 42
> 
> Is this a bug or an (un)intentional feature?
> 
> From one perspective, only one "x" is available for use in our context, and
> thus only one "x" should be printed by "info locals".  This is the "bug" view.
> 
> From another perspective, the outer "x" and inner "x" are two different
> variables that are located in separate locations but they just happen to have
> the same name.  Therefore, displaying both is giving valuable information.
> This is the "feature" view.
> 
> Displaying both names could be confusing for the user in particular if the 
> values of the variables come from complex execution flows and if the user cannot
> tell which "x" is which.  A potential improvement to the current state is
> annotating the outer scope variable for clarity.  E.g.
> 
>   (gdb) info locals
>   x = 99
>   x = 42 <shadowed>
> 
> What are your thoughts?

I don't have a strong opinion.  I maybe lean more towards the side of
showing all the information, but making it clearer which variable is
which (i.e. the example above).

But I just wanted to note that a change for MI's -stack-list-locals
command may also be desirable.  It could be a new attribute in the
shadowed variable's record that says it is shadowed, that would not
break backwards compatibility.

Simon

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

* Re: Shadowed local variables in "info locals"
  2021-06-29 14:21 Shadowed local variables in "info locals" Aktemur, Tankut Baris
  2021-06-29 14:49 ` Simon Marchi
@ 2021-06-30 16:54 ` Tom Tromey
  2021-07-01  8:12   ` Aktemur, Tankut Baris
  1 sibling, 1 reply; 4+ messages in thread
From: Tom Tromey @ 2021-06-30 16:54 UTC (permalink / raw)
  To: Aktemur, Tankut Baris via Gdb-patches

>>>>> ">" == Aktemur, Tankut Baris via Gdb-patches <gdb-patches@sourceware.org> writes:

>>   (gdb) info locals
>>   x = 99
>>   x = 42

>> Is this a bug or an (un)intentional feature?

I think it's an intentional feature.

>> Displaying both names could be confusing for the user in particular if the 
>> values of the variables come from complex execution flows and if the user cannot
>> tell which "x" is which.  A potential improvement to the current state is
>> annotating the outer scope variable for clarity.  E.g.

>>   (gdb) info locals
>>   x = 99
>>   x = 42 <shadowed>

>> What are your thoughts?

I wonder if it could show the line number of the declaration as well.


One other oddity here is that, in Rust, it's pretty normal to redeclare
variables -- but then the previous one isn't available.

Like:

pub fn main() {
    let x = 5;
    let x = x + 7;
    let y = 8;
}

Then in gdb:

(gdb) info local
x = 12
x = 5

... but the 'x = 5' one is actually inaccessible.

I'm not sure how to handle this.  It may require some special debuginfo.

Tom

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

* RE: Shadowed local variables in "info locals"
  2021-06-30 16:54 ` Tom Tromey
@ 2021-07-01  8:12   ` Aktemur, Tankut Baris
  0 siblings, 0 replies; 4+ messages in thread
From: Aktemur, Tankut Baris @ 2021-07-01  8:12 UTC (permalink / raw)
  To: Tom Tromey, Simon Marchi; +Cc: gdb-patches

On Wednesday, June 30, 2021 6:55 PM, Tom Tromey wrote:
> >>>>> ">" == Aktemur, Tankut Baris via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> >>   (gdb) info locals
> >>   x = 99
> >>   x = 42
> 
> >> Is this a bug or an (un)intentional feature?
> 
> I think it's an intentional feature.
> 
> >> Displaying both names could be confusing for the user in particular if the
> >> values of the variables come from complex execution flows and if the user cannot
> >> tell which "x" is which.  A potential improvement to the current state is
> >> annotating the outer scope variable for clarity.  E.g.
> 
> >>   (gdb) info locals
> >>   x = 99
> >>   x = 42 <shadowed>
> 
> >> What are your thoughts?
> 
> I wonder if it could show the line number of the declaration as well.
> 
> 
> One other oddity here is that, in Rust, it's pretty normal to redeclare
> variables -- but then the previous one isn't available.
> 
> Like:
> 
> pub fn main() {
>     let x = 5;
>     let x = x + 7;
>     let y = 8;
> }
> 
> Then in gdb:
> 
> (gdb) info local
> x = 12
> x = 5
> 
> ... but the 'x = 5' one is actually inaccessible.
> 
> I'm not sure how to handle this.  It may require some special debuginfo.
> 
> Tom

Hi Tom, Simon,

Thank you for your comments.  We shall investigate what can be done to improve the
output of "info locals".

-Baris


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] 4+ messages in thread

end of thread, other threads:[~2021-07-01  8:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-29 14:21 Shadowed local variables in "info locals" Aktemur, Tankut Baris
2021-06-29 14:49 ` Simon Marchi
2021-06-30 16:54 ` Tom Tromey
2021-07-01  8:12   ` Aktemur, Tankut Baris

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