1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB 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 1, or (at your option)
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
30 #if !defined (GET_SAVED_REGISTER)
32 /* Return the address in which frame FRAME's value of register REGNUM
33 has been saved in memory. Or return zero if it has not been saved.
34 If REGNUM specifies the SP, the value we return is actually
35 the SP value, not an address where it was saved. */
38 find_saved_register (frame
, regnum
)
42 struct frame_info
*fi
;
43 struct frame_saved_regs saved_regs
;
45 register FRAME frame1
= 0;
46 register CORE_ADDR addr
= 0;
48 if (frame
== 0) /* No regs saved if want current frame */
51 #ifdef HAVE_REGISTER_WINDOWS
52 /* We assume that a register in a register window will only be saved
53 in one place (since the name changes and/or disappears as you go
54 towards inner frames), so we only call get_frame_saved_regs on
55 the current frame. This is directly in contradiction to the
56 usage below, which assumes that registers used in a frame must be
57 saved in a lower (more interior) frame. This change is a result
58 of working on a register window machine; get_frame_saved_regs
59 always returns the registers saved within a frame, within the
60 context (register namespace) of that frame. */
62 /* However, note that we don't want this to return anything if
63 nothing is saved (if there's a frame inside of this one). Also,
64 callers to this routine asking for the stack pointer want the
65 stack pointer saved for *this* frame; this is returned from the
69 if (REGISTER_IN_WINDOW_P(regnum
))
71 frame1
= get_next_frame (frame
);
72 if (!frame1
) return 0; /* Registers of this frame are
75 /* Get the SP from the next frame in; it will be this
77 if (regnum
!= SP_REGNUM
)
80 fi
= get_frame_info (frame1
);
81 get_frame_saved_regs (fi
, &saved_regs
);
82 return saved_regs
.regs
[regnum
]; /* ... which might be zero */
84 #endif /* HAVE_REGISTER_WINDOWS */
86 /* Note that this next routine assumes that registers used in
87 frame x will be saved only in the frame that x calls and
88 frames interior to it. This is not true on the sparc, but the
89 above macro takes care of it, so we should be all right. */
93 frame1
= get_prev_frame (frame1
);
94 if (frame1
== 0 || frame1
== frame
)
96 fi
= get_frame_info (frame1
);
97 get_frame_saved_regs (fi
, &saved_regs
);
98 if (saved_regs
.regs
[regnum
])
99 addr
= saved_regs
.regs
[regnum
];
105 /* Find register number REGNUM relative to FRAME and put its
106 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
107 was optimized out (and thus can't be fetched). Set *LVAL to
108 lval_memory, lval_register, or not_lval, depending on whether the
109 value was fetched from memory, from a register, or in a strange
110 and non-modifiable way (e.g. a frame pointer which was calculated
111 rather than fetched). Set *ADDRP to the address, either in memory
112 on as a REGISTER_BYTE offset into the registers array.
114 Note that this implementation never sets *LVAL to not_lval. But
115 it can be replaced by defining GET_SAVED_REGISTER and supplying
118 The argument RAW_BUFFER must point to aligned memory. */
120 get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
126 enum lval_type
*lval
;
129 /* Normal systems don't optimize out things with register numbers. */
130 if (optimized
!= NULL
)
132 addr
= find_saved_register (frame
, regnum
);
137 if (regnum
== SP_REGNUM
)
139 if (raw_buffer
!= NULL
)
140 *(CORE_ADDR
*)raw_buffer
= addr
;
145 if (raw_buffer
!= NULL
)
146 read_memory (addr
, raw_buffer
, REGISTER_RAW_SIZE (regnum
));
151 *lval
= lval_register
;
152 addr
= REGISTER_BYTE (regnum
);
153 if (raw_buffer
!= NULL
)
154 read_register_gen (regnum
, raw_buffer
);
159 #endif /* GET_SAVED_REGISTER. */
161 /* Copy the bytes of register REGNUM, relative to the current stack frame,
162 into our memory at MYADDR, in target byte order.
163 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
165 Returns 1 if could not be read, 0 if could. */
168 read_relative_register_raw_bytes (regnum
, myaddr
)
173 if (regnum
== FP_REGNUM
&& selected_frame
)
175 bcopy (&FRAME_FP(selected_frame
), myaddr
, sizeof (CORE_ADDR
));
176 SWAP_TARGET_AND_HOST (myaddr
, sizeof (CORE_ADDR
)); /* in target order */
180 get_saved_register (myaddr
, &optim
, (CORE_ADDR
) NULL
, selected_frame
,
181 regnum
, (enum lval_type
*)NULL
);
185 /* Return a `value' with the contents of register REGNUM
186 in its virtual format, with the type specified by
187 REGISTER_VIRTUAL_TYPE. */
190 value_of_register (regnum
)
196 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
197 char virtual_buffer
[MAX_REGISTER_VIRTUAL_SIZE
];
200 get_saved_register (raw_buffer
, &optim
, &addr
,
201 selected_frame
, regnum
, &lval
);
203 target_convert_to_virtual (regnum
, raw_buffer
, virtual_buffer
);
204 val
= allocate_value (REGISTER_VIRTUAL_TYPE (regnum
));
205 bcopy (virtual_buffer
, VALUE_CONTENTS_RAW (val
),
206 REGISTER_VIRTUAL_SIZE (regnum
));
207 VALUE_LVAL (val
) = lval
;
208 VALUE_ADDRESS (val
) = addr
;
209 VALUE_REGNO (val
) = regnum
;
210 VALUE_OPTIMIZED_OUT (val
) = optim
;
214 /* Low level examining and depositing of registers.
216 The caller is responsible for making
217 sure that the inferior is stopped before calling the fetching routines,
218 or it will get garbage. (a change from GDB version 3, in which
219 the caller got the value from the last stop). */
221 /* Contents of the registers in target byte order.
222 We allocate some extra slop since we do a lot of bcopy's around `registers',
223 and failing-soft is better than failing hard. */
224 char registers
[REGISTER_BYTES
+ /* SLOP */ 256];
226 /* Nonzero if that register has been fetched. */
227 char register_valid
[NUM_REGS
];
229 /* Indicate that registers may have changed, so invalidate the cache. */
234 for (i
= 0; i
< NUM_REGS
; i
++)
235 register_valid
[i
] = 0;
238 /* Indicate that all registers have been fetched, so mark them all valid. */
243 for (i
= 0; i
< NUM_REGS
; i
++)
244 register_valid
[i
] = 1;
247 /* Copy LEN bytes of consecutive data from registers
248 starting with the REGBYTE'th byte of register data
249 into memory at MYADDR. */
252 read_register_bytes (regbyte
, myaddr
, len
)
257 /* Fetch all registers. */
259 for (i
= 0; i
< NUM_REGS
; i
++)
260 if (!register_valid
[i
])
262 target_fetch_registers (-1);
266 bcopy (®isters
[regbyte
], myaddr
, len
);
269 /* Read register REGNO into memory at MYADDR, which must be large enough
270 for REGISTER_RAW_BYTES (REGNO). If the register is known to be the
271 size of a CORE_ADDR or smaller, read_register can be used instead. */
273 read_register_gen (regno
, myaddr
)
277 if (!register_valid
[regno
])
278 target_fetch_registers (regno
);
279 bcopy (®isters
[REGISTER_BYTE (regno
)], myaddr
, REGISTER_RAW_SIZE (regno
));
282 /* Copy LEN bytes of consecutive data from memory at MYADDR
283 into registers starting with the REGBYTE'th byte of register data. */
286 write_register_bytes (regbyte
, myaddr
, len
)
291 /* Make sure the entire registers array is valid. */
292 read_register_bytes (0, (char *)NULL
, REGISTER_BYTES
);
293 bcopy (myaddr
, ®isters
[regbyte
], len
);
294 target_store_registers (-1);
297 /* Return the contents of register REGNO, regarding it as an integer. */
300 read_register (regno
)
304 if (!register_valid
[regno
])
305 target_fetch_registers (regno
);
306 /* FIXME, this loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
307 reg
= *(int *) ®isters
[REGISTER_BYTE (regno
)];
308 SWAP_TARGET_AND_HOST (®
, sizeof (int));
312 /* Registers we shouldn't try to store. */
313 #if !defined (CANNOT_STORE_REGISTER)
314 #define CANNOT_STORE_REGISTER(regno) 0
317 /* Store VALUE in the register number REGNO, regarded as an integer. */
320 write_register (regno
, val
)
323 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
324 the registers array if something writes to this register. */
325 if (CANNOT_STORE_REGISTER (regno
))
328 SWAP_TARGET_AND_HOST (&val
, sizeof (int));
330 target_prepare_to_store ();
332 register_valid
[regno
] = 1;
333 /* FIXME, this loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
334 /* FIXME, this depends on REGISTER_BYTE (regno) being aligned for host */
335 *(int *) ®isters
[REGISTER_BYTE (regno
)] = val
;
337 target_store_registers (regno
);
340 /* Record that register REGNO contains VAL.
341 This is used when the value is obtained from the inferior or core dump,
342 so there is no need to store the value there. */
345 supply_register (regno
, val
)
349 register_valid
[regno
] = 1;
350 bcopy (val
, ®isters
[REGISTER_BYTE (regno
)], REGISTER_RAW_SIZE (regno
));
353 /* Given a struct symbol for a variable,
354 and a stack frame id, read the value of the variable
355 and return a (pointer to a) struct value containing the value.
356 If the variable cannot be found, return a zero pointer. */
359 read_var_value (var
, frame
)
360 register struct symbol
*var
;
364 struct frame_info
*fi
;
365 struct type
*type
= SYMBOL_TYPE (var
);
370 v
= allocate_value (type
);
371 VALUE_LVAL (v
) = lval_memory
; /* The most likely possibility. */
372 len
= TYPE_LENGTH (type
);
374 if (frame
== 0) frame
= selected_frame
;
376 switch (SYMBOL_CLASS (var
))
379 val
= SYMBOL_VALUE (var
);
380 bcopy (&val
, VALUE_CONTENTS_RAW (v
), len
);
381 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (v
), len
);
382 VALUE_LVAL (v
) = not_lval
;
386 addr
= SYMBOL_VALUE_ADDRESS (var
);
387 bcopy (&addr
, VALUE_CONTENTS_RAW (v
), len
);
388 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (v
), len
);
389 VALUE_LVAL (v
) = not_lval
;
392 case LOC_CONST_BYTES
:
393 addr
= SYMBOL_VALUE_ADDRESS (var
);
394 bcopy (addr
, VALUE_CONTENTS_RAW (v
), len
);
395 VALUE_LVAL (v
) = not_lval
;
400 addr
= SYMBOL_VALUE_ADDRESS (var
);
403 /* Nonzero if a struct which is located in a register or a LOC_ARG
405 the address of the struct, not the struct itself. GCC_P is nonzero
406 if the function was compiled with GCC. */
407 #if !defined (REG_STRUCT_HAS_ADDR)
408 #define REG_STRUCT_HAS_ADDR(gcc_p) 0
412 fi
= get_frame_info (frame
);
413 addr
= FRAME_ARGS_ADDRESS (fi
);
417 addr
+= SYMBOL_VALUE (var
);
421 fi
= get_frame_info (frame
);
422 addr
= FRAME_ARGS_ADDRESS (fi
);
426 addr
+= SYMBOL_VALUE (var
);
427 addr
= read_memory_integer (addr
, sizeof (CORE_ADDR
));
432 fi
= get_frame_info (frame
);
433 addr
= SYMBOL_VALUE (var
) + FRAME_LOCALS_ADDRESS (fi
);
437 error ("Cannot look up value of a typedef");
441 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (var
));
447 struct block
*b
= get_frame_block (frame
);
449 v
= value_from_register (type
, SYMBOL_VALUE (var
), frame
);
451 if (REG_STRUCT_HAS_ADDR(b
->gcc_compile_flag
)
452 && TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
453 addr
= *(CORE_ADDR
*)VALUE_CONTENTS (v
);
460 error ("Cannot look up value of a botched symbol.");
464 VALUE_ADDRESS (v
) = addr
;
469 /* Return a value of type TYPE, stored in register REGNUM, in frame
473 value_from_register (type
, regnum
, frame
)
478 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
479 char virtual_buffer
[MAX_REGISTER_VIRTUAL_SIZE
];
482 value v
= allocate_value (type
);
483 int len
= TYPE_LENGTH (type
);
484 char *value_bytes
= 0;
485 int value_bytes_copied
= 0;
486 int num_storage_locs
;
489 VALUE_REGNO (v
) = regnum
;
491 num_storage_locs
= (len
> REGISTER_VIRTUAL_SIZE (regnum
) ?
492 ((len
- 1) / REGISTER_RAW_SIZE (regnum
)) + 1 :
495 if (num_storage_locs
> 1)
497 /* Value spread across multiple storage locations. */
500 int mem_stor
= 0, reg_stor
= 0;
501 int mem_tracking
= 1;
502 CORE_ADDR last_addr
= 0;
503 CORE_ADDR first_addr
;
505 value_bytes
= (char *) alloca (len
+ MAX_REGISTER_RAW_SIZE
);
507 /* Copy all of the data out, whereever it may be. */
509 for (local_regnum
= regnum
;
510 value_bytes_copied
< len
;
511 (value_bytes_copied
+= REGISTER_RAW_SIZE (local_regnum
),
514 get_saved_register (value_bytes
+ value_bytes_copied
,
520 if (lval
== lval_register
)
526 if (regnum
== local_regnum
)
531 && (regnum
== local_regnum
532 || addr
== last_addr
));
537 if ((reg_stor
&& mem_stor
)
538 || (mem_stor
&& !mem_tracking
))
539 /* Mixed storage; all of the hassle we just went through was
540 for some good purpose. */
542 VALUE_LVAL (v
) = lval_reg_frame_relative
;
543 VALUE_FRAME (v
) = FRAME_FP (frame
);
544 VALUE_FRAME_REGNUM (v
) = regnum
;
548 VALUE_LVAL (v
) = lval_memory
;
549 VALUE_ADDRESS (v
) = first_addr
;
553 VALUE_LVAL (v
) = lval_register
;
554 VALUE_ADDRESS (v
) = first_addr
;
557 fatal ("value_from_register: Value not stored anywhere!");
559 VALUE_OPTIMIZED_OUT (v
) = optim
;
561 /* Any structure stored in more than one register will always be
562 an integral number of registers. Otherwise, you'd need to do
563 some fiddling with the last register copied here for little
566 /* Copy into the contents section of the value. */
567 bcopy (value_bytes
, VALUE_CONTENTS_RAW (v
), len
);
572 /* Data is completely contained within a single register. Locate the
573 register's contents in a real register or in core;
574 read the data in raw format. */
576 get_saved_register (raw_buffer
, &optim
, &addr
, frame
, regnum
, &lval
);
577 VALUE_OPTIMIZED_OUT (v
) = optim
;
578 VALUE_LVAL (v
) = lval
;
579 VALUE_ADDRESS (v
) = addr
;
581 /* Convert the raw contents to virtual contents.
582 (Just copy them if the formats are the same.) */
584 target_convert_to_virtual (regnum
, raw_buffer
, virtual_buffer
);
586 if (REGISTER_CONVERTIBLE (regnum
))
588 /* When the raw and virtual formats differ, the virtual format
589 corresponds to a specific data type. If we want that type,
590 copy the data into the value.
591 Otherwise, do a type-conversion. */
593 if (type
!= REGISTER_VIRTUAL_TYPE (regnum
))
595 /* eg a variable of type `float' in a 68881 register
596 with raw type `extended' and virtual type `double'.
597 Fetch it as a `double' and then convert to `float'. */
598 v
= allocate_value (REGISTER_VIRTUAL_TYPE (regnum
));
599 bcopy (virtual_buffer
, VALUE_CONTENTS_RAW (v
), len
);
600 v
= value_cast (type
, v
);
603 bcopy (virtual_buffer
, VALUE_CONTENTS_RAW (v
), len
);
607 /* Raw and virtual formats are the same for this register. */
609 #if TARGET_BYTE_ORDER == BIG_ENDIAN
610 if (len
< REGISTER_RAW_SIZE (regnum
))
612 /* Big-endian, and we want less than full size. */
613 VALUE_OFFSET (v
) = REGISTER_RAW_SIZE (regnum
) - len
;
617 bcopy (virtual_buffer
+ VALUE_OFFSET (v
),
618 VALUE_CONTENTS_RAW (v
), len
);
624 /* Given a struct symbol for a variable,
625 and a stack frame id,
626 return a (pointer to a) struct value containing the variable's address. */
629 locate_var_value (var
, frame
)
630 register struct symbol
*var
;
634 struct type
*type
= SYMBOL_TYPE (var
);
635 struct type
*result_type
;
638 /* Evaluate it first; if the result is a memory address, we're fine.
639 Lazy evaluation pays off here. */
641 lazy_value
= read_var_value (var
, frame
);
643 error ("Address of \"%s\" is unknown.", SYMBOL_NAME (var
));
645 if (VALUE_LAZY (lazy_value
))
647 addr
= VALUE_ADDRESS (lazy_value
);
649 /* C++: The "address" of a reference should yield the address
650 * of the object pointed to. So force an extra de-reference. */
652 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
654 char *buf
= alloca (TYPE_LENGTH (type
));
655 read_memory (addr
, buf
, TYPE_LENGTH (type
));
656 addr
= unpack_long (type
, buf
);
657 type
= TYPE_TARGET_TYPE (type
);
660 /* Address of an array is of the type of address of it's elements. */
662 lookup_pointer_type (TYPE_CODE (type
) == TYPE_CODE_ARRAY
?
663 TYPE_TARGET_TYPE (type
) : type
);
665 return value_cast (result_type
,
666 value_from_long (builtin_type_long
, (LONGEST
) addr
));
669 /* Not a memory address; check what the problem was. */
670 switch (VALUE_LVAL (lazy_value
))
673 case lval_reg_frame_relative
:
674 error ("Address requested for identifier \"%s\" which is in a register.",
679 error ("Can't take address of \"%s\" which isn't an lvalue.",
683 return 0; /* For lint -- never reached */