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;
81 /* Cached table containing the type of each register. */
82 struct type **register_type;
86 init_legacy_regcache_descr (struct gdbarch *gdbarch,
87 struct regcache_descr *descr)
90 /* FIXME: cagney/2002-05-11: gdbarch_data() should take that
91 ``gdbarch'' as a parameter. */
92 gdb_assert (gdbarch != NULL);
94 /* FIXME: cagney/2002-05-11: Shouldn't be including pseudo-registers
95 in the register buffer. Unfortunatly some architectures do. */
96 descr->nr_raw_registers = descr->nr_cooked_registers;
97 descr->sizeof_raw_register_valid_p = descr->nr_cooked_registers;
99 /* FIXME: cagney/2002-05-11: Instead of using REGISTER_BYTE() this
100 code should compute the offets et.al. at runtime. This currently
101 isn't possible because some targets overlap register locations -
102 see the mess in read_register_bytes() and write_register_bytes()
104 descr->sizeof_register = XCALLOC (descr->nr_cooked_registers, long);
105 descr->register_offset = XCALLOC (descr->nr_cooked_registers, long);
106 descr->max_register_size = 0;
107 for (i = 0; i < descr->nr_cooked_registers; i++)
109 descr->register_offset[i] = REGISTER_BYTE (i);
110 descr->sizeof_register[i] = REGISTER_RAW_SIZE (i);
111 if (descr->max_register_size < REGISTER_RAW_SIZE (i))
112 descr->max_register_size = REGISTER_RAW_SIZE (i);
113 if (descr->max_register_size < REGISTER_VIRTUAL_SIZE (i))
114 descr->max_register_size = REGISTER_VIRTUAL_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;
139 init_regcache_descr (struct gdbarch *gdbarch)
142 struct regcache_descr *descr;
143 gdb_assert (gdbarch != NULL);
145 /* Create an initial, zero filled, table. */
146 descr = XCALLOC (1, struct regcache_descr);
147 descr->gdbarch = gdbarch;
149 /* Total size of the register space. The raw registers are mapped
150 directly onto the raw register cache while the pseudo's are
151 either mapped onto raw-registers or memory. */
152 descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS;
154 /* Fill in a table of register types. */
155 descr->register_type = XCALLOC (descr->nr_cooked_registers,
157 for (i = 0; i < descr->nr_cooked_registers; i++)
159 descr->register_type[i] = REGISTER_VIRTUAL_TYPE (i);
162 /* If an old style architecture, fill in the remainder of the
163 register cache descriptor using the register macros. */
164 if (!gdbarch_pseudo_register_read_p (gdbarch)
165 && !gdbarch_pseudo_register_write_p (gdbarch))
168 init_legacy_regcache_descr (gdbarch, descr);
172 /* Construct a strictly RAW register cache. Don't allow pseudo's
173 into the register cache. */
174 descr->nr_raw_registers = NUM_REGS;
176 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
177 array. This pretects GDB from erant code that accesses elements
178 of the global register_valid_p[] array in the range [NUM_REGS
179 .. NUM_REGS + NUM_PSEUDO_REGS). */
180 descr->sizeof_raw_register_valid_p = NUM_REGS + NUM_PSEUDO_REGS;
182 /* Lay out the register cache. The pseud-registers are included in
183 the layout even though their value isn't stored in the register
184 cache. Some code, via read_register_bytes() access a register
185 using an offset/length rather than a register number.
187 NOTE: cagney/2002-05-22: Only register_type() is used when
188 constructing the register cache. It is assumed that the
189 register's raw size, virtual size and type length are all the
194 descr->sizeof_register = XCALLOC (descr->nr_cooked_registers, long);
195 descr->register_offset = XCALLOC (descr->nr_cooked_registers, long);
196 descr->max_register_size = 0;
197 for (i = 0; i < descr->nr_cooked_registers; i++)
199 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
200 descr->register_offset[i] = offset;
201 offset += descr->sizeof_register[i];
202 if (descr->max_register_size < descr->sizeof_register[i])
203 descr->max_register_size = descr->sizeof_register[i];
205 /* Set the real size of the register cache buffer. */
206 /* FIXME: cagney/2002-05-22: Should only need to allocate space
207 for the raw registers. Unfortunatly some code still accesses
208 the register array directly using the global registers[].
209 Until that code has been purged, play safe and over allocating
210 the register buffer. Ulgh! */
211 descr->sizeof_raw_registers = offset;
212 /* = descr->register_offset[descr->nr_raw_registers]; */
216 /* Sanity check. Confirm that the assumptions about gdbarch are
217 true. The REGCACHE_DESCR_HANDLE is set before doing the checks
218 so that targets using the generic methods supplied by regcache
219 don't go into infinite recursion trying to, again, create the
221 set_gdbarch_data (gdbarch, regcache_descr_handle, descr);
222 for (i = 0; i < descr->nr_cooked_registers; i++)
224 gdb_assert (descr->sizeof_register[i] == REGISTER_RAW_SIZE (i));
225 gdb_assert (descr->sizeof_register[i] == REGISTER_VIRTUAL_SIZE (i));
226 gdb_assert (descr->register_offset[i] == REGISTER_BYTE (i));
228 /* gdb_assert (descr->sizeof_raw_registers == REGISTER_BYTES (i)); */
234 static struct regcache_descr *
235 regcache_descr (struct gdbarch *gdbarch)
237 return gdbarch_data (gdbarch, regcache_descr_handle);
241 xfree_regcache_descr (struct gdbarch *gdbarch, void *ptr)
243 struct regcache_descr *descr = ptr;
246 xfree (descr->register_offset);
247 xfree (descr->sizeof_register);
248 descr->register_offset = NULL;
249 descr->sizeof_register = NULL;
253 /* Utility functions returning useful register attributes stored in
254 the regcache descr. */
257 register_type (struct gdbarch *gdbarch, int regnum)
259 struct regcache_descr *descr = regcache_descr (gdbarch);
260 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
261 return descr->register_type[regnum];
264 /* Utility functions returning useful register attributes stored in
265 the regcache descr. */
268 max_register_size (struct gdbarch *gdbarch)
270 struct regcache_descr *descr = regcache_descr (gdbarch);
271 return descr->max_register_size;
274 /* The register cache for storing raw register values. */
278 struct regcache_descr *descr;
280 char *raw_register_valid_p;
281 /* If a value isn't in the cache should the corresponding target be
282 queried for a value. */
287 regcache_xmalloc (struct gdbarch *gdbarch)
289 struct regcache_descr *descr;
290 struct regcache *regcache;
291 gdb_assert (gdbarch != NULL);
292 descr = regcache_descr (gdbarch);
293 regcache = XMALLOC (struct regcache);
294 regcache->descr = descr;
295 regcache->raw_registers
296 = XCALLOC (descr->sizeof_raw_registers, char);
297 regcache->raw_register_valid_p
298 = XCALLOC (descr->sizeof_raw_register_valid_p, char);
299 regcache->passthrough_p = 0;
304 regcache_xfree (struct regcache *regcache)
306 if (regcache == NULL)
308 xfree (regcache->raw_registers);
309 xfree (regcache->raw_register_valid_p);
314 do_regcache_xfree (void *data)
316 regcache_xfree (data);
320 make_cleanup_regcache_xfree (struct regcache *regcache)
322 return make_cleanup (do_regcache_xfree, regcache);
326 regcache_cpy (struct regcache *dst, struct regcache *src)
330 gdb_assert (src != NULL && dst != NULL);
331 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
332 gdb_assert (src != dst);
333 /* FIXME: cagney/2002-05-17: To say this bit is bad is being polite.
334 It keeps the existing code working where things rely on going
335 through to the register cache. */
336 if (src == current_regcache && src->descr->legacy_p)
338 /* ULGH!!!! Old way. Use REGISTER bytes and let code below
340 read_register_bytes (0, dst->raw_registers, REGISTER_BYTES);
343 /* FIXME: cagney/2002-05-17: To say this bit is bad is being polite.
344 It keeps the existing code working where things rely on going
345 through to the register cache. */
346 if (dst == current_regcache && dst->descr->legacy_p)
348 /* ULGH!!!! Old way. Use REGISTER bytes and let code below
350 write_register_bytes (0, src->raw_registers, REGISTER_BYTES);
353 buf = alloca (src->descr->max_register_size);
354 for (i = 0; i < src->descr->nr_raw_registers; i++)
356 /* Should we worry about the valid bit here? */
357 regcache_raw_read (src, i, buf);
358 regcache_raw_write (dst, i, buf);
363 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
366 gdb_assert (src != NULL && dst != NULL);
367 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
368 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
369 move of data into the current_regcache(). Doing this would be
370 silly - it would mean that valid_p would be completly invalid. */
371 gdb_assert (dst != current_regcache);
372 memcpy (dst->raw_registers, src->raw_registers,
373 dst->descr->sizeof_raw_registers);
374 memcpy (dst->raw_register_valid_p, src->raw_register_valid_p,
375 dst->descr->sizeof_raw_register_valid_p);
379 regcache_dup (struct regcache *src)
381 struct regcache *newbuf;
382 gdb_assert (current_regcache != NULL);
383 newbuf = regcache_xmalloc (src->descr->gdbarch);
384 regcache_cpy (newbuf, src);
389 regcache_dup_no_passthrough (struct regcache *src)
391 struct regcache *newbuf;
392 gdb_assert (current_regcache != NULL);
393 newbuf = regcache_xmalloc (src->descr->gdbarch);
394 regcache_cpy_no_passthrough (newbuf, src);
399 regcache_valid_p (struct regcache *regcache, int regnum)
401 gdb_assert (regcache != NULL);
402 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
403 return regcache->raw_register_valid_p[regnum];
407 deprecated_grub_regcache_for_registers (struct regcache *regcache)
409 return regcache->raw_registers;
413 deprecated_grub_regcache_for_register_valid (struct regcache *regcache)
415 return regcache->raw_register_valid_p;
418 /* Global structure containing the current regcache. */
419 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
420 register_valid[] currently point into this structure. */
421 struct regcache *current_regcache;
423 /* NOTE: this is a write-through cache. There is no "dirty" bit for
424 recording if the register values have been changed (eg. by the
425 user). Therefore all registers must be written back to the
426 target when appropriate. */
428 /* REGISTERS contains the cached register values (in target byte order). */
432 /* REGISTER_VALID is 0 if the register needs to be fetched,
433 1 if it has been fetched, and
434 -1 if the register value was not available.
436 "Not available" indicates that the target is not not able to supply
437 the register at this state. The register may become available at a
438 later time (after the next resume). This often occures when GDB is
439 manipulating a target that contains only a snapshot of the entire
440 system being debugged - some of the registers in such a system may
441 not have been saved. */
443 signed char *register_valid;
445 /* The thread/process associated with the current set of registers. */
447 static ptid_t registers_ptid;
455 Returns 0 if the value is not in the cache (needs fetch).
456 >0 if the value is in the cache.
457 <0 if the value is permanently unavailable (don't ask again). */
460 register_cached (int regnum)
462 return register_valid[regnum];
465 /* Record that REGNUM's value is cached if STATE is >0, uncached but
466 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
469 set_register_cached (int regnum, int state)
471 gdb_assert (regnum >= 0);
472 gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
473 current_regcache->raw_register_valid_p[regnum] = state;
478 invalidate a single register REGNUM in the cache */
480 register_changed (int regnum)
482 set_register_cached (regnum, 0);
485 /* If REGNUM >= 0, return a pointer to register REGNUM's cache buffer area,
486 else return a pointer to the start of the cache buffer. */
489 register_buffer (struct regcache *regcache, int regnum)
491 return regcache->raw_registers + regcache->descr->register_offset[regnum];
494 /* Return whether register REGNUM is a real register. */
497 real_register (int regnum)
499 return regnum >= 0 && regnum < NUM_REGS;
502 /* Low level examining and depositing of registers.
504 The caller is responsible for making sure that the inferior is
505 stopped before calling the fetching routines, or it will get
506 garbage. (a change from GDB version 3, in which the caller got the
507 value from the last stop). */
509 /* REGISTERS_CHANGED ()
511 Indicate that registers may have changed, so invalidate the cache. */
514 registers_changed (void)
518 registers_ptid = pid_to_ptid (-1);
520 /* Force cleanup of any alloca areas if using C alloca instead of
521 a builtin alloca. This particular call is used to clean up
522 areas allocated by low level target code which may build up
523 during lengthy interactions between gdb and the target before
524 gdb gives control to the user (ie watchpoints). */
527 for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
528 set_register_cached (i, 0);
530 if (registers_changed_hook)
531 registers_changed_hook ();
534 /* REGISTERS_FETCHED ()
536 Indicate that all registers have been fetched, so mark them all valid. */
538 /* NOTE: cagney/2001-12-04: This function does not set valid on the
539 pseudo-register range since pseudo registers are always supplied
540 using supply_register(). */
541 /* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
542 code was blatting the registers[] array and then calling this.
543 Since targets should only be using supply_register() the need for
544 this function/hack is eliminated. */
547 registers_fetched (void)
551 for (i = 0; i < NUM_REGS; i++)
552 set_register_cached (i, 1);
553 /* Do not assume that the pseudo-regs have also been fetched.
554 Fetching all real regs NEVER accounts for pseudo-regs. */
557 /* read_register_bytes and write_register_bytes are generally a *BAD*
558 idea. They are inefficient because they need to check for partial
559 updates, which can only be done by scanning through all of the
560 registers and seeing if the bytes that are being read/written fall
561 inside of an invalid register. [The main reason this is necessary
562 is that register sizes can vary, so a simple index won't suffice.]
563 It is far better to call read_register_gen and write_register_gen
564 if you want to get at the raw register contents, as it only takes a
565 regnum as an argument, and therefore can't do a partial register
568 Prior to the recent fixes to check for partial updates, both read
569 and write_register_bytes always checked to see if any registers
570 were stale, and then called target_fetch_registers (-1) to update
571 the whole set. This caused really slowed things down for remote
574 /* Copy INLEN bytes of consecutive data from registers
575 starting with the INREGBYTE'th byte of register data
576 into memory at MYADDR. */
579 read_register_bytes (int in_start, char *in_buf, int in_len)
581 int in_end = in_start + in_len;
583 char *reg_buf = alloca (MAX_REGISTER_RAW_SIZE);
585 /* See if we are trying to read bytes from out-of-date registers. If so,
586 update just those registers. */
588 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
597 reg_start = REGISTER_BYTE (regnum);
598 reg_len = REGISTER_RAW_SIZE (regnum);
599 reg_end = reg_start + reg_len;
601 if (reg_end <= in_start || in_end <= reg_start)
602 /* The range the user wants to read doesn't overlap with regnum. */
605 if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0')
606 /* Force the cache to fetch the entire register. */
607 read_register_gen (regnum, reg_buf);
609 /* Legacy note: even though this register is ``invalid'' we
610 still need to return something. It would appear that some
611 code relies on apparent gaps in the register array also
613 /* FIXME: cagney/2001-08-18: This is just silly. It defeats
614 the entire register read/write flow of control. Must
615 resist temptation to return 0xdeadbeef. */
616 memcpy (reg_buf, registers + reg_start, reg_len);
618 /* Legacy note: This function, for some reason, allows a NULL
619 input buffer. If the buffer is NULL, the registers are still
620 fetched, just the final transfer is skipped. */
624 /* start = max (reg_start, in_start) */
625 if (reg_start > in_start)
630 /* end = min (reg_end, in_end) */
631 if (reg_end < in_end)
636 /* Transfer just the bytes common to both IN_BUF and REG_BUF */
637 for (byte = start; byte < end; byte++)
639 in_buf[byte - in_start] = reg_buf[byte - reg_start];
644 /* Read register REGNUM into memory at MYADDR, which must be large
645 enough for REGISTER_RAW_BYTES (REGNUM). Target byte-order. If the
646 register is known to be the size of a CORE_ADDR or smaller,
647 read_register can be used instead. */
650 legacy_read_register_gen (int regnum, char *myaddr)
652 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
653 if (! ptid_equal (registers_ptid, inferior_ptid))
655 registers_changed ();
656 registers_ptid = inferior_ptid;
659 if (!register_cached (regnum))
660 target_fetch_registers (regnum);
662 memcpy (myaddr, register_buffer (current_regcache, regnum),
663 REGISTER_RAW_SIZE (regnum));
667 regcache_raw_read (struct regcache *regcache, int regnum, void *buf)
669 gdb_assert (regcache != NULL && buf != NULL);
670 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
671 if (regcache->descr->legacy_p
672 && regcache->passthrough_p)
674 gdb_assert (regcache == current_regcache);
675 /* For moment, just use underlying legacy code. Ulgh!!! This
676 silently and very indirectly updates the regcache's regcache
677 via the global register_valid[]. */
678 legacy_read_register_gen (regnum, buf);
681 /* Make certain that the register cache is up-to-date with respect
682 to the current thread. This switching shouldn't be necessary
683 only there is still only one target side register cache. Sigh!
684 On the bright side, at least there is a regcache object. */
685 if (regcache->passthrough_p)
687 gdb_assert (regcache == current_regcache);
688 if (! ptid_equal (registers_ptid, inferior_ptid))
690 registers_changed ();
691 registers_ptid = inferior_ptid;
693 if (!register_cached (regnum))
694 target_fetch_registers (regnum);
696 /* Copy the value directly into the register cache. */
697 memcpy (buf, (regcache->raw_registers
698 + regcache->descr->register_offset[regnum]),
699 regcache->descr->sizeof_register[regnum]);
703 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
706 gdb_assert (regcache != NULL);
707 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
708 buf = alloca (regcache->descr->sizeof_register[regnum]);
709 regcache_raw_read (regcache, regnum, buf);
710 (*val) = extract_signed_integer (buf,
711 regcache->descr->sizeof_register[regnum]);
715 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
719 gdb_assert (regcache != NULL);
720 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
721 buf = alloca (regcache->descr->sizeof_register[regnum]);
722 regcache_raw_read (regcache, regnum, buf);
723 (*val) = extract_unsigned_integer (buf,
724 regcache->descr->sizeof_register[regnum]);
728 read_register_gen (int regnum, char *buf)
730 gdb_assert (current_regcache != NULL);
731 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
732 if (current_regcache->descr->legacy_p)
734 legacy_read_register_gen (regnum, buf);
737 regcache_cooked_read (current_regcache, regnum, buf);
741 regcache_cooked_read (struct regcache *regcache, int regnum, void *buf)
743 gdb_assert (regnum >= 0);
744 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
745 if (regnum < regcache->descr->nr_raw_registers)
746 regcache_raw_read (regcache, regnum, buf);
748 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
753 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
757 gdb_assert (regcache != NULL);
758 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
759 buf = alloca (regcache->descr->sizeof_register[regnum]);
760 regcache_cooked_read (regcache, regnum, buf);
761 (*val) = extract_signed_integer (buf,
762 regcache->descr->sizeof_register[regnum]);
766 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
770 gdb_assert (regcache != NULL);
771 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
772 buf = alloca (regcache->descr->sizeof_register[regnum]);
773 regcache_cooked_read (regcache, regnum, buf);
774 (*val) = extract_unsigned_integer (buf,
775 regcache->descr->sizeof_register[regnum]);
778 /* Write register REGNUM at MYADDR to the target. MYADDR points at
779 REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order. */
782 legacy_write_register_gen (int regnum, const void *myaddr)
785 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
787 /* On the sparc, writing %g0 is a no-op, so we don't even want to
788 change the registers array if something writes to this register. */
789 if (CANNOT_STORE_REGISTER (regnum))
792 if (! ptid_equal (registers_ptid, inferior_ptid))
794 registers_changed ();
795 registers_ptid = inferior_ptid;
798 size = REGISTER_RAW_SIZE (regnum);
800 if (real_register (regnum))
802 /* If we have a valid copy of the register, and new value == old
803 value, then don't bother doing the actual store. */
804 if (register_cached (regnum)
805 && (memcmp (register_buffer (current_regcache, regnum), myaddr, size)
809 target_prepare_to_store ();
812 memcpy (register_buffer (current_regcache, regnum), myaddr, size);
814 set_register_cached (regnum, 1);
815 target_store_registers (regnum);
819 regcache_raw_write (struct regcache *regcache, int regnum, const void *buf)
821 gdb_assert (regcache != NULL && buf != NULL);
822 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
824 if (regcache->passthrough_p
825 && regcache->descr->legacy_p)
827 /* For moment, just use underlying legacy code. Ulgh!!! This
828 silently and very indirectly updates the regcache's buffers
829 via the globals register_valid[] and registers[]. */
830 gdb_assert (regcache == current_regcache);
831 legacy_write_register_gen (regnum, buf);
835 /* On the sparc, writing %g0 is a no-op, so we don't even want to
836 change the registers array if something writes to this register. */
837 if (CANNOT_STORE_REGISTER (regnum))
840 /* Handle the simple case first -> not write through so just store
842 if (!regcache->passthrough_p)
844 memcpy ((regcache->raw_registers
845 + regcache->descr->register_offset[regnum]), buf,
846 regcache->descr->sizeof_register[regnum]);
847 regcache->raw_register_valid_p[regnum] = 1;
851 /* Make certain that the correct cache is selected. */
852 gdb_assert (regcache == current_regcache);
853 if (! ptid_equal (registers_ptid, inferior_ptid))
855 registers_changed ();
856 registers_ptid = inferior_ptid;
859 /* If we have a valid copy of the register, and new value == old
860 value, then don't bother doing the actual store. */
861 if (regcache_valid_p (regcache, regnum)
862 && (memcmp (register_buffer (regcache, regnum), buf,
863 regcache->descr->sizeof_register[regnum]) == 0))
866 target_prepare_to_store ();
867 memcpy (register_buffer (regcache, regnum), buf,
868 regcache->descr->sizeof_register[regnum]);
869 regcache->raw_register_valid_p[regnum] = 1;
870 target_store_registers (regnum);
874 write_register_gen (int regnum, char *buf)
876 gdb_assert (current_regcache != NULL);
877 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
878 if (current_regcache->descr->legacy_p)
880 legacy_write_register_gen (regnum, buf);
883 regcache_cooked_write (current_regcache, regnum, buf);
887 regcache_cooked_write (struct regcache *regcache, int regnum, const void *buf)
889 gdb_assert (regnum >= 0);
890 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
891 if (regnum < regcache->descr->nr_raw_registers)
892 regcache_raw_write (regcache, regnum, buf);
894 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
898 /* Copy INLEN bytes of consecutive data from memory at MYADDR
899 into registers starting with the MYREGSTART'th byte of register data. */
902 write_register_bytes (int myregstart, char *myaddr, int inlen)
904 int myregend = myregstart + inlen;
907 target_prepare_to_store ();
909 /* Scan through the registers updating any that are covered by the
910 range myregstart<=>myregend using write_register_gen, which does
911 nice things like handling threads, and avoiding updates when the
912 new and old contents are the same. */
914 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
916 int regstart, regend;
918 regstart = REGISTER_BYTE (regnum);
919 regend = regstart + REGISTER_RAW_SIZE (regnum);
921 /* Is this register completely outside the range the user is writing? */
922 if (myregend <= regstart || regend <= myregstart)
925 /* Is this register completely within the range the user is writing? */
926 else if (myregstart <= regstart && regend <= myregend)
927 write_register_gen (regnum, myaddr + (regstart - myregstart));
929 /* The register partially overlaps the range being written. */
932 char *regbuf = (char*) alloca (MAX_REGISTER_RAW_SIZE);
933 /* What's the overlap between this register's bytes and
934 those the caller wants to write? */
935 int overlapstart = max (regstart, myregstart);
936 int overlapend = min (regend, myregend);
938 /* We may be doing a partial update of an invalid register.
939 Update it from the target before scribbling on it. */
940 read_register_gen (regnum, regbuf);
942 memcpy (registers + overlapstart,
943 myaddr + (overlapstart - myregstart),
944 overlapend - overlapstart);
946 target_store_registers (regnum);
951 /* Perform a partial register transfer using a read, modify, write
954 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
956 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
960 regcache_xfer_part (struct regcache *regcache, int regnum,
961 int offset, int len, void *in, const void *out,
962 regcache_read_ftype *read, regcache_write_ftype *write)
964 struct regcache_descr *descr = regcache->descr;
965 bfd_byte *reg = alloca (descr->max_register_size);
966 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
967 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
968 /* Something to do? */
969 if (offset + len == 0)
971 /* Read (when needed) ... */
974 || offset + len < descr->sizeof_register[regnum])
976 gdb_assert (read != NULL);
977 read (regcache, regnum, reg);
981 memcpy (in, reg + offset, len);
983 memcpy (reg + offset, out, len);
984 /* ... write (when needed). */
987 gdb_assert (write != NULL);
988 write (regcache, regnum, reg);
993 regcache_raw_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_raw_registers);
998 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
999 regcache_raw_read, regcache_raw_write);
1003 regcache_raw_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_raw_registers);
1008 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1009 regcache_raw_read, regcache_raw_write);
1013 regcache_cooked_read_part (struct regcache *regcache, int regnum,
1014 int offset, int len, void *buf)
1016 struct regcache_descr *descr = regcache->descr;
1017 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1018 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1019 regcache_cooked_read, regcache_cooked_write);
1023 regcache_cooked_write_part (struct regcache *regcache, int regnum,
1024 int offset, int len, const void *buf)
1026 struct regcache_descr *descr = regcache->descr;
1027 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1028 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1029 regcache_cooked_read, regcache_cooked_write);
1032 /* Return the contents of register REGNUM as an unsigned integer. */
1035 read_register (int regnum)
1037 char *buf = alloca (REGISTER_RAW_SIZE (regnum));
1038 read_register_gen (regnum, buf);
1039 return (extract_unsigned_integer (buf, REGISTER_RAW_SIZE (regnum)));
1043 read_register_pid (int regnum, ptid_t ptid)
1049 if (ptid_equal (ptid, inferior_ptid))
1050 return read_register (regnum);
1052 save_ptid = inferior_ptid;
1054 inferior_ptid = ptid;
1056 retval = read_register (regnum);
1058 inferior_ptid = save_ptid;
1063 /* Return the contents of register REGNUM as a signed integer. */
1066 read_signed_register (int regnum)
1068 void *buf = alloca (REGISTER_RAW_SIZE (regnum));
1069 read_register_gen (regnum, buf);
1070 return (extract_signed_integer (buf, REGISTER_RAW_SIZE (regnum)));
1074 read_signed_register_pid (int regnum, ptid_t ptid)
1079 if (ptid_equal (ptid, inferior_ptid))
1080 return read_signed_register (regnum);
1082 save_ptid = inferior_ptid;
1084 inferior_ptid = ptid;
1086 retval = read_signed_register (regnum);
1088 inferior_ptid = save_ptid;
1093 /* Store VALUE into the raw contents of register number REGNUM. */
1096 write_register (int regnum, LONGEST val)
1100 size = REGISTER_RAW_SIZE (regnum);
1101 buf = alloca (size);
1102 store_signed_integer (buf, size, (LONGEST) val);
1103 write_register_gen (regnum, buf);
1107 write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
1111 if (ptid_equal (ptid, inferior_ptid))
1113 write_register (regnum, val);
1117 save_ptid = inferior_ptid;
1119 inferior_ptid = ptid;
1121 write_register (regnum, val);
1123 inferior_ptid = save_ptid;
1126 /* SUPPLY_REGISTER()
1128 Record that register REGNUM contains VAL. This is used when the
1129 value is obtained from the inferior or core dump, so there is no
1130 need to store the value there.
1132 If VAL is a NULL pointer, then it's probably an unsupported register.
1133 We just set its value to all zeros. We might want to record this
1134 fact, and report it to the users of read_register and friends. */
1137 supply_register (int regnum, const void *val)
1140 if (! ptid_equal (registers_ptid, inferior_ptid))
1142 registers_changed ();
1143 registers_ptid = inferior_ptid;
1147 set_register_cached (regnum, 1);
1149 memcpy (register_buffer (current_regcache, regnum), val,
1150 REGISTER_RAW_SIZE (regnum));
1152 memset (register_buffer (current_regcache, regnum), '\000',
1153 REGISTER_RAW_SIZE (regnum));
1155 /* On some architectures, e.g. HPPA, there are a few stray bits in
1156 some registers, that the rest of the code would like to ignore. */
1158 /* NOTE: cagney/2001-03-16: The macro CLEAN_UP_REGISTER_VALUE is
1159 going to be deprecated. Instead architectures will leave the raw
1160 register value as is and instead clean things up as they pass
1161 through the method gdbarch_pseudo_register_read() clean up the
1164 #ifdef DEPRECATED_CLEAN_UP_REGISTER_VALUE
1165 DEPRECATED_CLEAN_UP_REGISTER_VALUE \
1166 (regnum, register_buffer (current_regcache, regnum));
1171 regcache_collect (int regnum, void *buf)
1173 memcpy (buf, register_buffer (current_regcache, regnum),
1174 REGISTER_RAW_SIZE (regnum));
1178 /* read_pc, write_pc, read_sp, write_sp, read_fp, etc. Special
1179 handling for registers PC, SP, and FP. */
1181 /* NOTE: cagney/2001-02-18: The functions generic_target_read_pc(),
1182 read_pc_pid(), read_pc(), generic_target_write_pc(),
1183 write_pc_pid(), write_pc(), generic_target_read_sp(), read_sp(),
1184 generic_target_write_sp(), write_sp(), generic_target_read_fp() and
1185 read_fp(), will eventually be moved out of the reg-cache into
1186 either frame.[hc] or to the multi-arch framework. The are not part
1187 of the raw register cache. */
1189 /* This routine is getting awfully cluttered with #if's. It's probably
1190 time to turn this into READ_PC and define it in the tm.h file.
1193 1999-06-08: The following were re-written so that it assumes the
1194 existence of a TARGET_READ_PC et.al. macro. A default generic
1195 version of that macro is made available where needed.
1197 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
1198 by the multi-arch framework, it will eventually be possible to
1199 eliminate the intermediate read_pc_pid(). The client would call
1200 TARGET_READ_PC directly. (cagney). */
1203 generic_target_read_pc (ptid_t ptid)
1208 CORE_ADDR pc_val = ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, ptid));
1212 internal_error (__FILE__, __LINE__,
1213 "generic_target_read_pc");
1218 read_pc_pid (ptid_t ptid)
1220 ptid_t saved_inferior_ptid;
1223 /* In case ptid != inferior_ptid. */
1224 saved_inferior_ptid = inferior_ptid;
1225 inferior_ptid = ptid;
1227 pc_val = TARGET_READ_PC (ptid);
1229 inferior_ptid = saved_inferior_ptid;
1236 return read_pc_pid (inferior_ptid);
1240 generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
1244 write_register_pid (PC_REGNUM, pc, ptid);
1245 if (NPC_REGNUM >= 0)
1246 write_register_pid (NPC_REGNUM, pc + 4, ptid);
1248 internal_error (__FILE__, __LINE__,
1249 "generic_target_write_pc");
1254 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
1256 ptid_t saved_inferior_ptid;
1258 /* In case ptid != inferior_ptid. */
1259 saved_inferior_ptid = inferior_ptid;
1260 inferior_ptid = ptid;
1262 TARGET_WRITE_PC (pc, ptid);
1264 inferior_ptid = saved_inferior_ptid;
1268 write_pc (CORE_ADDR pc)
1270 write_pc_pid (pc, inferior_ptid);
1273 /* Cope with strage ways of getting to the stack and frame pointers */
1276 generic_target_read_sp (void)
1280 return read_register (SP_REGNUM);
1282 internal_error (__FILE__, __LINE__,
1283 "generic_target_read_sp");
1289 return TARGET_READ_SP ();
1293 generic_target_write_sp (CORE_ADDR val)
1298 write_register (SP_REGNUM, val);
1302 internal_error (__FILE__, __LINE__,
1303 "generic_target_write_sp");
1307 write_sp (CORE_ADDR val)
1309 TARGET_WRITE_SP (val);
1313 generic_target_read_fp (void)
1317 return read_register (FP_REGNUM);
1319 internal_error (__FILE__, __LINE__,
1320 "generic_target_read_fp");
1326 return TARGET_READ_FP ();
1331 reg_flush_command (char *command, int from_tty)
1333 /* Force-flush the register cache. */
1334 registers_changed ();
1336 printf_filtered ("Register cache flushed.\n");
1340 build_regcache (void)
1342 current_regcache = regcache_xmalloc (current_gdbarch);
1343 current_regcache->passthrough_p = 1;
1344 registers = deprecated_grub_regcache_for_registers (current_regcache);
1345 register_valid = deprecated_grub_regcache_for_register_valid (current_regcache);
1349 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1350 const unsigned char *buf, long len)
1355 case BFD_ENDIAN_BIG:
1356 for (i = 0; i < len; i++)
1357 fprintf_unfiltered (file, "%02x", buf[i]);
1359 case BFD_ENDIAN_LITTLE:
1360 for (i = len - 1; i >= 0; i--)
1361 fprintf_unfiltered (file, "%02x", buf[i]);
1364 internal_error (__FILE__, __LINE__, "Bad switch");
1368 enum regcache_dump_what
1370 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked
1374 regcache_dump (struct regcache *regcache, struct ui_file *file,
1375 enum regcache_dump_what what_to_dump)
1377 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1379 int footnote_nr = 0;
1380 int footnote_register_size = 0;
1381 int footnote_register_offset = 0;
1382 int footnote_register_type_name_null = 0;
1383 long register_offset = 0;
1384 unsigned char *buf = alloca (regcache->descr->max_register_size);
1387 fprintf_unfiltered (file, "legacy_p %d\n", regcache->descr->legacy_p);
1388 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1389 regcache->descr->nr_raw_registers);
1390 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1391 regcache->descr->nr_cooked_registers);
1392 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1393 regcache->descr->sizeof_raw_registers);
1394 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1395 regcache->descr->sizeof_raw_register_valid_p);
1396 fprintf_unfiltered (file, "max_register_size %ld\n",
1397 regcache->descr->max_register_size);
1398 fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1399 fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1402 gdb_assert (regcache->descr->nr_cooked_registers
1403 == (NUM_REGS + NUM_PSEUDO_REGS));
1405 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1409 fprintf_unfiltered (file, " %-10s", "Name");
1412 const char *p = REGISTER_NAME (regnum);
1415 else if (p[0] == '\0')
1417 fprintf_unfiltered (file, " %-10s", p);
1422 fprintf_unfiltered (file, " %4s", "Nr");
1424 fprintf_unfiltered (file, " %4d", regnum);
1426 /* Relative number. */
1428 fprintf_unfiltered (file, " %4s", "Rel");
1429 else if (regnum < NUM_REGS)
1430 fprintf_unfiltered (file, " %4d", regnum);
1432 fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1436 fprintf_unfiltered (file, " %6s ", "Offset");
1439 fprintf_unfiltered (file, " %6ld",
1440 regcache->descr->register_offset[regnum]);
1441 if (register_offset != regcache->descr->register_offset[regnum]
1442 || register_offset != REGISTER_BYTE (regnum))
1444 if (!footnote_register_offset)
1445 footnote_register_offset = ++footnote_nr;
1446 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1449 fprintf_unfiltered (file, " ");
1450 register_offset = (regcache->descr->register_offset[regnum]
1451 + regcache->descr->sizeof_register[regnum]);
1456 fprintf_unfiltered (file, " %5s ", "Size");
1459 fprintf_unfiltered (file, " %5ld",
1460 regcache->descr->sizeof_register[regnum]);
1461 if ((regcache->descr->sizeof_register[regnum]
1462 != REGISTER_RAW_SIZE (regnum))
1463 || (regcache->descr->sizeof_register[regnum]
1464 != REGISTER_VIRTUAL_SIZE (regnum))
1465 || (regcache->descr->sizeof_register[regnum]
1466 != TYPE_LENGTH (register_type (regcache->descr->gdbarch,
1470 if (!footnote_register_size)
1471 footnote_register_size = ++footnote_nr;
1472 fprintf_unfiltered (file, "*%d", footnote_register_size);
1475 fprintf_unfiltered (file, " ");
1480 fprintf_unfiltered (file, " %-20s", "Type");
1483 static const char blt[] = "builtin_type";
1484 const char *t = TYPE_NAME (register_type (regcache->descr->gdbarch,
1489 if (!footnote_register_type_name_null)
1490 footnote_register_type_name_null = ++footnote_nr;
1491 xasprintf (&n, "*%d", footnote_register_type_name_null);
1492 make_cleanup (xfree, n);
1495 /* Chop a leading builtin_type. */
1496 if (strncmp (t, blt, strlen (blt)) == 0)
1498 fprintf_unfiltered (file, " %-20s", t);
1502 if (what_to_dump == regcache_dump_raw)
1505 fprintf_unfiltered (file, "Raw value");
1506 else if (regnum >= regcache->descr->nr_raw_registers)
1507 fprintf_unfiltered (file, "<cooked>");
1508 else if (!regcache_valid_p (regcache, regnum))
1509 fprintf_unfiltered (file, "<invalid>");
1512 regcache_raw_read (regcache, regnum, buf);
1513 fprintf_unfiltered (file, "0x");
1514 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1515 REGISTER_RAW_SIZE (regnum));
1519 /* Value, cooked. */
1520 if (what_to_dump == regcache_dump_cooked)
1523 fprintf_unfiltered (file, "Cooked value");
1526 regcache_cooked_read (regcache, regnum, buf);
1527 fprintf_unfiltered (file, "0x");
1528 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1529 REGISTER_VIRTUAL_SIZE (regnum));
1533 fprintf_unfiltered (file, "\n");
1536 if (footnote_register_size)
1537 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1538 footnote_register_size);
1539 if (footnote_register_offset)
1540 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1541 footnote_register_offset);
1542 if (footnote_register_type_name_null)
1543 fprintf_unfiltered (file,
1544 "*%d: Register type's name NULL.\n",
1545 footnote_register_type_name_null);
1546 do_cleanups (cleanups);
1550 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1553 regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1556 struct ui_file *file = gdb_fopen (args, "w");
1558 perror_with_name ("maintenance print architecture");
1559 regcache_dump (current_regcache, file, what_to_dump);
1560 ui_file_delete (file);
1565 maintenance_print_registers (char *args, int from_tty)
1567 regcache_print (args, regcache_dump_none);
1571 maintenance_print_raw_registers (char *args, int from_tty)
1573 regcache_print (args, regcache_dump_raw);
1577 maintenance_print_cooked_registers (char *args, int from_tty)
1579 regcache_print (args, regcache_dump_cooked);
1583 _initialize_regcache (void)
1585 regcache_descr_handle = register_gdbarch_data (init_regcache_descr,
1586 xfree_regcache_descr);
1587 REGISTER_GDBARCH_SWAP (current_regcache);
1588 register_gdbarch_swap (®isters, sizeof (registers), NULL);
1589 register_gdbarch_swap (®ister_valid, sizeof (register_valid), NULL);
1590 register_gdbarch_swap (NULL, 0, build_regcache);
1592 add_com ("flushregs", class_maintenance, reg_flush_command,
1593 "Force gdb to flush its register cache (maintainer command)");
1595 /* Initialize the thread/process associated with the current set of
1596 registers. For now, -1 is special, and means `no current process'. */
1597 registers_ptid = pid_to_ptid (-1);
1599 add_cmd ("registers", class_maintenance,
1600 maintenance_print_registers,
1601 "Print the internal register configuration.\
1602 Takes an optional file parameter.",
1603 &maintenanceprintlist);
1604 add_cmd ("raw-registers", class_maintenance,
1605 maintenance_print_raw_registers,
1606 "Print the internal register configuration including raw values.\
1607 Takes an optional file parameter.",
1608 &maintenanceprintlist);
1609 add_cmd ("cooked-registers", class_maintenance,
1610 maintenance_print_cooked_registers,
1611 "Print the internal register configuration including cooked values.\
1612 Takes an optional file parameter.",
1613 &maintenanceprintlist);