1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 #if !defined (GET_SAVED_REGISTER)
31 /* Return the address in which frame FRAME's value of register REGNUM
32 has been saved in memory. Or return zero if it has not been saved.
33 If REGNUM specifies the SP, the value we return is actually
34 the SP value, not an address where it was saved. */
37 find_saved_register (frame, regnum)
41 struct frame_info *fi;
42 struct frame_saved_regs saved_regs;
44 register FRAME frame1 = 0;
45 register CORE_ADDR addr = 0;
47 if (frame == 0) /* No regs saved if want current frame */
50 #ifdef HAVE_REGISTER_WINDOWS
51 /* We assume that a register in a register window will only be saved
52 in one place (since the name changes and/or disappears as you go
53 towards inner frames), so we only call get_frame_saved_regs on
54 the current frame. This is directly in contradiction to the
55 usage below, which assumes that registers used in a frame must be
56 saved in a lower (more interior) frame. This change is a result
57 of working on a register window machine; get_frame_saved_regs
58 always returns the registers saved within a frame, within the
59 context (register namespace) of that frame. */
61 /* However, note that we don't want this to return anything if
62 nothing is saved (if there's a frame inside of this one). Also,
63 callers to this routine asking for the stack pointer want the
64 stack pointer saved for *this* frame; this is returned from the
68 if (REGISTER_IN_WINDOW_P(regnum))
70 frame1 = get_next_frame (frame);
71 if (!frame1) return 0; /* Registers of this frame are
74 /* Get the SP from the next frame in; it will be this
76 if (regnum != SP_REGNUM)
79 fi = get_frame_info (frame1);
80 get_frame_saved_regs (fi, &saved_regs);
81 return saved_regs.regs[regnum]; /* ... which might be zero */
83 #endif /* HAVE_REGISTER_WINDOWS */
85 /* Note that this next routine assumes that registers used in
86 frame x will be saved only in the frame that x calls and
87 frames interior to it. This is not true on the sparc, but the
88 above macro takes care of it, so we should be all right. */
92 frame1 = get_prev_frame (frame1);
93 if (frame1 == 0 || frame1 == frame)
95 fi = get_frame_info (frame1);
96 get_frame_saved_regs (fi, &saved_regs);
97 if (saved_regs.regs[regnum])
98 addr = saved_regs.regs[regnum];
104 /* Find register number REGNUM relative to FRAME and put its
105 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
106 was optimized out (and thus can't be fetched). Set *LVAL to
107 lval_memory, lval_register, or not_lval, depending on whether the
108 value was fetched from memory, from a register, or in a strange
109 and non-modifiable way (e.g. a frame pointer which was calculated
110 rather than fetched). Set *ADDRP to the address, either in memory
111 on as a REGISTER_BYTE offset into the registers array.
113 Note that this implementation never sets *LVAL to not_lval. But
114 it can be replaced by defining GET_SAVED_REGISTER and supplying
117 The argument RAW_BUFFER must point to aligned memory. */
119 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
125 enum lval_type *lval;
128 /* Normal systems don't optimize out things with register numbers. */
129 if (optimized != NULL)
131 addr = find_saved_register (frame, regnum);
136 if (regnum == SP_REGNUM)
138 if (raw_buffer != NULL)
139 *(CORE_ADDR *)raw_buffer = addr;
144 if (raw_buffer != NULL)
145 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
150 *lval = lval_register;
151 addr = REGISTER_BYTE (regnum);
152 if (raw_buffer != NULL)
153 read_register_gen (regnum, raw_buffer);
158 #endif /* GET_SAVED_REGISTER. */
160 /* Copy the bytes of register REGNUM, relative to the current stack frame,
161 into our memory at MYADDR, in target byte order.
162 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
164 Returns 1 if could not be read, 0 if could. */
167 read_relative_register_raw_bytes (regnum, myaddr)
172 if (regnum == FP_REGNUM && selected_frame)
174 memcpy (myaddr, &FRAME_FP(selected_frame), REGISTER_RAW_SIZE(FP_REGNUM));
175 SWAP_TARGET_AND_HOST (myaddr, REGISTER_RAW_SIZE(FP_REGNUM)); /* in target order */
179 get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, selected_frame,
180 regnum, (enum lval_type *)NULL);
184 /* Return a `value' with the contents of register REGNUM
185 in its virtual format, with the type specified by
186 REGISTER_VIRTUAL_TYPE. */
189 value_of_register (regnum)
195 char raw_buffer[MAX_REGISTER_RAW_SIZE];
196 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
199 get_saved_register (raw_buffer, &optim, &addr,
200 selected_frame, regnum, &lval);
202 REGISTER_CONVERT_TO_VIRTUAL (regnum, raw_buffer, virtual_buffer);
203 val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
204 memcpy (VALUE_CONTENTS_RAW (val), virtual_buffer,
205 REGISTER_VIRTUAL_SIZE (regnum));
206 VALUE_LVAL (val) = lval;
207 VALUE_ADDRESS (val) = addr;
208 VALUE_REGNO (val) = regnum;
209 VALUE_OPTIMIZED_OUT (val) = optim;
213 /* Low level examining and depositing of registers.
215 The caller is responsible for making
216 sure that the inferior is stopped before calling the fetching routines,
217 or it will get garbage. (a change from GDB version 3, in which
218 the caller got the value from the last stop). */
220 /* Contents of the registers in target byte order.
221 We allocate some extra slop since we do a lot of bcopy's around `registers',
222 and failing-soft is better than failing hard. */
223 char registers[REGISTER_BYTES + /* SLOP */ 256];
225 /* Nonzero if that register has been fetched. */
226 char register_valid[NUM_REGS];
228 /* Indicate that registers may have changed, so invalidate the cache. */
233 for (i = 0; i < NUM_REGS; i++)
234 register_valid[i] = 0;
237 /* Indicate that all registers have been fetched, so mark them all valid. */
242 for (i = 0; i < NUM_REGS; i++)
243 register_valid[i] = 1;
246 /* Copy LEN bytes of consecutive data from registers
247 starting with the REGBYTE'th byte of register data
248 into memory at MYADDR. */
251 read_register_bytes (regbyte, myaddr, len)
256 /* Fetch all registers. */
258 for (i = 0; i < NUM_REGS; i++)
259 if (!register_valid[i])
261 target_fetch_registers (-1);
265 memcpy (myaddr, ®isters[regbyte], len);
268 /* Read register REGNO into memory at MYADDR, which must be large enough
269 for REGISTER_RAW_BYTES (REGNO). Target byte-order.
270 If the register is known to be the size of a CORE_ADDR or smaller,
271 read_register can be used instead. */
273 read_register_gen (regno, myaddr)
277 if (!register_valid[regno])
278 target_fetch_registers (regno);
279 memcpy (myaddr, ®isters[REGISTER_BYTE (regno)],
280 REGISTER_RAW_SIZE (regno));
283 /* Copy LEN bytes of consecutive data from memory at MYADDR
284 into registers starting with the REGBYTE'th byte of register data. */
287 write_register_bytes (regbyte, myaddr, len)
292 /* Make sure the entire registers array is valid. */
293 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
294 memcpy (®isters[regbyte], myaddr, len);
295 target_store_registers (-1);
298 /* Return the contents of register REGNO, regarding it as an integer. */
299 /* FIXME, this loses when the REGISTER_VIRTUAL (REGNO) is true. Also,
300 why is the return type CORE_ADDR rather than some integer type? */
303 read_register (regno)
309 if (!register_valid[regno])
310 target_fetch_registers (regno);
312 switch (REGISTER_RAW_SIZE(regno))
314 case sizeof (unsigned char):
315 return registers[REGISTER_BYTE (regno)];
317 memcpy (&sval, ®isters[REGISTER_BYTE (regno)], sizeof (sval));
318 SWAP_TARGET_AND_HOST (&sval, sizeof (sval));
321 memcpy (&lval, ®isters[REGISTER_BYTE (regno)], sizeof (lval));
322 SWAP_TARGET_AND_HOST (&lval, sizeof (lval));
325 error ("GDB Internal Error in read_register() for register %d, size %d",
326 regno, RAW_REGISTER_SIZE(regno));
330 /* Registers we shouldn't try to store. */
331 #if !defined (CANNOT_STORE_REGISTER)
332 #define CANNOT_STORE_REGISTER(regno) 0
335 /* Store VALUE in the register number REGNO, regarded as an integer. */
336 /* FIXME, this loses when REGISTER_VIRTUAL (REGNO) is true. Also,
337 shouldn't the val arg be a LONGEST or something? */
340 write_register (regno, val)
346 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
347 the registers array if something writes to this register. */
348 if (CANNOT_STORE_REGISTER (regno))
351 target_prepare_to_store ();
353 register_valid [regno] = 1;
355 switch (REGISTER_RAW_SIZE(regno))
357 case sizeof (unsigned char):
358 registers[REGISTER_BYTE (regno)] = val;
362 SWAP_TARGET_AND_HOST (&sval, sizeof (sval));
363 memcpy (®isters[REGISTER_BYTE (regno)], &sval, sizeof (sval));
367 SWAP_TARGET_AND_HOST (&lval, sizeof (lval));
368 memcpy (®isters[REGISTER_BYTE (regno)], &lval, sizeof (lval));
371 error ("GDB Internal Error in write_register() for register %d, size %d",
372 regno, RAW_REGISTER_SIZE(regno));
375 target_store_registers (regno);
378 /* Record that register REGNO contains VAL.
379 This is used when the value is obtained from the inferior or core dump,
380 so there is no need to store the value there. */
383 supply_register (regno, val)
387 register_valid[regno] = 1;
388 memcpy (®isters[REGISTER_BYTE (regno)], val, REGISTER_RAW_SIZE (regno));
390 /* On some architectures, e.g. HPPA, there are a few stray bits in some
391 registers, that the rest of the code would like to ignore. */
392 #ifdef CLEAN_UP_REGISTER_VALUE
393 CLEAN_UP_REGISTER_VALUE(regno, ®isters[REGISTER_BYTE(regno)]);
397 /* Given a struct symbol for a variable,
398 and a stack frame id, read the value of the variable
399 and return a (pointer to a) struct value containing the value.
400 If the variable cannot be found, return a zero pointer.
401 If FRAME is NULL, use the selected_frame. */
404 read_var_value (var, frame)
405 register struct symbol *var;
409 struct frame_info *fi;
410 struct type *type = SYMBOL_TYPE (var);
414 v = allocate_value (type);
415 VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
416 len = TYPE_LENGTH (type);
418 if (frame == 0) frame = selected_frame;
420 switch (SYMBOL_CLASS (var))
423 memcpy (VALUE_CONTENTS_RAW (v), &SYMBOL_VALUE (var), len);
424 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (v), len);
425 VALUE_LVAL (v) = not_lval;
429 addr = SYMBOL_VALUE_ADDRESS (var);
430 memcpy (VALUE_CONTENTS_RAW (v), &addr, len);
431 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (v), len);
432 VALUE_LVAL (v) = not_lval;
435 case LOC_CONST_BYTES:
438 bytes_addr = SYMBOL_VALUE_BYTES (var);
439 memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
440 VALUE_LVAL (v) = not_lval;
445 addr = SYMBOL_VALUE_ADDRESS (var);
449 if (SYMBOL_BASEREG_VALID (var))
451 addr = FRAME_GET_BASEREG_VALUE (frame, SYMBOL_BASEREG (var));
455 fi = get_frame_info (frame);
458 addr = FRAME_ARGS_ADDRESS (fi);
464 addr += SYMBOL_VALUE (var);
468 if (SYMBOL_BASEREG_VALID (var))
470 addr = FRAME_GET_BASEREG_VALUE (frame, SYMBOL_BASEREG (var));
474 fi = get_frame_info (frame);
477 addr = FRAME_ARGS_ADDRESS (fi);
483 addr += SYMBOL_VALUE (var);
484 read_memory (addr, (char *) &addr, sizeof (CORE_ADDR));
489 if (SYMBOL_BASEREG_VALID (var))
491 addr = FRAME_GET_BASEREG_VALUE (frame, SYMBOL_BASEREG (var));
495 fi = get_frame_info (frame);
498 addr = FRAME_LOCALS_ADDRESS (fi);
500 addr += SYMBOL_VALUE (var);
504 error ("Cannot look up value of a typedef");
508 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
513 case LOC_REGPARM_ADDR:
519 b = get_frame_block (frame);
521 v = value_from_register (type, SYMBOL_VALUE (var), frame);
523 if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
525 addr = *(CORE_ADDR *)VALUE_CONTENTS (v);
526 VALUE_LVAL (v) = lval_memory;
533 case LOC_OPTIMIZED_OUT:
534 VALUE_LVAL (v) = not_lval;
535 VALUE_OPTIMIZED_OUT (v) = 1;
539 error ("Cannot look up value of a botched symbol.");
543 VALUE_ADDRESS (v) = addr;
548 /* Return a value of type TYPE, stored in register REGNUM, in frame
552 value_from_register (type, regnum, frame)
557 char raw_buffer [MAX_REGISTER_RAW_SIZE];
558 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
561 value v = allocate_value (type);
562 int len = TYPE_LENGTH (type);
563 char *value_bytes = 0;
564 int value_bytes_copied = 0;
565 int num_storage_locs;
568 VALUE_REGNO (v) = regnum;
570 num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
571 ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
574 if (num_storage_locs > 1
575 #ifdef GDB_TARGET_IS_H8500
576 || TYPE_CODE (type) == TYPE_CODE_PTR
580 /* Value spread across multiple storage locations. */
583 int mem_stor = 0, reg_stor = 0;
584 int mem_tracking = 1;
585 CORE_ADDR last_addr = 0;
586 CORE_ADDR first_addr;
588 value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
590 /* Copy all of the data out, whereever it may be. */
592 #ifdef GDB_TARGET_IS_H8500
593 /* This piece of hideosity is required because the H8500 treats registers
594 differently depending upon whether they are used as pointers or not. As a
595 pointer, a register needs to have a page register tacked onto the front.
596 An alternate way to do this would be to have gcc output different register
597 numbers for the pointer & non-pointer form of the register. But, it
598 doesn't, so we're stuck with this. */
600 if (TYPE_CODE (type) == TYPE_CODE_PTR
607 case R0_REGNUM: case R1_REGNUM: case R2_REGNUM: case R3_REGNUM:
608 page_regnum = SEG_D_REGNUM;
610 case R4_REGNUM: case R5_REGNUM:
611 page_regnum = SEG_E_REGNUM;
613 case R6_REGNUM: case R7_REGNUM:
614 page_regnum = SEG_T_REGNUM;
619 get_saved_register (value_bytes + 1,
626 if (lval == lval_register)
635 get_saved_register (value_bytes + 2,
642 if (lval == lval_register)
647 mem_tracking = mem_tracking && (addr == last_addr);
652 #endif /* GDB_TARGET_IS_H8500 */
653 for (local_regnum = regnum;
654 value_bytes_copied < len;
655 (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
658 get_saved_register (value_bytes + value_bytes_copied,
664 if (lval == lval_register)
670 if (regnum == local_regnum)
675 && (regnum == local_regnum
676 || addr == last_addr));
681 if ((reg_stor && mem_stor)
682 || (mem_stor && !mem_tracking))
683 /* Mixed storage; all of the hassle we just went through was
684 for some good purpose. */
686 VALUE_LVAL (v) = lval_reg_frame_relative;
687 VALUE_FRAME (v) = FRAME_FP (frame);
688 VALUE_FRAME_REGNUM (v) = regnum;
692 VALUE_LVAL (v) = lval_memory;
693 VALUE_ADDRESS (v) = first_addr;
697 VALUE_LVAL (v) = lval_register;
698 VALUE_ADDRESS (v) = first_addr;
701 fatal ("value_from_register: Value not stored anywhere!");
703 VALUE_OPTIMIZED_OUT (v) = optim;
705 /* Any structure stored in more than one register will always be
706 an integral number of registers. Otherwise, you'd need to do
707 some fiddling with the last register copied here for little
710 /* Copy into the contents section of the value. */
711 memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
716 /* Data is completely contained within a single register. Locate the
717 register's contents in a real register or in core;
718 read the data in raw format. */
720 get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
721 VALUE_OPTIMIZED_OUT (v) = optim;
722 VALUE_LVAL (v) = lval;
723 VALUE_ADDRESS (v) = addr;
725 /* Convert the raw contents to virtual contents.
726 (Just copy them if the formats are the same.) */
728 REGISTER_CONVERT_TO_VIRTUAL (regnum, raw_buffer, virtual_buffer);
730 if (REGISTER_CONVERTIBLE (regnum))
732 /* When the raw and virtual formats differ, the virtual format
733 corresponds to a specific data type. If we want that type,
734 copy the data into the value.
735 Otherwise, do a type-conversion. */
737 if (type != REGISTER_VIRTUAL_TYPE (regnum))
739 /* eg a variable of type `float' in a 68881 register
740 with raw type `extended' and virtual type `double'.
741 Fetch it as a `double' and then convert to `float'. */
742 v = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
743 memcpy (VALUE_CONTENTS_RAW (v), virtual_buffer, len);
744 v = value_cast (type, v);
747 memcpy (VALUE_CONTENTS_RAW (v), virtual_buffer, len);
751 /* Raw and virtual formats are the same for this register. */
753 #if TARGET_BYTE_ORDER == BIG_ENDIAN
754 if (len < REGISTER_RAW_SIZE (regnum))
756 /* Big-endian, and we want less than full size. */
757 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
761 memcpy (VALUE_CONTENTS_RAW (v), virtual_buffer + VALUE_OFFSET (v), len);
767 /* Given a struct symbol for a variable or function,
768 and a stack frame id,
769 return a (pointer to a) struct value containing the properly typed
773 locate_var_value (var, frame)
774 register struct symbol *var;
778 struct type *type = SYMBOL_TYPE (var);
781 /* Evaluate it first; if the result is a memory address, we're fine.
782 Lazy evaluation pays off here. */
784 lazy_value = read_var_value (var, frame);
786 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
788 if (VALUE_LAZY (lazy_value)
789 || TYPE_CODE (type) == TYPE_CODE_FUNC)
791 addr = VALUE_ADDRESS (lazy_value);
792 return value_from_longest (lookup_pointer_type (type), (LONGEST) addr);
795 /* Not a memory address; check what the problem was. */
796 switch (VALUE_LVAL (lazy_value))
799 case lval_reg_frame_relative:
800 error ("Address requested for identifier \"%s\" which is in a register.",
801 SYMBOL_SOURCE_NAME (var));
805 error ("Can't take address of \"%s\" which isn't an lvalue.",
806 SYMBOL_SOURCE_NAME (var));
809 return 0; /* For lint -- never reached */