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. */
26 CORE_ADDR read_register ();
28 /* Return the address in which frame FRAME's value of register REGNUM
29 has been saved in memory. Or return zero if it has not been saved.
30 If REGNUM specifies the SP, the value we return is actually
31 the SP value, not an address where it was saved. */
34 find_saved_register (frame, regnum)
38 struct frame_info *fi;
39 struct frame_saved_regs saved_regs;
41 register FRAME frame1 = 0;
42 register CORE_ADDR addr = 0;
44 #ifdef HAVE_REGISTER_WINDOWS
45 /* We assume that a register in a register window will only be saved
46 in one place (since the name changes and disappears as you go
47 towards inner frames), so we only call get_frame_saved_regs on
48 the current frame. This is directly in contradiction to the
49 usage below, which assumes that registers used in a frame must be
50 saved in a lower (more interior) frame. This change is a result
51 of working on a register window machine; get_frame_saved_regs
52 always returns the registers saved within a frame, within the
53 context (register namespace) of that frame. */
55 /* However, note that we don't want this to return anything if
56 nothing is saved (if there's a frame inside of this one). Also,
57 callers to this routine asking for the stack pointer want the
58 stack pointer saved for *this* frame; this is returned from the
62 if (REGISTER_IN_WINDOW_P(regnum))
64 frame1 = get_next_frame (frame);
65 if (!frame1) return 0; /* Registers of this frame are
68 /* Get the SP from the next frame in; it will be this
70 if (regnum != SP_REGNUM)
73 fi = get_frame_info (frame1);
74 get_frame_saved_regs (fi, &saved_regs);
75 return (saved_regs.regs[regnum] ?
76 saved_regs.regs[regnum] : 0);
78 #endif /* HAVE_REGISTER_WINDOWS */
80 /* Note that this next routine assumes that registers used in
81 frame x will be saved only in the frame that x calls and
82 frames interior to it. This is not true on the sparc, but the
83 above macro takes care of it, so we should be all right. */
87 frame1 = get_prev_frame (frame1);
88 if (frame1 == 0 || frame1 == frame)
90 fi = get_frame_info (frame1);
91 get_frame_saved_regs (fi, &saved_regs);
92 if (saved_regs.regs[regnum])
93 addr = saved_regs.regs[regnum];
99 /* Copy the bytes of register REGNUM, relative to the current stack frame,
100 into our memory at MYADDR.
101 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM). */
104 read_relative_register_raw_bytes (regnum, myaddr)
108 register CORE_ADDR addr;
110 if (regnum == FP_REGNUM)
112 bcopy (&FRAME_FP(selected_frame), myaddr, sizeof (CORE_ADDR));
116 addr = find_saved_register (selected_frame, regnum);
120 if (regnum == SP_REGNUM)
122 CORE_ADDR buffer = addr;
123 bcopy (&buffer, myaddr, sizeof (CORE_ADDR));
126 read_memory (addr, myaddr, REGISTER_RAW_SIZE (regnum));
129 read_register_bytes (REGISTER_BYTE (regnum),
130 myaddr, REGISTER_RAW_SIZE (regnum));
133 /* Return a `value' with the contents of register REGNUM
134 in its virtual format, with the type specified by
135 REGISTER_VIRTUAL_TYPE. */
138 value_of_register (regnum)
141 register CORE_ADDR addr;
143 char raw_buffer[MAX_REGISTER_RAW_SIZE];
144 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
146 if (! (have_inferior_p () || have_core_file_p ()))
147 error ("Can't get value of register without inferior or core file");
149 addr = find_saved_register (selected_frame, regnum);
152 if (regnum == SP_REGNUM)
153 return value_from_long (builtin_type_int, (LONGEST) addr);
154 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
157 read_register_bytes (REGISTER_BYTE (regnum), raw_buffer,
158 REGISTER_RAW_SIZE (regnum));
160 REGISTER_CONVERT_TO_VIRTUAL (regnum, raw_buffer, virtual_buffer);
161 val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
162 bcopy (virtual_buffer, VALUE_CONTENTS (val), REGISTER_VIRTUAL_SIZE (regnum));
163 VALUE_LVAL (val) = addr ? lval_memory : lval_register;
164 VALUE_ADDRESS (val) = addr ? addr : REGISTER_BYTE (regnum);
165 VALUE_REGNO (val) = regnum;
169 /* Low level examining and depositing of registers.
171 Note that you must call `fetch_registers' once
172 before examining or depositing any registers. */
174 char registers[REGISTER_BYTES];
176 /* Copy LEN bytes of consecutive data from registers
177 starting with the REGBYTE'th byte of register data
178 into memory at MYADDR. */
181 read_register_bytes (regbyte, myaddr, len)
186 bcopy (®isters[regbyte], myaddr, len);
189 /* Copy LEN bytes of consecutive data from memory at MYADDR
190 into registers starting with the REGBYTE'th byte of register data. */
193 write_register_bytes (regbyte, myaddr, len)
198 bcopy (myaddr, ®isters[regbyte], len);
199 if (have_inferior_p ())
200 store_inferior_registers (-1);
203 /* Return the contents of register REGNO,
204 regarding it as an integer. */
207 read_register (regno)
210 /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
211 return *(int *) ®isters[REGISTER_BYTE (regno)];
214 /* Store VALUE in the register number REGNO, regarded as an integer. */
217 write_register (regno, val)
220 /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
222 /* This is a no-op on a Sun 4. */
227 *(int *) ®isters[REGISTER_BYTE (regno)] = val;
229 if (have_inferior_p ())
230 store_inferior_registers (regno);
233 /* Record that register REGNO contains VAL.
234 This is used when the value is obtained from the inferior or core dump,
235 so there is no need to store the value there. */
238 supply_register (regno, val)
242 bcopy (val, ®isters[REGISTER_BYTE (regno)], REGISTER_RAW_SIZE (regno));
245 /* Given a struct symbol for a variable,
246 and a stack frame id, read the value of the variable
247 and return a (pointer to a) struct value containing the value. */
250 read_var_value (var, frame)
251 register struct symbol *var;
256 struct frame_info *fi;
258 struct type *type = SYMBOL_TYPE (var);
259 register CORE_ADDR addr = 0;
260 int val = SYMBOL_VALUE (var);
263 v = allocate_value (type);
264 VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
265 len = TYPE_LENGTH (type);
267 if (frame == 0) frame = selected_frame;
269 switch (SYMBOL_CLASS (var))
273 bcopy (&val, VALUE_CONTENTS (v), len);
274 VALUE_LVAL (v) = not_lval;
277 case LOC_CONST_BYTES:
278 bcopy (val, VALUE_CONTENTS (v), len);
279 VALUE_LVAL (v) = not_lval;
286 /* Nonzero if a struct which is located in a register or a LOC_ARG
288 the address of the struct, not the struct itself. GCC_P is nonzero
289 if the function was compiled with GCC. */
290 #if !defined (REG_STRUCT_HAS_ADDR)
291 #define REG_STRUCT_HAS_ADDR(gcc_p) 0
295 fi = get_frame_info (frame);
296 addr = val + FRAME_ARGS_ADDRESS (fi);
300 fi = get_frame_info (frame);
301 addr = val + FRAME_ARGS_ADDRESS (fi);
302 addr = read_memory_integer (addr, sizeof (CORE_ADDR));
306 fi = get_frame_info (frame);
307 addr = val + FRAME_LOCALS_ADDRESS (fi);
311 error ("Cannot look up value of a typedef");
314 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
320 struct block *b = get_frame_block (frame);
322 v = value_from_register (type, val, frame);
324 if (REG_STRUCT_HAS_ADDR(b->gcc_compile_flag)
325 && TYPE_CODE (type) == TYPE_CODE_STRUCT)
326 addr = *(CORE_ADDR *)VALUE_CONTENTS (v);
332 read_memory (addr, VALUE_CONTENTS (v), len);
333 VALUE_ADDRESS (v) = addr;
337 /* Return a value of type TYPE, stored in register REGNUM, in frame
341 value_from_register (type, regnum, frame)
346 char raw_buffer [MAX_REGISTER_RAW_SIZE];
347 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
349 value v = allocate_value (type);
350 int len = TYPE_LENGTH (type);
351 char *value_bytes = 0;
352 int value_bytes_copied = 0;
353 int num_storage_locs;
355 VALUE_REGNO (v) = regnum;
357 num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
358 ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
361 if (num_storage_locs > 1)
363 /* Value spread across multiple storage locations. */
366 int mem_stor = 0, reg_stor = 0;
367 int mem_tracking = 1;
368 CORE_ADDR last_addr = 0;
370 value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
372 /* Copy all of the data out, whereever it may be. */
374 for (local_regnum = regnum;
375 value_bytes_copied < len;
376 (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
379 int register_index = local_regnum - regnum;
380 addr = find_saved_register (frame, local_regnum);
383 read_register_bytes (REGISTER_BYTE (local_regnum),
384 value_bytes + value_bytes_copied,
385 REGISTER_RAW_SIZE (local_regnum));
390 read_memory (addr, value_bytes + value_bytes_copied,
391 REGISTER_RAW_SIZE (local_regnum));
395 && (regnum == local_regnum
396 || addr == last_addr));
401 if ((reg_stor && mem_stor)
402 || (mem_stor && !mem_tracking))
403 /* Mixed storage; all of the hassle we just went through was
404 for some good purpose. */
406 VALUE_LVAL (v) = lval_reg_frame_relative;
407 VALUE_FRAME (v) = FRAME_FP (frame);
408 VALUE_FRAME_REGNUM (v) = regnum;
412 VALUE_LVAL (v) = lval_memory;
413 VALUE_ADDRESS (v) = find_saved_register (frame, regnum);
417 VALUE_LVAL (v) = lval_register;
418 VALUE_ADDRESS (v) = REGISTER_BYTE (regnum);
421 fatal ("value_from_register: Value not stored anywhere!");
423 /* Any structure stored in more than one register will always be
424 an inegral number of registers. Otherwise, you'd need to do
425 some fiddling with the last register copied here for little
428 /* Copy into the contents section of the value. */
429 bcopy (value_bytes, VALUE_CONTENTS (v), len);
434 /* Data is completely contained within a single register. Locate the
435 register's contents in a real register or in core;
436 read the data in raw format. */
438 addr = find_saved_register (frame, regnum);
441 /* Value is really in a register. */
443 VALUE_LVAL (v) = lval_register;
444 VALUE_ADDRESS (v) = REGISTER_BYTE (regnum);
446 read_register_bytes (REGISTER_BYTE (regnum),
447 raw_buffer, REGISTER_RAW_SIZE (regnum));
451 /* Value was in a register that has been saved in memory. */
453 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
454 VALUE_LVAL (v) = lval_memory;
455 VALUE_ADDRESS (v) = addr;
458 /* Convert the raw contents to virtual contents.
459 (Just copy them if the formats are the same.) */
461 REGISTER_CONVERT_TO_VIRTUAL (regnum, raw_buffer, virtual_buffer);
463 if (REGISTER_CONVERTIBLE (regnum))
465 /* When the raw and virtual formats differ, the virtual format
466 corresponds to a specific data type. If we want that type,
467 copy the data into the value.
468 Otherwise, do a type-conversion. */
470 if (type != REGISTER_VIRTUAL_TYPE (regnum))
472 /* eg a variable of type `float' in a 68881 register
473 with raw type `extended' and virtual type `double'.
474 Fetch it as a `double' and then convert to `float'. */
475 v = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
476 bcopy (virtual_buffer, VALUE_CONTENTS (v), len);
477 v = value_cast (type, v);
480 bcopy (virtual_buffer, VALUE_CONTENTS (v), len);
484 /* Raw and virtual formats are the same for this register. */
486 #ifdef BYTES_BIG_ENDIAN
487 if (len < REGISTER_RAW_SIZE (regnum))
489 /* Big-endian, and we want less than full size. */
490 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
494 bcopy (virtual_buffer + VALUE_OFFSET (v),
495 VALUE_CONTENTS (v), len);
501 /* Given a struct symbol for a variable,
502 and a stack frame id,
503 return a (pointer to a) struct value containing the variable's address. */
506 locate_var_value (var, frame)
507 register struct symbol *var;
510 register CORE_ADDR addr = 0;
511 int val = SYMBOL_VALUE (var);
512 struct frame_info *fi;
513 struct type *type = SYMBOL_TYPE (var);
514 struct type *result_type;
516 if (frame == 0) frame = selected_frame;
518 switch (SYMBOL_CLASS (var))
521 case LOC_CONST_BYTES:
522 error ("Address requested for identifier \"%s\" which is a constant.",
527 addr = find_saved_register (frame, val);
530 int len = TYPE_LENGTH (type);
531 #ifdef BYTES_BIG_ENDIAN
532 if (len < REGISTER_RAW_SIZE (val))
533 /* Big-endian, and we want less than full size. */
534 addr += REGISTER_RAW_SIZE (val) - len;
538 error ("Address requested for identifier \"%s\" which is in a register.",
547 fi = get_frame_info (frame);
548 addr = val + FRAME_ARGS_ADDRESS (fi);
552 fi = get_frame_info (frame);
553 addr = val + FRAME_ARGS_ADDRESS (fi);
554 addr = read_memory_integer (addr, sizeof (CORE_ADDR));
558 fi = get_frame_info (frame);
559 addr = val + FRAME_LOCALS_ADDRESS (fi);
563 error ("Address requested for identifier \"%s\" which is a typedef.",
567 addr = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
571 /* Address of an array is of the type of address of it's elements. */
573 lookup_pointer_type (TYPE_CODE (type) == TYPE_CODE_ARRAY ?
574 TYPE_TARGET_TYPE (type) : type);
576 return value_cast (result_type,
577 value_from_long (builtin_type_long, (LONGEST) addr));