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.
49 "Not available" indicates that the target is not not able to supply
50 the register at this state. The register may become available at a
51 later time (after the next resume). This often occures when GDB is
52 manipulating a target that contains only a snapshot of the entire
53 system being debugged - some of the registers in such a system may
54 not have been saved. */
56 signed char *register_valid
;
58 /* The thread/process associated with the current set of registers. */
60 static ptid_t registers_ptid
;
68 Returns 0 if the value is not in the cache (needs fetch).
69 >0 if the value is in the cache.
70 <0 if the value is permanently unavailable (don't ask again). */
73 register_cached (int regnum
)
75 return register_valid
[regnum
];
78 /* Record that REGNUM's value is cached if STATE is >0, uncached but
79 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
82 set_register_cached (int regnum
, int state
)
84 register_valid
[regnum
] = state
;
89 invalidate a single register REGNUM in the cache */
91 register_changed (int regnum
)
93 set_register_cached (regnum
, 0);
96 /* If REGNUM >= 0, return a pointer to register REGNUM's cache buffer area,
97 else return a pointer to the start of the cache buffer. */
100 register_buffer (int regnum
)
102 gdb_assert (regnum
>= 0 && regnum
< (NUM_REGS
+ NUM_PSEUDO_REGS
));
103 return ®isters
[REGISTER_BYTE (regnum
)];
106 /* Return whether register REGNUM is a real register. */
109 real_register (int regnum
)
111 return regnum
>= 0 && regnum
< NUM_REGS
;
114 /* Return whether register REGNUM is a pseudo register. */
117 pseudo_register (int regnum
)
119 return regnum
>= NUM_REGS
&& regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
;
122 /* Fetch register REGNUM into the cache. */
125 fetch_register (int regnum
)
127 /* NOTE: cagney/2001-12-04: Legacy targets were using fetch/store
128 pseudo-register as a way of handling registers that needed to be
129 constructed from one or more raw registers. New targets instead
130 use gdbarch register read/write. */
131 if (FETCH_PSEUDO_REGISTER_P ()
132 && pseudo_register (regnum
))
133 FETCH_PSEUDO_REGISTER (regnum
);
135 target_fetch_registers (regnum
);
138 /* Write register REGNUM cached value to the target. */
141 store_register (int regnum
)
143 /* NOTE: cagney/2001-12-04: Legacy targets were using fetch/store
144 pseudo-register as a way of handling registers that needed to be
145 constructed from one or more raw registers. New targets instead
146 use gdbarch register read/write. */
147 if (STORE_PSEUDO_REGISTER_P ()
148 && pseudo_register (regnum
))
149 STORE_PSEUDO_REGISTER (regnum
);
151 target_store_registers (regnum
);
154 /* Low level examining and depositing of registers.
156 The caller is responsible for making sure that the inferior is
157 stopped before calling the fetching routines, or it will get
158 garbage. (a change from GDB version 3, in which the caller got the
159 value from the last stop). */
161 /* REGISTERS_CHANGED ()
163 Indicate that registers may have changed, so invalidate the cache. */
166 registers_changed (void)
170 registers_ptid
= pid_to_ptid (-1);
172 /* Force cleanup of any alloca areas if using C alloca instead of
173 a builtin alloca. This particular call is used to clean up
174 areas allocated by low level target code which may build up
175 during lengthy interactions between gdb and the target before
176 gdb gives control to the user (ie watchpoints). */
179 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
180 set_register_cached (i
, 0);
182 if (registers_changed_hook
)
183 registers_changed_hook ();
186 /* REGISTERS_FETCHED ()
188 Indicate that all registers have been fetched, so mark them all valid. */
190 /* NOTE: cagney/2001-12-04: This function does not set valid on the
191 pseudo-register range since pseudo registers are always supplied
192 using supply_register(). */
193 /* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
194 code was blatting the registers[] array and then calling this.
195 Since targets should only be using supply_register() the need for
196 this function/hack is eliminated. */
199 registers_fetched (void)
203 for (i
= 0; i
< NUM_REGS
; i
++)
204 set_register_cached (i
, 1);
205 /* Do not assume that the pseudo-regs have also been fetched.
206 Fetching all real regs NEVER accounts for pseudo-regs. */
209 /* read_register_bytes and write_register_bytes are generally a *BAD*
210 idea. They are inefficient because they need to check for partial
211 updates, which can only be done by scanning through all of the
212 registers and seeing if the bytes that are being read/written fall
213 inside of an invalid register. [The main reason this is necessary
214 is that register sizes can vary, so a simple index won't suffice.]
215 It is far better to call read_register_gen and write_register_gen
216 if you want to get at the raw register contents, as it only takes a
217 regnum as an argument, and therefore can't do a partial register
220 Prior to the recent fixes to check for partial updates, both read
221 and write_register_bytes always checked to see if any registers
222 were stale, and then called target_fetch_registers (-1) to update
223 the whole set. This caused really slowed things down for remote
226 /* Copy INLEN bytes of consecutive data from registers
227 starting with the INREGBYTE'th byte of register data
228 into memory at MYADDR. */
231 read_register_bytes (int in_start
, char *in_buf
, int in_len
)
233 int in_end
= in_start
+ in_len
;
235 char *reg_buf
= alloca (MAX_REGISTER_RAW_SIZE
);
237 /* See if we are trying to read bytes from out-of-date registers. If so,
238 update just those registers. */
240 for (regnum
= 0; regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
; regnum
++)
249 reg_start
= REGISTER_BYTE (regnum
);
250 reg_len
= REGISTER_RAW_SIZE (regnum
);
251 reg_end
= reg_start
+ reg_len
;
253 if (reg_end
<= in_start
|| in_end
<= reg_start
)
254 /* The range the user wants to read doesn't overlap with regnum. */
257 if (REGISTER_NAME (regnum
) != NULL
&& *REGISTER_NAME (regnum
) != '\0')
258 /* Force the cache to fetch the entire register. */
259 read_register_gen (regnum
, reg_buf
);
261 /* Legacy note: even though this register is ``invalid'' we
262 still need to return something. It would appear that some
263 code relies on apparent gaps in the register array also
265 /* FIXME: cagney/2001-08-18: This is just silly. It defeats
266 the entire register read/write flow of control. Must
267 resist temptation to return 0xdeadbeef. */
268 memcpy (reg_buf
, registers
+ reg_start
, reg_len
);
270 /* Legacy note: This function, for some reason, allows a NULL
271 input buffer. If the buffer is NULL, the registers are still
272 fetched, just the final transfer is skipped. */
276 /* start = max (reg_start, in_start) */
277 if (reg_start
> in_start
)
282 /* end = min (reg_end, in_end) */
283 if (reg_end
< in_end
)
288 /* Transfer just the bytes common to both IN_BUF and REG_BUF */
289 for (byte
= start
; byte
< end
; byte
++)
291 in_buf
[byte
- in_start
] = reg_buf
[byte
- reg_start
];
296 /* Read register REGNUM into memory at MYADDR, which must be large
297 enough for REGISTER_RAW_BYTES (REGNUM). Target byte-order. If the
298 register is known to be the size of a CORE_ADDR or smaller,
299 read_register can be used instead. */
302 legacy_read_register_gen (int regnum
, char *myaddr
)
304 gdb_assert (regnum
>= 0 && regnum
< (NUM_REGS
+ NUM_PSEUDO_REGS
));
305 if (! ptid_equal (registers_ptid
, inferior_ptid
))
307 registers_changed ();
308 registers_ptid
= inferior_ptid
;
311 if (!register_cached (regnum
))
312 fetch_register (regnum
);
314 memcpy (myaddr
, register_buffer (regnum
),
315 REGISTER_RAW_SIZE (regnum
));
319 regcache_read (int rawnum
, char *buf
)
321 gdb_assert (rawnum
>= 0 && rawnum
< (NUM_REGS
+ NUM_PSEUDO_REGS
));
322 /* For moment, just use underlying legacy code. Ulgh!!! */
323 legacy_read_register_gen (rawnum
, buf
);
327 read_register_gen (int regnum
, char *buf
)
329 if (! gdbarch_register_read_p (current_gdbarch
))
331 legacy_read_register_gen (regnum
, buf
);
334 gdbarch_register_read (current_gdbarch
, regnum
, buf
);
338 /* Write register REGNUM at MYADDR to the target. MYADDR points at
339 REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order. */
342 legacy_write_register_gen (int regnum
, char *myaddr
)
345 gdb_assert (regnum
>= 0 && regnum
< (NUM_REGS
+ NUM_PSEUDO_REGS
));
347 /* On the sparc, writing %g0 is a no-op, so we don't even want to
348 change the registers array if something writes to this register. */
349 if (CANNOT_STORE_REGISTER (regnum
))
352 if (! ptid_equal (registers_ptid
, inferior_ptid
))
354 registers_changed ();
355 registers_ptid
= inferior_ptid
;
358 size
= REGISTER_RAW_SIZE (regnum
);
360 if (real_register (regnum
))
362 /* If we have a valid copy of the register, and new value == old
363 value, then don't bother doing the actual store. */
364 if (register_cached (regnum
)
365 && memcmp (register_buffer (regnum
), myaddr
, size
) == 0)
368 target_prepare_to_store ();
371 memcpy (register_buffer (regnum
), myaddr
, size
);
373 set_register_cached (regnum
, 1);
374 store_register (regnum
);
378 regcache_write (int rawnum
, char *buf
)
380 gdb_assert (rawnum
>= 0 && rawnum
< (NUM_REGS
+ NUM_PSEUDO_REGS
));
381 /* For moment, just use underlying legacy code. Ulgh!!! */
382 legacy_write_register_gen (rawnum
, buf
);
386 write_register_gen (int regnum
, char *buf
)
388 if (! gdbarch_register_write_p (current_gdbarch
))
390 legacy_write_register_gen (regnum
, buf
);
393 gdbarch_register_write (current_gdbarch
, regnum
, buf
);
396 /* Copy INLEN bytes of consecutive data from memory at MYADDR
397 into registers starting with the MYREGSTART'th byte of register data. */
400 write_register_bytes (int myregstart
, char *myaddr
, int inlen
)
402 int myregend
= myregstart
+ inlen
;
405 target_prepare_to_store ();
407 /* Scan through the registers updating any that are covered by the
408 range myregstart<=>myregend using write_register_gen, which does
409 nice things like handling threads, and avoiding updates when the
410 new and old contents are the same. */
412 for (regnum
= 0; regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
; regnum
++)
414 int regstart
, regend
;
416 regstart
= REGISTER_BYTE (regnum
);
417 regend
= regstart
+ REGISTER_RAW_SIZE (regnum
);
419 /* Is this register completely outside the range the user is writing? */
420 if (myregend
<= regstart
|| regend
<= myregstart
)
423 /* Is this register completely within the range the user is writing? */
424 else if (myregstart
<= regstart
&& regend
<= myregend
)
425 write_register_gen (regnum
, myaddr
+ (regstart
- myregstart
));
427 /* The register partially overlaps the range being written. */
430 char *regbuf
= (char*) alloca (MAX_REGISTER_RAW_SIZE
);
431 /* What's the overlap between this register's bytes and
432 those the caller wants to write? */
433 int overlapstart
= max (regstart
, myregstart
);
434 int overlapend
= min (regend
, myregend
);
436 /* We may be doing a partial update of an invalid register.
437 Update it from the target before scribbling on it. */
438 read_register_gen (regnum
, regbuf
);
440 memcpy (registers
+ overlapstart
,
441 myaddr
+ (overlapstart
- myregstart
),
442 overlapend
- overlapstart
);
444 store_register (regnum
);
450 /* Return the contents of register REGNUM as an unsigned integer. */
453 read_register (int regnum
)
455 char *buf
= alloca (REGISTER_RAW_SIZE (regnum
));
456 read_register_gen (regnum
, buf
);
457 return (extract_unsigned_integer (buf
, REGISTER_RAW_SIZE (regnum
)));
461 read_register_pid (int regnum
, ptid_t ptid
)
467 if (ptid_equal (ptid
, inferior_ptid
))
468 return read_register (regnum
);
470 save_ptid
= inferior_ptid
;
472 inferior_ptid
= ptid
;
474 retval
= read_register (regnum
);
476 inferior_ptid
= save_ptid
;
481 /* Return the contents of register REGNUM as a signed integer. */
484 read_signed_register (int regnum
)
486 void *buf
= alloca (REGISTER_RAW_SIZE (regnum
));
487 read_register_gen (regnum
, buf
);
488 return (extract_signed_integer (buf
, REGISTER_RAW_SIZE (regnum
)));
492 read_signed_register_pid (int regnum
, ptid_t ptid
)
497 if (ptid_equal (ptid
, inferior_ptid
))
498 return read_signed_register (regnum
);
500 save_ptid
= inferior_ptid
;
502 inferior_ptid
= ptid
;
504 retval
= read_signed_register (regnum
);
506 inferior_ptid
= save_ptid
;
511 /* Store VALUE into the raw contents of register number REGNUM. */
514 write_register (int regnum
, LONGEST val
)
518 size
= REGISTER_RAW_SIZE (regnum
);
520 store_signed_integer (buf
, size
, (LONGEST
) val
);
521 write_register_gen (regnum
, buf
);
525 write_register_pid (int regnum
, CORE_ADDR val
, ptid_t ptid
)
529 if (ptid_equal (ptid
, inferior_ptid
))
531 write_register (regnum
, val
);
535 save_ptid
= inferior_ptid
;
537 inferior_ptid
= ptid
;
539 write_register (regnum
, val
);
541 inferior_ptid
= save_ptid
;
546 Record that register REGNUM contains VAL. This is used when the
547 value is obtained from the inferior or core dump, so there is no
548 need to store the value there.
550 If VAL is a NULL pointer, then it's probably an unsupported register.
551 We just set its value to all zeros. We might want to record this
552 fact, and report it to the users of read_register and friends. */
555 supply_register (int regnum
, char *val
)
558 if (! ptid_equal (registers_ptid
, inferior_ptid
))
560 registers_changed ();
561 registers_ptid
= inferior_ptid
;
565 set_register_cached (regnum
, 1);
567 memcpy (register_buffer (regnum
), val
,
568 REGISTER_RAW_SIZE (regnum
));
570 memset (register_buffer (regnum
), '\000',
571 REGISTER_RAW_SIZE (regnum
));
573 /* On some architectures, e.g. HPPA, there are a few stray bits in
574 some registers, that the rest of the code would like to ignore. */
576 /* NOTE: cagney/2001-03-16: The macro CLEAN_UP_REGISTER_VALUE is
577 going to be deprecated. Instead architectures will leave the raw
578 register value as is and instead clean things up as they pass
579 through the method gdbarch_register_read() clean up the
582 #ifdef DEPRECATED_CLEAN_UP_REGISTER_VALUE
583 DEPRECATED_CLEAN_UP_REGISTER_VALUE (regnum
, register_buffer (regnum
));
588 regcache_collect (int regnum
, void *buf
)
590 memcpy (buf
, register_buffer (regnum
), REGISTER_RAW_SIZE (regnum
));
594 /* read_pc, write_pc, read_sp, write_sp, read_fp, etc. Special
595 handling for registers PC, SP, and FP. */
597 /* NOTE: cagney/2001-02-18: The functions generic_target_read_pc(),
598 read_pc_pid(), read_pc(), generic_target_write_pc(),
599 write_pc_pid(), write_pc(), generic_target_read_sp(), read_sp(),
600 generic_target_write_sp(), write_sp(), generic_target_read_fp() and
601 read_fp(), will eventually be moved out of the reg-cache into
602 either frame.[hc] or to the multi-arch framework. The are not part
603 of the raw register cache. */
605 /* This routine is getting awfully cluttered with #if's. It's probably
606 time to turn this into READ_PC and define it in the tm.h file.
609 1999-06-08: The following were re-written so that it assumes the
610 existence of a TARGET_READ_PC et.al. macro. A default generic
611 version of that macro is made available where needed.
613 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
614 by the multi-arch framework, it will eventually be possible to
615 eliminate the intermediate read_pc_pid(). The client would call
616 TARGET_READ_PC directly. (cagney). */
619 generic_target_read_pc (ptid_t ptid
)
624 CORE_ADDR pc_val
= ADDR_BITS_REMOVE ((CORE_ADDR
) read_register_pid (PC_REGNUM
, ptid
));
628 internal_error (__FILE__
, __LINE__
,
629 "generic_target_read_pc");
634 read_pc_pid (ptid_t ptid
)
636 ptid_t saved_inferior_ptid
;
639 /* In case ptid != inferior_ptid. */
640 saved_inferior_ptid
= inferior_ptid
;
641 inferior_ptid
= ptid
;
643 pc_val
= TARGET_READ_PC (ptid
);
645 inferior_ptid
= saved_inferior_ptid
;
652 return read_pc_pid (inferior_ptid
);
656 generic_target_write_pc (CORE_ADDR pc
, ptid_t ptid
)
660 write_register_pid (PC_REGNUM
, pc
, ptid
);
662 write_register_pid (NPC_REGNUM
, pc
+ 4, ptid
);
664 internal_error (__FILE__
, __LINE__
,
665 "generic_target_write_pc");
670 write_pc_pid (CORE_ADDR pc
, ptid_t ptid
)
672 ptid_t saved_inferior_ptid
;
674 /* In case ptid != inferior_ptid. */
675 saved_inferior_ptid
= inferior_ptid
;
676 inferior_ptid
= ptid
;
678 TARGET_WRITE_PC (pc
, ptid
);
680 inferior_ptid
= saved_inferior_ptid
;
684 write_pc (CORE_ADDR pc
)
686 write_pc_pid (pc
, inferior_ptid
);
689 /* Cope with strage ways of getting to the stack and frame pointers */
692 generic_target_read_sp (void)
696 return read_register (SP_REGNUM
);
698 internal_error (__FILE__
, __LINE__
,
699 "generic_target_read_sp");
705 return TARGET_READ_SP ();
709 generic_target_write_sp (CORE_ADDR val
)
714 write_register (SP_REGNUM
, val
);
718 internal_error (__FILE__
, __LINE__
,
719 "generic_target_write_sp");
723 write_sp (CORE_ADDR val
)
725 TARGET_WRITE_SP (val
);
729 generic_target_read_fp (void)
733 return read_register (FP_REGNUM
);
735 internal_error (__FILE__
, __LINE__
,
736 "generic_target_read_fp");
742 return TARGET_READ_FP ();
747 reg_flush_command (char *command
, int from_tty
)
749 /* Force-flush the register cache. */
750 registers_changed ();
752 printf_filtered ("Register cache flushed.\n");
756 build_regcache (void)
759 int sizeof_register_valid
;
760 /* Come up with the real size of the registers buffer. */
761 int sizeof_registers
= REGISTER_BYTES
; /* OK use. */
762 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
765 /* Keep extending the buffer so that there is always enough
766 space for all registers. The comparison is necessary since
767 legacy code is free to put registers in random places in the
768 buffer separated by holes. Once REGISTER_BYTE() is killed
769 this can be greatly simplified. */
770 /* FIXME: cagney/2001-12-04: This code shouldn't need to use
771 REGISTER_BYTE(). Unfortunatly, legacy code likes to lay the
772 buffer out so that certain registers just happen to overlap.
773 Ulgh! New targets use gdbarch's register read/write and
774 entirely avoid this uglyness. */
775 regend
= REGISTER_BYTE (i
) + REGISTER_RAW_SIZE (i
);
776 if (sizeof_registers
< regend
)
777 sizeof_registers
= regend
;
779 registers
= xmalloc (sizeof_registers
);
780 sizeof_register_valid
= ((NUM_REGS
+ NUM_PSEUDO_REGS
)
781 * sizeof (*register_valid
));
782 register_valid
= xmalloc (sizeof_register_valid
);
783 memset (register_valid
, 0, sizeof_register_valid
);
787 _initialize_regcache (void)
789 register_gdbarch_swap (®isters
, sizeof (registers
), NULL
);
790 register_gdbarch_swap (®ister_valid
, sizeof (register_valid
), NULL
);
791 register_gdbarch_swap (NULL
, 0, build_regcache
);
793 add_com ("flushregs", class_maintenance
, reg_flush_command
,
794 "Force gdb to flush its register cache (maintainer command)");
796 /* Initialize the thread/process associated with the current set of
797 registers. For now, -1 is special, and means `no current process'. */
798 registers_ptid
= pid_to_ptid (-1);