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