1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001,
4 2002, 2004, 2007 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
29 #include "reggroups.h"
30 #include "gdb_assert.h"
31 #include "gdb_string.h"
32 #include "gdbcmd.h" /* For maintenanceprintlist. */
38 * Here is the actual register cache.
41 /* Per-architecture object describing the layout of a register cache.
42 Computed once when the architecture is created */
44 struct gdbarch_data *regcache_descr_handle;
48 /* The architecture this descriptor belongs to. */
49 struct gdbarch *gdbarch;
51 /* The raw register cache. Each raw (or hard) register is supplied
52 by the target interface. The raw cache should not contain
53 redundant information - if the PC is constructed from two
54 registers then those registers and not the PC lives in the raw
57 long sizeof_raw_registers;
58 long sizeof_raw_register_valid_p;
60 /* The cooked register space. Each cooked register in the range
61 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
62 register. The remaining [NR_RAW_REGISTERS
63 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
64 both raw registers and memory by the architecture methods
65 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
66 int nr_cooked_registers;
67 long sizeof_cooked_registers;
68 long sizeof_cooked_register_valid_p;
70 /* Offset and size (in 8 bit bytes), of reach register in the
71 register cache. All registers (including those in the range
72 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
73 Assigning all registers an offset makes it possible to keep
74 legacy code, such as that found in read_register_bytes() and
75 write_register_bytes() working. */
76 long *register_offset;
77 long *sizeof_register;
79 /* Cached table containing the type of each register. */
80 struct type **register_type;
84 init_regcache_descr (struct gdbarch *gdbarch)
87 struct regcache_descr *descr;
88 gdb_assert (gdbarch != NULL);
90 /* Create an initial, zero filled, table. */
91 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
92 descr->gdbarch = gdbarch;
94 /* Total size of the register space. The raw registers are mapped
95 directly onto the raw register cache while the pseudo's are
96 either mapped onto raw-registers or memory. */
97 descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS;
98 descr->sizeof_cooked_register_valid_p = NUM_REGS + NUM_PSEUDO_REGS;
100 /* Fill in a table of register types. */
102 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, struct type *);
103 for (i = 0; i < descr->nr_cooked_registers; i++)
104 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
106 /* Construct a strictly RAW register cache. Don't allow pseudo's
107 into the register cache. */
108 descr->nr_raw_registers = NUM_REGS;
110 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
111 array. This pretects GDB from erant code that accesses elements
112 of the global register_valid_p[] array in the range [NUM_REGS
113 .. NUM_REGS + NUM_PSEUDO_REGS). */
114 descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
116 /* Lay out the register cache.
118 NOTE: cagney/2002-05-22: Only register_type() is used when
119 constructing the register cache. It is assumed that the
120 register's raw size, virtual size and type length are all the
125 descr->sizeof_register
126 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
127 descr->register_offset
128 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
129 for (i = 0; i < descr->nr_cooked_registers; i++)
131 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
132 descr->register_offset[i] = offset;
133 offset += descr->sizeof_register[i];
134 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
136 /* Set the real size of the register cache buffer. */
137 descr->sizeof_cooked_registers = offset;
140 /* FIXME: cagney/2002-05-22: Should only need to allocate space for
141 the raw registers. Unfortunately some code still accesses the
142 register array directly using the global registers[]. Until that
143 code has been purged, play safe and over allocating the register
145 descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
150 static struct regcache_descr *
151 regcache_descr (struct gdbarch *gdbarch)
153 return gdbarch_data (gdbarch, regcache_descr_handle);
156 /* Utility functions returning useful register attributes stored in
157 the regcache descr. */
160 register_type (struct gdbarch *gdbarch, int regnum)
162 struct regcache_descr *descr = regcache_descr (gdbarch);
163 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
164 return descr->register_type[regnum];
167 /* Utility functions returning useful register attributes stored in
168 the regcache descr. */
171 register_size (struct gdbarch *gdbarch, int regnum)
173 struct regcache_descr *descr = regcache_descr (gdbarch);
175 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
176 size = descr->sizeof_register[regnum];
180 /* The register cache for storing raw register values. */
184 struct regcache_descr *descr;
185 /* The register buffers. A read-only register cache can hold the
186 full [0 .. NUM_REGS + NUM_PSEUDO_REGS) while a read/write
187 register cache can only hold [0 .. NUM_REGS). */
189 /* Register cache status:
190 register_valid_p[REG] == 0 if REG value is not in the cache
191 > 0 if REG value is in the cache
192 < 0 if REG value is permanently unavailable */
193 signed char *register_valid_p;
194 /* Is this a read-only cache? A read-only cache is used for saving
195 the target's register state (e.g, across an inferior function
196 call or just before forcing a function return). A read-only
197 cache can only be updated via the methods regcache_dup() and
198 regcache_cpy(). The actual contents are determined by the
199 reggroup_save and reggroup_restore methods. */
204 regcache_xmalloc (struct gdbarch *gdbarch)
206 struct regcache_descr *descr;
207 struct regcache *regcache;
208 gdb_assert (gdbarch != NULL);
209 descr = regcache_descr (gdbarch);
210 regcache = XMALLOC (struct regcache);
211 regcache->descr = descr;
213 = XCALLOC (descr->sizeof_raw_registers, gdb_byte);
214 regcache->register_valid_p
215 = XCALLOC (descr->sizeof_raw_register_valid_p, gdb_byte);
216 regcache->readonly_p = 1;
221 regcache_xfree (struct regcache *regcache)
223 if (regcache == NULL)
225 xfree (regcache->registers);
226 xfree (regcache->register_valid_p);
231 do_regcache_xfree (void *data)
233 regcache_xfree (data);
237 make_cleanup_regcache_xfree (struct regcache *regcache)
239 return make_cleanup (do_regcache_xfree, regcache);
242 /* Return REGCACHE's architecture. */
245 get_regcache_arch (const struct regcache *regcache)
247 return regcache->descr->gdbarch;
250 /* Return a pointer to register REGNUM's buffer cache. */
253 register_buffer (const struct regcache *regcache, int regnum)
255 return regcache->registers + regcache->descr->register_offset[regnum];
259 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
262 struct gdbarch *gdbarch = dst->descr->gdbarch;
263 gdb_byte buf[MAX_REGISTER_SIZE];
265 /* The DST should be `read-only', if it wasn't then the save would
266 end up trying to write the register values back out to the
268 gdb_assert (dst->readonly_p);
269 /* Clear the dest. */
270 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
271 memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
272 /* Copy over any registers (identified by their membership in the
273 save_reggroup) and mark them as valid. The full [0 .. NUM_REGS +
274 NUM_PSEUDO_REGS) range is checked since some architectures need
275 to save/restore `cooked' registers that live in memory. */
276 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
278 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
280 int valid = cooked_read (src, regnum, buf);
283 memcpy (register_buffer (dst, regnum), buf,
284 register_size (gdbarch, regnum));
285 dst->register_valid_p[regnum] = 1;
292 regcache_restore (struct regcache *dst,
293 regcache_cooked_read_ftype *cooked_read,
294 void *cooked_read_context)
296 struct gdbarch *gdbarch = dst->descr->gdbarch;
297 gdb_byte buf[MAX_REGISTER_SIZE];
299 /* The dst had better not be read-only. If it is, the `restore'
300 doesn't make much sense. */
301 gdb_assert (!dst->readonly_p);
302 /* Copy over any registers, being careful to only restore those that
303 were both saved and need to be restored. The full [0 .. NUM_REGS
304 + NUM_PSEUDO_REGS) range is checked since some architectures need
305 to save/restore `cooked' registers that live in memory. */
306 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
308 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
310 int valid = cooked_read (cooked_read_context, regnum, buf);
312 regcache_cooked_write (dst, regnum, buf);
318 do_cooked_read (void *src, int regnum, gdb_byte *buf)
320 struct regcache *regcache = src;
321 if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
322 /* Don't even think about fetching a register from a read-only
323 cache when the register isn't yet valid. There isn't a target
324 from which the register value can be fetched. */
326 regcache_cooked_read (regcache, regnum, buf);
332 regcache_cpy (struct regcache *dst, struct regcache *src)
336 gdb_assert (src != NULL && dst != NULL);
337 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
338 gdb_assert (src != dst);
339 gdb_assert (src->readonly_p || dst->readonly_p);
340 if (!src->readonly_p)
341 regcache_save (dst, do_cooked_read, src);
342 else if (!dst->readonly_p)
343 regcache_restore (dst, do_cooked_read, src);
345 regcache_cpy_no_passthrough (dst, src);
349 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
352 gdb_assert (src != NULL && dst != NULL);
353 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
354 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
355 move of data into the current_regcache(). Doing this would be
356 silly - it would mean that valid_p would be completely invalid. */
357 gdb_assert (dst != current_regcache);
358 memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
359 memcpy (dst->register_valid_p, src->register_valid_p,
360 dst->descr->sizeof_raw_register_valid_p);
364 regcache_dup (struct regcache *src)
366 struct regcache *newbuf;
367 gdb_assert (current_regcache != NULL);
368 newbuf = regcache_xmalloc (src->descr->gdbarch);
369 regcache_cpy (newbuf, src);
374 regcache_dup_no_passthrough (struct regcache *src)
376 struct regcache *newbuf;
377 gdb_assert (current_regcache != NULL);
378 newbuf = regcache_xmalloc (src->descr->gdbarch);
379 regcache_cpy_no_passthrough (newbuf, src);
384 regcache_valid_p (struct regcache *regcache, int regnum)
386 gdb_assert (regcache != NULL);
387 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
388 return regcache->register_valid_p[regnum];
391 /* Global structure containing the current regcache. */
392 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
393 deprecated_register_valid[] currently point into this structure. */
394 struct regcache *current_regcache;
396 /* NOTE: this is a write-through cache. There is no "dirty" bit for
397 recording if the register values have been changed (eg. by the
398 user). Therefore all registers must be written back to the
399 target when appropriate. */
401 /* The thread/process associated with the current set of registers. */
403 static ptid_t registers_ptid;
411 Returns 0 if the value is not in the cache (needs fetch).
412 >0 if the value is in the cache.
413 <0 if the value is permanently unavailable (don't ask again). */
416 register_cached (int regnum)
418 return current_regcache->register_valid_p[regnum];
421 /* Record that REGNUM's value is cached if STATE is >0, uncached but
422 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
425 set_register_cached (int regnum, int state)
427 gdb_assert (regnum >= 0);
428 gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
429 current_regcache->register_valid_p[regnum] = state;
432 /* Observer for the target_changed event. */
435 regcache_observer_target_changed (struct target_ops *target)
437 registers_changed ();
440 /* Low level examining and depositing of registers.
442 The caller is responsible for making sure that the inferior is
443 stopped before calling the fetching routines, or it will get
444 garbage. (a change from GDB version 3, in which the caller got the
445 value from the last stop). */
447 /* REGISTERS_CHANGED ()
449 Indicate that registers may have changed, so invalidate the cache. */
452 registers_changed (void)
456 registers_ptid = pid_to_ptid (-1);
458 /* Force cleanup of any alloca areas if using C alloca instead of
459 a builtin alloca. This particular call is used to clean up
460 areas allocated by low level target code which may build up
461 during lengthy interactions between gdb and the target before
462 gdb gives control to the user (ie watchpoints). */
465 for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
466 set_register_cached (i, 0);
469 /* DEPRECATED_REGISTERS_FETCHED ()
471 Indicate that all registers have been fetched, so mark them all valid. */
473 /* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
474 code was blatting the registers[] array and then calling this.
475 Since targets should only be using regcache_raw_supply() the need for
476 this function/hack is eliminated. */
479 deprecated_registers_fetched (void)
483 for (i = 0; i < NUM_REGS; i++)
484 set_register_cached (i, 1);
485 /* Do not assume that the pseudo-regs have also been fetched.
486 Fetching all real regs NEVER accounts for pseudo-regs. */
490 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
492 gdb_assert (regcache != NULL && buf != NULL);
493 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
494 /* Make certain that the register cache is up-to-date with respect
495 to the current thread. This switching shouldn't be necessary
496 only there is still only one target side register cache. Sigh!
497 On the bright side, at least there is a regcache object. */
498 if (!regcache->readonly_p)
500 gdb_assert (regcache == current_regcache);
501 if (! ptid_equal (registers_ptid, inferior_ptid))
503 registers_changed ();
504 registers_ptid = inferior_ptid;
506 if (!register_cached (regnum))
507 target_fetch_registers (regnum);
509 /* FIXME: cagney/2004-08-07: At present a number of targets
510 forget (or didn't know that they needed) to set this leading to
511 panics. Also is the problem that targets need to indicate
512 that a register is in one of the possible states: valid,
513 undefined, unknown. The last of which isn't yet
515 gdb_assert (register_cached (regnum));
518 /* Copy the value directly into the register cache. */
519 memcpy (buf, register_buffer (regcache, regnum),
520 regcache->descr->sizeof_register[regnum]);
524 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
527 gdb_assert (regcache != NULL);
528 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
529 buf = alloca (regcache->descr->sizeof_register[regnum]);
530 regcache_raw_read (regcache, regnum, buf);
531 (*val) = extract_signed_integer (buf,
532 regcache->descr->sizeof_register[regnum]);
536 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
540 gdb_assert (regcache != NULL);
541 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
542 buf = alloca (regcache->descr->sizeof_register[regnum]);
543 regcache_raw_read (regcache, regnum, buf);
544 (*val) = extract_unsigned_integer (buf,
545 regcache->descr->sizeof_register[regnum]);
549 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
552 gdb_assert (regcache != NULL);
553 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
554 buf = alloca (regcache->descr->sizeof_register[regnum]);
555 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
556 regcache_raw_write (regcache, regnum, buf);
560 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
564 gdb_assert (regcache != NULL);
565 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
566 buf = alloca (regcache->descr->sizeof_register[regnum]);
567 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
568 regcache_raw_write (regcache, regnum, buf);
572 deprecated_read_register_gen (int regnum, gdb_byte *buf)
574 gdb_assert (current_regcache != NULL);
575 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
576 regcache_cooked_read (current_regcache, regnum, buf);
580 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
582 gdb_assert (regnum >= 0);
583 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
584 if (regnum < regcache->descr->nr_raw_registers)
585 regcache_raw_read (regcache, regnum, buf);
586 else if (regcache->readonly_p
587 && regnum < regcache->descr->nr_cooked_registers
588 && regcache->register_valid_p[regnum])
589 /* Read-only register cache, perhaps the cooked value was cached? */
590 memcpy (buf, register_buffer (regcache, regnum),
591 regcache->descr->sizeof_register[regnum]);
593 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
598 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
602 gdb_assert (regcache != NULL);
603 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
604 buf = alloca (regcache->descr->sizeof_register[regnum]);
605 regcache_cooked_read (regcache, regnum, buf);
606 (*val) = extract_signed_integer (buf,
607 regcache->descr->sizeof_register[regnum]);
611 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
615 gdb_assert (regcache != NULL);
616 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
617 buf = alloca (regcache->descr->sizeof_register[regnum]);
618 regcache_cooked_read (regcache, regnum, buf);
619 (*val) = extract_unsigned_integer (buf,
620 regcache->descr->sizeof_register[regnum]);
624 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
628 gdb_assert (regcache != NULL);
629 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
630 buf = alloca (regcache->descr->sizeof_register[regnum]);
631 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
632 regcache_cooked_write (regcache, regnum, buf);
636 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
640 gdb_assert (regcache != NULL);
641 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
642 buf = alloca (regcache->descr->sizeof_register[regnum]);
643 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
644 regcache_cooked_write (regcache, regnum, buf);
648 regcache_raw_write (struct regcache *regcache, int regnum,
651 gdb_assert (regcache != NULL && buf != NULL);
652 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
653 gdb_assert (!regcache->readonly_p);
655 /* On the sparc, writing %g0 is a no-op, so we don't even want to
656 change the registers array if something writes to this register. */
657 if (CANNOT_STORE_REGISTER (regnum))
660 /* Make certain that the correct cache is selected. */
661 gdb_assert (regcache == current_regcache);
662 if (! ptid_equal (registers_ptid, inferior_ptid))
664 registers_changed ();
665 registers_ptid = inferior_ptid;
668 /* If we have a valid copy of the register, and new value == old
669 value, then don't bother doing the actual store. */
670 if (regcache_valid_p (regcache, regnum)
671 && (memcmp (register_buffer (regcache, regnum), buf,
672 regcache->descr->sizeof_register[regnum]) == 0))
675 target_prepare_to_store ();
676 memcpy (register_buffer (regcache, regnum), buf,
677 regcache->descr->sizeof_register[regnum]);
678 regcache->register_valid_p[regnum] = 1;
679 target_store_registers (regnum);
683 deprecated_write_register_gen (int regnum, gdb_byte *buf)
685 gdb_assert (current_regcache != NULL);
686 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
687 regcache_cooked_write (current_regcache, regnum, buf);
691 regcache_cooked_write (struct regcache *regcache, int regnum,
694 gdb_assert (regnum >= 0);
695 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
696 if (regnum < regcache->descr->nr_raw_registers)
697 regcache_raw_write (regcache, regnum, buf);
699 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
703 /* Perform a partial register transfer using a read, modify, write
706 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
708 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
712 regcache_xfer_part (struct regcache *regcache, int regnum,
713 int offset, int len, void *in, const void *out,
714 void (*read) (struct regcache *regcache, int regnum,
716 void (*write) (struct regcache *regcache, int regnum,
717 const gdb_byte *buf))
719 struct regcache_descr *descr = regcache->descr;
720 gdb_byte reg[MAX_REGISTER_SIZE];
721 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
722 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
723 /* Something to do? */
724 if (offset + len == 0)
726 /* Read (when needed) ... */
729 || offset + len < descr->sizeof_register[regnum])
731 gdb_assert (read != NULL);
732 read (regcache, regnum, reg);
736 memcpy (in, reg + offset, len);
738 memcpy (reg + offset, out, len);
739 /* ... write (when needed). */
742 gdb_assert (write != NULL);
743 write (regcache, regnum, reg);
748 regcache_raw_read_part (struct regcache *regcache, int regnum,
749 int offset, int len, gdb_byte *buf)
751 struct regcache_descr *descr = regcache->descr;
752 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
753 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
754 regcache_raw_read, regcache_raw_write);
758 regcache_raw_write_part (struct regcache *regcache, int regnum,
759 int offset, int len, const gdb_byte *buf)
761 struct regcache_descr *descr = regcache->descr;
762 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
763 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
764 regcache_raw_read, regcache_raw_write);
768 regcache_cooked_read_part (struct regcache *regcache, int regnum,
769 int offset, int len, gdb_byte *buf)
771 struct regcache_descr *descr = regcache->descr;
772 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
773 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
774 regcache_cooked_read, regcache_cooked_write);
778 regcache_cooked_write_part (struct regcache *regcache, int regnum,
779 int offset, int len, const gdb_byte *buf)
781 struct regcache_descr *descr = regcache->descr;
782 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
783 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
784 regcache_cooked_read, regcache_cooked_write);
787 /* Hack to keep code that view the register buffer as raw bytes
791 register_offset_hack (struct gdbarch *gdbarch, int regnum)
793 struct regcache_descr *descr = regcache_descr (gdbarch);
794 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
795 return descr->register_offset[regnum];
798 /* Return the contents of register REGNUM as an unsigned integer. */
801 read_register (int regnum)
803 gdb_byte *buf = alloca (register_size (current_gdbarch, regnum));
804 deprecated_read_register_gen (regnum, buf);
805 return (extract_unsigned_integer (buf, register_size (current_gdbarch, regnum)));
809 read_register_pid (int regnum, ptid_t ptid)
815 if (ptid_equal (ptid, inferior_ptid))
816 return read_register (regnum);
818 save_ptid = inferior_ptid;
820 inferior_ptid = ptid;
822 retval = read_register (regnum);
824 inferior_ptid = save_ptid;
829 /* Store VALUE into the raw contents of register number REGNUM. */
832 write_register (int regnum, LONGEST val)
836 size = register_size (current_gdbarch, regnum);
838 store_signed_integer (buf, size, (LONGEST) val);
839 deprecated_write_register_gen (regnum, buf);
843 write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
847 if (ptid_equal (ptid, inferior_ptid))
849 write_register (regnum, val);
853 save_ptid = inferior_ptid;
855 inferior_ptid = ptid;
857 write_register (regnum, val);
859 inferior_ptid = save_ptid;
862 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
865 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
870 gdb_assert (regcache != NULL);
871 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
872 gdb_assert (!regcache->readonly_p);
874 /* FIXME: kettenis/20030828: It shouldn't be necessary to handle
875 CURRENT_REGCACHE specially here. */
876 if (regcache == current_regcache
877 && !ptid_equal (registers_ptid, inferior_ptid))
879 registers_changed ();
880 registers_ptid = inferior_ptid;
883 regbuf = register_buffer (regcache, regnum);
884 size = regcache->descr->sizeof_register[regnum];
887 memcpy (regbuf, buf, size);
889 memset (regbuf, 0, size);
891 /* Mark the register as cached. */
892 regcache->register_valid_p[regnum] = 1;
895 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
898 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
903 gdb_assert (regcache != NULL && buf != NULL);
904 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
906 regbuf = register_buffer (regcache, regnum);
907 size = regcache->descr->sizeof_register[regnum];
908 memcpy (buf, regbuf, size);
912 /* read_pc, write_pc, read_sp, etc. Special handling for registers
915 /* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc() and
916 read_sp(), will eventually be replaced by per-frame methods.
917 Instead of relying on the global INFERIOR_PTID, they will use the
918 contextual information provided by the FRAME. These functions do
919 not belong in the register cache. */
921 /* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
922 write_pc_pid() and write_pc(), all need to be replaced by something
923 that does not rely on global state. But what? */
926 read_pc_pid (ptid_t ptid)
928 ptid_t saved_inferior_ptid;
931 /* In case ptid != inferior_ptid. */
932 saved_inferior_ptid = inferior_ptid;
933 inferior_ptid = ptid;
935 if (TARGET_READ_PC_P ())
936 pc_val = TARGET_READ_PC (ptid);
937 /* Else use per-frame method on get_current_frame. */
938 else if (PC_REGNUM >= 0)
940 CORE_ADDR raw_val = read_register_pid (PC_REGNUM, ptid);
941 pc_val = ADDR_BITS_REMOVE (raw_val);
944 internal_error (__FILE__, __LINE__, _("read_pc_pid: Unable to find PC"));
946 inferior_ptid = saved_inferior_ptid;
953 return read_pc_pid (inferior_ptid);
957 generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
960 write_register_pid (PC_REGNUM, pc, ptid);
962 internal_error (__FILE__, __LINE__,
963 _("generic_target_write_pc"));
967 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
969 ptid_t saved_inferior_ptid;
971 /* In case ptid != inferior_ptid. */
972 saved_inferior_ptid = inferior_ptid;
973 inferior_ptid = ptid;
975 TARGET_WRITE_PC (pc, ptid);
977 inferior_ptid = saved_inferior_ptid;
981 write_pc (CORE_ADDR pc)
983 write_pc_pid (pc, inferior_ptid);
986 /* Cope with strage ways of getting to the stack and frame pointers */
991 if (TARGET_READ_SP_P ())
992 return TARGET_READ_SP ();
993 else if (gdbarch_unwind_sp_p (current_gdbarch))
994 return get_frame_sp (get_current_frame ());
995 else if (SP_REGNUM >= 0)
996 /* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions
997 about the architecture so put it at the end. */
998 return read_register (SP_REGNUM);
999 internal_error (__FILE__, __LINE__, _("read_sp: Unable to find SP"));
1003 reg_flush_command (char *command, int from_tty)
1005 /* Force-flush the register cache. */
1006 registers_changed ();
1008 printf_filtered (_("Register cache flushed.\n"));
1012 build_regcache (void)
1014 current_regcache = regcache_xmalloc (current_gdbarch);
1015 current_regcache->readonly_p = 0;
1019 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1020 const unsigned char *buf, long len)
1025 case BFD_ENDIAN_BIG:
1026 for (i = 0; i < len; i++)
1027 fprintf_unfiltered (file, "%02x", buf[i]);
1029 case BFD_ENDIAN_LITTLE:
1030 for (i = len - 1; i >= 0; i--)
1031 fprintf_unfiltered (file, "%02x", buf[i]);
1034 internal_error (__FILE__, __LINE__, _("Bad switch"));
1038 enum regcache_dump_what
1040 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
1044 regcache_dump (struct regcache *regcache, struct ui_file *file,
1045 enum regcache_dump_what what_to_dump)
1047 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1048 struct gdbarch *gdbarch = regcache->descr->gdbarch;
1050 int footnote_nr = 0;
1051 int footnote_register_size = 0;
1052 int footnote_register_offset = 0;
1053 int footnote_register_type_name_null = 0;
1054 long register_offset = 0;
1055 unsigned char buf[MAX_REGISTER_SIZE];
1058 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1059 regcache->descr->nr_raw_registers);
1060 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1061 regcache->descr->nr_cooked_registers);
1062 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1063 regcache->descr->sizeof_raw_registers);
1064 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1065 regcache->descr->sizeof_raw_register_valid_p);
1066 fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1067 fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1070 gdb_assert (regcache->descr->nr_cooked_registers
1071 == (NUM_REGS + NUM_PSEUDO_REGS));
1073 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1077 fprintf_unfiltered (file, " %-10s", "Name");
1080 const char *p = REGISTER_NAME (regnum);
1083 else if (p[0] == '\0')
1085 fprintf_unfiltered (file, " %-10s", p);
1090 fprintf_unfiltered (file, " %4s", "Nr");
1092 fprintf_unfiltered (file, " %4d", regnum);
1094 /* Relative number. */
1096 fprintf_unfiltered (file, " %4s", "Rel");
1097 else if (regnum < NUM_REGS)
1098 fprintf_unfiltered (file, " %4d", regnum);
1100 fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1104 fprintf_unfiltered (file, " %6s ", "Offset");
1107 fprintf_unfiltered (file, " %6ld",
1108 regcache->descr->register_offset[regnum]);
1109 if (register_offset != regcache->descr->register_offset[regnum]
1110 || register_offset != DEPRECATED_REGISTER_BYTE (regnum)
1112 && (regcache->descr->register_offset[regnum]
1113 != (regcache->descr->register_offset[regnum - 1]
1114 + regcache->descr->sizeof_register[regnum - 1])))
1117 if (!footnote_register_offset)
1118 footnote_register_offset = ++footnote_nr;
1119 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1122 fprintf_unfiltered (file, " ");
1123 register_offset = (regcache->descr->register_offset[regnum]
1124 + regcache->descr->sizeof_register[regnum]);
1129 fprintf_unfiltered (file, " %5s ", "Size");
1131 fprintf_unfiltered (file, " %5ld",
1132 regcache->descr->sizeof_register[regnum]);
1141 static const char blt[] = "builtin_type";
1142 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1146 if (!footnote_register_type_name_null)
1147 footnote_register_type_name_null = ++footnote_nr;
1148 n = xstrprintf ("*%d", footnote_register_type_name_null);
1149 make_cleanup (xfree, n);
1152 /* Chop a leading builtin_type. */
1153 if (strncmp (t, blt, strlen (blt)) == 0)
1156 fprintf_unfiltered (file, " %-15s", t);
1159 /* Leading space always present. */
1160 fprintf_unfiltered (file, " ");
1163 if (what_to_dump == regcache_dump_raw)
1166 fprintf_unfiltered (file, "Raw value");
1167 else if (regnum >= regcache->descr->nr_raw_registers)
1168 fprintf_unfiltered (file, "<cooked>");
1169 else if (!regcache_valid_p (regcache, regnum))
1170 fprintf_unfiltered (file, "<invalid>");
1173 regcache_raw_read (regcache, regnum, buf);
1174 fprintf_unfiltered (file, "0x");
1175 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1176 regcache->descr->sizeof_register[regnum]);
1180 /* Value, cooked. */
1181 if (what_to_dump == regcache_dump_cooked)
1184 fprintf_unfiltered (file, "Cooked value");
1187 regcache_cooked_read (regcache, regnum, buf);
1188 fprintf_unfiltered (file, "0x");
1189 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1190 regcache->descr->sizeof_register[regnum]);
1194 /* Group members. */
1195 if (what_to_dump == regcache_dump_groups)
1198 fprintf_unfiltered (file, "Groups");
1201 const char *sep = "";
1202 struct reggroup *group;
1203 for (group = reggroup_next (gdbarch, NULL);
1205 group = reggroup_next (gdbarch, group))
1207 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1209 fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
1216 fprintf_unfiltered (file, "\n");
1219 if (footnote_register_size)
1220 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1221 footnote_register_size);
1222 if (footnote_register_offset)
1223 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1224 footnote_register_offset);
1225 if (footnote_register_type_name_null)
1226 fprintf_unfiltered (file,
1227 "*%d: Register type's name NULL.\n",
1228 footnote_register_type_name_null);
1229 do_cleanups (cleanups);
1233 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1236 regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1239 struct ui_file *file = gdb_fopen (args, "w");
1241 perror_with_name (_("maintenance print architecture"));
1242 regcache_dump (current_regcache, file, what_to_dump);
1243 ui_file_delete (file);
1248 maintenance_print_registers (char *args, int from_tty)
1250 regcache_print (args, regcache_dump_none);
1254 maintenance_print_raw_registers (char *args, int from_tty)
1256 regcache_print (args, regcache_dump_raw);
1260 maintenance_print_cooked_registers (char *args, int from_tty)
1262 regcache_print (args, regcache_dump_cooked);
1266 maintenance_print_register_groups (char *args, int from_tty)
1268 regcache_print (args, regcache_dump_groups);
1271 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1274 _initialize_regcache (void)
1276 regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
1277 DEPRECATED_REGISTER_GDBARCH_SWAP (current_regcache);
1278 deprecated_register_gdbarch_swap (NULL, 0, build_regcache);
1280 observer_attach_target_changed (regcache_observer_target_changed);
1282 add_com ("flushregs", class_maintenance, reg_flush_command,
1283 _("Force gdb to flush its register cache (maintainer command)"));
1285 /* Initialize the thread/process associated with the current set of
1286 registers. For now, -1 is special, and means `no current process'. */
1287 registers_ptid = pid_to_ptid (-1);
1289 add_cmd ("registers", class_maintenance, maintenance_print_registers, _("\
1290 Print the internal register configuration.\n\
1291 Takes an optional file parameter."), &maintenanceprintlist);
1292 add_cmd ("raw-registers", class_maintenance,
1293 maintenance_print_raw_registers, _("\
1294 Print the internal register configuration including raw values.\n\
1295 Takes an optional file parameter."), &maintenanceprintlist);
1296 add_cmd ("cooked-registers", class_maintenance,
1297 maintenance_print_cooked_registers, _("\
1298 Print the internal register configuration including cooked values.\n\
1299 Takes an optional file parameter."), &maintenanceprintlist);
1300 add_cmd ("register-groups", class_maintenance,
1301 maintenance_print_register_groups, _("\
1302 Print the internal register configuration including each register's group.\n\
1303 Takes an optional file parameter."),
1304 &maintenanceprintlist);