public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>, Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 2/2] gdb/testsuite: new test for recent dwarf reader issue
Date: Thu,  8 Dec 2022 15:38:33 +0000	[thread overview]
Message-ID: <c7588e14cbf089e6922b7a254402ffb7ca05fb53.1670513780.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1670513780.git.aburgess@redhat.com>

This commit provides a test for this commit:

  commit 55fc1623f942fba10362cb199f9356d75ca5835b
  Date:   Thu Nov 3 13:49:17 2022 -0600

      Add name canonicalization for C

Which resolves PR gdb/29105.  My reason for writing this test was a
desire to better understand the above commit, my process was to study
the commit until I thought I understood it, then write a test to
expose the issue.  As the original commit didn't have a test, I
thought it wouldn't hurt to commit this upstream.

The problem tested for here is already described in the above commit,
but I'll give a brief description here.  This description describes
GDB prior to the above commit:

  - Builtin types are added to GDB using their canonical name,
    e.g. "short", not "signed short",

  - When the user does something like 'p sizeof(short)', then this is
    handled in c-exp.y, and results in a call to lookup_signed_type
    for the name "int".  The "int" here is actually being looked up as
    the type for the result of the 'sizeof' expression,

  - In lookup_signed_type GDB first adds a 'signed' and looks for that
    type, so in this case 'signed int', and, if that lookup fails, GDB
    then looks up 'int',

  - The problem is that 'signed int' is not the canonical name for a
    signed int, so no builtin type with that name will be found, GDB
    will then go to each object file in turn looking for a matching
    type,

  - When checking each object file, GDB will first check the partial
    symtab to see if the full symtab should be expanded or not.
    Remember, at this point GDB is looking for 'signed int', there
    will be no partial symbols with that name, so GDB will not expand
    anything,

  - However, GDB checks each partial symbol using multiple languages,
    not just the current language (C in this case), so, when GDB
    checks using the C++ language, the symbol name is first demangled,
    the code that does this can be found
    lookup_name_info::language_lookup_name.  As the demangled form of
    'signed int' is just 'int', GDB then looks for any symbols with
    the name 'int', most partial symtabs will contain such a symbol,
    so GDB ends up expanding pretty much every symtab.

The above commit fixes this by avoiding the use of non-canonical names
with C, now the initial builtin type lookup will succeed, and GDB
never even considers whether to expand any additional symtabs.

The test case creates a library that includes char, short, int, and
long types, and a test program that links against the library.

In the test script we start the inferior, but don't allow it to
progress far enough that the debug information for the library has
been fully expanded yet.

Then we evaluate some 'sizeof(TYPE)' expressions.

In the buggy version of GDB this would cause the debug information
for the library to be fully expanded, while in the fixed version of
GDB this will not be the case.

We use 'info sources' to determine if the debug information has been
fully expanded or not.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29105
---
 .../gdb.base/signed-builtin-types-lib.c       |  30 +++++
 gdb/testsuite/gdb.base/signed-builtin-types.c |  25 ++++
 .../gdb.base/signed-builtin-types.exp         | 112 ++++++++++++++++++
 3 files changed, 167 insertions(+)
 create mode 100644 gdb/testsuite/gdb.base/signed-builtin-types-lib.c
 create mode 100644 gdb/testsuite/gdb.base/signed-builtin-types.c
 create mode 100644 gdb/testsuite/gdb.base/signed-builtin-types.exp

diff --git a/gdb/testsuite/gdb.base/signed-builtin-types-lib.c b/gdb/testsuite/gdb.base/signed-builtin-types-lib.c
new file mode 100644
index 00000000000..a32ec223ec1
--- /dev/null
+++ b/gdb/testsuite/gdb.base/signed-builtin-types-lib.c
@@ -0,0 +1,30 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2022 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 <http://www.gnu.org/licenses/>.  */
+
+extern int foo (void);
+
+short short_var = 1;
+int int_var = 2;
+long long_var = 3;
+char char_var = 4;
+
+int
+foo (void)
+{
+  /* Just use all the globals!  This works out as zero.  */
+  return (char_var / int_var) - (long_var - short_var);
+}
diff --git a/gdb/testsuite/gdb.base/signed-builtin-types.c b/gdb/testsuite/gdb.base/signed-builtin-types.c
new file mode 100644
index 00000000000..1975b20e277
--- /dev/null
+++ b/gdb/testsuite/gdb.base/signed-builtin-types.c
@@ -0,0 +1,25 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2022 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 <http://www.gnu.org/licenses/>.  */
+
+extern int foo (void);
+
+int
+main (void)
+{
+  int result = foo ();
+  return result;
+}
diff --git a/gdb/testsuite/gdb.base/signed-builtin-types.exp b/gdb/testsuite/gdb.base/signed-builtin-types.exp
new file mode 100644
index 00000000000..e9784330fee
--- /dev/null
+++ b/gdb/testsuite/gdb.base/signed-builtin-types.exp
@@ -0,0 +1,112 @@
+# Copyright 2022 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 <http://www.gnu.org/licenses/>.
+
+if {[skip_shlib_tests]} {
+    return -1
+}
+
+standard_testfile .c -lib.c
+
+# Compile the shared library.
+set srcdso [file join $srcdir $subdir $srcfile2]
+set objdso [standard_output_file lib${gdb_test_file_name}.so]
+if {[gdb_compile_shlib $srcdso $objdso {debug}] != ""} {
+    untested "failed to compile dso"
+    return -1
+}
+
+# Build the test executable and runto main.
+set opts [list debug shlib=$objdso]
+if { [prepare_for_testing "failed to " $testfile $srcfile $opts] } {
+    return -1
+}
+
+if {![runto_main]} {
+    return -1
+}
+
+if {[readnow]} {
+    untested "this test checks for delayed symtab expansion"
+    return -1
+}
+
+# Use 'info sources' to check if the debug information for the shared
+# library has been fully expanded or not.  Return true if the debug
+# information has NOT been fully expanded (which is what we want for this
+# test).
+proc shared_library_debug_not_fully_expanded {} {
+    set library_expanded ""
+    gdb_test_multiple "info sources" "" {
+	-re "^info sources\r\n" {
+	    exp_continue
+	}
+	-re "^(\[^\r\n\]+):\r\n\\(Full debug information has not yet been read for this file\\.\\)\r\n\r\n" {
+	    set libname $expect_out(1,string)
+	    if {$libname == $::objdso} {
+		set library_expanded "no"
+	    }
+	    exp_continue
+	}
+	-re "^(\[^\r\n\]+):\r\n\\(Objfile has no debug information\\.\\)\r\n\r\n" {
+	    set libname $expect_out(1,string)
+	    if {$libname == $::objdso} {
+		# For some reason the shared library has no debug
+		# information, this is not expected.
+		set library_expanded "missing debug"
+	    }
+	    exp_continue
+	}
+	-re "^(\[^\r\n\]+):\r\n\r\n" {
+	    set libname $expect_out(1,string)
+	    if {$libname == $::objdso} {
+		set library_expanded "yes"
+	    }
+	    exp_continue
+	}
+	-re "^$::gdb_prompt $" {
+	    gdb_assert {[string equal $library_expanded "yes"] \
+			    || [string equal $library_expanded "no"]} \
+		$gdb_test_name
+	}
+	-re "^(\[^\r\n:\]*)\r\n" {
+	    exp_continue
+	}
+    }
+
+    return [expr $library_expanded == "no"]
+}
+
+foreach_with_prefix type_name {"short" "int" "long" "char"} {
+    foreach_with_prefix type_prefix {"" "signed" "unsigned"} {
+	with_test_prefix "before sizeof expression" {
+	    # Check that the debug information for the shared library has
+	    # not yet been read in.
+	    gdb_assert { [shared_library_debug_not_fully_expanded] }
+	}
+
+	# Evaluate a sizeof expression for a builtin type.  At one point GDB
+	# would fail to find the builtin type, and would then start
+	# expanding compilation units looking for a suitable debug entry,
+	# for some builtin types GDB would never find a suitable match, and
+	# so would end up expanding all available compilation units.
+	gdb_test "print/d sizeof ($type_prefix $type_name)" " = $decimal"
+
+	with_test_prefix "after sizeof expression" {
+	    # Check that the debug information for the shared library has not
+	    # yet been read in.
+	    gdb_assert { [shared_library_debug_not_fully_expanded] }
+	}
+    }
+}
-- 
2.25.4


  parent reply	other threads:[~2022-12-08 15:38 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-08 15:38 [PATCH 0/2] New test for slow DWARF " Andrew Burgess
2022-12-08 15:38 ` [PATCH 1/2] gdb/testsuite: fix readnow detection Andrew Burgess
2022-12-08 15:38 ` Andrew Burgess [this message]
2022-12-09 18:18   ` [PATCH 2/2] gdb/testsuite: new test for recent dwarf reader issue Tom Tromey
2022-12-09 19:24     ` Andrew Burgess
2022-12-14 14:47       ` Luis Machado
2022-12-15 11:22         ` Andrew Burgess
2022-12-19 13:20           ` Luis Machado
2022-12-19 13:52             ` Andrew Burgess
2022-12-20  8:43               ` tdevries
2022-12-20 10:32                 ` Andrew Burgess
2022-12-20 13:20                   ` Andrew Burgess
2022-12-20 14:04                     ` Luis Machado
2022-12-20 14:54                     ` tdevries
2022-12-24 16:05                       ` Andrew Burgess
2022-12-09 18:18 ` [PATCH 0/2] New test for slow DWARF " Tom Tromey
2022-12-14 10:25   ` Andrew Burgess

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=c7588e14cbf089e6922b7a254402ffb7ca05fb53.1670513780.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.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).