Remove OBJF_REORDERED
[binutils-gdb.git] / gdb / gdbarch-selftests.c
1 /* Self tests for gdbarch for GDB, the GNU debugger.
2
3 Copyright (C) 2017-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "gdbsupport/selftest.h"
22 #include "selftest-arch.h"
23 #include "target.h"
24 #include "test-target.h"
25 #include "target-float.h"
26 #include "gdbsupport/def-vector.h"
27 #include "gdbarch.h"
28 #include "scoped-mock-context.h"
29
30 #include <map>
31
32 namespace selftests {
33
34 /* Test gdbarch methods register_to_value and value_to_register. */
35
36 static void
37 register_to_value_test (struct gdbarch *gdbarch)
38 {
39 const struct builtin_type *builtin = builtin_type (gdbarch);
40 struct type *types[] =
41 {
42 builtin->builtin_void,
43 builtin->builtin_char,
44 builtin->builtin_short,
45 builtin->builtin_int,
46 builtin->builtin_long,
47 builtin->builtin_signed_char,
48 builtin->builtin_unsigned_short,
49 builtin->builtin_unsigned_int,
50 builtin->builtin_unsigned_long,
51 builtin->builtin_float,
52 builtin->builtin_double,
53 builtin->builtin_long_double,
54 builtin->builtin_complex,
55 builtin->builtin_double_complex,
56 builtin->builtin_string,
57 builtin->builtin_bool,
58 builtin->builtin_long_long,
59 builtin->builtin_unsigned_long_long,
60 builtin->builtin_int8,
61 builtin->builtin_uint8,
62 builtin->builtin_int16,
63 builtin->builtin_uint16,
64 builtin->builtin_int32,
65 builtin->builtin_uint32,
66 builtin->builtin_int64,
67 builtin->builtin_uint64,
68 builtin->builtin_int128,
69 builtin->builtin_uint128,
70 builtin->builtin_char16,
71 builtin->builtin_char32,
72 };
73
74 scoped_mock_context<test_target_ops> mockctx (gdbarch);
75
76 frame_info_ptr frame = get_current_frame ();
77 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
78
79 /* Test gdbarch methods register_to_value and value_to_register with
80 different combinations of register numbers and types. */
81 for (const auto &type : types)
82 {
83 for (auto regnum = 0; regnum < num_regs; regnum++)
84 {
85 if (gdbarch_convert_register_p (gdbarch, regnum, type))
86 {
87 std::vector<gdb_byte> expected (type->length (), 0);
88
89 if (type->code () == TYPE_CODE_FLT)
90 {
91 /* Generate valid float format. */
92 target_float_from_string (expected.data (), type, "1.25");
93 }
94 else
95 {
96 for (auto j = 0; j < expected.size (); j++)
97 expected[j] = (regnum + j) % 16;
98 }
99
100 gdbarch_value_to_register (gdbarch, frame, regnum, type,
101 expected.data ());
102
103 /* Allocate two bytes more for overflow check. */
104 std::vector<gdb_byte> buf (type->length () + 2, 0);
105 int optim, unavail, ok;
106
107 /* Set the fingerprint in the last two bytes. */
108 buf [type->length ()]= 'w';
109 buf [type->length () + 1]= 'l';
110 ok = gdbarch_register_to_value (gdbarch, frame, regnum, type,
111 buf.data (), &optim, &unavail);
112
113 SELF_CHECK (ok);
114 SELF_CHECK (!optim);
115 SELF_CHECK (!unavail);
116
117 SELF_CHECK (buf[type->length ()] == 'w');
118 SELF_CHECK (buf[type->length () + 1] == 'l');
119
120 for (auto k = 0; k < type->length (); k++)
121 SELF_CHECK (buf[k] == expected[k]);
122 }
123 }
124 }
125 }
126
127 /* Test function gdbarch_register_name. */
128
129 static void
130 register_name_test (struct gdbarch *gdbarch)
131 {
132 scoped_mock_context<test_target_ops> mockctx (gdbarch);
133
134 /* Track the number of times each register name appears. */
135 std::map<const std::string, int> name_counts;
136
137 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
138 for (auto regnum = 0; regnum < num_regs; regnum++)
139 {
140 /* If a register is to be hidden from the user then we should get
141 back an empty string, not nullptr. Every other register should
142 return a non-empty string. */
143 const char *name = gdbarch_register_name (gdbarch, regnum);
144
145 if (run_verbose() && name == nullptr)
146 debug_printf ("arch: %s, register: %d returned nullptr\n",
147 gdbarch_bfd_arch_info (gdbarch)->printable_name,
148 regnum);
149 SELF_CHECK (name != nullptr);
150
151 /* Every register name, that is not the empty string, should be
152 unique. If this is not the case then the user will see duplicate
153 copies of the register in e.g. 'info registers' output, but will
154 only be able to interact with one of the copies. */
155 if (*name != '\0')
156 {
157 std::string s (name);
158 name_counts[s]++;
159 if (run_verbose() && name_counts[s] > 1)
160 debug_printf ("arch: %s, register: %d (%s) is a duplicate\n",
161 gdbarch_bfd_arch_info (gdbarch)->printable_name,
162 regnum, name);
163 SELF_CHECK (name_counts[s] == 1);
164 }
165 }
166 }
167
168 } // namespace selftests
169
170 void _initialize_gdbarch_selftests ();
171 void
172 _initialize_gdbarch_selftests ()
173 {
174 selftests::register_test_foreach_arch ("register_to_value",
175 selftests::register_to_value_test);
176
177 selftests::register_test_foreach_arch ("register_name",
178 selftests::register_name_test);
179 }