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