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 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 "gdb_assert.h"
30 #include "gdb_string.h"
31 #include "gdbcmd.h" /* For maintenanceprintlist. */
36 * Here is the actual register cache.
39 /* Per-architecture object describing the layout of a register cache.
40 Computed once when the architecture is created */
42 struct gdbarch_data *regcache_descr_handle;
46 /* The architecture this descriptor belongs to. */
47 struct gdbarch *gdbarch;
49 /* Is this a ``legacy'' register cache? Such caches reserve space
50 for raw and pseudo registers and allow access to both. */
53 /* The raw register cache. This should contain just [0
54 .. NUM_RAW_REGISTERS). However, for older targets, it contains
55 space for the full [0 .. NUM_RAW_REGISTERS +
56 NUM_PSEUDO_REGISTERS). */
58 long sizeof_raw_registers;
59 long sizeof_raw_register_valid_p;
61 /* The cooked register space. Each cooked register in the range
62 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
63 register. The remaining [NR_RAW_REGISTERS
64 .. NR_COOKED_REGISTERS) (a.k.a. pseudo regiters) are mapped onto
65 both raw registers and memory by the architecture methods
66 gdbarch_register_read and gdbarch_register_write. */
67 int nr_cooked_registers;
69 /* Offset and size (in 8 bit bytes), of reach register in the
70 register cache. All registers (including those in the range
71 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
72 Assigning all registers an offset makes it possible to keep
73 legacy code, such as that found in read_register_bytes() and
74 write_register_bytes() working. */
75 long *register_offset;
76 long *sizeof_register;
78 /* Useful constant. Largest of all the registers. */
79 long max_register_size;
83 init_legacy_regcache_descr (struct gdbarch *gdbarch)
86 struct regcache_descr *descr;
87 /* FIXME: cagney/2002-05-11: gdbarch_data() should take that
88 ``gdbarch'' as a parameter. */
89 gdb_assert (gdbarch != NULL);
91 descr = XMALLOC (struct regcache_descr);
92 descr->gdbarch = gdbarch;
95 /* FIXME: cagney/2002-05-11: Shouldn't be including pseudo-registers
96 in the register buffer. Unfortunatly some architectures do. */
97 descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS;
98 descr->nr_raw_registers = descr->nr_cooked_registers;
99 descr->sizeof_raw_register_valid_p = descr->nr_cooked_registers;
101 /* FIXME: cagney/2002-05-11: Instead of using REGISTER_BYTE() this
102 code should compute the offets et.al. at runtime. This currently
103 isn't possible because some targets overlap register locations -
104 see the mess in read_register_bytes() and write_register_bytes()
106 descr->sizeof_register = XCALLOC (descr->nr_cooked_registers, long);
107 descr->register_offset = XCALLOC (descr->nr_cooked_registers, long);
108 descr->max_register_size = 0;
109 for (i = 0; i < descr->nr_cooked_registers; i++)
111 descr->register_offset[i] = REGISTER_BYTE (i);
112 descr->sizeof_register[i] = REGISTER_RAW_SIZE (i);
113 if (descr->max_register_size < REGISTER_RAW_SIZE (i))
114 descr->max_register_size = REGISTER_RAW_SIZE (i);
115 if (descr->max_register_size < REGISTER_VIRTUAL_SIZE (i))
116 descr->max_register_size = REGISTER_VIRTUAL_SIZE (i);
119 /* Come up with the real size of the registers buffer. */
120 descr->sizeof_raw_registers = REGISTER_BYTES; /* OK use. */
121 for (i = 0; i < descr->nr_cooked_registers; i++)
124 /* Keep extending the buffer so that there is always enough
125 space for all registers. The comparison is necessary since
126 legacy code is free to put registers in random places in the
127 buffer separated by holes. Once REGISTER_BYTE() is killed
128 this can be greatly simplified. */
129 /* FIXME: cagney/2001-12-04: This code shouldn't need to use
130 REGISTER_BYTE(). Unfortunatly, legacy code likes to lay the
131 buffer out so that certain registers just happen to overlap.
132 Ulgh! New targets use gdbarch's register read/write and
133 entirely avoid this uglyness. */
134 regend = descr->register_offset[i] + descr->sizeof_register[i];
135 if (descr->sizeof_raw_registers < regend)
136 descr->sizeof_raw_registers = regend;
142 init_regcache_descr (struct gdbarch *gdbarch)
145 struct regcache_descr *descr;
146 gdb_assert (gdbarch != NULL);
148 /* If an old style architecture, construct the register cache
149 description using all the register macros. */
150 if (!gdbarch_pseudo_register_read_p (gdbarch)
151 && !gdbarch_pseudo_register_write_p (gdbarch))
152 return init_legacy_regcache_descr (gdbarch);
154 descr = XMALLOC (struct regcache_descr);
155 descr->gdbarch = gdbarch;
158 /* Total size of the register space. The raw registers are mapped
159 directly onto the raw register cache while the pseudo's are
160 either mapped onto raw-registers or memory. */
161 descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS;
163 /* Construct a strictly RAW register cache. Don't allow pseudo's
164 into the register cache. */
165 descr->nr_raw_registers = NUM_REGS;
167 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
168 array. This pretects GDB from erant code that accesses elements
169 of the global register_valid_p[] array in the range [NUM_REGS
170 .. NUM_REGS + NUM_PSEUDO_REGS). */
171 descr->sizeof_raw_register_valid_p = NUM_REGS + NUM_PSEUDO_REGS;
173 /* Lay out the register cache. The pseud-registers are included in
174 the layout even though their value isn't stored in the register
175 cache. Some code, via read_register_bytes() access a register
176 using an offset/length rather than a register number.
178 NOTE: cagney/2002-05-22: Only REGISTER_VIRTUAL_TYPE() needs to be
179 used when constructing the register cache. It is assumed that
180 register raw size, virtual size and type length of the type are
185 descr->sizeof_register = XCALLOC (descr->nr_cooked_registers, long);
186 descr->register_offset = XCALLOC (descr->nr_cooked_registers, long);
187 descr->max_register_size = 0;
188 for (i = 0; i < descr->nr_cooked_registers; i++)
190 descr->sizeof_register[i] = TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (i));
191 descr->register_offset[i] = offset;
192 offset += descr->sizeof_register[i];
193 if (descr->max_register_size < descr->sizeof_register[i])
194 descr->max_register_size = descr->sizeof_register[i];
196 /* Set the real size of the register cache buffer. */
197 /* FIXME: cagney/2002-05-22: Should only need to allocate space
198 for the raw registers. Unfortunatly some code still accesses
199 the register array directly using the global registers[].
200 Until that code has been purged, play safe and over allocating
201 the register buffer. Ulgh! */
202 descr->sizeof_raw_registers = offset;
203 /* = descr->register_offset[descr->nr_raw_registers]; */
207 /* Sanity check. Confirm that the assumptions about gdbarch are
208 true. The REGCACHE_DESCR_HANDLE is set before doing the checks
209 so that targets using the generic methods supplied by regcache
210 don't go into infinite recursion trying to, again, create the
212 set_gdbarch_data (gdbarch, regcache_descr_handle, descr);
213 for (i = 0; i < descr->nr_cooked_registers; i++)
215 gdb_assert (descr->sizeof_register[i] == REGISTER_RAW_SIZE (i));
216 gdb_assert (descr->sizeof_register[i] == REGISTER_VIRTUAL_SIZE (i));
217 gdb_assert (descr->register_offset[i] == REGISTER_BYTE (i));
219 /* gdb_assert (descr->sizeof_raw_registers == REGISTER_BYTES (i)); */
225 static struct regcache_descr *
226 regcache_descr (struct gdbarch *gdbarch)
228 return gdbarch_data (gdbarch, regcache_descr_handle);
232 xfree_regcache_descr (struct gdbarch *gdbarch, void *ptr)
234 struct regcache_descr *descr = ptr;
237 xfree (descr->register_offset);
238 xfree (descr->sizeof_register);
239 descr->register_offset = NULL;
240 descr->sizeof_register = NULL;
244 /* Utility functions returning useful register attributes stored in
245 the regcache descr. */
248 max_register_size (struct gdbarch *gdbarch)
250 struct regcache_descr *descr = regcache_descr (gdbarch);
251 return descr->max_register_size;
254 /* The register cache for storing raw register values. */
258 struct regcache_descr *descr;
260 char *raw_register_valid_p;
261 /* If a value isn't in the cache should the corresponding target be
262 queried for a value. */
267 regcache_xmalloc (struct gdbarch *gdbarch)
269 struct regcache_descr *descr;
270 struct regcache *regcache;
271 gdb_assert (gdbarch != NULL);
272 descr = regcache_descr (gdbarch);
273 regcache = XMALLOC (struct regcache);
274 regcache->descr = descr;
275 regcache->raw_registers
276 = XCALLOC (descr->sizeof_raw_registers, char);
277 regcache->raw_register_valid_p
278 = XCALLOC (descr->sizeof_raw_register_valid_p, char);
279 regcache->passthrough_p = 0;
284 regcache_xfree (struct regcache *regcache)
286 if (regcache == NULL)
288 xfree (regcache->raw_registers);
289 xfree (regcache->raw_register_valid_p);
294 do_regcache_xfree (void *data)
296 regcache_xfree (data);
300 make_cleanup_regcache_xfree (struct regcache *regcache)
302 return make_cleanup (do_regcache_xfree, regcache);
306 regcache_cpy (struct regcache *dst, struct regcache *src)
310 gdb_assert (src != NULL && dst != NULL);
311 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
312 gdb_assert (src != dst);
313 /* FIXME: cagney/2002-05-17: To say this bit is bad is being polite.
314 It keeps the existing code working where things rely on going
315 through to the register cache. */
316 if (src == current_regcache && src->descr->legacy_p)
318 /* ULGH!!!! Old way. Use REGISTER bytes and let code below
320 read_register_bytes (0, dst->raw_registers, REGISTER_BYTES);
323 /* FIXME: cagney/2002-05-17: To say this bit is bad is being polite.
324 It keeps the existing code working where things rely on going
325 through to the register cache. */
326 if (dst == current_regcache && dst->descr->legacy_p)
328 /* ULGH!!!! Old way. Use REGISTER bytes and let code below
330 write_register_bytes (0, src->raw_registers, REGISTER_BYTES);
333 buf = alloca (src->descr->max_register_size);
334 for (i = 0; i < src->descr->nr_raw_registers; i++)
336 /* Should we worry about the valid bit here? */
337 regcache_raw_read (src, i, buf);
338 regcache_raw_write (dst, i, buf);
343 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
346 gdb_assert (src != NULL && dst != NULL);
347 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
348 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
349 move of data into the current_regcache(). Doing this would be
350 silly - it would mean that valid_p would be completly invalid. */
351 gdb_assert (dst != current_regcache);
352 memcpy (dst->raw_registers, src->raw_registers,
353 dst->descr->sizeof_raw_registers);
354 memcpy (dst->raw_register_valid_p, src->raw_register_valid_p,
355 dst->descr->sizeof_raw_register_valid_p);
359 regcache_dup (struct regcache *src)
361 struct regcache *newbuf;
362 gdb_assert (current_regcache != NULL);
363 newbuf = regcache_xmalloc (src->descr->gdbarch);
364 regcache_cpy (newbuf, src);
369 regcache_dup_no_passthrough (struct regcache *src)
371 struct regcache *newbuf;
372 gdb_assert (current_regcache != NULL);
373 newbuf = regcache_xmalloc (src->descr->gdbarch);
374 regcache_cpy_no_passthrough (newbuf, src);
379 regcache_valid_p (struct regcache *regcache, int regnum)
381 gdb_assert (regcache != NULL);
382 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
383 return regcache->raw_register_valid_p[regnum];
387 deprecated_grub_regcache_for_registers (struct regcache *regcache)
389 return regcache->raw_registers;
393 deprecated_grub_regcache_for_register_valid (struct regcache *regcache)
395 return regcache->raw_register_valid_p;
398 /* Global structure containing the current regcache. */
399 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
400 register_valid[] currently point into this structure. */
401 struct regcache *current_regcache;
403 /* NOTE: this is a write-through cache. There is no "dirty" bit for
404 recording if the register values have been changed (eg. by the
405 user). Therefore all registers must be written back to the
406 target when appropriate. */
408 /* REGISTERS contains the cached register values (in target byte order). */
412 /* REGISTER_VALID is 0 if the register needs to be fetched,
413 1 if it has been fetched, and
414 -1 if the register value was not available.
416 "Not available" indicates that the target is not not able to supply
417 the register at this state. The register may become available at a
418 later time (after the next resume). This often occures when GDB is
419 manipulating a target that contains only a snapshot of the entire
420 system being debugged - some of the registers in such a system may
421 not have been saved. */
423 signed char *register_valid;
425 /* The thread/process associated with the current set of registers. */
427 static ptid_t registers_ptid;
435 Returns 0 if the value is not in the cache (needs fetch).
436 >0 if the value is in the cache.
437 <0 if the value is permanently unavailable (don't ask again). */
440 register_cached (int regnum)
442 return register_valid[regnum];
445 /* Record that REGNUM's value is cached if STATE is >0, uncached but
446 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
449 set_register_cached (int regnum, int state)
451 gdb_assert (regnum >= 0);
452 gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
453 current_regcache->raw_register_valid_p[regnum] = state;
458 invalidate a single register REGNUM in the cache */
460 register_changed (int regnum)
462 set_register_cached (regnum, 0);
465 /* If REGNUM >= 0, return a pointer to register REGNUM's cache buffer area,
466 else return a pointer to the start of the cache buffer. */
469 register_buffer (struct regcache *regcache, int regnum)
471 return regcache->raw_registers + regcache->descr->register_offset[regnum];
474 /* Return whether register REGNUM is a real register. */
477 real_register (int regnum)
479 return regnum >= 0 && regnum < NUM_REGS;
482 /* Low level examining and depositing of registers.
484 The caller is responsible for making sure that the inferior is
485 stopped before calling the fetching routines, or it will get
486 garbage. (a change from GDB version 3, in which the caller got the
487 value from the last stop). */
489 /* REGISTERS_CHANGED ()
491 Indicate that registers may have changed, so invalidate the cache. */
494 registers_changed (void)
498 registers_ptid = pid_to_ptid (-1);
500 /* Force cleanup of any alloca areas if using C alloca instead of
501 a builtin alloca. This particular call is used to clean up
502 areas allocated by low level target code which may build up
503 during lengthy interactions between gdb and the target before
504 gdb gives control to the user (ie watchpoints). */
507 for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
508 set_register_cached (i, 0);
510 if (registers_changed_hook)
511 registers_changed_hook ();
514 /* REGISTERS_FETCHED ()
516 Indicate that all registers have been fetched, so mark them all valid. */
518 /* NOTE: cagney/2001-12-04: This function does not set valid on the
519 pseudo-register range since pseudo registers are always supplied
520 using supply_register(). */
521 /* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
522 code was blatting the registers[] array and then calling this.
523 Since targets should only be using supply_register() the need for
524 this function/hack is eliminated. */
527 registers_fetched (void)
531 for (i = 0; i < NUM_REGS; i++)
532 set_register_cached (i, 1);
533 /* Do not assume that the pseudo-regs have also been fetched.
534 Fetching all real regs NEVER accounts for pseudo-regs. */
537 /* read_register_bytes and write_register_bytes are generally a *BAD*
538 idea. They are inefficient because they need to check for partial
539 updates, which can only be done by scanning through all of the
540 registers and seeing if the bytes that are being read/written fall
541 inside of an invalid register. [The main reason this is necessary
542 is that register sizes can vary, so a simple index won't suffice.]
543 It is far better to call read_register_gen and write_register_gen
544 if you want to get at the raw register contents, as it only takes a
545 regnum as an argument, and therefore can't do a partial register
548 Prior to the recent fixes to check for partial updates, both read
549 and write_register_bytes always checked to see if any registers
550 were stale, and then called target_fetch_registers (-1) to update
551 the whole set. This caused really slowed things down for remote
554 /* Copy INLEN bytes of consecutive data from registers
555 starting with the INREGBYTE'th byte of register data
556 into memory at MYADDR. */
559 read_register_bytes (int in_start, char *in_buf, int in_len)
561 int in_end = in_start + in_len;
563 char *reg_buf = alloca (MAX_REGISTER_RAW_SIZE);
565 /* See if we are trying to read bytes from out-of-date registers. If so,
566 update just those registers. */
568 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
577 reg_start = REGISTER_BYTE (regnum);
578 reg_len = REGISTER_RAW_SIZE (regnum);
579 reg_end = reg_start + reg_len;
581 if (reg_end <= in_start || in_end <= reg_start)
582 /* The range the user wants to read doesn't overlap with regnum. */
585 if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0')
586 /* Force the cache to fetch the entire register. */
587 read_register_gen (regnum, reg_buf);
589 /* Legacy note: even though this register is ``invalid'' we
590 still need to return something. It would appear that some
591 code relies on apparent gaps in the register array also
593 /* FIXME: cagney/2001-08-18: This is just silly. It defeats
594 the entire register read/write flow of control. Must
595 resist temptation to return 0xdeadbeef. */
596 memcpy (reg_buf, registers + reg_start, reg_len);
598 /* Legacy note: This function, for some reason, allows a NULL
599 input buffer. If the buffer is NULL, the registers are still
600 fetched, just the final transfer is skipped. */
604 /* start = max (reg_start, in_start) */
605 if (reg_start > in_start)
610 /* end = min (reg_end, in_end) */
611 if (reg_end < in_end)
616 /* Transfer just the bytes common to both IN_BUF and REG_BUF */
617 for (byte = start; byte < end; byte++)
619 in_buf[byte - in_start] = reg_buf[byte - reg_start];
624 /* Read register REGNUM into memory at MYADDR, which must be large
625 enough for REGISTER_RAW_BYTES (REGNUM). Target byte-order. If the
626 register is known to be the size of a CORE_ADDR or smaller,
627 read_register can be used instead. */
630 legacy_read_register_gen (int regnum, char *myaddr)
632 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
633 if (! ptid_equal (registers_ptid, inferior_ptid))
635 registers_changed ();
636 registers_ptid = inferior_ptid;
639 if (!register_cached (regnum))
640 target_fetch_registers (regnum);
642 memcpy (myaddr, register_buffer (current_regcache, regnum),
643 REGISTER_RAW_SIZE (regnum));
647 regcache_raw_read (struct regcache *regcache, int regnum, void *buf)
649 gdb_assert (regcache != NULL && buf != NULL);
650 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
651 if (regcache->descr->legacy_p
652 && regcache->passthrough_p)
654 gdb_assert (regcache == current_regcache);
655 /* For moment, just use underlying legacy code. Ulgh!!! This
656 silently and very indirectly updates the regcache's regcache
657 via the global register_valid[]. */
658 legacy_read_register_gen (regnum, buf);
661 /* Make certain that the register cache is up-to-date with respect
662 to the current thread. This switching shouldn't be necessary
663 only there is still only one target side register cache. Sigh!
664 On the bright side, at least there is a regcache object. */
665 if (regcache->passthrough_p)
667 gdb_assert (regcache == current_regcache);
668 if (! ptid_equal (registers_ptid, inferior_ptid))
670 registers_changed ();
671 registers_ptid = inferior_ptid;
673 if (!register_cached (regnum))
674 target_fetch_registers (regnum);
676 /* Copy the value directly into the register cache. */
677 memcpy (buf, (regcache->raw_registers
678 + regcache->descr->register_offset[regnum]),
679 regcache->descr->sizeof_register[regnum]);
683 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
686 gdb_assert (regcache != NULL);
687 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
688 buf = alloca (regcache->descr->sizeof_register[regnum]);
689 regcache_raw_read (regcache, regnum, buf);
690 (*val) = extract_signed_integer (buf,
691 regcache->descr->sizeof_register[regnum]);
695 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
699 gdb_assert (regcache != NULL);
700 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
701 buf = alloca (regcache->descr->sizeof_register[regnum]);
702 regcache_raw_read (regcache, regnum, buf);
703 (*val) = extract_unsigned_integer (buf,
704 regcache->descr->sizeof_register[regnum]);
708 read_register_gen (int regnum, char *buf)
710 gdb_assert (current_regcache != NULL);
711 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
712 if (current_regcache->descr->legacy_p)
714 legacy_read_register_gen (regnum, buf);
717 regcache_cooked_read (current_regcache, regnum, buf);
721 regcache_cooked_read (struct regcache *regcache, int regnum, void *buf)
723 gdb_assert (regnum >= 0);
724 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
725 if (regnum < regcache->descr->nr_raw_registers)
726 regcache_raw_read (regcache, regnum, buf);
728 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
733 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
737 gdb_assert (regcache != NULL);
738 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
739 buf = alloca (regcache->descr->sizeof_register[regnum]);
740 regcache_cooked_read (regcache, regnum, buf);
741 (*val) = extract_signed_integer (buf,
742 regcache->descr->sizeof_register[regnum]);
746 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
750 gdb_assert (regcache != NULL);
751 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
752 buf = alloca (regcache->descr->sizeof_register[regnum]);
753 regcache_cooked_read (regcache, regnum, buf);
754 (*val) = extract_unsigned_integer (buf,
755 regcache->descr->sizeof_register[regnum]);
758 /* Write register REGNUM at MYADDR to the target. MYADDR points at
759 REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order. */
762 legacy_write_register_gen (int regnum, const void *myaddr)
765 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
767 /* On the sparc, writing %g0 is a no-op, so we don't even want to
768 change the registers array if something writes to this register. */
769 if (CANNOT_STORE_REGISTER (regnum))
772 if (! ptid_equal (registers_ptid, inferior_ptid))
774 registers_changed ();
775 registers_ptid = inferior_ptid;
778 size = REGISTER_RAW_SIZE (regnum);
780 if (real_register (regnum))
782 /* If we have a valid copy of the register, and new value == old
783 value, then don't bother doing the actual store. */
784 if (register_cached (regnum)
785 && (memcmp (register_buffer (current_regcache, regnum), myaddr, size)
789 target_prepare_to_store ();
792 memcpy (register_buffer (current_regcache, regnum), myaddr, size);
794 set_register_cached (regnum, 1);
795 target_store_registers (regnum);
799 regcache_raw_write (struct regcache *regcache, int regnum, const void *buf)
801 gdb_assert (regcache != NULL && buf != NULL);
802 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
804 if (regcache->passthrough_p
805 && regcache->descr->legacy_p)
807 /* For moment, just use underlying legacy code. Ulgh!!! This
808 silently and very indirectly updates the regcache's buffers
809 via the globals register_valid[] and registers[]. */
810 gdb_assert (regcache == current_regcache);
811 legacy_write_register_gen (regnum, buf);
815 /* On the sparc, writing %g0 is a no-op, so we don't even want to
816 change the registers array if something writes to this register. */
817 if (CANNOT_STORE_REGISTER (regnum))
820 /* Handle the simple case first -> not write through so just store
822 if (!regcache->passthrough_p)
824 memcpy ((regcache->raw_registers
825 + regcache->descr->register_offset[regnum]), buf,
826 regcache->descr->sizeof_register[regnum]);
827 regcache->raw_register_valid_p[regnum] = 1;
831 /* Make certain that the correct cache is selected. */
832 gdb_assert (regcache == current_regcache);
833 if (! ptid_equal (registers_ptid, inferior_ptid))
835 registers_changed ();
836 registers_ptid = inferior_ptid;
839 /* If we have a valid copy of the register, and new value == old
840 value, then don't bother doing the actual store. */
841 if (regcache_valid_p (regcache, regnum)
842 && (memcmp (register_buffer (regcache, regnum), buf,
843 regcache->descr->sizeof_register[regnum]) == 0))
846 target_prepare_to_store ();
847 memcpy (register_buffer (regcache, regnum), buf,
848 regcache->descr->sizeof_register[regnum]);
849 regcache->raw_register_valid_p[regnum] = 1;
850 target_store_registers (regnum);
854 write_register_gen (int regnum, char *buf)
856 gdb_assert (current_regcache != NULL);
857 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
858 if (current_regcache->descr->legacy_p)
860 legacy_write_register_gen (regnum, buf);
863 regcache_cooked_write (current_regcache, regnum, buf);
867 regcache_cooked_write (struct regcache *regcache, int regnum, const void *buf)
869 gdb_assert (regnum >= 0);
870 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
871 if (regnum < regcache->descr->nr_raw_registers)
872 regcache_raw_write (regcache, regnum, buf);
874 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
878 /* Copy INLEN bytes of consecutive data from memory at MYADDR
879 into registers starting with the MYREGSTART'th byte of register data. */
882 write_register_bytes (int myregstart, char *myaddr, int inlen)
884 int myregend = myregstart + inlen;
887 target_prepare_to_store ();
889 /* Scan through the registers updating any that are covered by the
890 range myregstart<=>myregend using write_register_gen, which does
891 nice things like handling threads, and avoiding updates when the
892 new and old contents are the same. */
894 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
896 int regstart, regend;
898 regstart = REGISTER_BYTE (regnum);
899 regend = regstart + REGISTER_RAW_SIZE (regnum);
901 /* Is this register completely outside the range the user is writing? */
902 if (myregend <= regstart || regend <= myregstart)
905 /* Is this register completely within the range the user is writing? */
906 else if (myregstart <= regstart && regend <= myregend)
907 write_register_gen (regnum, myaddr + (regstart - myregstart));
909 /* The register partially overlaps the range being written. */
912 char *regbuf = (char*) alloca (MAX_REGISTER_RAW_SIZE);
913 /* What's the overlap between this register's bytes and
914 those the caller wants to write? */
915 int overlapstart = max (regstart, myregstart);
916 int overlapend = min (regend, myregend);
918 /* We may be doing a partial update of an invalid register.
919 Update it from the target before scribbling on it. */
920 read_register_gen (regnum, regbuf);
922 memcpy (registers + overlapstart,
923 myaddr + (overlapstart - myregstart),
924 overlapend - overlapstart);
926 target_store_registers (regnum);
931 /* Perform a partial register transfer using a read, modify, write
934 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
936 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
940 regcache_xfer_part (struct regcache *regcache, int regnum,
941 int offset, int len, void *in, const void *out,
942 regcache_read_ftype *read, regcache_write_ftype *write)
944 struct regcache_descr *descr = regcache->descr;
945 bfd_byte *reg = alloca (descr->max_register_size);
946 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
947 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
948 /* Something to do? */
949 if (offset + len == 0)
951 /* Read (when needed) ... */
954 || offset + len < descr->sizeof_register[regnum])
956 gdb_assert (read != NULL);
957 read (regcache, regnum, reg);
961 memcpy (in, reg + offset, len);
963 memcpy (reg + offset, out, len);
964 /* ... write (when needed). */
967 gdb_assert (write != NULL);
968 write (regcache, regnum, reg);
973 regcache_raw_read_part (struct regcache *regcache, int regnum,
974 int offset, int len, void *buf)
976 struct regcache_descr *descr = regcache->descr;
977 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
978 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
979 regcache_raw_read, regcache_raw_write);
983 regcache_raw_write_part (struct regcache *regcache, int regnum,
984 int offset, int len, const void *buf)
986 struct regcache_descr *descr = regcache->descr;
987 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
988 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
989 regcache_raw_read, regcache_raw_write);
993 regcache_cooked_read_part (struct regcache *regcache, int regnum,
994 int offset, int len, void *buf)
996 struct regcache_descr *descr = regcache->descr;
997 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
998 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
999 regcache_cooked_read, regcache_cooked_write);
1003 regcache_cooked_write_part (struct regcache *regcache, int regnum,
1004 int offset, int len, const void *buf)
1006 struct regcache_descr *descr = regcache->descr;
1007 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1008 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1009 regcache_cooked_read, regcache_cooked_write);
1012 /* Return the contents of register REGNUM as an unsigned integer. */
1015 read_register (int regnum)
1017 char *buf = alloca (REGISTER_RAW_SIZE (regnum));
1018 read_register_gen (regnum, buf);
1019 return (extract_unsigned_integer (buf, REGISTER_RAW_SIZE (regnum)));
1023 read_register_pid (int regnum, ptid_t ptid)
1029 if (ptid_equal (ptid, inferior_ptid))
1030 return read_register (regnum);
1032 save_ptid = inferior_ptid;
1034 inferior_ptid = ptid;
1036 retval = read_register (regnum);
1038 inferior_ptid = save_ptid;
1043 /* Return the contents of register REGNUM as a signed integer. */
1046 read_signed_register (int regnum)
1048 void *buf = alloca (REGISTER_RAW_SIZE (regnum));
1049 read_register_gen (regnum, buf);
1050 return (extract_signed_integer (buf, REGISTER_RAW_SIZE (regnum)));
1054 read_signed_register_pid (int regnum, ptid_t ptid)
1059 if (ptid_equal (ptid, inferior_ptid))
1060 return read_signed_register (regnum);
1062 save_ptid = inferior_ptid;
1064 inferior_ptid = ptid;
1066 retval = read_signed_register (regnum);
1068 inferior_ptid = save_ptid;
1073 /* Store VALUE into the raw contents of register number REGNUM. */
1076 write_register (int regnum, LONGEST val)
1080 size = REGISTER_RAW_SIZE (regnum);
1081 buf = alloca (size);
1082 store_signed_integer (buf, size, (LONGEST) val);
1083 write_register_gen (regnum, buf);
1087 write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
1091 if (ptid_equal (ptid, inferior_ptid))
1093 write_register (regnum, val);
1097 save_ptid = inferior_ptid;
1099 inferior_ptid = ptid;
1101 write_register (regnum, val);
1103 inferior_ptid = save_ptid;
1106 /* SUPPLY_REGISTER()
1108 Record that register REGNUM contains VAL. This is used when the
1109 value is obtained from the inferior or core dump, so there is no
1110 need to store the value there.
1112 If VAL is a NULL pointer, then it's probably an unsupported register.
1113 We just set its value to all zeros. We might want to record this
1114 fact, and report it to the users of read_register and friends. */
1117 supply_register (int regnum, const void *val)
1120 if (! ptid_equal (registers_ptid, inferior_ptid))
1122 registers_changed ();
1123 registers_ptid = inferior_ptid;
1127 set_register_cached (regnum, 1);
1129 memcpy (register_buffer (current_regcache, regnum), val,
1130 REGISTER_RAW_SIZE (regnum));
1132 memset (register_buffer (current_regcache, regnum), '\000',
1133 REGISTER_RAW_SIZE (regnum));
1135 /* On some architectures, e.g. HPPA, there are a few stray bits in
1136 some registers, that the rest of the code would like to ignore. */
1138 /* NOTE: cagney/2001-03-16: The macro CLEAN_UP_REGISTER_VALUE is
1139 going to be deprecated. Instead architectures will leave the raw
1140 register value as is and instead clean things up as they pass
1141 through the method gdbarch_pseudo_register_read() clean up the
1144 #ifdef DEPRECATED_CLEAN_UP_REGISTER_VALUE
1145 DEPRECATED_CLEAN_UP_REGISTER_VALUE \
1146 (regnum, register_buffer (current_regcache, regnum));
1151 regcache_collect (int regnum, void *buf)
1153 memcpy (buf, register_buffer (current_regcache, regnum),
1154 REGISTER_RAW_SIZE (regnum));
1158 /* read_pc, write_pc, read_sp, write_sp, read_fp, etc. Special
1159 handling for registers PC, SP, and FP. */
1161 /* NOTE: cagney/2001-02-18: The functions generic_target_read_pc(),
1162 read_pc_pid(), read_pc(), generic_target_write_pc(),
1163 write_pc_pid(), write_pc(), generic_target_read_sp(), read_sp(),
1164 generic_target_write_sp(), write_sp(), generic_target_read_fp() and
1165 read_fp(), will eventually be moved out of the reg-cache into
1166 either frame.[hc] or to the multi-arch framework. The are not part
1167 of the raw register cache. */
1169 /* This routine is getting awfully cluttered with #if's. It's probably
1170 time to turn this into READ_PC and define it in the tm.h file.
1173 1999-06-08: The following were re-written so that it assumes the
1174 existence of a TARGET_READ_PC et.al. macro. A default generic
1175 version of that macro is made available where needed.
1177 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
1178 by the multi-arch framework, it will eventually be possible to
1179 eliminate the intermediate read_pc_pid(). The client would call
1180 TARGET_READ_PC directly. (cagney). */
1183 generic_target_read_pc (ptid_t ptid)
1188 CORE_ADDR pc_val = ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, ptid));
1192 internal_error (__FILE__, __LINE__,
1193 "generic_target_read_pc");
1198 read_pc_pid (ptid_t ptid)
1200 ptid_t saved_inferior_ptid;
1203 /* In case ptid != inferior_ptid. */
1204 saved_inferior_ptid = inferior_ptid;
1205 inferior_ptid = ptid;
1207 pc_val = TARGET_READ_PC (ptid);
1209 inferior_ptid = saved_inferior_ptid;
1216 return read_pc_pid (inferior_ptid);
1220 generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
1224 write_register_pid (PC_REGNUM, pc, ptid);
1225 if (NPC_REGNUM >= 0)
1226 write_register_pid (NPC_REGNUM, pc + 4, ptid);
1228 internal_error (__FILE__, __LINE__,
1229 "generic_target_write_pc");
1234 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
1236 ptid_t saved_inferior_ptid;
1238 /* In case ptid != inferior_ptid. */
1239 saved_inferior_ptid = inferior_ptid;
1240 inferior_ptid = ptid;
1242 TARGET_WRITE_PC (pc, ptid);
1244 inferior_ptid = saved_inferior_ptid;
1248 write_pc (CORE_ADDR pc)
1250 write_pc_pid (pc, inferior_ptid);
1253 /* Cope with strage ways of getting to the stack and frame pointers */
1256 generic_target_read_sp (void)
1260 return read_register (SP_REGNUM);
1262 internal_error (__FILE__, __LINE__,
1263 "generic_target_read_sp");
1269 return TARGET_READ_SP ();
1273 generic_target_write_sp (CORE_ADDR val)
1278 write_register (SP_REGNUM, val);
1282 internal_error (__FILE__, __LINE__,
1283 "generic_target_write_sp");
1287 write_sp (CORE_ADDR val)
1289 TARGET_WRITE_SP (val);
1293 generic_target_read_fp (void)
1297 return read_register (FP_REGNUM);
1299 internal_error (__FILE__, __LINE__,
1300 "generic_target_read_fp");
1306 return TARGET_READ_FP ();
1311 reg_flush_command (char *command, int from_tty)
1313 /* Force-flush the register cache. */
1314 registers_changed ();
1316 printf_filtered ("Register cache flushed.\n");
1320 build_regcache (void)
1322 current_regcache = regcache_xmalloc (current_gdbarch);
1323 current_regcache->passthrough_p = 1;
1324 registers = deprecated_grub_regcache_for_registers (current_regcache);
1325 register_valid = deprecated_grub_regcache_for_register_valid (current_regcache);
1329 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1330 const unsigned char *buf, long len)
1335 case BFD_ENDIAN_BIG:
1336 for (i = 0; i < len; i++)
1337 fprintf_unfiltered (file, "%02x", buf[i]);
1339 case BFD_ENDIAN_LITTLE:
1340 for (i = len - 1; i >= 0; i--)
1341 fprintf_unfiltered (file, "%02x", buf[i]);
1344 internal_error (__FILE__, __LINE__, "Bad switch");
1348 enum regcache_dump_what
1350 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked
1354 regcache_dump (struct regcache *regcache, struct ui_file *file,
1355 enum regcache_dump_what what_to_dump)
1357 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1359 int footnote_nr = 0;
1360 int footnote_register_size = 0;
1361 int footnote_register_offset = 0;
1362 int footnote_register_type_name_null = 0;
1363 long register_offset = 0;
1364 unsigned char *buf = alloca (regcache->descr->max_register_size);
1367 fprintf_unfiltered (file, "legacy_p %d\n", regcache->descr->legacy_p);
1368 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1369 regcache->descr->nr_raw_registers);
1370 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1371 regcache->descr->nr_cooked_registers);
1372 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1373 regcache->descr->sizeof_raw_registers);
1374 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1375 regcache->descr->sizeof_raw_register_valid_p);
1376 fprintf_unfiltered (file, "max_register_size %ld\n",
1377 regcache->descr->max_register_size);
1378 fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1379 fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1382 gdb_assert (regcache->descr->nr_cooked_registers
1383 == (NUM_REGS + NUM_PSEUDO_REGS));
1385 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1389 fprintf_unfiltered (file, " %-10s", "Name");
1392 const char *p = REGISTER_NAME (regnum);
1395 else if (p[0] == '\0')
1397 fprintf_unfiltered (file, " %-10s", p);
1402 fprintf_unfiltered (file, " %4s", "Nr");
1404 fprintf_unfiltered (file, " %4d", regnum);
1406 /* Relative number. */
1408 fprintf_unfiltered (file, " %4s", "Rel");
1409 else if (regnum < NUM_REGS)
1410 fprintf_unfiltered (file, " %4d", regnum);
1412 fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1416 fprintf_unfiltered (file, " %6s ", "Offset");
1419 fprintf_unfiltered (file, " %6ld",
1420 regcache->descr->register_offset[regnum]);
1421 if (register_offset != regcache->descr->register_offset[regnum]
1422 || register_offset != REGISTER_BYTE (regnum))
1424 if (!footnote_register_offset)
1425 footnote_register_offset = ++footnote_nr;
1426 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1429 fprintf_unfiltered (file, " ");
1430 register_offset = (regcache->descr->register_offset[regnum]
1431 + regcache->descr->sizeof_register[regnum]);
1436 fprintf_unfiltered (file, " %5s ", "Size");
1439 fprintf_unfiltered (file, " %5ld",
1440 regcache->descr->sizeof_register[regnum]);
1441 if ((regcache->descr->sizeof_register[regnum]
1442 != REGISTER_RAW_SIZE (regnum))
1443 || (regcache->descr->sizeof_register[regnum]
1444 != REGISTER_VIRTUAL_SIZE (regnum))
1445 || (regcache->descr->sizeof_register[regnum]
1446 != TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (regnum)))
1449 if (!footnote_register_size)
1450 footnote_register_size = ++footnote_nr;
1451 fprintf_unfiltered (file, "*%d", footnote_register_size);
1454 fprintf_unfiltered (file, " ");
1459 fprintf_unfiltered (file, " %-20s", "Type");
1462 static const char blt[] = "builtin_type";
1463 const char *t = TYPE_NAME (REGISTER_VIRTUAL_TYPE (regnum));
1467 if (!footnote_register_type_name_null)
1468 footnote_register_type_name_null = ++footnote_nr;
1469 xasprintf (&n, "*%d", footnote_register_type_name_null);
1470 make_cleanup (xfree, n);
1473 /* Chop a leading builtin_type. */
1474 if (strncmp (t, blt, strlen (blt)) == 0)
1476 fprintf_unfiltered (file, " %-20s", t);
1480 if (what_to_dump == regcache_dump_raw)
1483 fprintf_unfiltered (file, "Raw value");
1484 else if (regnum >= regcache->descr->nr_raw_registers)
1485 fprintf_unfiltered (file, "<cooked>");
1486 else if (!regcache_valid_p (regcache, regnum))
1487 fprintf_unfiltered (file, "<invalid>");
1490 regcache_raw_read (regcache, regnum, buf);
1491 fprintf_unfiltered (file, "0x");
1492 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1493 REGISTER_RAW_SIZE (regnum));
1497 /* Value, cooked. */
1498 if (what_to_dump == regcache_dump_cooked)
1501 fprintf_unfiltered (file, "Cooked value");
1504 regcache_cooked_read (regcache, regnum, buf);
1505 fprintf_unfiltered (file, "0x");
1506 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1507 REGISTER_VIRTUAL_SIZE (regnum));
1511 fprintf_unfiltered (file, "\n");
1514 if (footnote_register_size)
1515 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1516 footnote_register_size);
1517 if (footnote_register_offset)
1518 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1519 footnote_register_offset);
1520 if (footnote_register_type_name_null)
1521 fprintf_unfiltered (file,
1522 "*%d: Register type's name NULL.\n",
1523 footnote_register_type_name_null);
1524 do_cleanups (cleanups);
1528 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1531 regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1534 struct ui_file *file = gdb_fopen (args, "w");
1536 perror_with_name ("maintenance print architecture");
1537 regcache_dump (current_regcache, file, what_to_dump);
1538 ui_file_delete (file);
1543 maintenance_print_registers (char *args, int from_tty)
1545 regcache_print (args, regcache_dump_none);
1549 maintenance_print_raw_registers (char *args, int from_tty)
1551 regcache_print (args, regcache_dump_raw);
1555 maintenance_print_cooked_registers (char *args, int from_tty)
1557 regcache_print (args, regcache_dump_cooked);
1561 _initialize_regcache (void)
1563 regcache_descr_handle = register_gdbarch_data (init_regcache_descr,
1564 xfree_regcache_descr);
1565 REGISTER_GDBARCH_SWAP (current_regcache);
1566 register_gdbarch_swap (®isters, sizeof (registers), NULL);
1567 register_gdbarch_swap (®ister_valid, sizeof (register_valid), NULL);
1568 register_gdbarch_swap (NULL, 0, build_regcache);
1570 add_com ("flushregs", class_maintenance, reg_flush_command,
1571 "Force gdb to flush its register cache (maintainer command)");
1573 /* Initialize the thread/process associated with the current set of
1574 registers. For now, -1 is special, and means `no current process'. */
1575 registers_ptid = pid_to_ptid (-1);
1577 add_cmd ("registers", class_maintenance,
1578 maintenance_print_registers,
1579 "Print the internal register configuration.\
1580 Takes an optional file parameter.",
1581 &maintenanceprintlist);
1582 add_cmd ("raw-registers", class_maintenance,
1583 maintenance_print_raw_registers,
1584 "Print the internal register configuration including raw values.\
1585 Takes an optional file parameter.",
1586 &maintenanceprintlist);
1587 add_cmd ("cooked-registers", class_maintenance,
1588 maintenance_print_cooked_registers,
1589 "Print the internal register configuration including cooked values.\
1590 Takes an optional file parameter.",
1591 &maintenanceprintlist);