1 /* Self tests for gdbarch for GDB, the GNU debugger.
3 Copyright (C) 2017-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "gdbsupport/selftest.h"
22 #include "selftest-arch.h"
24 #include "test-target.h"
25 #include "target-float.h"
26 #include "gdbsupport/def-vector.h"
28 #include "scoped-mock-context.h"
34 /* Test gdbarch methods register_to_value and value_to_register. */
37 register_to_value_test (struct gdbarch
*gdbarch
)
39 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
40 struct type
*types
[] =
42 builtin
->builtin_void
,
43 builtin
->builtin_char
,
44 builtin
->builtin_short
,
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
,
74 scoped_mock_context
<test_target_ops
> mockctx (gdbarch
);
76 frame_info_ptr frame
= get_current_frame ();
77 const int num_regs
= gdbarch_num_cooked_regs (gdbarch
);
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
)
83 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
85 if (gdbarch_convert_register_p (gdbarch
, regnum
, type
))
87 std::vector
<gdb_byte
> expected (type
->length (), 0);
89 if (type
->code () == TYPE_CODE_FLT
)
91 /* Generate valid float format. */
92 target_float_from_string (expected
.data (), type
, "1.25");
96 for (auto j
= 0; j
< expected
.size (); j
++)
97 expected
[j
] = (regnum
+ j
) % 16;
100 gdbarch_value_to_register (gdbarch
, frame
, regnum
, type
,
103 /* Allocate two bytes more for overflow check. */
104 std::vector
<gdb_byte
> buf (type
->length () + 2, 0);
105 int optim
, unavail
, ok
;
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
);
115 SELF_CHECK (!unavail
);
117 SELF_CHECK (buf
[type
->length ()] == 'w');
118 SELF_CHECK (buf
[type
->length () + 1] == 'l');
120 for (auto k
= 0; k
< type
->length (); k
++)
121 SELF_CHECK (buf
[k
] == expected
[k
]);
127 /* Test function gdbarch_register_name. */
130 register_name_test (struct gdbarch
*gdbarch
)
132 scoped_mock_context
<test_target_ops
> mockctx (gdbarch
);
134 /* Track the number of times each register name appears. */
135 std::map
<const std::string
, int> name_counts
;
137 const int num_regs
= gdbarch_num_cooked_regs (gdbarch
);
138 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
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
);
145 if (run_verbose() && name
== nullptr)
146 debug_printf ("arch: %s, register: %d returned nullptr\n",
147 gdbarch_bfd_arch_info (gdbarch
)->printable_name
,
149 SELF_CHECK (name
!= nullptr);
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. */
157 std::string
s (name
);
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
,
163 SELF_CHECK (name_counts
[s
] == 1);
168 } // namespace selftests
170 void _initialize_gdbarch_selftests ();
172 _initialize_gdbarch_selftests ()
174 selftests::register_test_foreach_arch ("register_to_value",
175 selftests::register_to_value_test
);
177 selftests::register_test_foreach_arch ("register_name",
178 selftests::register_name_test
);