binutils-gdb/gdb/selftest-arch.c
Lancelot SIX 531c82a1c7 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
2022-04-19 09:12:42 +01:00

104 lines
2.6 KiB
C

/* GDB self-test for each gdbarch.
Copyright (C) 2017-2022 Free Software Foundation, Inc.
This file is part of GDB.
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/>. */
#include "defs.h"
#include <functional>
#if GDB_SELF_TEST
#include "gdbsupport/selftest.h"
#include "selftest-arch.h"
#include "arch-utils.h"
namespace selftests {
static bool skip_arch (const char *arch)
{
if (strcmp ("fr300", arch) == 0)
{
/* PR 20946 */
return true;
}
if (strcmp ("powerpc:EC603e", arch) == 0
|| strcmp ("powerpc:e500mc", arch) == 0
|| strcmp ("powerpc:e500mc64", arch) == 0
|| strcmp ("powerpc:titan", arch) == 0
|| strcmp ("powerpc:vle", arch) == 0
|| strcmp ("powerpc:e5500", arch) == 0
|| strcmp ("powerpc:e6500", arch) == 0)
{
/* PR 19797 */
return true;
}
return false;
}
/* Generate a selftest for each gdbarch known to GDB. */
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))
continue;
auto test_fn
= ([=] ()
{
struct gdbarch_info info;
info.bfd_arch_info = bfd_scan_arch (arch);
struct gdbarch *gdbarch = gdbarch_find_by_info (info);
SELF_CHECK (gdbarch != NULL);
function (gdbarch);
reset ();
});
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
reset ()
{
/* Clear GDB internal state. */
registers_changed ();
reinit_frame_cache ();
}
} // namespace selftests
#endif /* GDB_SELF_TEST */