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;
147 /* Sanity check. Confirm that there is agreement between the
148 regcache and the target's redundant DEPRECATED_REGISTER_BYTE (new
149 targets should not even be defining it). */
150 for (i = 0; i < descr->nr_cooked_registers; i++)
152 if (DEPRECATED_REGISTER_BYTE_P ())
153 gdb_assert (descr->register_offset[i] == DEPRECATED_REGISTER_BYTE (i));
155 gdb_assert (descr->sizeof_register[i] == DEPRECATED_REGISTER_RAW_SIZE (i));
156 gdb_assert (descr->sizeof_register[i] == DEPRECATED_REGISTER_VIRTUAL_SIZE (i));
159 /* gdb_assert (descr->sizeof_raw_registers == DEPRECATED_REGISTER_BYTES (i)); */
164 static struct regcache_descr *
165 regcache_descr (struct gdbarch *gdbarch)
167 return gdbarch_data (gdbarch, regcache_descr_handle);
170 /* Utility functions returning useful register attributes stored in
171 the regcache descr. */
174 register_type (struct gdbarch *gdbarch, int regnum)
176 struct regcache_descr *descr = regcache_descr (gdbarch);
177 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
178 return descr->register_type[regnum];
181 /* Utility functions returning useful register attributes stored in
182 the regcache descr. */
185 register_size (struct gdbarch *gdbarch, int regnum)
187 struct regcache_descr *descr = regcache_descr (gdbarch);
189 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
190 size = descr->sizeof_register[regnum];
194 /* The register cache for storing raw register values. */
198 struct regcache_descr *descr;
199 /* The register buffers. A read-only register cache can hold the
200 full [0 .. NUM_REGS + NUM_PSEUDO_REGS) while a read/write
201 register cache can only hold [0 .. NUM_REGS). */
203 char *register_valid_p;
204 /* Is this a read-only cache? A read-only cache is used for saving
205 the target's register state (e.g, across an inferior function
206 call or just before forcing a function return). A read-only
207 cache can only be updated via the methods regcache_dup() and
208 regcache_cpy(). The actual contents are determined by the
209 reggroup_save and reggroup_restore methods. */
214 regcache_xmalloc (struct gdbarch *gdbarch)
216 struct regcache_descr *descr;
217 struct regcache *regcache;
218 gdb_assert (gdbarch != NULL);
219 descr = regcache_descr (gdbarch);
220 regcache = XMALLOC (struct regcache);
221 regcache->descr = descr;
223 = XCALLOC (descr->sizeof_raw_registers, char);
224 regcache->register_valid_p
225 = XCALLOC (descr->sizeof_raw_register_valid_p, char);
226 regcache->readonly_p = 1;
231 regcache_xfree (struct regcache *regcache)
233 if (regcache == NULL)
235 xfree (regcache->registers);
236 xfree (regcache->register_valid_p);
241 do_regcache_xfree (void *data)
243 regcache_xfree (data);
247 make_cleanup_regcache_xfree (struct regcache *regcache)
249 return make_cleanup (do_regcache_xfree, regcache);
252 /* Return REGCACHE's architecture. */
255 get_regcache_arch (const struct regcache *regcache)
257 return regcache->descr->gdbarch;
260 /* Return a pointer to register REGNUM's buffer cache. */
263 register_buffer (const struct regcache *regcache, int regnum)
265 return regcache->registers + regcache->descr->register_offset[regnum];
269 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
272 struct gdbarch *gdbarch = dst->descr->gdbarch;
273 char buf[MAX_REGISTER_SIZE];
275 /* The DST should be `read-only', if it wasn't then the save would
276 end up trying to write the register values back out to the
278 gdb_assert (dst->readonly_p);
279 /* Clear the dest. */
280 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
281 memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
282 /* Copy over any registers (identified by their membership in the
283 save_reggroup) and mark them as valid. The full [0 .. NUM_REGS +
284 NUM_PSEUDO_REGS) range is checked since some architectures need
285 to save/restore `cooked' registers that live in memory. */
286 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
288 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
290 int valid = cooked_read (src, regnum, buf);
293 memcpy (register_buffer (dst, regnum), buf,
294 register_size (gdbarch, regnum));
295 dst->register_valid_p[regnum] = 1;
302 regcache_restore (struct regcache *dst,
303 regcache_cooked_read_ftype *cooked_read,
306 struct gdbarch *gdbarch = dst->descr->gdbarch;
307 char buf[MAX_REGISTER_SIZE];
309 /* The dst had better not be read-only. If it is, the `restore'
310 doesn't make much sense. */
311 gdb_assert (!dst->readonly_p);
312 /* Copy over any registers, being careful to only restore those that
313 were both saved and need to be restored. The full [0 .. NUM_REGS
314 + NUM_PSEUDO_REGS) range is checked since some architectures need
315 to save/restore `cooked' registers that live in memory. */
316 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
318 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
320 int valid = cooked_read (src, regnum, buf);
322 regcache_cooked_write (dst, regnum, buf);
328 do_cooked_read (void *src, int regnum, void *buf)
330 struct regcache *regcache = src;
331 if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
332 /* Don't even think about fetching a register from a read-only
333 cache when the register isn't yet valid. There isn't a target
334 from which the register value can be fetched. */
336 regcache_cooked_read (regcache, regnum, buf);
342 regcache_cpy (struct regcache *dst, struct regcache *src)
346 gdb_assert (src != NULL && dst != NULL);
347 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
348 gdb_assert (src != dst);
349 gdb_assert (src->readonly_p || dst->readonly_p);
350 if (!src->readonly_p)
351 regcache_save (dst, do_cooked_read, src);
352 else if (!dst->readonly_p)
353 regcache_restore (dst, do_cooked_read, src);
355 regcache_cpy_no_passthrough (dst, src);
359 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
362 gdb_assert (src != NULL && dst != NULL);
363 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
364 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
365 move of data into the current_regcache(). Doing this would be
366 silly - it would mean that valid_p would be completely invalid. */
367 gdb_assert (dst != current_regcache);
368 memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
369 memcpy (dst->register_valid_p, src->register_valid_p,
370 dst->descr->sizeof_raw_register_valid_p);
374 regcache_dup (struct regcache *src)
376 struct regcache *newbuf;
377 gdb_assert (current_regcache != NULL);
378 newbuf = regcache_xmalloc (src->descr->gdbarch);
379 regcache_cpy (newbuf, src);
384 regcache_dup_no_passthrough (struct regcache *src)
386 struct regcache *newbuf;
387 gdb_assert (current_regcache != NULL);
388 newbuf = regcache_xmalloc (src->descr->gdbarch);
389 regcache_cpy_no_passthrough (newbuf, src);
394 regcache_valid_p (struct regcache *regcache, int regnum)
396 gdb_assert (regcache != NULL);
397 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
398 return regcache->register_valid_p[regnum];
402 deprecated_grub_regcache_for_registers (struct regcache *regcache)
404 return regcache->registers;
407 /* Global structure containing the current regcache. */
408 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
409 deprecated_register_valid[] currently point into this structure. */
410 struct regcache *current_regcache;
412 /* NOTE: this is a write-through cache. There is no "dirty" bit for
413 recording if the register values have been changed (eg. by the
414 user). Therefore all registers must be written back to the
415 target when appropriate. */
417 /* REGISTERS contains the cached register values (in target byte order). */
419 char *deprecated_registers;
421 /* DEPRECATED_REGISTER_VALID is 0 if the register needs to be fetched,
422 1 if it has been fetched, and
423 -1 if the register value was not available.
425 "Not available" indicates that the target is not not able to supply
426 the register at this state. The register may become available at a
427 later time (after the next resume). This often occures when GDB is
428 manipulating a target that contains only a snapshot of the entire
429 system being debugged - some of the registers in such a system may
430 not have been saved. */
432 signed char *deprecated_register_valid;
434 /* The thread/process associated with the current set of registers. */
436 static ptid_t registers_ptid;
444 Returns 0 if the value is not in the cache (needs fetch).
445 >0 if the value is in the cache.
446 <0 if the value is permanently unavailable (don't ask again). */
449 register_cached (int regnum)
451 return deprecated_register_valid[regnum];
454 /* Record that REGNUM's value is cached if STATE is >0, uncached but
455 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
458 set_register_cached (int regnum, int state)
460 gdb_assert (regnum >= 0);
461 gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
462 current_regcache->register_valid_p[regnum] = state;
465 /* Observer for the target_changed event. */
468 regcache_observer_target_changed (struct target_ops *target)
470 registers_changed ();
473 /* Low level examining and depositing of registers.
475 The caller is responsible for making sure that the inferior is
476 stopped before calling the fetching routines, or it will get
477 garbage. (a change from GDB version 3, in which the caller got the
478 value from the last stop). */
480 /* REGISTERS_CHANGED ()
482 Indicate that registers may have changed, so invalidate the cache. */
485 registers_changed (void)
489 registers_ptid = pid_to_ptid (-1);
491 /* Force cleanup of any alloca areas if using C alloca instead of
492 a builtin alloca. This particular call is used to clean up
493 areas allocated by low level target code which may build up
494 during lengthy interactions between gdb and the target before
495 gdb gives control to the user (ie watchpoints). */
498 for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
499 set_register_cached (i, 0);
501 if (deprecated_registers_changed_hook)
502 deprecated_registers_changed_hook ();
505 /* DEPRECATED_REGISTERS_FETCHED ()
507 Indicate that all registers have been fetched, so mark them all valid. */
509 /* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
510 code was blatting the registers[] array and then calling this.
511 Since targets should only be using regcache_raw_supply() the need for
512 this function/hack is eliminated. */
515 deprecated_registers_fetched (void)
519 for (i = 0; i < NUM_REGS; i++)
520 set_register_cached (i, 1);
521 /* Do not assume that the pseudo-regs have also been fetched.
522 Fetching all real regs NEVER accounts for pseudo-regs. */
525 /* deprecated_read_register_bytes and deprecated_write_register_bytes
526 are generally a *BAD* idea. They are inefficient because they need
527 to check for partial updates, which can only be done by scanning
528 through all of the registers and seeing if the bytes that are being
529 read/written fall inside of an invalid register. [The main reason
530 this is necessary is that register sizes can vary, so a simple
531 index won't suffice.] It is far better to call read_register_gen
532 and write_register_gen if you want to get at the raw register
533 contents, as it only takes a regnum as an argument, and therefore
534 can't do a partial register update.
536 Prior to the recent fixes to check for partial updates, both read
537 and deprecated_write_register_bytes always checked to see if any
538 registers were stale, and then called target_fetch_registers (-1)
539 to update the whole set. This caused really slowed things down for
542 /* Copy INLEN bytes of consecutive data from registers
543 starting with the INREGBYTE'th byte of register data
544 into memory at MYADDR. */
547 deprecated_read_register_bytes (int in_start, char *in_buf, int in_len)
549 int in_end = in_start + in_len;
551 char reg_buf[MAX_REGISTER_SIZE];
553 /* See if we are trying to read bytes from out-of-date registers. If so,
554 update just those registers. */
556 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
565 reg_start = DEPRECATED_REGISTER_BYTE (regnum);
566 reg_len = DEPRECATED_REGISTER_RAW_SIZE (regnum);
567 reg_end = reg_start + reg_len;
569 if (reg_end <= in_start || in_end <= reg_start)
570 /* The range the user wants to read doesn't overlap with regnum. */
573 if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0')
574 /* Force the cache to fetch the entire register. */
575 deprecated_read_register_gen (regnum, reg_buf);
577 /* Legacy note: even though this register is ``invalid'' we
578 still need to return something. It would appear that some
579 code relies on apparent gaps in the register array also
581 /* FIXME: cagney/2001-08-18: This is just silly. It defeats
582 the entire register read/write flow of control. Must
583 resist temptation to return 0xdeadbeef. */
584 memcpy (reg_buf, &deprecated_registers[reg_start], reg_len);
586 /* Legacy note: This function, for some reason, allows a NULL
587 input buffer. If the buffer is NULL, the registers are still
588 fetched, just the final transfer is skipped. */
592 /* start = max (reg_start, in_start) */
593 if (reg_start > in_start)
598 /* end = min (reg_end, in_end) */
599 if (reg_end < in_end)
604 /* Transfer just the bytes common to both IN_BUF and REG_BUF */
605 for (byte = start; byte < end; byte++)
607 in_buf[byte - in_start] = reg_buf[byte - reg_start];
613 regcache_raw_read (struct regcache *regcache, int regnum, void *buf)
615 gdb_assert (regcache != NULL && buf != NULL);
616 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
617 /* Make certain that the register cache is up-to-date with respect
618 to the current thread. This switching shouldn't be necessary
619 only there is still only one target side register cache. Sigh!
620 On the bright side, at least there is a regcache object. */
621 if (!regcache->readonly_p)
623 gdb_assert (regcache == current_regcache);
624 if (! ptid_equal (registers_ptid, inferior_ptid))
626 registers_changed ();
627 registers_ptid = inferior_ptid;
629 if (!register_cached (regnum))
630 target_fetch_registers (regnum);
632 /* Copy the value directly into the register cache. */
633 memcpy (buf, register_buffer (regcache, regnum),
634 regcache->descr->sizeof_register[regnum]);
638 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
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_signed_integer (buf,
646 regcache->descr->sizeof_register[regnum]);
650 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
654 gdb_assert (regcache != NULL);
655 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
656 buf = alloca (regcache->descr->sizeof_register[regnum]);
657 regcache_raw_read (regcache, regnum, buf);
658 (*val) = extract_unsigned_integer (buf,
659 regcache->descr->sizeof_register[regnum]);
663 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
666 gdb_assert (regcache != NULL);
667 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
668 buf = alloca (regcache->descr->sizeof_register[regnum]);
669 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
670 regcache_raw_write (regcache, regnum, buf);
674 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
678 gdb_assert (regcache != NULL);
679 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
680 buf = alloca (regcache->descr->sizeof_register[regnum]);
681 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
682 regcache_raw_write (regcache, regnum, buf);
686 deprecated_read_register_gen (int regnum, char *buf)
688 gdb_assert (current_regcache != NULL);
689 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
690 regcache_cooked_read (current_regcache, regnum, buf);
694 regcache_cooked_read (struct regcache *regcache, int regnum, void *buf)
696 gdb_assert (regnum >= 0);
697 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
698 if (regnum < regcache->descr->nr_raw_registers)
699 regcache_raw_read (regcache, regnum, buf);
700 else if (regcache->readonly_p
701 && regnum < regcache->descr->nr_cooked_registers
702 && regcache->register_valid_p[regnum])
703 /* Read-only register cache, perhaps the cooked value was cached? */
704 memcpy (buf, register_buffer (regcache, regnum),
705 regcache->descr->sizeof_register[regnum]);
707 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
712 regcache_cooked_read_signed (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_signed_integer (buf,
721 regcache->descr->sizeof_register[regnum]);
725 regcache_cooked_read_unsigned (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 regcache_cooked_read (regcache, regnum, buf);
733 (*val) = extract_unsigned_integer (buf,
734 regcache->descr->sizeof_register[regnum]);
738 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
742 gdb_assert (regcache != NULL);
743 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
744 buf = alloca (regcache->descr->sizeof_register[regnum]);
745 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
746 regcache_cooked_write (regcache, regnum, buf);
750 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
754 gdb_assert (regcache != NULL);
755 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
756 buf = alloca (regcache->descr->sizeof_register[regnum]);
757 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
758 regcache_cooked_write (regcache, regnum, buf);
762 regcache_raw_write (struct regcache *regcache, int regnum, const void *buf)
764 gdb_assert (regcache != NULL && buf != NULL);
765 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
766 gdb_assert (!regcache->readonly_p);
768 /* On the sparc, writing %g0 is a no-op, so we don't even want to
769 change the registers array if something writes to this register. */
770 if (CANNOT_STORE_REGISTER (regnum))
773 /* Make certain that the correct cache is selected. */
774 gdb_assert (regcache == current_regcache);
775 if (! ptid_equal (registers_ptid, inferior_ptid))
777 registers_changed ();
778 registers_ptid = inferior_ptid;
781 /* If we have a valid copy of the register, and new value == old
782 value, then don't bother doing the actual store. */
783 if (regcache_valid_p (regcache, regnum)
784 && (memcmp (register_buffer (regcache, regnum), buf,
785 regcache->descr->sizeof_register[regnum]) == 0))
788 target_prepare_to_store ();
789 memcpy (register_buffer (regcache, regnum), buf,
790 regcache->descr->sizeof_register[regnum]);
791 regcache->register_valid_p[regnum] = 1;
792 target_store_registers (regnum);
796 deprecated_write_register_gen (int regnum, char *buf)
798 gdb_assert (current_regcache != NULL);
799 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
800 regcache_cooked_write (current_regcache, regnum, buf);
804 regcache_cooked_write (struct regcache *regcache, int regnum, const void *buf)
806 gdb_assert (regnum >= 0);
807 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
808 if (regnum < regcache->descr->nr_raw_registers)
809 regcache_raw_write (regcache, regnum, buf);
811 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
815 /* Copy INLEN bytes of consecutive data from memory at MYADDR
816 into registers starting with the MYREGSTART'th byte of register data. */
819 deprecated_write_register_bytes (int myregstart, char *myaddr, int inlen)
821 int myregend = myregstart + inlen;
824 target_prepare_to_store ();
826 /* Scan through the registers updating any that are covered by the
827 range myregstart<=>myregend using write_register_gen, which does
828 nice things like handling threads, and avoiding updates when the
829 new and old contents are the same. */
831 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
833 int regstart, regend;
835 regstart = DEPRECATED_REGISTER_BYTE (regnum);
836 regend = regstart + DEPRECATED_REGISTER_RAW_SIZE (regnum);
838 /* Is this register completely outside the range the user is writing? */
839 if (myregend <= regstart || regend <= myregstart)
842 /* Is this register completely within the range the user is writing? */
843 else if (myregstart <= regstart && regend <= myregend)
844 deprecated_write_register_gen (regnum, myaddr + (regstart - myregstart));
846 /* The register partially overlaps the range being written. */
849 char regbuf[MAX_REGISTER_SIZE];
850 /* What's the overlap between this register's bytes and
851 those the caller wants to write? */
852 int overlapstart = max (regstart, myregstart);
853 int overlapend = min (regend, myregend);
855 /* We may be doing a partial update of an invalid register.
856 Update it from the target before scribbling on it. */
857 deprecated_read_register_gen (regnum, regbuf);
859 memcpy (&deprecated_registers[overlapstart],
860 myaddr + (overlapstart - myregstart),
861 overlapend - overlapstart);
863 target_store_registers (regnum);
868 /* Perform a partial register transfer using a read, modify, write
871 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
873 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
877 regcache_xfer_part (struct regcache *regcache, int regnum,
878 int offset, int len, void *in, const void *out,
879 regcache_read_ftype *read, regcache_write_ftype *write)
881 struct regcache_descr *descr = regcache->descr;
882 bfd_byte reg[MAX_REGISTER_SIZE];
883 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
884 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
885 /* Something to do? */
886 if (offset + len == 0)
888 /* Read (when needed) ... */
891 || offset + len < descr->sizeof_register[regnum])
893 gdb_assert (read != NULL);
894 read (regcache, regnum, reg);
898 memcpy (in, reg + offset, len);
900 memcpy (reg + offset, out, len);
901 /* ... write (when needed). */
904 gdb_assert (write != NULL);
905 write (regcache, regnum, reg);
910 regcache_raw_read_part (struct regcache *regcache, int regnum,
911 int offset, int len, void *buf)
913 struct regcache_descr *descr = regcache->descr;
914 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
915 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
916 regcache_raw_read, regcache_raw_write);
920 regcache_raw_write_part (struct regcache *regcache, int regnum,
921 int offset, int len, const void *buf)
923 struct regcache_descr *descr = regcache->descr;
924 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
925 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
926 regcache_raw_read, regcache_raw_write);
930 regcache_cooked_read_part (struct regcache *regcache, int regnum,
931 int offset, int len, void *buf)
933 struct regcache_descr *descr = regcache->descr;
934 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
935 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
936 regcache_cooked_read, regcache_cooked_write);
940 regcache_cooked_write_part (struct regcache *regcache, int regnum,
941 int offset, int len, const void *buf)
943 struct regcache_descr *descr = regcache->descr;
944 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
945 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
946 regcache_cooked_read, regcache_cooked_write);
949 /* Hack to keep code that view the register buffer as raw bytes
953 register_offset_hack (struct gdbarch *gdbarch, int regnum)
955 struct regcache_descr *descr = regcache_descr (gdbarch);
956 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
957 return descr->register_offset[regnum];
960 /* Return the contents of register REGNUM as an unsigned integer. */
963 read_register (int regnum)
965 char *buf = alloca (DEPRECATED_REGISTER_RAW_SIZE (regnum));
966 deprecated_read_register_gen (regnum, buf);
967 return (extract_unsigned_integer (buf, DEPRECATED_REGISTER_RAW_SIZE (regnum)));
971 read_register_pid (int regnum, ptid_t ptid)
977 if (ptid_equal (ptid, inferior_ptid))
978 return read_register (regnum);
980 save_ptid = inferior_ptid;
982 inferior_ptid = ptid;
984 retval = read_register (regnum);
986 inferior_ptid = save_ptid;
991 /* Store VALUE into the raw contents of register number REGNUM. */
994 write_register (int regnum, LONGEST val)
998 size = DEPRECATED_REGISTER_RAW_SIZE (regnum);
1000 store_signed_integer (buf, size, (LONGEST) val);
1001 deprecated_write_register_gen (regnum, buf);
1005 write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
1009 if (ptid_equal (ptid, inferior_ptid))
1011 write_register (regnum, val);
1015 save_ptid = inferior_ptid;
1017 inferior_ptid = ptid;
1019 write_register (regnum, val);
1021 inferior_ptid = save_ptid;
1024 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1027 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1032 gdb_assert (regcache != NULL);
1033 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1034 gdb_assert (!regcache->readonly_p);
1036 /* FIXME: kettenis/20030828: It shouldn't be necessary to handle
1037 CURRENT_REGCACHE specially here. */
1038 if (regcache == current_regcache
1039 && !ptid_equal (registers_ptid, inferior_ptid))
1041 registers_changed ();
1042 registers_ptid = inferior_ptid;
1045 regbuf = register_buffer (regcache, regnum);
1046 size = regcache->descr->sizeof_register[regnum];
1049 memcpy (regbuf, buf, size);
1051 memset (regbuf, 0, size);
1053 /* Mark the register as cached. */
1054 regcache->register_valid_p[regnum] = 1;
1057 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1060 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1065 gdb_assert (regcache != NULL && buf != NULL);
1066 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1068 regbuf = register_buffer (regcache, regnum);
1069 size = regcache->descr->sizeof_register[regnum];
1070 memcpy (buf, regbuf, size);
1074 /* read_pc, write_pc, read_sp, deprecated_read_fp, etc. Special
1075 handling for registers PC, SP, and FP. */
1077 /* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc(),
1078 read_sp(), and deprecated_read_fp(), will eventually be replaced by
1079 per-frame methods. Instead of relying on the global INFERIOR_PTID,
1080 they will use the contextual information provided by the FRAME.
1081 These functions do not belong in the register cache. */
1083 /* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
1084 write_pc_pid(), write_pc(), and deprecated_read_fp(), all need to
1085 be replaced by something that does not rely on global state. But
1089 read_pc_pid (ptid_t ptid)
1091 ptid_t saved_inferior_ptid;
1094 /* In case ptid != inferior_ptid. */
1095 saved_inferior_ptid = inferior_ptid;
1096 inferior_ptid = ptid;
1098 if (TARGET_READ_PC_P ())
1099 pc_val = TARGET_READ_PC (ptid);
1100 /* Else use per-frame method on get_current_frame. */
1101 else if (PC_REGNUM >= 0)
1103 CORE_ADDR raw_val = read_register_pid (PC_REGNUM, ptid);
1104 pc_val = ADDR_BITS_REMOVE (raw_val);
1107 internal_error (__FILE__, __LINE__, "read_pc_pid: Unable to find PC");
1109 inferior_ptid = saved_inferior_ptid;
1116 return read_pc_pid (inferior_ptid);
1120 generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
1123 write_register_pid (PC_REGNUM, pc, ptid);
1125 internal_error (__FILE__, __LINE__,
1126 "generic_target_write_pc");
1130 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
1132 ptid_t saved_inferior_ptid;
1134 /* In case ptid != inferior_ptid. */
1135 saved_inferior_ptid = inferior_ptid;
1136 inferior_ptid = ptid;
1138 TARGET_WRITE_PC (pc, ptid);
1140 inferior_ptid = saved_inferior_ptid;
1144 write_pc (CORE_ADDR pc)
1146 write_pc_pid (pc, inferior_ptid);
1149 /* Cope with strage ways of getting to the stack and frame pointers */
1154 if (TARGET_READ_SP_P ())
1155 return TARGET_READ_SP ();
1156 else if (gdbarch_unwind_sp_p (current_gdbarch))
1157 return get_frame_sp (get_current_frame ());
1158 else if (SP_REGNUM >= 0)
1159 /* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions
1160 about the architecture so put it at the end. */
1161 return read_register (SP_REGNUM);
1162 internal_error (__FILE__, __LINE__, "read_sp: Unable to find SP");
1166 deprecated_write_sp (CORE_ADDR val)
1168 gdb_assert (SP_REGNUM >= 0);
1169 write_register (SP_REGNUM, val);
1173 deprecated_read_fp (void)
1175 if (DEPRECATED_TARGET_READ_FP_P ())
1176 return DEPRECATED_TARGET_READ_FP ();
1177 else if (DEPRECATED_FP_REGNUM >= 0)
1178 return read_register (DEPRECATED_FP_REGNUM);
1180 internal_error (__FILE__, __LINE__, "deprecated_read_fp");
1184 reg_flush_command (char *command, int from_tty)
1186 /* Force-flush the register cache. */
1187 registers_changed ();
1189 printf_filtered ("Register cache flushed.\n");
1193 build_regcache (void)
1195 current_regcache = regcache_xmalloc (current_gdbarch);
1196 current_regcache->readonly_p = 0;
1197 deprecated_registers = deprecated_grub_regcache_for_registers (current_regcache);
1198 deprecated_register_valid = current_regcache->register_valid_p;
1202 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1203 const unsigned char *buf, long len)
1208 case BFD_ENDIAN_BIG:
1209 for (i = 0; i < len; i++)
1210 fprintf_unfiltered (file, "%02x", buf[i]);
1212 case BFD_ENDIAN_LITTLE:
1213 for (i = len - 1; i >= 0; i--)
1214 fprintf_unfiltered (file, "%02x", buf[i]);
1217 internal_error (__FILE__, __LINE__, "Bad switch");
1221 enum regcache_dump_what
1223 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
1227 regcache_dump (struct regcache *regcache, struct ui_file *file,
1228 enum regcache_dump_what what_to_dump)
1230 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1231 struct gdbarch *gdbarch = regcache->descr->gdbarch;
1233 int footnote_nr = 0;
1234 int footnote_register_size = 0;
1235 int footnote_register_offset = 0;
1236 int footnote_register_type_name_null = 0;
1237 long register_offset = 0;
1238 unsigned char buf[MAX_REGISTER_SIZE];
1241 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1242 regcache->descr->nr_raw_registers);
1243 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1244 regcache->descr->nr_cooked_registers);
1245 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1246 regcache->descr->sizeof_raw_registers);
1247 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1248 regcache->descr->sizeof_raw_register_valid_p);
1249 fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1250 fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1253 gdb_assert (regcache->descr->nr_cooked_registers
1254 == (NUM_REGS + NUM_PSEUDO_REGS));
1256 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1260 fprintf_unfiltered (file, " %-10s", "Name");
1263 const char *p = REGISTER_NAME (regnum);
1266 else if (p[0] == '\0')
1268 fprintf_unfiltered (file, " %-10s", p);
1273 fprintf_unfiltered (file, " %4s", "Nr");
1275 fprintf_unfiltered (file, " %4d", regnum);
1277 /* Relative number. */
1279 fprintf_unfiltered (file, " %4s", "Rel");
1280 else if (regnum < NUM_REGS)
1281 fprintf_unfiltered (file, " %4d", regnum);
1283 fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1287 fprintf_unfiltered (file, " %6s ", "Offset");
1290 fprintf_unfiltered (file, " %6ld",
1291 regcache->descr->register_offset[regnum]);
1292 if (register_offset != regcache->descr->register_offset[regnum]
1293 || register_offset != DEPRECATED_REGISTER_BYTE (regnum)
1295 && (regcache->descr->register_offset[regnum]
1296 != (regcache->descr->register_offset[regnum - 1]
1297 + regcache->descr->sizeof_register[regnum - 1])))
1300 if (!footnote_register_offset)
1301 footnote_register_offset = ++footnote_nr;
1302 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1305 fprintf_unfiltered (file, " ");
1306 register_offset = (regcache->descr->register_offset[regnum]
1307 + regcache->descr->sizeof_register[regnum]);
1312 fprintf_unfiltered (file, " %5s ", "Size");
1315 fprintf_unfiltered (file, " %5ld",
1316 regcache->descr->sizeof_register[regnum]);
1317 if ((regcache->descr->sizeof_register[regnum]
1318 != DEPRECATED_REGISTER_RAW_SIZE (regnum))
1319 || (regcache->descr->sizeof_register[regnum]
1320 != DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum))
1321 || (regcache->descr->sizeof_register[regnum]
1322 != TYPE_LENGTH (register_type (regcache->descr->gdbarch,
1326 if (!footnote_register_size)
1327 footnote_register_size = ++footnote_nr;
1328 fprintf_unfiltered (file, "*%d", footnote_register_size);
1331 fprintf_unfiltered (file, " ");
1341 static const char blt[] = "builtin_type";
1342 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1346 if (!footnote_register_type_name_null)
1347 footnote_register_type_name_null = ++footnote_nr;
1348 n = xstrprintf ("*%d", footnote_register_type_name_null);
1349 make_cleanup (xfree, n);
1352 /* Chop a leading builtin_type. */
1353 if (strncmp (t, blt, strlen (blt)) == 0)
1356 fprintf_unfiltered (file, " %-15s", t);
1359 /* Leading space always present. */
1360 fprintf_unfiltered (file, " ");
1363 if (what_to_dump == regcache_dump_raw)
1366 fprintf_unfiltered (file, "Raw value");
1367 else if (regnum >= regcache->descr->nr_raw_registers)
1368 fprintf_unfiltered (file, "<cooked>");
1369 else if (!regcache_valid_p (regcache, regnum))
1370 fprintf_unfiltered (file, "<invalid>");
1373 regcache_raw_read (regcache, regnum, buf);
1374 fprintf_unfiltered (file, "0x");
1375 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1376 DEPRECATED_REGISTER_RAW_SIZE (regnum));
1380 /* Value, cooked. */
1381 if (what_to_dump == regcache_dump_cooked)
1384 fprintf_unfiltered (file, "Cooked value");
1387 regcache_cooked_read (regcache, regnum, buf);
1388 fprintf_unfiltered (file, "0x");
1389 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1390 DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum));
1394 /* Group members. */
1395 if (what_to_dump == regcache_dump_groups)
1398 fprintf_unfiltered (file, "Groups");
1401 const char *sep = "";
1402 struct reggroup *group;
1403 for (group = reggroup_next (gdbarch, NULL);
1405 group = reggroup_next (gdbarch, group))
1407 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1409 fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
1416 fprintf_unfiltered (file, "\n");
1419 if (footnote_register_size)
1420 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1421 footnote_register_size);
1422 if (footnote_register_offset)
1423 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1424 footnote_register_offset);
1425 if (footnote_register_type_name_null)
1426 fprintf_unfiltered (file,
1427 "*%d: Register type's name NULL.\n",
1428 footnote_register_type_name_null);
1429 do_cleanups (cleanups);
1433 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1436 regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1439 struct ui_file *file = gdb_fopen (args, "w");
1441 perror_with_name ("maintenance print architecture");
1442 regcache_dump (current_regcache, file, what_to_dump);
1443 ui_file_delete (file);
1448 maintenance_print_registers (char *args, int from_tty)
1450 regcache_print (args, regcache_dump_none);
1454 maintenance_print_raw_registers (char *args, int from_tty)
1456 regcache_print (args, regcache_dump_raw);
1460 maintenance_print_cooked_registers (char *args, int from_tty)
1462 regcache_print (args, regcache_dump_cooked);
1466 maintenance_print_register_groups (char *args, int from_tty)
1468 regcache_print (args, regcache_dump_groups);
1471 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1474 _initialize_regcache (void)
1476 regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
1477 DEPRECATED_REGISTER_GDBARCH_SWAP (current_regcache);
1478 DEPRECATED_REGISTER_GDBARCH_SWAP (deprecated_registers);
1479 DEPRECATED_REGISTER_GDBARCH_SWAP (deprecated_register_valid);
1480 deprecated_register_gdbarch_swap (NULL, 0, build_regcache);
1482 observer_attach_target_changed (regcache_observer_target_changed);
1484 add_com ("flushregs", class_maintenance, reg_flush_command,
1485 "Force gdb to flush its register cache (maintainer command)");
1487 /* Initialize the thread/process associated with the current set of
1488 registers. For now, -1 is special, and means `no current process'. */
1489 registers_ptid = pid_to_ptid (-1);
1491 add_cmd ("registers", class_maintenance,
1492 maintenance_print_registers,
1493 "Print the internal register configuration.\
1494 Takes an optional file parameter.",
1495 &maintenanceprintlist);
1496 add_cmd ("raw-registers", class_maintenance,
1497 maintenance_print_raw_registers,
1498 "Print the internal register configuration including raw values.\
1499 Takes an optional file parameter.",
1500 &maintenanceprintlist);
1501 add_cmd ("cooked-registers", class_maintenance,
1502 maintenance_print_cooked_registers,
1503 "Print the internal register configuration including cooked values.\
1504 Takes an optional file parameter.",
1505 &maintenanceprintlist);
1506 add_cmd ("register-groups", class_maintenance,
1507 maintenance_print_register_groups,
1508 "Print the internal register configuration including each register's group.\
1509 Takes an optional file parameter.",
1510 &maintenanceprintlist);