1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2 Copyright 1986, 87, 89, 91, 94, 95, 96, 1998
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. */
30 #include "gdb_string.h"
31 #include "floatformat.h"
32 #include "symfile.h" /* for overlay functions */
34 /* This is used to indicate that we don't know the format of the floating point
35 number. Typically, this is useful for native ports, where the actual format
36 is irrelevant, since no conversions will be taking place. */
38 const struct floatformat floatformat_unknown
;
40 /* Registers we shouldn't try to store. */
41 #if !defined (CANNOT_STORE_REGISTER)
42 #define CANNOT_STORE_REGISTER(regno) 0
45 static void write_register_gen
PARAMS ((int, char *));
47 static int read_relative_register_raw_bytes_for_frame
PARAMS ((int regnum
, char *myaddr
, struct frame_info
* frame
));
49 /* Basic byte-swapping routines. GDB has needed these for a long time...
50 All extract a target-format integer at ADDR which is LEN bytes long. */
52 #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
53 /* 8 bit characters are a pretty safe assumption these days, so we
54 assume it throughout all these swapping routines. If we had to deal with
55 9 bit characters, we would need to make len be in bits and would have
56 to re-write these routines... */
61 extract_signed_integer (addr
, len
)
67 unsigned char *startaddr
= (unsigned char *) addr
;
68 unsigned char *endaddr
= startaddr
+ len
;
70 if (len
> (int) sizeof (LONGEST
))
72 That operation is not available on integers of more than %d bytes.",
75 /* Start at the most significant end of the integer, and work towards
76 the least significant. */
77 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
80 /* Do the sign extension once at the start. */
81 retval
= ((LONGEST
) * p
^ 0x80) - 0x80;
82 for (++p
; p
< endaddr
; ++p
)
83 retval
= (retval
<< 8) | *p
;
88 /* Do the sign extension once at the start. */
89 retval
= ((LONGEST
) * p
^ 0x80) - 0x80;
90 for (--p
; p
>= startaddr
; --p
)
91 retval
= (retval
<< 8) | *p
;
97 extract_unsigned_integer (addr
, len
)
103 unsigned char *startaddr
= (unsigned char *) addr
;
104 unsigned char *endaddr
= startaddr
+ len
;
106 if (len
> (int) sizeof (ULONGEST
))
108 That operation is not available on integers of more than %d bytes.",
111 /* Start at the most significant end of the integer, and work towards
112 the least significant. */
114 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
116 for (p
= startaddr
; p
< endaddr
; ++p
)
117 retval
= (retval
<< 8) | *p
;
121 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
122 retval
= (retval
<< 8) | *p
;
127 /* Sometimes a long long unsigned integer can be extracted as a
128 LONGEST value. This is done so that we can print these values
129 better. If this integer can be converted to a LONGEST, this
130 function returns 1 and sets *PVAL. Otherwise it returns 0. */
133 extract_long_unsigned_integer (addr
, orig_len
, pval
)
138 char *p
, *first_addr
;
142 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
144 for (p
= (char *) addr
;
145 len
> (int) sizeof (LONGEST
) && p
< (char *) addr
+ orig_len
;
157 first_addr
= (char *) addr
;
158 for (p
= (char *) addr
+ orig_len
- 1;
159 len
> (int) sizeof (LONGEST
) && p
>= (char *) addr
;
169 if (len
<= (int) sizeof (LONGEST
))
171 *pval
= (LONGEST
) extract_unsigned_integer (first_addr
,
180 extract_address (addr
, len
)
184 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
185 whether we want this to be true eventually. */
186 return (CORE_ADDR
) extract_unsigned_integer (addr
, len
);
190 store_signed_integer (addr
, len
, val
)
196 unsigned char *startaddr
= (unsigned char *) addr
;
197 unsigned char *endaddr
= startaddr
+ len
;
199 /* Start at the least significant end of the integer, and work towards
200 the most significant. */
201 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
203 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
211 for (p
= startaddr
; p
< endaddr
; ++p
)
220 store_unsigned_integer (addr
, len
, val
)
226 unsigned char *startaddr
= (unsigned char *) addr
;
227 unsigned char *endaddr
= startaddr
+ len
;
229 /* Start at the least significant end of the integer, and work towards
230 the most significant. */
231 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
233 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
241 for (p
= startaddr
; p
< endaddr
; ++p
)
249 /* Store the literal address "val" into
250 gdb-local memory pointed to by "addr"
253 store_address (addr
, len
, val
)
258 store_unsigned_integer (addr
, len
, val
);
261 /* Swap LEN bytes at BUFFER between target and host byte-order. */
262 #define SWAP_FLOATING(buffer,len) \
265 if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER) \
268 char *p = (char *)(buffer); \
269 char *q = ((char *)(buffer)) + len - 1; \
270 for (; p < q; p++, q--) \
280 /* Extract a floating-point number from a target-order byte-stream at ADDR.
281 Returns the value as type DOUBLEST.
283 If the host and target formats agree, we just copy the raw data into the
284 appropriate type of variable and return, letting the host increase precision
285 as necessary. Otherwise, we call the conversion routine and let it do the
289 extract_floating (addr
, len
)
295 if (len
== sizeof (float))
297 if (HOST_FLOAT_FORMAT
== TARGET_FLOAT_FORMAT
)
301 memcpy (&retval
, addr
, sizeof (retval
));
305 floatformat_to_doublest (TARGET_FLOAT_FORMAT
, addr
, &dretval
);
307 else if (len
== sizeof (double))
309 if (HOST_DOUBLE_FORMAT
== TARGET_DOUBLE_FORMAT
)
313 memcpy (&retval
, addr
, sizeof (retval
));
317 floatformat_to_doublest (TARGET_DOUBLE_FORMAT
, addr
, &dretval
);
319 else if (len
== sizeof (DOUBLEST
))
321 if (HOST_LONG_DOUBLE_FORMAT
== TARGET_LONG_DOUBLE_FORMAT
)
325 memcpy (&retval
, addr
, sizeof (retval
));
329 floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT
, addr
, &dretval
);
333 error ("Can't deal with a floating point number of %d bytes.", len
);
340 store_floating (addr
, len
, val
)
345 if (len
== sizeof (float))
347 if (HOST_FLOAT_FORMAT
== TARGET_FLOAT_FORMAT
)
349 float floatval
= val
;
351 memcpy (addr
, &floatval
, sizeof (floatval
));
354 floatformat_from_doublest (TARGET_FLOAT_FORMAT
, &val
, addr
);
356 else if (len
== sizeof (double))
358 if (HOST_DOUBLE_FORMAT
== TARGET_DOUBLE_FORMAT
)
360 double doubleval
= val
;
362 memcpy (addr
, &doubleval
, sizeof (doubleval
));
365 floatformat_from_doublest (TARGET_DOUBLE_FORMAT
, &val
, addr
);
367 else if (len
== sizeof (DOUBLEST
))
369 if (HOST_LONG_DOUBLE_FORMAT
== TARGET_LONG_DOUBLE_FORMAT
)
370 memcpy (addr
, &val
, sizeof (val
));
372 floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT
, &val
, addr
);
376 error ("Can't deal with a floating point number of %d bytes.", len
);
381 /* Return the address in which frame FRAME's value of register REGNUM
382 has been saved in memory. Or return zero if it has not been saved.
383 If REGNUM specifies the SP, the value we return is actually
384 the SP value, not an address where it was saved. */
387 find_saved_register (frame
, regnum
)
388 struct frame_info
*frame
;
391 register struct frame_info
*frame1
= NULL
;
392 register CORE_ADDR addr
= 0;
394 if (frame
== NULL
) /* No regs saved if want current frame */
397 #ifdef HAVE_REGISTER_WINDOWS
398 /* We assume that a register in a register window will only be saved
399 in one place (since the name changes and/or disappears as you go
400 towards inner frames), so we only call get_frame_saved_regs on
401 the current frame. This is directly in contradiction to the
402 usage below, which assumes that registers used in a frame must be
403 saved in a lower (more interior) frame. This change is a result
404 of working on a register window machine; get_frame_saved_regs
405 always returns the registers saved within a frame, within the
406 context (register namespace) of that frame. */
408 /* However, note that we don't want this to return anything if
409 nothing is saved (if there's a frame inside of this one). Also,
410 callers to this routine asking for the stack pointer want the
411 stack pointer saved for *this* frame; this is returned from the
414 if (REGISTER_IN_WINDOW_P (regnum
))
416 frame1
= get_next_frame (frame
);
418 return 0; /* Registers of this frame are active. */
420 /* Get the SP from the next frame in; it will be this
422 if (regnum
!= SP_REGNUM
)
425 FRAME_INIT_SAVED_REGS (frame1
);
426 return frame1
->saved_regs
[regnum
]; /* ... which might be zero */
428 #endif /* HAVE_REGISTER_WINDOWS */
430 /* Note that this next routine assumes that registers used in
431 frame x will be saved only in the frame that x calls and
432 frames interior to it. This is not true on the sparc, but the
433 above macro takes care of it, so we should be all right. */
437 frame1
= get_prev_frame (frame1
);
438 if (frame1
== 0 || frame1
== frame
)
440 FRAME_INIT_SAVED_REGS (frame1
);
441 if (frame1
->saved_regs
[regnum
])
442 addr
= frame1
->saved_regs
[regnum
];
448 /* Find register number REGNUM relative to FRAME and put its (raw,
449 target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the
450 variable was optimized out (and thus can't be fetched). Set *LVAL
451 to lval_memory, lval_register, or not_lval, depending on whether
452 the value was fetched from memory, from a register, or in a strange
453 and non-modifiable way (e.g. a frame pointer which was calculated
454 rather than fetched). Set *ADDRP to the address, either in memory
455 on as a REGISTER_BYTE offset into the registers array.
457 Note that this implementation never sets *LVAL to not_lval. But
458 it can be replaced by defining GET_SAVED_REGISTER and supplying
461 The argument RAW_BUFFER must point to aligned memory. */
464 default_get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
468 struct frame_info
*frame
;
470 enum lval_type
*lval
;
474 if (!target_has_registers
)
475 error ("No registers.");
477 /* Normal systems don't optimize out things with register numbers. */
478 if (optimized
!= NULL
)
480 addr
= find_saved_register (frame
, regnum
);
485 if (regnum
== SP_REGNUM
)
487 if (raw_buffer
!= NULL
)
489 /* Put it back in target format. */
490 store_address (raw_buffer
, REGISTER_RAW_SIZE (regnum
), (LONGEST
) addr
);
496 if (raw_buffer
!= NULL
)
497 read_memory (addr
, raw_buffer
, REGISTER_RAW_SIZE (regnum
));
502 *lval
= lval_register
;
503 addr
= REGISTER_BYTE (regnum
);
504 if (raw_buffer
!= NULL
)
505 read_register_gen (regnum
, raw_buffer
);
511 #if !defined (GET_SAVED_REGISTER)
512 #define GET_SAVED_REGISTER(raw_buffer, optimized, addrp, frame, regnum, lval) \
513 default_get_saved_register(raw_buffer, optimized, addrp, frame, regnum, lval)
516 get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
520 struct frame_info
*frame
;
522 enum lval_type
*lval
;
524 GET_SAVED_REGISTER (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
);
527 /* Copy the bytes of register REGNUM, relative to the input stack frame,
528 into our memory at MYADDR, in target byte order.
529 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
531 Returns 1 if could not be read, 0 if could. */
534 read_relative_register_raw_bytes_for_frame (regnum
, myaddr
, frame
)
537 struct frame_info
*frame
;
540 if (regnum
== FP_REGNUM
&& frame
)
542 /* Put it back in target format. */
543 store_address (myaddr
, REGISTER_RAW_SIZE (FP_REGNUM
),
544 (LONGEST
) FRAME_FP (frame
));
549 get_saved_register (myaddr
, &optim
, (CORE_ADDR
*) NULL
, frame
,
550 regnum
, (enum lval_type
*) NULL
);
552 if (register_valid
[regnum
] < 0)
553 return 1; /* register value not available */
558 /* Copy the bytes of register REGNUM, relative to the current stack frame,
559 into our memory at MYADDR, in target byte order.
560 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
562 Returns 1 if could not be read, 0 if could. */
565 read_relative_register_raw_bytes (regnum
, myaddr
)
569 return read_relative_register_raw_bytes_for_frame (regnum
, myaddr
,
573 /* Return a `value' with the contents of register REGNUM
574 in its virtual format, with the type specified by
575 REGISTER_VIRTUAL_TYPE.
577 NOTE: returns NULL if register value is not available.
578 Caller will check return value or die! */
581 value_of_register (regnum
)
586 register value_ptr reg_val
;
587 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
590 get_saved_register (raw_buffer
, &optim
, &addr
,
591 selected_frame
, regnum
, &lval
);
593 if (register_valid
[regnum
] < 0)
594 return NULL
; /* register value not available */
596 reg_val
= allocate_value (REGISTER_VIRTUAL_TYPE (regnum
));
598 /* Convert raw data to virtual format if necessary. */
600 if (REGISTER_CONVERTIBLE (regnum
))
602 REGISTER_CONVERT_TO_VIRTUAL (regnum
, REGISTER_VIRTUAL_TYPE (regnum
),
603 raw_buffer
, VALUE_CONTENTS_RAW (reg_val
));
605 else if (REGISTER_RAW_SIZE (regnum
) == REGISTER_VIRTUAL_SIZE (regnum
))
606 memcpy (VALUE_CONTENTS_RAW (reg_val
), raw_buffer
,
607 REGISTER_RAW_SIZE (regnum
));
609 fatal ("Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
610 REGISTER_NAME (regnum
), regnum
,
611 REGISTER_RAW_SIZE (regnum
), REGISTER_VIRTUAL_SIZE (regnum
));
612 VALUE_LVAL (reg_val
) = lval
;
613 VALUE_ADDRESS (reg_val
) = addr
;
614 VALUE_REGNO (reg_val
) = regnum
;
615 VALUE_OPTIMIZED_OUT (reg_val
) = optim
;
619 /* Low level examining and depositing of registers.
621 The caller is responsible for making
622 sure that the inferior is stopped before calling the fetching routines,
623 or it will get garbage. (a change from GDB version 3, in which
624 the caller got the value from the last stop). */
626 /* Contents and state of the registers (in target byte order). */
630 /* VALID_REGISTER is non-zero if it has been fetched, -1 if the
631 register value was not available. */
633 signed char *register_valid
;
635 /* The thread/process associated with the current set of registers. For now,
636 -1 is special, and means `no current process'. */
637 int registers_pid
= -1;
639 /* Indicate that registers may have changed, so invalidate the cache. */
645 int numregs
= ARCH_NUM_REGS
;
649 /* Force cleanup of any alloca areas if using C alloca instead of
650 a builtin alloca. This particular call is used to clean up
651 areas allocated by low level target code which may build up
652 during lengthy interactions between gdb and the target before
653 gdb gives control to the user (ie watchpoints). */
656 for (i
= 0; i
< numregs
; i
++)
657 register_valid
[i
] = 0;
659 if (registers_changed_hook
)
660 registers_changed_hook ();
663 /* Indicate that all registers have been fetched, so mark them all valid. */
668 int numregs
= ARCH_NUM_REGS
;
669 for (i
= 0; i
< numregs
; i
++)
670 register_valid
[i
] = 1;
673 /* read_register_bytes and write_register_bytes are generally a *BAD* idea.
674 They are inefficient because they need to check for partial updates, which
675 can only be done by scanning through all of the registers and seeing if the
676 bytes that are being read/written fall inside of an invalid register. [The
677 main reason this is necessary is that register sizes can vary, so a simple
678 index won't suffice.] It is far better to call read_register_gen if you
679 want to get at the raw register contents, as it only takes a regno as an
680 argument, and therefore can't do a partial register update. It would also
681 be good to have a write_register_gen for similar reasons.
683 Prior to the recent fixes to check for partial updates, both read and
684 write_register_bytes always checked to see if any registers were stale, and
685 then called target_fetch_registers (-1) to update the whole set. This
686 caused really slowed things down for remote targets. */
688 /* Copy INLEN bytes of consecutive data from registers
689 starting with the INREGBYTE'th byte of register data
690 into memory at MYADDR. */
693 read_register_bytes (inregbyte
, myaddr
, inlen
)
698 int inregend
= inregbyte
+ inlen
;
701 if (registers_pid
!= inferior_pid
)
703 registers_changed ();
704 registers_pid
= inferior_pid
;
707 /* See if we are trying to read bytes from out-of-date registers. If so,
708 update just those registers. */
710 for (regno
= 0; regno
< NUM_REGS
; regno
++)
712 int regstart
, regend
;
715 if (register_valid
[regno
])
718 if (REGISTER_NAME (regno
) == NULL
|| *REGISTER_NAME (regno
) == '\0')
721 regstart
= REGISTER_BYTE (regno
);
722 regend
= regstart
+ REGISTER_RAW_SIZE (regno
);
724 startin
= regstart
>= inregbyte
&& regstart
< inregend
;
725 endin
= regend
> inregbyte
&& regend
<= inregend
;
727 if (!startin
&& !endin
)
730 /* We've found an invalid register where at least one byte will be read.
731 Update it from the target. */
733 target_fetch_registers (regno
);
735 if (!register_valid
[regno
])
736 error ("read_register_bytes: Couldn't update register %d.", regno
);
740 memcpy (myaddr
, ®isters
[inregbyte
], inlen
);
743 /* Read register REGNO into memory at MYADDR, which must be large enough
744 for REGISTER_RAW_BYTES (REGNO). Target byte-order.
745 If the register is known to be the size of a CORE_ADDR or smaller,
746 read_register can be used instead. */
748 read_register_gen (regno
, myaddr
)
752 if (registers_pid
!= inferior_pid
)
754 registers_changed ();
755 registers_pid
= inferior_pid
;
758 if (!register_valid
[regno
])
759 target_fetch_registers (regno
);
760 memcpy (myaddr
, ®isters
[REGISTER_BYTE (regno
)],
761 REGISTER_RAW_SIZE (regno
));
764 /* Write register REGNO at MYADDR to the target. MYADDR points at
765 REGISTER_RAW_BYTES(REGNO), which must be in target byte-order. */
768 write_register_gen (regno
, myaddr
)
774 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
775 the registers array if something writes to this register. */
776 if (CANNOT_STORE_REGISTER (regno
))
779 if (registers_pid
!= inferior_pid
)
781 registers_changed ();
782 registers_pid
= inferior_pid
;
785 size
= REGISTER_RAW_SIZE (regno
);
787 /* If we have a valid copy of the register, and new value == old value,
788 then don't bother doing the actual store. */
790 if (register_valid
[regno
]
791 && memcmp (®isters
[REGISTER_BYTE (regno
)], myaddr
, size
) == 0)
794 target_prepare_to_store ();
796 memcpy (®isters
[REGISTER_BYTE (regno
)], myaddr
, size
);
798 register_valid
[regno
] = 1;
800 target_store_registers (regno
);
803 /* Copy INLEN bytes of consecutive data from memory at MYADDR
804 into registers starting with the MYREGSTART'th byte of register data. */
807 write_register_bytes (myregstart
, myaddr
, inlen
)
812 int myregend
= myregstart
+ inlen
;
815 target_prepare_to_store ();
817 /* Scan through the registers updating any that are covered by the range
818 myregstart<=>myregend using write_register_gen, which does nice things
819 like handling threads, and avoiding updates when the new and old contents
822 for (regno
= 0; regno
< NUM_REGS
; regno
++)
824 int regstart
, regend
;
826 char regbuf
[MAX_REGISTER_RAW_SIZE
];
828 regstart
= REGISTER_BYTE (regno
);
829 regend
= regstart
+ REGISTER_RAW_SIZE (regno
);
831 startin
= regstart
>= myregstart
&& regstart
< myregend
;
832 endin
= regend
> myregstart
&& regend
<= myregend
;
834 if (!startin
&& !endin
)
835 continue; /* Register is completely out of range */
837 if (startin
&& endin
) /* register is completely in range */
839 write_register_gen (regno
, myaddr
+ (regstart
- myregstart
));
843 /* We may be doing a partial update of an invalid register. Update it
844 from the target before scribbling on it. */
845 read_register_gen (regno
, regbuf
);
848 memcpy (registers
+ regstart
,
849 myaddr
+ regstart
- myregstart
,
850 myregend
- regstart
);
852 memcpy (registers
+ myregstart
,
854 regend
- myregstart
);
855 target_store_registers (regno
);
859 /* Return the raw contents of register REGNO, regarding it as an integer. */
860 /* This probably should be returning LONGEST rather than CORE_ADDR. */
863 read_register (regno
)
866 if (registers_pid
!= inferior_pid
)
868 registers_changed ();
869 registers_pid
= inferior_pid
;
872 if (!register_valid
[regno
])
873 target_fetch_registers (regno
);
875 return (CORE_ADDR
) extract_address (®isters
[REGISTER_BYTE (regno
)],
876 REGISTER_RAW_SIZE (regno
));
880 read_register_pid (regno
, pid
)
886 if (pid
== inferior_pid
)
887 return read_register (regno
);
889 save_pid
= inferior_pid
;
893 retval
= read_register (regno
);
895 inferior_pid
= save_pid
;
900 /* Store VALUE, into the raw contents of register number REGNO.
901 This should probably write a LONGEST rather than a CORE_ADDR */
904 write_register (regno
, val
)
911 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
912 the registers array if something writes to this register. */
913 if (CANNOT_STORE_REGISTER (regno
))
916 if (registers_pid
!= inferior_pid
)
918 registers_changed ();
919 registers_pid
= inferior_pid
;
922 size
= REGISTER_RAW_SIZE (regno
);
924 store_signed_integer (buf
, size
, (LONGEST
) val
);
926 /* If we have a valid copy of the register, and new value == old value,
927 then don't bother doing the actual store. */
929 if (register_valid
[regno
]
930 && memcmp (®isters
[REGISTER_BYTE (regno
)], buf
, size
) == 0)
933 target_prepare_to_store ();
935 memcpy (®isters
[REGISTER_BYTE (regno
)], buf
, size
);
937 register_valid
[regno
] = 1;
939 target_store_registers (regno
);
943 write_register_pid (regno
, val
, pid
)
950 if (pid
== inferior_pid
)
952 write_register (regno
, val
);
956 save_pid
= inferior_pid
;
960 write_register (regno
, val
);
962 inferior_pid
= save_pid
;
965 /* Record that register REGNO contains VAL.
966 This is used when the value is obtained from the inferior or core dump,
967 so there is no need to store the value there.
969 If VAL is a NULL pointer, then it's probably an unsupported register. We
970 just set it's value to all zeros. We might want to record this fact, and
971 report it to the users of read_register and friends.
975 supply_register (regno
, val
)
980 if (registers_pid
!= inferior_pid
)
982 registers_changed ();
983 registers_pid
= inferior_pid
;
987 register_valid
[regno
] = 1;
989 memcpy (®isters
[REGISTER_BYTE (regno
)], val
, REGISTER_RAW_SIZE (regno
));
991 memset (®isters
[REGISTER_BYTE (regno
)], '\000', REGISTER_RAW_SIZE (regno
));
993 /* On some architectures, e.g. HPPA, there are a few stray bits in some
994 registers, that the rest of the code would like to ignore. */
995 #ifdef CLEAN_UP_REGISTER_VALUE
996 CLEAN_UP_REGISTER_VALUE (regno
, ®isters
[REGISTER_BYTE (regno
)]);
1001 /* This routine is getting awfully cluttered with #if's. It's probably
1002 time to turn this into READ_PC and define it in the tm.h file.
1005 1999-06-08: The following were re-written so that it assumes the
1006 existance of a TARGET_READ_PC et.al. macro. A default generic
1007 version of that macro is made available where needed.
1009 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
1010 by the multi-arch framework, it will eventually be possible to
1011 eliminate the intermediate read_pc_pid(). The client would call
1012 TARGET_READ_PC directly. (cagney). */
1014 #ifndef TARGET_READ_PC
1015 #define TARGET_READ_PC generic_target_read_pc
1019 generic_target_read_pc (pid
)
1024 CORE_ADDR pc_val
= ADDR_BITS_REMOVE ((CORE_ADDR
) read_register_pid (PC_REGNUM
, pid
));
1028 fatal ("generic_target_read_pc");
1036 int saved_inferior_pid
;
1039 /* In case pid != inferior_pid. */
1040 saved_inferior_pid
= inferior_pid
;
1043 pc_val
= TARGET_READ_PC (pid
);
1045 inferior_pid
= saved_inferior_pid
;
1052 return read_pc_pid (inferior_pid
);
1055 #ifndef TARGET_WRITE_PC
1056 #define TARGET_WRITE_PC generic_target_write_pc
1060 generic_target_write_pc (pc
, pid
)
1066 write_register_pid (PC_REGNUM
, pc
, pid
);
1068 if (NPC_REGNUM
>= 0)
1069 write_register_pid (NPC_REGNUM
, pc
+ 4, pid
);
1071 if (NNPC_REGNUM
>= 0)
1072 write_register_pid (NNPC_REGNUM
, pc
+ 8, pid
);
1076 fatal ("generic_target_write_pc");
1081 write_pc_pid (pc
, pid
)
1085 int saved_inferior_pid
;
1087 /* In case pid != inferior_pid. */
1088 saved_inferior_pid
= inferior_pid
;
1091 TARGET_WRITE_PC (pc
, pid
);
1093 inferior_pid
= saved_inferior_pid
;
1100 write_pc_pid (pc
, inferior_pid
);
1103 /* Cope with strage ways of getting to the stack and frame pointers */
1105 #ifndef TARGET_READ_SP
1106 #define TARGET_READ_SP generic_target_read_sp
1110 generic_target_read_sp ()
1114 return read_register (SP_REGNUM
);
1116 fatal ("generic_target_read_sp");
1122 return TARGET_READ_SP ();
1125 #ifndef TARGET_WRITE_SP
1126 #define TARGET_WRITE_SP generic_target_write_sp
1130 generic_target_write_sp (val
)
1136 write_register (SP_REGNUM
, val
);
1140 fatal ("generic_target_write_sp");
1147 TARGET_WRITE_SP (val
);
1150 #ifndef TARGET_READ_FP
1151 #define TARGET_READ_FP generic_target_read_fp
1155 generic_target_read_fp ()
1159 return read_register (FP_REGNUM
);
1161 fatal ("generic_target_read_fp");
1167 return TARGET_READ_FP ();
1170 #ifndef TARGET_WRITE_FP
1171 #define TARGET_WRITE_FP generic_target_write_fp
1175 generic_target_write_fp (val
)
1181 write_register (FP_REGNUM
, val
);
1185 fatal ("generic_target_write_fp");
1192 TARGET_WRITE_FP (val
);
1195 /* Will calling read_var_value or locate_var_value on SYM end
1196 up caring what frame it is being evaluated relative to? SYM must
1199 symbol_read_needs_frame (sym
)
1202 switch (SYMBOL_CLASS (sym
))
1204 /* All cases listed explicitly so that gcc -Wall will detect it if
1205 we failed to consider one. */
1210 case LOC_REGPARM_ADDR
:
1214 case LOC_BASEREG_ARG
:
1215 case LOC_THREAD_LOCAL_STATIC
:
1225 /* Getting the address of a label can be done independently of the block,
1226 even if some *uses* of that address wouldn't work so well without
1230 case LOC_CONST_BYTES
:
1231 case LOC_UNRESOLVED
:
1232 case LOC_OPTIMIZED_OUT
:
1238 /* Given a struct symbol for a variable,
1239 and a stack frame id, read the value of the variable
1240 and return a (pointer to a) struct value containing the value.
1241 If the variable cannot be found, return a zero pointer.
1242 If FRAME is NULL, use the selected_frame. */
1245 read_var_value (var
, frame
)
1246 register struct symbol
*var
;
1247 struct frame_info
*frame
;
1249 register value_ptr v
;
1250 struct type
*type
= SYMBOL_TYPE (var
);
1254 v
= allocate_value (type
);
1255 VALUE_LVAL (v
) = lval_memory
; /* The most likely possibility. */
1256 VALUE_BFD_SECTION (v
) = SYMBOL_BFD_SECTION (var
);
1258 len
= TYPE_LENGTH (type
);
1261 frame
= selected_frame
;
1263 switch (SYMBOL_CLASS (var
))
1266 /* Put the constant back in target format. */
1267 store_signed_integer (VALUE_CONTENTS_RAW (v
), len
,
1268 (LONGEST
) SYMBOL_VALUE (var
));
1269 VALUE_LVAL (v
) = not_lval
;
1273 /* Put the constant back in target format. */
1274 if (overlay_debugging
)
1275 store_address (VALUE_CONTENTS_RAW (v
), len
,
1276 (LONGEST
) symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
1277 SYMBOL_BFD_SECTION (var
)));
1279 store_address (VALUE_CONTENTS_RAW (v
), len
,
1280 (LONGEST
) SYMBOL_VALUE_ADDRESS (var
));
1281 VALUE_LVAL (v
) = not_lval
;
1284 case LOC_CONST_BYTES
:
1287 bytes_addr
= SYMBOL_VALUE_BYTES (var
);
1288 memcpy (VALUE_CONTENTS_RAW (v
), bytes_addr
, len
);
1289 VALUE_LVAL (v
) = not_lval
;
1294 if (overlay_debugging
)
1295 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
1296 SYMBOL_BFD_SECTION (var
));
1298 addr
= SYMBOL_VALUE_ADDRESS (var
);
1302 /* The import slot does not have a real address in it from the
1303 dynamic loader (dld.sl on HP-UX), if the target hasn't begun
1304 execution yet, so check for that. */
1305 if (!target_has_execution
)
1307 Attempt to access variable defined in different shared object or load module when\n\
1308 addresses have not been bound by the dynamic loader. Try again when executable is running.");
1310 addr
= SYMBOL_VALUE_ADDRESS (var
);
1311 addr
= read_memory_unsigned_integer
1312 (addr
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
1318 addr
= FRAME_ARGS_ADDRESS (frame
);
1321 addr
+= SYMBOL_VALUE (var
);
1327 addr
= FRAME_ARGS_ADDRESS (frame
);
1330 addr
+= SYMBOL_VALUE (var
);
1331 addr
= read_memory_unsigned_integer
1332 (addr
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
1339 addr
= FRAME_LOCALS_ADDRESS (frame
);
1340 addr
+= SYMBOL_VALUE (var
);
1344 case LOC_BASEREG_ARG
:
1346 char buf
[MAX_REGISTER_RAW_SIZE
];
1347 get_saved_register (buf
, NULL
, NULL
, frame
, SYMBOL_BASEREG (var
),
1349 addr
= extract_address (buf
, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var
)));
1350 addr
+= SYMBOL_VALUE (var
);
1354 case LOC_THREAD_LOCAL_STATIC
:
1356 char buf
[MAX_REGISTER_RAW_SIZE
];
1358 get_saved_register (buf
, NULL
, NULL
, frame
, SYMBOL_BASEREG (var
),
1360 addr
= extract_address (buf
, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var
)));
1361 addr
+= SYMBOL_VALUE (var
);
1366 error ("Cannot look up value of a typedef");
1370 if (overlay_debugging
)
1371 VALUE_ADDRESS (v
) = symbol_overlayed_address
1372 (BLOCK_START (SYMBOL_BLOCK_VALUE (var
)), SYMBOL_BFD_SECTION (var
));
1374 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (var
));
1379 case LOC_REGPARM_ADDR
:
1382 int regno
= SYMBOL_VALUE (var
);
1387 b
= get_frame_block (frame
);
1389 if (SYMBOL_CLASS (var
) == LOC_REGPARM_ADDR
)
1391 regval
= value_from_register (lookup_pointer_type (type
),
1396 error ("Value of register variable not available.");
1398 addr
= value_as_pointer (regval
);
1399 VALUE_LVAL (v
) = lval_memory
;
1403 regval
= value_from_register (type
, regno
, frame
);
1406 error ("Value of register variable not available.");
1412 case LOC_UNRESOLVED
:
1414 struct minimal_symbol
*msym
;
1416 msym
= lookup_minimal_symbol (SYMBOL_NAME (var
), NULL
, NULL
);
1419 if (overlay_debugging
)
1420 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym
),
1421 SYMBOL_BFD_SECTION (msym
));
1423 addr
= SYMBOL_VALUE_ADDRESS (msym
);
1427 case LOC_OPTIMIZED_OUT
:
1428 VALUE_LVAL (v
) = not_lval
;
1429 VALUE_OPTIMIZED_OUT (v
) = 1;
1433 error ("Cannot look up value of a botched symbol.");
1437 VALUE_ADDRESS (v
) = addr
;
1442 /* Return a value of type TYPE, stored in register REGNUM, in frame
1445 NOTE: returns NULL if register value is not available.
1446 Caller will check return value or die! */
1449 value_from_register (type
, regnum
, frame
)
1452 struct frame_info
*frame
;
1454 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
1457 value_ptr v
= allocate_value (type
);
1458 char *value_bytes
= 0;
1459 int value_bytes_copied
= 0;
1460 int num_storage_locs
;
1461 enum lval_type lval
;
1464 CHECK_TYPEDEF (type
);
1465 len
= TYPE_LENGTH (type
);
1467 VALUE_REGNO (v
) = regnum
;
1469 num_storage_locs
= (len
> REGISTER_VIRTUAL_SIZE (regnum
) ?
1470 ((len
- 1) / REGISTER_RAW_SIZE (regnum
)) + 1 :
1473 if (num_storage_locs
> 1
1474 #ifdef GDB_TARGET_IS_H8500
1475 || TYPE_CODE (type
) == TYPE_CODE_PTR
1479 /* Value spread across multiple storage locations. */
1482 int mem_stor
= 0, reg_stor
= 0;
1483 int mem_tracking
= 1;
1484 CORE_ADDR last_addr
= 0;
1485 CORE_ADDR first_addr
= 0;
1487 value_bytes
= (char *) alloca (len
+ MAX_REGISTER_RAW_SIZE
);
1489 /* Copy all of the data out, whereever it may be. */
1491 #ifdef GDB_TARGET_IS_H8500
1492 /* This piece of hideosity is required because the H8500 treats registers
1493 differently depending upon whether they are used as pointers or not. As a
1494 pointer, a register needs to have a page register tacked onto the front.
1495 An alternate way to do this would be to have gcc output different register
1496 numbers for the pointer & non-pointer form of the register. But, it
1497 doesn't, so we're stuck with this. */
1499 if (TYPE_CODE (type
) == TYPE_CODE_PTR
1510 page_regnum
= SEG_D_REGNUM
;
1514 page_regnum
= SEG_E_REGNUM
;
1518 page_regnum
= SEG_T_REGNUM
;
1523 get_saved_register (value_bytes
+ 1,
1530 if (register_valid
[page_regnum
] == -1)
1531 return NULL
; /* register value not available */
1533 if (lval
== lval_register
)
1540 get_saved_register (value_bytes
+ 2,
1547 if (register_valid
[regnum
] == -1)
1548 return NULL
; /* register value not available */
1550 if (lval
== lval_register
)
1555 mem_tracking
= mem_tracking
&& (addr
== last_addr
);
1560 #endif /* GDB_TARGET_IS_H8500 */
1561 for (local_regnum
= regnum
;
1562 value_bytes_copied
< len
;
1563 (value_bytes_copied
+= REGISTER_RAW_SIZE (local_regnum
),
1566 get_saved_register (value_bytes
+ value_bytes_copied
,
1573 if (register_valid
[local_regnum
] == -1)
1574 return NULL
; /* register value not available */
1576 if (regnum
== local_regnum
)
1578 if (lval
== lval_register
)
1586 && (regnum
== local_regnum
1587 || addr
== last_addr
));
1592 if ((reg_stor
&& mem_stor
)
1593 || (mem_stor
&& !mem_tracking
))
1594 /* Mixed storage; all of the hassle we just went through was
1595 for some good purpose. */
1597 VALUE_LVAL (v
) = lval_reg_frame_relative
;
1598 VALUE_FRAME (v
) = FRAME_FP (frame
);
1599 VALUE_FRAME_REGNUM (v
) = regnum
;
1603 VALUE_LVAL (v
) = lval_memory
;
1604 VALUE_ADDRESS (v
) = first_addr
;
1608 VALUE_LVAL (v
) = lval_register
;
1609 VALUE_ADDRESS (v
) = first_addr
;
1612 fatal ("value_from_register: Value not stored anywhere!");
1614 VALUE_OPTIMIZED_OUT (v
) = optim
;
1616 /* Any structure stored in more than one register will always be
1617 an integral number of registers. Otherwise, you'd need to do
1618 some fiddling with the last register copied here for little
1621 /* Copy into the contents section of the value. */
1622 memcpy (VALUE_CONTENTS_RAW (v
), value_bytes
, len
);
1624 /* Finally do any conversion necessary when extracting this
1625 type from more than one register. */
1626 #ifdef REGISTER_CONVERT_TO_TYPE
1627 REGISTER_CONVERT_TO_TYPE (regnum
, type
, VALUE_CONTENTS_RAW (v
));
1632 /* Data is completely contained within a single register. Locate the
1633 register's contents in a real register or in core;
1634 read the data in raw format. */
1636 get_saved_register (raw_buffer
, &optim
, &addr
, frame
, regnum
, &lval
);
1638 if (register_valid
[regnum
] == -1)
1639 return NULL
; /* register value not available */
1641 VALUE_OPTIMIZED_OUT (v
) = optim
;
1642 VALUE_LVAL (v
) = lval
;
1643 VALUE_ADDRESS (v
) = addr
;
1645 /* Convert raw data to virtual format if necessary. */
1647 if (REGISTER_CONVERTIBLE (regnum
))
1649 REGISTER_CONVERT_TO_VIRTUAL (regnum
, type
,
1650 raw_buffer
, VALUE_CONTENTS_RAW (v
));
1654 /* Raw and virtual formats are the same for this register. */
1656 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
&& len
< REGISTER_RAW_SIZE (regnum
))
1658 /* Big-endian, and we want less than full size. */
1659 VALUE_OFFSET (v
) = REGISTER_RAW_SIZE (regnum
) - len
;
1662 memcpy (VALUE_CONTENTS_RAW (v
), raw_buffer
+ VALUE_OFFSET (v
), len
);
1668 /* Given a struct symbol for a variable or function,
1669 and a stack frame id,
1670 return a (pointer to a) struct value containing the properly typed
1674 locate_var_value (var
, frame
)
1675 register struct symbol
*var
;
1676 struct frame_info
*frame
;
1679 struct type
*type
= SYMBOL_TYPE (var
);
1680 value_ptr lazy_value
;
1682 /* Evaluate it first; if the result is a memory address, we're fine.
1683 Lazy evaluation pays off here. */
1685 lazy_value
= read_var_value (var
, frame
);
1686 if (lazy_value
== 0)
1687 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var
));
1689 if (VALUE_LAZY (lazy_value
)
1690 || TYPE_CODE (type
) == TYPE_CODE_FUNC
)
1694 addr
= VALUE_ADDRESS (lazy_value
);
1695 val
= value_from_longest (lookup_pointer_type (type
), (LONGEST
) addr
);
1696 VALUE_BFD_SECTION (val
) = VALUE_BFD_SECTION (lazy_value
);
1700 /* Not a memory address; check what the problem was. */
1701 switch (VALUE_LVAL (lazy_value
))
1704 case lval_reg_frame_relative
:
1705 error ("Address requested for identifier \"%s\" which is in a register.",
1706 SYMBOL_SOURCE_NAME (var
));
1710 error ("Can't take address of \"%s\" which isn't an lvalue.",
1711 SYMBOL_SOURCE_NAME (var
));
1714 return 0; /* For lint -- never reached */
1718 static void build_findvar
PARAMS ((void));
1722 /* We allocate some extra slop since we do a lot of memcpy's around
1723 `registers', and failing-soft is better than failing hard. */
1724 int sizeof_registers
= REGISTER_BYTES
+ /* SLOP */ 256;
1725 int sizeof_register_valid
= NUM_REGS
* sizeof (*register_valid
);
1726 registers
= xmalloc (sizeof_registers
);
1727 memset (registers
, 0, sizeof_registers
);
1728 register_valid
= xmalloc (sizeof_register_valid
);
1729 memset (register_valid
, 0, sizeof_register_valid
);
1732 void _initialize_findvar
PARAMS ((void));
1734 _initialize_findvar ()
1738 register_gdbarch_swap (®isters
, sizeof (registers
), NULL
);
1739 register_gdbarch_swap (®ister_valid
, sizeof (register_valid
), NULL
);
1740 register_gdbarch_swap (NULL
, 0, build_findvar
);