1 /* Copyright (C) 1986-2024 Free Software Foundation, Inc.
3 This file is part of GDB.
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.
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.
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/>. */
18 #include "cli/cli-cmds.h"
20 #include "gdbsupport/def-vector.h"
23 #include "reggroups.h"
28 /* Dump registers from regcache, used for dumping raw registers and
31 class register_dump_regcache
: public register_dump
34 register_dump_regcache (regcache
*regcache
, bool dump_pseudo
)
35 : register_dump (regcache
->arch ()), m_regcache (regcache
),
36 m_dump_pseudo (dump_pseudo
)
42 int num_additional_headers () override
45 void additional_headers (ui_out
*out
) override
47 out
->table_header (0, ui_left
, "value",
48 m_dump_pseudo
? "Cooked value" : "Raw value");
51 void dump_reg (ui_out
*out
, int regnum
) override
53 if (regnum
< gdbarch_num_regs (m_gdbarch
) || m_dump_pseudo
)
55 auto size
= register_size (m_gdbarch
, regnum
);
60 gdb::byte_vector
buf (size
);
61 auto status
= m_regcache
->cooked_read (regnum
, buf
.data ());
63 if (status
== REG_UNKNOWN
)
64 out
->field_string ("value", "<invalid>");
65 else if (status
== REG_UNAVAILABLE
)
66 out
->field_string ("value", "<unavailable>");
70 print_hex_chars (&str
, buf
.data (), size
,
71 gdbarch_byte_order (m_gdbarch
), true);
72 out
->field_stream ("value", str
);
77 /* Just print "<cooked>" for pseudo register when
79 out
->field_string ("value", "<cooked>");
86 /* Dump pseudo registers or not. */
87 const bool m_dump_pseudo
;
90 /* Dump from reg_buffer, used when there is no thread or
93 class register_dump_reg_buffer
: public register_dump
, reg_buffer
96 register_dump_reg_buffer (gdbarch
*gdbarch
, bool dump_pseudo
)
97 : register_dump (gdbarch
), reg_buffer (gdbarch
, dump_pseudo
)
103 int num_additional_headers () override
106 void additional_headers (ui_out
*out
) override
108 out
->table_header (0, ui_left
, "value",
109 m_has_pseudo
? "Cooked value" : "Raw value");
112 void dump_reg (ui_out
*out
, int regnum
) override
114 if (regnum
< gdbarch_num_regs (m_gdbarch
) || m_has_pseudo
)
116 auto size
= register_size (regnum
);
121 auto status
= get_register_status (regnum
);
123 gdb_assert (status
!= REG_VALID
);
125 if (status
== REG_UNKNOWN
)
126 out
->field_string ("value", "<invalid>");
128 out
->field_string ("value", "<unavailable>");
132 /* Just print "<cooked>" for pseudo register when
133 regcache_dump_raw. */
134 out
->field_string ("value", "<cooked>");
139 /* For "maint print registers". */
141 class register_dump_none
: public register_dump
144 register_dump_none (gdbarch
*arch
)
145 : register_dump (arch
)
150 int num_additional_headers () override
153 void additional_headers (ui_out
*out
) override
156 void dump_reg (ui_out
*out
, int regnum
) override
160 /* For "maint print remote-registers". */
162 class register_dump_remote
: public register_dump
165 register_dump_remote (gdbarch
*arch
)
166 : register_dump (arch
)
171 int num_additional_headers () override
174 void additional_headers (ui_out
*out
) override
176 out
->table_header (7, ui_left
, "remnum", "Rmt Nr");
177 out
->table_header (11, ui_left
, "goffset", "g/G Offset");
178 out
->table_header (3, ui_left
, "expedited", "Expedited");
181 void dump_reg (ui_out
*out
, int regnum
) override
185 if (regnum
< gdbarch_num_regs (m_gdbarch
)
186 && remote_register_number_and_offset (m_gdbarch
, regnum
,
189 out
->field_signed ("remnum", pnum
);
190 out
->field_signed ("goffset", poffset
);
192 if (remote_register_is_expedited (regnum
))
193 out
->field_string ("expedited", "yes");
195 out
->field_skip ("expedited");
199 out
->field_skip ("remnum");
200 out
->field_skip ("goffset");
201 out
->field_skip ("expedited");
206 /* For "maint print register-groups". */
208 class register_dump_groups
: public register_dump
211 register_dump_groups (gdbarch
*arch
)
212 : register_dump (arch
)
217 int num_additional_headers () override
220 void additional_headers (ui_out
*out
) override
222 out
->table_header (0, ui_left
, "groups", "Groups");
225 void dump_reg (ui_out
*out
, int regnum
) override
228 const char *sep
= "";
229 for (const struct reggroup
*group
: gdbarch_reggroups (m_gdbarch
))
231 if (gdbarch_register_reggroup_p (m_gdbarch
, regnum
, group
))
233 gdb_printf (&file
, "%s%s", sep
, group
->name ());
237 out
->field_stream ("groups", file
);
241 enum regcache_dump_what
243 regcache_dump_none
, regcache_dump_raw
,
244 regcache_dump_cooked
, regcache_dump_groups
,
248 /* Helper for the various maint commands that print registers. ARGS
249 is the arguments passed to the command. WHAT_TO_DUMP indicates
250 exactly which registers to display. COMMAND is the command name,
251 used in error messages. */
254 regcache_print (const char *args
, enum regcache_dump_what what_to_dump
,
257 /* Where to send output. */
259 std::optional
<ui_out_redirect_pop
> redirect
;
263 if (!file
.open (args
, "w"))
264 perror_with_name (command
);
265 redirect
.emplace (current_uiout
, &file
);
268 std::unique_ptr
<register_dump
> dump
;
269 std::unique_ptr
<regcache
> regs
;
272 if (target_has_registers ())
273 gdbarch
= get_thread_regcache (inferior_thread ())->arch ();
275 gdbarch
= current_inferior ()->arch ();
278 switch (what_to_dump
)
280 case regcache_dump_none
:
281 dump
= std::make_unique
<register_dump_none
> (gdbarch
);
284 case regcache_dump_remote
:
285 dump
= std::make_unique
<register_dump_remote
> (gdbarch
);
286 name
= "RegisterRemote";
288 case regcache_dump_groups
:
289 dump
= std::make_unique
<register_dump_groups
> (gdbarch
);
290 name
= "RegisterGroups";
292 case regcache_dump_raw
:
293 case regcache_dump_cooked
:
295 name
= "RegisterDump";
296 auto dump_pseudo
= (what_to_dump
== regcache_dump_cooked
);
298 if (target_has_registers ())
299 dump
= (std::make_unique
<register_dump_regcache
>
300 (get_thread_regcache (inferior_thread ()), dump_pseudo
));
303 /* For the benefit of "maint print registers" & co when
304 debugging an executable, allow dumping a regcache even when
305 there is no thread selected / no registers. */
306 dump
= std::make_unique
<register_dump_reg_buffer
> (gdbarch
,
313 dump
->dump (current_uiout
, name
);
317 maintenance_print_registers (const char *args
, int from_tty
)
319 regcache_print (args
, regcache_dump_none
, "maintenance print registers");
323 maintenance_print_raw_registers (const char *args
, int from_tty
)
325 regcache_print (args
, regcache_dump_raw
, "maintenance print raw-registers");
329 maintenance_print_cooked_registers (const char *args
, int from_tty
)
331 regcache_print (args
, regcache_dump_cooked
,
332 "maintenance print cooked-registers");
336 maintenance_print_register_groups (const char *args
, int from_tty
)
338 regcache_print (args
, regcache_dump_groups
,
339 "maintenance print register-groups");
343 maintenance_print_remote_registers (const char *args
, int from_tty
)
345 regcache_print (args
, regcache_dump_remote
,
346 "maintenance print remote-registers");
349 void _initialize_regcache_dump ();
351 _initialize_regcache_dump ()
353 add_cmd ("registers", class_maintenance
, maintenance_print_registers
,
354 _("Print the internal register configuration.\n"
355 "Takes an optional file parameter."), &maintenanceprintlist
);
356 add_cmd ("raw-registers", class_maintenance
,
357 maintenance_print_raw_registers
,
358 _("Print the internal register configuration "
359 "including raw values.\n"
360 "Takes an optional file parameter."), &maintenanceprintlist
);
361 add_cmd ("cooked-registers", class_maintenance
,
362 maintenance_print_cooked_registers
,
363 _("Print the internal register configuration "
364 "including cooked values.\n"
365 "Takes an optional file parameter."), &maintenanceprintlist
);
366 add_cmd ("register-groups", class_maintenance
,
367 maintenance_print_register_groups
,
368 _("Print the internal register configuration "
369 "including each register's group.\n"
370 "Takes an optional file parameter."),
371 &maintenanceprintlist
);
372 add_cmd ("remote-registers", class_maintenance
,
373 maintenance_print_remote_registers
, _("\
374 Print the internal register configuration.\n\
375 Usage: maintenance print remote-registers [FILE]\n\
376 The remote register number and g/G packets offset are included,\n\
377 as well as which registers were sent in the last stop reply packet\n\
378 (i.e., expedited)."),
379 &maintenanceprintlist
);