From 9ba5ef4bf1e3266db47eb500beab803d03c74b3f Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Tue, 12 Nov 2024 13:07:46 -0700 Subject: Omit artificial symbols from DAP variables response While testing DAP, we found a situation where a compiler-generated variable caused the "variables" request to fail -- the variable in question being an apparent 67-megabyte string. It seems to me that artificial variables like this aren't interesting to DAP users, and the gdb CLI omits these as well. This patch changes DAP to omit these variables, adding a new gdb.Symbol.is_artificial attribute to make this possible. --- gdb/NEWS | 4 ++ gdb/doc/python.texi | 5 +++ gdb/python/lib/gdb/FrameDecorator.py | 3 ++ gdb/python/lib/gdb/dap/globalvars.py | 2 +- gdb/python/py-symbol.c | 14 ++++++ gdb/testsuite/gdb.python/py-sym-artificial.exp | 62 ++++++++++++++++++++++++++ 6 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.python/py-sym-artificial.exp diff --git a/gdb/NEWS b/gdb/NEWS index 047f8ad..7f0bd7e 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -89,6 +89,8 @@ ** The 'qualified' argument to gdb.Breakpoint constructor will no longer accept non-bool types. + ** Added the gdb.Symbol.is_artificial attribute. + * Debugger Adapter Protocol changes ** The "scopes" request will now return a scope holding global @@ -104,6 +106,8 @@ (or send a response) until after the "configurationDone" request has been sent. + ** The "variables" request will not return artificial symbols. + * New commands show jit-reader-directory diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 5cbcc5b..063dc7e 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -6208,6 +6208,11 @@ local variables will require a frame, but other symbols will not. @code{True} if the symbol is an argument of a function. @end defvar +@defvar Symbol.is_artificial +@code{True} if the symbol is artificial. An artificial symbol is one +that is introduced by the compiler. +@end defvar + @defvar Symbol.is_constant @code{True} if the symbol is a constant. @end defvar diff --git a/gdb/python/lib/gdb/FrameDecorator.py b/gdb/python/lib/gdb/FrameDecorator.py index 82412de..5cdfbe1 100644 --- a/gdb/python/lib/gdb/FrameDecorator.py +++ b/gdb/python/lib/gdb/FrameDecorator.py @@ -285,6 +285,9 @@ class FrameVars(object): # returns False for arguments as well. Anyway, # don't include non-variables here. continue + elif sym.is_artificial: + # Skip artificial symbols. + continue lvars.append(SymValueWrapper(frame, sym)) if block.function is not None: diff --git a/gdb/python/lib/gdb/dap/globalvars.py b/gdb/python/lib/gdb/dap/globalvars.py index 38bdc5c..104b242 100644 --- a/gdb/python/lib/gdb/dap/globalvars.py +++ b/gdb/python/lib/gdb/dap/globalvars.py @@ -86,7 +86,7 @@ def get_global_scope(frame): syms = [] block_iter = block while block_iter is not None: - syms += [sym for sym in block_iter if sym.is_variable] + syms += [sym for sym in block_iter if sym.is_variable and not sym.is_artificial] block_iter = block_iter.superblock if len(syms) == 0: diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c index 24b53bb..c1f8d6c 100644 --- a/gdb/python/py-symbol.c +++ b/gdb/python/py-symbol.c @@ -205,6 +205,18 @@ sympy_is_variable (PyObject *self, void *closure) || theclass == LOC_OPTIMIZED_OUT)); } +/* Implementation of Symbol.is_artificial. */ + +static PyObject * +sympy_is_artificial (PyObject *self, void *closure) +{ + struct symbol *symbol = nullptr; + + SYMPY_REQUIRE_VALID (self, symbol); + + return PyBool_FromLong (symbol->is_artificial ()); +} + /* Implementation of gdb.Symbol.needs_frame -> Boolean. Returns true iff the symbol needs a frame for evaluation. */ @@ -709,6 +721,8 @@ to display demangled or mangled names.", NULL }, { "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." }, { "is_argument", sympy_is_argument, NULL, "True if the symbol is an argument of a function." }, + { "is_artificial", sympy_is_artificial, nullptr, + "True if the symbol is marked artificial." }, { "is_constant", sympy_is_constant, NULL, "True if the symbol is a constant." }, { "is_function", sympy_is_function, NULL, diff --git a/gdb/testsuite/gdb.python/py-sym-artificial.exp b/gdb/testsuite/gdb.python/py-sym-artificial.exp new file mode 100644 index 0000000..3ae516c --- /dev/null +++ b/gdb/testsuite/gdb.python/py-sym-artificial.exp @@ -0,0 +1,62 @@ +# Copyright (C) 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# Test gdb.Symbol.artificial. + +load_lib dwarf.exp +load_lib gdb-python.exp + +require dwarf2_support allow_python_tests + +# Just use a no-op main. +standard_testfile py-arch.c -dw.S + +set asm_file [standard_output_file ${srcfile2}] +Dwarf::assemble $asm_file { + cu {} { + compile_unit { + {language @DW_LANG_C} + {name py-sym-artificial.c} + } { + declare_labels signed + + signed: DW_TAG_base_type { + {DW_AT_byte_size 1 DW_FORM_sdata} + {DW_AT_encoding @DW_ATE_signed} + {DW_AT_name bool} + } + + DW_TAG_variable { + {name the_variable} + {DW_AT_type :$signed} + {artificial 1 DW_FORM_flag_present} + } + } + } +} + +if {[prepare_for_testing "failed to prepare" ${testfile} \ + [list $srcfile $asm_file] {nodebug}]} { + return -1 +} + +if {![runto_main]} { + return -1 +} + +gdb_py_test_silent_cmd "python v = gdb.lookup_symbol('the_variable')" \ + "get variable" 1 +gdb_test "python print(v\[0\].is_artificial)" True \ + "variable is artificial" -- cgit v1.1