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