GDB (xrefs)

/tmp/gdb-8.1/gdb/gdbarch-selftests.c
Go to the documentation of this file.
1 /* Self tests for gdbarch for GDB, the GNU debugger.
2
3 Copyright (C) 2017-2018 Free Software Foundation, Inc.
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
22 #include "selftest.h"
23 #include "selftest-arch.h"
24 #include "inferior.h"
25 #include "gdbthread.h"
26 #include "target.h"
27 #include "target-float.h"
28 #include "common/def-vector.h"
29
30 namespace selftests {
31
32 /* Test gdbarch methods register_to_value and value_to_register. */
33
34 static void
35 register_to_value_test (struct gdbarch *gdbarch)
36 {
37 const struct builtin_type *builtin = builtin_type (gdbarch);
38 struct type *types[] =
39 {
40 builtin->builtin_void,
41 builtin->builtin_char,
42 builtin->builtin_short,
43 builtin->builtin_int,
44 builtin->builtin_long,
45 builtin->builtin_signed_char,
46 builtin->builtin_unsigned_short,
47 builtin->builtin_unsigned_int,
48 builtin->builtin_unsigned_long,
49 builtin->builtin_float,
50 builtin->builtin_double,
51 builtin->builtin_long_double,
52 builtin->builtin_complex,
53 builtin->builtin_double_complex,
54 builtin->builtin_string,
55 builtin->builtin_bool,
56 builtin->builtin_long_long,
57 builtin->builtin_unsigned_long_long,
58 builtin->builtin_int8,
59 builtin->builtin_uint8,
60 builtin->builtin_int16,
61 builtin->builtin_uint16,
62 builtin->builtin_int32,
63 builtin->builtin_uint32,
64 builtin->builtin_int64,
65 builtin->builtin_uint64,
66 builtin->builtin_int128,
67 builtin->builtin_uint128,
68 builtin->builtin_char16,
69 builtin->builtin_char32,
70 };
71
72 /* Error out if debugging something, because we're going to push the
73 test target, which would pop any existing target. */
74 if (current_target.to_stratum >= process_stratum)
75 error (_("target already pushed"));
76
77 /* Create a mock environment. An inferior with a thread, with a
78 process_stratum target pushed. */
79
80 test_target_ops mock_target;
81 ptid_t mock_ptid (1, 1);
82 inferior mock_inferior (mock_ptid.pid ());
83 address_space mock_aspace {};
84 mock_inferior.gdbarch = gdbarch;
85 mock_inferior.aspace = &mock_aspace;
86 thread_info mock_thread (&mock_inferior, mock_ptid);
87
88 scoped_restore restore_thread_list
89 = make_scoped_restore (&thread_list, &mock_thread);
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). */
106 struct on_exit
107 {
108 ~on_exit ()
109 {
110 pop_all_targets_at_and_above (process_stratum);
111 }
112 } pop_targets;
113
114 /* Switch to the mock thread. */
115 scoped_restore restore_inferior_ptid
116 = make_scoped_restore (&inferior_ptid, mock_ptid);
117
118 struct frame_info *frame = get_current_frame ();
119 const int num_regs = (gdbarch_num_regs (gdbarch)
120 + gdbarch_num_pseudo_regs (gdbarch));
121
122 /* Test gdbarch methods register_to_value and value_to_register with
123 different combinations of register numbers and types. */
124 for (const auto &type : types)
125 {
126 for (auto regnum = 0; regnum < num_regs; regnum++)
127 {
128 if (gdbarch_convert_register_p (gdbarch, regnum, type))
129 {
130 std::vector<gdb_byte> expected (TYPE_LENGTH (type), 0);
131
132 if (TYPE_CODE (type) == TYPE_CODE_FLT)
133 {
134 /* Generate valid float format. */
135 target_float_from_string (expected.data (), type, "1.25");
136 }
137 else
138 {
139 for (auto j = 0; j < expected.size (); j++)
140 expected[j] = (regnum + j) % 16;
141 }
142
143 gdbarch_value_to_register (gdbarch, frame, regnum, type,
144 expected.data ());
145
146 /* Allocate two bytes more for overflow check. */
147 std::vector<gdb_byte> buf (TYPE_LENGTH (type) + 2, 0);
148 int optim, unavail, ok;
149
150 /* Set the fingerprint in the last two bytes. */
151 buf [TYPE_LENGTH (type)]= 'w';
152 buf [TYPE_LENGTH (type) + 1]= 'l';
153 ok = gdbarch_register_to_value (gdbarch, frame, regnum, type,
154 buf.data (), &optim, &unavail);
155
156 SELF_CHECK (ok);
157 SELF_CHECK (!optim);
158 SELF_CHECK (!unavail);
159
160 SELF_CHECK (buf[TYPE_LENGTH (type)] == 'w');
161 SELF_CHECK (buf[TYPE_LENGTH (type) + 1] == 'l');
162
163 for (auto k = 0; k < TYPE_LENGTH(type); k++)
164 SELF_CHECK (buf[k] == expected[k]);
165 }
166 }
167 }
168 }
169
170 } // namespace selftests
171 #endif /* GDB_SELF_TEST */
172
173 void
174 _initialize_gdbarch_selftests (void)
175 {
176 #if GDB_SELF_TEST
177 selftests::register_test_foreach_arch ("register_to_value",
178 selftests::register_to_value_test);
179 #endif
180 }
gdbarch
Definition: gdbarch.c:127
builtin_type::builtin_long_double
struct type * builtin_long_double
Definition: gdbtypes.h:1512
get_current_frame
struct frame_info * get_current_frame(void)
Definition: frame.c:1563
builtin_type::builtin_unsigned_int
struct type * builtin_unsigned_int
Definition: gdbtypes.h:1508
builtin_type::builtin_double_complex
struct type * builtin_double_complex
Definition: gdbtypes.h:1514
push_target
void push_target(struct target_ops *t)
Definition: target.c:642
builtin_type
const struct builtin_type * builtin_type(struct gdbarch *gdbarch)
Definition: gdbtypes.c:5217
builtin_type::builtin_uint8
struct type * builtin_uint8
Definition: gdbtypes.h:1535
inferior
Definition: inferior.h:310
builtin_type::builtin_uint16
struct type * builtin_uint16
Definition: gdbtypes.h:1537
frame_info
Definition: frame.c:85
gdbarch_num_regs
int gdbarch_num_regs(struct gdbarch *gdbarch)
Definition: gdbarch.c:2039
_
#define _(String)
Definition: gdb_locale.h:35
selftests::register_test_foreach_arch
void register_test_foreach_arch(const std::string &name, self_test_foreach_arch_function *function)
builtin_type::builtin_complex
struct type * builtin_complex
Definition: gdbtypes.h:1513
defs.h
builtin_type
Definition: gdbtypes.h:1495
gdbarch_register_to_value
int gdbarch_register_to_value(struct gdbarch *gdbarch, struct frame_info *frame, int regnum, struct type *type, gdb_byte *buf, int *optimizedp, int *unavailablep)
Definition: gdbarch.c:2612
selftests
Definition: selftest.c:25
builtin_type::builtin_int32
struct type * builtin_int32
Definition: gdbtypes.h:1538
gdbarch_convert_register_p
int gdbarch_convert_register_p(struct gdbarch *gdbarch, int regnum, struct type *type)
Definition: gdbarch.c:2595
builtin_type::builtin_short
struct type * builtin_short
Definition: gdbtypes.h:1502
builtin_type::builtin_char16
struct type * builtin_char16
Definition: gdbtypes.h:1546
make_scoped_restore
scoped_restore_tmpl< T > make_scoped_restore(T *var)
Definition: scoped_restore.h:105
gdbarch_num_pseudo_regs
int gdbarch_num_pseudo_regs(struct gdbarch *gdbarch)
Definition: gdbarch.c:2057
target-float.h
builtin_type::builtin_int128
struct type * builtin_int128
Definition: gdbtypes.h:1542
builtin_type::builtin_unsigned_long
struct type * builtin_unsigned_long
Definition: gdbtypes.h:1509
builtin_type::builtin_char32
struct type * builtin_char32
Definition: gdbtypes.h:1547
current_target
struct target_ops current_target
ptid_t
Definition: ptid.h:35
TYPE_CODE_FLT
Definition: gdbtypes.h:116
builtin_type::builtin_int16
struct type * builtin_int16
Definition: gdbtypes.h:1536
gdbthread.h
inferior.h
scoped_restore_base
Definition: scoped_restore.h:24
scoped_restore_current_inferior
Definition: inferior.h:529
type
Definition: gdbtypes.h:749
inferior_list
struct inferior * inferior_list
Definition: inferior.c:45
target_float_from_string
bool target_float_from_string(gdb_byte *addr, const struct type *type, const std::string &string)
Definition: target-float.c:2369
process_stratum
Definition: target.h:87
builtin_type::builtin_uint32
struct type * builtin_uint32
Definition: gdbtypes.h:1539
type
static const char * type
Definition: language.c:113
builtin_type::builtin_long
struct type * builtin_long
Definition: gdbtypes.h:1504
regnum
int regnum
Definition: aarch64-tdep.c:77
builtin_type::builtin_bool
struct type * builtin_bool
Definition: gdbtypes.h:1516
builtin_type::builtin_uint128
struct type * builtin_uint128
Definition: gdbtypes.h:1543
builtin_type::builtin_unsigned_long_long
struct type * builtin_unsigned_long_long
Definition: gdbtypes.h:1518
builtin_type::builtin_signed_char
struct type * builtin_signed_char
Definition: gdbtypes.h:1505
selftest-arch.h
builtin_type::builtin_string
struct type * builtin_string
Definition: gdbtypes.h:1515
builtin_type::builtin_unsigned_short
struct type * builtin_unsigned_short
Definition: gdbtypes.h:1507
builtin_type::builtin_char
struct type * builtin_char
Definition: gdbtypes.h:1501
builtin_type::builtin_double
struct type * builtin_double
Definition: gdbtypes.h:1511
address_space
Definition: progspace.h:216
gdbarch_value_to_register
void gdbarch_value_to_register(struct gdbarch *gdbarch, struct frame_info *frame, int regnum, struct type *type, const gdb_byte *buf)
Definition: gdbarch.c:2629
TYPE_CODE
#define TYPE_CODE(thistype)
Definition: gdbtypes.h:1238
inferior_ptid
ptid_t inferior_ptid
Definition: infcmd.c:94
target_ops::to_stratum
enum strata to_stratum
Definition: target.h:656
thread_list
struct thread_info * thread_list
Definition: thread.c:53
builtin_type::builtin_long_long
struct type * builtin_long_long
Definition: gdbtypes.h:1517
builtin_type::builtin_int64
struct type * builtin_int64
Definition: gdbtypes.h:1540
selftest.h
def-vector.h
TYPE_LENGTH
#define TYPE_LENGTH(thistype)
Definition: gdbtypes.h:1235
builtin_type::builtin_uint64
struct type * builtin_uint64
Definition: gdbtypes.h:1541
builtin_type::builtin_int8
struct type * builtin_int8
Definition: gdbtypes.h:1534
builtin_type::builtin_void
struct type * builtin_void
Definition: gdbtypes.h:1500
pop_all_targets_at_and_above
void pop_all_targets_at_and_above(enum strata stratum)
Definition: target.c:752
thread_info
Definition: gdbthread.h:205
error
void error(const char *fmt,...)
Definition: errors.c:38
_initialize_gdbarch_selftests
void _initialize_gdbarch_selftests(void)
Definition: gdbarch-selftests.c:174
set_current_inferior
void set_current_inferior(struct inferior *inf)
Definition: inferior.c:64
SELF_CHECK
#define SELF_CHECK(VALUE)
Definition: selftest.h:67
builtin_type::builtin_float
struct type * builtin_float
Definition: gdbtypes.h:1510
builtin_type::builtin_int
struct type * builtin_int
Definition: gdbtypes.h:1503