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"
35 * Here is the actual register cache.
38 /* Per-architecture object describing the layout of a register cache.
39 Computed once when the architecture is created */
41 struct gdbarch_data
*regcache_descr_handle
;
45 /* The architecture this descriptor belongs to. */
46 struct gdbarch
*gdbarch
;
48 /* Is this a ``legacy'' register cache? Such caches reserve space
49 for raw and pseudo registers and allow access to both. */
52 /* The raw register cache. This should contain just [0
53 .. NUM_RAW_REGISTERS). However, for older targets, it contains
54 space for the full [0 .. NUM_RAW_REGISTERS +
55 NUM_PSEUDO_REGISTERS). */
57 long sizeof_raw_registers
;
58 long sizeof_raw_register_valid_p
;
60 /* The cooked register space. Each cooked register in the range
61 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
62 register. The remaining [NR_RAW_REGISTERS
63 .. NR_COOKED_REGISTERS) (a.k.a. pseudo regiters) are mapped onto
64 both raw registers and memory by the architecture methods
65 gdbarch_register_read and gdbarch_register_write. */
66 int nr_cooked_registers
;
68 /* Offset and size (in 8 bit bytes), of reach register in the
69 register cache. All registers (including those in the range
70 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
71 Assigning all registers an offset makes it possible to keep
72 legacy code, such as that found in read_register_bytes() and
73 write_register_bytes() working. */
74 long *register_offset
;
75 long *sizeof_register
;
77 /* Useful constant. Largest of all the registers. */
78 long max_register_size
;
82 init_legacy_regcache_descr (struct gdbarch
*gdbarch
)
85 struct regcache_descr
*descr
;
86 /* FIXME: cagney/2002-05-11: gdbarch_data() should take that
87 ``gdbarch'' as a parameter. */
88 gdb_assert (gdbarch
!= NULL
);
90 descr
= XMALLOC (struct regcache_descr
);
91 descr
->gdbarch
= gdbarch
;
94 /* FIXME: cagney/2002-05-11: Shouldn't be including pseudo-registers
95 in the register buffer. Unfortunatly some architectures do. */
96 descr
->nr_cooked_registers
= NUM_REGS
+ NUM_PSEUDO_REGS
;
97 descr
->nr_raw_registers
= descr
->nr_cooked_registers
;
98 descr
->sizeof_raw_register_valid_p
= descr
->nr_cooked_registers
;
100 /* FIXME: cagney/2002-05-11: Instead of using REGISTER_BYTE() this
101 code should compute the offets et.al. at runtime. This currently
102 isn't possible because some targets overlap register locations -
103 see the mess in read_register_bytes() and write_register_bytes()
105 descr
->sizeof_register
= XCALLOC (descr
->nr_cooked_registers
, long);
106 descr
->register_offset
= XCALLOC (descr
->nr_cooked_registers
, long);
107 descr
->max_register_size
= 0;
108 for (i
= 0; i
< descr
->nr_cooked_registers
; i
++)
110 descr
->register_offset
[i
] = REGISTER_BYTE (i
);
111 descr
->sizeof_register
[i
] = REGISTER_RAW_SIZE (i
);
112 if (descr
->max_register_size
< REGISTER_RAW_SIZE (i
))
113 descr
->max_register_size
= REGISTER_RAW_SIZE (i
);
116 /* Come up with the real size of the registers buffer. */
117 descr
->sizeof_raw_registers
= REGISTER_BYTES
; /* OK use. */
118 for (i
= 0; i
< descr
->nr_cooked_registers
; i
++)
121 /* Keep extending the buffer so that there is always enough
122 space for all registers. The comparison is necessary since
123 legacy code is free to put registers in random places in the
124 buffer separated by holes. Once REGISTER_BYTE() is killed
125 this can be greatly simplified. */
126 /* FIXME: cagney/2001-12-04: This code shouldn't need to use
127 REGISTER_BYTE(). Unfortunatly, legacy code likes to lay the
128 buffer out so that certain registers just happen to overlap.
129 Ulgh! New targets use gdbarch's register read/write and
130 entirely avoid this uglyness. */
131 regend
= descr
->register_offset
[i
] + descr
->sizeof_register
[i
];
132 if (descr
->sizeof_raw_registers
< regend
)
133 descr
->sizeof_raw_registers
= regend
;
139 init_regcache_descr (struct gdbarch
*gdbarch
)
142 struct regcache_descr
*descr
;
143 gdb_assert (gdbarch
!= NULL
);
145 /* If an old style architecture, construct the register cache
146 description using all the register macros. */
147 if (!gdbarch_pseudo_register_read_p (gdbarch
)
148 && !gdbarch_pseudo_register_write_p (gdbarch
))
149 return init_legacy_regcache_descr (gdbarch
);
151 descr
= XMALLOC (struct regcache_descr
);
152 descr
->gdbarch
= gdbarch
;
155 /* Total size of the register space. The raw registers are mapped
156 directly onto the raw register cache while the pseudo's are
157 either mapped onto raw-registers or memory. */
158 descr
->nr_cooked_registers
= NUM_REGS
+ NUM_PSEUDO_REGS
;
160 /* Construct a strictly RAW register cache. Don't allow pseudo's
161 into the register cache. */
162 descr
->nr_raw_registers
= NUM_REGS
;
163 descr
->sizeof_raw_register_valid_p
= NUM_REGS
;
165 /* Lay out the register cache. The pseud-registers are included in
166 the layout even though their value isn't stored in the register
167 cache. Some code, via read_register_bytes() access a register
168 using an offset/length rather than a register number.
170 NOTE: cagney/2002-05-22: Only REGISTER_VIRTUAL_TYPE() needs to be
171 used when constructing the register cache. It is assumed that
172 register raw size, virtual size and type length of the type are
177 descr
->sizeof_register
= XCALLOC (descr
->nr_cooked_registers
, long);
178 descr
->register_offset
= XCALLOC (descr
->nr_cooked_registers
, long);
179 descr
->max_register_size
= 0;
180 for (i
= 0; i
< descr
->nr_cooked_registers
; i
++)
182 descr
->sizeof_register
[i
] = TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (i
));
183 descr
->register_offset
[i
] = offset
;
184 offset
+= descr
->sizeof_register
[i
];
185 if (descr
->max_register_size
< descr
->sizeof_register
[i
])
186 descr
->max_register_size
= descr
->sizeof_register
[i
];
188 /* Set the real size of the register cache buffer. */
189 /* FIXME: cagney/2002-05-22: Should only need to allocate space
190 for the raw registers. Unfortunatly some code still accesses
191 the register array directly using the global registers[].
192 Until that code has been purged, play safe and over allocating
193 the register buffer. Ulgh! */
194 descr
->sizeof_raw_registers
= offset
;
195 /* = descr->register_offset[descr->nr_raw_registers]; */
199 /* Sanity check. Confirm that the assumptions about gdbarch are
200 true. The REGCACHE_DESCR_HANDLE is set before doing the checks
201 so that targets using the generic methods supplied by regcache
202 don't go into infinite recursion trying to, again, create the
204 set_gdbarch_data (gdbarch
, regcache_descr_handle
, descr
);
205 for (i
= 0; i
< descr
->nr_cooked_registers
; i
++)
207 gdb_assert (descr
->sizeof_register
[i
] == REGISTER_RAW_SIZE (i
));
208 gdb_assert (descr
->sizeof_register
[i
] == REGISTER_VIRTUAL_SIZE (i
));
209 gdb_assert (descr
->register_offset
[i
] == REGISTER_BYTE (i
));
211 /* gdb_assert (descr->sizeof_raw_registers == REGISTER_BYTES (i)); */
217 static struct regcache_descr
*
218 regcache_descr (struct gdbarch
*gdbarch
)
220 return gdbarch_data (gdbarch
, regcache_descr_handle
);
224 xfree_regcache_descr (struct gdbarch
*gdbarch
, void *ptr
)
226 struct regcache_descr
*descr
= ptr
;
229 xfree (descr
->register_offset
);
230 xfree (descr
->sizeof_register
);
231 descr
->register_offset
= NULL
;
232 descr
->sizeof_register
= NULL
;
236 /* The register cache for storing raw register values. */
240 struct regcache_descr
*descr
;
242 char *raw_register_valid_p
;
243 /* If a value isn't in the cache should the corresponding target be
244 queried for a value. */
249 regcache_xmalloc (struct gdbarch
*gdbarch
)
251 struct regcache_descr
*descr
;
252 struct regcache
*regcache
;
253 gdb_assert (gdbarch
!= NULL
);
254 descr
= regcache_descr (gdbarch
);
255 regcache
= XMALLOC (struct regcache
);
256 regcache
->descr
= descr
;
257 regcache
->raw_registers
258 = XCALLOC (descr
->sizeof_raw_registers
, char);
259 regcache
->raw_register_valid_p
260 = XCALLOC (descr
->sizeof_raw_register_valid_p
, char);
261 regcache
->passthrough_p
= 0;
266 regcache_xfree (struct regcache
*regcache
)
268 if (regcache
== NULL
)
270 xfree (regcache
->raw_registers
);
271 xfree (regcache
->raw_register_valid_p
);
276 do_regcache_xfree (void *data
)
278 regcache_xfree (data
);
282 make_cleanup_regcache_xfree (struct regcache
*regcache
)
284 return make_cleanup (do_regcache_xfree
, regcache
);
288 regcache_cpy (struct regcache
*dst
, struct regcache
*src
)
292 gdb_assert (src
!= NULL
&& dst
!= NULL
);
293 gdb_assert (src
->descr
->gdbarch
== dst
->descr
->gdbarch
);
294 gdb_assert (src
!= dst
);
295 /* FIXME: cagney/2002-05-17: To say this bit is bad is being polite.
296 It keeps the existing code working where things rely on going
297 through to the register cache. */
298 if (src
== current_regcache
&& src
->descr
->legacy_p
)
300 /* ULGH!!!! Old way. Use REGISTER bytes and let code below
302 read_register_bytes (0, dst
->raw_registers
, REGISTER_BYTES
);
305 /* FIXME: cagney/2002-05-17: To say this bit is bad is being polite.
306 It keeps the existing code working where things rely on going
307 through to the register cache. */
308 if (dst
== current_regcache
&& dst
->descr
->legacy_p
)
310 /* ULGH!!!! Old way. Use REGISTER bytes and let code below
312 write_register_bytes (0, src
->raw_registers
, REGISTER_BYTES
);
315 buf
= alloca (src
->descr
->max_register_size
);
316 for (i
= 0; i
< src
->descr
->nr_raw_registers
; i
++)
318 /* Should we worry about the valid bit here? */
319 regcache_raw_read (src
, i
, buf
);
320 regcache_raw_write (dst
, i
, buf
);
325 regcache_cpy_no_passthrough (struct regcache
*dst
, struct regcache
*src
)
328 gdb_assert (src
!= NULL
&& dst
!= NULL
);
329 gdb_assert (src
->descr
->gdbarch
== dst
->descr
->gdbarch
);
330 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
331 move of data into the current_regcache(). Doing this would be
332 silly - it would mean that valid_p would be completly invalid. */
333 gdb_assert (dst
!= current_regcache
);
334 memcpy (dst
->raw_registers
, src
->raw_registers
,
335 dst
->descr
->sizeof_raw_registers
);
336 memcpy (dst
->raw_register_valid_p
, src
->raw_register_valid_p
,
337 dst
->descr
->sizeof_raw_register_valid_p
);
341 regcache_dup (struct regcache
*src
)
343 struct regcache
*newbuf
;
344 gdb_assert (current_regcache
!= NULL
);
345 newbuf
= regcache_xmalloc (src
->descr
->gdbarch
);
346 regcache_cpy (newbuf
, src
);
351 regcache_dup_no_passthrough (struct regcache
*src
)
353 struct regcache
*newbuf
;
354 gdb_assert (current_regcache
!= NULL
);
355 newbuf
= regcache_xmalloc (src
->descr
->gdbarch
);
356 regcache_cpy_no_passthrough (newbuf
, src
);
361 regcache_valid_p (struct regcache
*regcache
, int regnum
)
363 gdb_assert (regcache
!= NULL
);
364 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
365 return regcache
->raw_register_valid_p
[regnum
];
369 regcache_raw_read_as_address (struct regcache
*regcache
, int regnum
)
372 gdb_assert (regcache
!= NULL
);
373 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
374 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
375 regcache_raw_read (regcache
, regnum
, buf
);
376 return extract_address (buf
, regcache
->descr
->sizeof_register
[regnum
]);
380 deprecated_grub_regcache_for_registers (struct regcache
*regcache
)
382 return regcache
->raw_registers
;
386 deprecated_grub_regcache_for_register_valid (struct regcache
*regcache
)
388 return regcache
->raw_register_valid_p
;
391 /* Global structure containing the current regcache. */
392 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
393 register_valid[] currently point into this structure. */
394 struct regcache
*current_regcache
;
396 /* NOTE: this is a write-through cache. There is no "dirty" bit for
397 recording if the register values have been changed (eg. by the
398 user). Therefore all registers must be written back to the
399 target when appropriate. */
401 /* REGISTERS contains the cached register values (in target byte order). */
405 /* REGISTER_VALID is 0 if the register needs to be fetched,
406 1 if it has been fetched, and
407 -1 if the register value was not available.
409 "Not available" indicates that the target is not not able to supply
410 the register at this state. The register may become available at a
411 later time (after the next resume). This often occures when GDB is
412 manipulating a target that contains only a snapshot of the entire
413 system being debugged - some of the registers in such a system may
414 not have been saved. */
416 signed char *register_valid
;
418 /* The thread/process associated with the current set of registers. */
420 static ptid_t registers_ptid
;
428 Returns 0 if the value is not in the cache (needs fetch).
429 >0 if the value is in the cache.
430 <0 if the value is permanently unavailable (don't ask again). */
433 register_cached (int regnum
)
435 return register_valid
[regnum
];
438 /* Record that REGNUM's value is cached if STATE is >0, uncached but
439 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
442 set_register_cached (int regnum
, int state
)
444 register_valid
[regnum
] = state
;
449 invalidate a single register REGNUM in the cache */
451 register_changed (int regnum
)
453 set_register_cached (regnum
, 0);
456 /* If REGNUM >= 0, return a pointer to register REGNUM's cache buffer area,
457 else return a pointer to the start of the cache buffer. */
460 register_buffer (struct regcache
*regcache
, int regnum
)
462 return regcache
->raw_registers
+ regcache
->descr
->register_offset
[regnum
];
465 /* Return whether register REGNUM is a real register. */
468 real_register (int regnum
)
470 return regnum
>= 0 && regnum
< NUM_REGS
;
473 /* Low level examining and depositing of registers.
475 The caller is responsible for making sure that the inferior is
476 stopped before calling the fetching routines, or it will get
477 garbage. (a change from GDB version 3, in which the caller got the
478 value from the last stop). */
480 /* REGISTERS_CHANGED ()
482 Indicate that registers may have changed, so invalidate the cache. */
485 registers_changed (void)
489 registers_ptid
= pid_to_ptid (-1);
491 /* Force cleanup of any alloca areas if using C alloca instead of
492 a builtin alloca. This particular call is used to clean up
493 areas allocated by low level target code which may build up
494 during lengthy interactions between gdb and the target before
495 gdb gives control to the user (ie watchpoints). */
498 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
499 set_register_cached (i
, 0);
501 if (registers_changed_hook
)
502 registers_changed_hook ();
505 /* REGISTERS_FETCHED ()
507 Indicate that all registers have been fetched, so mark them all valid. */
509 /* NOTE: cagney/2001-12-04: This function does not set valid on the
510 pseudo-register range since pseudo registers are always supplied
511 using supply_register(). */
512 /* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
513 code was blatting the registers[] array and then calling this.
514 Since targets should only be using supply_register() the need for
515 this function/hack is eliminated. */
518 registers_fetched (void)
522 for (i
= 0; i
< NUM_REGS
; i
++)
523 set_register_cached (i
, 1);
524 /* Do not assume that the pseudo-regs have also been fetched.
525 Fetching all real regs NEVER accounts for pseudo-regs. */
528 /* read_register_bytes and write_register_bytes are generally a *BAD*
529 idea. They are inefficient because they need to check for partial
530 updates, which can only be done by scanning through all of the
531 registers and seeing if the bytes that are being read/written fall
532 inside of an invalid register. [The main reason this is necessary
533 is that register sizes can vary, so a simple index won't suffice.]
534 It is far better to call read_register_gen and write_register_gen
535 if you want to get at the raw register contents, as it only takes a
536 regnum as an argument, and therefore can't do a partial register
539 Prior to the recent fixes to check for partial updates, both read
540 and write_register_bytes always checked to see if any registers
541 were stale, and then called target_fetch_registers (-1) to update
542 the whole set. This caused really slowed things down for remote
545 /* Copy INLEN bytes of consecutive data from registers
546 starting with the INREGBYTE'th byte of register data
547 into memory at MYADDR. */
550 read_register_bytes (int in_start
, char *in_buf
, int in_len
)
552 int in_end
= in_start
+ in_len
;
554 char *reg_buf
= alloca (MAX_REGISTER_RAW_SIZE
);
556 /* See if we are trying to read bytes from out-of-date registers. If so,
557 update just those registers. */
559 for (regnum
= 0; regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
; regnum
++)
568 reg_start
= REGISTER_BYTE (regnum
);
569 reg_len
= REGISTER_RAW_SIZE (regnum
);
570 reg_end
= reg_start
+ reg_len
;
572 if (reg_end
<= in_start
|| in_end
<= reg_start
)
573 /* The range the user wants to read doesn't overlap with regnum. */
576 if (REGISTER_NAME (regnum
) != NULL
&& *REGISTER_NAME (regnum
) != '\0')
577 /* Force the cache to fetch the entire register. */
578 read_register_gen (regnum
, reg_buf
);
580 /* Legacy note: even though this register is ``invalid'' we
581 still need to return something. It would appear that some
582 code relies on apparent gaps in the register array also
584 /* FIXME: cagney/2001-08-18: This is just silly. It defeats
585 the entire register read/write flow of control. Must
586 resist temptation to return 0xdeadbeef. */
587 memcpy (reg_buf
, registers
+ reg_start
, reg_len
);
589 /* Legacy note: This function, for some reason, allows a NULL
590 input buffer. If the buffer is NULL, the registers are still
591 fetched, just the final transfer is skipped. */
595 /* start = max (reg_start, in_start) */
596 if (reg_start
> in_start
)
601 /* end = min (reg_end, in_end) */
602 if (reg_end
< in_end
)
607 /* Transfer just the bytes common to both IN_BUF and REG_BUF */
608 for (byte
= start
; byte
< end
; byte
++)
610 in_buf
[byte
- in_start
] = reg_buf
[byte
- reg_start
];
615 /* Read register REGNUM into memory at MYADDR, which must be large
616 enough for REGISTER_RAW_BYTES (REGNUM). Target byte-order. If the
617 register is known to be the size of a CORE_ADDR or smaller,
618 read_register can be used instead. */
621 legacy_read_register_gen (int regnum
, char *myaddr
)
623 gdb_assert (regnum
>= 0 && regnum
< (NUM_REGS
+ NUM_PSEUDO_REGS
));
624 if (! ptid_equal (registers_ptid
, inferior_ptid
))
626 registers_changed ();
627 registers_ptid
= inferior_ptid
;
630 if (!register_cached (regnum
))
631 target_fetch_registers (regnum
);
633 memcpy (myaddr
, register_buffer (current_regcache
, regnum
),
634 REGISTER_RAW_SIZE (regnum
));
638 regcache_raw_read (struct regcache
*regcache
, int regnum
, void *buf
)
640 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
641 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
642 if (regcache
->descr
->legacy_p
643 && regcache
->passthrough_p
)
645 gdb_assert (regcache
== current_regcache
);
646 /* For moment, just use underlying legacy code. Ulgh!!! This
647 silently and very indirectly updates the regcache's regcache
648 via the global register_valid[]. */
649 legacy_read_register_gen (regnum
, buf
);
652 /* Make certain that the register cache is up-to-date with respect
653 to the current thread. This switching shouldn't be necessary
654 only there is still only one target side register cache. Sigh!
655 On the bright side, at least there is a regcache object. */
656 if (regcache
->passthrough_p
)
658 gdb_assert (regcache
== current_regcache
);
659 if (! ptid_equal (registers_ptid
, inferior_ptid
))
661 registers_changed ();
662 registers_ptid
= inferior_ptid
;
664 if (!register_cached (regnum
))
665 target_fetch_registers (regnum
);
667 /* Copy the value directly into the register cache. */
668 memcpy (buf
, (regcache
->raw_registers
669 + regcache
->descr
->register_offset
[regnum
]),
670 regcache
->descr
->sizeof_register
[regnum
]);
674 read_register_gen (int regnum
, char *buf
)
676 gdb_assert (current_regcache
!= NULL
);
677 gdb_assert (current_regcache
->descr
->gdbarch
== current_gdbarch
);
678 if (current_regcache
->descr
->legacy_p
)
680 legacy_read_register_gen (regnum
, buf
);
683 regcache_cooked_read (current_regcache
, regnum
, buf
);
687 regcache_cooked_read (struct regcache
*regcache
, int regnum
, void *buf
)
689 gdb_assert (regnum
>= 0);
690 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
691 if (regnum
< regcache
->descr
->nr_raw_registers
)
692 regcache_raw_read (regcache
, regnum
, buf
);
694 gdbarch_pseudo_register_read (regcache
->descr
->gdbarch
, regcache
,
698 /* Write register REGNUM at MYADDR to the target. MYADDR points at
699 REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order. */
702 legacy_write_register_gen (int regnum
, const void *myaddr
)
705 gdb_assert (regnum
>= 0 && regnum
< (NUM_REGS
+ NUM_PSEUDO_REGS
));
707 /* On the sparc, writing %g0 is a no-op, so we don't even want to
708 change the registers array if something writes to this register. */
709 if (CANNOT_STORE_REGISTER (regnum
))
712 if (! ptid_equal (registers_ptid
, inferior_ptid
))
714 registers_changed ();
715 registers_ptid
= inferior_ptid
;
718 size
= REGISTER_RAW_SIZE (regnum
);
720 if (real_register (regnum
))
722 /* If we have a valid copy of the register, and new value == old
723 value, then don't bother doing the actual store. */
724 if (register_cached (regnum
)
725 && (memcmp (register_buffer (current_regcache
, regnum
), myaddr
, size
)
729 target_prepare_to_store ();
732 memcpy (register_buffer (current_regcache
, regnum
), myaddr
, size
);
734 set_register_cached (regnum
, 1);
735 target_store_registers (regnum
);
739 regcache_raw_write (struct regcache
*regcache
, int regnum
, const void *buf
)
741 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
742 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
744 if (regcache
->passthrough_p
745 && regcache
->descr
->legacy_p
)
747 /* For moment, just use underlying legacy code. Ulgh!!! This
748 silently and very indirectly updates the regcache's buffers
749 via the globals register_valid[] and registers[]. */
750 gdb_assert (regcache
== current_regcache
);
751 legacy_write_register_gen (regnum
, buf
);
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 /* Handle the simple case first -> not write through so just store
762 if (!regcache
->passthrough_p
)
764 memcpy ((regcache
->raw_registers
765 + regcache
->descr
->register_offset
[regnum
]), buf
,
766 regcache
->descr
->sizeof_register
[regnum
]);
767 regcache
->raw_register_valid_p
[regnum
] = 1;
771 /* Make certain that the correct cache is selected. */
772 gdb_assert (regcache
== current_regcache
);
773 if (! ptid_equal (registers_ptid
, inferior_ptid
))
775 registers_changed ();
776 registers_ptid
= inferior_ptid
;
779 /* If we have a valid copy of the register, and new value == old
780 value, then don't bother doing the actual store. */
781 if (regcache_valid_p (regcache
, regnum
)
782 && (memcmp (register_buffer (regcache
, regnum
), buf
,
783 regcache
->descr
->sizeof_register
[regnum
]) == 0))
786 target_prepare_to_store ();
787 memcpy (register_buffer (regcache
, regnum
), buf
,
788 regcache
->descr
->sizeof_register
[regnum
]);
789 regcache
->raw_register_valid_p
[regnum
] = 1;
790 target_store_registers (regnum
);
794 write_register_gen (int regnum
, char *buf
)
796 gdb_assert (current_regcache
!= NULL
);
797 gdb_assert (current_regcache
->descr
->gdbarch
== current_gdbarch
);
798 if (current_regcache
->descr
->legacy_p
)
800 legacy_write_register_gen (regnum
, buf
);
803 regcache_cooked_write (current_regcache
, regnum
, buf
);
807 regcache_cooked_write (struct regcache
*regcache
, int regnum
, const void *buf
)
809 gdb_assert (regnum
>= 0);
810 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
811 if (regnum
< regcache
->descr
->nr_raw_registers
)
812 regcache_raw_write (regcache
, regnum
, buf
);
814 gdbarch_pseudo_register_write (regcache
->descr
->gdbarch
, regcache
,
818 /* Copy INLEN bytes of consecutive data from memory at MYADDR
819 into registers starting with the MYREGSTART'th byte of register data. */
822 write_register_bytes (int myregstart
, char *myaddr
, int inlen
)
824 int myregend
= myregstart
+ inlen
;
827 target_prepare_to_store ();
829 /* Scan through the registers updating any that are covered by the
830 range myregstart<=>myregend using write_register_gen, which does
831 nice things like handling threads, and avoiding updates when the
832 new and old contents are the same. */
834 for (regnum
= 0; regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
; regnum
++)
836 int regstart
, regend
;
838 regstart
= REGISTER_BYTE (regnum
);
839 regend
= regstart
+ REGISTER_RAW_SIZE (regnum
);
841 /* Is this register completely outside the range the user is writing? */
842 if (myregend
<= regstart
|| regend
<= myregstart
)
845 /* Is this register completely within the range the user is writing? */
846 else if (myregstart
<= regstart
&& regend
<= myregend
)
847 write_register_gen (regnum
, myaddr
+ (regstart
- myregstart
));
849 /* The register partially overlaps the range being written. */
852 char *regbuf
= (char*) alloca (MAX_REGISTER_RAW_SIZE
);
853 /* What's the overlap between this register's bytes and
854 those the caller wants to write? */
855 int overlapstart
= max (regstart
, myregstart
);
856 int overlapend
= min (regend
, myregend
);
858 /* We may be doing a partial update of an invalid register.
859 Update it from the target before scribbling on it. */
860 read_register_gen (regnum
, regbuf
);
862 memcpy (registers
+ overlapstart
,
863 myaddr
+ (overlapstart
- myregstart
),
864 overlapend
- overlapstart
);
866 target_store_registers (regnum
);
872 /* Return the contents of register REGNUM as an unsigned integer. */
875 read_register (int regnum
)
877 char *buf
= alloca (REGISTER_RAW_SIZE (regnum
));
878 read_register_gen (regnum
, buf
);
879 return (extract_unsigned_integer (buf
, REGISTER_RAW_SIZE (regnum
)));
883 read_register_pid (int regnum
, ptid_t ptid
)
889 if (ptid_equal (ptid
, inferior_ptid
))
890 return read_register (regnum
);
892 save_ptid
= inferior_ptid
;
894 inferior_ptid
= ptid
;
896 retval
= read_register (regnum
);
898 inferior_ptid
= save_ptid
;
903 /* Return the contents of register REGNUM as a signed integer. */
906 read_signed_register (int regnum
)
908 void *buf
= alloca (REGISTER_RAW_SIZE (regnum
));
909 read_register_gen (regnum
, buf
);
910 return (extract_signed_integer (buf
, REGISTER_RAW_SIZE (regnum
)));
914 read_signed_register_pid (int regnum
, ptid_t ptid
)
919 if (ptid_equal (ptid
, inferior_ptid
))
920 return read_signed_register (regnum
);
922 save_ptid
= inferior_ptid
;
924 inferior_ptid
= ptid
;
926 retval
= read_signed_register (regnum
);
928 inferior_ptid
= save_ptid
;
933 /* Store VALUE into the raw contents of register number REGNUM. */
936 write_register (int regnum
, LONGEST val
)
940 size
= REGISTER_RAW_SIZE (regnum
);
942 store_signed_integer (buf
, size
, (LONGEST
) val
);
943 write_register_gen (regnum
, buf
);
947 write_register_pid (int regnum
, CORE_ADDR val
, ptid_t ptid
)
951 if (ptid_equal (ptid
, inferior_ptid
))
953 write_register (regnum
, val
);
957 save_ptid
= inferior_ptid
;
959 inferior_ptid
= ptid
;
961 write_register (regnum
, val
);
963 inferior_ptid
= save_ptid
;
968 Record that register REGNUM contains VAL. This is used when the
969 value is obtained from the inferior or core dump, so there is no
970 need to store the value there.
972 If VAL is a NULL pointer, then it's probably an unsupported register.
973 We just set its value to all zeros. We might want to record this
974 fact, and report it to the users of read_register and friends. */
977 supply_register (int regnum
, const void *val
)
980 if (! ptid_equal (registers_ptid
, inferior_ptid
))
982 registers_changed ();
983 registers_ptid
= inferior_ptid
;
987 set_register_cached (regnum
, 1);
989 memcpy (register_buffer (current_regcache
, regnum
), val
,
990 REGISTER_RAW_SIZE (regnum
));
992 memset (register_buffer (current_regcache
, regnum
), '\000',
993 REGISTER_RAW_SIZE (regnum
));
995 /* On some architectures, e.g. HPPA, there are a few stray bits in
996 some registers, that the rest of the code would like to ignore. */
998 /* NOTE: cagney/2001-03-16: The macro CLEAN_UP_REGISTER_VALUE is
999 going to be deprecated. Instead architectures will leave the raw
1000 register value as is and instead clean things up as they pass
1001 through the method gdbarch_pseudo_register_read() clean up the
1004 #ifdef DEPRECATED_CLEAN_UP_REGISTER_VALUE
1005 DEPRECATED_CLEAN_UP_REGISTER_VALUE \
1006 (regnum
, register_buffer (current_regcache
, regnum
));
1011 regcache_collect (int regnum
, void *buf
)
1013 memcpy (buf
, register_buffer (current_regcache
, regnum
),
1014 REGISTER_RAW_SIZE (regnum
));
1018 /* read_pc, write_pc, read_sp, write_sp, read_fp, etc. Special
1019 handling for registers PC, SP, and FP. */
1021 /* NOTE: cagney/2001-02-18: The functions generic_target_read_pc(),
1022 read_pc_pid(), read_pc(), generic_target_write_pc(),
1023 write_pc_pid(), write_pc(), generic_target_read_sp(), read_sp(),
1024 generic_target_write_sp(), write_sp(), generic_target_read_fp() and
1025 read_fp(), will eventually be moved out of the reg-cache into
1026 either frame.[hc] or to the multi-arch framework. The are not part
1027 of the raw register cache. */
1029 /* This routine is getting awfully cluttered with #if's. It's probably
1030 time to turn this into READ_PC and define it in the tm.h file.
1033 1999-06-08: The following were re-written so that it assumes the
1034 existence of a TARGET_READ_PC et.al. macro. A default generic
1035 version of that macro is made available where needed.
1037 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
1038 by the multi-arch framework, it will eventually be possible to
1039 eliminate the intermediate read_pc_pid(). The client would call
1040 TARGET_READ_PC directly. (cagney). */
1043 generic_target_read_pc (ptid_t ptid
)
1048 CORE_ADDR pc_val
= ADDR_BITS_REMOVE ((CORE_ADDR
) read_register_pid (PC_REGNUM
, ptid
));
1052 internal_error (__FILE__
, __LINE__
,
1053 "generic_target_read_pc");
1058 read_pc_pid (ptid_t ptid
)
1060 ptid_t saved_inferior_ptid
;
1063 /* In case ptid != inferior_ptid. */
1064 saved_inferior_ptid
= inferior_ptid
;
1065 inferior_ptid
= ptid
;
1067 pc_val
= TARGET_READ_PC (ptid
);
1069 inferior_ptid
= saved_inferior_ptid
;
1076 return read_pc_pid (inferior_ptid
);
1080 generic_target_write_pc (CORE_ADDR pc
, ptid_t ptid
)
1084 write_register_pid (PC_REGNUM
, pc
, ptid
);
1085 if (NPC_REGNUM
>= 0)
1086 write_register_pid (NPC_REGNUM
, pc
+ 4, ptid
);
1088 internal_error (__FILE__
, __LINE__
,
1089 "generic_target_write_pc");
1094 write_pc_pid (CORE_ADDR pc
, ptid_t ptid
)
1096 ptid_t saved_inferior_ptid
;
1098 /* In case ptid != inferior_ptid. */
1099 saved_inferior_ptid
= inferior_ptid
;
1100 inferior_ptid
= ptid
;
1102 TARGET_WRITE_PC (pc
, ptid
);
1104 inferior_ptid
= saved_inferior_ptid
;
1108 write_pc (CORE_ADDR pc
)
1110 write_pc_pid (pc
, inferior_ptid
);
1113 /* Cope with strage ways of getting to the stack and frame pointers */
1116 generic_target_read_sp (void)
1120 return read_register (SP_REGNUM
);
1122 internal_error (__FILE__
, __LINE__
,
1123 "generic_target_read_sp");
1129 return TARGET_READ_SP ();
1133 generic_target_write_sp (CORE_ADDR val
)
1138 write_register (SP_REGNUM
, val
);
1142 internal_error (__FILE__
, __LINE__
,
1143 "generic_target_write_sp");
1147 write_sp (CORE_ADDR val
)
1149 TARGET_WRITE_SP (val
);
1153 generic_target_read_fp (void)
1157 return read_register (FP_REGNUM
);
1159 internal_error (__FILE__
, __LINE__
,
1160 "generic_target_read_fp");
1166 return TARGET_READ_FP ();
1171 reg_flush_command (char *command
, int from_tty
)
1173 /* Force-flush the register cache. */
1174 registers_changed ();
1176 printf_filtered ("Register cache flushed.\n");
1180 build_regcache (void)
1182 current_regcache
= regcache_xmalloc (current_gdbarch
);
1183 current_regcache
->passthrough_p
= 1;
1184 registers
= deprecated_grub_regcache_for_registers (current_regcache
);
1185 register_valid
= deprecated_grub_regcache_for_register_valid (current_regcache
);
1189 _initialize_regcache (void)
1191 regcache_descr_handle
= register_gdbarch_data (init_regcache_descr
,
1192 xfree_regcache_descr
);
1193 REGISTER_GDBARCH_SWAP (current_regcache
);
1194 register_gdbarch_swap (®isters
, sizeof (registers
), NULL
);
1195 register_gdbarch_swap (®ister_valid
, sizeof (register_valid
), NULL
);
1196 register_gdbarch_swap (NULL
, 0, build_regcache
);
1198 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1199 "Force gdb to flush its register cache (maintainer command)");
1201 /* Initialize the thread/process associated with the current set of
1202 registers. For now, -1 is special, and means `no current process'. */
1203 registers_ptid
= pid_to_ptid (-1);