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);
117 /* Come up with the real size of the registers buffer. */
118 descr->sizeof_raw_registers = REGISTER_BYTES; /* OK use. */
119 for (i = 0; i < descr->nr_cooked_registers; i++)
122 /* Keep extending the buffer so that there is always enough
123 space for all registers. The comparison is necessary since
124 legacy code is free to put registers in random places in the
125 buffer separated by holes. Once REGISTER_BYTE() is killed
126 this can be greatly simplified. */
127 /* FIXME: cagney/2001-12-04: This code shouldn't need to use
128 REGISTER_BYTE(). Unfortunatly, legacy code likes to lay the
129 buffer out so that certain registers just happen to overlap.
130 Ulgh! New targets use gdbarch's register read/write and
131 entirely avoid this uglyness. */
132 regend = descr->register_offset[i] + descr->sizeof_register[i];
133 if (descr->sizeof_raw_registers < regend)
134 descr->sizeof_raw_registers = regend;
140 init_regcache_descr (struct gdbarch *gdbarch)
143 struct regcache_descr *descr;
144 gdb_assert (gdbarch != NULL);
146 /* If an old style architecture, construct the register cache
147 description using all the register macros. */
148 if (!gdbarch_pseudo_register_read_p (gdbarch)
149 && !gdbarch_pseudo_register_write_p (gdbarch))
150 return init_legacy_regcache_descr (gdbarch);
152 descr = XMALLOC (struct regcache_descr);
153 descr->gdbarch = gdbarch;
156 /* Total size of the register space. The raw registers are mapped
157 directly onto the raw register cache while the pseudo's are
158 either mapped onto raw-registers or memory. */
159 descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS;
161 /* Construct a strictly RAW register cache. Don't allow pseudo's
162 into the register cache. */
163 descr->nr_raw_registers = NUM_REGS;
165 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
166 array. This pretects GDB from erant code that accesses elements
167 of the global register_valid_p[] array in the range [NUM_REGS
168 .. NUM_REGS + NUM_PSEUDO_REGS). */
169 descr->sizeof_raw_register_valid_p = NUM_REGS + NUM_PSEUDO_REGS;
171 /* Lay out the register cache. The pseud-registers are included in
172 the layout even though their value isn't stored in the register
173 cache. Some code, via read_register_bytes() access a register
174 using an offset/length rather than a register number.
176 NOTE: cagney/2002-05-22: Only REGISTER_VIRTUAL_TYPE() needs to be
177 used when constructing the register cache. It is assumed that
178 register raw size, virtual size and type length of the type are
183 descr->sizeof_register = XCALLOC (descr->nr_cooked_registers, long);
184 descr->register_offset = XCALLOC (descr->nr_cooked_registers, long);
185 descr->max_register_size = 0;
186 for (i = 0; i < descr->nr_cooked_registers; i++)
188 descr->sizeof_register[i] = TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (i));
189 descr->register_offset[i] = offset;
190 offset += descr->sizeof_register[i];
191 if (descr->max_register_size < descr->sizeof_register[i])
192 descr->max_register_size = descr->sizeof_register[i];
194 /* Set the real size of the register cache buffer. */
195 /* FIXME: cagney/2002-05-22: Should only need to allocate space
196 for the raw registers. Unfortunatly some code still accesses
197 the register array directly using the global registers[].
198 Until that code has been purged, play safe and over allocating
199 the register buffer. Ulgh! */
200 descr->sizeof_raw_registers = offset;
201 /* = descr->register_offset[descr->nr_raw_registers]; */
205 /* Sanity check. Confirm that the assumptions about gdbarch are
206 true. The REGCACHE_DESCR_HANDLE is set before doing the checks
207 so that targets using the generic methods supplied by regcache
208 don't go into infinite recursion trying to, again, create the
210 set_gdbarch_data (gdbarch, regcache_descr_handle, descr);
211 for (i = 0; i < descr->nr_cooked_registers; i++)
213 gdb_assert (descr->sizeof_register[i] == REGISTER_RAW_SIZE (i));
214 gdb_assert (descr->sizeof_register[i] == REGISTER_VIRTUAL_SIZE (i));
215 gdb_assert (descr->register_offset[i] == REGISTER_BYTE (i));
217 /* gdb_assert (descr->sizeof_raw_registers == REGISTER_BYTES (i)); */
223 static struct regcache_descr *
224 regcache_descr (struct gdbarch *gdbarch)
226 return gdbarch_data (gdbarch, regcache_descr_handle);
230 xfree_regcache_descr (struct gdbarch *gdbarch, void *ptr)
232 struct regcache_descr *descr = ptr;
235 xfree (descr->register_offset);
236 xfree (descr->sizeof_register);
237 descr->register_offset = NULL;
238 descr->sizeof_register = NULL;
242 /* The register cache for storing raw register values. */
246 struct regcache_descr *descr;
248 char *raw_register_valid_p;
249 /* If a value isn't in the cache should the corresponding target be
250 queried for a value. */
255 regcache_xmalloc (struct gdbarch *gdbarch)
257 struct regcache_descr *descr;
258 struct regcache *regcache;
259 gdb_assert (gdbarch != NULL);
260 descr = regcache_descr (gdbarch);
261 regcache = XMALLOC (struct regcache);
262 regcache->descr = descr;
263 regcache->raw_registers
264 = XCALLOC (descr->sizeof_raw_registers, char);
265 regcache->raw_register_valid_p
266 = XCALLOC (descr->sizeof_raw_register_valid_p, char);
267 regcache->passthrough_p = 0;
272 regcache_xfree (struct regcache *regcache)
274 if (regcache == NULL)
276 xfree (regcache->raw_registers);
277 xfree (regcache->raw_register_valid_p);
282 do_regcache_xfree (void *data)
284 regcache_xfree (data);
288 make_cleanup_regcache_xfree (struct regcache *regcache)
290 return make_cleanup (do_regcache_xfree, regcache);
294 regcache_cpy (struct regcache *dst, struct regcache *src)
298 gdb_assert (src != NULL && dst != NULL);
299 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
300 gdb_assert (src != dst);
301 /* FIXME: cagney/2002-05-17: To say this bit is bad is being polite.
302 It keeps the existing code working where things rely on going
303 through to the register cache. */
304 if (src == current_regcache && src->descr->legacy_p)
306 /* ULGH!!!! Old way. Use REGISTER bytes and let code below
308 read_register_bytes (0, dst->raw_registers, REGISTER_BYTES);
311 /* FIXME: cagney/2002-05-17: To say this bit is bad is being polite.
312 It keeps the existing code working where things rely on going
313 through to the register cache. */
314 if (dst == current_regcache && dst->descr->legacy_p)
316 /* ULGH!!!! Old way. Use REGISTER bytes and let code below
318 write_register_bytes (0, src->raw_registers, REGISTER_BYTES);
321 buf = alloca (src->descr->max_register_size);
322 for (i = 0; i < src->descr->nr_raw_registers; i++)
324 /* Should we worry about the valid bit here? */
325 regcache_raw_read (src, i, buf);
326 regcache_raw_write (dst, i, buf);
331 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
334 gdb_assert (src != NULL && dst != NULL);
335 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
336 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
337 move of data into the current_regcache(). Doing this would be
338 silly - it would mean that valid_p would be completly invalid. */
339 gdb_assert (dst != current_regcache);
340 memcpy (dst->raw_registers, src->raw_registers,
341 dst->descr->sizeof_raw_registers);
342 memcpy (dst->raw_register_valid_p, src->raw_register_valid_p,
343 dst->descr->sizeof_raw_register_valid_p);
347 regcache_dup (struct regcache *src)
349 struct regcache *newbuf;
350 gdb_assert (current_regcache != NULL);
351 newbuf = regcache_xmalloc (src->descr->gdbarch);
352 regcache_cpy (newbuf, src);
357 regcache_dup_no_passthrough (struct regcache *src)
359 struct regcache *newbuf;
360 gdb_assert (current_regcache != NULL);
361 newbuf = regcache_xmalloc (src->descr->gdbarch);
362 regcache_cpy_no_passthrough (newbuf, src);
367 regcache_valid_p (struct regcache *regcache, int regnum)
369 gdb_assert (regcache != NULL);
370 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
371 return regcache->raw_register_valid_p[regnum];
375 deprecated_grub_regcache_for_registers (struct regcache *regcache)
377 return regcache->raw_registers;
381 deprecated_grub_regcache_for_register_valid (struct regcache *regcache)
383 return regcache->raw_register_valid_p;
386 /* Global structure containing the current regcache. */
387 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
388 register_valid[] currently point into this structure. */
389 struct regcache *current_regcache;
391 /* NOTE: this is a write-through cache. There is no "dirty" bit for
392 recording if the register values have been changed (eg. by the
393 user). Therefore all registers must be written back to the
394 target when appropriate. */
396 /* REGISTERS contains the cached register values (in target byte order). */
400 /* REGISTER_VALID is 0 if the register needs to be fetched,
401 1 if it has been fetched, and
402 -1 if the register value was not available.
404 "Not available" indicates that the target is not not able to supply
405 the register at this state. The register may become available at a
406 later time (after the next resume). This often occures when GDB is
407 manipulating a target that contains only a snapshot of the entire
408 system being debugged - some of the registers in such a system may
409 not have been saved. */
411 signed char *register_valid;
413 /* The thread/process associated with the current set of registers. */
415 static ptid_t registers_ptid;
423 Returns 0 if the value is not in the cache (needs fetch).
424 >0 if the value is in the cache.
425 <0 if the value is permanently unavailable (don't ask again). */
428 register_cached (int regnum)
430 return register_valid[regnum];
433 /* Record that REGNUM's value is cached if STATE is >0, uncached but
434 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
437 set_register_cached (int regnum, int state)
439 gdb_assert (regnum >= 0);
440 gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
441 current_regcache->raw_register_valid_p[regnum] = state;
446 invalidate a single register REGNUM in the cache */
448 register_changed (int regnum)
450 set_register_cached (regnum, 0);
453 /* If REGNUM >= 0, return a pointer to register REGNUM's cache buffer area,
454 else return a pointer to the start of the cache buffer. */
457 register_buffer (struct regcache *regcache, int regnum)
459 return regcache->raw_registers + regcache->descr->register_offset[regnum];
462 /* Return whether register REGNUM is a real register. */
465 real_register (int regnum)
467 return regnum >= 0 && regnum < NUM_REGS;
470 /* Low level examining and depositing of registers.
472 The caller is responsible for making sure that the inferior is
473 stopped before calling the fetching routines, or it will get
474 garbage. (a change from GDB version 3, in which the caller got the
475 value from the last stop). */
477 /* REGISTERS_CHANGED ()
479 Indicate that registers may have changed, so invalidate the cache. */
482 registers_changed (void)
486 registers_ptid = pid_to_ptid (-1);
488 /* Force cleanup of any alloca areas if using C alloca instead of
489 a builtin alloca. This particular call is used to clean up
490 areas allocated by low level target code which may build up
491 during lengthy interactions between gdb and the target before
492 gdb gives control to the user (ie watchpoints). */
495 for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
496 set_register_cached (i, 0);
498 if (registers_changed_hook)
499 registers_changed_hook ();
502 /* REGISTERS_FETCHED ()
504 Indicate that all registers have been fetched, so mark them all valid. */
506 /* NOTE: cagney/2001-12-04: This function does not set valid on the
507 pseudo-register range since pseudo registers are always supplied
508 using supply_register(). */
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 supply_register() the need for
512 this function/hack is eliminated. */
515 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 /* read_register_bytes and write_register_bytes are generally a *BAD*
526 idea. They are inefficient because they need to check for partial
527 updates, which can only be done by scanning through all of the
528 registers and seeing if the bytes that are being read/written fall
529 inside of an invalid register. [The main reason this is necessary
530 is that register sizes can vary, so a simple index won't suffice.]
531 It is far better to call read_register_gen and write_register_gen
532 if you want to get at the raw register contents, as it only takes a
533 regnum as an argument, and therefore can't do a partial register
536 Prior to the recent fixes to check for partial updates, both read
537 and write_register_bytes always checked to see if any registers
538 were stale, and then called target_fetch_registers (-1) to update
539 the whole set. This caused really slowed things down for remote
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 read_register_bytes (int in_start, char *in_buf, int in_len)
549 int in_end = in_start + in_len;
551 char *reg_buf = alloca (MAX_REGISTER_RAW_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 = REGISTER_BYTE (regnum);
566 reg_len = 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 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, 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];
612 /* Read register REGNUM into memory at MYADDR, which must be large
613 enough for REGISTER_RAW_BYTES (REGNUM). Target byte-order. If the
614 register is known to be the size of a CORE_ADDR or smaller,
615 read_register can be used instead. */
618 legacy_read_register_gen (int regnum, char *myaddr)
620 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
621 if (! ptid_equal (registers_ptid, inferior_ptid))
623 registers_changed ();
624 registers_ptid = inferior_ptid;
627 if (!register_cached (regnum))
628 target_fetch_registers (regnum);
630 memcpy (myaddr, register_buffer (current_regcache, regnum),
631 REGISTER_RAW_SIZE (regnum));
635 regcache_raw_read (struct regcache *regcache, int regnum, void *buf)
637 gdb_assert (regcache != NULL && buf != NULL);
638 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
639 if (regcache->descr->legacy_p
640 && regcache->passthrough_p)
642 gdb_assert (regcache == current_regcache);
643 /* For moment, just use underlying legacy code. Ulgh!!! This
644 silently and very indirectly updates the regcache's regcache
645 via the global register_valid[]. */
646 legacy_read_register_gen (regnum, buf);
649 /* Make certain that the register cache is up-to-date with respect
650 to the current thread. This switching shouldn't be necessary
651 only there is still only one target side register cache. Sigh!
652 On the bright side, at least there is a regcache object. */
653 if (regcache->passthrough_p)
655 gdb_assert (regcache == current_regcache);
656 if (! ptid_equal (registers_ptid, inferior_ptid))
658 registers_changed ();
659 registers_ptid = inferior_ptid;
661 if (!register_cached (regnum))
662 target_fetch_registers (regnum);
664 /* Copy the value directly into the register cache. */
665 memcpy (buf, (regcache->raw_registers
666 + regcache->descr->register_offset[regnum]),
667 regcache->descr->sizeof_register[regnum]);
671 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
674 gdb_assert (regcache != NULL);
675 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
676 buf = alloca (regcache->descr->sizeof_register[regnum]);
677 regcache_raw_read (regcache, regnum, buf);
678 (*val) = extract_signed_integer (buf,
679 regcache->descr->sizeof_register[regnum]);
683 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
687 gdb_assert (regcache != NULL);
688 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
689 buf = alloca (regcache->descr->sizeof_register[regnum]);
690 regcache_raw_read (regcache, regnum, buf);
691 (*val) = extract_unsigned_integer (buf,
692 regcache->descr->sizeof_register[regnum]);
696 read_register_gen (int regnum, char *buf)
698 gdb_assert (current_regcache != NULL);
699 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
700 if (current_regcache->descr->legacy_p)
702 legacy_read_register_gen (regnum, buf);
705 regcache_cooked_read (current_regcache, regnum, buf);
709 regcache_cooked_read (struct regcache *regcache, int regnum, void *buf)
711 gdb_assert (regnum >= 0);
712 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
713 if (regnum < regcache->descr->nr_raw_registers)
714 regcache_raw_read (regcache, regnum, buf);
716 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
721 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
725 gdb_assert (regcache != NULL);
726 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
727 buf = alloca (regcache->descr->sizeof_register[regnum]);
728 regcache_cooked_read (regcache, regnum, buf);
729 (*val) = extract_signed_integer (buf,
730 regcache->descr->sizeof_register[regnum]);
734 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
738 gdb_assert (regcache != NULL);
739 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
740 buf = alloca (regcache->descr->sizeof_register[regnum]);
741 regcache_cooked_read (regcache, regnum, buf);
742 (*val) = extract_unsigned_integer (buf,
743 regcache->descr->sizeof_register[regnum]);
746 /* Write register REGNUM at MYADDR to the target. MYADDR points at
747 REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order. */
750 legacy_write_register_gen (int regnum, const void *myaddr)
753 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
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 if (! ptid_equal (registers_ptid, inferior_ptid))
762 registers_changed ();
763 registers_ptid = inferior_ptid;
766 size = REGISTER_RAW_SIZE (regnum);
768 if (real_register (regnum))
770 /* If we have a valid copy of the register, and new value == old
771 value, then don't bother doing the actual store. */
772 if (register_cached (regnum)
773 && (memcmp (register_buffer (current_regcache, regnum), myaddr, size)
777 target_prepare_to_store ();
780 memcpy (register_buffer (current_regcache, regnum), myaddr, size);
782 set_register_cached (regnum, 1);
783 target_store_registers (regnum);
787 regcache_raw_write (struct regcache *regcache, int regnum, const void *buf)
789 gdb_assert (regcache != NULL && buf != NULL);
790 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
792 if (regcache->passthrough_p
793 && regcache->descr->legacy_p)
795 /* For moment, just use underlying legacy code. Ulgh!!! This
796 silently and very indirectly updates the regcache's buffers
797 via the globals register_valid[] and registers[]. */
798 gdb_assert (regcache == current_regcache);
799 legacy_write_register_gen (regnum, buf);
803 /* On the sparc, writing %g0 is a no-op, so we don't even want to
804 change the registers array if something writes to this register. */
805 if (CANNOT_STORE_REGISTER (regnum))
808 /* Handle the simple case first -> not write through so just store
810 if (!regcache->passthrough_p)
812 memcpy ((regcache->raw_registers
813 + regcache->descr->register_offset[regnum]), buf,
814 regcache->descr->sizeof_register[regnum]);
815 regcache->raw_register_valid_p[regnum] = 1;
819 /* Make certain that the correct cache is selected. */
820 gdb_assert (regcache == current_regcache);
821 if (! ptid_equal (registers_ptid, inferior_ptid))
823 registers_changed ();
824 registers_ptid = inferior_ptid;
827 /* If we have a valid copy of the register, and new value == old
828 value, then don't bother doing the actual store. */
829 if (regcache_valid_p (regcache, regnum)
830 && (memcmp (register_buffer (regcache, regnum), buf,
831 regcache->descr->sizeof_register[regnum]) == 0))
834 target_prepare_to_store ();
835 memcpy (register_buffer (regcache, regnum), buf,
836 regcache->descr->sizeof_register[regnum]);
837 regcache->raw_register_valid_p[regnum] = 1;
838 target_store_registers (regnum);
842 write_register_gen (int regnum, char *buf)
844 gdb_assert (current_regcache != NULL);
845 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
846 if (current_regcache->descr->legacy_p)
848 legacy_write_register_gen (regnum, buf);
851 regcache_cooked_write (current_regcache, regnum, buf);
855 regcache_cooked_write (struct regcache *regcache, int regnum, const void *buf)
857 gdb_assert (regnum >= 0);
858 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
859 if (regnum < regcache->descr->nr_raw_registers)
860 regcache_raw_write (regcache, regnum, buf);
862 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
866 /* Copy INLEN bytes of consecutive data from memory at MYADDR
867 into registers starting with the MYREGSTART'th byte of register data. */
870 write_register_bytes (int myregstart, char *myaddr, int inlen)
872 int myregend = myregstart + inlen;
875 target_prepare_to_store ();
877 /* Scan through the registers updating any that are covered by the
878 range myregstart<=>myregend using write_register_gen, which does
879 nice things like handling threads, and avoiding updates when the
880 new and old contents are the same. */
882 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
884 int regstart, regend;
886 regstart = REGISTER_BYTE (regnum);
887 regend = regstart + REGISTER_RAW_SIZE (regnum);
889 /* Is this register completely outside the range the user is writing? */
890 if (myregend <= regstart || regend <= myregstart)
893 /* Is this register completely within the range the user is writing? */
894 else if (myregstart <= regstart && regend <= myregend)
895 write_register_gen (regnum, myaddr + (regstart - myregstart));
897 /* The register partially overlaps the range being written. */
900 char *regbuf = (char*) alloca (MAX_REGISTER_RAW_SIZE);
901 /* What's the overlap between this register's bytes and
902 those the caller wants to write? */
903 int overlapstart = max (regstart, myregstart);
904 int overlapend = min (regend, myregend);
906 /* We may be doing a partial update of an invalid register.
907 Update it from the target before scribbling on it. */
908 read_register_gen (regnum, regbuf);
910 memcpy (registers + overlapstart,
911 myaddr + (overlapstart - myregstart),
912 overlapend - overlapstart);
914 target_store_registers (regnum);
920 /* Return the contents of register REGNUM as an unsigned integer. */
923 read_register (int regnum)
925 char *buf = alloca (REGISTER_RAW_SIZE (regnum));
926 read_register_gen (regnum, buf);
927 return (extract_unsigned_integer (buf, REGISTER_RAW_SIZE (regnum)));
931 read_register_pid (int regnum, ptid_t ptid)
937 if (ptid_equal (ptid, inferior_ptid))
938 return read_register (regnum);
940 save_ptid = inferior_ptid;
942 inferior_ptid = ptid;
944 retval = read_register (regnum);
946 inferior_ptid = save_ptid;
951 /* Return the contents of register REGNUM as a signed integer. */
954 read_signed_register (int regnum)
956 void *buf = alloca (REGISTER_RAW_SIZE (regnum));
957 read_register_gen (regnum, buf);
958 return (extract_signed_integer (buf, REGISTER_RAW_SIZE (regnum)));
962 read_signed_register_pid (int regnum, ptid_t ptid)
967 if (ptid_equal (ptid, inferior_ptid))
968 return read_signed_register (regnum);
970 save_ptid = inferior_ptid;
972 inferior_ptid = ptid;
974 retval = read_signed_register (regnum);
976 inferior_ptid = save_ptid;
981 /* Store VALUE into the raw contents of register number REGNUM. */
984 write_register (int regnum, LONGEST val)
988 size = REGISTER_RAW_SIZE (regnum);
990 store_signed_integer (buf, size, (LONGEST) val);
991 write_register_gen (regnum, buf);
995 write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
999 if (ptid_equal (ptid, inferior_ptid))
1001 write_register (regnum, val);
1005 save_ptid = inferior_ptid;
1007 inferior_ptid = ptid;
1009 write_register (regnum, val);
1011 inferior_ptid = save_ptid;
1014 /* SUPPLY_REGISTER()
1016 Record that register REGNUM contains VAL. This is used when the
1017 value is obtained from the inferior or core dump, so there is no
1018 need to store the value there.
1020 If VAL is a NULL pointer, then it's probably an unsupported register.
1021 We just set its value to all zeros. We might want to record this
1022 fact, and report it to the users of read_register and friends. */
1025 supply_register (int regnum, const void *val)
1028 if (! ptid_equal (registers_ptid, inferior_ptid))
1030 registers_changed ();
1031 registers_ptid = inferior_ptid;
1035 set_register_cached (regnum, 1);
1037 memcpy (register_buffer (current_regcache, regnum), val,
1038 REGISTER_RAW_SIZE (regnum));
1040 memset (register_buffer (current_regcache, regnum), '\000',
1041 REGISTER_RAW_SIZE (regnum));
1043 /* On some architectures, e.g. HPPA, there are a few stray bits in
1044 some registers, that the rest of the code would like to ignore. */
1046 /* NOTE: cagney/2001-03-16: The macro CLEAN_UP_REGISTER_VALUE is
1047 going to be deprecated. Instead architectures will leave the raw
1048 register value as is and instead clean things up as they pass
1049 through the method gdbarch_pseudo_register_read() clean up the
1052 #ifdef DEPRECATED_CLEAN_UP_REGISTER_VALUE
1053 DEPRECATED_CLEAN_UP_REGISTER_VALUE \
1054 (regnum, register_buffer (current_regcache, regnum));
1059 regcache_collect (int regnum, void *buf)
1061 memcpy (buf, register_buffer (current_regcache, regnum),
1062 REGISTER_RAW_SIZE (regnum));
1066 /* read_pc, write_pc, read_sp, write_sp, read_fp, etc. Special
1067 handling for registers PC, SP, and FP. */
1069 /* NOTE: cagney/2001-02-18: The functions generic_target_read_pc(),
1070 read_pc_pid(), read_pc(), generic_target_write_pc(),
1071 write_pc_pid(), write_pc(), generic_target_read_sp(), read_sp(),
1072 generic_target_write_sp(), write_sp(), generic_target_read_fp() and
1073 read_fp(), will eventually be moved out of the reg-cache into
1074 either frame.[hc] or to the multi-arch framework. The are not part
1075 of the raw register cache. */
1077 /* This routine is getting awfully cluttered with #if's. It's probably
1078 time to turn this into READ_PC and define it in the tm.h file.
1081 1999-06-08: The following were re-written so that it assumes the
1082 existence of a TARGET_READ_PC et.al. macro. A default generic
1083 version of that macro is made available where needed.
1085 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
1086 by the multi-arch framework, it will eventually be possible to
1087 eliminate the intermediate read_pc_pid(). The client would call
1088 TARGET_READ_PC directly. (cagney). */
1091 generic_target_read_pc (ptid_t ptid)
1096 CORE_ADDR pc_val = ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, ptid));
1100 internal_error (__FILE__, __LINE__,
1101 "generic_target_read_pc");
1106 read_pc_pid (ptid_t ptid)
1108 ptid_t saved_inferior_ptid;
1111 /* In case ptid != inferior_ptid. */
1112 saved_inferior_ptid = inferior_ptid;
1113 inferior_ptid = ptid;
1115 pc_val = TARGET_READ_PC (ptid);
1117 inferior_ptid = saved_inferior_ptid;
1124 return read_pc_pid (inferior_ptid);
1128 generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
1132 write_register_pid (PC_REGNUM, pc, ptid);
1133 if (NPC_REGNUM >= 0)
1134 write_register_pid (NPC_REGNUM, pc + 4, ptid);
1136 internal_error (__FILE__, __LINE__,
1137 "generic_target_write_pc");
1142 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
1144 ptid_t saved_inferior_ptid;
1146 /* In case ptid != inferior_ptid. */
1147 saved_inferior_ptid = inferior_ptid;
1148 inferior_ptid = ptid;
1150 TARGET_WRITE_PC (pc, ptid);
1152 inferior_ptid = saved_inferior_ptid;
1156 write_pc (CORE_ADDR pc)
1158 write_pc_pid (pc, inferior_ptid);
1161 /* Cope with strage ways of getting to the stack and frame pointers */
1164 generic_target_read_sp (void)
1168 return read_register (SP_REGNUM);
1170 internal_error (__FILE__, __LINE__,
1171 "generic_target_read_sp");
1177 return TARGET_READ_SP ();
1181 generic_target_write_sp (CORE_ADDR val)
1186 write_register (SP_REGNUM, val);
1190 internal_error (__FILE__, __LINE__,
1191 "generic_target_write_sp");
1195 write_sp (CORE_ADDR val)
1197 TARGET_WRITE_SP (val);
1201 generic_target_read_fp (void)
1205 return read_register (FP_REGNUM);
1207 internal_error (__FILE__, __LINE__,
1208 "generic_target_read_fp");
1214 return TARGET_READ_FP ();
1219 reg_flush_command (char *command, int from_tty)
1221 /* Force-flush the register cache. */
1222 registers_changed ();
1224 printf_filtered ("Register cache flushed.\n");
1228 build_regcache (void)
1230 current_regcache = regcache_xmalloc (current_gdbarch);
1231 current_regcache->passthrough_p = 1;
1232 registers = deprecated_grub_regcache_for_registers (current_regcache);
1233 register_valid = deprecated_grub_regcache_for_register_valid (current_regcache);
1237 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1238 const unsigned char *buf, long len)
1243 case BFD_ENDIAN_BIG:
1244 for (i = 0; i < len; i++)
1245 fprintf_unfiltered (file, "%02x", buf[i]);
1247 case BFD_ENDIAN_LITTLE:
1248 for (i = len - 1; i >= 0; i--)
1249 fprintf_unfiltered (file, "%02x", buf[i]);
1252 internal_error (__FILE__, __LINE__, "Bad switch");
1256 enum regcache_dump_what
1258 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked
1262 regcache_dump (struct regcache *regcache, struct ui_file *file,
1263 enum regcache_dump_what what_to_dump)
1265 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1267 int footnote_nr = 0;
1268 int footnote_register_size = 0;
1269 int footnote_register_offset = 0;
1270 int footnote_register_type_name_null = 0;
1271 long register_offset = 0;
1272 unsigned char *buf = alloca (regcache->descr->max_register_size);
1275 fprintf_unfiltered (file, "legacy_p %d\n", regcache->descr->legacy_p);
1276 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1277 regcache->descr->nr_raw_registers);
1278 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1279 regcache->descr->nr_cooked_registers);
1280 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1281 regcache->descr->sizeof_raw_registers);
1282 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1283 regcache->descr->sizeof_raw_register_valid_p);
1284 fprintf_unfiltered (file, "max_register_size %ld\n",
1285 regcache->descr->max_register_size);
1286 fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1287 fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1290 gdb_assert (regcache->descr->nr_cooked_registers
1291 == (NUM_REGS + NUM_PSEUDO_REGS));
1293 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1297 fprintf_unfiltered (file, " %-10s", "Name");
1300 const char *p = REGISTER_NAME (regnum);
1303 else if (p[0] == '\0')
1305 fprintf_unfiltered (file, " %-10s", p);
1310 fprintf_unfiltered (file, " %4s", "Nr");
1312 fprintf_unfiltered (file, " %4d", regnum);
1314 /* Relative number. */
1316 fprintf_unfiltered (file, " %4s", "Rel");
1317 else if (regnum < NUM_REGS)
1318 fprintf_unfiltered (file, " %4d", regnum);
1320 fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1324 fprintf_unfiltered (file, " %6s ", "Offset");
1327 fprintf_unfiltered (file, " %6ld",
1328 regcache->descr->register_offset[regnum]);
1329 if (register_offset != regcache->descr->register_offset[regnum]
1330 || register_offset != REGISTER_BYTE (regnum))
1332 if (!footnote_register_offset)
1333 footnote_register_offset = ++footnote_nr;
1334 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1337 fprintf_unfiltered (file, " ");
1338 register_offset = (regcache->descr->register_offset[regnum]
1339 + regcache->descr->sizeof_register[regnum]);
1344 fprintf_unfiltered (file, " %5s ", "Size");
1347 fprintf_unfiltered (file, " %5ld",
1348 regcache->descr->sizeof_register[regnum]);
1349 if ((regcache->descr->sizeof_register[regnum]
1350 != REGISTER_RAW_SIZE (regnum))
1351 || (regcache->descr->sizeof_register[regnum]
1352 != REGISTER_VIRTUAL_SIZE (regnum))
1353 || (regcache->descr->sizeof_register[regnum]
1354 != TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (regnum)))
1357 if (!footnote_register_size)
1358 footnote_register_size = ++footnote_nr;
1359 fprintf_unfiltered (file, "*%d", footnote_register_size);
1362 fprintf_unfiltered (file, " ");
1367 fprintf_unfiltered (file, " %-20s", "Type");
1370 static const char blt[] = "builtin_type";
1371 const char *t = TYPE_NAME (REGISTER_VIRTUAL_TYPE (regnum));
1375 if (!footnote_register_type_name_null)
1376 footnote_register_type_name_null = ++footnote_nr;
1377 xasprintf (&n, "*%d", footnote_register_type_name_null);
1378 make_cleanup (xfree, n);
1381 /* Chop a leading builtin_type. */
1382 if (strncmp (t, blt, strlen (blt)) == 0)
1384 fprintf_unfiltered (file, " %-20s", t);
1388 if (what_to_dump == regcache_dump_raw)
1391 fprintf_unfiltered (file, "Raw value");
1392 else if (regnum >= regcache->descr->nr_raw_registers)
1393 fprintf_unfiltered (file, "<cooked>");
1394 else if (!regcache_valid_p (regcache, regnum))
1395 fprintf_unfiltered (file, "<invalid>");
1398 regcache_raw_read (regcache, regnum, buf);
1399 fprintf_unfiltered (file, "0x");
1400 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1401 REGISTER_RAW_SIZE (regnum));
1405 /* Value, cooked. */
1406 if (what_to_dump == regcache_dump_cooked)
1409 fprintf_unfiltered (file, "Cooked value");
1412 regcache_cooked_read (regcache, regnum, buf);
1413 fprintf_unfiltered (file, "0x");
1414 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1415 REGISTER_VIRTUAL_SIZE (regnum));
1419 fprintf_unfiltered (file, "\n");
1422 if (footnote_register_size)
1423 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1424 footnote_register_size);
1425 if (footnote_register_offset)
1426 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1427 footnote_register_offset);
1428 if (footnote_register_type_name_null)
1429 fprintf_unfiltered (file,
1430 "*%d: Register type's name NULL.\n",
1431 footnote_register_type_name_null);
1432 do_cleanups (cleanups);
1436 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1439 regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1442 struct ui_file *file = gdb_fopen (args, "w");
1444 perror_with_name ("maintenance print architecture");
1445 regcache_dump (current_regcache, file, what_to_dump);
1446 ui_file_delete (file);
1451 maintenance_print_registers (char *args, int from_tty)
1453 regcache_print (args, regcache_dump_none);
1457 maintenance_print_raw_registers (char *args, int from_tty)
1459 regcache_print (args, regcache_dump_raw);
1463 maintenance_print_cooked_registers (char *args, int from_tty)
1465 regcache_print (args, regcache_dump_cooked);
1469 _initialize_regcache (void)
1471 regcache_descr_handle = register_gdbarch_data (init_regcache_descr,
1472 xfree_regcache_descr);
1473 REGISTER_GDBARCH_SWAP (current_regcache);
1474 register_gdbarch_swap (®isters, sizeof (registers), NULL);
1475 register_gdbarch_swap (®ister_valid, sizeof (register_valid), NULL);
1476 register_gdbarch_swap (NULL, 0, build_regcache);
1478 add_com ("flushregs", class_maintenance, reg_flush_command,
1479 "Force gdb to flush its register cache (maintainer command)");
1481 /* Initialize the thread/process associated with the current set of
1482 registers. For now, -1 is special, and means `no current process'. */
1483 registers_ptid = pid_to_ptid (-1);
1485 add_cmd ("registers", class_maintenance,
1486 maintenance_print_registers,
1487 "Print the internal register configuration.\
1488 Takes an optional file parameter.",
1489 &maintenanceprintlist);
1490 add_cmd ("raw-registers", class_maintenance,
1491 maintenance_print_raw_registers,
1492 "Print the internal register configuration including raw values.\
1493 Takes an optional file parameter.",
1494 &maintenanceprintlist);
1495 add_cmd ("cooked-registers", class_maintenance,
1496 maintenance_print_cooked_registers,
1497 "Print the internal register configuration including cooked values.\
1498 Takes an optional file parameter.",
1499 &maintenanceprintlist);