gdb: Don't reorder line table entries too much when sorting.
[deliverable/binutils-gdb.git] / gdb / regcache-dump.c
1 /* Copyright (C) 1986-2020 Free Software Foundation, Inc.
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"
21 #include "gdbsupport/def-vector.h"
22 #include "valprint.h"
23 #include "remote.h"
24 #include "reggroups.h"
25 #include "target.h"
26 #include "gdbarch.h"
27
28 /* Dump registers from regcache, used for dumping raw registers and
29 cooked registers. */
30
31 class register_dump_regcache : public register_dump
32 {
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)
37 {
38 }
39
40 protected:
41 void dump_reg (ui_file *file, int regnum) override
42 {
43 if (regnum < 0)
44 {
45 if (m_dump_pseudo)
46 fprintf_unfiltered (file, "Cooked value");
47 else
48 fprintf_unfiltered (file, "Raw value");
49 }
50 else
51 {
52 if (regnum < gdbarch_num_regs (m_gdbarch) || m_dump_pseudo)
53 {
54 auto size = register_size (m_gdbarch, regnum);
55
56 if (size == 0)
57 return;
58
59 gdb::def_vector<gdb_byte> buf (size);
60 auto status = m_regcache->cooked_read (regnum, buf.data ());
61
62 if (status == REG_UNKNOWN)
63 fprintf_unfiltered (file, "<invalid>");
64 else if (status == REG_UNAVAILABLE)
65 fprintf_unfiltered (file, "<unavailable>");
66 else
67 {
68 print_hex_chars (file, buf.data (), size,
69 gdbarch_byte_order (m_gdbarch), true);
70 }
71 }
72 else
73 {
74 /* Just print "<cooked>" for pseudo register when
75 regcache_dump_raw. */
76 fprintf_unfiltered (file, "<cooked>");
77 }
78 }
79 }
80
81 private:
82 regcache *m_regcache;
83
84 /* Dump pseudo registers or not. */
85 const bool m_dump_pseudo;
86 };
87
88 /* Dump from reg_buffer, used when there is no thread or
89 registers. */
90
91 class register_dump_reg_buffer : public register_dump, reg_buffer
92 {
93 public:
94 register_dump_reg_buffer (gdbarch *gdbarch, bool dump_pseudo)
95 : register_dump (gdbarch), reg_buffer (gdbarch, dump_pseudo)
96 {
97 }
98
99 protected:
100 void dump_reg (ui_file *file, int regnum) override
101 {
102 if (regnum < 0)
103 {
104 if (m_has_pseudo)
105 fprintf_unfiltered (file, "Cooked value");
106 else
107 fprintf_unfiltered (file, "Raw value");
108 }
109 else
110 {
111 if (regnum < gdbarch_num_regs (m_gdbarch) || m_has_pseudo)
112 {
113 auto size = register_size (m_gdbarch, regnum);
114
115 if (size == 0)
116 return;
117
118 auto status = get_register_status (regnum);
119
120 gdb_assert (status != REG_VALID);
121
122 if (status == REG_UNKNOWN)
123 fprintf_unfiltered (file, "<invalid>");
124 else
125 fprintf_unfiltered (file, "<unavailable>");
126 }
127 else
128 {
129 /* Just print "<cooked>" for pseudo register when
130 regcache_dump_raw. */
131 fprintf_unfiltered (file, "<cooked>");
132 }
133 }
134 }
135 };
136
137 /* For "maint print registers". */
138
139 class register_dump_none : public register_dump
140 {
141 public:
142 register_dump_none (gdbarch *arch)
143 : register_dump (arch)
144 {}
145
146 protected:
147 void dump_reg (ui_file *file, int regnum) override
148 {}
149 };
150
151 /* For "maint print remote-registers". */
152
153 class register_dump_remote : public register_dump
154 {
155 public:
156 register_dump_remote (gdbarch *arch)
157 : register_dump (arch)
158 {}
159
160 protected:
161 void dump_reg (ui_file *file, int regnum) override
162 {
163 if (regnum < 0)
164 {
165 fprintf_unfiltered (file, "Rmt Nr g/G Offset");
166 }
167 else if (regnum < gdbarch_num_regs (m_gdbarch))
168 {
169 int pnum, poffset;
170
171 if (remote_register_number_and_offset (m_gdbarch, regnum,
172 &pnum, &poffset))
173 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
174 }
175 }
176 };
177
178 /* For "maint print register-groups". */
179
180 class register_dump_groups : public register_dump
181 {
182 public:
183 register_dump_groups (gdbarch *arch)
184 : register_dump (arch)
185 {}
186
187 protected:
188 void dump_reg (ui_file *file, int regnum) override
189 {
190 if (regnum < 0)
191 fprintf_unfiltered (file, "Groups");
192 else
193 {
194 const char *sep = "";
195 struct reggroup *group;
196
197 for (group = reggroup_next (m_gdbarch, NULL);
198 group != NULL;
199 group = reggroup_next (m_gdbarch, group))
200 {
201 if (gdbarch_register_reggroup_p (m_gdbarch, regnum, group))
202 {
203 fprintf_unfiltered (file,
204 "%s%s", sep, reggroup_name (group));
205 sep = ",";
206 }
207 }
208 }
209 }
210 };
211
212 enum regcache_dump_what
213 {
214 regcache_dump_none, regcache_dump_raw,
215 regcache_dump_cooked, regcache_dump_groups,
216 regcache_dump_remote
217 };
218
219 static void
220 regcache_print (const char *args, enum regcache_dump_what what_to_dump)
221 {
222 /* Where to send output. */
223 stdio_file file;
224 ui_file *out;
225
226 if (args == NULL)
227 out = gdb_stdout;
228 else
229 {
230 if (!file.open (args, "w"))
231 perror_with_name (_("maintenance print architecture"));
232 out = &file;
233 }
234
235 std::unique_ptr<register_dump> dump;
236 std::unique_ptr<regcache> regs;
237 gdbarch *gdbarch;
238
239 if (target_has_registers)
240 gdbarch = get_current_regcache ()->arch ();
241 else
242 gdbarch = target_gdbarch ();
243
244 switch (what_to_dump)
245 {
246 case regcache_dump_none:
247 dump.reset (new register_dump_none (gdbarch));
248 break;
249 case regcache_dump_remote:
250 dump.reset (new register_dump_remote (gdbarch));
251 break;
252 case regcache_dump_groups:
253 dump.reset (new register_dump_groups (gdbarch));
254 break;
255 case regcache_dump_raw:
256 case regcache_dump_cooked:
257 {
258 auto dump_pseudo = (what_to_dump == regcache_dump_cooked);
259
260 if (target_has_registers)
261 dump.reset (new register_dump_regcache (get_current_regcache (),
262 dump_pseudo));
263 else
264 {
265 /* For the benefit of "maint print registers" & co when
266 debugging an executable, allow dumping a regcache even when
267 there is no thread selected / no registers. */
268 dump.reset (new register_dump_reg_buffer (target_gdbarch (),
269 dump_pseudo));
270 }
271 }
272 break;
273 }
274
275 dump->dump (out);
276 }
277
278 static void
279 maintenance_print_registers (const char *args, int from_tty)
280 {
281 regcache_print (args, regcache_dump_none);
282 }
283
284 static void
285 maintenance_print_raw_registers (const char *args, int from_tty)
286 {
287 regcache_print (args, regcache_dump_raw);
288 }
289
290 static void
291 maintenance_print_cooked_registers (const char *args, int from_tty)
292 {
293 regcache_print (args, regcache_dump_cooked);
294 }
295
296 static void
297 maintenance_print_register_groups (const char *args, int from_tty)
298 {
299 regcache_print (args, regcache_dump_groups);
300 }
301
302 static void
303 maintenance_print_remote_registers (const char *args, int from_tty)
304 {
305 regcache_print (args, regcache_dump_remote);
306 }
307
308 void _initialize_regcache_dump ();
309 void
310 _initialize_regcache_dump ()
311 {
312 add_cmd ("registers", class_maintenance, maintenance_print_registers,
313 _("Print the internal register configuration.\n"
314 "Takes an optional file parameter."), &maintenanceprintlist);
315 add_cmd ("raw-registers", class_maintenance,
316 maintenance_print_raw_registers,
317 _("Print the internal register configuration "
318 "including raw values.\n"
319 "Takes an optional file parameter."), &maintenanceprintlist);
320 add_cmd ("cooked-registers", class_maintenance,
321 maintenance_print_cooked_registers,
322 _("Print the internal register configuration "
323 "including cooked values.\n"
324 "Takes an optional file parameter."), &maintenanceprintlist);
325 add_cmd ("register-groups", class_maintenance,
326 maintenance_print_register_groups,
327 _("Print the internal register configuration "
328 "including each register's group.\n"
329 "Takes an optional file parameter."),
330 &maintenanceprintlist);
331 add_cmd ("remote-registers", class_maintenance,
332 maintenance_print_remote_registers, _("\
333 Print the internal register configuration including remote register number "
334 "and g/G packets offset.\n\
335 Takes an optional file parameter."),
336 &maintenanceprintlist);
337 }
This page took 0.0481 seconds and 4 git commands to generate.