Add translations for various sub-directories
[binutils-gdb.git] / gdb / regcache-dump.c
blob2719c924ba63d4ef89b1c9715f05878ead436a8c
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"
19 #include "regcache.h"
20 #include "gdbsupport/def-vector.h"
21 #include "valprint.h"
22 #include "remote.h"
23 #include "reggroups.h"
24 #include "target.h"
25 #include "gdbarch.h"
26 #include "inferior.h"
28 /* Dump registers from regcache, used for dumping raw registers and
29 cooked registers. */
31 class register_dump_regcache : public register_dump
33 public:
34 register_dump_regcache (regcache *regcache, bool dump_pseudo)
35 : register_dump (regcache->arch ()), m_regcache (regcache),
36 m_dump_pseudo (dump_pseudo)
40 protected:
42 int num_additional_headers () override
43 { return 1; }
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);
57 if (size == 0)
58 return;
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>");
67 else
69 string_file str;
70 print_hex_chars (&str, buf.data (), size,
71 gdbarch_byte_order (m_gdbarch), true);
72 out->field_stream ("value", str);
75 else
77 /* Just print "<cooked>" for pseudo register when
78 regcache_dump_raw. */
79 out->field_string ("value", "<cooked>");
83 private:
84 regcache *m_regcache;
86 /* Dump pseudo registers or not. */
87 const bool m_dump_pseudo;
90 /* Dump from reg_buffer, used when there is no thread or
91 registers. */
93 class register_dump_reg_buffer : public register_dump, reg_buffer
95 public:
96 register_dump_reg_buffer (gdbarch *gdbarch, bool dump_pseudo)
97 : register_dump (gdbarch), reg_buffer (gdbarch, dump_pseudo)
101 protected:
103 int num_additional_headers () override
104 { return 1; }
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);
118 if (size == 0)
119 return;
121 auto status = get_register_status (regnum);
123 gdb_assert (status != REG_VALID);
125 if (status == REG_UNKNOWN)
126 out->field_string ("value", "<invalid>");
127 else
128 out->field_string ("value", "<unavailable>");
130 else
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
143 public:
144 register_dump_none (gdbarch *arch)
145 : register_dump (arch)
148 protected:
150 int num_additional_headers () override
151 { return 0; }
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
164 public:
165 register_dump_remote (gdbarch *arch)
166 : register_dump (arch)
169 protected:
171 int num_additional_headers () override
172 { return 3; }
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
183 int pnum, poffset;
185 if (regnum < gdbarch_num_regs (m_gdbarch)
186 && remote_register_number_and_offset (m_gdbarch, regnum,
187 &pnum, &poffset))
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");
194 else
195 out->field_skip ("expedited");
197 else
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
210 public:
211 register_dump_groups (gdbarch *arch)
212 : register_dump (arch)
215 protected:
217 int num_additional_headers () override
218 { return 1; }
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
227 string_file file;
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 ());
234 sep = ",";
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,
245 regcache_dump_remote
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. */
253 static void
254 regcache_print (const char *args, enum regcache_dump_what what_to_dump,
255 const char *command)
257 /* Where to send output. */
258 stdio_file file;
259 std::optional<ui_out_redirect_pop> redirect;
261 if (args != nullptr)
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;
270 gdbarch *gdbarch;
272 if (target_has_registers ())
273 gdbarch = get_thread_regcache (inferior_thread ())->arch ();
274 else
275 gdbarch = current_inferior ()->arch ();
277 const char *name;
278 switch (what_to_dump)
280 case regcache_dump_none:
281 dump = std::make_unique<register_dump_none> (gdbarch);
282 name = "Registers";
283 break;
284 case regcache_dump_remote:
285 dump = std::make_unique<register_dump_remote> (gdbarch);
286 name = "RegisterRemote";
287 break;
288 case regcache_dump_groups:
289 dump = std::make_unique<register_dump_groups> (gdbarch);
290 name = "RegisterGroups";
291 break;
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));
301 else
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,
307 dump_pseudo);
310 break;
313 dump->dump (current_uiout, name);
316 static void
317 maintenance_print_registers (const char *args, int from_tty)
319 regcache_print (args, regcache_dump_none, "maintenance print registers");
322 static void
323 maintenance_print_raw_registers (const char *args, int from_tty)
325 regcache_print (args, regcache_dump_raw, "maintenance print raw-registers");
328 static void
329 maintenance_print_cooked_registers (const char *args, int from_tty)
331 regcache_print (args, regcache_dump_cooked,
332 "maintenance print cooked-registers");
335 static void
336 maintenance_print_register_groups (const char *args, int from_tty)
338 regcache_print (args, regcache_dump_groups,
339 "maintenance print register-groups");
342 static void
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 ();
350 void
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);