1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
28 #include "gdb_assert.h"
33 * Here is the actual register cache.
36 /* NOTE: this is a write-through cache. There is no "dirty" bit for
37 recording if the register values have been changed (eg. by the
38 user). Therefore all registers must be written back to the
39 target when appropriate. */
41 /* REGISTERS contains the cached register values (in target byte order). */
45 /* REGISTER_VALID is 0 if the register needs to be fetched,
46 1 if it has been fetched, and
47 -1 if the register value was not available.
48 "Not available" means don't try to fetch it again. */
50 signed char *register_valid
;
52 /* The thread/process associated with the current set of registers. */
54 static ptid_t registers_ptid
;
62 Returns 0 if the value is not in the cache (needs fetch).
63 >0 if the value is in the cache.
64 <0 if the value is permanently unavailable (don't ask again). */
67 register_cached (int regnum
)
69 return register_valid
[regnum
];
72 /* Record that REGNUM's value is cached if STATE is >0, uncached but
73 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
76 set_register_cached (int regnum
, int state
)
78 register_valid
[regnum
] = state
;
83 invalidate a single register REGNUM in the cache */
85 register_changed (int regnum
)
87 set_register_cached (regnum
, 0);
90 /* If REGNUM >= 0, return a pointer to register REGNUM's cache buffer area,
91 else return a pointer to the start of the cache buffer. */
94 register_buffer (int regnum
)
96 gdb_assert (regnum
>= 0 && regnum
< (NUM_REGS
+ NUM_PSEUDO_REGS
));
97 return ®isters
[REGISTER_BYTE (regnum
)];
100 /* Return whether register REGNUM is a real register. */
103 real_register (int regnum
)
105 return regnum
>= 0 && regnum
< NUM_REGS
;
108 /* Return whether register REGNUM is a pseudo register. */
111 pseudo_register (int regnum
)
113 return regnum
>= NUM_REGS
&& regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
;
116 /* Fetch register REGNUM into the cache. */
119 fetch_register (int regnum
)
121 /* NOTE: cagney/2001-12-04: Legacy targets were using fetch/store
122 pseudo-register as a way of handling registers that needed to be
123 constructed from one or more raw registers. New targets instead
124 use gdbarch register read/write. */
125 if (FETCH_PSEUDO_REGISTER_P ()
126 && pseudo_register (regnum
))
127 FETCH_PSEUDO_REGISTER (regnum
);
129 target_fetch_registers (regnum
);
132 /* Write register REGNUM cached value to the target. */
135 store_register (int regnum
)
137 /* NOTE: cagney/2001-12-04: Legacy targets were using fetch/store
138 pseudo-register as a way of handling registers that needed to be
139 constructed from one or more raw registers. New targets instead
140 use gdbarch register read/write. */
141 if (STORE_PSEUDO_REGISTER_P ()
142 && pseudo_register (regnum
))
143 STORE_PSEUDO_REGISTER (regnum
);
145 target_store_registers (regnum
);
148 /* Low level examining and depositing of registers.
150 The caller is responsible for making sure that the inferior is
151 stopped before calling the fetching routines, or it will get
152 garbage. (a change from GDB version 3, in which the caller got the
153 value from the last stop). */
155 /* REGISTERS_CHANGED ()
157 Indicate that registers may have changed, so invalidate the cache. */
160 registers_changed (void)
164 registers_ptid
= pid_to_ptid (-1);
166 /* Force cleanup of any alloca areas if using C alloca instead of
167 a builtin alloca. This particular call is used to clean up
168 areas allocated by low level target code which may build up
169 during lengthy interactions between gdb and the target before
170 gdb gives control to the user (ie watchpoints). */
173 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
174 set_register_cached (i
, 0);
176 if (registers_changed_hook
)
177 registers_changed_hook ();
180 /* REGISTERS_FETCHED ()
182 Indicate that all registers have been fetched, so mark them all valid. */
184 /* NOTE: cagney/2001-12-04: This function does not set valid on the
185 pseudo-register range since pseudo registers are always supplied
186 using supply_register(). */
187 /* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
188 code was blatting the registers[] array and then calling this.
189 Since targets should only be using supply_register() the need for
190 this function/hack is eliminated. */
193 registers_fetched (void)
197 for (i
= 0; i
< NUM_REGS
; i
++)
198 set_register_cached (i
, 1);
199 /* Do not assume that the pseudo-regs have also been fetched.
200 Fetching all real regs NEVER accounts for pseudo-regs. */
203 /* read_register_bytes and write_register_bytes are generally a *BAD*
204 idea. They are inefficient because they need to check for partial
205 updates, which can only be done by scanning through all of the
206 registers and seeing if the bytes that are being read/written fall
207 inside of an invalid register. [The main reason this is necessary
208 is that register sizes can vary, so a simple index won't suffice.]
209 It is far better to call read_register_gen and write_register_gen
210 if you want to get at the raw register contents, as it only takes a
211 regnum as an argument, and therefore can't do a partial register
214 Prior to the recent fixes to check for partial updates, both read
215 and write_register_bytes always checked to see if any registers
216 were stale, and then called target_fetch_registers (-1) to update
217 the whole set. This caused really slowed things down for remote
220 /* Copy INLEN bytes of consecutive data from registers
221 starting with the INREGBYTE'th byte of register data
222 into memory at MYADDR. */
225 read_register_bytes (int in_start
, char *in_buf
, int in_len
)
227 int in_end
= in_start
+ in_len
;
229 char *reg_buf
= alloca (MAX_REGISTER_RAW_SIZE
);
231 /* See if we are trying to read bytes from out-of-date registers. If so,
232 update just those registers. */
234 for (regnum
= 0; regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
; regnum
++)
243 reg_start
= REGISTER_BYTE (regnum
);
244 reg_len
= REGISTER_RAW_SIZE (regnum
);
245 reg_end
= reg_start
+ reg_len
;
247 if (reg_end
<= in_start
|| in_end
<= reg_start
)
248 /* The range the user wants to read doesn't overlap with regnum. */
251 if (REGISTER_NAME (regnum
) != NULL
&& *REGISTER_NAME (regnum
) != '\0')
252 /* Force the cache to fetch the entire register. */
253 read_register_gen (regnum
, reg_buf
);
255 /* Legacy note: even though this register is ``invalid'' we
256 still need to return something. It would appear that some
257 code relies on apparent gaps in the register array also
259 /* FIXME: cagney/2001-08-18: This is just silly. It defeats
260 the entire register read/write flow of control. Must
261 resist temptation to return 0xdeadbeef. */
262 memcpy (reg_buf
, registers
+ reg_start
, reg_len
);
264 /* Legacy note: This function, for some reason, allows a NULL
265 input buffer. If the buffer is NULL, the registers are still
266 fetched, just the final transfer is skipped. */
270 /* start = max (reg_start, in_start) */
271 if (reg_start
> in_start
)
276 /* end = min (reg_end, in_end) */
277 if (reg_end
< in_end
)
282 /* Transfer just the bytes common to both IN_BUF and REG_BUF */
283 for (byte
= start
; byte
< end
; byte
++)
285 in_buf
[byte
- in_start
] = reg_buf
[byte
- reg_start
];
290 /* Read register REGNUM into memory at MYADDR, which must be large
291 enough for REGISTER_RAW_BYTES (REGNUM). Target byte-order. If the
292 register is known to be the size of a CORE_ADDR or smaller,
293 read_register can be used instead. */
296 legacy_read_register_gen (int regnum
, char *myaddr
)
298 gdb_assert (regnum
>= 0 && regnum
< (NUM_REGS
+ NUM_PSEUDO_REGS
));
299 if (! ptid_equal (registers_ptid
, inferior_ptid
))
301 registers_changed ();
302 registers_ptid
= inferior_ptid
;
305 if (!register_cached (regnum
))
306 fetch_register (regnum
);
308 memcpy (myaddr
, register_buffer (regnum
),
309 REGISTER_RAW_SIZE (regnum
));
313 regcache_read (int rawnum
, char *buf
)
315 gdb_assert (rawnum
>= 0 && rawnum
< (NUM_REGS
+ NUM_PSEUDO_REGS
));
316 /* For moment, just use underlying legacy code. Ulgh!!! */
317 legacy_read_register_gen (rawnum
, buf
);
321 read_register_gen (int regnum
, char *buf
)
323 if (! gdbarch_register_read_p (current_gdbarch
))
325 legacy_read_register_gen (regnum
, buf
);
328 gdbarch_register_read (current_gdbarch
, regnum
, buf
);
332 /* Write register REGNUM at MYADDR to the target. MYADDR points at
333 REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order. */
336 legacy_write_register_gen (int regnum
, char *myaddr
)
339 gdb_assert (regnum
>= 0 && regnum
< (NUM_REGS
+ NUM_PSEUDO_REGS
));
341 /* On the sparc, writing %g0 is a no-op, so we don't even want to
342 change the registers array if something writes to this register. */
343 if (CANNOT_STORE_REGISTER (regnum
))
346 if (! ptid_equal (registers_ptid
, inferior_ptid
))
348 registers_changed ();
349 registers_ptid
= inferior_ptid
;
352 size
= REGISTER_RAW_SIZE (regnum
);
354 if (real_register (regnum
))
356 /* If we have a valid copy of the register, and new value == old
357 value, then don't bother doing the actual store. */
358 if (register_cached (regnum
)
359 && memcmp (register_buffer (regnum
), myaddr
, size
) == 0)
362 target_prepare_to_store ();
365 memcpy (register_buffer (regnum
), myaddr
, size
);
367 set_register_cached (regnum
, 1);
368 store_register (regnum
);
372 regcache_write (int rawnum
, char *buf
)
374 gdb_assert (rawnum
>= 0 && rawnum
< (NUM_REGS
+ NUM_PSEUDO_REGS
));
375 /* For moment, just use underlying legacy code. Ulgh!!! */
376 legacy_write_register_gen (rawnum
, buf
);
380 write_register_gen (int regnum
, char *buf
)
382 if (! gdbarch_register_write_p (current_gdbarch
))
384 legacy_write_register_gen (regnum
, buf
);
387 gdbarch_register_write (current_gdbarch
, regnum
, buf
);
390 /* Copy INLEN bytes of consecutive data from memory at MYADDR
391 into registers starting with the MYREGSTART'th byte of register data. */
394 write_register_bytes (int myregstart
, char *myaddr
, int inlen
)
396 int myregend
= myregstart
+ inlen
;
399 target_prepare_to_store ();
401 /* Scan through the registers updating any that are covered by the
402 range myregstart<=>myregend using write_register_gen, which does
403 nice things like handling threads, and avoiding updates when the
404 new and old contents are the same. */
406 for (regnum
= 0; regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
; regnum
++)
408 int regstart
, regend
;
410 regstart
= REGISTER_BYTE (regnum
);
411 regend
= regstart
+ REGISTER_RAW_SIZE (regnum
);
413 /* Is this register completely outside the range the user is writing? */
414 if (myregend
<= regstart
|| regend
<= myregstart
)
417 /* Is this register completely within the range the user is writing? */
418 else if (myregstart
<= regstart
&& regend
<= myregend
)
419 write_register_gen (regnum
, myaddr
+ (regstart
- myregstart
));
421 /* The register partially overlaps the range being written. */
424 char *regbuf
= (char*) alloca (MAX_REGISTER_RAW_SIZE
);
425 /* What's the overlap between this register's bytes and
426 those the caller wants to write? */
427 int overlapstart
= max (regstart
, myregstart
);
428 int overlapend
= min (regend
, myregend
);
430 /* We may be doing a partial update of an invalid register.
431 Update it from the target before scribbling on it. */
432 read_register_gen (regnum
, regbuf
);
434 memcpy (registers
+ overlapstart
,
435 myaddr
+ (overlapstart
- myregstart
),
436 overlapend
- overlapstart
);
438 store_register (regnum
);
444 /* Return the contents of register REGNUM as an unsigned integer. */
447 read_register (int regnum
)
449 char *buf
= alloca (REGISTER_RAW_SIZE (regnum
));
450 read_register_gen (regnum
, buf
);
451 return (extract_unsigned_integer (buf
, REGISTER_RAW_SIZE (regnum
)));
455 read_register_pid (int regnum
, ptid_t ptid
)
461 if (ptid_equal (ptid
, inferior_ptid
))
462 return read_register (regnum
);
464 save_ptid
= inferior_ptid
;
466 inferior_ptid
= ptid
;
468 retval
= read_register (regnum
);
470 inferior_ptid
= save_ptid
;
475 /* Return the contents of register REGNUM as a signed integer. */
478 read_signed_register (int regnum
)
480 void *buf
= alloca (REGISTER_RAW_SIZE (regnum
));
481 read_register_gen (regnum
, buf
);
482 return (extract_signed_integer (buf
, REGISTER_RAW_SIZE (regnum
)));
486 read_signed_register_pid (int regnum
, ptid_t ptid
)
491 if (ptid_equal (ptid
, inferior_ptid
))
492 return read_signed_register (regnum
);
494 save_ptid
= inferior_ptid
;
496 inferior_ptid
= ptid
;
498 retval
= read_signed_register (regnum
);
500 inferior_ptid
= save_ptid
;
505 /* Store VALUE into the raw contents of register number REGNUM. */
508 write_register (int regnum
, LONGEST val
)
512 size
= REGISTER_RAW_SIZE (regnum
);
514 store_signed_integer (buf
, size
, (LONGEST
) val
);
515 write_register_gen (regnum
, buf
);
519 write_register_pid (int regnum
, CORE_ADDR val
, ptid_t ptid
)
523 if (ptid_equal (ptid
, inferior_ptid
))
525 write_register (regnum
, val
);
529 save_ptid
= inferior_ptid
;
531 inferior_ptid
= ptid
;
533 write_register (regnum
, val
);
535 inferior_ptid
= save_ptid
;
540 Record that register REGNUM contains VAL. This is used when the
541 value is obtained from the inferior or core dump, so there is no
542 need to store the value there.
544 If VAL is a NULL pointer, then it's probably an unsupported register.
545 We just set its value to all zeros. We might want to record this
546 fact, and report it to the users of read_register and friends. */
549 supply_register (int regnum
, char *val
)
552 if (! ptid_equal (registers_ptid
, inferior_ptid
))
554 registers_changed ();
555 registers_ptid
= inferior_ptid
;
559 set_register_cached (regnum
, 1);
561 memcpy (register_buffer (regnum
), val
,
562 REGISTER_RAW_SIZE (regnum
));
564 memset (register_buffer (regnum
), '\000',
565 REGISTER_RAW_SIZE (regnum
));
567 /* On some architectures, e.g. HPPA, there are a few stray bits in
568 some registers, that the rest of the code would like to ignore. */
570 /* NOTE: cagney/2001-03-16: The macro CLEAN_UP_REGISTER_VALUE is
571 going to be deprecated. Instead architectures will leave the raw
572 register value as is and instead clean things up as they pass
573 through the method gdbarch_register_read() clean up the
576 #ifdef DEPRECATED_CLEAN_UP_REGISTER_VALUE
577 DEPRECATED_CLEAN_UP_REGISTER_VALUE (regnum
, register_buffer (regnum
));
582 regcache_collect (int regnum
, void *buf
)
584 memcpy (buf
, register_buffer (regnum
), REGISTER_RAW_SIZE (regnum
));
588 /* read_pc, write_pc, read_sp, write_sp, read_fp, write_fp, etc.
589 Special handling for registers PC, SP, and FP. */
591 /* NOTE: cagney/2001-02-18: The functions generic_target_read_pc(),
592 read_pc_pid(), read_pc(), generic_target_write_pc(),
593 write_pc_pid(), write_pc(), generic_target_read_sp(), read_sp(),
594 generic_target_write_sp(), write_sp(), generic_target_read_fp(),
595 read_fp(), generic_target_write_fp(), write_fp will eventually be
596 moved out of the reg-cache into either frame.[hc] or to the
597 multi-arch framework. The are not part of the raw register cache. */
599 /* This routine is getting awfully cluttered with #if's. It's probably
600 time to turn this into READ_PC and define it in the tm.h file.
603 1999-06-08: The following were re-written so that it assumes the
604 existence of a TARGET_READ_PC et.al. macro. A default generic
605 version of that macro is made available where needed.
607 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
608 by the multi-arch framework, it will eventually be possible to
609 eliminate the intermediate read_pc_pid(). The client would call
610 TARGET_READ_PC directly. (cagney). */
613 generic_target_read_pc (ptid_t ptid
)
618 CORE_ADDR pc_val
= ADDR_BITS_REMOVE ((CORE_ADDR
) read_register_pid (PC_REGNUM
, ptid
));
622 internal_error (__FILE__
, __LINE__
,
623 "generic_target_read_pc");
628 read_pc_pid (ptid_t ptid
)
630 ptid_t saved_inferior_ptid
;
633 /* In case ptid != inferior_ptid. */
634 saved_inferior_ptid
= inferior_ptid
;
635 inferior_ptid
= ptid
;
637 pc_val
= TARGET_READ_PC (ptid
);
639 inferior_ptid
= saved_inferior_ptid
;
646 return read_pc_pid (inferior_ptid
);
650 generic_target_write_pc (CORE_ADDR pc
, ptid_t ptid
)
654 write_register_pid (PC_REGNUM
, pc
, ptid
);
656 write_register_pid (NPC_REGNUM
, pc
+ 4, ptid
);
657 if (NNPC_REGNUM
>= 0)
658 write_register_pid (NNPC_REGNUM
, pc
+ 8, ptid
);
660 internal_error (__FILE__
, __LINE__
,
661 "generic_target_write_pc");
666 write_pc_pid (CORE_ADDR pc
, ptid_t ptid
)
668 ptid_t saved_inferior_ptid
;
670 /* In case ptid != inferior_ptid. */
671 saved_inferior_ptid
= inferior_ptid
;
672 inferior_ptid
= ptid
;
674 TARGET_WRITE_PC (pc
, ptid
);
676 inferior_ptid
= saved_inferior_ptid
;
680 write_pc (CORE_ADDR pc
)
682 write_pc_pid (pc
, inferior_ptid
);
685 /* Cope with strage ways of getting to the stack and frame pointers */
688 generic_target_read_sp (void)
692 return read_register (SP_REGNUM
);
694 internal_error (__FILE__
, __LINE__
,
695 "generic_target_read_sp");
701 return TARGET_READ_SP ();
705 generic_target_write_sp (CORE_ADDR val
)
710 write_register (SP_REGNUM
, val
);
714 internal_error (__FILE__
, __LINE__
,
715 "generic_target_write_sp");
719 write_sp (CORE_ADDR val
)
721 TARGET_WRITE_SP (val
);
725 generic_target_read_fp (void)
729 return read_register (FP_REGNUM
);
731 internal_error (__FILE__
, __LINE__
,
732 "generic_target_read_fp");
738 return TARGET_READ_FP ();
742 generic_target_write_fp (CORE_ADDR val
)
747 write_register (FP_REGNUM
, val
);
751 internal_error (__FILE__
, __LINE__
,
752 "generic_target_write_fp");
756 write_fp (CORE_ADDR val
)
758 TARGET_WRITE_FP (val
);
763 reg_flush_command (char *command
, int from_tty
)
765 /* Force-flush the register cache. */
766 registers_changed ();
768 printf_filtered ("Register cache flushed.\n");
772 build_regcache (void)
775 int sizeof_register_valid
;
776 /* Come up with the real size of the registers buffer. */
777 int sizeof_registers
= REGISTER_BYTES
; /* OK use. */
778 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
781 /* Keep extending the buffer so that there is always enough
782 space for all registers. The comparison is necessary since
783 legacy code is free to put registers in random places in the
784 buffer separated by holes. Once REGISTER_BYTE() is killed
785 this can be greatly simplified. */
786 /* FIXME: cagney/2001-12-04: This code shouldn't need to use
787 REGISTER_BYTE(). Unfortunatly, legacy code likes to lay the
788 buffer out so that certain registers just happen to overlap.
789 Ulgh! New targets use gdbarch's register read/write and
790 entirely avoid this uglyness. */
791 regend
= REGISTER_BYTE (i
) + REGISTER_RAW_SIZE (i
);
792 if (sizeof_registers
< regend
)
793 sizeof_registers
= regend
;
795 registers
= xmalloc (sizeof_registers
);
796 sizeof_register_valid
= ((NUM_REGS
+ NUM_PSEUDO_REGS
)
797 * sizeof (*register_valid
));
798 register_valid
= xmalloc (sizeof_register_valid
);
799 memset (register_valid
, 0, sizeof_register_valid
);
803 _initialize_regcache (void)
807 register_gdbarch_swap (®isters
, sizeof (registers
), NULL
);
808 register_gdbarch_swap (®ister_valid
, sizeof (register_valid
), NULL
);
809 register_gdbarch_swap (NULL
, 0, build_regcache
);
811 add_com ("flushregs", class_maintenance
, reg_flush_command
,
812 "Force gdb to flush its register cache (maintainer command)");
814 /* Initialize the thread/process associated with the current set of
815 registers. For now, -1 is special, and means `no current process'. */
816 registers_ptid
= pid_to_ptid (-1);