/* Self tests for gdbarch for GDB, the GNU debugger.
- Copyright (C) 2017-2022 Free Software Foundation, Inc.
+ Copyright (C) 2017-2023 Free Software Foundation, Inc.
This file is part of GDB.
#include "gdbarch.h"
#include "scoped-mock-context.h"
+#include <map>
+
namespace selftests {
/* Test gdbarch methods register_to_value and value_to_register. */
scoped_mock_context<test_target_ops> mockctx (gdbarch);
- struct frame_info *frame = get_current_frame ();
+ frame_info_ptr frame = get_current_frame ();
const int num_regs = gdbarch_num_cooked_regs (gdbarch);
/* Test gdbarch methods register_to_value and value_to_register with
}
}
+/* Test function gdbarch_register_name. */
+
+static void
+register_name_test (struct gdbarch *gdbarch)
+{
+ scoped_mock_context<test_target_ops> mockctx (gdbarch);
+
+ /* Track the number of times each register name appears. */
+ std::map<const std::string, int> name_counts;
+
+ const int num_regs = gdbarch_num_cooked_regs (gdbarch);
+ for (auto regnum = 0; regnum < num_regs; regnum++)
+ {
+ /* If a register is to be hidden from the user then we should get
+ back an empty string, not nullptr. Every other register should
+ return a non-empty string. */
+ const char *name = gdbarch_register_name (gdbarch, regnum);
+
+ if (run_verbose() && name == nullptr)
+ debug_printf ("arch: %s, register: %d returned nullptr\n",
+ gdbarch_bfd_arch_info (gdbarch)->printable_name,
+ regnum);
+ SELF_CHECK (name != nullptr);
+
+ /* Every register name, that is not the empty string, should be
+ unique. If this is not the case then the user will see duplicate
+ copies of the register in e.g. 'info registers' output, but will
+ only be able to interact with one of the copies. */
+ if (*name != '\0')
+ {
+ std::string s (name);
+ name_counts[s]++;
+ if (run_verbose() && name_counts[s] > 1)
+ debug_printf ("arch: %s, register: %d (%s) is a duplicate\n",
+ gdbarch_bfd_arch_info (gdbarch)->printable_name,
+ regnum, name);
+ SELF_CHECK (name_counts[s] == 1);
+ }
+ }
+}
+
} // namespace selftests
void _initialize_gdbarch_selftests ();
{
selftests::register_test_foreach_arch ("register_to_value",
selftests::register_to_value_test);
+
+ selftests::register_test_foreach_arch ("register_name",
+ selftests::register_name_test);
}