]>
Commit | Line | Data |
---|---|---|
1 | /* Register groupings for GDB, the GNU debugger. | |
2 | ||
3 | Copyright (C) 2002-2025 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 "arch-utils.h" | |
23 | #include "reggroups.h" | |
24 | #include "gdbtypes.h" | |
25 | #include "regcache.h" | |
26 | #include "command.h" | |
27 | #include "cli/cli-cmds.h" | |
28 | #include "gdbsupport/gdb_obstack.h" | |
29 | ||
30 | /* See reggroups.h. */ | |
31 | ||
32 | const reggroup * | |
33 | reggroup_new (const char *name, enum reggroup_type type) | |
34 | { | |
35 | return new reggroup (name, type); | |
36 | } | |
37 | ||
38 | /* See reggroups.h. */ | |
39 | ||
40 | const reggroup * | |
41 | reggroup_gdbarch_new (struct gdbarch *gdbarch, const char *name, | |
42 | enum reggroup_type type) | |
43 | { | |
44 | name = gdbarch_obstack_strdup (gdbarch, name); | |
45 | return obstack_new<struct reggroup> (gdbarch_obstack (gdbarch), | |
46 | name, type); | |
47 | } | |
48 | ||
49 | /* A container holding all the register groups for a particular | |
50 | architecture. */ | |
51 | ||
52 | struct reggroups | |
53 | { | |
54 | reggroups () | |
55 | { | |
56 | /* Add the default groups. */ | |
57 | add (general_reggroup); | |
58 | add (float_reggroup); | |
59 | add (system_reggroup); | |
60 | add (vector_reggroup); | |
61 | add (all_reggroup); | |
62 | add (save_reggroup); | |
63 | add (restore_reggroup); | |
64 | } | |
65 | ||
66 | DISABLE_COPY_AND_ASSIGN (reggroups); | |
67 | ||
68 | /* Add GROUP to the list of register groups. */ | |
69 | ||
70 | void add (const reggroup *group) | |
71 | { | |
72 | gdb_assert (group != nullptr); | |
73 | ||
74 | auto find_by_name = [group] (const reggroup *g) | |
75 | { | |
76 | return streq (group->name (), g->name ()); | |
77 | }; | |
78 | gdb_assert (std::find_if (m_groups.begin (), m_groups.end (), find_by_name) | |
79 | == m_groups.end ()); | |
80 | ||
81 | m_groups.push_back (group); | |
82 | } | |
83 | ||
84 | /* The number of register groups. */ | |
85 | ||
86 | std::vector<struct reggroup *>::size_type | |
87 | size () const | |
88 | { | |
89 | return m_groups.size (); | |
90 | } | |
91 | ||
92 | /* Return a reference to the list of all groups. */ | |
93 | ||
94 | const std::vector<const struct reggroup *> & | |
95 | groups () const | |
96 | { | |
97 | return m_groups; | |
98 | } | |
99 | ||
100 | private: | |
101 | /* The register groups. */ | |
102 | std::vector<const struct reggroup *> m_groups; | |
103 | }; | |
104 | ||
105 | /* Key used to lookup register group data from a gdbarch. */ | |
106 | ||
107 | static const registry<gdbarch>::key<reggroups> reggroups_data; | |
108 | ||
109 | /* Get the reggroups for the architecture, creating if necessary. */ | |
110 | ||
111 | static reggroups * | |
112 | get_reggroups (struct gdbarch *gdbarch) | |
113 | { | |
114 | struct reggroups *groups = reggroups_data.get (gdbarch); | |
115 | if (groups == nullptr) | |
116 | groups = reggroups_data.emplace (gdbarch); | |
117 | return groups; | |
118 | } | |
119 | ||
120 | /* See reggroups.h. */ | |
121 | ||
122 | void | |
123 | reggroup_add (struct gdbarch *gdbarch, const reggroup *group) | |
124 | { | |
125 | struct reggroups *groups = get_reggroups (gdbarch); | |
126 | ||
127 | gdb_assert (groups != nullptr); | |
128 | gdb_assert (group != nullptr); | |
129 | ||
130 | groups->add (group); | |
131 | } | |
132 | ||
133 | /* See reggroups.h. */ | |
134 | const std::vector<const reggroup *> & | |
135 | gdbarch_reggroups (struct gdbarch *gdbarch) | |
136 | { | |
137 | struct reggroups *groups = get_reggroups (gdbarch); | |
138 | gdb_assert (groups != nullptr); | |
139 | gdb_assert (groups->size () > 0); | |
140 | return groups->groups (); | |
141 | } | |
142 | ||
143 | /* See reggroups.h. */ | |
144 | ||
145 | int | |
146 | default_register_reggroup_p (struct gdbarch *gdbarch, int regnum, | |
147 | const struct reggroup *group) | |
148 | { | |
149 | int vector_p; | |
150 | int float_p; | |
151 | int raw_p; | |
152 | ||
153 | if (*gdbarch_register_name (gdbarch, regnum) == '\0') | |
154 | return 0; | |
155 | if (group == all_reggroup) | |
156 | return 1; | |
157 | vector_p = register_type (gdbarch, regnum)->is_vector (); | |
158 | float_p = (register_type (gdbarch, regnum)->code () == TYPE_CODE_FLT | |
159 | || (register_type (gdbarch, regnum)->code () | |
160 | == TYPE_CODE_DECFLOAT)); | |
161 | raw_p = regnum < gdbarch_num_regs (gdbarch); | |
162 | if (group == float_reggroup) | |
163 | return float_p; | |
164 | if (group == vector_reggroup) | |
165 | return vector_p; | |
166 | if (group == general_reggroup) | |
167 | return (!vector_p && !float_p); | |
168 | if (group == save_reggroup || group == restore_reggroup) | |
169 | return raw_p; | |
170 | return 0; | |
171 | } | |
172 | ||
173 | /* See reggroups.h. */ | |
174 | ||
175 | const reggroup * | |
176 | reggroup_find (struct gdbarch *gdbarch, const char *name) | |
177 | { | |
178 | for (const struct reggroup *group : gdbarch_reggroups (gdbarch)) | |
179 | { | |
180 | if (strcmp (name, group->name ()) == 0) | |
181 | return group; | |
182 | } | |
183 | return NULL; | |
184 | } | |
185 | ||
186 | /* Dump out a table of register groups for the current architecture. */ | |
187 | ||
188 | static void | |
189 | reggroups_dump (gdbarch *gdbarch, ui_out *out) | |
190 | { | |
191 | ui_out_emit_table table (out, 2, -1, "RegGroups"); | |
192 | out->table_header (10, ui_left, "group", "Group"); | |
193 | out->table_header (10, ui_left, "type", "Type"); | |
194 | out->table_body (); | |
195 | ||
196 | for (const struct reggroup *group : gdbarch_reggroups (gdbarch)) | |
197 | { | |
198 | ui_out_emit_tuple tuple_emitter (out, nullptr); | |
199 | ||
200 | /* Group name. */ | |
201 | out->field_string ("group", group->name ()); | |
202 | ||
203 | /* Group type. */ | |
204 | const char *type; | |
205 | ||
206 | switch (group->type ()) | |
207 | { | |
208 | case USER_REGGROUP: | |
209 | type = "user"; | |
210 | break; | |
211 | case INTERNAL_REGGROUP: | |
212 | type = "internal"; | |
213 | break; | |
214 | default: | |
215 | internal_error (_("bad switch")); | |
216 | } | |
217 | ||
218 | /* Note: If you change this, be sure to also update the | |
219 | documentation. */ | |
220 | out->field_string ("type", type); | |
221 | out->text ("\n"); | |
222 | } | |
223 | } | |
224 | ||
225 | /* Implement 'maintenance print reggroups' command. */ | |
226 | ||
227 | static void | |
228 | maintenance_print_reggroups (const char *args, int from_tty) | |
229 | { | |
230 | struct gdbarch *gdbarch = get_current_arch (); | |
231 | ||
232 | if (args == NULL) | |
233 | reggroups_dump (gdbarch, current_uiout); | |
234 | else | |
235 | { | |
236 | stdio_file file; | |
237 | ||
238 | if (!file.open (args, "w")) | |
239 | perror_with_name (_("maintenance print reggroups")); | |
240 | ui_out_redirect_pop redirect (current_uiout, &file); | |
241 | reggroups_dump (gdbarch, current_uiout); | |
242 | } | |
243 | } | |
244 | ||
245 | /* Pre-defined register groups. */ | |
246 | static const reggroup general_group = { "general", USER_REGGROUP }; | |
247 | static const reggroup float_group = { "float", USER_REGGROUP }; | |
248 | static const reggroup system_group = { "system", USER_REGGROUP }; | |
249 | static const reggroup vector_group = { "vector", USER_REGGROUP }; | |
250 | static const reggroup all_group = { "all", USER_REGGROUP }; | |
251 | static const reggroup save_group = { "save", INTERNAL_REGGROUP }; | |
252 | static const reggroup restore_group = { "restore", INTERNAL_REGGROUP }; | |
253 | ||
254 | const reggroup *const general_reggroup = &general_group; | |
255 | const reggroup *const float_reggroup = &float_group; | |
256 | const reggroup *const system_reggroup = &system_group; | |
257 | const reggroup *const vector_reggroup = &vector_group; | |
258 | const reggroup *const all_reggroup = &all_group; | |
259 | const reggroup *const save_reggroup = &save_group; | |
260 | const reggroup *const restore_reggroup = &restore_group; | |
261 | ||
262 | INIT_GDB_FILE (reggroup) | |
263 | { | |
264 | add_cmd ("reggroups", class_maintenance, | |
265 | maintenance_print_reggroups, _("\ | |
266 | Print the internal register group names.\n\ | |
267 | Takes an optional file parameter."), | |
268 | &maintenanceprintlist); | |
269 | ||
270 | } |