Commit | Line | Data |
---|---|---|
b77b02a5 YQ |
1 | /* Self tests for gdbarch for GDB, the GNU debugger. |
2 | ||
42a4f53d | 3 | Copyright (C) 2017-2019 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" | |
21 | #if GDB_SELF_TEST | |
268a13a5 | 22 | #include "gdbsupport/selftest.h" |
b77b02a5 YQ |
23 | #include "selftest-arch.h" |
24 | #include "inferior.h" | |
55b11ddf PA |
25 | #include "gdbthread.h" |
26 | #include "target.h" | |
c180496d | 27 | #include "test-target.h" |
f69fdf9b | 28 | #include "target-float.h" |
268a13a5 | 29 | #include "gdbsupport/def-vector.h" |
0d12e84c | 30 | #include "gdbarch.h" |
b77b02a5 YQ |
31 | |
32 | namespace selftests { | |
33 | ||
b77b02a5 YQ |
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 | ||
55b11ddf PA |
74 | /* Error out if debugging something, because we're going to push the |
75 | test target, which would pop any existing target. */ | |
66b4deae | 76 | if (current_top_target ()->stratum () >= process_stratum) |
f6ac5f3d | 77 | error (_("target already pushed")); |
55b11ddf PA |
78 | |
79 | /* Create a mock environment. An inferior with a thread, with a | |
80 | process_stratum target pushed. */ | |
81 | ||
82 | test_target_ops mock_target; | |
83 | ptid_t mock_ptid (1, 1); | |
84 | inferior mock_inferior (mock_ptid.pid ()); | |
85 | address_space mock_aspace {}; | |
86 | mock_inferior.gdbarch = gdbarch; | |
87 | mock_inferior.aspace = &mock_aspace; | |
88 | thread_info mock_thread (&mock_inferior, mock_ptid); | |
708e4b9f | 89 | mock_inferior.thread_map[mock_ptid] = &mock_thread; |
55b11ddf PA |
90 | |
91 | /* Add the mock inferior to the inferior list so that look ups by | |
92 | target+ptid can find it. */ | |
93 | scoped_restore restore_inferior_list | |
94 | = make_scoped_restore (&inferior_list); | |
95 | inferior_list = &mock_inferior; | |
96 | ||
97 | /* Switch to the mock inferior. */ | |
98 | scoped_restore_current_inferior restore_current_inferior; | |
99 | set_current_inferior (&mock_inferior); | |
100 | ||
101 | /* Push the process_stratum target so we can mock accessing | |
102 | registers. */ | |
103 | push_target (&mock_target); | |
104 | ||
105 | /* Pop it again on exit (return/exception). */ | |
e587ef42 | 106 | SCOPE_EXIT { pop_all_targets_at_and_above (process_stratum); }; |
b77b02a5 | 107 | |
55b11ddf PA |
108 | /* Switch to the mock thread. */ |
109 | scoped_restore restore_inferior_ptid | |
110 | = make_scoped_restore (&inferior_ptid, mock_ptid); | |
111 | ||
112 | struct frame_info *frame = get_current_frame (); | |
f6efe3f8 | 113 | const int num_regs = gdbarch_num_cooked_regs (gdbarch); |
b77b02a5 | 114 | |
b77b02a5 YQ |
115 | /* Test gdbarch methods register_to_value and value_to_register with |
116 | different combinations of register numbers and types. */ | |
117 | for (const auto &type : types) | |
118 | { | |
119 | for (auto regnum = 0; regnum < num_regs; regnum++) | |
120 | { | |
121 | if (gdbarch_convert_register_p (gdbarch, regnum, type)) | |
122 | { | |
123 | std::vector<gdb_byte> expected (TYPE_LENGTH (type), 0); | |
124 | ||
125 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
126 | { | |
b77b02a5 | 127 | /* Generate valid float format. */ |
f69fdf9b | 128 | target_float_from_string (expected.data (), type, "1.25"); |
b77b02a5 YQ |
129 | } |
130 | else | |
131 | { | |
132 | for (auto j = 0; j < expected.size (); j++) | |
133 | expected[j] = (regnum + j) % 16; | |
134 | } | |
135 | ||
136 | gdbarch_value_to_register (gdbarch, frame, regnum, type, | |
137 | expected.data ()); | |
138 | ||
139 | /* Allocate two bytes more for overflow check. */ | |
140 | std::vector<gdb_byte> buf (TYPE_LENGTH (type) + 2, 0); | |
141 | int optim, unavail, ok; | |
142 | ||
143 | /* Set the fingerprint in the last two bytes. */ | |
144 | buf [TYPE_LENGTH (type)]= 'w'; | |
145 | buf [TYPE_LENGTH (type) + 1]= 'l'; | |
146 | ok = gdbarch_register_to_value (gdbarch, frame, regnum, type, | |
147 | buf.data (), &optim, &unavail); | |
148 | ||
149 | SELF_CHECK (ok); | |
150 | SELF_CHECK (!optim); | |
151 | SELF_CHECK (!unavail); | |
152 | ||
153 | SELF_CHECK (buf[TYPE_LENGTH (type)] == 'w'); | |
154 | SELF_CHECK (buf[TYPE_LENGTH (type) + 1] == 'l'); | |
155 | ||
156 | for (auto k = 0; k < TYPE_LENGTH(type); k++) | |
157 | SELF_CHECK (buf[k] == expected[k]); | |
158 | } | |
159 | } | |
160 | } | |
161 | } | |
162 | ||
163 | } // namespace selftests | |
164 | #endif /* GDB_SELF_TEST */ | |
165 | ||
166 | void | |
167 | _initialize_gdbarch_selftests (void) | |
168 | { | |
169 | #if GDB_SELF_TEST | |
1526853e SM |
170 | selftests::register_test_foreach_arch ("register_to_value", |
171 | selftests::register_to_value_test); | |
b77b02a5 YQ |
172 | #endif |
173 | } |