]>
Commit | Line | Data |
---|---|---|
213516ef | 1 | /* Copyright (C) 1986-2023 Free Software Foundation, Inc. |
4c74fe6b YQ |
2 | |
3 | This file is part of GDB. | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
17 | ||
18 | #include "defs.h" | |
19 | #include "gdbcmd.h" | |
20 | #include "regcache.h" | |
268a13a5 | 21 | #include "gdbsupport/def-vector.h" |
4c74fe6b YQ |
22 | #include "valprint.h" |
23 | #include "remote.h" | |
24 | #include "reggroups.h" | |
25 | #include "target.h" | |
0d12e84c | 26 | #include "gdbarch.h" |
99d9c3b9 | 27 | #include "inferior.h" |
4c74fe6b YQ |
28 | |
29 | /* Dump registers from regcache, used for dumping raw registers and | |
30 | cooked registers. */ | |
31 | ||
32 | class register_dump_regcache : public register_dump | |
33 | { | |
34 | public: | |
35 | register_dump_regcache (regcache *regcache, bool dump_pseudo) | |
36 | : register_dump (regcache->arch ()), m_regcache (regcache), | |
37 | m_dump_pseudo (dump_pseudo) | |
38 | { | |
39 | } | |
40 | ||
41 | protected: | |
42 | void dump_reg (ui_file *file, int regnum) override | |
43 | { | |
44 | if (regnum < 0) | |
45 | { | |
46 | if (m_dump_pseudo) | |
6cb06a8c | 47 | gdb_printf (file, "Cooked value"); |
4c74fe6b | 48 | else |
6cb06a8c | 49 | gdb_printf (file, "Raw value"); |
4c74fe6b YQ |
50 | } |
51 | else | |
52 | { | |
53 | if (regnum < gdbarch_num_regs (m_gdbarch) || m_dump_pseudo) | |
54 | { | |
55 | auto size = register_size (m_gdbarch, regnum); | |
56 | ||
57 | if (size == 0) | |
58 | return; | |
59 | ||
60 | gdb::def_vector<gdb_byte> buf (size); | |
61 | auto status = m_regcache->cooked_read (regnum, buf.data ()); | |
62 | ||
63 | if (status == REG_UNKNOWN) | |
6cb06a8c | 64 | gdb_printf (file, "<invalid>"); |
4c74fe6b | 65 | else if (status == REG_UNAVAILABLE) |
6cb06a8c | 66 | gdb_printf (file, "<unavailable>"); |
4c74fe6b YQ |
67 | else |
68 | { | |
69 | print_hex_chars (file, buf.data (), size, | |
70 | gdbarch_byte_order (m_gdbarch), true); | |
71 | } | |
72 | } | |
73 | else | |
74 | { | |
75 | /* Just print "<cooked>" for pseudo register when | |
76 | regcache_dump_raw. */ | |
6cb06a8c | 77 | gdb_printf (file, "<cooked>"); |
4c74fe6b YQ |
78 | } |
79 | } | |
80 | } | |
81 | ||
82 | private: | |
83 | regcache *m_regcache; | |
84 | ||
85 | /* Dump pseudo registers or not. */ | |
86 | const bool m_dump_pseudo; | |
87 | }; | |
88 | ||
89 | /* Dump from reg_buffer, used when there is no thread or | |
90 | registers. */ | |
91 | ||
92 | class register_dump_reg_buffer : public register_dump, reg_buffer | |
93 | { | |
94 | public: | |
95 | register_dump_reg_buffer (gdbarch *gdbarch, bool dump_pseudo) | |
96 | : register_dump (gdbarch), reg_buffer (gdbarch, dump_pseudo) | |
97 | { | |
98 | } | |
99 | ||
100 | protected: | |
101 | void dump_reg (ui_file *file, int regnum) override | |
102 | { | |
103 | if (regnum < 0) | |
104 | { | |
105 | if (m_has_pseudo) | |
6cb06a8c | 106 | gdb_printf (file, "Cooked value"); |
4c74fe6b | 107 | else |
6cb06a8c | 108 | gdb_printf (file, "Raw value"); |
4c74fe6b YQ |
109 | } |
110 | else | |
111 | { | |
112 | if (regnum < gdbarch_num_regs (m_gdbarch) || m_has_pseudo) | |
113 | { | |
114 | auto size = register_size (m_gdbarch, regnum); | |
115 | ||
116 | if (size == 0) | |
117 | return; | |
118 | ||
119 | auto status = get_register_status (regnum); | |
120 | ||
121 | gdb_assert (status != REG_VALID); | |
122 | ||
123 | if (status == REG_UNKNOWN) | |
6cb06a8c | 124 | gdb_printf (file, "<invalid>"); |
4c74fe6b | 125 | else |
6cb06a8c | 126 | gdb_printf (file, "<unavailable>"); |
4c74fe6b YQ |
127 | } |
128 | else | |
129 | { | |
130 | /* Just print "<cooked>" for pseudo register when | |
131 | regcache_dump_raw. */ | |
6cb06a8c | 132 | gdb_printf (file, "<cooked>"); |
4c74fe6b YQ |
133 | } |
134 | } | |
135 | } | |
136 | }; | |
137 | ||
138 | /* For "maint print registers". */ | |
139 | ||
140 | class register_dump_none : public register_dump | |
141 | { | |
142 | public: | |
143 | register_dump_none (gdbarch *arch) | |
144 | : register_dump (arch) | |
145 | {} | |
146 | ||
147 | protected: | |
148 | void dump_reg (ui_file *file, int regnum) override | |
149 | {} | |
150 | }; | |
151 | ||
152 | /* For "maint print remote-registers". */ | |
153 | ||
154 | class register_dump_remote : public register_dump | |
155 | { | |
156 | public: | |
157 | register_dump_remote (gdbarch *arch) | |
158 | : register_dump (arch) | |
159 | {} | |
160 | ||
161 | protected: | |
162 | void dump_reg (ui_file *file, int regnum) override | |
163 | { | |
164 | if (regnum < 0) | |
165 | { | |
6cb06a8c | 166 | gdb_printf (file, "Rmt Nr g/G Offset"); |
4c74fe6b YQ |
167 | } |
168 | else if (regnum < gdbarch_num_regs (m_gdbarch)) | |
169 | { | |
170 | int pnum, poffset; | |
171 | ||
172 | if (remote_register_number_and_offset (m_gdbarch, regnum, | |
173 | &pnum, &poffset)) | |
6cb06a8c | 174 | gdb_printf (file, "%7d %11d", pnum, poffset); |
4c74fe6b YQ |
175 | } |
176 | } | |
177 | }; | |
178 | ||
179 | /* For "maint print register-groups". */ | |
180 | ||
181 | class register_dump_groups : public register_dump | |
182 | { | |
183 | public: | |
184 | register_dump_groups (gdbarch *arch) | |
185 | : register_dump (arch) | |
186 | {} | |
187 | ||
188 | protected: | |
189 | void dump_reg (ui_file *file, int regnum) override | |
190 | { | |
191 | if (regnum < 0) | |
6cb06a8c | 192 | gdb_printf (file, "Groups"); |
4c74fe6b YQ |
193 | else |
194 | { | |
195 | const char *sep = ""; | |
1bca9b1e | 196 | for (const struct reggroup *group : gdbarch_reggroups (m_gdbarch)) |
4c74fe6b YQ |
197 | { |
198 | if (gdbarch_register_reggroup_p (m_gdbarch, regnum, group)) | |
199 | { | |
af7ce09b | 200 | gdb_printf (file, "%s%s", sep, group->name ()); |
4c74fe6b YQ |
201 | sep = ","; |
202 | } | |
203 | } | |
204 | } | |
205 | } | |
206 | }; | |
207 | ||
208 | enum regcache_dump_what | |
209 | { | |
210 | regcache_dump_none, regcache_dump_raw, | |
211 | regcache_dump_cooked, regcache_dump_groups, | |
212 | regcache_dump_remote | |
213 | }; | |
214 | ||
215 | static void | |
216 | regcache_print (const char *args, enum regcache_dump_what what_to_dump) | |
217 | { | |
218 | /* Where to send output. */ | |
219 | stdio_file file; | |
220 | ui_file *out; | |
221 | ||
222 | if (args == NULL) | |
223 | out = gdb_stdout; | |
224 | else | |
225 | { | |
226 | if (!file.open (args, "w")) | |
227 | perror_with_name (_("maintenance print architecture")); | |
228 | out = &file; | |
229 | } | |
230 | ||
231 | std::unique_ptr<register_dump> dump; | |
232 | std::unique_ptr<regcache> regs; | |
233 | gdbarch *gdbarch; | |
234 | ||
9dccd06e | 235 | if (target_has_registers ()) |
4c74fe6b YQ |
236 | gdbarch = get_current_regcache ()->arch (); |
237 | else | |
99d9c3b9 | 238 | gdbarch = current_inferior ()->arch (); |
4c74fe6b YQ |
239 | |
240 | switch (what_to_dump) | |
241 | { | |
242 | case regcache_dump_none: | |
243 | dump.reset (new register_dump_none (gdbarch)); | |
244 | break; | |
245 | case regcache_dump_remote: | |
246 | dump.reset (new register_dump_remote (gdbarch)); | |
247 | break; | |
248 | case regcache_dump_groups: | |
249 | dump.reset (new register_dump_groups (gdbarch)); | |
250 | break; | |
251 | case regcache_dump_raw: | |
252 | case regcache_dump_cooked: | |
253 | { | |
254 | auto dump_pseudo = (what_to_dump == regcache_dump_cooked); | |
255 | ||
9dccd06e | 256 | if (target_has_registers ()) |
4c74fe6b YQ |
257 | dump.reset (new register_dump_regcache (get_current_regcache (), |
258 | dump_pseudo)); | |
259 | else | |
260 | { | |
261 | /* For the benefit of "maint print registers" & co when | |
262 | debugging an executable, allow dumping a regcache even when | |
263 | there is no thread selected / no registers. */ | |
99d9c3b9 | 264 | dump.reset (new register_dump_reg_buffer (gdbarch, dump_pseudo)); |
4c74fe6b YQ |
265 | } |
266 | } | |
267 | break; | |
268 | } | |
269 | ||
270 | dump->dump (out); | |
271 | } | |
272 | ||
273 | static void | |
274 | maintenance_print_registers (const char *args, int from_tty) | |
275 | { | |
276 | regcache_print (args, regcache_dump_none); | |
277 | } | |
278 | ||
279 | static void | |
280 | maintenance_print_raw_registers (const char *args, int from_tty) | |
281 | { | |
282 | regcache_print (args, regcache_dump_raw); | |
283 | } | |
284 | ||
285 | static void | |
286 | maintenance_print_cooked_registers (const char *args, int from_tty) | |
287 | { | |
288 | regcache_print (args, regcache_dump_cooked); | |
289 | } | |
290 | ||
291 | static void | |
292 | maintenance_print_register_groups (const char *args, int from_tty) | |
293 | { | |
294 | regcache_print (args, regcache_dump_groups); | |
295 | } | |
296 | ||
297 | static void | |
298 | maintenance_print_remote_registers (const char *args, int from_tty) | |
299 | { | |
300 | regcache_print (args, regcache_dump_remote); | |
301 | } | |
302 | ||
6c265988 | 303 | void _initialize_regcache_dump (); |
4c74fe6b | 304 | void |
6c265988 | 305 | _initialize_regcache_dump () |
4c74fe6b YQ |
306 | { |
307 | add_cmd ("registers", class_maintenance, maintenance_print_registers, | |
308 | _("Print the internal register configuration.\n" | |
309 | "Takes an optional file parameter."), &maintenanceprintlist); | |
310 | add_cmd ("raw-registers", class_maintenance, | |
311 | maintenance_print_raw_registers, | |
312 | _("Print the internal register configuration " | |
313 | "including raw values.\n" | |
314 | "Takes an optional file parameter."), &maintenanceprintlist); | |
315 | add_cmd ("cooked-registers", class_maintenance, | |
316 | maintenance_print_cooked_registers, | |
317 | _("Print the internal register configuration " | |
318 | "including cooked values.\n" | |
319 | "Takes an optional file parameter."), &maintenanceprintlist); | |
320 | add_cmd ("register-groups", class_maintenance, | |
321 | maintenance_print_register_groups, | |
322 | _("Print the internal register configuration " | |
323 | "including each register's group.\n" | |
324 | "Takes an optional file parameter."), | |
325 | &maintenanceprintlist); | |
326 | add_cmd ("remote-registers", class_maintenance, | |
327 | maintenance_print_remote_registers, _("\ | |
590042fc PW |
328 | Print the internal register configuration including remote register number " |
329 | "and g/G packets offset.\n\ | |
4c74fe6b YQ |
330 | Takes an optional file parameter."), |
331 | &maintenanceprintlist); | |
332 | } |