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 /* Extract a floating-point number from a target-order byte-stream at ADDR.
262 Returns the value as type DOUBLEST.
264 If the host and target formats agree, we just copy the raw data into the
265 appropriate type of variable and return, letting the host increase precision
266 as necessary. Otherwise, we call the conversion routine and let it do the
270 extract_floating (addr, len)
276 if (len == sizeof (float))
278 if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT)
282 memcpy (&retval, addr, sizeof (retval));
286 floatformat_to_doublest (TARGET_FLOAT_FORMAT, addr, &dretval);
288 else if (len == sizeof (double))
290 if (HOST_DOUBLE_FORMAT == TARGET_DOUBLE_FORMAT)
294 memcpy (&retval, addr, sizeof (retval));
298 floatformat_to_doublest (TARGET_DOUBLE_FORMAT, addr, &dretval);
300 else if (len == sizeof (DOUBLEST))
302 if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT)
306 memcpy (&retval, addr, sizeof (retval));
310 floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT, addr, &dretval);
312 #ifdef TARGET_EXTRACT_FLOATING
313 else if (TARGET_EXTRACT_FLOATING (addr, len, &dretval))
318 error ("Can't deal with a floating point number of %d bytes.", len);
325 store_floating (addr, len, val)
330 if (len == sizeof (float))
332 if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT)
334 float floatval = val;
336 memcpy (addr, &floatval, sizeof (floatval));
339 floatformat_from_doublest (TARGET_FLOAT_FORMAT, &val, addr);
341 else if (len == sizeof (double))
343 if (HOST_DOUBLE_FORMAT == TARGET_DOUBLE_FORMAT)
345 double doubleval = val;
347 memcpy (addr, &doubleval, sizeof (doubleval));
350 floatformat_from_doublest (TARGET_DOUBLE_FORMAT, &val, addr);
352 else if (len == sizeof (DOUBLEST))
354 if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT)
355 memcpy (addr, &val, sizeof (val));
357 floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT, &val, addr);
359 #ifdef TARGET_STORE_FLOATING
360 else if (TARGET_STORE_FLOATING (addr, len, val))
365 error ("Can't deal with a floating point number of %d bytes.", len);
370 /* Return the address in which frame FRAME's value of register REGNUM
371 has been saved in memory. Or return zero if it has not been saved.
372 If REGNUM specifies the SP, the value we return is actually
373 the SP value, not an address where it was saved. */
376 find_saved_register (frame, regnum)
377 struct frame_info *frame;
380 register struct frame_info *frame1 = NULL;
381 register CORE_ADDR addr = 0;
383 if (frame == NULL) /* No regs saved if want current frame */
386 #ifdef HAVE_REGISTER_WINDOWS
387 /* We assume that a register in a register window will only be saved
388 in one place (since the name changes and/or disappears as you go
389 towards inner frames), so we only call get_frame_saved_regs on
390 the current frame. This is directly in contradiction to the
391 usage below, which assumes that registers used in a frame must be
392 saved in a lower (more interior) frame. This change is a result
393 of working on a register window machine; get_frame_saved_regs
394 always returns the registers saved within a frame, within the
395 context (register namespace) of that frame. */
397 /* However, note that we don't want this to return anything if
398 nothing is saved (if there's a frame inside of this one). Also,
399 callers to this routine asking for the stack pointer want the
400 stack pointer saved for *this* frame; this is returned from the
403 if (REGISTER_IN_WINDOW_P (regnum))
405 frame1 = get_next_frame (frame);
407 return 0; /* Registers of this frame are active. */
409 /* Get the SP from the next frame in; it will be this
411 if (regnum != SP_REGNUM)
414 FRAME_INIT_SAVED_REGS (frame1);
415 return frame1->saved_regs[regnum]; /* ... which might be zero */
417 #endif /* HAVE_REGISTER_WINDOWS */
419 /* Note that this next routine assumes that registers used in
420 frame x will be saved only in the frame that x calls and
421 frames interior to it. This is not true on the sparc, but the
422 above macro takes care of it, so we should be all right. */
426 frame1 = get_prev_frame (frame1);
427 if (frame1 == 0 || frame1 == frame)
429 FRAME_INIT_SAVED_REGS (frame1);
430 if (frame1->saved_regs[regnum])
431 addr = frame1->saved_regs[regnum];
437 /* Find register number REGNUM relative to FRAME and put its (raw,
438 target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the
439 variable was optimized out (and thus can't be fetched). Set *LVAL
440 to lval_memory, lval_register, or not_lval, depending on whether
441 the value was fetched from memory, from a register, or in a strange
442 and non-modifiable way (e.g. a frame pointer which was calculated
443 rather than fetched). Set *ADDRP to the address, either in memory
444 on as a REGISTER_BYTE offset into the registers array.
446 Note that this implementation never sets *LVAL to not_lval. But
447 it can be replaced by defining GET_SAVED_REGISTER and supplying
450 The argument RAW_BUFFER must point to aligned memory. */
453 default_get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
457 struct frame_info *frame;
459 enum lval_type *lval;
463 if (!target_has_registers)
464 error ("No registers.");
466 /* Normal systems don't optimize out things with register numbers. */
467 if (optimized != NULL)
469 addr = find_saved_register (frame, regnum);
474 if (regnum == SP_REGNUM)
476 if (raw_buffer != NULL)
478 /* Put it back in target format. */
479 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), (LONGEST) addr);
485 if (raw_buffer != NULL)
486 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
491 *lval = lval_register;
492 addr = REGISTER_BYTE (regnum);
493 if (raw_buffer != NULL)
494 read_register_gen (regnum, raw_buffer);
500 #if !defined (GET_SAVED_REGISTER)
501 #define GET_SAVED_REGISTER(raw_buffer, optimized, addrp, frame, regnum, lval) \
502 default_get_saved_register(raw_buffer, optimized, addrp, frame, regnum, lval)
505 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
509 struct frame_info *frame;
511 enum lval_type *lval;
513 GET_SAVED_REGISTER (raw_buffer, optimized, addrp, frame, regnum, lval);
516 /* Copy the bytes of register REGNUM, relative to the input stack frame,
517 into our memory at MYADDR, in target byte order.
518 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
520 Returns 1 if could not be read, 0 if could. */
523 read_relative_register_raw_bytes_for_frame (regnum, myaddr, frame)
526 struct frame_info *frame;
529 if (regnum == FP_REGNUM && frame)
531 /* Put it back in target format. */
532 store_address (myaddr, REGISTER_RAW_SIZE (FP_REGNUM),
533 (LONGEST) FRAME_FP (frame));
538 get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, frame,
539 regnum, (enum lval_type *) NULL);
541 if (register_valid[regnum] < 0)
542 return 1; /* register value not available */
547 /* Copy the bytes of register REGNUM, relative to the current stack frame,
548 into our memory at MYADDR, in target byte order.
549 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
551 Returns 1 if could not be read, 0 if could. */
554 read_relative_register_raw_bytes (regnum, myaddr)
558 return read_relative_register_raw_bytes_for_frame (regnum, myaddr,
562 /* Return a `value' with the contents of register REGNUM
563 in its virtual format, with the type specified by
564 REGISTER_VIRTUAL_TYPE.
566 NOTE: returns NULL if register value is not available.
567 Caller will check return value or die! */
570 value_of_register (regnum)
575 register value_ptr reg_val;
576 char raw_buffer[MAX_REGISTER_RAW_SIZE];
579 get_saved_register (raw_buffer, &optim, &addr,
580 selected_frame, regnum, &lval);
582 if (register_valid[regnum] < 0)
583 return NULL; /* register value not available */
585 reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
587 /* Convert raw data to virtual format if necessary. */
589 if (REGISTER_CONVERTIBLE (regnum))
591 REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
592 raw_buffer, VALUE_CONTENTS_RAW (reg_val));
594 else if (REGISTER_RAW_SIZE (regnum) == REGISTER_VIRTUAL_SIZE (regnum))
595 memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
596 REGISTER_RAW_SIZE (regnum));
598 internal_error ("Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
599 REGISTER_NAME (regnum),
601 REGISTER_RAW_SIZE (regnum),
602 REGISTER_VIRTUAL_SIZE (regnum));
603 VALUE_LVAL (reg_val) = lval;
604 VALUE_ADDRESS (reg_val) = addr;
605 VALUE_REGNO (reg_val) = regnum;
606 VALUE_OPTIMIZED_OUT (reg_val) = optim;
610 /* Low level examining and depositing of registers.
612 The caller is responsible for making
613 sure that the inferior is stopped before calling the fetching routines,
614 or it will get garbage. (a change from GDB version 3, in which
615 the caller got the value from the last stop). */
617 /* Contents and state of the registers (in target byte order). */
621 /* VALID_REGISTER is non-zero if it has been fetched, -1 if the
622 register value was not available. */
624 signed char *register_valid;
626 /* The thread/process associated with the current set of registers. For now,
627 -1 is special, and means `no current process'. */
628 int registers_pid = -1;
630 /* Indicate that registers may have changed, so invalidate the cache. */
636 int numregs = ARCH_NUM_REGS;
640 /* Force cleanup of any alloca areas if using C alloca instead of
641 a builtin alloca. This particular call is used to clean up
642 areas allocated by low level target code which may build up
643 during lengthy interactions between gdb and the target before
644 gdb gives control to the user (ie watchpoints). */
647 for (i = 0; i < numregs; i++)
648 register_valid[i] = 0;
650 if (registers_changed_hook)
651 registers_changed_hook ();
654 /* Indicate that all registers have been fetched, so mark them all valid. */
659 int numregs = ARCH_NUM_REGS;
660 for (i = 0; i < numregs; i++)
661 register_valid[i] = 1;
664 /* read_register_bytes and write_register_bytes are generally a *BAD*
665 idea. They are inefficient because they need to check for partial
666 updates, which can only be done by scanning through all of the
667 registers and seeing if the bytes that are being read/written fall
668 inside of an invalid register. [The main reason this is necessary
669 is that register sizes can vary, so a simple index won't suffice.]
670 It is far better to call read_register_gen and write_register_gen
671 if you want to get at the raw register contents, as it only takes a
672 regno as an argument, and therefore can't do a partial register
675 Prior to the recent fixes to check for partial updates, both read
676 and write_register_bytes always checked to see if any registers
677 were stale, and then called target_fetch_registers (-1) to update
678 the whole set. This caused really slowed things down for remote
681 /* Copy INLEN bytes of consecutive data from registers
682 starting with the INREGBYTE'th byte of register data
683 into memory at MYADDR. */
686 read_register_bytes (inregbyte, myaddr, inlen)
691 int inregend = inregbyte + inlen;
694 if (registers_pid != inferior_pid)
696 registers_changed ();
697 registers_pid = inferior_pid;
700 /* See if we are trying to read bytes from out-of-date registers. If so,
701 update just those registers. */
703 for (regno = 0; regno < NUM_REGS; regno++)
705 int regstart, regend;
707 if (register_valid[regno])
710 if (REGISTER_NAME (regno) == NULL || *REGISTER_NAME (regno) == '\0')
713 regstart = REGISTER_BYTE (regno);
714 regend = regstart + REGISTER_RAW_SIZE (regno);
716 if (regend <= inregbyte || inregend <= regstart)
717 /* The range the user wants to read doesn't overlap with regno. */
720 /* We've found an invalid register where at least one byte will be read.
721 Update it from the target. */
722 target_fetch_registers (regno);
724 if (!register_valid[regno])
725 error ("read_register_bytes: Couldn't update register %d.", regno);
729 memcpy (myaddr, ®isters[inregbyte], inlen);
732 /* Read register REGNO into memory at MYADDR, which must be large enough
733 for REGISTER_RAW_BYTES (REGNO). Target byte-order.
734 If the register is known to be the size of a CORE_ADDR or smaller,
735 read_register can be used instead. */
737 read_register_gen (regno, myaddr)
741 if (registers_pid != inferior_pid)
743 registers_changed ();
744 registers_pid = inferior_pid;
747 if (!register_valid[regno])
748 target_fetch_registers (regno);
749 memcpy (myaddr, ®isters[REGISTER_BYTE (regno)],
750 REGISTER_RAW_SIZE (regno));
753 /* Write register REGNO at MYADDR to the target. MYADDR points at
754 REGISTER_RAW_BYTES(REGNO), which must be in target byte-order. */
757 write_register_gen (regno, myaddr)
763 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
764 the registers array if something writes to this register. */
765 if (CANNOT_STORE_REGISTER (regno))
768 if (registers_pid != inferior_pid)
770 registers_changed ();
771 registers_pid = inferior_pid;
774 size = REGISTER_RAW_SIZE (regno);
776 /* If we have a valid copy of the register, and new value == old value,
777 then don't bother doing the actual store. */
779 if (register_valid[regno]
780 && memcmp (®isters[REGISTER_BYTE (regno)], myaddr, size) == 0)
783 target_prepare_to_store ();
785 memcpy (®isters[REGISTER_BYTE (regno)], myaddr, size);
787 register_valid[regno] = 1;
789 target_store_registers (regno);
792 /* Copy INLEN bytes of consecutive data from memory at MYADDR
793 into registers starting with the MYREGSTART'th byte of register data. */
796 write_register_bytes (myregstart, myaddr, inlen)
801 int myregend = myregstart + inlen;
804 target_prepare_to_store ();
806 /* Scan through the registers updating any that are covered by the range
807 myregstart<=>myregend using write_register_gen, which does nice things
808 like handling threads, and avoiding updates when the new and old contents
811 for (regno = 0; regno < NUM_REGS; regno++)
813 int regstart, regend;
815 regstart = REGISTER_BYTE (regno);
816 regend = regstart + REGISTER_RAW_SIZE (regno);
818 /* Is this register completely outside the range the user is writing? */
819 if (myregend <= regstart || regend <= myregstart)
822 /* Is this register completely within the range the user is writing? */
823 else if (myregstart <= regstart && regend <= myregend)
824 write_register_gen (regno, myaddr + (regstart - myregstart));
826 /* The register partially overlaps the range being written. */
829 char regbuf[MAX_REGISTER_RAW_SIZE];
830 /* What's the overlap between this register's bytes and
831 those the caller wants to write? */
832 int overlapstart = max (regstart, myregstart);
833 int overlapend = min (regend, myregend);
835 /* We may be doing a partial update of an invalid register.
836 Update it from the target before scribbling on it. */
837 read_register_gen (regno, regbuf);
839 memcpy (registers + overlapstart,
840 myaddr + (overlapstart - myregstart),
841 overlapend - overlapstart);
843 target_store_registers (regno);
849 /* Return the raw contents of register REGNO, regarding it as an integer. */
850 /* This probably should be returning LONGEST rather than CORE_ADDR. */
853 read_register (regno)
856 if (registers_pid != inferior_pid)
858 registers_changed ();
859 registers_pid = inferior_pid;
862 if (!register_valid[regno])
863 target_fetch_registers (regno);
865 return (CORE_ADDR) extract_address (®isters[REGISTER_BYTE (regno)],
866 REGISTER_RAW_SIZE (regno));
870 read_register_pid (regno, pid)
876 if (pid == inferior_pid)
877 return read_register (regno);
879 save_pid = inferior_pid;
883 retval = read_register (regno);
885 inferior_pid = save_pid;
890 /* Store VALUE, into the raw contents of register number REGNO.
891 This should probably write a LONGEST rather than a CORE_ADDR */
894 write_register (regno, val)
901 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
902 the registers array if something writes to this register. */
903 if (CANNOT_STORE_REGISTER (regno))
906 if (registers_pid != inferior_pid)
908 registers_changed ();
909 registers_pid = inferior_pid;
912 size = REGISTER_RAW_SIZE (regno);
914 store_signed_integer (buf, size, (LONGEST) val);
916 /* If we have a valid copy of the register, and new value == old value,
917 then don't bother doing the actual store. */
919 if (register_valid[regno]
920 && memcmp (®isters[REGISTER_BYTE (regno)], buf, size) == 0)
923 target_prepare_to_store ();
925 memcpy (®isters[REGISTER_BYTE (regno)], buf, size);
927 register_valid[regno] = 1;
929 target_store_registers (regno);
933 write_register_pid (regno, val, pid)
940 if (pid == inferior_pid)
942 write_register (regno, val);
946 save_pid = inferior_pid;
950 write_register (regno, val);
952 inferior_pid = save_pid;
955 /* Record that register REGNO contains VAL.
956 This is used when the value is obtained from the inferior or core dump,
957 so there is no need to store the value there.
959 If VAL is a NULL pointer, then it's probably an unsupported register. We
960 just set it's value to all zeros. We might want to record this fact, and
961 report it to the users of read_register and friends.
965 supply_register (regno, val)
970 if (registers_pid != inferior_pid)
972 registers_changed ();
973 registers_pid = inferior_pid;
977 register_valid[regno] = 1;
979 memcpy (®isters[REGISTER_BYTE (regno)], val, REGISTER_RAW_SIZE (regno));
981 memset (®isters[REGISTER_BYTE (regno)], '\000', REGISTER_RAW_SIZE (regno));
983 /* On some architectures, e.g. HPPA, there are a few stray bits in some
984 registers, that the rest of the code would like to ignore. */
985 #ifdef CLEAN_UP_REGISTER_VALUE
986 CLEAN_UP_REGISTER_VALUE (regno, ®isters[REGISTER_BYTE (regno)]);
991 /* This routine is getting awfully cluttered with #if's. It's probably
992 time to turn this into READ_PC and define it in the tm.h file.
995 1999-06-08: The following were re-written so that it assumes the
996 existance of a TARGET_READ_PC et.al. macro. A default generic
997 version of that macro is made available where needed.
999 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
1000 by the multi-arch framework, it will eventually be possible to
1001 eliminate the intermediate read_pc_pid(). The client would call
1002 TARGET_READ_PC directly. (cagney). */
1004 #ifndef TARGET_READ_PC
1005 #define TARGET_READ_PC generic_target_read_pc
1009 generic_target_read_pc (int pid)
1014 CORE_ADDR pc_val = ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, pid));
1018 internal_error ("generic_target_read_pc");
1026 int saved_inferior_pid;
1029 /* In case pid != inferior_pid. */
1030 saved_inferior_pid = inferior_pid;
1033 pc_val = TARGET_READ_PC (pid);
1035 inferior_pid = saved_inferior_pid;
1042 return read_pc_pid (inferior_pid);
1045 #ifndef TARGET_WRITE_PC
1046 #define TARGET_WRITE_PC generic_target_write_pc
1050 generic_target_write_pc (pc, pid)
1056 write_register_pid (PC_REGNUM, pc, pid);
1058 if (NPC_REGNUM >= 0)
1059 write_register_pid (NPC_REGNUM, pc + 4, pid);
1061 if (NNPC_REGNUM >= 0)
1062 write_register_pid (NNPC_REGNUM, pc + 8, pid);
1066 internal_error ("generic_target_write_pc");
1071 write_pc_pid (pc, pid)
1075 int saved_inferior_pid;
1077 /* In case pid != inferior_pid. */
1078 saved_inferior_pid = inferior_pid;
1081 TARGET_WRITE_PC (pc, pid);
1083 inferior_pid = saved_inferior_pid;
1090 write_pc_pid (pc, inferior_pid);
1093 /* Cope with strage ways of getting to the stack and frame pointers */
1095 #ifndef TARGET_READ_SP
1096 #define TARGET_READ_SP generic_target_read_sp
1100 generic_target_read_sp ()
1104 return read_register (SP_REGNUM);
1106 internal_error ("generic_target_read_sp");
1112 return TARGET_READ_SP ();
1115 #ifndef TARGET_WRITE_SP
1116 #define TARGET_WRITE_SP generic_target_write_sp
1120 generic_target_write_sp (val)
1126 write_register (SP_REGNUM, val);
1130 internal_error ("generic_target_write_sp");
1137 TARGET_WRITE_SP (val);
1140 #ifndef TARGET_READ_FP
1141 #define TARGET_READ_FP generic_target_read_fp
1145 generic_target_read_fp ()
1149 return read_register (FP_REGNUM);
1151 internal_error ("generic_target_read_fp");
1157 return TARGET_READ_FP ();
1160 #ifndef TARGET_WRITE_FP
1161 #define TARGET_WRITE_FP generic_target_write_fp
1165 generic_target_write_fp (val)
1171 write_register (FP_REGNUM, val);
1175 internal_error ("generic_target_write_fp");
1182 TARGET_WRITE_FP (val);
1185 /* Will calling read_var_value or locate_var_value on SYM end
1186 up caring what frame it is being evaluated relative to? SYM must
1189 symbol_read_needs_frame (sym)
1192 switch (SYMBOL_CLASS (sym))
1194 /* All cases listed explicitly so that gcc -Wall will detect it if
1195 we failed to consider one. */
1200 case LOC_REGPARM_ADDR:
1204 case LOC_BASEREG_ARG:
1205 case LOC_THREAD_LOCAL_STATIC:
1215 /* Getting the address of a label can be done independently of the block,
1216 even if some *uses* of that address wouldn't work so well without
1220 case LOC_CONST_BYTES:
1221 case LOC_UNRESOLVED:
1222 case LOC_OPTIMIZED_OUT:
1228 /* Given a struct symbol for a variable,
1229 and a stack frame id, read the value of the variable
1230 and return a (pointer to a) struct value containing the value.
1231 If the variable cannot be found, return a zero pointer.
1232 If FRAME is NULL, use the selected_frame. */
1235 read_var_value (var, frame)
1236 register struct symbol *var;
1237 struct frame_info *frame;
1239 register value_ptr v;
1240 struct type *type = SYMBOL_TYPE (var);
1244 v = allocate_value (type);
1245 VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
1246 VALUE_BFD_SECTION (v) = SYMBOL_BFD_SECTION (var);
1248 len = TYPE_LENGTH (type);
1251 frame = selected_frame;
1253 switch (SYMBOL_CLASS (var))
1256 /* Put the constant back in target format. */
1257 store_signed_integer (VALUE_CONTENTS_RAW (v), len,
1258 (LONGEST) SYMBOL_VALUE (var));
1259 VALUE_LVAL (v) = not_lval;
1263 /* Put the constant back in target format. */
1264 if (overlay_debugging)
1265 store_address (VALUE_CONTENTS_RAW (v), len,
1266 (LONGEST) symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
1267 SYMBOL_BFD_SECTION (var)));
1269 store_address (VALUE_CONTENTS_RAW (v), len,
1270 (LONGEST) SYMBOL_VALUE_ADDRESS (var));
1271 VALUE_LVAL (v) = not_lval;
1274 case LOC_CONST_BYTES:
1277 bytes_addr = SYMBOL_VALUE_BYTES (var);
1278 memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
1279 VALUE_LVAL (v) = not_lval;
1284 if (overlay_debugging)
1285 addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
1286 SYMBOL_BFD_SECTION (var));
1288 addr = SYMBOL_VALUE_ADDRESS (var);
1292 /* The import slot does not have a real address in it from the
1293 dynamic loader (dld.sl on HP-UX), if the target hasn't begun
1294 execution yet, so check for that. */
1295 if (!target_has_execution)
1297 Attempt to access variable defined in different shared object or load module when\n\
1298 addresses have not been bound by the dynamic loader. Try again when executable is running.");
1300 addr = SYMBOL_VALUE_ADDRESS (var);
1301 addr = read_memory_unsigned_integer
1302 (addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
1308 addr = FRAME_ARGS_ADDRESS (frame);
1311 addr += SYMBOL_VALUE (var);
1317 addr = FRAME_ARGS_ADDRESS (frame);
1320 addr += SYMBOL_VALUE (var);
1321 addr = read_memory_unsigned_integer
1322 (addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
1329 addr = FRAME_LOCALS_ADDRESS (frame);
1330 addr += SYMBOL_VALUE (var);
1334 case LOC_BASEREG_ARG:
1336 char buf[MAX_REGISTER_RAW_SIZE];
1337 get_saved_register (buf, NULL, NULL, frame, SYMBOL_BASEREG (var),
1339 addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var)));
1340 addr += SYMBOL_VALUE (var);
1344 case LOC_THREAD_LOCAL_STATIC:
1346 char buf[MAX_REGISTER_RAW_SIZE];
1348 get_saved_register (buf, NULL, NULL, frame, SYMBOL_BASEREG (var),
1350 addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var)));
1351 addr += SYMBOL_VALUE (var);
1356 error ("Cannot look up value of a typedef");
1360 if (overlay_debugging)
1361 VALUE_ADDRESS (v) = symbol_overlayed_address
1362 (BLOCK_START (SYMBOL_BLOCK_VALUE (var)), SYMBOL_BFD_SECTION (var));
1364 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
1369 case LOC_REGPARM_ADDR:
1372 int regno = SYMBOL_VALUE (var);
1377 b = get_frame_block (frame);
1379 if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
1381 regval = value_from_register (lookup_pointer_type (type),
1386 error ("Value of register variable not available.");
1388 addr = value_as_pointer (regval);
1389 VALUE_LVAL (v) = lval_memory;
1393 regval = value_from_register (type, regno, frame);
1396 error ("Value of register variable not available.");
1402 case LOC_UNRESOLVED:
1404 struct minimal_symbol *msym;
1406 msym = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL);
1409 if (overlay_debugging)
1410 addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym),
1411 SYMBOL_BFD_SECTION (msym));
1413 addr = SYMBOL_VALUE_ADDRESS (msym);
1417 case LOC_OPTIMIZED_OUT:
1418 VALUE_LVAL (v) = not_lval;
1419 VALUE_OPTIMIZED_OUT (v) = 1;
1423 error ("Cannot look up value of a botched symbol.");
1427 VALUE_ADDRESS (v) = addr;
1432 /* Return a value of type TYPE, stored in register REGNUM, in frame
1435 NOTE: returns NULL if register value is not available.
1436 Caller will check return value or die! */
1439 value_from_register (type, regnum, frame)
1442 struct frame_info *frame;
1444 char raw_buffer[MAX_REGISTER_RAW_SIZE];
1447 value_ptr v = allocate_value (type);
1448 char *value_bytes = 0;
1449 int value_bytes_copied = 0;
1450 int num_storage_locs;
1451 enum lval_type lval;
1454 CHECK_TYPEDEF (type);
1455 len = TYPE_LENGTH (type);
1457 /* Pointers on D10V are really only 16 bits, but we lie to gdb elsewhere... */
1458 if (GDB_TARGET_IS_D10V && TYPE_CODE (type) == TYPE_CODE_PTR)
1461 VALUE_REGNO (v) = regnum;
1463 num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
1464 ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
1467 if (num_storage_locs > 1
1468 #ifdef GDB_TARGET_IS_H8500
1469 || TYPE_CODE (type) == TYPE_CODE_PTR
1473 /* Value spread across multiple storage locations. */
1476 int mem_stor = 0, reg_stor = 0;
1477 int mem_tracking = 1;
1478 CORE_ADDR last_addr = 0;
1479 CORE_ADDR first_addr = 0;
1481 value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
1483 /* Copy all of the data out, whereever it may be. */
1485 #ifdef GDB_TARGET_IS_H8500
1486 /* This piece of hideosity is required because the H8500 treats registers
1487 differently depending upon whether they are used as pointers or not. As a
1488 pointer, a register needs to have a page register tacked onto the front.
1489 An alternate way to do this would be to have gcc output different register
1490 numbers for the pointer & non-pointer form of the register. But, it
1491 doesn't, so we're stuck with this. */
1493 if (TYPE_CODE (type) == TYPE_CODE_PTR
1504 page_regnum = SEG_D_REGNUM;
1508 page_regnum = SEG_E_REGNUM;
1512 page_regnum = SEG_T_REGNUM;
1517 get_saved_register (value_bytes + 1,
1524 if (register_valid[page_regnum] == -1)
1525 return NULL; /* register value not available */
1527 if (lval == lval_register)
1534 get_saved_register (value_bytes + 2,
1541 if (register_valid[regnum] == -1)
1542 return NULL; /* register value not available */
1544 if (lval == lval_register)
1549 mem_tracking = mem_tracking && (addr == last_addr);
1554 #endif /* GDB_TARGET_IS_H8500 */
1555 for (local_regnum = regnum;
1556 value_bytes_copied < len;
1557 (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
1560 get_saved_register (value_bytes + value_bytes_copied,
1567 if (register_valid[local_regnum] == -1)
1568 return NULL; /* register value not available */
1570 if (regnum == local_regnum)
1572 if (lval == lval_register)
1580 && (regnum == local_regnum
1581 || addr == last_addr));
1586 if ((reg_stor && mem_stor)
1587 || (mem_stor && !mem_tracking))
1588 /* Mixed storage; all of the hassle we just went through was
1589 for some good purpose. */
1591 VALUE_LVAL (v) = lval_reg_frame_relative;
1592 VALUE_FRAME (v) = FRAME_FP (frame);
1593 VALUE_FRAME_REGNUM (v) = regnum;
1597 VALUE_LVAL (v) = lval_memory;
1598 VALUE_ADDRESS (v) = first_addr;
1602 VALUE_LVAL (v) = lval_register;
1603 VALUE_ADDRESS (v) = first_addr;
1606 internal_error ("value_from_register: Value not stored anywhere!");
1608 VALUE_OPTIMIZED_OUT (v) = optim;
1610 /* Any structure stored in more than one register will always be
1611 an integral number of registers. Otherwise, you'd need to do
1612 some fiddling with the last register copied here for little
1615 /* Copy into the contents section of the value. */
1616 memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
1618 /* Finally do any conversion necessary when extracting this
1619 type from more than one register. */
1620 #ifdef REGISTER_CONVERT_TO_TYPE
1621 REGISTER_CONVERT_TO_TYPE (regnum, type, VALUE_CONTENTS_RAW (v));
1626 /* Data is completely contained within a single register. Locate the
1627 register's contents in a real register or in core;
1628 read the data in raw format. */
1630 get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
1632 if (register_valid[regnum] == -1)
1633 return NULL; /* register value not available */
1635 VALUE_OPTIMIZED_OUT (v) = optim;
1636 VALUE_LVAL (v) = lval;
1637 VALUE_ADDRESS (v) = addr;
1639 /* Convert raw data to virtual format if necessary. */
1641 if (REGISTER_CONVERTIBLE (regnum))
1643 REGISTER_CONVERT_TO_VIRTUAL (regnum, type,
1644 raw_buffer, VALUE_CONTENTS_RAW (v));
1648 /* Raw and virtual formats are the same for this register. */
1650 if (TARGET_BYTE_ORDER == BIG_ENDIAN && len < REGISTER_RAW_SIZE (regnum))
1652 /* Big-endian, and we want less than full size. */
1653 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
1656 memcpy (VALUE_CONTENTS_RAW (v), raw_buffer + VALUE_OFFSET (v), len);
1659 if (GDB_TARGET_IS_D10V
1660 && TYPE_CODE (type) == TYPE_CODE_PTR
1661 && TYPE_TARGET_TYPE (type)
1662 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC))
1664 /* pointer to function */
1666 unsigned short snum;
1667 snum = (unsigned short) extract_unsigned_integer (VALUE_CONTENTS_RAW (v), 2);
1668 num = D10V_MAKE_IADDR (snum);
1669 store_address (VALUE_CONTENTS_RAW (v), 4, num);
1671 else if (GDB_TARGET_IS_D10V
1672 && TYPE_CODE (type) == TYPE_CODE_PTR)
1674 /* pointer to data */
1676 unsigned short snum;
1677 snum = (unsigned short) extract_unsigned_integer (VALUE_CONTENTS_RAW (v), 2);
1678 num = D10V_MAKE_DADDR (snum);
1679 store_address (VALUE_CONTENTS_RAW (v), 4, num);
1685 /* Given a struct symbol for a variable or function,
1686 and a stack frame id,
1687 return a (pointer to a) struct value containing the properly typed
1691 locate_var_value (var, frame)
1692 register struct symbol *var;
1693 struct frame_info *frame;
1696 struct type *type = SYMBOL_TYPE (var);
1697 value_ptr lazy_value;
1699 /* Evaluate it first; if the result is a memory address, we're fine.
1700 Lazy evaluation pays off here. */
1702 lazy_value = read_var_value (var, frame);
1703 if (lazy_value == 0)
1704 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
1706 if (VALUE_LAZY (lazy_value)
1707 || TYPE_CODE (type) == TYPE_CODE_FUNC)
1711 addr = VALUE_ADDRESS (lazy_value);
1712 val = value_from_longest (lookup_pointer_type (type), (LONGEST) addr);
1713 VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (lazy_value);
1717 /* Not a memory address; check what the problem was. */
1718 switch (VALUE_LVAL (lazy_value))
1721 case lval_reg_frame_relative:
1722 error ("Address requested for identifier \"%s\" which is in a register.",
1723 SYMBOL_SOURCE_NAME (var));
1727 error ("Can't take address of \"%s\" which isn't an lvalue.",
1728 SYMBOL_SOURCE_NAME (var));
1731 return 0; /* For lint -- never reached */
1735 static void build_findvar PARAMS ((void));
1739 /* We allocate some extra slop since we do a lot of memcpy's around
1740 `registers', and failing-soft is better than failing hard. */
1741 int sizeof_registers = REGISTER_BYTES + /* SLOP */ 256;
1742 int sizeof_register_valid = NUM_REGS * sizeof (*register_valid);
1743 registers = xmalloc (sizeof_registers);
1744 memset (registers, 0, sizeof_registers);
1745 register_valid = xmalloc (sizeof_register_valid);
1746 memset (register_valid, 0, sizeof_register_valid);
1749 void _initialize_findvar PARAMS ((void));
1751 _initialize_findvar ()
1755 register_gdbarch_swap (®isters, sizeof (registers), NULL);
1756 register_gdbarch_swap (®ister_valid, sizeof (register_valid), NULL);
1757 register_gdbarch_swap (NULL, 0, build_findvar);