1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000,
4 2001, 2002, 2004 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., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, 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 regigisters 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 char *register_valid_p;
190 /* Is this a read-only cache? A read-only cache is used for saving
191 the target's register state (e.g, across an inferior function
192 call or just before forcing a function return). A read-only
193 cache can only be updated via the methods regcache_dup() and
194 regcache_cpy(). The actual contents are determined by the
195 reggroup_save and reggroup_restore methods. */
200 regcache_xmalloc (struct gdbarch *gdbarch)
202 struct regcache_descr *descr;
203 struct regcache *regcache;
204 gdb_assert (gdbarch != NULL);
205 descr = regcache_descr (gdbarch);
206 regcache = XMALLOC (struct regcache);
207 regcache->descr = descr;
209 = XCALLOC (descr->sizeof_raw_registers, char);
210 regcache->register_valid_p
211 = XCALLOC (descr->sizeof_raw_register_valid_p, char);
212 regcache->readonly_p = 1;
217 regcache_xfree (struct regcache *regcache)
219 if (regcache == NULL)
221 xfree (regcache->registers);
222 xfree (regcache->register_valid_p);
227 do_regcache_xfree (void *data)
229 regcache_xfree (data);
233 make_cleanup_regcache_xfree (struct regcache *regcache)
235 return make_cleanup (do_regcache_xfree, regcache);
238 /* Return REGCACHE's architecture. */
241 get_regcache_arch (const struct regcache *regcache)
243 return regcache->descr->gdbarch;
246 /* Return a pointer to register REGNUM's buffer cache. */
249 register_buffer (const struct regcache *regcache, int regnum)
251 return regcache->registers + regcache->descr->register_offset[regnum];
255 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
258 struct gdbarch *gdbarch = dst->descr->gdbarch;
259 char buf[MAX_REGISTER_SIZE];
261 /* The DST should be `read-only', if it wasn't then the save would
262 end up trying to write the register values back out to the
264 gdb_assert (dst->readonly_p);
265 /* Clear the dest. */
266 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
267 memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
268 /* Copy over any registers (identified by their membership in the
269 save_reggroup) and mark them as valid. The full [0 .. NUM_REGS +
270 NUM_PSEUDO_REGS) range is checked since some architectures need
271 to save/restore `cooked' registers that live in memory. */
272 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
274 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
276 int valid = cooked_read (src, regnum, buf);
279 memcpy (register_buffer (dst, regnum), buf,
280 register_size (gdbarch, regnum));
281 dst->register_valid_p[regnum] = 1;
288 regcache_restore (struct regcache *dst,
289 regcache_cooked_read_ftype *cooked_read,
292 struct gdbarch *gdbarch = dst->descr->gdbarch;
293 char buf[MAX_REGISTER_SIZE];
295 /* The dst had better not be read-only. If it is, the `restore'
296 doesn't make much sense. */
297 gdb_assert (!dst->readonly_p);
298 /* Copy over any registers, being careful to only restore those that
299 were both saved and need to be restored. The full [0 .. NUM_REGS
300 + NUM_PSEUDO_REGS) range is checked since some architectures need
301 to save/restore `cooked' registers that live in memory. */
302 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
304 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
306 int valid = cooked_read (src, regnum, buf);
308 regcache_cooked_write (dst, regnum, buf);
314 do_cooked_read (void *src, int regnum, void *buf)
316 struct regcache *regcache = src;
317 if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
318 /* Don't even think about fetching a register from a read-only
319 cache when the register isn't yet valid. There isn't a target
320 from which the register value can be fetched. */
322 regcache_cooked_read (regcache, regnum, buf);
328 regcache_cpy (struct regcache *dst, struct regcache *src)
332 gdb_assert (src != NULL && dst != NULL);
333 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
334 gdb_assert (src != dst);
335 gdb_assert (src->readonly_p || dst->readonly_p);
336 if (!src->readonly_p)
337 regcache_save (dst, do_cooked_read, src);
338 else if (!dst->readonly_p)
339 regcache_restore (dst, do_cooked_read, src);
341 regcache_cpy_no_passthrough (dst, src);
345 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
348 gdb_assert (src != NULL && dst != NULL);
349 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
350 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
351 move of data into the current_regcache(). Doing this would be
352 silly - it would mean that valid_p would be completely invalid. */
353 gdb_assert (dst != current_regcache);
354 memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
355 memcpy (dst->register_valid_p, src->register_valid_p,
356 dst->descr->sizeof_raw_register_valid_p);
360 regcache_dup (struct regcache *src)
362 struct regcache *newbuf;
363 gdb_assert (current_regcache != NULL);
364 newbuf = regcache_xmalloc (src->descr->gdbarch);
365 regcache_cpy (newbuf, src);
370 regcache_dup_no_passthrough (struct regcache *src)
372 struct regcache *newbuf;
373 gdb_assert (current_regcache != NULL);
374 newbuf = regcache_xmalloc (src->descr->gdbarch);
375 regcache_cpy_no_passthrough (newbuf, src);
380 regcache_valid_p (struct regcache *regcache, int regnum)
382 gdb_assert (regcache != NULL);
383 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
384 return regcache->register_valid_p[regnum];
388 deprecated_grub_regcache_for_registers (struct regcache *regcache)
390 return regcache->registers;
393 /* Global structure containing the current regcache. */
394 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
395 deprecated_register_valid[] currently point into this structure. */
396 struct regcache *current_regcache;
398 /* NOTE: this is a write-through cache. There is no "dirty" bit for
399 recording if the register values have been changed (eg. by the
400 user). Therefore all registers must be written back to the
401 target when appropriate. */
403 /* REGISTERS contains the cached register values (in target byte order). */
405 char *deprecated_registers;
407 /* DEPRECATED_REGISTER_VALID is 0 if the register needs to be fetched,
408 1 if it has been fetched, and
409 -1 if the register value was not available.
411 "Not available" indicates that the target is not not able to supply
412 the register at this state. The register may become available at a
413 later time (after the next resume). This often occures when GDB is
414 manipulating a target that contains only a snapshot of the entire
415 system being debugged - some of the registers in such a system may
416 not have been saved. */
418 signed char *deprecated_register_valid;
420 /* The thread/process associated with the current set of registers. */
422 static ptid_t registers_ptid;
430 Returns 0 if the value is not in the cache (needs fetch).
431 >0 if the value is in the cache.
432 <0 if the value is permanently unavailable (don't ask again). */
435 register_cached (int regnum)
437 return deprecated_register_valid[regnum];
440 /* Record that REGNUM's value is cached if STATE is >0, uncached but
441 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
444 set_register_cached (int regnum, int state)
446 gdb_assert (regnum >= 0);
447 gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
448 current_regcache->register_valid_p[regnum] = state;
451 /* Observer for the target_changed event. */
454 regcache_observer_target_changed (struct target_ops *target)
456 registers_changed ();
459 /* Low level examining and depositing of registers.
461 The caller is responsible for making sure that the inferior is
462 stopped before calling the fetching routines, or it will get
463 garbage. (a change from GDB version 3, in which the caller got the
464 value from the last stop). */
466 /* REGISTERS_CHANGED ()
468 Indicate that registers may have changed, so invalidate the cache. */
471 registers_changed (void)
475 registers_ptid = pid_to_ptid (-1);
477 /* Force cleanup of any alloca areas if using C alloca instead of
478 a builtin alloca. This particular call is used to clean up
479 areas allocated by low level target code which may build up
480 during lengthy interactions between gdb and the target before
481 gdb gives control to the user (ie watchpoints). */
484 for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
485 set_register_cached (i, 0);
487 if (deprecated_registers_changed_hook)
488 deprecated_registers_changed_hook ();
491 /* DEPRECATED_REGISTERS_FETCHED ()
493 Indicate that all registers have been fetched, so mark them all valid. */
495 /* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
496 code was blatting the registers[] array and then calling this.
497 Since targets should only be using regcache_raw_supply() the need for
498 this function/hack is eliminated. */
501 deprecated_registers_fetched (void)
505 for (i = 0; i < NUM_REGS; i++)
506 set_register_cached (i, 1);
507 /* Do not assume that the pseudo-regs have also been fetched.
508 Fetching all real regs NEVER accounts for pseudo-regs. */
511 /* deprecated_read_register_bytes and deprecated_write_register_bytes
512 are generally a *BAD* idea. They are inefficient because they need
513 to check for partial updates, which can only be done by scanning
514 through all of the registers and seeing if the bytes that are being
515 read/written fall inside of an invalid register. [The main reason
516 this is necessary is that register sizes can vary, so a simple
517 index won't suffice.] It is far better to call read_register_gen
518 and write_register_gen if you want to get at the raw register
519 contents, as it only takes a regnum as an argument, and therefore
520 can't do a partial register update.
522 Prior to the recent fixes to check for partial updates, both read
523 and deprecated_write_register_bytes always checked to see if any
524 registers were stale, and then called target_fetch_registers (-1)
525 to update the whole set. This caused really slowed things down for
528 /* Copy INLEN bytes of consecutive data from registers
529 starting with the INREGBYTE'th byte of register data
530 into memory at MYADDR. */
533 deprecated_read_register_bytes (int in_start, char *in_buf, int in_len)
535 int in_end = in_start + in_len;
537 char reg_buf[MAX_REGISTER_SIZE];
539 /* See if we are trying to read bytes from out-of-date registers. If so,
540 update just those registers. */
542 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
551 reg_start = DEPRECATED_REGISTER_BYTE (regnum);
552 reg_len = register_size (current_gdbarch, regnum);
553 reg_end = reg_start + reg_len;
555 if (reg_end <= in_start || in_end <= reg_start)
556 /* The range the user wants to read doesn't overlap with regnum. */
559 if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0')
560 /* Force the cache to fetch the entire register. */
561 deprecated_read_register_gen (regnum, reg_buf);
563 /* Legacy note: even though this register is ``invalid'' we
564 still need to return something. It would appear that some
565 code relies on apparent gaps in the register array also
567 /* FIXME: cagney/2001-08-18: This is just silly. It defeats
568 the entire register read/write flow of control. Must
569 resist temptation to return 0xdeadbeef. */
570 memcpy (reg_buf, &deprecated_registers[reg_start], reg_len);
572 /* Legacy note: This function, for some reason, allows a NULL
573 input buffer. If the buffer is NULL, the registers are still
574 fetched, just the final transfer is skipped. */
578 /* start = max (reg_start, in_start) */
579 if (reg_start > in_start)
584 /* end = min (reg_end, in_end) */
585 if (reg_end < in_end)
590 /* Transfer just the bytes common to both IN_BUF and REG_BUF */
591 for (byte = start; byte < end; byte++)
593 in_buf[byte - in_start] = reg_buf[byte - reg_start];
599 regcache_raw_read (struct regcache *regcache, int regnum, void *buf)
601 gdb_assert (regcache != NULL && buf != NULL);
602 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
603 /* Make certain that the register cache is up-to-date with respect
604 to the current thread. This switching shouldn't be necessary
605 only there is still only one target side register cache. Sigh!
606 On the bright side, at least there is a regcache object. */
607 if (!regcache->readonly_p)
609 gdb_assert (regcache == current_regcache);
610 if (! ptid_equal (registers_ptid, inferior_ptid))
612 registers_changed ();
613 registers_ptid = inferior_ptid;
615 if (!register_cached (regnum))
616 target_fetch_registers (regnum);
617 gdb_assert (register_cached (regnum));
619 /* Copy the value directly into the register cache. */
620 memcpy (buf, register_buffer (regcache, regnum),
621 regcache->descr->sizeof_register[regnum]);
625 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
628 gdb_assert (regcache != NULL);
629 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
630 buf = alloca (regcache->descr->sizeof_register[regnum]);
631 regcache_raw_read (regcache, regnum, buf);
632 (*val) = extract_signed_integer (buf,
633 regcache->descr->sizeof_register[regnum]);
637 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
641 gdb_assert (regcache != NULL);
642 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
643 buf = alloca (regcache->descr->sizeof_register[regnum]);
644 regcache_raw_read (regcache, regnum, buf);
645 (*val) = extract_unsigned_integer (buf,
646 regcache->descr->sizeof_register[regnum]);
650 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
653 gdb_assert (regcache != NULL);
654 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
655 buf = alloca (regcache->descr->sizeof_register[regnum]);
656 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
657 regcache_raw_write (regcache, regnum, buf);
661 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
665 gdb_assert (regcache != NULL);
666 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
667 buf = alloca (regcache->descr->sizeof_register[regnum]);
668 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
669 regcache_raw_write (regcache, regnum, buf);
673 deprecated_read_register_gen (int regnum, char *buf)
675 gdb_assert (current_regcache != NULL);
676 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
677 regcache_cooked_read (current_regcache, regnum, buf);
681 regcache_cooked_read (struct regcache *regcache, int regnum, void *buf)
683 gdb_assert (regnum >= 0);
684 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
685 if (regnum < regcache->descr->nr_raw_registers)
686 regcache_raw_read (regcache, regnum, buf);
687 else if (regcache->readonly_p
688 && regnum < regcache->descr->nr_cooked_registers
689 && regcache->register_valid_p[regnum])
690 /* Read-only register cache, perhaps the cooked value was cached? */
691 memcpy (buf, register_buffer (regcache, regnum),
692 regcache->descr->sizeof_register[regnum]);
694 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
699 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
703 gdb_assert (regcache != NULL);
704 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
705 buf = alloca (regcache->descr->sizeof_register[regnum]);
706 regcache_cooked_read (regcache, regnum, buf);
707 (*val) = extract_signed_integer (buf,
708 regcache->descr->sizeof_register[regnum]);
712 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
716 gdb_assert (regcache != NULL);
717 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
718 buf = alloca (regcache->descr->sizeof_register[regnum]);
719 regcache_cooked_read (regcache, regnum, buf);
720 (*val) = extract_unsigned_integer (buf,
721 regcache->descr->sizeof_register[regnum]);
725 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
729 gdb_assert (regcache != NULL);
730 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
731 buf = alloca (regcache->descr->sizeof_register[regnum]);
732 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
733 regcache_cooked_write (regcache, regnum, buf);
737 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
741 gdb_assert (regcache != NULL);
742 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
743 buf = alloca (regcache->descr->sizeof_register[regnum]);
744 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
745 regcache_cooked_write (regcache, regnum, buf);
749 regcache_raw_write (struct regcache *regcache, int regnum, const void *buf)
751 gdb_assert (regcache != NULL && buf != NULL);
752 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
753 gdb_assert (!regcache->readonly_p);
755 /* On the sparc, writing %g0 is a no-op, so we don't even want to
756 change the registers array if something writes to this register. */
757 if (CANNOT_STORE_REGISTER (regnum))
760 /* Make certain that the correct cache is selected. */
761 gdb_assert (regcache == current_regcache);
762 if (! ptid_equal (registers_ptid, inferior_ptid))
764 registers_changed ();
765 registers_ptid = inferior_ptid;
768 /* If we have a valid copy of the register, and new value == old
769 value, then don't bother doing the actual store. */
770 if (regcache_valid_p (regcache, regnum)
771 && (memcmp (register_buffer (regcache, regnum), buf,
772 regcache->descr->sizeof_register[regnum]) == 0))
775 target_prepare_to_store ();
776 memcpy (register_buffer (regcache, regnum), buf,
777 regcache->descr->sizeof_register[regnum]);
778 regcache->register_valid_p[regnum] = 1;
779 target_store_registers (regnum);
783 deprecated_write_register_gen (int regnum, char *buf)
785 gdb_assert (current_regcache != NULL);
786 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
787 regcache_cooked_write (current_regcache, regnum, buf);
791 regcache_cooked_write (struct regcache *regcache, int regnum, const void *buf)
793 gdb_assert (regnum >= 0);
794 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
795 if (regnum < regcache->descr->nr_raw_registers)
796 regcache_raw_write (regcache, regnum, buf);
798 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
802 /* Copy INLEN bytes of consecutive data from memory at MYADDR
803 into registers starting with the MYREGSTART'th byte of register data. */
806 deprecated_write_register_bytes (int myregstart, char *myaddr, int inlen)
808 int myregend = myregstart + inlen;
811 target_prepare_to_store ();
813 /* Scan through the registers updating any that are covered by the
814 range myregstart<=>myregend using write_register_gen, which does
815 nice things like handling threads, and avoiding updates when the
816 new and old contents are the same. */
818 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
820 int regstart, regend;
822 regstart = DEPRECATED_REGISTER_BYTE (regnum);
823 regend = regstart + register_size (current_gdbarch, regnum);
825 /* Is this register completely outside the range the user is writing? */
826 if (myregend <= regstart || regend <= myregstart)
829 /* Is this register completely within the range the user is writing? */
830 else if (myregstart <= regstart && regend <= myregend)
831 deprecated_write_register_gen (regnum, myaddr + (regstart - myregstart));
833 /* The register partially overlaps the range being written. */
836 char regbuf[MAX_REGISTER_SIZE];
837 /* What's the overlap between this register's bytes and
838 those the caller wants to write? */
839 int overlapstart = max (regstart, myregstart);
840 int overlapend = min (regend, myregend);
842 /* We may be doing a partial update of an invalid register.
843 Update it from the target before scribbling on it. */
844 deprecated_read_register_gen (regnum, regbuf);
846 memcpy (&deprecated_registers[overlapstart],
847 myaddr + (overlapstart - myregstart),
848 overlapend - overlapstart);
850 target_store_registers (regnum);
855 /* Perform a partial register transfer using a read, modify, write
858 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
860 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
864 regcache_xfer_part (struct regcache *regcache, int regnum,
865 int offset, int len, void *in, const void *out,
866 regcache_read_ftype *read, regcache_write_ftype *write)
868 struct regcache_descr *descr = regcache->descr;
869 bfd_byte reg[MAX_REGISTER_SIZE];
870 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
871 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
872 /* Something to do? */
873 if (offset + len == 0)
875 /* Read (when needed) ... */
878 || offset + len < descr->sizeof_register[regnum])
880 gdb_assert (read != NULL);
881 read (regcache, regnum, reg);
885 memcpy (in, reg + offset, len);
887 memcpy (reg + offset, out, len);
888 /* ... write (when needed). */
891 gdb_assert (write != NULL);
892 write (regcache, regnum, reg);
897 regcache_raw_read_part (struct regcache *regcache, int regnum,
898 int offset, int len, void *buf)
900 struct regcache_descr *descr = regcache->descr;
901 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
902 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
903 regcache_raw_read, regcache_raw_write);
907 regcache_raw_write_part (struct regcache *regcache, int regnum,
908 int offset, int len, const void *buf)
910 struct regcache_descr *descr = regcache->descr;
911 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
912 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
913 regcache_raw_read, regcache_raw_write);
917 regcache_cooked_read_part (struct regcache *regcache, int regnum,
918 int offset, int len, void *buf)
920 struct regcache_descr *descr = regcache->descr;
921 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
922 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
923 regcache_cooked_read, regcache_cooked_write);
927 regcache_cooked_write_part (struct regcache *regcache, int regnum,
928 int offset, int len, const void *buf)
930 struct regcache_descr *descr = regcache->descr;
931 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
932 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
933 regcache_cooked_read, regcache_cooked_write);
936 /* Hack to keep code that view the register buffer as raw bytes
940 register_offset_hack (struct gdbarch *gdbarch, int regnum)
942 struct regcache_descr *descr = regcache_descr (gdbarch);
943 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
944 return descr->register_offset[regnum];
947 /* Hack to keep code using register_bytes working. */
950 deprecated_register_bytes (void)
952 return current_regcache->descr->sizeof_raw_registers;
955 /* Return the contents of register REGNUM as an unsigned integer. */
958 read_register (int regnum)
960 char *buf = alloca (register_size (current_gdbarch, regnum));
961 deprecated_read_register_gen (regnum, buf);
962 return (extract_unsigned_integer (buf, register_size (current_gdbarch, regnum)));
966 read_register_pid (int regnum, ptid_t ptid)
972 if (ptid_equal (ptid, inferior_ptid))
973 return read_register (regnum);
975 save_ptid = inferior_ptid;
977 inferior_ptid = ptid;
979 retval = read_register (regnum);
981 inferior_ptid = save_ptid;
986 /* Store VALUE into the raw contents of register number REGNUM. */
989 write_register (int regnum, LONGEST val)
993 size = register_size (current_gdbarch, regnum);
995 store_signed_integer (buf, size, (LONGEST) val);
996 deprecated_write_register_gen (regnum, buf);
1000 write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
1004 if (ptid_equal (ptid, inferior_ptid))
1006 write_register (regnum, val);
1010 save_ptid = inferior_ptid;
1012 inferior_ptid = ptid;
1014 write_register (regnum, val);
1016 inferior_ptid = save_ptid;
1019 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1022 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1027 gdb_assert (regcache != NULL);
1028 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1029 gdb_assert (!regcache->readonly_p);
1031 /* FIXME: kettenis/20030828: It shouldn't be necessary to handle
1032 CURRENT_REGCACHE specially here. */
1033 if (regcache == current_regcache
1034 && !ptid_equal (registers_ptid, inferior_ptid))
1036 registers_changed ();
1037 registers_ptid = inferior_ptid;
1040 regbuf = register_buffer (regcache, regnum);
1041 size = regcache->descr->sizeof_register[regnum];
1044 memcpy (regbuf, buf, size);
1046 memset (regbuf, 0, size);
1048 /* Mark the register as cached. */
1049 regcache->register_valid_p[regnum] = 1;
1052 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1055 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1060 gdb_assert (regcache != NULL && buf != NULL);
1061 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1063 regbuf = register_buffer (regcache, regnum);
1064 size = regcache->descr->sizeof_register[regnum];
1065 memcpy (buf, regbuf, size);
1069 /* read_pc, write_pc, read_sp, etc. Special handling for registers
1072 /* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc() and
1073 read_sp(), will eventually be replaced by per-frame methods.
1074 Instead of relying on the global INFERIOR_PTID, they will use the
1075 contextual information provided by the FRAME. These functions do
1076 not belong in the register cache. */
1078 /* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
1079 write_pc_pid() and write_pc(), all need to be replaced by something
1080 that does not rely on global state. But what? */
1083 read_pc_pid (ptid_t ptid)
1085 ptid_t saved_inferior_ptid;
1088 /* In case ptid != inferior_ptid. */
1089 saved_inferior_ptid = inferior_ptid;
1090 inferior_ptid = ptid;
1092 if (TARGET_READ_PC_P ())
1093 pc_val = TARGET_READ_PC (ptid);
1094 /* Else use per-frame method on get_current_frame. */
1095 else if (PC_REGNUM >= 0)
1097 CORE_ADDR raw_val = read_register_pid (PC_REGNUM, ptid);
1098 pc_val = ADDR_BITS_REMOVE (raw_val);
1101 internal_error (__FILE__, __LINE__, "read_pc_pid: Unable to find PC");
1103 inferior_ptid = saved_inferior_ptid;
1110 return read_pc_pid (inferior_ptid);
1114 generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
1117 write_register_pid (PC_REGNUM, pc, ptid);
1119 internal_error (__FILE__, __LINE__,
1120 "generic_target_write_pc");
1124 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
1126 ptid_t saved_inferior_ptid;
1128 /* In case ptid != inferior_ptid. */
1129 saved_inferior_ptid = inferior_ptid;
1130 inferior_ptid = ptid;
1132 TARGET_WRITE_PC (pc, ptid);
1134 inferior_ptid = saved_inferior_ptid;
1138 write_pc (CORE_ADDR pc)
1140 write_pc_pid (pc, inferior_ptid);
1143 /* Cope with strage ways of getting to the stack and frame pointers */
1148 if (TARGET_READ_SP_P ())
1149 return TARGET_READ_SP ();
1150 else if (gdbarch_unwind_sp_p (current_gdbarch))
1151 return get_frame_sp (get_current_frame ());
1152 else if (SP_REGNUM >= 0)
1153 /* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions
1154 about the architecture so put it at the end. */
1155 return read_register (SP_REGNUM);
1156 internal_error (__FILE__, __LINE__, "read_sp: Unable to find SP");
1160 reg_flush_command (char *command, int from_tty)
1162 /* Force-flush the register cache. */
1163 registers_changed ();
1165 printf_filtered ("Register cache flushed.\n");
1169 build_regcache (void)
1171 current_regcache = regcache_xmalloc (current_gdbarch);
1172 current_regcache->readonly_p = 0;
1173 deprecated_registers = deprecated_grub_regcache_for_registers (current_regcache);
1174 deprecated_register_valid = current_regcache->register_valid_p;
1178 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1179 const unsigned char *buf, long len)
1184 case BFD_ENDIAN_BIG:
1185 for (i = 0; i < len; i++)
1186 fprintf_unfiltered (file, "%02x", buf[i]);
1188 case BFD_ENDIAN_LITTLE:
1189 for (i = len - 1; i >= 0; i--)
1190 fprintf_unfiltered (file, "%02x", buf[i]);
1193 internal_error (__FILE__, __LINE__, "Bad switch");
1197 enum regcache_dump_what
1199 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
1203 regcache_dump (struct regcache *regcache, struct ui_file *file,
1204 enum regcache_dump_what what_to_dump)
1206 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1207 struct gdbarch *gdbarch = regcache->descr->gdbarch;
1209 int footnote_nr = 0;
1210 int footnote_register_size = 0;
1211 int footnote_register_offset = 0;
1212 int footnote_register_type_name_null = 0;
1213 long register_offset = 0;
1214 unsigned char buf[MAX_REGISTER_SIZE];
1217 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1218 regcache->descr->nr_raw_registers);
1219 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1220 regcache->descr->nr_cooked_registers);
1221 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1222 regcache->descr->sizeof_raw_registers);
1223 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1224 regcache->descr->sizeof_raw_register_valid_p);
1225 fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1226 fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1229 gdb_assert (regcache->descr->nr_cooked_registers
1230 == (NUM_REGS + NUM_PSEUDO_REGS));
1232 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1236 fprintf_unfiltered (file, " %-10s", "Name");
1239 const char *p = REGISTER_NAME (regnum);
1242 else if (p[0] == '\0')
1244 fprintf_unfiltered (file, " %-10s", p);
1249 fprintf_unfiltered (file, " %4s", "Nr");
1251 fprintf_unfiltered (file, " %4d", regnum);
1253 /* Relative number. */
1255 fprintf_unfiltered (file, " %4s", "Rel");
1256 else if (regnum < NUM_REGS)
1257 fprintf_unfiltered (file, " %4d", regnum);
1259 fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1263 fprintf_unfiltered (file, " %6s ", "Offset");
1266 fprintf_unfiltered (file, " %6ld",
1267 regcache->descr->register_offset[regnum]);
1268 if (register_offset != regcache->descr->register_offset[regnum]
1269 || register_offset != DEPRECATED_REGISTER_BYTE (regnum)
1271 && (regcache->descr->register_offset[regnum]
1272 != (regcache->descr->register_offset[regnum - 1]
1273 + regcache->descr->sizeof_register[regnum - 1])))
1276 if (!footnote_register_offset)
1277 footnote_register_offset = ++footnote_nr;
1278 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1281 fprintf_unfiltered (file, " ");
1282 register_offset = (regcache->descr->register_offset[regnum]
1283 + regcache->descr->sizeof_register[regnum]);
1288 fprintf_unfiltered (file, " %5s ", "Size");
1290 fprintf_unfiltered (file, " %5ld",
1291 regcache->descr->sizeof_register[regnum]);
1300 static const char blt[] = "builtin_type";
1301 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1305 if (!footnote_register_type_name_null)
1306 footnote_register_type_name_null = ++footnote_nr;
1307 n = xstrprintf ("*%d", footnote_register_type_name_null);
1308 make_cleanup (xfree, n);
1311 /* Chop a leading builtin_type. */
1312 if (strncmp (t, blt, strlen (blt)) == 0)
1315 fprintf_unfiltered (file, " %-15s", t);
1318 /* Leading space always present. */
1319 fprintf_unfiltered (file, " ");
1322 if (what_to_dump == regcache_dump_raw)
1325 fprintf_unfiltered (file, "Raw value");
1326 else if (regnum >= regcache->descr->nr_raw_registers)
1327 fprintf_unfiltered (file, "<cooked>");
1328 else if (!regcache_valid_p (regcache, regnum))
1329 fprintf_unfiltered (file, "<invalid>");
1332 regcache_raw_read (regcache, regnum, buf);
1333 fprintf_unfiltered (file, "0x");
1334 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1335 regcache->descr->sizeof_register[regnum]);
1339 /* Value, cooked. */
1340 if (what_to_dump == regcache_dump_cooked)
1343 fprintf_unfiltered (file, "Cooked value");
1346 regcache_cooked_read (regcache, regnum, buf);
1347 fprintf_unfiltered (file, "0x");
1348 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1349 regcache->descr->sizeof_register[regnum]);
1353 /* Group members. */
1354 if (what_to_dump == regcache_dump_groups)
1357 fprintf_unfiltered (file, "Groups");
1360 const char *sep = "";
1361 struct reggroup *group;
1362 for (group = reggroup_next (gdbarch, NULL);
1364 group = reggroup_next (gdbarch, group))
1366 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1368 fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
1375 fprintf_unfiltered (file, "\n");
1378 if (footnote_register_size)
1379 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1380 footnote_register_size);
1381 if (footnote_register_offset)
1382 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1383 footnote_register_offset);
1384 if (footnote_register_type_name_null)
1385 fprintf_unfiltered (file,
1386 "*%d: Register type's name NULL.\n",
1387 footnote_register_type_name_null);
1388 do_cleanups (cleanups);
1392 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1395 regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1398 struct ui_file *file = gdb_fopen (args, "w");
1400 perror_with_name ("maintenance print architecture");
1401 regcache_dump (current_regcache, file, what_to_dump);
1402 ui_file_delete (file);
1407 maintenance_print_registers (char *args, int from_tty)
1409 regcache_print (args, regcache_dump_none);
1413 maintenance_print_raw_registers (char *args, int from_tty)
1415 regcache_print (args, regcache_dump_raw);
1419 maintenance_print_cooked_registers (char *args, int from_tty)
1421 regcache_print (args, regcache_dump_cooked);
1425 maintenance_print_register_groups (char *args, int from_tty)
1427 regcache_print (args, regcache_dump_groups);
1430 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1433 _initialize_regcache (void)
1435 regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
1436 DEPRECATED_REGISTER_GDBARCH_SWAP (current_regcache);
1437 DEPRECATED_REGISTER_GDBARCH_SWAP (deprecated_registers);
1438 DEPRECATED_REGISTER_GDBARCH_SWAP (deprecated_register_valid);
1439 deprecated_register_gdbarch_swap (NULL, 0, build_regcache);
1441 observer_attach_target_changed (regcache_observer_target_changed);
1443 add_com ("flushregs", class_maintenance, reg_flush_command,
1444 "Force gdb to flush its register cache (maintainer command)");
1446 /* Initialize the thread/process associated with the current set of
1447 registers. For now, -1 is special, and means `no current process'. */
1448 registers_ptid = pid_to_ptid (-1);
1450 add_cmd ("registers", class_maintenance,
1451 maintenance_print_registers,
1452 "Print the internal register configuration.\
1453 Takes an optional file parameter.",
1454 &maintenanceprintlist);
1455 add_cmd ("raw-registers", class_maintenance,
1456 maintenance_print_raw_registers,
1457 "Print the internal register configuration including raw values.\
1458 Takes an optional file parameter.",
1459 &maintenanceprintlist);
1460 add_cmd ("cooked-registers", class_maintenance,
1461 maintenance_print_cooked_registers,
1462 "Print the internal register configuration including cooked values.\
1463 Takes an optional file parameter.",
1464 &maintenanceprintlist);
1465 add_cmd ("register-groups", class_maintenance,
1466 maintenance_print_register_groups,
1467 "Print the internal register configuration including each register's group.\
1468 Takes an optional file parameter.",
1469 &maintenanceprintlist);