gdb: bool-ify follow_fork
[deliverable/binutils-gdb.git] / gdb / gdbarch-selftests.c
CommitLineData
b77b02a5
YQ
1/* Self tests for gdbarch for GDB, the GNU debugger.
2
b811d2c2 3 Copyright (C) 2017-2020 Free Software Foundation, Inc.
b77b02a5
YQ
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"
268a13a5 21#include "gdbsupport/selftest.h"
b77b02a5
YQ
22#include "selftest-arch.h"
23#include "inferior.h"
55b11ddf
PA
24#include "gdbthread.h"
25#include "target.h"
c180496d 26#include "test-target.h"
f69fdf9b 27#include "target-float.h"
268a13a5 28#include "gdbsupport/def-vector.h"
0d12e84c 29#include "gdbarch.h"
b77b02a5
YQ
30
31namespace selftests {
32
b77b02a5
YQ
33/* Test gdbarch methods register_to_value and value_to_register. */
34
35static void
36register_to_value_test (struct gdbarch *gdbarch)
37{
38 const struct builtin_type *builtin = builtin_type (gdbarch);
39 struct type *types[] =
40 {
41 builtin->builtin_void,
42 builtin->builtin_char,
43 builtin->builtin_short,
44 builtin->builtin_int,
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,
71 };
72
55b11ddf
PA
73 /* Create a mock environment. An inferior with a thread, with a
74 process_stratum target pushed. */
75
76 test_target_ops mock_target;
77 ptid_t mock_ptid (1, 1);
78 inferior mock_inferior (mock_ptid.pid ());
79 address_space mock_aspace {};
80 mock_inferior.gdbarch = gdbarch;
81 mock_inferior.aspace = &mock_aspace;
82 thread_info mock_thread (&mock_inferior, mock_ptid);
83
84 scoped_restore restore_thread_list
08036331 85 = make_scoped_restore (&mock_inferior.thread_list, &mock_thread);
55b11ddf
PA
86
87 /* Add the mock inferior to the inferior list so that look ups by
88 target+ptid can find it. */
89 scoped_restore restore_inferior_list
90 = make_scoped_restore (&inferior_list);
91 inferior_list = &mock_inferior;
92
93 /* Switch to the mock inferior. */
94 scoped_restore_current_inferior restore_current_inferior;
95 set_current_inferior (&mock_inferior);
96
97 /* Push the process_stratum target so we can mock accessing
98 registers. */
99 push_target (&mock_target);
100
101 /* Pop it again on exit (return/exception). */
e587ef42 102 SCOPE_EXIT { pop_all_targets_at_and_above (process_stratum); };
b77b02a5 103
55b11ddf
PA
104 /* Switch to the mock thread. */
105 scoped_restore restore_inferior_ptid
106 = make_scoped_restore (&inferior_ptid, mock_ptid);
107
108 struct frame_info *frame = get_current_frame ();
f6efe3f8 109 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
b77b02a5 110
b77b02a5
YQ
111 /* Test gdbarch methods register_to_value and value_to_register with
112 different combinations of register numbers and types. */
113 for (const auto &type : types)
114 {
115 for (auto regnum = 0; regnum < num_regs; regnum++)
116 {
117 if (gdbarch_convert_register_p (gdbarch, regnum, type))
118 {
119 std::vector<gdb_byte> expected (TYPE_LENGTH (type), 0);
120
121 if (TYPE_CODE (type) == TYPE_CODE_FLT)
122 {
b77b02a5 123 /* Generate valid float format. */
f69fdf9b 124 target_float_from_string (expected.data (), type, "1.25");
b77b02a5
YQ
125 }
126 else
127 {
128 for (auto j = 0; j < expected.size (); j++)
129 expected[j] = (regnum + j) % 16;
130 }
131
132 gdbarch_value_to_register (gdbarch, frame, regnum, type,
133 expected.data ());
134
135 /* Allocate two bytes more for overflow check. */
136 std::vector<gdb_byte> buf (TYPE_LENGTH (type) + 2, 0);
137 int optim, unavail, ok;
138
139 /* Set the fingerprint in the last two bytes. */
140 buf [TYPE_LENGTH (type)]= 'w';
141 buf [TYPE_LENGTH (type) + 1]= 'l';
142 ok = gdbarch_register_to_value (gdbarch, frame, regnum, type,
143 buf.data (), &optim, &unavail);
144
145 SELF_CHECK (ok);
146 SELF_CHECK (!optim);
147 SELF_CHECK (!unavail);
148
149 SELF_CHECK (buf[TYPE_LENGTH (type)] == 'w');
150 SELF_CHECK (buf[TYPE_LENGTH (type) + 1] == 'l');
151
152 for (auto k = 0; k < TYPE_LENGTH(type); k++)
153 SELF_CHECK (buf[k] == expected[k]);
154 }
155 }
156 }
157}
158
159} // namespace selftests
b77b02a5 160
6c265988 161void _initialize_gdbarch_selftests ();
b77b02a5 162void
6c265988 163_initialize_gdbarch_selftests ()
b77b02a5 164{
1526853e
SM
165 selftests::register_test_foreach_arch ("register_to_value",
166 selftests::register_to_value_test);
b77b02a5 167}
This page took 0.268923 seconds and 4 git commands to generate.