* [PATCH 0/1] gdb: add 'set print shadowed' to hide shadowed variables @ 2026-08-11 20:27 Stephan Rohr 2026-08-11 20:27 ` [PATCH 1/1] gdb: Introduce new setting to filter out " Stephan Rohr 0 siblings, 1 reply; 3+ messages in thread From: Stephan Rohr @ 2026-08-11 20:27 UTC (permalink / raw) To: gdb-patches From: "Rohr, Stephan" <stephan.rohr@intel.com> Hello all, this patch adds a new setting 'set print shadowed on|off' to control whether shadowed variables are printed by 'info locals'. The default is 'on', matching current behaviour. The patch was originally written by Eduard Sargsyan; I rebased to the current master. It is a follow-up to the original RFC: https://sourceware.org/pipermail/gdb-patches/2023-November/203961.html and Kevin's response: https://sourceware.org/pipermail/gdb-patches/2023-November/203973.html It depends on Abdul's preliminary series, which was merged recently: https://sourceware.org/pipermail/gdb-patches/2026-July/228668.html Regression tested on x86_64 Ubuntu 22 with the native, native gdbserver and extended gdbserver targets; no regressions. I appreciate your feedback. Thanks Stephan Sargsyan, Eduard (1): gdb: Introduce new setting to filter out shadowed variables. gdb/NEWS | 8 ++++++++ gdb/doc/gdb.texinfo | 15 +++++++++++++- gdb/printcmd.c | 8 +++++++- gdb/stack.c | 25 +++++++++++++++++++++++- gdb/testsuite/gdb.ada/var_shadowing.exp | 1 + gdb/testsuite/gdb.base/options.exp | 1 + gdb/testsuite/gdb.base/var-shadowing.exp | 15 ++++++++++++++ gdb/valprint.c | 20 +++++++++++++++++++ gdb/valprint.h | 3 +++ 9 files changed, 93 insertions(+), 3 deletions(-) -- 2.43.0 ________________________________________ Intel Deutschland GmbH Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany Tel: +49 (89) 99143-0 www.intel.de Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman Chairperson of the Supervisory Board: Sonja Pierer Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928 This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies. ^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 1/1] gdb: Introduce new setting to filter out shadowed variables. 2026-08-11 20:27 [PATCH 0/1] gdb: add 'set print shadowed' to hide shadowed variables Stephan Rohr @ 2026-08-11 20:27 ` Stephan Rohr 2026-08-13 4:24 ` Simon Marchi 0 siblings, 1 reply; 3+ messages in thread From: Stephan Rohr @ 2026-08-11 20:27 UTC (permalink / raw) To: gdb-patches From: "Sargsyan, Eduard" <eduard.sargsyan@intel.com> Add a new setting 'print shadowed on|off' to control the printing of shadowed variables in 'info locals'. When shadowed variables are present, each variable involved in a shadowing relationship is annotated with its declaration location '<file:line>'; the ones hidden by an inner declaration are additionally marked ', shadowed'. Given this code, stopped at line 10: 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 } By default, 'info locals' prints shadowed variables: (gdb) info locals str = 0x555555556009 "nested" <main.c:8> num = 5 <main.c:9> str = 0x555555556004 "main" <main.c:5, shadowed> num = 3 <main.c:6, shadowed> Use 'set print shadowed off' to hide shadowed variables. The user may not want to print shadowed variables: (gdb) set print shadowed off (gdb) info locals str = 0x555555556009 "nested" num = 5 Use 'set print shadowed on' to include shadowed variables. --- gdb/NEWS | 8 ++++++++ gdb/doc/gdb.texinfo | 15 +++++++++++++- gdb/printcmd.c | 8 +++++++- gdb/stack.c | 25 +++++++++++++++++++++++- gdb/testsuite/gdb.ada/var_shadowing.exp | 1 + gdb/testsuite/gdb.base/options.exp | 1 + gdb/testsuite/gdb.base/var-shadowing.exp | 15 ++++++++++++++ gdb/valprint.c | 20 +++++++++++++++++++ gdb/valprint.h | 3 +++ 9 files changed, 93 insertions(+), 3 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 10c182067f9..a3348436984 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -3,6 +3,14 @@ *** Changes since GDB 18 +* New commands + +set print shadowed on|off +show print shadowed + This controls the output of the "info locals" command for C/C++/Fortran. If + the option is 'off' shadowed variables will be omitted in output. The + default is to print shadowed variables. + *** Changes in GDB 18 * Support for the Common Trace Format (CTF) has been removed. GDB now diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 0030698dcee..232a9901a2c 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -9212,7 +9212,8 @@ same name which is declared within an inner scope (decision block, method, or inner class). When shadowing is detected, location information is added to all instances of the shadowed variable name. The outermost instances are additionally followed by @samp{shadowed} -to indicate that they are not the active variable. +to indicate that they are not the active variable. Printing shadowed +variables can be controlled by @ref{set print shadowed}. @item info locals [-q] [-t @var{type_regexp}] [@var{regexp}] Like @kbd{info locals}, but only print the local variables selected @@ -12829,6 +12830,18 @@ Do not pretty print C@t{++} virtual function tables. @item show print vtbl Show whether C@t{++} virtual function tables are pretty printed, or not. + +@anchor{set print shadowed} +@item set print shadowed +@itemx set print shadowed on +@cindex shadowed variables in C/C@t{++}/Fortran +Print shadowed variables for C/C@t{++}/Fortran. The default is on. + +@item set print shadowed off +Do not print shadowed variables for C/C@t{++}/Fortran. + +@item show print shadowed +Show whether C/C@t{++}/Fortran shadowed variables are printed or not. @end table @node Pretty Printing diff --git a/gdb/printcmd.c b/gdb/printcmd.c index c9e6e4886e3..303066065f8 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -2402,7 +2402,7 @@ print_variable_value (symbol *var, const frame_info_ptr &frame, /* Print <%line, shadowed> after the variable value only when it is variable shadowing case. */ - if (shadow_status != var_shadowing::NONE) + if (opts.print_shadowed && shadow_status != var_shadowing::NONE) { /* Use lbasename instead of symtab_to_filename_for_display as the latter defaults to relative path while basename is preferred @@ -2454,6 +2454,12 @@ print_variable_and_value (const char *name, symbol *var, if (name == nullptr) name = var->print_name (); + struct value_print_options opts; + get_user_print_options (&opts); + + if (!opts.print_shadowed && shadow_status == var_shadowing::SHADOWED) + return; + gdb_printf (stream, "%*s%ps = ", 2 * indent, "", styled_string (variable_name_style.style (), name)); diff --git a/gdb/stack.c b/gdb/stack.c index 954ef6b11a4..cd21aa3a40c 100644 --- a/gdb/stack.c +++ b/gdb/stack.c @@ -2258,6 +2258,7 @@ struct print_variable_and_value_data int num_tabs; struct ui_file *stream; int values_printed; + bool has_shadowed_variables; void operator() (const char *print_name, struct symbol *sym, var_shadowing shadow_status); @@ -2292,6 +2293,8 @@ print_variable_and_value_data::operator() (const char *print_name, shadow_status); values_printed = 1; + + has_shadowed_variables |= (shadow_status == var_shadowing::SHADOWED); } /* Prepares the regular expression REG from REGEXP. @@ -2351,6 +2354,7 @@ print_frame_local_vars (const frame_info_ptr &frame, cb_data.num_tabs = 4 * num_tabs; cb_data.stream = stream; cb_data.values_printed = 0; + cb_data.has_shadowed_variables = false; /* Temporarily change the selected frame to the given FRAME. This allows routines that rely on the selected frame instead @@ -2360,7 +2364,26 @@ print_frame_local_vars (const frame_info_ptr &frame, iterate_over_block_local_vars_printing (block, cb_data); - if (!cb_data.values_printed && !quiet) + if (quiet) + return; + + if (cb_data.values_printed) + { + if (!cb_data.has_shadowed_variables) + return; + + value_print_options opts; + get_user_print_options (&opts); + if (opts.print_shadowed) + gdb_printf (stream, + _("Use 'set print shadowed off' " + "to hide shadowed variables.\n")); + else + gdb_printf (stream, + _("Use 'set print shadowed on' " + "to include shadowed variables.\n")); + } + else { if (regexp == NULL && t_regexp == NULL) gdb_printf (stream, _("No locals.\n")); diff --git a/gdb/testsuite/gdb.ada/var_shadowing.exp b/gdb/testsuite/gdb.ada/var_shadowing.exp index ffa96b049cf..d927b0d9888 100644 --- a/gdb/testsuite/gdb.ada/var_shadowing.exp +++ b/gdb/testsuite/gdb.ada/var_shadowing.exp @@ -36,4 +36,5 @@ gdb_test "info locals" [multi_line \ "i = 111\t<$testfile.adb:$i_level3>" \ "i = 11\t<$testfile.adb:$i_level2, shadowed>" \ "i = 1\t<$testfile.adb:$i_level1, shadowed>" \ + "Use 'set print shadowed off' to hide shadowed variables." \ ] "info locals at innermost level" diff --git a/gdb/testsuite/gdb.base/options.exp b/gdb/testsuite/gdb.base/options.exp index 35487ead6a7..34e4b642181 100644 --- a/gdb/testsuite/gdb.base/options.exp +++ b/gdb/testsuite/gdb.base/options.exp @@ -198,6 +198,7 @@ proc_with_prefix test-print {{prefix ""}} { "-pretty" "-raw-values" "-repeats" + "-shadowed" "-static-members" "-symbol" "-union" diff --git a/gdb/testsuite/gdb.base/var-shadowing.exp b/gdb/testsuite/gdb.base/var-shadowing.exp index 502cbad11f6..c3b9d77eb77 100644 --- a/gdb/testsuite/gdb.base/var-shadowing.exp +++ b/gdb/testsuite/gdb.base/var-shadowing.exp @@ -54,6 +54,7 @@ gdb_test "info locals" [multi_line \ "a = 101" \ "val1 = 1" \ "val2 = 2\t<$srcfile:$val2_d1, shadowed>" \ + "Use 'set print shadowed off' to hide shadowed variables." \ ] "info locals first level" gdb_breakpoint $srcfile:$bp_line3 @@ -65,6 +66,7 @@ gdb_test "info locals" [multi_line \ "a = 102" \ "val1 = 1\t<$srcfile:$val1_d1, shadowed>" \ "val2 = 2\t<$srcfile:$val2_d1, shadowed>" \ + "Use 'set print shadowed off' to hide shadowed variables." \ ] "info locals second level" gdb_breakpoint $srcfile:$bp_line4 @@ -80,8 +82,21 @@ gdb_test "info locals" [multi_line \ "a = 103\t<$srcfile:$a_line, shadowed>" \ "val1 = 1\t<$srcfile:$val1_d1, shadowed>" \ "val2 = 2\t<$srcfile:$val2_d1, shadowed>" \ + "Use 'set print shadowed off' to hide shadowed variables." \ ] "info locals at innermost level" +gdb_test_no_output "set print shadowed off" + +gdb_test "info locals" [multi_line \ + "a = 999" \ + "val1 = 6" \ + "val2 = 7" \ + "val3 = 8" \ + "Use 'set print shadowed on' to include shadowed variables." \ + ] "info locals at innermost level with filtered out shadowed" + +gdb_test_no_output "set print shadowed on" + gdb_breakpoint $srcfile:$bp_line5 gdb_continue_to_breakpoint "continue to outermost level last" \ ".*$srcfile:$bp_line5.*" diff --git a/gdb/valprint.c b/gdb/valprint.c index 3e7a37338b3..4b65e438c33 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -90,6 +90,7 @@ struct value_print_options user_print_options = false, /* print_array_indexes */ false, /* deref_ref */ true, /* static_field_print */ + true, /* print_shadowed */ true, /* pascal_static_field_print */ false, /* raw */ false, /* summary */ @@ -2395,6 +2396,16 @@ show_static_field_print (struct ui_file *file, int from_tty, value); } +static void +show_shadowed_print (struct ui_file *file, int from_tty, + struct cmd_list_element *c, + const char *value) +{ + gdb_printf (file, + _("Printing of shadowed variables is %s.\n"), + value); +} + \f /* A couple typedefs to make writing the options a bit more @@ -2557,6 +2568,15 @@ pretty-printers for that value.") NULL, /* help_doc */ }, + boolean_option_def { + "shadowed", + [] (value_print_options *opt) { return &opt->print_shadowed; }, + show_shadowed_print, /* show_cmd_cb */ + N_("Set printing of shadowed variables."), + N_("Show printing of shadowed variables."), + NULL, /* help_doc */ + }, + boolean_option_def { "symbol", [] (value_print_options *opt) { return &opt->symbol_print; }, diff --git a/gdb/valprint.h b/gdb/valprint.h index 15d0be2ac7a..e63c3b9fb03 100644 --- a/gdb/valprint.h +++ b/gdb/valprint.h @@ -99,6 +99,9 @@ struct value_print_options /* If true, print static fields. */ bool static_field_print; + /* If true, print shadowed variables. */ + bool print_shadowed; + /* If true, print static fields for Pascal. FIXME: C++ has a flag, why not share with Pascal too? */ bool pascal_static_field_print; -- 2.43.0 ________________________________________ Intel Deutschland GmbH Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany Tel: +49 (89) 99143-0 www.intel.de Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman Chairperson of the Supervisory Board: Sonja Pierer Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928 This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies. ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH 1/1] gdb: Introduce new setting to filter out shadowed variables. 2026-08-11 20:27 ` [PATCH 1/1] gdb: Introduce new setting to filter out " Stephan Rohr @ 2026-08-13 4:24 ` Simon Marchi 0 siblings, 0 replies; 3+ messages in thread From: Simon Marchi @ 2026-08-13 4:24 UTC (permalink / raw) To: Stephan Rohr, gdb-patches On 8/11/26 4:27 PM, Stephan Rohr wrote: > From: "Sargsyan, Eduard" <eduard.sargsyan@intel.com> > > Add a new setting 'print shadowed on|off' to control the printing of > shadowed variables in 'info locals'. > > When shadowed variables are present, each variable involved in a > shadowing relationship is annotated with its declaration location > '<file:line>'; the ones hidden by an inner declaration are additionally > marked ', shadowed'. > > Given this code, stopped at line 10: > > 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 } > > By default, 'info locals' prints shadowed variables: > > (gdb) info locals > str = 0x555555556009 "nested" <main.c:8> > num = 5 <main.c:9> > str = 0x555555556004 "main" <main.c:5, shadowed> > num = 3 <main.c:6, shadowed> > Use 'set print shadowed off' to hide shadowed variables. > > The user may not want to print shadowed variables: > > (gdb) set print shadowed off > (gdb) info locals > str = 0x555555556009 "nested" > num = 5 > Use 'set print shadowed on' to include shadowed variables. > --- > gdb/NEWS | 8 ++++++++ > gdb/doc/gdb.texinfo | 15 +++++++++++++- > gdb/printcmd.c | 8 +++++++- > gdb/stack.c | 25 +++++++++++++++++++++++- > gdb/testsuite/gdb.ada/var_shadowing.exp | 1 + > gdb/testsuite/gdb.base/options.exp | 1 + > gdb/testsuite/gdb.base/var-shadowing.exp | 15 ++++++++++++++ > gdb/valprint.c | 20 +++++++++++++++++++ > gdb/valprint.h | 3 +++ > 9 files changed, 93 insertions(+), 3 deletions(-) > > diff --git a/gdb/NEWS b/gdb/NEWS > index 10c182067f9..a3348436984 100644 > --- a/gdb/NEWS > +++ b/gdb/NEWS > @@ -3,6 +3,14 @@ > > *** Changes since GDB 18 > > +* New commands > + > +set print shadowed on|off > +show print shadowed > + This controls the output of the "info locals" command for C/C++/Fortran. If > + the option is 'off' shadowed variables will be omitted in output. The > + default is to print shadowed variables. > + > *** Changes in GDB 18 > > * Support for the Common Trace Format (CTF) has been removed. GDB now > diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo > index 0030698dcee..232a9901a2c 100644 > --- a/gdb/doc/gdb.texinfo > +++ b/gdb/doc/gdb.texinfo > @@ -9212,7 +9212,8 @@ same name which is declared within an inner scope (decision block, > method, or inner class). When shadowing is detected, location > information is added to all instances of the shadowed variable name. > The outermost instances are additionally followed by @samp{shadowed} > -to indicate that they are not the active variable. > +to indicate that they are not the active variable. Printing shadowed > +variables can be controlled by @ref{set print shadowed}. > > @item info locals [-q] [-t @var{type_regexp}] [@var{regexp}] > Like @kbd{info locals}, but only print the local variables selected > @@ -12829,6 +12830,18 @@ Do not pretty print C@t{++} virtual function tables. > > @item show print vtbl > Show whether C@t{++} virtual function tables are pretty printed, or not. > + > +@anchor{set print shadowed} > +@item set print shadowed > +@itemx set print shadowed on > +@cindex shadowed variables in C/C@t{++}/Fortran > +Print shadowed variables for C/C@t{++}/Fortran. The default is on. > + > +@item set print shadowed off > +Do not print shadowed variables for C/C@t{++}/Fortran. > + > +@item show print shadowed > +Show whether C/C@t{++}/Fortran shadowed variables are printed or not. > @end table Is there a reason you specify C/C++/Fortran everywhere? Apparently, Ada supports shadowing too (although I haven't verified). But in any case, I don't think it's necessary to specify the languages everywhere like this. If you want to mention the languages that support it, then perhaps one mention in the section about shadowed variables should be enough (but even then, I feel like it's bound to become outdated at some point). > diff --git a/gdb/stack.c b/gdb/stack.c > index 954ef6b11a4..cd21aa3a40c 100644 > --- a/gdb/stack.c > +++ b/gdb/stack.c > @@ -2258,6 +2258,7 @@ struct print_variable_and_value_data > int num_tabs; > struct ui_file *stream; > int values_printed; > + bool has_shadowed_variables; I think it would be good to initialize the field directly in here, instead of doing it at the call site. I don't know if it matters, but print_frame_arg_vars uses this type without initializing the field. It would just be simpler and safer to do it here. > > void operator() (const char *print_name, struct symbol *sym, > var_shadowing shadow_status); > @@ -2292,6 +2293,8 @@ print_variable_and_value_data::operator() (const char *print_name, > shadow_status); > > values_printed = 1; Claude found this corner case: int main (void) { double x = 1.5; { int x = 2; x = x + 1; /* bp */ return x; } } (gdb) set print shadowed off (gdb) info locals -t double Use 'set print shadowed on' to include shadowed variables. What do we want to print here? I think it's a case where it would probably make sense to print the "No matching locals" message, but also the "Use 'set print shadowed on'..." message to indicate that some variables were hidden due to shadowing. The point it made was that here, values_printed is set even if the call to print_variable_and_value above didn't actually print a variable. > @@ -2360,7 +2364,26 @@ print_frame_local_vars (const frame_info_ptr &frame, > > iterate_over_block_local_vars_printing (block, cb_data); > > - if (!cb_data.values_printed && !quiet) > + if (quiet) > + return; > + > + if (cb_data.values_printed) > + { > + if (!cb_data.has_shadowed_variables) > + return; > + > + value_print_options opts; > + get_user_print_options (&opts); > + if (opts.print_shadowed) > + gdb_printf (stream, > + _("Use 'set print shadowed off' " > + "to hide shadowed variables.\n")); > + else > + gdb_printf (stream, > + _("Use 'set print shadowed on' " > + "to include shadowed variables.\n")); I would find it a bit heavy to see this message printed every time, but at least having shadowed variables is not that common. And the positive side is that it makes the setting discoverable. Nit about the message: if there are some shadowed variables printed, it's quite clear why you would see the message "Use '...' to hide shadowed variables". However, if they were hidden, it's not obvious why GDB prints "Use '...' to include shadowed variable". The user has to infer that GDB prints this because there are hidden shadowed variable. I think the message should be more direct, saying something like "Some shadowed variables were omitted, use '...' to include them". Printing this here means that it will be visible in `bt -full`, like this: (gdb) bt -full #0 func () at test.c:9 x = 4 <test.c:8> x = 2 <test.c:5, shadowed> y = 3 Use 'set print shadowed off' to hide shadowed variables. #1 0x000055555555517b in main () at test.c:28 str = 0x555555556009 "nested" <test.c:24> num = 0 <test.c:25> str = 0x555555556004 "main" <test.c:21, shadowed> num = 3 <test.c:22, shadowed> Use 'set print shadowed off' to hide shadowed variables. Just wondering if you expected it to work like this or not. > diff --git a/gdb/testsuite/gdb.base/options.exp b/gdb/testsuite/gdb.base/options.exp > index 35487ead6a7..34e4b642181 100644 > --- a/gdb/testsuite/gdb.base/options.exp > +++ b/gdb/testsuite/gdb.base/options.exp > @@ -198,6 +198,7 @@ proc_with_prefix test-print {{prefix ""}} { > "-pretty" > "-raw-values" > "-repeats" > + "-shadowed" > "-static-members" > "-symbol" > "-union" This looks wrong, I don't think the print command accepts a "-shadowed" option, so it shouldn't exist in the completion. On the other hand, it would be nice for "info locals" to accept "-shadowed on|off". Simon ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-08-13 4:25 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-08-11 20:27 [PATCH 0/1] gdb: add 'set print shadowed' to hide shadowed variables Stephan Rohr 2026-08-11 20:27 ` [PATCH 1/1] gdb: Introduce new setting to filter out " Stephan Rohr 2026-08-13 4:24 ` Simon Marchi
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox