public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] gdb/selftest-arch: Make register_test_foreach_arch generate arch tests lazily
@ 2022-04-19  8:18 Lancelot SIX
  0 siblings, 0 replies; only message in thread
From: Lancelot SIX @ 2022-04-19  8:18 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=531c82a1c724079f98a4b069584681bc66da4dae

commit 531c82a1c724079f98a4b069584681bc66da4dae
Author: Lancelot SIX <lancelot.six@amd.com>
Date:   Tue Mar 22 10:02:54 2022 -0400

    gdb/selftest-arch: Make register_test_foreach_arch generate arch tests lazily
    
    The register_test_foreach_arch is used to instantiate a given selftest
    for all architectures supported by GDB.  It is used in many _initialize_*
    functions (under initialize_all_files, called by gdb_init).
    
    Because the call is done during GDB's initialization, and because there
    is no guaranty about the order in which all the _initialize_* functions
    are executed, when register_test_foreach_arch is called, GDB is not
    fully initialized.  Specifically, when a particular initialize function
    is executed, only the architectures registered at that point are listed
    by gdbarch_printable_names.
    
    As a consequence, the list of selftest effectively executed depends on
    the order the _initialize_* functions are called.  This can be observed
    with the following:
    
        $ ./gdb/gdb \
            -data-directory ./gdb/data-directory \
            -quiet -batch -ex "maint selftest" 2>&1 \
            | grep -E "Ran [0-9]+ unit tests"
        Ran 145 unit tests, 0 failed
        $ GDB_REVERSE_INIT_FUNCTIONS=1 ./gdb/gdb \
            -data-directory ./gdb/data-directory \
            -quiet -batch -ex "maint selftest" 2>&1 \
            | grep -E "Ran [0-9]+ unit tests"
        Ran 82 unit tests, 0 failed
    
    To fix this, make register_test_foreach_arch register a lazy selftest
    generator.  This way when the test generator is eventually executed, all
    architectures are registered and we do not have a dependency on the
    order the initialize functions are executed in.
    
    Tested on x86_64-linux
    
    Change-Id: I88eefebf7d372ad672f42d3a103e89354bc8a925

Diff:
---
 gdb/selftest-arch.c                | 29 +++++++++++++++++++++--------
 gdb/selftest-arch.h                |  3 +++
 gdb/testsuite/gdb.gdb/unittest.exp | 25 +++++++++++++++++++++++--
 3 files changed, 47 insertions(+), 10 deletions(-)

diff --git a/gdb/selftest-arch.c b/gdb/selftest-arch.c
index 1375838b0c2..f434da718d5 100644
--- a/gdb/selftest-arch.c
+++ b/gdb/selftest-arch.c
@@ -49,14 +49,15 @@ static bool skip_arch (const char *arch)
   return false;
 }
 
-/* Register a kind of selftest that calls the test function once for each
-   gdbarch known to GDB.  */
+/* Generate a selftest for each gdbarch known to GDB.  */
 
-void
-register_test_foreach_arch (const std::string &name,
-			    self_test_foreach_arch_function *function)
+static std::vector<selftest>
+foreach_arch_test_generator (const std::string &name,
+			     self_test_foreach_arch_function *function)
 {
+  std::vector<selftest> tests;
   std::vector<const char *> arches = gdbarch_printable_names ();
+  tests.reserve (arches.size ());
   for (const char *arch : arches)
     {
       if (skip_arch (arch))
@@ -73,10 +74,22 @@ register_test_foreach_arch (const std::string &name,
 	     reset ();
 	   });
 
-      std::string test_name
-	= name + std::string ("::") + std::string (arch);
-      register_test (test_name, test_fn);
+      tests.emplace_back (string_printf ("%s::%s", name.c_str (), arch),
+			  test_fn);
     }
+  return tests;
+}
+
+/* See selftest-arch.h.  */
+
+void
+register_test_foreach_arch (const std::string &name,
+			    self_test_foreach_arch_function *function)
+{
+  add_lazy_generator ([=] ()
+		      {
+		        return foreach_arch_test_generator (name, function);
+		      });
 }
 
 void
diff --git a/gdb/selftest-arch.h b/gdb/selftest-arch.h
index f359e4a0ed9..6bdef267c44 100644
--- a/gdb/selftest-arch.h
+++ b/gdb/selftest-arch.h
@@ -23,6 +23,9 @@ typedef void self_test_foreach_arch_function (struct gdbarch *);
 
 namespace selftests
 {
+
+/* Register a selftest running FUNCTION for each arch supported by GDB. */
+
 extern void
   register_test_foreach_arch (const std::string &name,
 			      self_test_foreach_arch_function *function);
diff --git a/gdb/testsuite/gdb.gdb/unittest.exp b/gdb/testsuite/gdb.gdb/unittest.exp
index 46583cd3504..2967b994cc3 100644
--- a/gdb/testsuite/gdb.gdb/unittest.exp
+++ b/gdb/testsuite/gdb.gdb/unittest.exp
@@ -58,11 +58,12 @@ proc run_selftests { binfile } {
 	}
 	-re "Selftests have been disabled for this build.\r\n$gdb_prompt $" {
 	    unsupported $test
+	    set num_ran 0
 	    set enabled 0
 	}
     }
 
-    return $enabled
+    return [list $enabled $num_ran]
 }
 
 # Test completion of command "maintenance selftest".
@@ -86,7 +87,27 @@ proc_with_prefix test_completion {} {
 }
 
 with_test_prefix "no executable loaded" {
-    set self_tests_enabled [run_selftests ""]
+    set res [run_selftests ""]
+    set self_tests_enabled [lindex $res 0]
+    set num_ran [lindex $res 1]
+}
+
+if { $self_tests_enabled && ![is_remote host] } {
+    # Check that we have the same amount of selftests whatever the
+    # initialization order of GDB.
+    with_test_prefix "reversed initialization" {
+	save_vars { env(GDB_REVERSE_INIT_FUNCTIONS) } {
+	    if [info exists env(GDB_REVERSE_INIT_FUNCTIONS)] {
+		unset env(GDB_REVERSE_INIT_FUNCTIONS)
+	    } else {
+		set env(GDB_REVERSE_INIT_FUNCTIONS) 1
+	    }
+
+	    set res [run_selftests ""]
+	    gdb_assert "$num_ran == [lindex $res 1]" \
+		"selftest not dependent on initialization order"
+	}
+    }
 }
 
 with_test_prefix "executable loaded" {


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-04-19  8:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-19  8:18 [binutils-gdb] gdb/selftest-arch: Make register_test_foreach_arch generate arch tests lazily Lancelot SIX

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).