GDB (xrefs)
Loading...
Searching...
No Matches
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-2023 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#include "gdbsupport/selftest.h"
22#include "selftest-arch.h"
23#include "target.h"
24#include "test-target.h"
25#include "target-float.h"
26#include "gdbsupport/def-vector.h"
27#include "gdbarch.h"
28#include "scoped-mock-context.h"
29
30#include <map>
31
32namespace selftests {
33
34/* Test gdbarch methods register_to_value and value_to_register. */
35
36static void
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,
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,
56 builtin->builtin_string,
57 builtin->builtin_bool,
58 builtin->builtin_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
74 scoped_mock_context<test_target_ops> mockctx (gdbarch);
75
77 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
78
79 /* Test gdbarch methods register_to_value and value_to_register with
80 different combinations of register numbers and types. */
81 for (const auto &type : types)
82 {
83 for (auto regnum = 0; regnum < num_regs; regnum++)
84 {
86 {
87 std::vector<gdb_byte> expected (type->length (), 0);
88
89 if (type->code () == TYPE_CODE_FLT)
90 {
91 /* Generate valid float format. */
92 target_float_from_string (expected.data (), type, "1.25");
93 }
94 else
95 {
96 for (auto j = 0; j < expected.size (); j++)
97 expected[j] = (regnum + j) % 16;
98 }
99
101 expected.data ());
102
103 /* Allocate two bytes more for overflow check. */
104 std::vector<gdb_byte> buf (type->length () + 2, 0);
105 int optim, unavail, ok;
106
107 /* Set the fingerprint in the last two bytes. */
108 buf [type->length ()]= 'w';
109 buf [type->length () + 1]= 'l';
111 buf.data (), &optim, &unavail);
112
113 SELF_CHECK (ok);
114 SELF_CHECK (!optim);
115 SELF_CHECK (!unavail);
116
117 SELF_CHECK (buf[type->length ()] == 'w');
118 SELF_CHECK (buf[type->length () + 1] == 'l');
119
120 for (auto k = 0; k < type->length (); k++)
121 SELF_CHECK (buf[k] == expected[k]);
122 }
123 }
124 }
125}
126
127/* Test function gdbarch_register_name. */
128
129static void
131{
132 scoped_mock_context<test_target_ops> mockctx (gdbarch);
133
134 /* Track the number of times each register name appears. */
135 std::map<const std::string, int> name_counts;
136
137 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
138 for (auto regnum = 0; regnum < num_regs; regnum++)
139 {
140 /* If a register is to be hidden from the user then we should get
141 back an empty string, not nullptr. Every other register should
142 return a non-empty string. */
143 const char *name = gdbarch_register_name (gdbarch, regnum);
144
145 if (run_verbose() && name == nullptr)
146 debug_printf ("arch: %s, register: %d returned nullptr\n",
147 gdbarch_bfd_arch_info (gdbarch)->printable_name,
148 regnum);
149 SELF_CHECK (name != nullptr);
150
151 /* Every register name, that is not the empty string, should be
152 unique. If this is not the case then the user will see duplicate
153 copies of the register in e.g. 'info registers' output, but will
154 only be able to interact with one of the copies. */
155 if (*name != '\0')
156 {
157 std::string s (name);
158 name_counts[s]++;
159 if (run_verbose() && name_counts[s] > 1)
160 debug_printf ("arch: %s, register: %d (%s) is a duplicate\n",
161 gdbarch_bfd_arch_info (gdbarch)->printable_name,
162 regnum, name);
163 SELF_CHECK (name_counts[s] == 1);
164 }
165 }
166}
167
168} // namespace selftests
169
171void
int regnum
const char *const name
frame_info_ptr get_current_frame(void)
Definition frame.c:1670
void gdbarch_value_to_register(struct gdbarch *gdbarch, frame_info_ptr frame, int regnum, struct type *type, const gdb_byte *buf)
Definition gdbarch.c:2511
const char * gdbarch_register_name(struct gdbarch *gdbarch, int regnr)
Definition gdbarch.c:2173
int gdbarch_register_to_value(struct gdbarch *gdbarch, frame_info_ptr frame, int regnum, struct type *type, gdb_byte *buf, int *optimizedp, int *unavailablep)
Definition gdbarch.c:2494
int gdbarch_convert_register_p(struct gdbarch *gdbarch, int regnum, struct type *type)
Definition gdbarch.c:2477
const struct bfd_arch_info * gdbarch_bfd_arch_info(struct gdbarch *gdbarch)
Definition gdbarch.c:1387
void _initialize_gdbarch_selftests()
static int gdbarch_num_cooked_regs(gdbarch *arch)
Definition gdbarch.h:390
const struct builtin_type * builtin_type(struct gdbarch *gdbarch)
Definition gdbtypes.c:6168
static void register_name_test(struct gdbarch *gdbarch)
static void register_to_value_test(struct gdbarch *gdbarch)
void register_test_foreach_arch(const std::string &name, self_test_foreach_arch_function *function)
struct type * builtin_signed_char
Definition gdbtypes.h:2082
struct type * builtin_long_long
Definition gdbtypes.h:2096
struct type * builtin_uint16
Definition gdbtypes.h:2116
struct type * builtin_double
Definition gdbtypes.h:2090
struct type * builtin_string
Definition gdbtypes.h:2094
struct type * builtin_int8
Definition gdbtypes.h:2113
struct type * builtin_uint128
Definition gdbtypes.h:2124
struct type * builtin_long
Definition gdbtypes.h:2081
struct type * builtin_bool
Definition gdbtypes.h:2095
struct type * builtin_complex
Definition gdbtypes.h:2092
struct type * builtin_long_double
Definition gdbtypes.h:2091
struct type * builtin_unsigned_long_long
Definition gdbtypes.h:2097
struct type * builtin_uint32
Definition gdbtypes.h:2120
struct type * builtin_uint64
Definition gdbtypes.h:2122
struct type * builtin_char16
Definition gdbtypes.h:2127
struct type * builtin_short
Definition gdbtypes.h:2079
struct type * builtin_double_complex
Definition gdbtypes.h:2093
struct type * builtin_int64
Definition gdbtypes.h:2121
struct type * builtin_char
Definition gdbtypes.h:2078
struct type * builtin_int
Definition gdbtypes.h:2080
struct type * builtin_int32
Definition gdbtypes.h:2119
struct type * builtin_unsigned_short
Definition gdbtypes.h:2084
struct type * builtin_unsigned_int
Definition gdbtypes.h:2085
struct type * builtin_char32
Definition gdbtypes.h:2128
struct type * builtin_uint8
Definition gdbtypes.h:2114
struct type * builtin_int128
Definition gdbtypes.h:2123
struct type * builtin_unsigned_long
Definition gdbtypes.h:2086
struct type * builtin_int16
Definition gdbtypes.h:2115
struct type * builtin_void
Definition gdbtypes.h:2077
struct type * builtin_float
Definition gdbtypes.h:2089
type_code code() const
Definition gdbtypes.h:956
ULONGEST length() const
Definition gdbtypes.h:983
bool target_float_from_string(gdb_byte *addr, const struct type *type, const std::string &string)