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 internal_error ("Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
610 REGISTER_NAME (regnum
),
612 REGISTER_RAW_SIZE (regnum
),
613 REGISTER_VIRTUAL_SIZE (regnum
));
614 VALUE_LVAL (reg_val
) = lval
;
615 VALUE_ADDRESS (reg_val
) = addr
;
616 VALUE_REGNO (reg_val
) = regnum
;
617 VALUE_OPTIMIZED_OUT (reg_val
) = optim
;
621 /* Low level examining and depositing of registers.
623 The caller is responsible for making
624 sure that the inferior is stopped before calling the fetching routines,
625 or it will get garbage. (a change from GDB version 3, in which
626 the caller got the value from the last stop). */
628 /* Contents and state of the registers (in target byte order). */
632 /* VALID_REGISTER is non-zero if it has been fetched, -1 if the
633 register value was not available. */
635 signed char *register_valid
;
637 /* The thread/process associated with the current set of registers. For now,
638 -1 is special, and means `no current process'. */
639 int registers_pid
= -1;
641 /* Indicate that registers may have changed, so invalidate the cache. */
647 int numregs
= ARCH_NUM_REGS
;
651 /* Force cleanup of any alloca areas if using C alloca instead of
652 a builtin alloca. This particular call is used to clean up
653 areas allocated by low level target code which may build up
654 during lengthy interactions between gdb and the target before
655 gdb gives control to the user (ie watchpoints). */
658 for (i
= 0; i
< numregs
; i
++)
659 register_valid
[i
] = 0;
661 if (registers_changed_hook
)
662 registers_changed_hook ();
665 /* Indicate that all registers have been fetched, so mark them all valid. */
670 int numregs
= ARCH_NUM_REGS
;
671 for (i
= 0; i
< numregs
; i
++)
672 register_valid
[i
] = 1;
675 /* read_register_bytes and write_register_bytes are generally a *BAD* idea.
676 They are inefficient because they need to check for partial updates, which
677 can only be done by scanning through all of the registers and seeing if the
678 bytes that are being read/written fall inside of an invalid register. [The
679 main reason this is necessary is that register sizes can vary, so a simple
680 index won't suffice.] It is far better to call read_register_gen if you
681 want to get at the raw register contents, as it only takes a regno as an
682 argument, and therefore can't do a partial register update. It would also
683 be good to have a write_register_gen for similar reasons.
685 Prior to the recent fixes to check for partial updates, both read and
686 write_register_bytes always checked to see if any registers were stale, and
687 then called target_fetch_registers (-1) to update the whole set. This
688 caused really slowed things down for remote targets. */
690 /* Copy INLEN bytes of consecutive data from registers
691 starting with the INREGBYTE'th byte of register data
692 into memory at MYADDR. */
695 read_register_bytes (inregbyte
, myaddr
, inlen
)
700 int inregend
= inregbyte
+ inlen
;
703 if (registers_pid
!= inferior_pid
)
705 registers_changed ();
706 registers_pid
= inferior_pid
;
709 /* See if we are trying to read bytes from out-of-date registers. If so,
710 update just those registers. */
712 for (regno
= 0; regno
< NUM_REGS
; regno
++)
714 int regstart
, regend
;
717 if (register_valid
[regno
])
720 if (REGISTER_NAME (regno
) == NULL
|| *REGISTER_NAME (regno
) == '\0')
723 regstart
= REGISTER_BYTE (regno
);
724 regend
= regstart
+ REGISTER_RAW_SIZE (regno
);
726 startin
= regstart
>= inregbyte
&& regstart
< inregend
;
727 endin
= regend
> inregbyte
&& regend
<= inregend
;
729 if (!startin
&& !endin
)
732 /* We've found an invalid register where at least one byte will be read.
733 Update it from the target. */
735 target_fetch_registers (regno
);
737 if (!register_valid
[regno
])
738 error ("read_register_bytes: Couldn't update register %d.", regno
);
742 memcpy (myaddr
, ®isters
[inregbyte
], inlen
);
745 /* Read register REGNO into memory at MYADDR, which must be large enough
746 for REGISTER_RAW_BYTES (REGNO). Target byte-order.
747 If the register is known to be the size of a CORE_ADDR or smaller,
748 read_register can be used instead. */
750 read_register_gen (regno
, myaddr
)
754 if (registers_pid
!= inferior_pid
)
756 registers_changed ();
757 registers_pid
= inferior_pid
;
760 if (!register_valid
[regno
])
761 target_fetch_registers (regno
);
762 memcpy (myaddr
, ®isters
[REGISTER_BYTE (regno
)],
763 REGISTER_RAW_SIZE (regno
));
766 /* Write register REGNO at MYADDR to the target. MYADDR points at
767 REGISTER_RAW_BYTES(REGNO), which must be in target byte-order. */
770 write_register_gen (regno
, myaddr
)
776 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
777 the registers array if something writes to this register. */
778 if (CANNOT_STORE_REGISTER (regno
))
781 if (registers_pid
!= inferior_pid
)
783 registers_changed ();
784 registers_pid
= inferior_pid
;
787 size
= REGISTER_RAW_SIZE (regno
);
789 /* If we have a valid copy of the register, and new value == old value,
790 then don't bother doing the actual store. */
792 if (register_valid
[regno
]
793 && memcmp (®isters
[REGISTER_BYTE (regno
)], myaddr
, size
) == 0)
796 target_prepare_to_store ();
798 memcpy (®isters
[REGISTER_BYTE (regno
)], myaddr
, size
);
800 register_valid
[regno
] = 1;
802 target_store_registers (regno
);
805 /* Copy INLEN bytes of consecutive data from memory at MYADDR
806 into registers starting with the MYREGSTART'th byte of register data. */
809 write_register_bytes (myregstart
, myaddr
, inlen
)
814 int myregend
= myregstart
+ inlen
;
817 target_prepare_to_store ();
819 /* Scan through the registers updating any that are covered by the range
820 myregstart<=>myregend using write_register_gen, which does nice things
821 like handling threads, and avoiding updates when the new and old contents
824 for (regno
= 0; regno
< NUM_REGS
; regno
++)
826 int regstart
, regend
;
828 char regbuf
[MAX_REGISTER_RAW_SIZE
];
830 regstart
= REGISTER_BYTE (regno
);
831 regend
= regstart
+ REGISTER_RAW_SIZE (regno
);
833 startin
= regstart
>= myregstart
&& regstart
< myregend
;
834 endin
= regend
> myregstart
&& regend
<= myregend
;
836 if (!startin
&& !endin
)
837 continue; /* Register is completely out of range */
839 if (startin
&& endin
) /* register is completely in range */
841 write_register_gen (regno
, myaddr
+ (regstart
- myregstart
));
845 /* We may be doing a partial update of an invalid register. Update it
846 from the target before scribbling on it. */
847 read_register_gen (regno
, regbuf
);
850 memcpy (registers
+ regstart
,
851 myaddr
+ regstart
- myregstart
,
852 myregend
- regstart
);
854 memcpy (registers
+ myregstart
,
856 regend
- myregstart
);
857 target_store_registers (regno
);
861 /* Return the raw contents of register REGNO, regarding it as an integer. */
862 /* This probably should be returning LONGEST rather than CORE_ADDR. */
865 read_register (regno
)
868 if (registers_pid
!= inferior_pid
)
870 registers_changed ();
871 registers_pid
= inferior_pid
;
874 if (!register_valid
[regno
])
875 target_fetch_registers (regno
);
877 return (CORE_ADDR
) extract_address (®isters
[REGISTER_BYTE (regno
)],
878 REGISTER_RAW_SIZE (regno
));
882 read_register_pid (regno
, pid
)
888 if (pid
== inferior_pid
)
889 return read_register (regno
);
891 save_pid
= inferior_pid
;
895 retval
= read_register (regno
);
897 inferior_pid
= save_pid
;
902 /* Store VALUE, into the raw contents of register number REGNO.
903 This should probably write a LONGEST rather than a CORE_ADDR */
906 write_register (regno
, val
)
913 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
914 the registers array if something writes to this register. */
915 if (CANNOT_STORE_REGISTER (regno
))
918 if (registers_pid
!= inferior_pid
)
920 registers_changed ();
921 registers_pid
= inferior_pid
;
924 size
= REGISTER_RAW_SIZE (regno
);
926 store_signed_integer (buf
, size
, (LONGEST
) val
);
928 /* If we have a valid copy of the register, and new value == old value,
929 then don't bother doing the actual store. */
931 if (register_valid
[regno
]
932 && memcmp (®isters
[REGISTER_BYTE (regno
)], buf
, size
) == 0)
935 target_prepare_to_store ();
937 memcpy (®isters
[REGISTER_BYTE (regno
)], buf
, size
);
939 register_valid
[regno
] = 1;
941 target_store_registers (regno
);
945 write_register_pid (regno
, val
, pid
)
952 if (pid
== inferior_pid
)
954 write_register (regno
, val
);
958 save_pid
= inferior_pid
;
962 write_register (regno
, val
);
964 inferior_pid
= save_pid
;
967 /* Record that register REGNO contains VAL.
968 This is used when the value is obtained from the inferior or core dump,
969 so there is no need to store the value there.
971 If VAL is a NULL pointer, then it's probably an unsupported register. We
972 just set it's value to all zeros. We might want to record this fact, and
973 report it to the users of read_register and friends.
977 supply_register (regno
, val
)
982 if (registers_pid
!= inferior_pid
)
984 registers_changed ();
985 registers_pid
= inferior_pid
;
989 register_valid
[regno
] = 1;
991 memcpy (®isters
[REGISTER_BYTE (regno
)], val
, REGISTER_RAW_SIZE (regno
));
993 memset (®isters
[REGISTER_BYTE (regno
)], '\000', REGISTER_RAW_SIZE (regno
));
995 /* On some architectures, e.g. HPPA, there are a few stray bits in some
996 registers, that the rest of the code would like to ignore. */
997 #ifdef CLEAN_UP_REGISTER_VALUE
998 CLEAN_UP_REGISTER_VALUE (regno
, ®isters
[REGISTER_BYTE (regno
)]);
1003 /* This routine is getting awfully cluttered with #if's. It's probably
1004 time to turn this into READ_PC and define it in the tm.h file.
1007 1999-06-08: The following were re-written so that it assumes the
1008 existance of a TARGET_READ_PC et.al. macro. A default generic
1009 version of that macro is made available where needed.
1011 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
1012 by the multi-arch framework, it will eventually be possible to
1013 eliminate the intermediate read_pc_pid(). The client would call
1014 TARGET_READ_PC directly. (cagney). */
1016 #ifndef TARGET_READ_PC
1017 #define TARGET_READ_PC generic_target_read_pc
1021 generic_target_read_pc (pid
)
1026 CORE_ADDR pc_val
= ADDR_BITS_REMOVE ((CORE_ADDR
) read_register_pid (PC_REGNUM
, pid
));
1030 internal_error ("generic_target_read_pc");
1038 int saved_inferior_pid
;
1041 /* In case pid != inferior_pid. */
1042 saved_inferior_pid
= inferior_pid
;
1045 pc_val
= TARGET_READ_PC (pid
);
1047 inferior_pid
= saved_inferior_pid
;
1054 return read_pc_pid (inferior_pid
);
1057 #ifndef TARGET_WRITE_PC
1058 #define TARGET_WRITE_PC generic_target_write_pc
1062 generic_target_write_pc (pc
, pid
)
1068 write_register_pid (PC_REGNUM
, pc
, pid
);
1070 if (NPC_REGNUM
>= 0)
1071 write_register_pid (NPC_REGNUM
, pc
+ 4, pid
);
1073 if (NNPC_REGNUM
>= 0)
1074 write_register_pid (NNPC_REGNUM
, pc
+ 8, pid
);
1078 internal_error ("generic_target_write_pc");
1083 write_pc_pid (pc
, pid
)
1087 int saved_inferior_pid
;
1089 /* In case pid != inferior_pid. */
1090 saved_inferior_pid
= inferior_pid
;
1093 TARGET_WRITE_PC (pc
, pid
);
1095 inferior_pid
= saved_inferior_pid
;
1102 write_pc_pid (pc
, inferior_pid
);
1105 /* Cope with strage ways of getting to the stack and frame pointers */
1107 #ifndef TARGET_READ_SP
1108 #define TARGET_READ_SP generic_target_read_sp
1112 generic_target_read_sp ()
1116 return read_register (SP_REGNUM
);
1118 internal_error ("generic_target_read_sp");
1124 return TARGET_READ_SP ();
1127 #ifndef TARGET_WRITE_SP
1128 #define TARGET_WRITE_SP generic_target_write_sp
1132 generic_target_write_sp (val
)
1138 write_register (SP_REGNUM
, val
);
1142 internal_error ("generic_target_write_sp");
1149 TARGET_WRITE_SP (val
);
1152 #ifndef TARGET_READ_FP
1153 #define TARGET_READ_FP generic_target_read_fp
1157 generic_target_read_fp ()
1161 return read_register (FP_REGNUM
);
1163 internal_error ("generic_target_read_fp");
1169 return TARGET_READ_FP ();
1172 #ifndef TARGET_WRITE_FP
1173 #define TARGET_WRITE_FP generic_target_write_fp
1177 generic_target_write_fp (val
)
1183 write_register (FP_REGNUM
, val
);
1187 internal_error ("generic_target_write_fp");
1194 TARGET_WRITE_FP (val
);
1197 /* Will calling read_var_value or locate_var_value on SYM end
1198 up caring what frame it is being evaluated relative to? SYM must
1201 symbol_read_needs_frame (sym
)
1204 switch (SYMBOL_CLASS (sym
))
1206 /* All cases listed explicitly so that gcc -Wall will detect it if
1207 we failed to consider one. */
1212 case LOC_REGPARM_ADDR
:
1216 case LOC_BASEREG_ARG
:
1217 case LOC_THREAD_LOCAL_STATIC
:
1227 /* Getting the address of a label can be done independently of the block,
1228 even if some *uses* of that address wouldn't work so well without
1232 case LOC_CONST_BYTES
:
1233 case LOC_UNRESOLVED
:
1234 case LOC_OPTIMIZED_OUT
:
1240 /* Given a struct symbol for a variable,
1241 and a stack frame id, read the value of the variable
1242 and return a (pointer to a) struct value containing the value.
1243 If the variable cannot be found, return a zero pointer.
1244 If FRAME is NULL, use the selected_frame. */
1247 read_var_value (var
, frame
)
1248 register struct symbol
*var
;
1249 struct frame_info
*frame
;
1251 register value_ptr v
;
1252 struct type
*type
= SYMBOL_TYPE (var
);
1256 v
= allocate_value (type
);
1257 VALUE_LVAL (v
) = lval_memory
; /* The most likely possibility. */
1258 VALUE_BFD_SECTION (v
) = SYMBOL_BFD_SECTION (var
);
1260 len
= TYPE_LENGTH (type
);
1263 frame
= selected_frame
;
1265 switch (SYMBOL_CLASS (var
))
1268 /* Put the constant back in target format. */
1269 store_signed_integer (VALUE_CONTENTS_RAW (v
), len
,
1270 (LONGEST
) SYMBOL_VALUE (var
));
1271 VALUE_LVAL (v
) = not_lval
;
1275 /* Put the constant back in target format. */
1276 if (overlay_debugging
)
1277 store_address (VALUE_CONTENTS_RAW (v
), len
,
1278 (LONGEST
) symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
1279 SYMBOL_BFD_SECTION (var
)));
1281 store_address (VALUE_CONTENTS_RAW (v
), len
,
1282 (LONGEST
) SYMBOL_VALUE_ADDRESS (var
));
1283 VALUE_LVAL (v
) = not_lval
;
1286 case LOC_CONST_BYTES
:
1289 bytes_addr
= SYMBOL_VALUE_BYTES (var
);
1290 memcpy (VALUE_CONTENTS_RAW (v
), bytes_addr
, len
);
1291 VALUE_LVAL (v
) = not_lval
;
1296 if (overlay_debugging
)
1297 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var
),
1298 SYMBOL_BFD_SECTION (var
));
1300 addr
= SYMBOL_VALUE_ADDRESS (var
);
1304 /* The import slot does not have a real address in it from the
1305 dynamic loader (dld.sl on HP-UX), if the target hasn't begun
1306 execution yet, so check for that. */
1307 if (!target_has_execution
)
1309 Attempt to access variable defined in different shared object or load module when\n\
1310 addresses have not been bound by the dynamic loader. Try again when executable is running.");
1312 addr
= SYMBOL_VALUE_ADDRESS (var
);
1313 addr
= read_memory_unsigned_integer
1314 (addr
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
1320 addr
= FRAME_ARGS_ADDRESS (frame
);
1323 addr
+= SYMBOL_VALUE (var
);
1329 addr
= FRAME_ARGS_ADDRESS (frame
);
1332 addr
+= SYMBOL_VALUE (var
);
1333 addr
= read_memory_unsigned_integer
1334 (addr
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
);
1341 addr
= FRAME_LOCALS_ADDRESS (frame
);
1342 addr
+= SYMBOL_VALUE (var
);
1346 case LOC_BASEREG_ARG
:
1348 char buf
[MAX_REGISTER_RAW_SIZE
];
1349 get_saved_register (buf
, NULL
, NULL
, frame
, SYMBOL_BASEREG (var
),
1351 addr
= extract_address (buf
, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var
)));
1352 addr
+= SYMBOL_VALUE (var
);
1356 case LOC_THREAD_LOCAL_STATIC
:
1358 char buf
[MAX_REGISTER_RAW_SIZE
];
1360 get_saved_register (buf
, NULL
, NULL
, frame
, SYMBOL_BASEREG (var
),
1362 addr
= extract_address (buf
, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var
)));
1363 addr
+= SYMBOL_VALUE (var
);
1368 error ("Cannot look up value of a typedef");
1372 if (overlay_debugging
)
1373 VALUE_ADDRESS (v
) = symbol_overlayed_address
1374 (BLOCK_START (SYMBOL_BLOCK_VALUE (var
)), SYMBOL_BFD_SECTION (var
));
1376 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (var
));
1381 case LOC_REGPARM_ADDR
:
1384 int regno
= SYMBOL_VALUE (var
);
1389 b
= get_frame_block (frame
);
1391 if (SYMBOL_CLASS (var
) == LOC_REGPARM_ADDR
)
1393 regval
= value_from_register (lookup_pointer_type (type
),
1398 error ("Value of register variable not available.");
1400 addr
= value_as_pointer (regval
);
1401 VALUE_LVAL (v
) = lval_memory
;
1405 regval
= value_from_register (type
, regno
, frame
);
1408 error ("Value of register variable not available.");
1414 case LOC_UNRESOLVED
:
1416 struct minimal_symbol
*msym
;
1418 msym
= lookup_minimal_symbol (SYMBOL_NAME (var
), NULL
, NULL
);
1421 if (overlay_debugging
)
1422 addr
= symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym
),
1423 SYMBOL_BFD_SECTION (msym
));
1425 addr
= SYMBOL_VALUE_ADDRESS (msym
);
1429 case LOC_OPTIMIZED_OUT
:
1430 VALUE_LVAL (v
) = not_lval
;
1431 VALUE_OPTIMIZED_OUT (v
) = 1;
1435 error ("Cannot look up value of a botched symbol.");
1439 VALUE_ADDRESS (v
) = addr
;
1444 /* Return a value of type TYPE, stored in register REGNUM, in frame
1447 NOTE: returns NULL if register value is not available.
1448 Caller will check return value or die! */
1451 value_from_register (type
, regnum
, frame
)
1454 struct frame_info
*frame
;
1456 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
1459 value_ptr v
= allocate_value (type
);
1460 char *value_bytes
= 0;
1461 int value_bytes_copied
= 0;
1462 int num_storage_locs
;
1463 enum lval_type lval
;
1466 CHECK_TYPEDEF (type
);
1467 len
= TYPE_LENGTH (type
);
1469 VALUE_REGNO (v
) = regnum
;
1471 num_storage_locs
= (len
> REGISTER_VIRTUAL_SIZE (regnum
) ?
1472 ((len
- 1) / REGISTER_RAW_SIZE (regnum
)) + 1 :
1475 if (num_storage_locs
> 1
1476 #ifdef GDB_TARGET_IS_H8500
1477 || TYPE_CODE (type
) == TYPE_CODE_PTR
1481 /* Value spread across multiple storage locations. */
1484 int mem_stor
= 0, reg_stor
= 0;
1485 int mem_tracking
= 1;
1486 CORE_ADDR last_addr
= 0;
1487 CORE_ADDR first_addr
= 0;
1489 value_bytes
= (char *) alloca (len
+ MAX_REGISTER_RAW_SIZE
);
1491 /* Copy all of the data out, whereever it may be. */
1493 #ifdef GDB_TARGET_IS_H8500
1494 /* This piece of hideosity is required because the H8500 treats registers
1495 differently depending upon whether they are used as pointers or not. As a
1496 pointer, a register needs to have a page register tacked onto the front.
1497 An alternate way to do this would be to have gcc output different register
1498 numbers for the pointer & non-pointer form of the register. But, it
1499 doesn't, so we're stuck with this. */
1501 if (TYPE_CODE (type
) == TYPE_CODE_PTR
1512 page_regnum
= SEG_D_REGNUM
;
1516 page_regnum
= SEG_E_REGNUM
;
1520 page_regnum
= SEG_T_REGNUM
;
1525 get_saved_register (value_bytes
+ 1,
1532 if (register_valid
[page_regnum
] == -1)
1533 return NULL
; /* register value not available */
1535 if (lval
== lval_register
)
1542 get_saved_register (value_bytes
+ 2,
1549 if (register_valid
[regnum
] == -1)
1550 return NULL
; /* register value not available */
1552 if (lval
== lval_register
)
1557 mem_tracking
= mem_tracking
&& (addr
== last_addr
);
1562 #endif /* GDB_TARGET_IS_H8500 */
1563 for (local_regnum
= regnum
;
1564 value_bytes_copied
< len
;
1565 (value_bytes_copied
+= REGISTER_RAW_SIZE (local_regnum
),
1568 get_saved_register (value_bytes
+ value_bytes_copied
,
1575 if (register_valid
[local_regnum
] == -1)
1576 return NULL
; /* register value not available */
1578 if (regnum
== local_regnum
)
1580 if (lval
== lval_register
)
1588 && (regnum
== local_regnum
1589 || addr
== last_addr
));
1594 if ((reg_stor
&& mem_stor
)
1595 || (mem_stor
&& !mem_tracking
))
1596 /* Mixed storage; all of the hassle we just went through was
1597 for some good purpose. */
1599 VALUE_LVAL (v
) = lval_reg_frame_relative
;
1600 VALUE_FRAME (v
) = FRAME_FP (frame
);
1601 VALUE_FRAME_REGNUM (v
) = regnum
;
1605 VALUE_LVAL (v
) = lval_memory
;
1606 VALUE_ADDRESS (v
) = first_addr
;
1610 VALUE_LVAL (v
) = lval_register
;
1611 VALUE_ADDRESS (v
) = first_addr
;
1614 internal_error ("value_from_register: Value not stored anywhere!");
1616 VALUE_OPTIMIZED_OUT (v
) = optim
;
1618 /* Any structure stored in more than one register will always be
1619 an integral number of registers. Otherwise, you'd need to do
1620 some fiddling with the last register copied here for little
1623 /* Copy into the contents section of the value. */
1624 memcpy (VALUE_CONTENTS_RAW (v
), value_bytes
, len
);
1626 /* Finally do any conversion necessary when extracting this
1627 type from more than one register. */
1628 #ifdef REGISTER_CONVERT_TO_TYPE
1629 REGISTER_CONVERT_TO_TYPE (regnum
, type
, VALUE_CONTENTS_RAW (v
));
1634 /* Data is completely contained within a single register. Locate the
1635 register's contents in a real register or in core;
1636 read the data in raw format. */
1638 get_saved_register (raw_buffer
, &optim
, &addr
, frame
, regnum
, &lval
);
1640 if (register_valid
[regnum
] == -1)
1641 return NULL
; /* register value not available */
1643 VALUE_OPTIMIZED_OUT (v
) = optim
;
1644 VALUE_LVAL (v
) = lval
;
1645 VALUE_ADDRESS (v
) = addr
;
1647 /* Convert raw data to virtual format if necessary. */
1649 if (REGISTER_CONVERTIBLE (regnum
))
1651 REGISTER_CONVERT_TO_VIRTUAL (regnum
, type
,
1652 raw_buffer
, VALUE_CONTENTS_RAW (v
));
1656 /* Raw and virtual formats are the same for this register. */
1658 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
&& len
< REGISTER_RAW_SIZE (regnum
))
1660 /* Big-endian, and we want less than full size. */
1661 VALUE_OFFSET (v
) = REGISTER_RAW_SIZE (regnum
) - len
;
1664 memcpy (VALUE_CONTENTS_RAW (v
), raw_buffer
+ VALUE_OFFSET (v
), len
);
1670 /* Given a struct symbol for a variable or function,
1671 and a stack frame id,
1672 return a (pointer to a) struct value containing the properly typed
1676 locate_var_value (var
, frame
)
1677 register struct symbol
*var
;
1678 struct frame_info
*frame
;
1681 struct type
*type
= SYMBOL_TYPE (var
);
1682 value_ptr lazy_value
;
1684 /* Evaluate it first; if the result is a memory address, we're fine.
1685 Lazy evaluation pays off here. */
1687 lazy_value
= read_var_value (var
, frame
);
1688 if (lazy_value
== 0)
1689 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var
));
1691 if (VALUE_LAZY (lazy_value
)
1692 || TYPE_CODE (type
) == TYPE_CODE_FUNC
)
1696 addr
= VALUE_ADDRESS (lazy_value
);
1697 val
= value_from_longest (lookup_pointer_type (type
), (LONGEST
) addr
);
1698 VALUE_BFD_SECTION (val
) = VALUE_BFD_SECTION (lazy_value
);
1702 /* Not a memory address; check what the problem was. */
1703 switch (VALUE_LVAL (lazy_value
))
1706 case lval_reg_frame_relative
:
1707 error ("Address requested for identifier \"%s\" which is in a register.",
1708 SYMBOL_SOURCE_NAME (var
));
1712 error ("Can't take address of \"%s\" which isn't an lvalue.",
1713 SYMBOL_SOURCE_NAME (var
));
1716 return 0; /* For lint -- never reached */
1720 static void build_findvar
PARAMS ((void));
1724 /* We allocate some extra slop since we do a lot of memcpy's around
1725 `registers', and failing-soft is better than failing hard. */
1726 int sizeof_registers
= REGISTER_BYTES
+ /* SLOP */ 256;
1727 int sizeof_register_valid
= NUM_REGS
* sizeof (*register_valid
);
1728 registers
= xmalloc (sizeof_registers
);
1729 memset (registers
, 0, sizeof_registers
);
1730 register_valid
= xmalloc (sizeof_register_valid
);
1731 memset (register_valid
, 0, sizeof_register_valid
);
1734 void _initialize_findvar
PARAMS ((void));
1736 _initialize_findvar ()
1740 register_gdbarch_swap (®isters
, sizeof (registers
), NULL
);
1741 register_gdbarch_swap (®ister_valid
, sizeof (register_valid
), NULL
);
1742 register_gdbarch_swap (NULL
, 0, build_findvar
);