1 /* Self tests for gdbarch for GDB, the GNU debugger.
3 Copyright (C) 2017 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/>. */
23 #include "selftest-arch.h"
25 #include "gdbthread.h"
27 #include "target-float.h"
31 /* A mock process_stratum target_ops that doesn't read/write registers
35 test_target_has_registers (target_ops
*self
)
41 test_target_has_stack (target_ops
*self
)
47 test_target_has_memory (target_ops
*self
)
53 test_target_prepare_to_store (target_ops
*self
, regcache
*regs
)
58 test_target_store_registers (target_ops
*self
, regcache
*regs
, int regno
)
62 class test_target_ops
: public target_ops
69 to_stratum
= process_stratum
;
70 to_has_memory
= test_target_has_memory
;
71 to_has_stack
= test_target_has_stack
;
72 to_has_registers
= test_target_has_registers
;
73 to_prepare_to_store
= test_target_prepare_to_store
;
74 to_store_registers
= test_target_store_registers
;
76 complete_target_initialization (this);
80 /* Test gdbarch methods register_to_value and value_to_register. */
83 register_to_value_test (struct gdbarch
*gdbarch
)
85 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
86 struct type
*types
[] =
88 builtin
->builtin_void
,
89 builtin
->builtin_char
,
90 builtin
->builtin_short
,
92 builtin
->builtin_long
,
93 builtin
->builtin_signed_char
,
94 builtin
->builtin_unsigned_short
,
95 builtin
->builtin_unsigned_int
,
96 builtin
->builtin_unsigned_long
,
97 builtin
->builtin_float
,
98 builtin
->builtin_double
,
99 builtin
->builtin_long_double
,
100 builtin
->builtin_complex
,
101 builtin
->builtin_double_complex
,
102 builtin
->builtin_string
,
103 builtin
->builtin_bool
,
104 builtin
->builtin_long_long
,
105 builtin
->builtin_unsigned_long_long
,
106 builtin
->builtin_int8
,
107 builtin
->builtin_uint8
,
108 builtin
->builtin_int16
,
109 builtin
->builtin_uint16
,
110 builtin
->builtin_int32
,
111 builtin
->builtin_uint32
,
112 builtin
->builtin_int64
,
113 builtin
->builtin_uint64
,
114 builtin
->builtin_int128
,
115 builtin
->builtin_uint128
,
116 builtin
->builtin_char16
,
117 builtin
->builtin_char32
,
120 /* Error out if debugging something, because we're going to push the
121 test target, which would pop any existing target. */
122 if (current_target
.to_stratum
>= process_stratum
)
123 error (_("target already pushed"));
125 /* Create a mock environment. An inferior with a thread, with a
126 process_stratum target pushed. */
128 test_target_ops mock_target
;
129 ptid_t
mock_ptid (1, 1);
130 inferior
mock_inferior (mock_ptid
.pid ());
131 address_space mock_aspace
{};
132 mock_inferior
.gdbarch
= gdbarch
;
133 mock_inferior
.aspace
= &mock_aspace
;
134 thread_info
mock_thread (&mock_inferior
, mock_ptid
);
136 scoped_restore restore_thread_list
137 = make_scoped_restore (&thread_list
, &mock_thread
);
139 /* Add the mock inferior to the inferior list so that look ups by
140 target+ptid can find it. */
141 scoped_restore restore_inferior_list
142 = make_scoped_restore (&inferior_list
);
143 inferior_list
= &mock_inferior
;
145 /* Switch to the mock inferior. */
146 scoped_restore_current_inferior restore_current_inferior
;
147 set_current_inferior (&mock_inferior
);
149 /* Push the process_stratum target so we can mock accessing
151 push_target (&mock_target
);
153 /* Pop it again on exit (return/exception). */
158 pop_all_targets_at_and_above (process_stratum
);
162 /* Switch to the mock thread. */
163 scoped_restore restore_inferior_ptid
164 = make_scoped_restore (&inferior_ptid
, mock_ptid
);
166 struct frame_info
*frame
= get_current_frame ();
167 const int num_regs
= (gdbarch_num_regs (gdbarch
)
168 + gdbarch_num_pseudo_regs (gdbarch
));
170 /* Test gdbarch methods register_to_value and value_to_register with
171 different combinations of register numbers and types. */
172 for (const auto &type
: types
)
174 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
176 if (gdbarch_convert_register_p (gdbarch
, regnum
, type
))
178 std::vector
<gdb_byte
> expected (TYPE_LENGTH (type
), 0);
180 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
182 /* Generate valid float format. */
183 target_float_from_string (expected
.data (), type
, "1.25");
187 for (auto j
= 0; j
< expected
.size (); j
++)
188 expected
[j
] = (regnum
+ j
) % 16;
191 gdbarch_value_to_register (gdbarch
, frame
, regnum
, type
,
194 /* Allocate two bytes more for overflow check. */
195 std::vector
<gdb_byte
> buf (TYPE_LENGTH (type
) + 2, 0);
196 int optim
, unavail
, ok
;
198 /* Set the fingerprint in the last two bytes. */
199 buf
[TYPE_LENGTH (type
)]= 'w';
200 buf
[TYPE_LENGTH (type
) + 1]= 'l';
201 ok
= gdbarch_register_to_value (gdbarch
, frame
, regnum
, type
,
202 buf
.data (), &optim
, &unavail
);
206 SELF_CHECK (!unavail
);
208 SELF_CHECK (buf
[TYPE_LENGTH (type
)] == 'w');
209 SELF_CHECK (buf
[TYPE_LENGTH (type
) + 1] == 'l');
211 for (auto k
= 0; k
< TYPE_LENGTH(type
); k
++)
212 SELF_CHECK (buf
[k
] == expected
[k
]);
218 } // namespace selftests
219 #endif /* GDB_SELF_TEST */
222 _initialize_gdbarch_selftests (void)
225 selftests::register_test_foreach_arch ("register_to_value",
226 selftests::register_to_value_test
);