GDB (xrefs)
Loading...
Searching...
No Matches
reggroups.c
Go to the documentation of this file.
1/* Register groupings for GDB, the GNU debugger.
2
3 Copyright (C) 2002-2023 Free Software Foundation, Inc.
4
5 Contributed by Red Hat.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22#include "defs.h"
23#include "arch-utils.h"
24#include "reggroups.h"
25#include "gdbtypes.h"
26#include "regcache.h"
27#include "command.h"
28#include "gdbcmd.h"
29#include "gdbsupport/gdb_obstack.h"
30
31/* See reggroups.h. */
32
33const reggroup *
35{
36 return new reggroup (name, type);
37}
38
39/* See reggroups.h. */
40
41const reggroup *
44{
46 return obstack_new<struct reggroup> (gdbarch_obstack (gdbarch),
47 name, type);
48}
49
50/* A container holding all the register groups for a particular
51 architecture. */
52
54{
56 {
57 /* Add the default groups. */
65 }
66
68
69 /* Add GROUP to the list of register groups. */
70
71 void add (const reggroup *group)
72 {
73 gdb_assert (group != nullptr);
74
75 auto find_by_name = [group] (const reggroup *g)
76 {
77 return streq (group->name (), g->name ());
78 };
79 gdb_assert (std::find_if (m_groups.begin (), m_groups.end (), find_by_name)
80 == m_groups.end ());
81
82 m_groups.push_back (group);
83 }
84
85 /* The number of register groups. */
86
87 std::vector<struct reggroup *>::size_type
88 size () const
89 {
90 return m_groups.size ();
91 }
92
93 /* Return a reference to the list of all groups. */
94
95 const std::vector<const struct reggroup *> &
96 groups () const
97 {
98 return m_groups;
99 }
100
101private:
102 /* The register groups. */
103 std::vector<const struct reggroup *> m_groups;
104};
105
106/* Key used to lookup register group data from a gdbarch. */
107
109
110/* Get the reggroups for the architecture, creating if necessary. */
111
112static reggroups *
114{
116 if (groups == nullptr)
117 groups = reggroups_data.emplace (gdbarch);
118 return groups;
119}
120
121/* See reggroups.h. */
122
123void
124reggroup_add (struct gdbarch *gdbarch, const reggroup *group)
125{
127
128 gdb_assert (groups != nullptr);
129 gdb_assert (group != nullptr);
130
131 groups->add (group);
132}
133
134/* See reggroups.h. */
135const std::vector<const reggroup *> &
137{
139 gdb_assert (groups != nullptr);
140 gdb_assert (groups->size () > 0);
141 return groups->groups ();
142}
143
144/* See reggroups.h. */
145
146int
148 const struct reggroup *group)
149{
150 int vector_p;
151 int float_p;
152 int raw_p;
153
154 if (*gdbarch_register_name (gdbarch, regnum) == '\0')
155 return 0;
156 if (group == all_reggroup)
157 return 1;
158 vector_p = register_type (gdbarch, regnum)->is_vector ();
159 float_p = (register_type (gdbarch, regnum)->code () == TYPE_CODE_FLT
161 == TYPE_CODE_DECFLOAT));
162 raw_p = regnum < gdbarch_num_regs (gdbarch);
163 if (group == float_reggroup)
164 return float_p;
165 if (group == vector_reggroup)
166 return vector_p;
167 if (group == general_reggroup)
168 return (!vector_p && !float_p);
169 if (group == save_reggroup || group == restore_reggroup)
170 return raw_p;
171 return 0;
172}
173
174/* See reggroups.h. */
175
176const reggroup *
177reggroup_find (struct gdbarch *gdbarch, const char *name)
178{
179 for (const struct reggroup *group : gdbarch_reggroups (gdbarch))
180 {
181 if (strcmp (name, group->name ()) == 0)
182 return group;
183 }
184 return NULL;
185}
186
187/* Dump out a table of register groups for the current architecture. */
188
189static void
190reggroups_dump (struct gdbarch *gdbarch, struct ui_file *file)
191{
192 static constexpr const char *fmt = " %-10s %-10s\n";
193
194 gdb_printf (file, fmt, "Group", "Type");
195
196 for (const struct reggroup *group : gdbarch_reggroups (gdbarch))
197 {
198 /* Group name. */
199 const char *name = group->name ();
200
201 /* Group type. */
202 const char *type;
203
204 switch (group->type ())
205 {
206 case USER_REGGROUP:
207 type = "user";
208 break;
210 type = "internal";
211 break;
212 default:
213 internal_error (_("bad switch"));
214 }
215
216 /* Note: If you change this, be sure to also update the
217 documentation. */
218
219 gdb_printf (file, fmt, name, type);
220 }
221}
222
223/* Implement 'maintenance print reggroups' command. */
224
225static void
226maintenance_print_reggroups (const char *args, int from_tty)
227{
228 struct gdbarch *gdbarch = get_current_arch ();
229
230 if (args == NULL)
232 else
233 {
234 stdio_file file;
235
236 if (!file.open (args, "w"))
237 perror_with_name (_("maintenance print reggroups"));
238 reggroups_dump (gdbarch, &file);
239 }
240}
241
242/* Pre-defined register groups. */
243static const reggroup general_group = { "general", USER_REGGROUP };
244static const reggroup float_group = { "float", USER_REGGROUP };
245static const reggroup system_group = { "system", USER_REGGROUP };
246static const reggroup vector_group = { "vector", USER_REGGROUP };
247static const reggroup all_group = { "all", USER_REGGROUP };
248static const reggroup save_group = { "save", INTERNAL_REGGROUP };
249static const reggroup restore_group = { "restore", INTERNAL_REGGROUP };
250
258
260void
262{
263 add_cmd ("reggroups", class_maintenance,
265Print the internal register group names.\n\
266Takes an optional file parameter."),
268
269}
int regnum
const char *const name
struct gdbarch * get_current_arch(void)
Definition arch-utils.c:846
char * gdbarch_obstack_strdup(struct gdbarch *arch, const char *string)
obstack * gdbarch_obstack(gdbarch *arch)
void * get(unsigned key)
Definition registry.h:211
struct cmd_list_element * maintenanceprintlist
Definition cli-cmds.c:151
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition cli-decode.c:233
@ class_maintenance
Definition command.h:65
const char * gdbarch_register_name(struct gdbarch *gdbarch, int regnr)
Definition gdbarch.c:2173
int gdbarch_num_regs(struct gdbarch *gdbarch)
Definition gdbarch.c:1930
struct type * register_type(struct gdbarch *gdbarch, int regnum)
Definition regcache.c:158
static const reggroup vector_group
Definition reggroups.c:246
static void maintenance_print_reggroups(const char *args, int from_tty)
Definition reggroups.c:226
const reggroup *const general_reggroup
Definition reggroups.c:251
int default_register_reggroup_p(struct gdbarch *gdbarch, int regnum, const struct reggroup *group)
Definition reggroups.c:147
const reggroup * reggroup_new(const char *name, enum reggroup_type type)
Definition reggroups.c:34
const reggroup *const system_reggroup
Definition reggroups.c:253
void _initialize_reggroup()
Definition reggroups.c:261
static const reggroup general_group
Definition reggroups.c:243
static const reggroup all_group
Definition reggroups.c:247
static const reggroup float_group
Definition reggroups.c:244
const reggroup * reggroup_find(struct gdbarch *gdbarch, const char *name)
Definition reggroups.c:177
const reggroup *const float_reggroup
Definition reggroups.c:252
const reggroup *const save_reggroup
Definition reggroups.c:256
const reggroup *const all_reggroup
Definition reggroups.c:255
static const registry< gdbarch >::key< reggroups > reggroups_data
Definition reggroups.c:108
void reggroup_add(struct gdbarch *gdbarch, const reggroup *group)
Definition reggroups.c:124
static const reggroup save_group
Definition reggroups.c:248
const std::vector< const reggroup * > & gdbarch_reggroups(struct gdbarch *gdbarch)
Definition reggroups.c:136
static reggroups * get_reggroups(struct gdbarch *gdbarch)
Definition reggroups.c:113
static const reggroup restore_group
Definition reggroups.c:249
static const reggroup system_group
Definition reggroups.c:245
static void reggroups_dump(struct gdbarch *gdbarch, struct ui_file *file)
Definition reggroups.c:190
const reggroup *const restore_reggroup
Definition reggroups.c:257
const reggroup * reggroup_gdbarch_new(struct gdbarch *gdbarch, const char *name, enum reggroup_type type)
Definition reggroups.c:42
const reggroup *const vector_reggroup
Definition reggroups.c:254
reggroup_type
Definition reggroups.h:28
@ INTERNAL_REGGROUP
Definition reggroups.h:36
@ USER_REGGROUP
Definition reggroups.h:32
enum var_types type
Definition scm-param.c:142
const char * name() const
Definition reggroups.h:51
std::vector< structreggroup * >::size_type size() const
Definition reggroups.c:88
void add(const reggroup *group)
Definition reggroups.c:71
const std::vector< const struct reggroup * > & groups() const
Definition reggroups.c:96
DISABLE_COPY_AND_ASSIGN(reggroups)
std::vector< const struct reggroup * > m_groups
Definition reggroups.c:103
type_code code() const
Definition gdbtypes.h:956
bool is_vector() const
Definition gdbtypes.h:1186
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
#define gdb_stdout
Definition utils.h:182