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