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
;
287 fi
= get_frame_info (frame
);
288 addr
= val
+ FRAME_ARGS_ADDRESS (fi
);
292 fi
= get_frame_info (frame
);
293 addr
= val
+ FRAME_ARGS_ADDRESS (fi
);
294 addr
= read_memory_integer (addr
, sizeof (CORE_ADDR
));
298 fi
= get_frame_info (frame
);
299 addr
= val
+ FRAME_LOCALS_ADDRESS (fi
);
303 error ("Cannot look up value of a typedef");
306 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (var
));
311 v
= value_from_register (type
, val
, frame
);
315 read_memory (addr
, VALUE_CONTENTS (v
), len
);
316 VALUE_ADDRESS (v
) = addr
;
320 /* Return a value of type TYPE, stored in register REGNUM, in frame
324 value_from_register (type
, regnum
, frame
)
329 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
330 char virtual_buffer
[MAX_REGISTER_VIRTUAL_SIZE
];
332 value v
= allocate_value (type
);
333 int len
= TYPE_LENGTH (type
);
334 char *value_bytes
= 0;
335 int value_bytes_copied
= 0;
336 int num_storage_locs
;
338 VALUE_REGNO (v
) = regnum
;
340 num_storage_locs
= (len
> REGISTER_VIRTUAL_SIZE (regnum
) ?
341 ((len
- 1) / REGISTER_RAW_SIZE (regnum
)) + 1 :
344 if (num_storage_locs
> 1)
346 /* Value spread across multiple storage locations. */
349 int mem_stor
= 0, reg_stor
= 0;
350 int mem_tracking
= 1;
351 CORE_ADDR last_addr
= 0;
353 value_bytes
= (char *) alloca (len
+ MAX_REGISTER_RAW_SIZE
);
355 /* Copy all of the data out, whereever it may be. */
357 for (local_regnum
= regnum
;
358 value_bytes_copied
< len
;
359 (value_bytes_copied
+= REGISTER_RAW_SIZE (local_regnum
),
362 int register_index
= local_regnum
- regnum
;
363 addr
= find_saved_register (frame
, local_regnum
);
366 read_register_bytes (REGISTER_BYTE (local_regnum
),
367 value_bytes
+ value_bytes_copied
,
368 REGISTER_RAW_SIZE (local_regnum
));
373 read_memory (addr
, value_bytes
+ value_bytes_copied
,
374 REGISTER_RAW_SIZE (local_regnum
));
378 && (regnum
== local_regnum
379 || addr
== last_addr
));
384 if ((reg_stor
&& mem_stor
)
385 || (mem_stor
&& !mem_tracking
))
386 /* Mixed storage; all of the hassle we just went through was
387 for some good purpose. */
389 VALUE_LVAL (v
) = lval_reg_frame_relative
;
390 VALUE_FRAME (v
) = FRAME_FP (frame
);
391 VALUE_FRAME_REGNUM (v
) = regnum
;
395 VALUE_LVAL (v
) = lval_memory
;
396 VALUE_ADDRESS (v
) = find_saved_register (frame
, regnum
);
400 VALUE_LVAL (v
) = lval_register
;
401 VALUE_ADDRESS (v
) = REGISTER_BYTE (regnum
);
404 fatal ("value_from_register: Value not stored anywhere!");
406 /* Any structure stored in more than one register will always be
407 an inegral number of registers. Otherwise, you'd need to do
408 some fiddling with the last register copied here for little
411 /* Copy into the contents section of the value. */
412 bcopy (value_bytes
, VALUE_CONTENTS (v
), len
);
417 /* Data is completely contained within a single register. Locate the
418 register's contents in a real register or in core;
419 read the data in raw format. */
421 addr
= find_saved_register (frame
, regnum
);
424 /* Value is really in a register. */
426 VALUE_LVAL (v
) = lval_register
;
427 VALUE_ADDRESS (v
) = REGISTER_BYTE (regnum
);
429 read_register_bytes (REGISTER_BYTE (regnum
),
430 raw_buffer
, REGISTER_RAW_SIZE (regnum
));
434 /* Value was in a register that has been saved in memory. */
436 read_memory (addr
, raw_buffer
, REGISTER_RAW_SIZE (regnum
));
437 VALUE_LVAL (v
) = lval_memory
;
438 VALUE_ADDRESS (v
) = addr
;
441 /* Convert the raw contents to virtual contents.
442 (Just copy them if the formats are the same.) */
444 REGISTER_CONVERT_TO_VIRTUAL (regnum
, raw_buffer
, virtual_buffer
);
446 if (REGISTER_CONVERTIBLE (regnum
))
448 /* When the raw and virtual formats differ, the virtual format
449 corresponds to a specific data type. If we want that type,
450 copy the data into the value.
451 Otherwise, do a type-conversion. */
453 if (type
!= REGISTER_VIRTUAL_TYPE (regnum
))
455 /* eg a variable of type `float' in a 68881 register
456 with raw type `extended' and virtual type `double'.
457 Fetch it as a `double' and then convert to `float'. */
458 v
= allocate_value (REGISTER_VIRTUAL_TYPE (regnum
));
459 bcopy (virtual_buffer
, VALUE_CONTENTS (v
), len
);
460 v
= value_cast (type
, v
);
463 bcopy (virtual_buffer
, VALUE_CONTENTS (v
), len
);
467 /* Raw and virtual formats are the same for this register. */
469 #ifdef BYTES_BIG_ENDIAN
470 if (len
< REGISTER_RAW_SIZE (regnum
))
472 /* Big-endian, and we want less than full size. */
473 VALUE_OFFSET (v
) = REGISTER_RAW_SIZE (regnum
) - len
;
477 bcopy (virtual_buffer
+ VALUE_OFFSET (v
),
478 VALUE_CONTENTS (v
), len
);
484 /* Given a struct symbol for a variable,
485 and a stack frame id,
486 return a (pointer to a) struct value containing the variable's address. */
489 locate_var_value (var
, frame
)
490 register struct symbol
*var
;
493 register CORE_ADDR addr
= 0;
494 int val
= SYMBOL_VALUE (var
);
495 struct frame_info
*fi
;
496 struct type
*type
= SYMBOL_TYPE (var
);
497 struct type
*result_type
;
499 if (frame
== 0) frame
= selected_frame
;
501 switch (SYMBOL_CLASS (var
))
504 case LOC_CONST_BYTES
:
505 error ("Address requested for identifier \"%s\" which is a constant.",
510 addr
= find_saved_register (frame
, val
);
513 int len
= TYPE_LENGTH (type
);
514 #ifdef BYTES_BIG_ENDIAN
515 if (len
< REGISTER_RAW_SIZE (val
))
516 /* Big-endian, and we want less than full size. */
517 addr
+= REGISTER_RAW_SIZE (val
) - len
;
521 error ("Address requested for identifier \"%s\" which is in a register.",
530 fi
= get_frame_info (frame
);
531 addr
= val
+ FRAME_ARGS_ADDRESS (fi
);
535 fi
= get_frame_info (frame
);
536 addr
= val
+ FRAME_ARGS_ADDRESS (fi
);
537 addr
= read_memory_integer (addr
, sizeof (CORE_ADDR
));
541 fi
= get_frame_info (frame
);
542 addr
= val
+ FRAME_LOCALS_ADDRESS (fi
);
546 error ("Address requested for identifier \"%s\" which is a typedef.",
550 addr
= BLOCK_START (SYMBOL_BLOCK_VALUE (var
));
554 /* Address of an array is of the type of address of it's elements. */
556 lookup_pointer_type (TYPE_CODE (type
) == TYPE_CODE_ARRAY
?
557 TYPE_TARGET_TYPE (type
) : type
);
559 return value_cast (result_type
,
560 value_from_long (builtin_type_long
, (LONGEST
) addr
));