From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2114) id 14C79386CE61; Tue, 14 Jun 2022 05:13:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 14C79386CE61 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Alok Kumar Sharma To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Debug support for global alias variable X-Act-Checkin: binutils-gdb X-Git-Author: Kavitha Natarajan X-Git-Refname: refs/heads/master X-Git-Oldrev: d712f2768ac5e71027657d85b921fc0e85d94bcd X-Git-Newrev: 6df97c56ea0f3086c96743ec47148ee69fd8cf71 Message-Id: <20220614051303.14C79386CE61@sourceware.org> Date: Tue, 14 Jun 2022 05:13:03 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Jun 2022 05:13:03 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D6df97c56ea0f= 3086c96743ec47148ee69fd8cf71 commit 6df97c56ea0f3086c96743ec47148ee69fd8cf71 Author: Kavitha Natarajan Date: Tue Jun 14 10:37:46 2022 +0530 Debug support for global alias variable =20 Starting with (future) Clang 15 (since https://reviews.llvm.org/D120989), Clang emits the DWARF information of global alias variables as DW_TAG_imported_declaration. However, GDB does not handle it. It incorrectly always reads this tag as C++/Fortran imported declaration (type alias, namespace alias and Fortran module). This commit adds support to handle this tag as an alias variable. =20 This change fixes the failures in the gdb.base/symbol-alias.exp testcase with current git Clang. This testcase is also updated to test nested (recursive) aliases. Diff: --- gdb/dwarf2/read.c | 63 ++++++++++++++++++++++-------= ---- gdb/testsuite/gdb.base/symbol-alias.exp | 63 +++++++++++++++++++++++++++++= ++-- gdb/testsuite/gdb.base/symbol-alias2.c | 12 +++++++ 3 files changed, 114 insertions(+), 24 deletions(-) diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 64cb8d421b9..e22e09b4e2b 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -894,6 +894,10 @@ static void build_type_psymtabs_reader (cutu_reader *r= eader, =20 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile); =20 +static void var_decode_location (struct attribute *attr, + struct symbol *sym, + struct dwarf2_cu *cu); + static unsigned int peek_abbrev_code (bfd *, const gdb_byte *); =20 static const gdb_byte *read_attribute (const struct die_reader_specs *, @@ -1071,7 +1075,7 @@ static struct using_direct **using_directives (struct= dwarf2_cu *cu); =20 static void read_import_statement (struct die_info *die, struct dwarf2_cu = *); =20 -static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *c= u); +static bool read_alias (struct die_info *die, struct dwarf2_cu *cu); =20 static struct type *read_module_type (struct die_info *die, struct dwarf2_cu *cu); @@ -8664,9 +8668,10 @@ process_die (struct die_info *die, struct dwarf2_cu = *cu) break; case DW_TAG_imported_declaration: cu->processing_has_namespace_info =3D true; - if (read_namespace_alias (die, cu)) + if (read_alias (die, cu)) break; - /* The declaration is not a global namespace alias. */ + /* The declaration is neither a global namespace nor a variable + alias. */ /* Fall through. */ case DW_TAG_imported_module: cu->processing_has_namespace_info =3D true; @@ -9134,18 +9139,18 @@ dwarf2_physname (const char *name, struct die_info = *die, struct dwarf2_cu *cu) return retval; } =20 -/* Inspect DIE in CU for a namespace alias. If one exists, record - a new symbol for it. +/* Inspect DIE in CU for a namespace alias or a variable with alias + attribute. If one exists, record a new symbol for it. =20 - Returns 1 if a namespace alias was recorded, 0 otherwise. */ + Returns true if an alias was recorded, false otherwise. */ =20 -static int -read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu) +static bool +read_alias (struct die_info *die, struct dwarf2_cu *cu) { struct attribute *attr; =20 - /* If the die does not have a name, this is not a namespace - alias. */ + /* If the die does not have a name, this is neither a namespace + alias nor a variable alias. */ attr =3D dwarf2_attr (die, DW_AT_name, cu); if (attr !=3D NULL) { @@ -9171,26 +9176,42 @@ read_namespace_alias (struct die_info *die, struct = dwarf2_cu *cu) { complaint (_("DIE at %s has too many recursively imported " "declarations"), sect_offset_str (d->sect_off)); - return 0; + return false; } =20 if (attr !=3D NULL) { struct type *type; - sect_offset sect_off =3D attr->get_ref_die_offset (); - - type =3D get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile); - if (type !=3D NULL && type->code () =3D=3D TYPE_CODE_NAMESPACE) + if (d->tag =3D=3D DW_TAG_variable) { - /* This declaration is a global namespace alias. Add - a symbol for it whose type is the aliased namespace. */ - new_symbol (die, type, cu); - return 1; + /* This declaration is a C/C++ global variable alias. + Add a symbol for it whose type is the same as the + aliased variable's. */ + type =3D die_type (d, imported_cu); + struct symbol *sym =3D new_symbol (die, type, cu); + attr =3D dwarf2_attr (d, DW_AT_location, imported_cu); + sym->set_aclass_index (LOC_UNRESOLVED); + if (attr !=3D nullptr) + var_decode_location (attr, sym, cu); + return true; + } + else + { + sect_offset sect_off =3D attr->get_ref_die_offset (); + type =3D get_die_type_at_offset (sect_off, cu->per_cu, + cu->per_objfile); + if (type !=3D nullptr && type->code () =3D=3D TYPE_CODE_NAMESPACE) + { + /* This declaration is a global namespace alias. Add + a symbol for it whose type is the aliased + namespace. */ + new_symbol (die, type, cu); + return true; + } } } } - - return 0; + return false; } =20 /* Return the using directives repository (global or local?) to use in the diff --git a/gdb/testsuite/gdb.base/symbol-alias.exp b/gdb/testsuite/gdb.ba= se/symbol-alias.exp index 289f49bbc3f..9cc26bb66e5 100644 --- a/gdb/testsuite/gdb.base/symbol-alias.exp +++ b/gdb/testsuite/gdb.base/symbol-alias.exp @@ -15,6 +15,9 @@ # along with this program. If not, see . =20 standard_testfile symbol-alias.c symbol-alias2.c +# Clang versions prior to v15 do not emit debug info for aliases. +set old_clang [expr [test_compiler_info {clang-1[0-4]-*-*}] \ + || [test_compiler_info {clang-[1-9]-*}]] =20 if { [prepare_for_testing "failed to prepare" ${testfile} [list $srcfile $= srcfile2]] } { return -1 @@ -30,7 +33,61 @@ foreach f {"func" "func_alias"} { gdb_test "p *${f}()" "=3D {field1 =3D 1, field2 =3D 2}" } =20 -# Variables. -foreach v {"g_var_s" "g_var_s_alias"} { - gdb_test "p $v" "=3D {field1 =3D 1, field2 =3D 2}" +# Extern global variable. +gdb_test "p g_var_s" "=3D {field1 =3D 1, field2 =3D 2}" +foreach v {"g_var_s_alias" "g_var_s_alias2"} { + gdb_test_multiple "p $v" "print alias variable $v" { + -re -wrap " =3D {field1 =3D 1, field2 =3D 2}" { + pass $gdb_test_name + } + -re -wrap "has unknown type; cast it to its declared type" { + if { $old_clang } { + xfail $gdb_test_name + } else { + fail $gdb_test_name + } + } + } +} + +# Static global variable. +gdb_test "p g_var" " =3D 1" +gdb_test_multiple "p g_var_alias" "print alias of static variable" { + -re -wrap " =3D 1" { + pass $gdb_test_name + } + -re -wrap "has unknown type; cast it to its declared type" { + if { $old_clang } { + xfail $gdb_test_name + } else { + fail $gdb_test_name + } + } +} + +# Alias of a variable that is declared later in the scope. +gdb_test "p g_def_var" " =3D 2" +gdb_test_multiple "p g_def_var_alias" "print g_def_var_alias" { + -re -wrap " =3D 2" { + pass $gdb_test_name + } + -re -wrap "has unknown type; cast it to its declared type" { + if { $old_clang } { + xfail $gdb_test_name + } else { + fail $gdb_test_name + } + } +} +gdb_test_multiple "p g_def_var_alias2" "print g_def_var_alias2" { + -re -wrap " =3D 2" { + pass $gdb_test_name + } + -re -wrap "has unknown type; cast it to its declared type" { + if { $old_clang } { + xfail $gdb_test_name + } else { + fail $gdb_test_name + } + } } diff --git a/gdb/testsuite/gdb.base/symbol-alias2.c b/gdb/testsuite/gdb.bas= e/symbol-alias2.c index 34f4e121e25..c1c0413ed60 100644 --- a/gdb/testsuite/gdb.base/symbol-alias2.c +++ b/gdb/testsuite/gdb.base/symbol-alias2.c @@ -23,6 +23,8 @@ struct S =20 struct S g_var_s =3D { 1, 2 }; =20 +static int g_var =3D 1; + #ifdef __cplusplus /* So that the alias attribute below work without having to figure out this function's mangled name. */ @@ -38,3 +40,13 @@ func (void) struct S *func_alias (void) __attribute__ ((alias ("func"))); =20 extern struct S g_var_s_alias __attribute__ ((alias ("g_var_s"))); + +extern struct S g_var_s_alias2 __attribute__ ((alias ("g_var_s_alias"))); + +extern int g_var_alias __attribute__ ((alias ("g_var"))); + +extern int g_def_var_alias __attribute__ ((alias ("g_def_var"))); + +int g_def_var =3D 2; + +extern int g_def_var_alias2 __attribute__ ((alias ("g_def_var_alias")));