1 /* Self tests for gdbarch for GDB, the GNU debugger.
3 Copyright (C) 2017-2024 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/>. */
20 #include "gdbsupport/selftest.h"
21 #include "selftest-arch.h"
23 #include "test-target.h"
24 #include "target-float.h"
25 #include "gdbsupport/def-vector.h"
27 #include "scoped-mock-context.h"
33 /* Test gdbarch methods register_to_value and value_to_register. */
36 register_to_value_test (struct gdbarch
*gdbarch
)
38 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
39 struct type
*types
[] =
41 builtin
->builtin_void
,
42 builtin
->builtin_char
,
43 builtin
->builtin_short
,
45 builtin
->builtin_long
,
46 builtin
->builtin_signed_char
,
47 builtin
->builtin_unsigned_short
,
48 builtin
->builtin_unsigned_int
,
49 builtin
->builtin_unsigned_long
,
50 builtin
->builtin_float
,
51 builtin
->builtin_double
,
52 builtin
->builtin_long_double
,
53 builtin
->builtin_complex
,
54 builtin
->builtin_double_complex
,
55 builtin
->builtin_string
,
56 builtin
->builtin_bool
,
57 builtin
->builtin_long_long
,
58 builtin
->builtin_unsigned_long_long
,
59 builtin
->builtin_int8
,
60 builtin
->builtin_uint8
,
61 builtin
->builtin_int16
,
62 builtin
->builtin_uint16
,
63 builtin
->builtin_int32
,
64 builtin
->builtin_uint32
,
65 builtin
->builtin_int64
,
66 builtin
->builtin_uint64
,
67 builtin
->builtin_int128
,
68 builtin
->builtin_uint128
,
69 builtin
->builtin_char16
,
70 builtin
->builtin_char32
,
73 scoped_mock_context
<test_target_ops
> mockctx (gdbarch
);
75 frame_info_ptr frame
= get_current_frame ();
76 const int num_regs
= gdbarch_num_cooked_regs (gdbarch
);
78 /* Test gdbarch methods register_to_value and value_to_register with
79 different combinations of register numbers and types. */
80 for (const auto &type
: types
)
82 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
84 if (gdbarch_convert_register_p (gdbarch
, regnum
, type
))
86 std::vector
<gdb_byte
> expected (type
->length (), 0);
88 if (type
->code () == TYPE_CODE_FLT
)
90 /* Generate valid float format. */
91 target_float_from_string (expected
.data (), type
, "1.25");
95 for (auto j
= 0; j
< expected
.size (); j
++)
96 expected
[j
] = (regnum
+ j
) % 16;
99 gdbarch_value_to_register (gdbarch
, frame
, regnum
, type
,
102 /* Allocate two bytes more for overflow check. */
103 std::vector
<gdb_byte
> buf (type
->length () + 2, 0);
104 int optim
, unavail
, ok
;
106 /* Set the fingerprint in the last two bytes. */
107 buf
[type
->length ()]= 'w';
108 buf
[type
->length () + 1]= 'l';
109 ok
= gdbarch_register_to_value (gdbarch
, frame
, regnum
, type
,
110 buf
.data (), &optim
, &unavail
);
114 SELF_CHECK (!unavail
);
116 SELF_CHECK (buf
[type
->length ()] == 'w');
117 SELF_CHECK (buf
[type
->length () + 1] == 'l');
119 for (auto k
= 0; k
< type
->length (); k
++)
120 SELF_CHECK (buf
[k
] == expected
[k
]);
126 /* Test function gdbarch_register_name. */
129 register_name_test (struct gdbarch
*gdbarch
)
131 scoped_mock_context
<test_target_ops
> mockctx (gdbarch
);
133 /* Track the number of times each register name appears. */
134 std::map
<const std::string
, int> name_counts
;
136 const int num_regs
= gdbarch_num_cooked_regs (gdbarch
);
137 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
139 /* If a register is to be hidden from the user then we should get
140 back an empty string, not nullptr. Every other register should
141 return a non-empty string. */
142 const char *name
= gdbarch_register_name (gdbarch
, regnum
);
144 if (run_verbose() && name
== nullptr)
145 debug_printf ("arch: %s, register: %d returned nullptr\n",
146 gdbarch_bfd_arch_info (gdbarch
)->printable_name
,
148 SELF_CHECK (name
!= nullptr);
150 /* Every register name, that is not the empty string, should be
151 unique. If this is not the case then the user will see duplicate
152 copies of the register in e.g. 'info registers' output, but will
153 only be able to interact with one of the copies. */
156 std::string
s (name
);
158 if (run_verbose() && name_counts
[s
] > 1)
159 debug_printf ("arch: %s, register: %d (%s) is a duplicate\n",
160 gdbarch_bfd_arch_info (gdbarch
)->printable_name
,
162 SELF_CHECK (name_counts
[s
] == 1);
167 /* Test gdbarch_stack_grows_down. Stacks must either grow down or up. */
170 check_stack_growth (struct gdbarch
*gdbarch
)
172 /* We don't call gdbarch_stack_grows_down here, instead we're testing the
173 implementation by calling gdbarch_inner_than. GDB assumes that stacks
174 either grow down or up (see uses of gdbarch_stack_grows_down), so exactly
175 one of these needs to be true. */
176 bool stack_grows_down
= gdbarch_inner_than (gdbarch
, 1, 2);
177 bool stack_grows_up
= gdbarch_inner_than (gdbarch
, 2, 1);
179 SELF_CHECK (stack_grows_up
!= stack_grows_down
);
182 } // namespace selftests
184 void _initialize_gdbarch_selftests ();
186 _initialize_gdbarch_selftests ()
188 selftests::register_test_foreach_arch ("register_to_value",
189 selftests::register_to_value_test
);
191 selftests::register_test_foreach_arch ("register_name",
192 selftests::register_name_test
);
194 selftests::register_test_foreach_arch ("stack_growth",
195 selftests::check_stack_growth
);