Commit | Line | Data |
---|---|---|
32178cab | 1 | /* Cache and manage the values of registers for GDB, the GNU debugger. |
3fadccb3 AC |
2 | |
3 | Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, | |
9564ee9f | 4 | 2001, 2002, 2004 Free Software Foundation, Inc. |
32178cab MS |
5 | |
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | #include "defs.h" | |
32178cab MS |
24 | #include "inferior.h" |
25 | #include "target.h" | |
26 | #include "gdbarch.h" | |
705152c5 | 27 | #include "gdbcmd.h" |
4e052eda | 28 | #include "regcache.h" |
b59ff9d5 | 29 | #include "reggroups.h" |
61a0eb5b | 30 | #include "gdb_assert.h" |
b66d6d2e | 31 | #include "gdb_string.h" |
af030b9a | 32 | #include "gdbcmd.h" /* For maintenanceprintlist. */ |
32178cab MS |
33 | |
34 | /* | |
35 | * DATA STRUCTURE | |
36 | * | |
37 | * Here is the actual register cache. | |
38 | */ | |
39 | ||
3fadccb3 AC |
40 | /* Per-architecture object describing the layout of a register cache. |
41 | Computed once when the architecture is created */ | |
42 | ||
43 | struct gdbarch_data *regcache_descr_handle; | |
44 | ||
45 | struct regcache_descr | |
46 | { | |
47 | /* The architecture this descriptor belongs to. */ | |
48 | struct gdbarch *gdbarch; | |
49 | ||
50 | /* Is this a ``legacy'' register cache? Such caches reserve space | |
51 | for raw and pseudo registers and allow access to both. */ | |
52 | int legacy_p; | |
53 | ||
bb1db049 AC |
54 | /* The raw register cache. Each raw (or hard) register is supplied |
55 | by the target interface. The raw cache should not contain | |
56 | redundant information - if the PC is constructed from two | |
57 | registers then those regigisters and not the PC lives in the raw | |
58 | cache. */ | |
3fadccb3 AC |
59 | int nr_raw_registers; |
60 | long sizeof_raw_registers; | |
61 | long sizeof_raw_register_valid_p; | |
62 | ||
d138e37a AC |
63 | /* The cooked register space. Each cooked register in the range |
64 | [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw | |
65 | register. The remaining [NR_RAW_REGISTERS | |
66 | .. NR_COOKED_REGISTERS) (a.k.a. pseudo regiters) are mapped onto | |
67 | both raw registers and memory by the architecture methods | |
68 | gdbarch_register_read and gdbarch_register_write. */ | |
69 | int nr_cooked_registers; | |
067df2e5 AC |
70 | long sizeof_cooked_registers; |
71 | long sizeof_cooked_register_valid_p; | |
d138e37a AC |
72 | |
73 | /* Offset and size (in 8 bit bytes), of reach register in the | |
74 | register cache. All registers (including those in the range | |
75 | [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset. | |
76 | Assigning all registers an offset makes it possible to keep | |
77 | legacy code, such as that found in read_register_bytes() and | |
78 | write_register_bytes() working. */ | |
3fadccb3 | 79 | long *register_offset; |
3fadccb3 | 80 | long *sizeof_register; |
3fadccb3 | 81 | |
bb425013 AC |
82 | /* Cached table containing the type of each register. */ |
83 | struct type **register_type; | |
3fadccb3 AC |
84 | }; |
85 | ||
b9362cc7 | 86 | static void |
bb425013 AC |
87 | init_legacy_regcache_descr (struct gdbarch *gdbarch, |
88 | struct regcache_descr *descr) | |
3fadccb3 AC |
89 | { |
90 | int i; | |
3fadccb3 AC |
91 | /* FIXME: cagney/2002-05-11: gdbarch_data() should take that |
92 | ``gdbarch'' as a parameter. */ | |
93 | gdb_assert (gdbarch != NULL); | |
94 | ||
067df2e5 | 95 | /* Compute the offset of each register. Legacy architectures define |
62700349 AC |
96 | DEPRECATED_REGISTER_BYTE() so use that. */ |
97 | /* FIXME: cagney/2002-11-07: Instead of using | |
98 | DEPRECATED_REGISTER_BYTE() this code should, as is done in | |
99 | init_regcache_descr(), compute the offets at runtime. This | |
100 | currently isn't possible as some ISAs define overlapping register | |
101 | regions - see the mess in read_register_bytes() and | |
102 | write_register_bytes() registers. */ | |
116f06ea AC |
103 | descr->sizeof_register |
104 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
105 | descr->register_offset | |
106 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
d138e37a | 107 | for (i = 0; i < descr->nr_cooked_registers; i++) |
3fadccb3 | 108 | { |
067df2e5 | 109 | /* FIXME: cagney/2001-12-04: This code shouldn't need to use |
ce2826aa | 110 | DEPRECATED_REGISTER_BYTE(). Unfortunately, legacy code likes |
62700349 AC |
111 | to lay the buffer out so that certain registers just happen |
112 | to overlap. Ulgh! New targets use gdbarch's register | |
113 | read/write and entirely avoid this uglyness. */ | |
114 | descr->register_offset[i] = DEPRECATED_REGISTER_BYTE (i); | |
12c266ea AC |
115 | descr->sizeof_register[i] = DEPRECATED_REGISTER_RAW_SIZE (i); |
116 | gdb_assert (MAX_REGISTER_SIZE >= DEPRECATED_REGISTER_RAW_SIZE (i)); | |
f30992d4 | 117 | gdb_assert (MAX_REGISTER_SIZE >= DEPRECATED_REGISTER_VIRTUAL_SIZE (i)); |
3fadccb3 AC |
118 | } |
119 | ||
067df2e5 | 120 | /* Compute the real size of the register buffer. Start out by |
b8b527c5 AC |
121 | trusting DEPRECATED_REGISTER_BYTES, but then adjust it upwards |
122 | should that be found to not be sufficient. */ | |
123 | /* FIXME: cagney/2002-11-05: Instead of using the macro | |
124 | DEPRECATED_REGISTER_BYTES, this code should, as is done in | |
125 | init_regcache_descr(), compute the total number of register bytes | |
126 | using the accumulated offsets. */ | |
127 | descr->sizeof_cooked_registers = DEPRECATED_REGISTER_BYTES; /* OK */ | |
d138e37a | 128 | for (i = 0; i < descr->nr_cooked_registers; i++) |
3fadccb3 AC |
129 | { |
130 | long regend; | |
131 | /* Keep extending the buffer so that there is always enough | |
132 | space for all registers. The comparison is necessary since | |
133 | legacy code is free to put registers in random places in the | |
62700349 AC |
134 | buffer separated by holes. Once DEPRECATED_REGISTER_BYTE() |
135 | is killed this can be greatly simplified. */ | |
3fadccb3 | 136 | regend = descr->register_offset[i] + descr->sizeof_register[i]; |
067df2e5 AC |
137 | if (descr->sizeof_cooked_registers < regend) |
138 | descr->sizeof_cooked_registers = regend; | |
3fadccb3 | 139 | } |
067df2e5 | 140 | /* FIXME: cagney/2002-05-11: Shouldn't be including pseudo-registers |
ce2826aa | 141 | in the register cache. Unfortunately some architectures still |
067df2e5 AC |
142 | rely on this and the pseudo_register_write() method. */ |
143 | descr->sizeof_raw_registers = descr->sizeof_cooked_registers; | |
3fadccb3 AC |
144 | } |
145 | ||
146 | static void * | |
147 | init_regcache_descr (struct gdbarch *gdbarch) | |
148 | { | |
149 | int i; | |
150 | struct regcache_descr *descr; | |
151 | gdb_assert (gdbarch != NULL); | |
152 | ||
bb425013 | 153 | /* Create an initial, zero filled, table. */ |
116f06ea | 154 | descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr); |
3fadccb3 | 155 | descr->gdbarch = gdbarch; |
3fadccb3 | 156 | |
d138e37a AC |
157 | /* Total size of the register space. The raw registers are mapped |
158 | directly onto the raw register cache while the pseudo's are | |
3fadccb3 | 159 | either mapped onto raw-registers or memory. */ |
d138e37a | 160 | descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS; |
067df2e5 | 161 | descr->sizeof_cooked_register_valid_p = NUM_REGS + NUM_PSEUDO_REGS; |
3fadccb3 | 162 | |
bb425013 | 163 | /* Fill in a table of register types. */ |
116f06ea AC |
164 | descr->register_type |
165 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, struct type *); | |
bb425013 AC |
166 | for (i = 0; i < descr->nr_cooked_registers; i++) |
167 | { | |
35cac7cf AC |
168 | if (gdbarch_register_type_p (gdbarch)) |
169 | { | |
2e092625 | 170 | gdb_assert (!DEPRECATED_REGISTER_VIRTUAL_TYPE_P ()); /* OK */ |
35cac7cf AC |
171 | descr->register_type[i] = gdbarch_register_type (gdbarch, i); |
172 | } | |
173 | else | |
2e092625 | 174 | descr->register_type[i] = DEPRECATED_REGISTER_VIRTUAL_TYPE (i); /* OK */ |
bb425013 AC |
175 | } |
176 | ||
bb1db049 AC |
177 | /* Construct a strictly RAW register cache. Don't allow pseudo's |
178 | into the register cache. */ | |
179 | descr->nr_raw_registers = NUM_REGS; | |
180 | ||
181 | /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p | |
182 | array. This pretects GDB from erant code that accesses elements | |
183 | of the global register_valid_p[] array in the range [NUM_REGS | |
184 | .. NUM_REGS + NUM_PSEUDO_REGS). */ | |
185 | descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p; | |
186 | ||
bb425013 AC |
187 | /* If an old style architecture, fill in the remainder of the |
188 | register cache descriptor using the register macros. */ | |
62700349 | 189 | /* NOTE: cagney/2003-06-29: If either of DEPRECATED_REGISTER_BYTE or |
12c266ea | 190 | DEPRECATED_REGISTER_RAW_SIZE are still present, things are most likely |
dadd712e | 191 | totally screwed. Ex: an architecture with raw register sizes |
62700349 AC |
192 | smaller than what DEPRECATED_REGISTER_BYTE indicates; non |
193 | monotonic DEPRECATED_REGISTER_BYTE values. For GDB 6 check for | |
194 | these nasty methods and fall back to legacy code when present. | |
195 | Sigh! */ | |
dadd712e AC |
196 | if ((!gdbarch_pseudo_register_read_p (gdbarch) |
197 | && !gdbarch_pseudo_register_write_p (gdbarch) | |
198 | && !gdbarch_register_type_p (gdbarch)) | |
12c266ea AC |
199 | || DEPRECATED_REGISTER_BYTE_P () |
200 | || DEPRECATED_REGISTER_RAW_SIZE_P ()) | |
bb425013 AC |
201 | { |
202 | descr->legacy_p = 1; | |
203 | init_legacy_regcache_descr (gdbarch, descr); | |
204 | return descr; | |
205 | } | |
206 | ||
067df2e5 | 207 | /* Lay out the register cache. |
3fadccb3 | 208 | |
bb425013 AC |
209 | NOTE: cagney/2002-05-22: Only register_type() is used when |
210 | constructing the register cache. It is assumed that the | |
211 | register's raw size, virtual size and type length are all the | |
212 | same. */ | |
3fadccb3 AC |
213 | |
214 | { | |
215 | long offset = 0; | |
116f06ea AC |
216 | descr->sizeof_register |
217 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
218 | descr->register_offset | |
219 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
d138e37a | 220 | for (i = 0; i < descr->nr_cooked_registers; i++) |
3fadccb3 | 221 | { |
bb425013 | 222 | descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]); |
3fadccb3 AC |
223 | descr->register_offset[i] = offset; |
224 | offset += descr->sizeof_register[i]; | |
123a958e | 225 | gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]); |
3fadccb3 AC |
226 | } |
227 | /* Set the real size of the register cache buffer. */ | |
067df2e5 | 228 | descr->sizeof_cooked_registers = offset; |
3fadccb3 AC |
229 | } |
230 | ||
067df2e5 | 231 | /* FIXME: cagney/2002-05-22: Should only need to allocate space for |
ce2826aa | 232 | the raw registers. Unfortunately some code still accesses the |
067df2e5 AC |
233 | register array directly using the global registers[]. Until that |
234 | code has been purged, play safe and over allocating the register | |
235 | buffer. Ulgh! */ | |
236 | descr->sizeof_raw_registers = descr->sizeof_cooked_registers; | |
237 | ||
46654a5b | 238 | /* Sanity check. Confirm that there is agreement between the |
62700349 AC |
239 | regcache and the target's redundant DEPRECATED_REGISTER_BYTE (new |
240 | targets should not even be defining it). */ | |
d138e37a | 241 | for (i = 0; i < descr->nr_cooked_registers; i++) |
3fadccb3 | 242 | { |
62700349 AC |
243 | if (DEPRECATED_REGISTER_BYTE_P ()) |
244 | gdb_assert (descr->register_offset[i] == DEPRECATED_REGISTER_BYTE (i)); | |
46654a5b | 245 | #if 0 |
12c266ea | 246 | gdb_assert (descr->sizeof_register[i] == DEPRECATED_REGISTER_RAW_SIZE (i)); |
f30992d4 | 247 | gdb_assert (descr->sizeof_register[i] == DEPRECATED_REGISTER_VIRTUAL_SIZE (i)); |
46654a5b | 248 | #endif |
3fadccb3 | 249 | } |
b8b527c5 | 250 | /* gdb_assert (descr->sizeof_raw_registers == DEPRECATED_REGISTER_BYTES (i)); */ |
3fadccb3 AC |
251 | |
252 | return descr; | |
253 | } | |
254 | ||
255 | static struct regcache_descr * | |
256 | regcache_descr (struct gdbarch *gdbarch) | |
257 | { | |
258 | return gdbarch_data (gdbarch, regcache_descr_handle); | |
259 | } | |
260 | ||
bb425013 AC |
261 | /* Utility functions returning useful register attributes stored in |
262 | the regcache descr. */ | |
263 | ||
264 | struct type * | |
265 | register_type (struct gdbarch *gdbarch, int regnum) | |
266 | { | |
267 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
268 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); | |
269 | return descr->register_type[regnum]; | |
270 | } | |
271 | ||
0ed04cce AC |
272 | /* Utility functions returning useful register attributes stored in |
273 | the regcache descr. */ | |
274 | ||
08a617da AC |
275 | int |
276 | register_size (struct gdbarch *gdbarch, int regnum) | |
277 | { | |
278 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
279 | int size; | |
280 | gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS)); | |
281 | size = descr->sizeof_register[regnum]; | |
12c266ea | 282 | /* NB: The deprecated DEPRECATED_REGISTER_RAW_SIZE, if not provided, defaults |
96a4ee76 | 283 | to the size of the register's type. */ |
12c266ea | 284 | gdb_assert (size == DEPRECATED_REGISTER_RAW_SIZE (regnum)); /* OK */ |
96a4ee76 AC |
285 | /* NB: Don't check the register's virtual size. It, in say the case |
286 | of the MIPS, may not match the raw size! */ | |
08a617da AC |
287 | return size; |
288 | } | |
289 | ||
3fadccb3 AC |
290 | /* The register cache for storing raw register values. */ |
291 | ||
292 | struct regcache | |
293 | { | |
294 | struct regcache_descr *descr; | |
51b1fe4e AC |
295 | /* The register buffers. A read-only register cache can hold the |
296 | full [0 .. NUM_REGS + NUM_PSEUDO_REGS) while a read/write | |
297 | register cache can only hold [0 .. NUM_REGS). */ | |
298 | char *registers; | |
299 | char *register_valid_p; | |
2d28509a AC |
300 | /* Is this a read-only cache? A read-only cache is used for saving |
301 | the target's register state (e.g, across an inferior function | |
302 | call or just before forcing a function return). A read-only | |
303 | cache can only be updated via the methods regcache_dup() and | |
304 | regcache_cpy(). The actual contents are determined by the | |
305 | reggroup_save and reggroup_restore methods. */ | |
306 | int readonly_p; | |
3fadccb3 AC |
307 | }; |
308 | ||
309 | struct regcache * | |
310 | regcache_xmalloc (struct gdbarch *gdbarch) | |
311 | { | |
312 | struct regcache_descr *descr; | |
313 | struct regcache *regcache; | |
314 | gdb_assert (gdbarch != NULL); | |
315 | descr = regcache_descr (gdbarch); | |
316 | regcache = XMALLOC (struct regcache); | |
317 | regcache->descr = descr; | |
51b1fe4e | 318 | regcache->registers |
3fadccb3 | 319 | = XCALLOC (descr->sizeof_raw_registers, char); |
51b1fe4e | 320 | regcache->register_valid_p |
3fadccb3 | 321 | = XCALLOC (descr->sizeof_raw_register_valid_p, char); |
2d28509a | 322 | regcache->readonly_p = 1; |
3fadccb3 AC |
323 | return regcache; |
324 | } | |
325 | ||
326 | void | |
327 | regcache_xfree (struct regcache *regcache) | |
328 | { | |
329 | if (regcache == NULL) | |
330 | return; | |
51b1fe4e AC |
331 | xfree (regcache->registers); |
332 | xfree (regcache->register_valid_p); | |
3fadccb3 AC |
333 | xfree (regcache); |
334 | } | |
335 | ||
b9362cc7 | 336 | static void |
36160dc4 AC |
337 | do_regcache_xfree (void *data) |
338 | { | |
339 | regcache_xfree (data); | |
340 | } | |
341 | ||
342 | struct cleanup * | |
343 | make_cleanup_regcache_xfree (struct regcache *regcache) | |
344 | { | |
345 | return make_cleanup (do_regcache_xfree, regcache); | |
346 | } | |
347 | ||
41d35cb0 MK |
348 | /* Return REGCACHE's architecture. */ |
349 | ||
350 | struct gdbarch * | |
351 | get_regcache_arch (const struct regcache *regcache) | |
352 | { | |
353 | return regcache->descr->gdbarch; | |
354 | } | |
355 | ||
51b1fe4e AC |
356 | /* Return a pointer to register REGNUM's buffer cache. */ |
357 | ||
358 | static char * | |
9a661b68 | 359 | register_buffer (const struct regcache *regcache, int regnum) |
51b1fe4e AC |
360 | { |
361 | return regcache->registers + regcache->descr->register_offset[regnum]; | |
362 | } | |
363 | ||
2d28509a | 364 | void |
5602984a AC |
365 | regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read, |
366 | void *src) | |
2d28509a AC |
367 | { |
368 | struct gdbarch *gdbarch = dst->descr->gdbarch; | |
123a958e | 369 | char buf[MAX_REGISTER_SIZE]; |
2d28509a | 370 | int regnum; |
2d28509a | 371 | /* The DST should be `read-only', if it wasn't then the save would |
5602984a | 372 | end up trying to write the register values back out to the |
2d28509a | 373 | target. */ |
2d28509a AC |
374 | gdb_assert (dst->readonly_p); |
375 | /* Clear the dest. */ | |
376 | memset (dst->registers, 0, dst->descr->sizeof_cooked_registers); | |
377 | memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p); | |
378 | /* Copy over any registers (identified by their membership in the | |
5602984a AC |
379 | save_reggroup) and mark them as valid. The full [0 .. NUM_REGS + |
380 | NUM_PSEUDO_REGS) range is checked since some architectures need | |
381 | to save/restore `cooked' registers that live in memory. */ | |
2d28509a AC |
382 | for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++) |
383 | { | |
384 | if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) | |
385 | { | |
5602984a AC |
386 | int valid = cooked_read (src, regnum, buf); |
387 | if (valid) | |
388 | { | |
389 | memcpy (register_buffer (dst, regnum), buf, | |
390 | register_size (gdbarch, regnum)); | |
391 | dst->register_valid_p[regnum] = 1; | |
392 | } | |
2d28509a AC |
393 | } |
394 | } | |
395 | } | |
396 | ||
397 | void | |
5602984a AC |
398 | regcache_restore (struct regcache *dst, |
399 | regcache_cooked_read_ftype *cooked_read, | |
400 | void *src) | |
2d28509a AC |
401 | { |
402 | struct gdbarch *gdbarch = dst->descr->gdbarch; | |
123a958e | 403 | char buf[MAX_REGISTER_SIZE]; |
2d28509a | 404 | int regnum; |
5602984a AC |
405 | /* The dst had better not be read-only. If it is, the `restore' |
406 | doesn't make much sense. */ | |
2d28509a | 407 | gdb_assert (!dst->readonly_p); |
2d28509a | 408 | /* Copy over any registers, being careful to only restore those that |
5602984a AC |
409 | were both saved and need to be restored. The full [0 .. NUM_REGS |
410 | + NUM_PSEUDO_REGS) range is checked since some architectures need | |
411 | to save/restore `cooked' registers that live in memory. */ | |
412 | for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++) | |
2d28509a | 413 | { |
5602984a | 414 | if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup)) |
2d28509a | 415 | { |
5602984a AC |
416 | int valid = cooked_read (src, regnum, buf); |
417 | if (valid) | |
418 | regcache_cooked_write (dst, regnum, buf); | |
2d28509a AC |
419 | } |
420 | } | |
421 | } | |
422 | ||
5602984a AC |
423 | static int |
424 | do_cooked_read (void *src, int regnum, void *buf) | |
425 | { | |
426 | struct regcache *regcache = src; | |
6f4e5a41 | 427 | if (!regcache->register_valid_p[regnum] && regcache->readonly_p) |
5602984a AC |
428 | /* Don't even think about fetching a register from a read-only |
429 | cache when the register isn't yet valid. There isn't a target | |
430 | from which the register value can be fetched. */ | |
431 | return 0; | |
432 | regcache_cooked_read (regcache, regnum, buf); | |
433 | return 1; | |
434 | } | |
435 | ||
436 | ||
3fadccb3 AC |
437 | void |
438 | regcache_cpy (struct regcache *dst, struct regcache *src) | |
439 | { | |
440 | int i; | |
441 | char *buf; | |
442 | gdb_assert (src != NULL && dst != NULL); | |
443 | gdb_assert (src->descr->gdbarch == dst->descr->gdbarch); | |
444 | gdb_assert (src != dst); | |
2d28509a AC |
445 | gdb_assert (src->readonly_p || dst->readonly_p); |
446 | if (!src->readonly_p) | |
5602984a | 447 | regcache_save (dst, do_cooked_read, src); |
2d28509a | 448 | else if (!dst->readonly_p) |
5602984a | 449 | regcache_restore (dst, do_cooked_read, src); |
2d28509a AC |
450 | else |
451 | regcache_cpy_no_passthrough (dst, src); | |
3fadccb3 AC |
452 | } |
453 | ||
454 | void | |
455 | regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src) | |
456 | { | |
457 | int i; | |
458 | gdb_assert (src != NULL && dst != NULL); | |
459 | gdb_assert (src->descr->gdbarch == dst->descr->gdbarch); | |
460 | /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough | |
461 | move of data into the current_regcache(). Doing this would be | |
9564ee9f | 462 | silly - it would mean that valid_p would be completely invalid. */ |
3fadccb3 | 463 | gdb_assert (dst != current_regcache); |
51b1fe4e AC |
464 | memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers); |
465 | memcpy (dst->register_valid_p, src->register_valid_p, | |
3fadccb3 AC |
466 | dst->descr->sizeof_raw_register_valid_p); |
467 | } | |
468 | ||
469 | struct regcache * | |
470 | regcache_dup (struct regcache *src) | |
471 | { | |
472 | struct regcache *newbuf; | |
473 | gdb_assert (current_regcache != NULL); | |
474 | newbuf = regcache_xmalloc (src->descr->gdbarch); | |
475 | regcache_cpy (newbuf, src); | |
476 | return newbuf; | |
477 | } | |
478 | ||
479 | struct regcache * | |
480 | regcache_dup_no_passthrough (struct regcache *src) | |
481 | { | |
482 | struct regcache *newbuf; | |
483 | gdb_assert (current_regcache != NULL); | |
484 | newbuf = regcache_xmalloc (src->descr->gdbarch); | |
485 | regcache_cpy_no_passthrough (newbuf, src); | |
486 | return newbuf; | |
487 | } | |
488 | ||
489 | int | |
490 | regcache_valid_p (struct regcache *regcache, int regnum) | |
491 | { | |
492 | gdb_assert (regcache != NULL); | |
493 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
51b1fe4e | 494 | return regcache->register_valid_p[regnum]; |
3fadccb3 AC |
495 | } |
496 | ||
3fadccb3 AC |
497 | char * |
498 | deprecated_grub_regcache_for_registers (struct regcache *regcache) | |
499 | { | |
51b1fe4e | 500 | return regcache->registers; |
3fadccb3 AC |
501 | } |
502 | ||
3fadccb3 AC |
503 | /* Global structure containing the current regcache. */ |
504 | /* FIXME: cagney/2002-05-11: The two global arrays registers[] and | |
8262ee23 | 505 | deprecated_register_valid[] currently point into this structure. */ |
3fadccb3 AC |
506 | struct regcache *current_regcache; |
507 | ||
5ebd2499 | 508 | /* NOTE: this is a write-through cache. There is no "dirty" bit for |
32178cab MS |
509 | recording if the register values have been changed (eg. by the |
510 | user). Therefore all registers must be written back to the | |
511 | target when appropriate. */ | |
512 | ||
513 | /* REGISTERS contains the cached register values (in target byte order). */ | |
514 | ||
524d7c18 | 515 | char *deprecated_registers; |
32178cab | 516 | |
8262ee23 | 517 | /* DEPRECATED_REGISTER_VALID is 0 if the register needs to be fetched, |
32178cab MS |
518 | 1 if it has been fetched, and |
519 | -1 if the register value was not available. | |
c97dcfc7 AC |
520 | |
521 | "Not available" indicates that the target is not not able to supply | |
522 | the register at this state. The register may become available at a | |
523 | later time (after the next resume). This often occures when GDB is | |
524 | manipulating a target that contains only a snapshot of the entire | |
525 | system being debugged - some of the registers in such a system may | |
526 | not have been saved. */ | |
32178cab | 527 | |
8262ee23 | 528 | signed char *deprecated_register_valid; |
32178cab | 529 | |
39f77062 | 530 | /* The thread/process associated with the current set of registers. */ |
32178cab | 531 | |
39f77062 | 532 | static ptid_t registers_ptid; |
32178cab MS |
533 | |
534 | /* | |
535 | * FUNCTIONS: | |
536 | */ | |
537 | ||
538 | /* REGISTER_CACHED() | |
539 | ||
540 | Returns 0 if the value is not in the cache (needs fetch). | |
541 | >0 if the value is in the cache. | |
542 | <0 if the value is permanently unavailable (don't ask again). */ | |
543 | ||
544 | int | |
545 | register_cached (int regnum) | |
546 | { | |
8262ee23 | 547 | return deprecated_register_valid[regnum]; |
32178cab MS |
548 | } |
549 | ||
7302a204 ND |
550 | /* Record that REGNUM's value is cached if STATE is >0, uncached but |
551 | fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */ | |
552 | ||
553 | void | |
554 | set_register_cached (int regnum, int state) | |
555 | { | |
53826de9 AC |
556 | gdb_assert (regnum >= 0); |
557 | gdb_assert (regnum < current_regcache->descr->nr_raw_registers); | |
51b1fe4e | 558 | current_regcache->register_valid_p[regnum] = state; |
7302a204 ND |
559 | } |
560 | ||
561 | /* Return whether register REGNUM is a real register. */ | |
562 | ||
563 | static int | |
564 | real_register (int regnum) | |
565 | { | |
566 | return regnum >= 0 && regnum < NUM_REGS; | |
567 | } | |
568 | ||
32178cab MS |
569 | /* Low level examining and depositing of registers. |
570 | ||
571 | The caller is responsible for making sure that the inferior is | |
572 | stopped before calling the fetching routines, or it will get | |
573 | garbage. (a change from GDB version 3, in which the caller got the | |
574 | value from the last stop). */ | |
575 | ||
576 | /* REGISTERS_CHANGED () | |
577 | ||
578 | Indicate that registers may have changed, so invalidate the cache. */ | |
579 | ||
580 | void | |
581 | registers_changed (void) | |
582 | { | |
583 | int i; | |
32178cab | 584 | |
39f77062 | 585 | registers_ptid = pid_to_ptid (-1); |
32178cab MS |
586 | |
587 | /* Force cleanup of any alloca areas if using C alloca instead of | |
588 | a builtin alloca. This particular call is used to clean up | |
589 | areas allocated by low level target code which may build up | |
590 | during lengthy interactions between gdb and the target before | |
591 | gdb gives control to the user (ie watchpoints). */ | |
592 | alloca (0); | |
593 | ||
53826de9 | 594 | for (i = 0; i < current_regcache->descr->nr_raw_registers; i++) |
7302a204 | 595 | set_register_cached (i, 0); |
32178cab MS |
596 | |
597 | if (registers_changed_hook) | |
598 | registers_changed_hook (); | |
599 | } | |
600 | ||
2b9e5f3f | 601 | /* DEPRECATED_REGISTERS_FETCHED () |
32178cab MS |
602 | |
603 | Indicate that all registers have been fetched, so mark them all valid. */ | |
604 | ||
31e9866e AC |
605 | /* NOTE: cagney/2001-12-04: This function does not set valid on the |
606 | pseudo-register range since pseudo registers are always supplied | |
607 | using supply_register(). */ | |
608 | /* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target | |
609 | code was blatting the registers[] array and then calling this. | |
610 | Since targets should only be using supply_register() the need for | |
611 | this function/hack is eliminated. */ | |
32178cab MS |
612 | |
613 | void | |
2b9e5f3f | 614 | deprecated_registers_fetched (void) |
32178cab MS |
615 | { |
616 | int i; | |
32178cab | 617 | |
a728f042 | 618 | for (i = 0; i < NUM_REGS; i++) |
7302a204 | 619 | set_register_cached (i, 1); |
fcdc5976 | 620 | /* Do not assume that the pseudo-regs have also been fetched. |
31e9866e | 621 | Fetching all real regs NEVER accounts for pseudo-regs. */ |
32178cab MS |
622 | } |
623 | ||
73937e03 AC |
624 | /* deprecated_read_register_bytes and deprecated_write_register_bytes |
625 | are generally a *BAD* idea. They are inefficient because they need | |
626 | to check for partial updates, which can only be done by scanning | |
627 | through all of the registers and seeing if the bytes that are being | |
628 | read/written fall inside of an invalid register. [The main reason | |
629 | this is necessary is that register sizes can vary, so a simple | |
630 | index won't suffice.] It is far better to call read_register_gen | |
631 | and write_register_gen if you want to get at the raw register | |
632 | contents, as it only takes a regnum as an argument, and therefore | |
633 | can't do a partial register update. | |
32178cab MS |
634 | |
635 | Prior to the recent fixes to check for partial updates, both read | |
73937e03 AC |
636 | and deprecated_write_register_bytes always checked to see if any |
637 | registers were stale, and then called target_fetch_registers (-1) | |
638 | to update the whole set. This caused really slowed things down for | |
639 | remote targets. */ | |
32178cab MS |
640 | |
641 | /* Copy INLEN bytes of consecutive data from registers | |
642 | starting with the INREGBYTE'th byte of register data | |
643 | into memory at MYADDR. */ | |
644 | ||
645 | void | |
73937e03 | 646 | deprecated_read_register_bytes (int in_start, char *in_buf, int in_len) |
32178cab | 647 | { |
61a0eb5b | 648 | int in_end = in_start + in_len; |
5ebd2499 | 649 | int regnum; |
d9d9c31f | 650 | char reg_buf[MAX_REGISTER_SIZE]; |
32178cab MS |
651 | |
652 | /* See if we are trying to read bytes from out-of-date registers. If so, | |
653 | update just those registers. */ | |
654 | ||
5ebd2499 | 655 | for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++) |
32178cab | 656 | { |
61a0eb5b AC |
657 | int reg_start; |
658 | int reg_end; | |
659 | int reg_len; | |
660 | int start; | |
661 | int end; | |
662 | int byte; | |
32178cab | 663 | |
62700349 | 664 | reg_start = DEPRECATED_REGISTER_BYTE (regnum); |
12c266ea | 665 | reg_len = DEPRECATED_REGISTER_RAW_SIZE (regnum); |
61a0eb5b | 666 | reg_end = reg_start + reg_len; |
32178cab | 667 | |
61a0eb5b | 668 | if (reg_end <= in_start || in_end <= reg_start) |
5ebd2499 | 669 | /* The range the user wants to read doesn't overlap with regnum. */ |
32178cab MS |
670 | continue; |
671 | ||
275f450c AC |
672 | if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0') |
673 | /* Force the cache to fetch the entire register. */ | |
4caf0990 | 674 | deprecated_read_register_gen (regnum, reg_buf); |
275f450c AC |
675 | else |
676 | /* Legacy note: even though this register is ``invalid'' we | |
677 | still need to return something. It would appear that some | |
678 | code relies on apparent gaps in the register array also | |
679 | being returned. */ | |
680 | /* FIXME: cagney/2001-08-18: This is just silly. It defeats | |
681 | the entire register read/write flow of control. Must | |
682 | resist temptation to return 0xdeadbeef. */ | |
524d7c18 | 683 | memcpy (reg_buf, &deprecated_registers[reg_start], reg_len); |
32178cab | 684 | |
61a0eb5b AC |
685 | /* Legacy note: This function, for some reason, allows a NULL |
686 | input buffer. If the buffer is NULL, the registers are still | |
687 | fetched, just the final transfer is skipped. */ | |
688 | if (in_buf == NULL) | |
689 | continue; | |
690 | ||
691 | /* start = max (reg_start, in_start) */ | |
692 | if (reg_start > in_start) | |
693 | start = reg_start; | |
694 | else | |
695 | start = in_start; | |
696 | ||
697 | /* end = min (reg_end, in_end) */ | |
698 | if (reg_end < in_end) | |
699 | end = reg_end; | |
700 | else | |
701 | end = in_end; | |
702 | ||
703 | /* Transfer just the bytes common to both IN_BUF and REG_BUF */ | |
704 | for (byte = start; byte < end; byte++) | |
165cd47f | 705 | { |
61a0eb5b | 706 | in_buf[byte - in_start] = reg_buf[byte - reg_start]; |
165cd47f | 707 | } |
32178cab | 708 | } |
32178cab MS |
709 | } |
710 | ||
5ebd2499 ND |
711 | /* Read register REGNUM into memory at MYADDR, which must be large |
712 | enough for REGISTER_RAW_BYTES (REGNUM). Target byte-order. If the | |
32178cab MS |
713 | register is known to be the size of a CORE_ADDR or smaller, |
714 | read_register can be used instead. */ | |
715 | ||
61a0eb5b AC |
716 | static void |
717 | legacy_read_register_gen (int regnum, char *myaddr) | |
32178cab | 718 | { |
61a0eb5b | 719 | gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS)); |
39f77062 | 720 | if (! ptid_equal (registers_ptid, inferior_ptid)) |
32178cab MS |
721 | { |
722 | registers_changed (); | |
39f77062 | 723 | registers_ptid = inferior_ptid; |
32178cab MS |
724 | } |
725 | ||
7302a204 | 726 | if (!register_cached (regnum)) |
5c27f28a | 727 | target_fetch_registers (regnum); |
7302a204 | 728 | |
3fadccb3 | 729 | memcpy (myaddr, register_buffer (current_regcache, regnum), |
12c266ea | 730 | DEPRECATED_REGISTER_RAW_SIZE (regnum)); |
32178cab MS |
731 | } |
732 | ||
61a0eb5b | 733 | void |
1aaa5f99 | 734 | regcache_raw_read (struct regcache *regcache, int regnum, void *buf) |
61a0eb5b | 735 | { |
3fadccb3 AC |
736 | gdb_assert (regcache != NULL && buf != NULL); |
737 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
738 | if (regcache->descr->legacy_p | |
2d28509a | 739 | && !regcache->readonly_p) |
3fadccb3 AC |
740 | { |
741 | gdb_assert (regcache == current_regcache); | |
742 | /* For moment, just use underlying legacy code. Ulgh!!! This | |
743 | silently and very indirectly updates the regcache's regcache | |
8262ee23 | 744 | via the global deprecated_register_valid[]. */ |
3fadccb3 AC |
745 | legacy_read_register_gen (regnum, buf); |
746 | return; | |
747 | } | |
748 | /* Make certain that the register cache is up-to-date with respect | |
749 | to the current thread. This switching shouldn't be necessary | |
750 | only there is still only one target side register cache. Sigh! | |
751 | On the bright side, at least there is a regcache object. */ | |
2d28509a | 752 | if (!regcache->readonly_p) |
3fadccb3 AC |
753 | { |
754 | gdb_assert (regcache == current_regcache); | |
755 | if (! ptid_equal (registers_ptid, inferior_ptid)) | |
756 | { | |
757 | registers_changed (); | |
758 | registers_ptid = inferior_ptid; | |
759 | } | |
760 | if (!register_cached (regnum)) | |
5c27f28a | 761 | target_fetch_registers (regnum); |
3fadccb3 AC |
762 | } |
763 | /* Copy the value directly into the register cache. */ | |
51b1fe4e | 764 | memcpy (buf, register_buffer (regcache, regnum), |
3fadccb3 | 765 | regcache->descr->sizeof_register[regnum]); |
61a0eb5b AC |
766 | } |
767 | ||
28fc6740 AC |
768 | void |
769 | regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val) | |
770 | { | |
771 | char *buf; | |
772 | gdb_assert (regcache != NULL); | |
773 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
774 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
775 | regcache_raw_read (regcache, regnum, buf); | |
776 | (*val) = extract_signed_integer (buf, | |
777 | regcache->descr->sizeof_register[regnum]); | |
778 | } | |
779 | ||
780 | void | |
781 | regcache_raw_read_unsigned (struct regcache *regcache, int regnum, | |
782 | ULONGEST *val) | |
783 | { | |
784 | char *buf; | |
785 | gdb_assert (regcache != NULL); | |
786 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
787 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
788 | regcache_raw_read (regcache, regnum, buf); | |
789 | (*val) = extract_unsigned_integer (buf, | |
790 | regcache->descr->sizeof_register[regnum]); | |
791 | } | |
792 | ||
c00dcbe9 MK |
793 | void |
794 | regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val) | |
795 | { | |
796 | void *buf; | |
797 | gdb_assert (regcache != NULL); | |
798 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers); | |
799 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
800 | store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val); | |
801 | regcache_raw_write (regcache, regnum, buf); | |
802 | } | |
803 | ||
804 | void | |
805 | regcache_raw_write_unsigned (struct regcache *regcache, int regnum, | |
806 | ULONGEST val) | |
807 | { | |
808 | void *buf; | |
809 | gdb_assert (regcache != NULL); | |
810 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers); | |
811 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
812 | store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val); | |
813 | regcache_raw_write (regcache, regnum, buf); | |
814 | } | |
815 | ||
61a0eb5b | 816 | void |
4caf0990 | 817 | deprecated_read_register_gen (int regnum, char *buf) |
61a0eb5b | 818 | { |
3fadccb3 AC |
819 | gdb_assert (current_regcache != NULL); |
820 | gdb_assert (current_regcache->descr->gdbarch == current_gdbarch); | |
821 | if (current_regcache->descr->legacy_p) | |
61a0eb5b AC |
822 | { |
823 | legacy_read_register_gen (regnum, buf); | |
824 | return; | |
825 | } | |
68365089 AC |
826 | regcache_cooked_read (current_regcache, regnum, buf); |
827 | } | |
828 | ||
829 | void | |
29e1842b | 830 | regcache_cooked_read (struct regcache *regcache, int regnum, void *buf) |
68365089 | 831 | { |
d138e37a | 832 | gdb_assert (regnum >= 0); |
68365089 AC |
833 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); |
834 | if (regnum < regcache->descr->nr_raw_registers) | |
835 | regcache_raw_read (regcache, regnum, buf); | |
2d28509a AC |
836 | else if (regcache->readonly_p |
837 | && regnum < regcache->descr->nr_cooked_registers | |
838 | && regcache->register_valid_p[regnum]) | |
839 | /* Read-only register cache, perhaphs the cooked value was cached? */ | |
840 | memcpy (buf, register_buffer (regcache, regnum), | |
841 | regcache->descr->sizeof_register[regnum]); | |
d138e37a | 842 | else |
68365089 AC |
843 | gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache, |
844 | regnum, buf); | |
61a0eb5b AC |
845 | } |
846 | ||
a378f419 AC |
847 | void |
848 | regcache_cooked_read_signed (struct regcache *regcache, int regnum, | |
849 | LONGEST *val) | |
850 | { | |
851 | char *buf; | |
852 | gdb_assert (regcache != NULL); | |
a66a9c23 | 853 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers); |
a378f419 AC |
854 | buf = alloca (regcache->descr->sizeof_register[regnum]); |
855 | regcache_cooked_read (regcache, regnum, buf); | |
856 | (*val) = extract_signed_integer (buf, | |
857 | regcache->descr->sizeof_register[regnum]); | |
858 | } | |
859 | ||
860 | void | |
861 | regcache_cooked_read_unsigned (struct regcache *regcache, int regnum, | |
862 | ULONGEST *val) | |
863 | { | |
864 | char *buf; | |
865 | gdb_assert (regcache != NULL); | |
a66a9c23 | 866 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers); |
a378f419 AC |
867 | buf = alloca (regcache->descr->sizeof_register[regnum]); |
868 | regcache_cooked_read (regcache, regnum, buf); | |
869 | (*val) = extract_unsigned_integer (buf, | |
870 | regcache->descr->sizeof_register[regnum]); | |
871 | } | |
872 | ||
a66a9c23 AC |
873 | void |
874 | regcache_cooked_write_signed (struct regcache *regcache, int regnum, | |
875 | LONGEST val) | |
876 | { | |
877 | void *buf; | |
878 | gdb_assert (regcache != NULL); | |
879 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers); | |
880 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
881 | store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val); | |
882 | regcache_cooked_write (regcache, regnum, buf); | |
883 | } | |
884 | ||
885 | void | |
886 | regcache_cooked_write_unsigned (struct regcache *regcache, int regnum, | |
887 | ULONGEST val) | |
888 | { | |
889 | void *buf; | |
890 | gdb_assert (regcache != NULL); | |
891 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers); | |
892 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
893 | store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val); | |
894 | regcache_cooked_write (regcache, regnum, buf); | |
895 | } | |
896 | ||
5ebd2499 ND |
897 | /* Write register REGNUM at MYADDR to the target. MYADDR points at |
898 | REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order. */ | |
32178cab | 899 | |
61a0eb5b | 900 | static void |
1aaa5f99 | 901 | legacy_write_register_gen (int regnum, const void *myaddr) |
32178cab MS |
902 | { |
903 | int size; | |
61a0eb5b | 904 | gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS)); |
32178cab MS |
905 | |
906 | /* On the sparc, writing %g0 is a no-op, so we don't even want to | |
907 | change the registers array if something writes to this register. */ | |
5ebd2499 | 908 | if (CANNOT_STORE_REGISTER (regnum)) |
32178cab MS |
909 | return; |
910 | ||
39f77062 | 911 | if (! ptid_equal (registers_ptid, inferior_ptid)) |
32178cab MS |
912 | { |
913 | registers_changed (); | |
39f77062 | 914 | registers_ptid = inferior_ptid; |
32178cab MS |
915 | } |
916 | ||
12c266ea | 917 | size = DEPRECATED_REGISTER_RAW_SIZE (regnum); |
32178cab | 918 | |
7302a204 | 919 | if (real_register (regnum)) |
1297a2f0 MS |
920 | { |
921 | /* If we have a valid copy of the register, and new value == old | |
922 | value, then don't bother doing the actual store. */ | |
923 | if (register_cached (regnum) | |
3fadccb3 AC |
924 | && (memcmp (register_buffer (current_regcache, regnum), myaddr, size) |
925 | == 0)) | |
1297a2f0 MS |
926 | return; |
927 | else | |
928 | target_prepare_to_store (); | |
929 | } | |
32178cab | 930 | |
3fadccb3 | 931 | memcpy (register_buffer (current_regcache, regnum), myaddr, size); |
32178cab | 932 | |
7302a204 | 933 | set_register_cached (regnum, 1); |
5c27f28a | 934 | target_store_registers (regnum); |
32178cab MS |
935 | } |
936 | ||
61a0eb5b | 937 | void |
1aaa5f99 | 938 | regcache_raw_write (struct regcache *regcache, int regnum, const void *buf) |
61a0eb5b | 939 | { |
3fadccb3 AC |
940 | gdb_assert (regcache != NULL && buf != NULL); |
941 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
2d28509a | 942 | gdb_assert (!regcache->readonly_p); |
3fadccb3 | 943 | |
2d28509a | 944 | if (regcache->descr->legacy_p) |
3fadccb3 AC |
945 | { |
946 | /* For moment, just use underlying legacy code. Ulgh!!! This | |
947 | silently and very indirectly updates the regcache's buffers | |
8262ee23 | 948 | via the globals deprecated_register_valid[] and registers[]. */ |
3fadccb3 AC |
949 | gdb_assert (regcache == current_regcache); |
950 | legacy_write_register_gen (regnum, buf); | |
951 | return; | |
952 | } | |
953 | ||
954 | /* On the sparc, writing %g0 is a no-op, so we don't even want to | |
955 | change the registers array if something writes to this register. */ | |
956 | if (CANNOT_STORE_REGISTER (regnum)) | |
957 | return; | |
958 | ||
3fadccb3 AC |
959 | /* Make certain that the correct cache is selected. */ |
960 | gdb_assert (regcache == current_regcache); | |
961 | if (! ptid_equal (registers_ptid, inferior_ptid)) | |
962 | { | |
963 | registers_changed (); | |
964 | registers_ptid = inferior_ptid; | |
965 | } | |
966 | ||
967 | /* If we have a valid copy of the register, and new value == old | |
968 | value, then don't bother doing the actual store. */ | |
969 | if (regcache_valid_p (regcache, regnum) | |
970 | && (memcmp (register_buffer (regcache, regnum), buf, | |
971 | regcache->descr->sizeof_register[regnum]) == 0)) | |
972 | return; | |
973 | ||
974 | target_prepare_to_store (); | |
975 | memcpy (register_buffer (regcache, regnum), buf, | |
976 | regcache->descr->sizeof_register[regnum]); | |
51b1fe4e | 977 | regcache->register_valid_p[regnum] = 1; |
5c27f28a | 978 | target_store_registers (regnum); |
61a0eb5b AC |
979 | } |
980 | ||
981 | void | |
4caf0990 | 982 | deprecated_write_register_gen (int regnum, char *buf) |
61a0eb5b | 983 | { |
3fadccb3 AC |
984 | gdb_assert (current_regcache != NULL); |
985 | gdb_assert (current_regcache->descr->gdbarch == current_gdbarch); | |
986 | if (current_regcache->descr->legacy_p) | |
61a0eb5b AC |
987 | { |
988 | legacy_write_register_gen (regnum, buf); | |
989 | return; | |
990 | } | |
68365089 AC |
991 | regcache_cooked_write (current_regcache, regnum, buf); |
992 | } | |
993 | ||
994 | void | |
29e1842b | 995 | regcache_cooked_write (struct regcache *regcache, int regnum, const void *buf) |
68365089 | 996 | { |
d138e37a | 997 | gdb_assert (regnum >= 0); |
68365089 AC |
998 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); |
999 | if (regnum < regcache->descr->nr_raw_registers) | |
1000 | regcache_raw_write (regcache, regnum, buf); | |
d138e37a | 1001 | else |
68365089 | 1002 | gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache, |
d8124050 | 1003 | regnum, buf); |
61a0eb5b AC |
1004 | } |
1005 | ||
32178cab MS |
1006 | /* Copy INLEN bytes of consecutive data from memory at MYADDR |
1007 | into registers starting with the MYREGSTART'th byte of register data. */ | |
1008 | ||
1009 | void | |
73937e03 | 1010 | deprecated_write_register_bytes (int myregstart, char *myaddr, int inlen) |
32178cab MS |
1011 | { |
1012 | int myregend = myregstart + inlen; | |
5ebd2499 | 1013 | int regnum; |
32178cab MS |
1014 | |
1015 | target_prepare_to_store (); | |
1016 | ||
1017 | /* Scan through the registers updating any that are covered by the | |
1018 | range myregstart<=>myregend using write_register_gen, which does | |
1019 | nice things like handling threads, and avoiding updates when the | |
1020 | new and old contents are the same. */ | |
1021 | ||
5ebd2499 | 1022 | for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++) |
32178cab MS |
1023 | { |
1024 | int regstart, regend; | |
1025 | ||
62700349 | 1026 | regstart = DEPRECATED_REGISTER_BYTE (regnum); |
12c266ea | 1027 | regend = regstart + DEPRECATED_REGISTER_RAW_SIZE (regnum); |
32178cab MS |
1028 | |
1029 | /* Is this register completely outside the range the user is writing? */ | |
1030 | if (myregend <= regstart || regend <= myregstart) | |
1031 | /* do nothing */ ; | |
1032 | ||
1033 | /* Is this register completely within the range the user is writing? */ | |
1034 | else if (myregstart <= regstart && regend <= myregend) | |
4caf0990 | 1035 | deprecated_write_register_gen (regnum, myaddr + (regstart - myregstart)); |
32178cab MS |
1036 | |
1037 | /* The register partially overlaps the range being written. */ | |
1038 | else | |
1039 | { | |
d9d9c31f | 1040 | char regbuf[MAX_REGISTER_SIZE]; |
32178cab MS |
1041 | /* What's the overlap between this register's bytes and |
1042 | those the caller wants to write? */ | |
1043 | int overlapstart = max (regstart, myregstart); | |
1044 | int overlapend = min (regend, myregend); | |
1045 | ||
1046 | /* We may be doing a partial update of an invalid register. | |
1047 | Update it from the target before scribbling on it. */ | |
4caf0990 | 1048 | deprecated_read_register_gen (regnum, regbuf); |
32178cab | 1049 | |
524d7c18 | 1050 | memcpy (&deprecated_registers[overlapstart], |
32178cab MS |
1051 | myaddr + (overlapstart - myregstart), |
1052 | overlapend - overlapstart); | |
1053 | ||
5c27f28a | 1054 | target_store_registers (regnum); |
32178cab MS |
1055 | } |
1056 | } | |
1057 | } | |
1058 | ||
06c0b04e AC |
1059 | /* Perform a partial register transfer using a read, modify, write |
1060 | operation. */ | |
1061 | ||
1062 | typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum, | |
1063 | void *buf); | |
1064 | typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum, | |
1065 | const void *buf); | |
1066 | ||
b9362cc7 | 1067 | static void |
06c0b04e AC |
1068 | regcache_xfer_part (struct regcache *regcache, int regnum, |
1069 | int offset, int len, void *in, const void *out, | |
1070 | regcache_read_ftype *read, regcache_write_ftype *write) | |
1071 | { | |
1072 | struct regcache_descr *descr = regcache->descr; | |
123a958e | 1073 | bfd_byte reg[MAX_REGISTER_SIZE]; |
06c0b04e AC |
1074 | gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]); |
1075 | gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]); | |
1076 | /* Something to do? */ | |
1077 | if (offset + len == 0) | |
1078 | return; | |
1079 | /* Read (when needed) ... */ | |
1080 | if (in != NULL | |
1081 | || offset > 0 | |
1082 | || offset + len < descr->sizeof_register[regnum]) | |
1083 | { | |
1084 | gdb_assert (read != NULL); | |
1085 | read (regcache, regnum, reg); | |
1086 | } | |
1087 | /* ... modify ... */ | |
1088 | if (in != NULL) | |
1089 | memcpy (in, reg + offset, len); | |
1090 | if (out != NULL) | |
1091 | memcpy (reg + offset, out, len); | |
1092 | /* ... write (when needed). */ | |
1093 | if (out != NULL) | |
1094 | { | |
1095 | gdb_assert (write != NULL); | |
1096 | write (regcache, regnum, reg); | |
1097 | } | |
1098 | } | |
1099 | ||
1100 | void | |
1101 | regcache_raw_read_part (struct regcache *regcache, int regnum, | |
1102 | int offset, int len, void *buf) | |
1103 | { | |
1104 | struct regcache_descr *descr = regcache->descr; | |
1105 | gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers); | |
1106 | regcache_xfer_part (regcache, regnum, offset, len, buf, NULL, | |
1107 | regcache_raw_read, regcache_raw_write); | |
1108 | } | |
1109 | ||
1110 | void | |
1111 | regcache_raw_write_part (struct regcache *regcache, int regnum, | |
1112 | int offset, int len, const void *buf) | |
1113 | { | |
1114 | struct regcache_descr *descr = regcache->descr; | |
1115 | gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers); | |
1116 | regcache_xfer_part (regcache, regnum, offset, len, NULL, buf, | |
1117 | regcache_raw_read, regcache_raw_write); | |
1118 | } | |
1119 | ||
1120 | void | |
1121 | regcache_cooked_read_part (struct regcache *regcache, int regnum, | |
1122 | int offset, int len, void *buf) | |
1123 | { | |
1124 | struct regcache_descr *descr = regcache->descr; | |
1125 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); | |
1126 | regcache_xfer_part (regcache, regnum, offset, len, buf, NULL, | |
1127 | regcache_cooked_read, regcache_cooked_write); | |
1128 | } | |
1129 | ||
1130 | void | |
1131 | regcache_cooked_write_part (struct regcache *regcache, int regnum, | |
1132 | int offset, int len, const void *buf) | |
1133 | { | |
1134 | struct regcache_descr *descr = regcache->descr; | |
1135 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); | |
1136 | regcache_xfer_part (regcache, regnum, offset, len, NULL, buf, | |
1137 | regcache_cooked_read, regcache_cooked_write); | |
1138 | } | |
32178cab | 1139 | |
d3b22ed5 AC |
1140 | /* Hack to keep code that view the register buffer as raw bytes |
1141 | working. */ | |
1142 | ||
1143 | int | |
1144 | register_offset_hack (struct gdbarch *gdbarch, int regnum) | |
1145 | { | |
1146 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
1147 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); | |
1148 | return descr->register_offset[regnum]; | |
1149 | } | |
1150 | ||
5ebd2499 | 1151 | /* Return the contents of register REGNUM as an unsigned integer. */ |
32178cab | 1152 | |
173155e8 | 1153 | ULONGEST |
5ebd2499 | 1154 | read_register (int regnum) |
32178cab | 1155 | { |
12c266ea | 1156 | char *buf = alloca (DEPRECATED_REGISTER_RAW_SIZE (regnum)); |
4caf0990 | 1157 | deprecated_read_register_gen (regnum, buf); |
12c266ea | 1158 | return (extract_unsigned_integer (buf, DEPRECATED_REGISTER_RAW_SIZE (regnum))); |
32178cab MS |
1159 | } |
1160 | ||
173155e8 | 1161 | ULONGEST |
39f77062 | 1162 | read_register_pid (int regnum, ptid_t ptid) |
32178cab | 1163 | { |
39f77062 | 1164 | ptid_t save_ptid; |
32178cab MS |
1165 | int save_pid; |
1166 | CORE_ADDR retval; | |
1167 | ||
39f77062 | 1168 | if (ptid_equal (ptid, inferior_ptid)) |
5ebd2499 | 1169 | return read_register (regnum); |
32178cab | 1170 | |
39f77062 | 1171 | save_ptid = inferior_ptid; |
32178cab | 1172 | |
39f77062 | 1173 | inferior_ptid = ptid; |
32178cab | 1174 | |
5ebd2499 | 1175 | retval = read_register (regnum); |
32178cab | 1176 | |
39f77062 | 1177 | inferior_ptid = save_ptid; |
32178cab MS |
1178 | |
1179 | return retval; | |
1180 | } | |
1181 | ||
5ebd2499 | 1182 | /* Store VALUE into the raw contents of register number REGNUM. */ |
32178cab MS |
1183 | |
1184 | void | |
5ebd2499 | 1185 | write_register (int regnum, LONGEST val) |
32178cab | 1186 | { |
61a0eb5b | 1187 | void *buf; |
32178cab | 1188 | int size; |
12c266ea | 1189 | size = DEPRECATED_REGISTER_RAW_SIZE (regnum); |
32178cab MS |
1190 | buf = alloca (size); |
1191 | store_signed_integer (buf, size, (LONGEST) val); | |
4caf0990 | 1192 | deprecated_write_register_gen (regnum, buf); |
32178cab MS |
1193 | } |
1194 | ||
1195 | void | |
39f77062 | 1196 | write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid) |
32178cab | 1197 | { |
39f77062 | 1198 | ptid_t save_ptid; |
32178cab | 1199 | |
39f77062 | 1200 | if (ptid_equal (ptid, inferior_ptid)) |
32178cab | 1201 | { |
5ebd2499 | 1202 | write_register (regnum, val); |
32178cab MS |
1203 | return; |
1204 | } | |
1205 | ||
39f77062 | 1206 | save_ptid = inferior_ptid; |
32178cab | 1207 | |
39f77062 | 1208 | inferior_ptid = ptid; |
32178cab | 1209 | |
5ebd2499 | 1210 | write_register (regnum, val); |
32178cab | 1211 | |
39f77062 | 1212 | inferior_ptid = save_ptid; |
32178cab MS |
1213 | } |
1214 | ||
9a661b68 MK |
1215 | /* FIXME: kettenis/20030828: We should get rid of supply_register and |
1216 | regcache_collect in favour of regcache_raw_supply and | |
1217 | regcache_raw_collect. */ | |
1218 | ||
32178cab MS |
1219 | /* SUPPLY_REGISTER() |
1220 | ||
5ebd2499 | 1221 | Record that register REGNUM contains VAL. This is used when the |
32178cab MS |
1222 | value is obtained from the inferior or core dump, so there is no |
1223 | need to store the value there. | |
1224 | ||
1225 | If VAL is a NULL pointer, then it's probably an unsupported register. | |
5ebd2499 | 1226 | We just set its value to all zeros. We might want to record this |
32178cab MS |
1227 | fact, and report it to the users of read_register and friends. */ |
1228 | ||
1229 | void | |
1aaa5f99 | 1230 | supply_register (int regnum, const void *val) |
32178cab | 1231 | { |
7bace51b | 1232 | regcache_raw_supply (current_regcache, regnum, val); |
32178cab MS |
1233 | |
1234 | /* On some architectures, e.g. HPPA, there are a few stray bits in | |
1235 | some registers, that the rest of the code would like to ignore. */ | |
1236 | ||
61a0eb5b AC |
1237 | /* NOTE: cagney/2001-03-16: The macro CLEAN_UP_REGISTER_VALUE is |
1238 | going to be deprecated. Instead architectures will leave the raw | |
1239 | register value as is and instead clean things up as they pass | |
d8124050 | 1240 | through the method gdbarch_pseudo_register_read() clean up the |
61a0eb5b AC |
1241 | values. */ |
1242 | ||
4ee3352d | 1243 | #ifdef DEPRECATED_CLEAN_UP_REGISTER_VALUE |
0b434a00 AC |
1244 | DEPRECATED_CLEAN_UP_REGISTER_VALUE \ |
1245 | (regnum, register_buffer (current_regcache, regnum)); | |
32178cab MS |
1246 | #endif |
1247 | } | |
1248 | ||
193cb69f AC |
1249 | void |
1250 | regcache_collect (int regnum, void *buf) | |
1251 | { | |
7bace51b | 1252 | regcache_raw_collect (current_regcache, regnum, buf); |
193cb69f AC |
1253 | } |
1254 | ||
a16d75cc | 1255 | /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */ |
9a661b68 MK |
1256 | |
1257 | void | |
1258 | regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf) | |
1259 | { | |
1260 | void *regbuf; | |
1261 | size_t size; | |
1262 | ||
a16d75cc | 1263 | gdb_assert (regcache != NULL); |
9a661b68 MK |
1264 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); |
1265 | gdb_assert (!regcache->readonly_p); | |
1266 | ||
1267 | /* FIXME: kettenis/20030828: It shouldn't be necessary to handle | |
1268 | CURRENT_REGCACHE specially here. */ | |
1269 | if (regcache == current_regcache | |
1270 | && !ptid_equal (registers_ptid, inferior_ptid)) | |
1271 | { | |
1272 | registers_changed (); | |
1273 | registers_ptid = inferior_ptid; | |
1274 | } | |
1275 | ||
1276 | regbuf = register_buffer (regcache, regnum); | |
1277 | size = regcache->descr->sizeof_register[regnum]; | |
1278 | ||
1279 | if (buf) | |
1280 | memcpy (regbuf, buf, size); | |
1281 | else | |
1282 | memset (regbuf, 0, size); | |
1283 | ||
1284 | /* Mark the register as cached. */ | |
1285 | regcache->register_valid_p[regnum] = 1; | |
1286 | } | |
1287 | ||
1288 | /* Collect register REGNUM from REGCACHE and store its contents in BUF. */ | |
1289 | ||
1290 | void | |
1291 | regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf) | |
1292 | { | |
1293 | const void *regbuf; | |
1294 | size_t size; | |
1295 | ||
1296 | gdb_assert (regcache != NULL && buf != NULL); | |
1297 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
1298 | ||
1299 | regbuf = register_buffer (regcache, regnum); | |
1300 | size = regcache->descr->sizeof_register[regnum]; | |
1301 | memcpy (buf, regbuf, size); | |
1302 | } | |
1303 | ||
193cb69f | 1304 | |
0ba6dca9 AC |
1305 | /* read_pc, write_pc, read_sp, deprecated_read_fp, etc. Special |
1306 | handling for registers PC, SP, and FP. */ | |
32178cab | 1307 | |
cde9ea48 AC |
1308 | /* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc(), |
1309 | read_sp(), and deprecated_read_fp(), will eventually be replaced by | |
1310 | per-frame methods. Instead of relying on the global INFERIOR_PTID, | |
1311 | they will use the contextual information provided by the FRAME. | |
1312 | These functions do not belong in the register cache. */ | |
32178cab | 1313 | |
cde9ea48 AC |
1314 | /* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(), |
1315 | write_pc_pid(), write_pc(), and deprecated_read_fp(), all need to | |
1316 | be replaced by something that does not rely on global state. But | |
1317 | what? */ | |
32178cab MS |
1318 | |
1319 | CORE_ADDR | |
39f77062 | 1320 | read_pc_pid (ptid_t ptid) |
32178cab | 1321 | { |
39f77062 | 1322 | ptid_t saved_inferior_ptid; |
32178cab MS |
1323 | CORE_ADDR pc_val; |
1324 | ||
39f77062 KB |
1325 | /* In case ptid != inferior_ptid. */ |
1326 | saved_inferior_ptid = inferior_ptid; | |
1327 | inferior_ptid = ptid; | |
32178cab | 1328 | |
cde9ea48 AC |
1329 | if (TARGET_READ_PC_P ()) |
1330 | pc_val = TARGET_READ_PC (ptid); | |
1331 | /* Else use per-frame method on get_current_frame. */ | |
1332 | else if (PC_REGNUM >= 0) | |
1333 | { | |
1334 | CORE_ADDR raw_val = read_register_pid (PC_REGNUM, ptid); | |
6ba34a8d | 1335 | pc_val = ADDR_BITS_REMOVE (raw_val); |
cde9ea48 AC |
1336 | } |
1337 | else | |
1338 | internal_error (__FILE__, __LINE__, "read_pc_pid: Unable to find PC"); | |
32178cab | 1339 | |
39f77062 | 1340 | inferior_ptid = saved_inferior_ptid; |
32178cab MS |
1341 | return pc_val; |
1342 | } | |
1343 | ||
1344 | CORE_ADDR | |
1345 | read_pc (void) | |
1346 | { | |
39f77062 | 1347 | return read_pc_pid (inferior_ptid); |
32178cab MS |
1348 | } |
1349 | ||
32178cab | 1350 | void |
39f77062 | 1351 | generic_target_write_pc (CORE_ADDR pc, ptid_t ptid) |
32178cab | 1352 | { |
32178cab | 1353 | if (PC_REGNUM >= 0) |
39f77062 | 1354 | write_register_pid (PC_REGNUM, pc, ptid); |
afb18d0f AC |
1355 | else |
1356 | internal_error (__FILE__, __LINE__, | |
1357 | "generic_target_write_pc"); | |
32178cab MS |
1358 | } |
1359 | ||
1360 | void | |
39f77062 | 1361 | write_pc_pid (CORE_ADDR pc, ptid_t ptid) |
32178cab | 1362 | { |
39f77062 | 1363 | ptid_t saved_inferior_ptid; |
32178cab | 1364 | |
39f77062 KB |
1365 | /* In case ptid != inferior_ptid. */ |
1366 | saved_inferior_ptid = inferior_ptid; | |
1367 | inferior_ptid = ptid; | |
32178cab | 1368 | |
39f77062 | 1369 | TARGET_WRITE_PC (pc, ptid); |
32178cab | 1370 | |
39f77062 | 1371 | inferior_ptid = saved_inferior_ptid; |
32178cab MS |
1372 | } |
1373 | ||
1374 | void | |
1375 | write_pc (CORE_ADDR pc) | |
1376 | { | |
39f77062 | 1377 | write_pc_pid (pc, inferior_ptid); |
32178cab MS |
1378 | } |
1379 | ||
1380 | /* Cope with strage ways of getting to the stack and frame pointers */ | |
1381 | ||
32178cab MS |
1382 | CORE_ADDR |
1383 | read_sp (void) | |
1384 | { | |
bd1ce8ba AC |
1385 | if (TARGET_READ_SP_P ()) |
1386 | return TARGET_READ_SP (); | |
a9e5fdc2 AC |
1387 | else if (gdbarch_unwind_sp_p (current_gdbarch)) |
1388 | return get_frame_sp (get_current_frame ()); | |
bd1ce8ba | 1389 | else if (SP_REGNUM >= 0) |
a9e5fdc2 AC |
1390 | /* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions |
1391 | about the architecture so put it at the end. */ | |
bd1ce8ba AC |
1392 | return read_register (SP_REGNUM); |
1393 | internal_error (__FILE__, __LINE__, "read_sp: Unable to find SP"); | |
32178cab MS |
1394 | } |
1395 | ||
32178cab | 1396 | void |
b46e02f6 | 1397 | deprecated_write_sp (CORE_ADDR val) |
32178cab | 1398 | { |
b46e02f6 AC |
1399 | gdb_assert (SP_REGNUM >= 0); |
1400 | write_register (SP_REGNUM, val); | |
32178cab MS |
1401 | } |
1402 | ||
32178cab | 1403 | CORE_ADDR |
0ba6dca9 | 1404 | deprecated_read_fp (void) |
32178cab | 1405 | { |
0ba6dca9 AC |
1406 | if (DEPRECATED_TARGET_READ_FP_P ()) |
1407 | return DEPRECATED_TARGET_READ_FP (); | |
1408 | else if (DEPRECATED_FP_REGNUM >= 0) | |
1409 | return read_register (DEPRECATED_FP_REGNUM); | |
1410 | else | |
1411 | internal_error (__FILE__, __LINE__, "deprecated_read_fp"); | |
32178cab MS |
1412 | } |
1413 | ||
705152c5 MS |
1414 | static void |
1415 | reg_flush_command (char *command, int from_tty) | |
1416 | { | |
1417 | /* Force-flush the register cache. */ | |
1418 | registers_changed (); | |
1419 | if (from_tty) | |
1420 | printf_filtered ("Register cache flushed.\n"); | |
1421 | } | |
1422 | ||
32178cab MS |
1423 | static void |
1424 | build_regcache (void) | |
3fadccb3 AC |
1425 | { |
1426 | current_regcache = regcache_xmalloc (current_gdbarch); | |
2d28509a | 1427 | current_regcache->readonly_p = 0; |
524d7c18 | 1428 | deprecated_registers = deprecated_grub_regcache_for_registers (current_regcache); |
b923b08d | 1429 | deprecated_register_valid = current_regcache->register_valid_p; |
3fadccb3 AC |
1430 | } |
1431 | ||
af030b9a AC |
1432 | static void |
1433 | dump_endian_bytes (struct ui_file *file, enum bfd_endian endian, | |
1434 | const unsigned char *buf, long len) | |
1435 | { | |
1436 | int i; | |
1437 | switch (endian) | |
1438 | { | |
1439 | case BFD_ENDIAN_BIG: | |
1440 | for (i = 0; i < len; i++) | |
1441 | fprintf_unfiltered (file, "%02x", buf[i]); | |
1442 | break; | |
1443 | case BFD_ENDIAN_LITTLE: | |
1444 | for (i = len - 1; i >= 0; i--) | |
1445 | fprintf_unfiltered (file, "%02x", buf[i]); | |
1446 | break; | |
1447 | default: | |
1448 | internal_error (__FILE__, __LINE__, "Bad switch"); | |
1449 | } | |
1450 | } | |
1451 | ||
1452 | enum regcache_dump_what | |
1453 | { | |
b59ff9d5 | 1454 | regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups |
af030b9a AC |
1455 | }; |
1456 | ||
1457 | static void | |
1458 | regcache_dump (struct regcache *regcache, struct ui_file *file, | |
1459 | enum regcache_dump_what what_to_dump) | |
1460 | { | |
1461 | struct cleanup *cleanups = make_cleanup (null_cleanup, NULL); | |
b59ff9d5 | 1462 | struct gdbarch *gdbarch = regcache->descr->gdbarch; |
af030b9a AC |
1463 | int regnum; |
1464 | int footnote_nr = 0; | |
1465 | int footnote_register_size = 0; | |
1466 | int footnote_register_offset = 0; | |
1467 | int footnote_register_type_name_null = 0; | |
1468 | long register_offset = 0; | |
123a958e | 1469 | unsigned char buf[MAX_REGISTER_SIZE]; |
af030b9a AC |
1470 | |
1471 | #if 0 | |
1472 | fprintf_unfiltered (file, "legacy_p %d\n", regcache->descr->legacy_p); | |
1473 | fprintf_unfiltered (file, "nr_raw_registers %d\n", | |
1474 | regcache->descr->nr_raw_registers); | |
1475 | fprintf_unfiltered (file, "nr_cooked_registers %d\n", | |
1476 | regcache->descr->nr_cooked_registers); | |
1477 | fprintf_unfiltered (file, "sizeof_raw_registers %ld\n", | |
1478 | regcache->descr->sizeof_raw_registers); | |
1479 | fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n", | |
1480 | regcache->descr->sizeof_raw_register_valid_p); | |
af030b9a AC |
1481 | fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS); |
1482 | fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS); | |
1483 | #endif | |
1484 | ||
1485 | gdb_assert (regcache->descr->nr_cooked_registers | |
1486 | == (NUM_REGS + NUM_PSEUDO_REGS)); | |
1487 | ||
1488 | for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++) | |
1489 | { | |
1490 | /* Name. */ | |
1491 | if (regnum < 0) | |
1492 | fprintf_unfiltered (file, " %-10s", "Name"); | |
1493 | else | |
1494 | { | |
1495 | const char *p = REGISTER_NAME (regnum); | |
1496 | if (p == NULL) | |
1497 | p = ""; | |
1498 | else if (p[0] == '\0') | |
1499 | p = "''"; | |
1500 | fprintf_unfiltered (file, " %-10s", p); | |
1501 | } | |
1502 | ||
1503 | /* Number. */ | |
1504 | if (regnum < 0) | |
1505 | fprintf_unfiltered (file, " %4s", "Nr"); | |
1506 | else | |
1507 | fprintf_unfiltered (file, " %4d", regnum); | |
1508 | ||
1509 | /* Relative number. */ | |
1510 | if (regnum < 0) | |
1511 | fprintf_unfiltered (file, " %4s", "Rel"); | |
1512 | else if (regnum < NUM_REGS) | |
1513 | fprintf_unfiltered (file, " %4d", regnum); | |
1514 | else | |
1515 | fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS)); | |
1516 | ||
1517 | /* Offset. */ | |
1518 | if (regnum < 0) | |
1519 | fprintf_unfiltered (file, " %6s ", "Offset"); | |
1520 | else | |
1521 | { | |
1522 | fprintf_unfiltered (file, " %6ld", | |
1523 | regcache->descr->register_offset[regnum]); | |
a7e3c2ad | 1524 | if (register_offset != regcache->descr->register_offset[regnum] |
62700349 | 1525 | || register_offset != DEPRECATED_REGISTER_BYTE (regnum) |
d3b22ed5 AC |
1526 | || (regnum > 0 |
1527 | && (regcache->descr->register_offset[regnum] | |
1528 | != (regcache->descr->register_offset[regnum - 1] | |
1529 | + regcache->descr->sizeof_register[regnum - 1]))) | |
1530 | ) | |
af030b9a AC |
1531 | { |
1532 | if (!footnote_register_offset) | |
1533 | footnote_register_offset = ++footnote_nr; | |
1534 | fprintf_unfiltered (file, "*%d", footnote_register_offset); | |
1535 | } | |
1536 | else | |
1537 | fprintf_unfiltered (file, " "); | |
1538 | register_offset = (regcache->descr->register_offset[regnum] | |
1539 | + regcache->descr->sizeof_register[regnum]); | |
1540 | } | |
1541 | ||
1542 | /* Size. */ | |
1543 | if (regnum < 0) | |
1544 | fprintf_unfiltered (file, " %5s ", "Size"); | |
1545 | else | |
1546 | { | |
1547 | fprintf_unfiltered (file, " %5ld", | |
1548 | regcache->descr->sizeof_register[regnum]); | |
1549 | if ((regcache->descr->sizeof_register[regnum] | |
12c266ea | 1550 | != DEPRECATED_REGISTER_RAW_SIZE (regnum)) |
af030b9a | 1551 | || (regcache->descr->sizeof_register[regnum] |
f30992d4 | 1552 | != DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum)) |
af030b9a | 1553 | || (regcache->descr->sizeof_register[regnum] |
bb425013 AC |
1554 | != TYPE_LENGTH (register_type (regcache->descr->gdbarch, |
1555 | regnum))) | |
af030b9a AC |
1556 | ) |
1557 | { | |
1558 | if (!footnote_register_size) | |
1559 | footnote_register_size = ++footnote_nr; | |
1560 | fprintf_unfiltered (file, "*%d", footnote_register_size); | |
1561 | } | |
1562 | else | |
1563 | fprintf_unfiltered (file, " "); | |
1564 | } | |
1565 | ||
1566 | /* Type. */ | |
b59ff9d5 AC |
1567 | { |
1568 | const char *t; | |
1569 | if (regnum < 0) | |
1570 | t = "Type"; | |
1571 | else | |
1572 | { | |
1573 | static const char blt[] = "builtin_type"; | |
1574 | t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum)); | |
1575 | if (t == NULL) | |
1576 | { | |
1577 | char *n; | |
1578 | if (!footnote_register_type_name_null) | |
1579 | footnote_register_type_name_null = ++footnote_nr; | |
1580 | xasprintf (&n, "*%d", footnote_register_type_name_null); | |
1581 | make_cleanup (xfree, n); | |
1582 | t = n; | |
1583 | } | |
1584 | /* Chop a leading builtin_type. */ | |
1585 | if (strncmp (t, blt, strlen (blt)) == 0) | |
1586 | t += strlen (blt); | |
1587 | } | |
1588 | fprintf_unfiltered (file, " %-15s", t); | |
1589 | } | |
1590 | ||
1591 | /* Leading space always present. */ | |
1592 | fprintf_unfiltered (file, " "); | |
af030b9a AC |
1593 | |
1594 | /* Value, raw. */ | |
1595 | if (what_to_dump == regcache_dump_raw) | |
1596 | { | |
1597 | if (regnum < 0) | |
1598 | fprintf_unfiltered (file, "Raw value"); | |
1599 | else if (regnum >= regcache->descr->nr_raw_registers) | |
1600 | fprintf_unfiltered (file, "<cooked>"); | |
1601 | else if (!regcache_valid_p (regcache, regnum)) | |
1602 | fprintf_unfiltered (file, "<invalid>"); | |
1603 | else | |
1604 | { | |
1605 | regcache_raw_read (regcache, regnum, buf); | |
1606 | fprintf_unfiltered (file, "0x"); | |
1607 | dump_endian_bytes (file, TARGET_BYTE_ORDER, buf, | |
12c266ea | 1608 | DEPRECATED_REGISTER_RAW_SIZE (regnum)); |
af030b9a AC |
1609 | } |
1610 | } | |
1611 | ||
1612 | /* Value, cooked. */ | |
1613 | if (what_to_dump == regcache_dump_cooked) | |
1614 | { | |
1615 | if (regnum < 0) | |
1616 | fprintf_unfiltered (file, "Cooked value"); | |
1617 | else | |
1618 | { | |
1619 | regcache_cooked_read (regcache, regnum, buf); | |
1620 | fprintf_unfiltered (file, "0x"); | |
1621 | dump_endian_bytes (file, TARGET_BYTE_ORDER, buf, | |
f30992d4 | 1622 | DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum)); |
af030b9a AC |
1623 | } |
1624 | } | |
1625 | ||
b59ff9d5 AC |
1626 | /* Group members. */ |
1627 | if (what_to_dump == regcache_dump_groups) | |
1628 | { | |
1629 | if (regnum < 0) | |
1630 | fprintf_unfiltered (file, "Groups"); | |
1631 | else | |
1632 | { | |
b59ff9d5 | 1633 | const char *sep = ""; |
6c7d17ba AC |
1634 | struct reggroup *group; |
1635 | for (group = reggroup_next (gdbarch, NULL); | |
1636 | group != NULL; | |
1637 | group = reggroup_next (gdbarch, group)) | |
b59ff9d5 | 1638 | { |
6c7d17ba | 1639 | if (gdbarch_register_reggroup_p (gdbarch, regnum, group)) |
b59ff9d5 | 1640 | { |
6c7d17ba | 1641 | fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group)); |
b59ff9d5 AC |
1642 | sep = ","; |
1643 | } | |
1644 | } | |
1645 | } | |
1646 | } | |
1647 | ||
af030b9a AC |
1648 | fprintf_unfiltered (file, "\n"); |
1649 | } | |
1650 | ||
1651 | if (footnote_register_size) | |
1652 | fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n", | |
1653 | footnote_register_size); | |
1654 | if (footnote_register_offset) | |
1655 | fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n", | |
1656 | footnote_register_offset); | |
1657 | if (footnote_register_type_name_null) | |
1658 | fprintf_unfiltered (file, | |
1659 | "*%d: Register type's name NULL.\n", | |
1660 | footnote_register_type_name_null); | |
1661 | do_cleanups (cleanups); | |
1662 | } | |
1663 | ||
1664 | static void | |
1665 | regcache_print (char *args, enum regcache_dump_what what_to_dump) | |
1666 | { | |
1667 | if (args == NULL) | |
1668 | regcache_dump (current_regcache, gdb_stdout, what_to_dump); | |
1669 | else | |
1670 | { | |
1671 | struct ui_file *file = gdb_fopen (args, "w"); | |
1672 | if (file == NULL) | |
1673 | perror_with_name ("maintenance print architecture"); | |
1674 | regcache_dump (current_regcache, file, what_to_dump); | |
1675 | ui_file_delete (file); | |
1676 | } | |
1677 | } | |
1678 | ||
1679 | static void | |
1680 | maintenance_print_registers (char *args, int from_tty) | |
1681 | { | |
1682 | regcache_print (args, regcache_dump_none); | |
1683 | } | |
1684 | ||
1685 | static void | |
1686 | maintenance_print_raw_registers (char *args, int from_tty) | |
1687 | { | |
1688 | regcache_print (args, regcache_dump_raw); | |
1689 | } | |
1690 | ||
1691 | static void | |
1692 | maintenance_print_cooked_registers (char *args, int from_tty) | |
1693 | { | |
1694 | regcache_print (args, regcache_dump_cooked); | |
1695 | } | |
1696 | ||
b59ff9d5 AC |
1697 | static void |
1698 | maintenance_print_register_groups (char *args, int from_tty) | |
1699 | { | |
1700 | regcache_print (args, regcache_dump_groups); | |
1701 | } | |
1702 | ||
b9362cc7 AC |
1703 | extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */ |
1704 | ||
32178cab MS |
1705 | void |
1706 | _initialize_regcache (void) | |
1707 | { | |
030f20e1 | 1708 | regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr); |
046a4708 AC |
1709 | DEPRECATED_REGISTER_GDBARCH_SWAP (current_regcache); |
1710 | DEPRECATED_REGISTER_GDBARCH_SWAP (deprecated_registers); | |
1711 | DEPRECATED_REGISTER_GDBARCH_SWAP (deprecated_register_valid); | |
1712 | deprecated_register_gdbarch_swap (NULL, 0, build_regcache); | |
705152c5 MS |
1713 | |
1714 | add_com ("flushregs", class_maintenance, reg_flush_command, | |
1715 | "Force gdb to flush its register cache (maintainer command)"); | |
39f77062 KB |
1716 | |
1717 | /* Initialize the thread/process associated with the current set of | |
1718 | registers. For now, -1 is special, and means `no current process'. */ | |
1719 | registers_ptid = pid_to_ptid (-1); | |
af030b9a AC |
1720 | |
1721 | add_cmd ("registers", class_maintenance, | |
1722 | maintenance_print_registers, | |
1723 | "Print the internal register configuration.\ | |
1724 | Takes an optional file parameter.", | |
1725 | &maintenanceprintlist); | |
1726 | add_cmd ("raw-registers", class_maintenance, | |
1727 | maintenance_print_raw_registers, | |
1728 | "Print the internal register configuration including raw values.\ | |
1729 | Takes an optional file parameter.", | |
1730 | &maintenanceprintlist); | |
1731 | add_cmd ("cooked-registers", class_maintenance, | |
1732 | maintenance_print_cooked_registers, | |
1733 | "Print the internal register configuration including cooked values.\ | |
b59ff9d5 AC |
1734 | Takes an optional file parameter.", |
1735 | &maintenanceprintlist); | |
1736 | add_cmd ("register-groups", class_maintenance, | |
1737 | maintenance_print_register_groups, | |
1738 | "Print the internal register configuration including each register's group.\ | |
af030b9a AC |
1739 | Takes an optional file parameter.", |
1740 | &maintenanceprintlist); | |
1741 | ||
32178cab | 1742 | } |