public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: "Sargsyan, Eduard" <eduard.sargsyan@intel.com>
To: "gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Cc: "Aktemur, Tankut Baris" <tankut.baris.aktemur@intel.com>,
	"Metzger, Markus T" <markus.t.metzger@intel.com>,
	"Schimpe, Christina" <christina.schimpe@intel.com>
Subject: Filtering shadowed variables in info local
Date: Fri, 10 Nov 2023 17:54:53 +0000	[thread overview]
Message-ID: <SA2PR11MB47795DF0FA8F02C6B75036B5E0AEA@SA2PR11MB4779.namprd11.prod.outlook.com> (raw)

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

             reply	other threads:[~2023-11-10 17:54 UTC|newest]

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

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=SA2PR11MB47795DF0FA8F02C6B75036B5E0AEA@SA2PR11MB4779.namprd11.prod.outlook.com \
    --to=eduard.sargsyan@intel.com \
    --cc=christina.schimpe@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).