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 (void *addr
, int len
)
65 unsigned char *startaddr
= (unsigned char *) addr
;
66 unsigned char *endaddr
= startaddr
+ len
;
68 if (len
> (int) sizeof (LONGEST
))
70 That operation is not available on integers of more than %d bytes.",
73 /* Start at the most significant end of the integer, and work towards
74 the least significant. */
75 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
78 /* Do the sign extension once at the start. */
79 retval
= ((LONGEST
) * p
^ 0x80) - 0x80;
80 for (++p
; p
< endaddr
; ++p
)
81 retval
= (retval
<< 8) | *p
;
86 /* Do the sign extension once at the start. */
87 retval
= ((LONGEST
) * p
^ 0x80) - 0x80;
88 for (--p
; p
>= startaddr
; --p
)
89 retval
= (retval
<< 8) | *p
;
95 extract_unsigned_integer (void *addr
, int len
)
99 unsigned char *startaddr
= (unsigned char *) addr
;
100 unsigned char *endaddr
= startaddr
+ len
;
102 if (len
> (int) sizeof (ULONGEST
))
104 That operation is not available on integers of more than %d bytes.",
107 /* Start at the most significant end of the integer, and work towards
108 the least significant. */
110 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
112 for (p
= startaddr
; p
< endaddr
; ++p
)
113 retval
= (retval
<< 8) | *p
;
117 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
118 retval
= (retval
<< 8) | *p
;
123 /* Sometimes a long long unsigned integer can be extracted as a
124 LONGEST value. This is done so that we can print these values
125 better. If this integer can be converted to a LONGEST, this
126 function returns 1 and sets *PVAL. Otherwise it returns 0. */
129 extract_long_unsigned_integer (void *addr
, int orig_len
, LONGEST
*pval
)
131 char *p
, *first_addr
;
135 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
137 for (p
= (char *) addr
;
138 len
> (int) sizeof (LONGEST
) && p
< (char *) addr
+ orig_len
;
150 first_addr
= (char *) addr
;
151 for (p
= (char *) addr
+ orig_len
- 1;
152 len
> (int) sizeof (LONGEST
) && p
>= (char *) addr
;
162 if (len
<= (int) sizeof (LONGEST
))
164 *pval
= (LONGEST
) extract_unsigned_integer (first_addr
,
173 /* Treat the LEN bytes at ADDR as a target-format address, and return
174 that address. ADDR is a buffer in the GDB process, not in the
177 This function should only be used by target-specific code. It
178 assumes that a pointer has the same representation as that thing's
179 address represented as an integer. Some machines use word
180 addresses, or similarly munged things, for certain types of
181 pointers, so that assumption doesn't hold everywhere.
183 Common code should use extract_typed_address instead, or something
184 else based on POINTER_TO_ADDRESS. */
187 extract_address (void *addr
, int len
)
189 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
190 whether we want this to be true eventually. */
191 return (CORE_ADDR
) extract_unsigned_integer (addr
, len
);
195 /* Treat the bytes at BUF as a pointer of type TYPE, and return the
196 address it represents. */
198 extract_typed_address (void *buf
, struct type
*type
)
200 if (TYPE_CODE (type
) != TYPE_CODE_PTR
201 && TYPE_CODE (type
) != TYPE_CODE_REF
)
202 internal_error ("findvar.c (extract_typed_address): "
203 "type is not a pointer or reference");
205 return POINTER_TO_ADDRESS (type
, buf
);
210 store_signed_integer (void *addr
, int len
, LONGEST val
)
213 unsigned char *startaddr
= (unsigned char *) addr
;
214 unsigned char *endaddr
= startaddr
+ len
;
216 /* Start at the least significant end of the integer, and work towards
217 the most significant. */
218 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
220 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
228 for (p
= startaddr
; p
< endaddr
; ++p
)
237 store_unsigned_integer (void *addr
, int len
, ULONGEST val
)
240 unsigned char *startaddr
= (unsigned char *) addr
;
241 unsigned char *endaddr
= startaddr
+ len
;
243 /* Start at the least significant end of the integer, and work towards
244 the most significant. */
245 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
247 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
255 for (p
= startaddr
; p
< endaddr
; ++p
)
263 /* Store the address VAL as a LEN-byte value in target byte order at
264 ADDR. ADDR is a buffer in the GDB process, not in the inferior.
266 This function should only be used by target-specific code. It
267 assumes that a pointer has the same representation as that thing's
268 address represented as an integer. Some machines use word
269 addresses, or similarly munged things, for certain types of
270 pointers, so that assumption doesn't hold everywhere.
272 Common code should use store_typed_address instead, or something else
273 based on ADDRESS_TO_POINTER. */
275 store_address (void *addr
, int len
, LONGEST val
)
277 store_unsigned_integer (addr
, len
, val
);
281 /* Store the address ADDR as a pointer of type TYPE at BUF, in target
284 store_typed_address (void *buf
, struct type
*type
, CORE_ADDR addr
)
286 if (TYPE_CODE (type
) != TYPE_CODE_PTR
287 && TYPE_CODE (type
) != TYPE_CODE_REF
)
288 internal_error ("findvar.c (store_typed_address): "
289 "type is not a pointer or reference");
291 ADDRESS_TO_POINTER (type
, buf
, addr
);
297 /* Extract a floating-point number from a target-order byte-stream at ADDR.
298 Returns the value as type DOUBLEST.
300 If the host and target formats agree, we just copy the raw data into the
301 appropriate type of variable and return, letting the host increase precision
302 as necessary. Otherwise, we call the conversion routine and let it do the
306 extract_floating (void *addr
, int len
)
310 if (len
* TARGET_CHAR_BIT
== TARGET_FLOAT_BIT
)
312 if (HOST_FLOAT_FORMAT
== TARGET_FLOAT_FORMAT
)
316 memcpy (&retval
, addr
, sizeof (retval
));
320 floatformat_to_doublest (TARGET_FLOAT_FORMAT
, addr
, &dretval
);
322 else if (len
* TARGET_CHAR_BIT
== TARGET_DOUBLE_BIT
)
324 if (HOST_DOUBLE_FORMAT
== TARGET_DOUBLE_FORMAT
)
328 memcpy (&retval
, addr
, sizeof (retval
));
332 floatformat_to_doublest (TARGET_DOUBLE_FORMAT
, addr
, &dretval
);
334 else if (len
* TARGET_CHAR_BIT
== TARGET_LONG_DOUBLE_BIT
)
336 if (HOST_LONG_DOUBLE_FORMAT
== TARGET_LONG_DOUBLE_FORMAT
)
340 memcpy (&retval
, addr
, sizeof (retval
));
344 floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT
, addr
, &dretval
);
348 error ("Can't deal with a floating point number of %d bytes.", len
);
355 store_floating (void *addr
, int len
, DOUBLEST val
)
357 if (len
* TARGET_CHAR_BIT
== TARGET_FLOAT_BIT
)
359 if (HOST_FLOAT_FORMAT
== TARGET_FLOAT_FORMAT
)
361 float floatval
= val
;
363 memcpy (addr
, &floatval
, sizeof (floatval
));
366 floatformat_from_doublest (TARGET_FLOAT_FORMAT
, &val
, addr
);
368 else if (len
* TARGET_CHAR_BIT
== TARGET_DOUBLE_BIT
)
370 if (HOST_DOUBLE_FORMAT
== TARGET_DOUBLE_FORMAT
)
372 double doubleval
= val
;
374 memcpy (addr
, &doubleval
, sizeof (doubleval
));
377 floatformat_from_doublest (TARGET_DOUBLE_FORMAT
, &val
, addr
);
379 else if (len
* TARGET_CHAR_BIT
== TARGET_LONG_DOUBLE_BIT
)
381 if (HOST_LONG_DOUBLE_FORMAT
== TARGET_LONG_DOUBLE_FORMAT
)
382 memcpy (addr
, &val
, sizeof (val
));
384 floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT
, &val
, addr
);
388 error ("Can't deal with a floating point number of %d bytes.", len
);
393 /* Return the address in which frame FRAME's value of register REGNUM
394 has been saved in memory. Or return zero if it has not been saved.
395 If REGNUM specifies the SP, the value we return is actually
396 the SP value, not an address where it was saved. */
399 find_saved_register (frame
, regnum
)
400 struct frame_info
*frame
;
403 register struct frame_info
*frame1
= NULL
;
404 register CORE_ADDR addr
= 0;
406 if (frame
== NULL
) /* No regs saved if want current frame */
409 #ifdef HAVE_REGISTER_WINDOWS
410 /* We assume that a register in a register window will only be saved
411 in one place (since the name changes and/or disappears as you go
412 towards inner frames), so we only call get_frame_saved_regs on
413 the current frame. This is directly in contradiction to the
414 usage below, which assumes that registers used in a frame must be
415 saved in a lower (more interior) frame. This change is a result
416 of working on a register window machine; get_frame_saved_regs
417 always returns the registers saved within a frame, within the
418 context (register namespace) of that frame. */
420 /* However, note that we don't want this to return anything if
421 nothing is saved (if there's a frame inside of this one). Also,
422 callers to this routine asking for the stack pointer want the
423 stack pointer saved for *this* frame; this is returned from the
426 if (REGISTER_IN_WINDOW_P (regnum
))
428 frame1
= get_next_frame (frame
);
430 return 0; /* Registers of this frame are active. */
432 /* Get the SP from the next frame in; it will be this
434 if (regnum
!= SP_REGNUM
)
437 FRAME_INIT_SAVED_REGS (frame1
);
438 return frame1
->saved_regs
[regnum
]; /* ... which might be zero */
440 #endif /* HAVE_REGISTER_WINDOWS */
442 /* Note that this next routine assumes that registers used in
443 frame x will be saved only in the frame that x calls and
444 frames interior to it. This is not true on the sparc, but the
445 above macro takes care of it, so we should be all right. */
449 frame1
= get_prev_frame (frame1
);
450 if (frame1
== 0 || frame1
== frame
)
452 FRAME_INIT_SAVED_REGS (frame1
);
453 if (frame1
->saved_regs
[regnum
])
454 addr
= frame1
->saved_regs
[regnum
];
460 /* Find register number REGNUM relative to FRAME and put its (raw,
461 target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the
462 variable was optimized out (and thus can't be fetched). Set *LVAL
463 to lval_memory, lval_register, or not_lval, depending on whether
464 the value was fetched from memory, from a register, or in a strange
465 and non-modifiable way (e.g. a frame pointer which was calculated
466 rather than fetched). Set *ADDRP to the address, either in memory
467 on as a REGISTER_BYTE offset into the registers array.
469 Note that this implementation never sets *LVAL to not_lval. But
470 it can be replaced by defining GET_SAVED_REGISTER and supplying
473 The argument RAW_BUFFER must point to aligned memory. */
476 default_get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
480 struct frame_info
*frame
;
482 enum lval_type
*lval
;
486 if (!target_has_registers
)
487 error ("No registers.");
489 /* Normal systems don't optimize out things with register numbers. */
490 if (optimized
!= NULL
)
492 addr
= find_saved_register (frame
, regnum
);
497 if (regnum
== SP_REGNUM
)
499 if (raw_buffer
!= NULL
)
501 /* Put it back in target format. */
502 store_address (raw_buffer
, REGISTER_RAW_SIZE (regnum
),
509 if (raw_buffer
!= NULL
)
510 read_memory (addr
, raw_buffer
, REGISTER_RAW_SIZE (regnum
));
515 *lval
= lval_register
;
516 addr
= REGISTER_BYTE (regnum
);
517 if (raw_buffer
!= NULL
)
518 read_register_gen (regnum
, raw_buffer
);
524 #if !defined (GET_SAVED_REGISTER)
525 #define GET_SAVED_REGISTER(raw_buffer, optimized, addrp, frame, regnum, lval) \
526 default_get_saved_register(raw_buffer, optimized, addrp, frame, regnum, lval)
529 get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
533 struct frame_info
*frame
;
535 enum lval_type
*lval
;
537 GET_SAVED_REGISTER (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
);
540 /* Copy the bytes of register REGNUM, relative to the input stack frame,
541 into our memory at MYADDR, in target byte order.
542 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
544 Returns 1 if could not be read, 0 if could. */
547 read_relative_register_raw_bytes_for_frame (regnum
, myaddr
, frame
)
550 struct frame_info
*frame
;
553 if (regnum
== FP_REGNUM
&& frame
)
555 /* Put it back in target format. */
556 store_address (myaddr
, REGISTER_RAW_SIZE (FP_REGNUM
),
557 (LONGEST
) FRAME_FP (frame
));
562 get_saved_register (myaddr
, &optim
, (CORE_ADDR
*) NULL
, frame
,
563 regnum
, (enum lval_type
*) NULL
);
565 if (register_valid
[regnum
] < 0)
566 return 1; /* register value not available */
571 /* Copy the bytes of register REGNUM, relative to the current stack frame,
572 into our memory at MYADDR, in target byte order.
573 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
575 Returns 1 if could not be read, 0 if could. */
578 read_relative_register_raw_bytes (regnum
, myaddr
)
582 return read_relative_register_raw_bytes_for_frame (regnum
, myaddr
,
586 /* Return a `value' with the contents of register REGNUM
587 in its virtual format, with the type specified by
588 REGISTER_VIRTUAL_TYPE.
590 NOTE: returns NULL if register value is not available.
591 Caller will check return value or die! */
594 value_of_register (regnum
)
599 register value_ptr reg_val
;
600 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
603 get_saved_register (raw_buffer
, &optim
, &addr
,
604 selected_frame
, regnum
, &lval
);
606 if (register_valid
[regnum
] < 0)
607 return NULL
; /* register value not available */
609 reg_val
= allocate_value (REGISTER_VIRTUAL_TYPE (regnum
));
611 /* Convert raw data to virtual format if necessary. */
613 if (REGISTER_CONVERTIBLE (regnum
))
615 REGISTER_CONVERT_TO_VIRTUAL (regnum
, REGISTER_VIRTUAL_TYPE (regnum
),
616 raw_buffer
, VALUE_CONTENTS_RAW (reg_val
));
618 else if (REGISTER_RAW_SIZE (regnum
) == REGISTER_VIRTUAL_SIZE (regnum
))
619 memcpy (VALUE_CONTENTS_RAW (reg_val
), raw_buffer
,
620 REGISTER_RAW_SIZE (regnum
));
622 internal_error ("Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
623 REGISTER_NAME (regnum
),
625 REGISTER_RAW_SIZE (regnum
),
626 REGISTER_VIRTUAL_SIZE (regnum
));
627 VALUE_LVAL (reg_val
) = lval
;
628 VALUE_ADDRESS (reg_val
) = addr
;
629 VALUE_REGNO (reg_val
) = regnum
;
630 VALUE_OPTIMIZED_OUT (reg_val
) = optim
;
634 /* Low level examining and depositing of registers.
636 The caller is responsible for making
637 sure that the inferior is stopped before calling the fetching routines,
638 or it will get garbage. (a change from GDB version 3, in which
639 the caller got the value from the last stop). */
641 /* Contents and state of the registers (in target byte order). */
645 /* VALID_REGISTER is non-zero if it has been fetched, -1 if the
646 register value was not available. */
648 signed char *register_valid
;
650 /* The thread/process associated with the current set of registers. For now,
651 -1 is special, and means `no current process'. */
652 int registers_pid
= -1;
654 /* Indicate that registers may have changed, so invalidate the cache. */
660 int numregs
= ARCH_NUM_REGS
;
664 /* Force cleanup of any alloca areas if using C alloca instead of
665 a builtin alloca. This particular call is used to clean up
666 areas allocated by low level target code which may build up
667 during lengthy interactions between gdb and the target before
668 gdb gives control to the user (ie watchpoints). */
671 for (i
= 0; i
< numregs
; i
++)
672 register_valid
[i
] = 0;
674 if (registers_changed_hook
)
675 registers_changed_hook ();
678 /* Indicate that all registers have been fetched, so mark them all valid. */
683 int numregs
= ARCH_NUM_REGS
;
684 for (i
= 0; i
< numregs
; i
++)
685 register_valid
[i
] = 1;
688 /* read_register_bytes and write_register_bytes are generally a *BAD*
689 idea. They are inefficient because they need to check for partial
690 updates, which can only be done by scanning through all of the
691 registers and seeing if the bytes that are being read/written fall
692 inside of an invalid register. [The main reason this is necessary
693 is that register sizes can vary, so a simple index won't suffice.]
694 It is far better to call read_register_gen and write_register_gen
695 if you want to get at the raw register contents, as it only takes a
696 regno as an argument, and therefore can't do a partial register
699 Prior to the recent fixes to check for partial updates, both read
700 and write_register_bytes always checked to see if any registers
701 were stale, and then called target_fetch_registers (-1) to update
702 the whole set. This caused really slowed things down for remote
705 /* Copy INLEN bytes of consecutive data from registers
706 starting with the INREGBYTE'th byte of register data
707 into memory at MYADDR. */
710 read_register_bytes (inregbyte
, myaddr
, inlen
)
715 int inregend
= inregbyte
+ inlen
;
718 if (registers_pid
!= inferior_pid
)
720 registers_changed ();
721 registers_pid
= inferior_pid
;
724 /* See if we are trying to read bytes from out-of-date registers. If so,
725 update just those registers. */
727 for (regno
= 0; regno
< NUM_REGS
; regno
++)
729 int regstart
, regend
;
731 if (register_valid
[regno
])
734 if (REGISTER_NAME (regno
) == NULL
|| *REGISTER_NAME (regno
) == '\0')
737 regstart
= REGISTER_BYTE (regno
);
738 regend
= regstart
+ REGISTER_RAW_SIZE (regno
);
740 if (regend
<= inregbyte
|| inregend
<= regstart
)
741 /* The range the user wants to read doesn't overlap with regno. */
744 /* We've found an invalid register where at least one byte will be read.
745 Update it from the target. */
746 target_fetch_registers (regno
);
748 if (!register_valid
[regno
])
749 error ("read_register_bytes: Couldn't update register %d.", regno
);
753 memcpy (myaddr
, ®isters
[inregbyte
], inlen
);
756 /* Read register REGNO into memory at MYADDR, which must be large enough
757 for REGISTER_RAW_BYTES (REGNO). Target byte-order.
758 If the register is known to be the size of a CORE_ADDR or smaller,
759 read_register can be used instead. */
761 read_register_gen (regno
, myaddr
)
765 if (registers_pid
!= inferior_pid
)
767 registers_changed ();
768 registers_pid
= inferior_pid
;
771 if (!register_valid
[regno
])
772 target_fetch_registers (regno
);
773 memcpy (myaddr
, ®isters
[REGISTER_BYTE (regno
)],
774 REGISTER_RAW_SIZE (regno
));
777 /* Write register REGNO at MYADDR to the target. MYADDR points at
778 REGISTER_RAW_BYTES(REGNO), which must be in target byte-order. */
781 write_register_gen (regno
, myaddr
)
787 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
788 the registers array if something writes to this register. */
789 if (CANNOT_STORE_REGISTER (regno
))
792 if (registers_pid
!= inferior_pid
)
794 registers_changed ();
795 registers_pid
= inferior_pid
;
798 size
= REGISTER_RAW_SIZE (regno
);
800 /* If we have a valid copy of the register, and new value == old value,
801 then don't bother doing the actual store. */
803 if (register_valid
[regno
]
804 && memcmp (®isters
[REGISTER_BYTE (regno
)], myaddr
, size
) == 0)
807 target_prepare_to_store ();
809 memcpy (®isters
[REGISTER_BYTE (regno
)], myaddr
, size
);
811 register_valid
[regno
] = 1;
813 target_store_registers (regno
);
816 /* Copy INLEN bytes of consecutive data from memory at MYADDR
817 into registers starting with the MYREGSTART'th byte of register data. */
820 write_register_bytes (myregstart
, myaddr
, inlen
)
825 int myregend
= myregstart
+ inlen
;
828 target_prepare_to_store ();
830 /* Scan through the registers updating any that are covered by the range
831 myregstart<=>myregend using write_register_gen, which does nice things
832 like handling threads, and avoiding updates when the new and old contents
835 for (regno
= 0; regno
< NUM_REGS
; regno
++)
837 int regstart
, regend
;
839 regstart
= REGISTER_BYTE (regno
);
840 regend
= regstart
+ REGISTER_RAW_SIZE (regno
);
842 /* Is this register completely outside the range the user is writing? */
843 if (myregend
<= regstart
|| regend
<= myregstart
)
846 /* Is this register completely within the range the user is writing? */
847 else if (myregstart
<= regstart
&& regend
<= myregend
)
848 write_register_gen (regno
, myaddr
+ (regstart
- myregstart
));
850 /* The register partially overlaps the range being written. */
853 char regbuf
[MAX_REGISTER_RAW_SIZE
];
854 /* What's the overlap between this register's bytes and
855 those the caller wants to write? */
856 int overlapstart
= max (regstart
, myregstart
);
857 int overlapend
= min (regend
, myregend
);
859 /* We may be doing a partial update of an invalid register.
860 Update it from the target before scribbling on it. */
861 read_register_gen (regno
, regbuf
);
863 memcpy (registers
+ overlapstart
,
864 myaddr
+ (overlapstart
- myregstart
),
865 overlapend
- overlapstart
);
867 target_store_registers (regno
);
873 /* Return the raw contents of register REGNO, regarding it as an integer. */
874 /* This probably should be returning LONGEST rather than CORE_ADDR. */
877 read_register (regno
)
880 if (registers_pid
!= inferior_pid
)
882 registers_changed ();
883 registers_pid
= inferior_pid
;
886 if (!register_valid
[regno
])
887 target_fetch_registers (regno
);
890 extract_unsigned_integer (®isters
[REGISTER_BYTE (regno
)],
891 REGISTER_RAW_SIZE (regno
)));
895 read_register_pid (regno
, pid
)
901 if (pid
== inferior_pid
)
902 return read_register (regno
);
904 save_pid
= inferior_pid
;
908 retval
= read_register (regno
);
910 inferior_pid
= save_pid
;
915 /* Store VALUE, into the raw contents of register number REGNO.
916 This should probably write a LONGEST rather than a CORE_ADDR */
919 write_register (regno
, val
)
926 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
927 the registers array if something writes to this register. */
928 if (CANNOT_STORE_REGISTER (regno
))
931 if (registers_pid
!= inferior_pid
)
933 registers_changed ();
934 registers_pid
= inferior_pid
;
937 size
= REGISTER_RAW_SIZE (regno
);
939 store_signed_integer (buf
, size
, (LONGEST
) val
);
941 /* If we have a valid copy of the register, and new value == old value,
942 then don't bother doing the actual store. */
944 if (register_valid
[regno
]
945 && memcmp (®isters
[REGISTER_BYTE (regno
)], buf
, size
) == 0)
948 target_prepare_to_store ();
950 memcpy (®isters
[REGISTER_BYTE (regno
)], buf
, size
);
952 register_valid
[regno
] = 1;
954 target_store_registers (regno
);
958 write_register_pid (regno
, val
, pid
)
965 if (pid
== inferior_pid
)
967 write_register (regno
, val
);
971 save_pid
= inferior_pid
;
975 write_register (regno
, val
);
977 inferior_pid
= save_pid
;
980 /* Record that register REGNO contains VAL.
981 This is used when the value is obtained from the inferior or core dump,
982 so there is no need to store the value there.
984 If VAL is a NULL pointer, then it's probably an unsupported register. We
985 just set it's value to all zeros. We might want to record this fact, and
986 report it to the users of read_register and friends.
990 supply_register (regno
, val
)
995 if (registers_pid
!= inferior_pid
)
997 registers_changed ();
998 registers_pid
= inferior_pid
;
1002 register_valid
[regno
] = 1;
1004 memcpy (®isters
[REGISTER_BYTE (regno
)], val
, REGISTER_RAW_SIZE (regno
));
1006 memset (®isters
[REGISTER_BYTE (regno
)], '\000', REGISTER_RAW_SIZE (regno
));
1008 /* On some architectures, e.g. HPPA, there are a few stray bits in some
1009 registers, that the rest of the code would like to ignore. */
1010 #ifdef CLEAN_UP_REGISTER_VALUE
1011 CLEAN_UP_REGISTER_VALUE (regno
, ®isters
[REGISTER_BYTE (regno
)]);
1016 /* This routine is getting awfully cluttered with #if's. It's probably
1017 time to turn this into READ_PC and define it in the tm.h file.
1020 1999-06-08: The following were re-written so that it assumes the
1021 existance of a TARGET_READ_PC et.al. macro. A default generic
1022 version of that macro is made available where needed.
1024 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
1025 by the multi-arch framework, it will eventually be possible to
1026 eliminate the intermediate read_pc_pid(). The client would call
1027 TARGET_READ_PC directly. (cagney). */
1029 #ifndef TARGET_READ_PC
1030 #define TARGET_READ_PC generic_target_read_pc
1034 generic_target_read_pc (int pid
)
1039 CORE_ADDR pc_val
= ADDR_BITS_REMOVE ((CORE_ADDR
) read_register_pid (PC_REGNUM
, pid
));
1043 internal_error ("generic_target_read_pc");
1051 int saved_inferior_pid
;
1054 /* In case pid != inferior_pid. */
1055 saved_inferior_pid
= inferior_pid
;
1058 pc_val
= TARGET_READ_PC (pid
);
1060 inferior_pid
= saved_inferior_pid
;
1067 return read_pc_pid (inferior_pid
);
1070 #ifndef TARGET_WRITE_PC
1071 #define TARGET_WRITE_PC generic_target_write_pc
1075 generic_target_write_pc (pc
, pid
)
1081 write_register_pid (PC_REGNUM
, pc
, pid
);
1083 if (NPC_REGNUM
>= 0)
1084 write_register_pid (NPC_REGNUM
, pc
+ 4, pid
);
1086 if (NNPC_REGNUM
>= 0)
1087 write_register_pid (NNPC_REGNUM
, pc
+ 8, pid
);
1091 internal_error ("generic_target_write_pc");
1096 write_pc_pid (pc
, pid
)
1100 int saved_inferior_pid
;
1102 /* In case pid != inferior_pid. */
1103 saved_inferior_pid
= inferior_pid
;
1106 TARGET_WRITE_PC (pc
, pid
);
1108 inferior_pid
= saved_inferior_pid
;
1115 write_pc_pid (pc
, inferior_pid
);
1118 /* Cope with strage ways of getting to the stack and frame pointers */
1120 #ifndef TARGET_READ_SP
1121 #define TARGET_READ_SP generic_target_read_sp
1125 generic_target_read_sp ()
1129 return read_register (SP_REGNUM
);
1131 internal_error ("generic_target_read_sp");
1137 return TARGET_READ_SP ();
1140 #ifndef TARGET_WRITE_SP
1141 #define TARGET_WRITE_SP generic_target_write_sp
1145 generic_target_write_sp (val
)
1151 write_register (SP_REGNUM
, val
);
1155 internal_error ("generic_target_write_sp");
1162 TARGET_WRITE_SP (val
);
1165 #ifndef TARGET_READ_FP
1166 #define TARGET_READ_FP generic_target_read_fp
1170 generic_target_read_fp ()
1174 return read_register (FP_REGNUM
);
1176 internal_error ("generic_target_read_fp");
1182 return TARGET_READ_FP ();
1185 #ifndef TARGET_WRITE_FP
1186 #define TARGET_WRITE_FP generic_target_write_fp
1190 generic_target_write_fp (val
)
1196 write_register (FP_REGNUM
, val
);
1200 internal_error ("generic_target_write_fp");
1207 TARGET_WRITE_FP (val
);
1211 /* Given a pointer of type TYPE in target form in BUF, return the
1212 address it represents. */
1214 generic_pointer_to_address (struct type
*type
, char *buf
)
1216 return extract_address (buf
, TYPE_LENGTH (type
));
1220 /* Given an address, store it as a pointer of type TYPE in target
1223 generic_address_to_pointer (struct type
*type
, char *buf
, CORE_ADDR addr
)
1225 store_address (buf
, TYPE_LENGTH (type
), addr
);
1229 /* Will calling read_var_value or locate_var_value on SYM end
1230 up caring what frame it is being evaluated relative to? SYM must
1233 symbol_read_needs_frame (sym
)
1236 switch (SYMBOL_CLASS (sym
))
1238 /* All cases listed explicitly so that gcc -Wall will detect it if
1239 we failed to consider one. */
1244 case LOC_REGPARM_ADDR
:
1248 case LOC_BASEREG_ARG
:
1249 case LOC_THREAD_LOCAL_STATIC
:
1259 /* Getting the address of a label can be done independently of the block,
1260 even if some *uses* of that address wouldn't work so well without
1264 case LOC_CONST_BYTES
:
1265 case LOC_UNRESOLVED
:
1266 case LOC_OPTIMIZED_OUT
:
1272 /* Given a struct symbol for a variable,
1273 and a stack frame id, read the value of the variable
1274 and return a (pointer to a) struct value containing the value.
1275 If the variable cannot be found, return a zero pointer.
1276 If FRAME is NULL, use the selected_frame. */
1279 read_var_value (var
, frame
)
1280 register struct symbol
*var
;
1281 struct frame_info
*frame
;
1283 register value_ptr v
;
1284 struct type
*type
= SYMBOL_TYPE (var
);
1288 v
= allocate_value (type
);
1289 VALUE_LVAL (v
) = lval_memory
; /* The most likely possibility. */
1290 VALUE_BFD_SECTION (v
) = SYMBOL_BFD_SECTION (var
);
1292 len
= TYPE_LENGTH (type
);
1295 frame
= selected_frame
;
1297 switch (SYMBOL_CLASS (var
))
1300 /* Put the constant back in target format. */
1301 store_signed_integer (VALUE_CONTENTS_RAW (v
), len
,
1302 (LONGEST
) SYMBOL_VALUE (var
));
1303 VALUE_LVAL (v
) = not_lval
;
1307 /* Put the constant back in target format. */
1308 if (overlay_debugging
)
1311 = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
1312 SYMBOL_BFD_SECTION (var
));
1313 store_typed_address (VALUE_CONTENTS_RAW (v
), type
, addr
);
1316 store_typed_address (VALUE_CONTENTS_RAW (v
), type
,
1317 SYMBOL_VALUE_ADDRESS (var
));
1318 VALUE_LVAL (v
) = not_lval
;
1321 case LOC_CONST_BYTES
:
1324 bytes_addr
= SYMBOL_VALUE_BYTES (var
);
1325 memcpy (VALUE_CONTENTS_RAW (v
), bytes_addr
, len
);
1326 VALUE_LVAL (v
) = not_lval
;
1331 if (overlay_debugging
)
1332 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
1333 SYMBOL_BFD_SECTION (var
));
1335 addr
= SYMBOL_VALUE_ADDRESS (var
);
1339 /* The import slot does not have a real address in it from the
1340 dynamic loader (dld.sl on HP-UX), if the target hasn't begun
1341 execution yet, so check for that. */
1342 if (!target_has_execution
)
1344 Attempt to access variable defined in different shared object or load module when\n\
1345 addresses have not been bound by the dynamic loader. Try again when executable is running.");
1347 addr
= SYMBOL_VALUE_ADDRESS (var
);
1348 addr
= read_memory_unsigned_integer
1349 (addr
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
1355 addr
= FRAME_ARGS_ADDRESS (frame
);
1358 addr
+= SYMBOL_VALUE (var
);
1364 addr
= FRAME_ARGS_ADDRESS (frame
);
1367 addr
+= SYMBOL_VALUE (var
);
1368 addr
= read_memory_unsigned_integer
1369 (addr
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
1376 addr
= FRAME_LOCALS_ADDRESS (frame
);
1377 addr
+= SYMBOL_VALUE (var
);
1381 case LOC_BASEREG_ARG
:
1383 char buf
[MAX_REGISTER_RAW_SIZE
];
1384 get_saved_register (buf
, NULL
, NULL
, frame
, SYMBOL_BASEREG (var
),
1386 addr
= extract_address (buf
, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var
)));
1387 addr
+= SYMBOL_VALUE (var
);
1391 case LOC_THREAD_LOCAL_STATIC
:
1393 char buf
[MAX_REGISTER_RAW_SIZE
];
1395 get_saved_register (buf
, NULL
, NULL
, frame
, SYMBOL_BASEREG (var
),
1397 addr
= extract_address (buf
, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var
)));
1398 addr
+= SYMBOL_VALUE (var
);
1403 error ("Cannot look up value of a typedef");
1407 if (overlay_debugging
)
1408 VALUE_ADDRESS (v
) = symbol_overlayed_address
1409 (BLOCK_START (SYMBOL_BLOCK_VALUE (var
)), SYMBOL_BFD_SECTION (var
));
1411 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (var
));
1416 case LOC_REGPARM_ADDR
:
1419 int regno
= SYMBOL_VALUE (var
);
1424 b
= get_frame_block (frame
);
1426 if (SYMBOL_CLASS (var
) == LOC_REGPARM_ADDR
)
1428 regval
= value_from_register (lookup_pointer_type (type
),
1433 error ("Value of register variable not available.");
1435 addr
= value_as_pointer (regval
);
1436 VALUE_LVAL (v
) = lval_memory
;
1440 regval
= value_from_register (type
, regno
, frame
);
1443 error ("Value of register variable not available.");
1449 case LOC_UNRESOLVED
:
1451 struct minimal_symbol
*msym
;
1453 msym
= lookup_minimal_symbol (SYMBOL_NAME (var
), NULL
, NULL
);
1456 if (overlay_debugging
)
1457 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym
),
1458 SYMBOL_BFD_SECTION (msym
));
1460 addr
= SYMBOL_VALUE_ADDRESS (msym
);
1464 case LOC_OPTIMIZED_OUT
:
1465 VALUE_LVAL (v
) = not_lval
;
1466 VALUE_OPTIMIZED_OUT (v
) = 1;
1470 error ("Cannot look up value of a botched symbol.");
1474 VALUE_ADDRESS (v
) = addr
;
1479 /* Return a value of type TYPE, stored in register REGNUM, in frame
1482 NOTE: returns NULL if register value is not available.
1483 Caller will check return value or die! */
1486 value_from_register (type
, regnum
, frame
)
1489 struct frame_info
*frame
;
1491 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
1494 value_ptr v
= allocate_value (type
);
1495 char *value_bytes
= 0;
1496 int value_bytes_copied
= 0;
1497 int num_storage_locs
;
1498 enum lval_type lval
;
1501 CHECK_TYPEDEF (type
);
1502 len
= TYPE_LENGTH (type
);
1504 /* Pointers on D10V are really only 16 bits, but we lie to gdb elsewhere... */
1505 if (GDB_TARGET_IS_D10V
&& TYPE_CODE (type
) == TYPE_CODE_PTR
)
1508 VALUE_REGNO (v
) = regnum
;
1510 num_storage_locs
= (len
> REGISTER_VIRTUAL_SIZE (regnum
) ?
1511 ((len
- 1) / REGISTER_RAW_SIZE (regnum
)) + 1 :
1514 if (num_storage_locs
> 1
1515 #ifdef GDB_TARGET_IS_H8500
1516 || TYPE_CODE (type
) == TYPE_CODE_PTR
1520 /* Value spread across multiple storage locations. */
1523 int mem_stor
= 0, reg_stor
= 0;
1524 int mem_tracking
= 1;
1525 CORE_ADDR last_addr
= 0;
1526 CORE_ADDR first_addr
= 0;
1528 value_bytes
= (char *) alloca (len
+ MAX_REGISTER_RAW_SIZE
);
1530 /* Copy all of the data out, whereever it may be. */
1532 #ifdef GDB_TARGET_IS_H8500
1533 /* This piece of hideosity is required because the H8500 treats registers
1534 differently depending upon whether they are used as pointers or not. As a
1535 pointer, a register needs to have a page register tacked onto the front.
1536 An alternate way to do this would be to have gcc output different register
1537 numbers for the pointer & non-pointer form of the register. But, it
1538 doesn't, so we're stuck with this. */
1540 if (TYPE_CODE (type
) == TYPE_CODE_PTR
1551 page_regnum
= SEG_D_REGNUM
;
1555 page_regnum
= SEG_E_REGNUM
;
1559 page_regnum
= SEG_T_REGNUM
;
1564 get_saved_register (value_bytes
+ 1,
1571 if (register_valid
[page_regnum
] == -1)
1572 return NULL
; /* register value not available */
1574 if (lval
== lval_register
)
1581 get_saved_register (value_bytes
+ 2,
1588 if (register_valid
[regnum
] == -1)
1589 return NULL
; /* register value not available */
1591 if (lval
== lval_register
)
1596 mem_tracking
= mem_tracking
&& (addr
== last_addr
);
1601 #endif /* GDB_TARGET_IS_H8500 */
1602 for (local_regnum
= regnum
;
1603 value_bytes_copied
< len
;
1604 (value_bytes_copied
+= REGISTER_RAW_SIZE (local_regnum
),
1607 get_saved_register (value_bytes
+ value_bytes_copied
,
1614 if (register_valid
[local_regnum
] == -1)
1615 return NULL
; /* register value not available */
1617 if (regnum
== local_regnum
)
1619 if (lval
== lval_register
)
1627 && (regnum
== local_regnum
1628 || addr
== last_addr
));
1633 if ((reg_stor
&& mem_stor
)
1634 || (mem_stor
&& !mem_tracking
))
1635 /* Mixed storage; all of the hassle we just went through was
1636 for some good purpose. */
1638 VALUE_LVAL (v
) = lval_reg_frame_relative
;
1639 VALUE_FRAME (v
) = FRAME_FP (frame
);
1640 VALUE_FRAME_REGNUM (v
) = regnum
;
1644 VALUE_LVAL (v
) = lval_memory
;
1645 VALUE_ADDRESS (v
) = first_addr
;
1649 VALUE_LVAL (v
) = lval_register
;
1650 VALUE_ADDRESS (v
) = first_addr
;
1653 internal_error ("value_from_register: Value not stored anywhere!");
1655 VALUE_OPTIMIZED_OUT (v
) = optim
;
1657 /* Any structure stored in more than one register will always be
1658 an integral number of registers. Otherwise, you'd need to do
1659 some fiddling with the last register copied here for little
1662 /* Copy into the contents section of the value. */
1663 memcpy (VALUE_CONTENTS_RAW (v
), value_bytes
, len
);
1665 /* Finally do any conversion necessary when extracting this
1666 type from more than one register. */
1667 #ifdef REGISTER_CONVERT_TO_TYPE
1668 REGISTER_CONVERT_TO_TYPE (regnum
, type
, VALUE_CONTENTS_RAW (v
));
1673 /* Data is completely contained within a single register. Locate the
1674 register's contents in a real register or in core;
1675 read the data in raw format. */
1677 get_saved_register (raw_buffer
, &optim
, &addr
, frame
, regnum
, &lval
);
1679 if (register_valid
[regnum
] == -1)
1680 return NULL
; /* register value not available */
1682 VALUE_OPTIMIZED_OUT (v
) = optim
;
1683 VALUE_LVAL (v
) = lval
;
1684 VALUE_ADDRESS (v
) = addr
;
1686 /* Convert raw data to virtual format if necessary. */
1688 if (REGISTER_CONVERTIBLE (regnum
))
1690 REGISTER_CONVERT_TO_VIRTUAL (regnum
, type
,
1691 raw_buffer
, VALUE_CONTENTS_RAW (v
));
1695 /* Raw and virtual formats are the same for this register. */
1697 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
&& len
< REGISTER_RAW_SIZE (regnum
))
1699 /* Big-endian, and we want less than full size. */
1700 VALUE_OFFSET (v
) = REGISTER_RAW_SIZE (regnum
) - len
;
1703 memcpy (VALUE_CONTENTS_RAW (v
), raw_buffer
+ VALUE_OFFSET (v
), len
);
1706 if (GDB_TARGET_IS_D10V
1707 && TYPE_CODE (type
) == TYPE_CODE_PTR
1708 && TYPE_TARGET_TYPE (type
)
1709 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_FUNC
))
1711 /* pointer to function */
1713 unsigned short snum
;
1714 snum
= (unsigned short) extract_unsigned_integer (VALUE_CONTENTS_RAW (v
), 2);
1715 num
= D10V_MAKE_IADDR (snum
);
1716 store_address (VALUE_CONTENTS_RAW (v
), 4, num
);
1718 else if (GDB_TARGET_IS_D10V
1719 && TYPE_CODE (type
) == TYPE_CODE_PTR
)
1721 /* pointer to data */
1723 unsigned short snum
;
1724 snum
= (unsigned short) extract_unsigned_integer (VALUE_CONTENTS_RAW (v
), 2);
1725 num
= D10V_MAKE_DADDR (snum
);
1726 store_address (VALUE_CONTENTS_RAW (v
), 4, num
);
1732 /* Given a struct symbol for a variable or function,
1733 and a stack frame id,
1734 return a (pointer to a) struct value containing the properly typed
1738 locate_var_value (var
, frame
)
1739 register struct symbol
*var
;
1740 struct frame_info
*frame
;
1743 struct type
*type
= SYMBOL_TYPE (var
);
1744 value_ptr lazy_value
;
1746 /* Evaluate it first; if the result is a memory address, we're fine.
1747 Lazy evaluation pays off here. */
1749 lazy_value
= read_var_value (var
, frame
);
1750 if (lazy_value
== 0)
1751 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var
));
1753 if (VALUE_LAZY (lazy_value
)
1754 || TYPE_CODE (type
) == TYPE_CODE_FUNC
)
1758 addr
= VALUE_ADDRESS (lazy_value
);
1759 val
= value_from_pointer (lookup_pointer_type (type
), addr
);
1760 VALUE_BFD_SECTION (val
) = VALUE_BFD_SECTION (lazy_value
);
1764 /* Not a memory address; check what the problem was. */
1765 switch (VALUE_LVAL (lazy_value
))
1768 case lval_reg_frame_relative
:
1769 error ("Address requested for identifier \"%s\" which is in a register.",
1770 SYMBOL_SOURCE_NAME (var
));
1774 error ("Can't take address of \"%s\" which isn't an lvalue.",
1775 SYMBOL_SOURCE_NAME (var
));
1778 return 0; /* For lint -- never reached */
1782 static void build_findvar
PARAMS ((void));
1786 /* We allocate some extra slop since we do a lot of memcpy's around
1787 `registers', and failing-soft is better than failing hard. */
1788 int sizeof_registers
= REGISTER_BYTES
+ /* SLOP */ 256;
1789 int sizeof_register_valid
= NUM_REGS
* sizeof (*register_valid
);
1790 registers
= xmalloc (sizeof_registers
);
1791 memset (registers
, 0, sizeof_registers
);
1792 register_valid
= xmalloc (sizeof_register_valid
);
1793 memset (register_valid
, 0, sizeof_register_valid
);
1796 void _initialize_findvar
PARAMS ((void));
1798 _initialize_findvar ()
1802 register_gdbarch_swap (®isters
, sizeof (registers
), NULL
);
1803 register_gdbarch_swap (®ister_valid
, sizeof (register_valid
), NULL
);
1804 register_gdbarch_swap (NULL
, 0, build_findvar
);