1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
22 #include "initialize.h"
28 CORE_ADDR read_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)
43 struct frame_saved_regs saved_regs;
45 register FRAME frame1 = 0;
46 register CORE_ADDR addr = 0;
51 fi = get_prev_frame_info (frame1);
52 if (fi.frame == 0 || fi.frame == frame)
54 get_frame_saved_regs (&fi, &saved_regs);
55 if (saved_regs.regs[regnum])
56 addr = saved_regs.regs[regnum];
63 /* Copy the bytes of register REGNUM, relative to the current stack frame,
64 into our memory at MYADDR.
65 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM). */
68 read_relative_register_raw_bytes (regnum, myaddr)
72 register CORE_ADDR addr;
74 if (regnum == FP_REGNUM)
76 bcopy (&selected_frame, myaddr, sizeof (CORE_ADDR));
80 addr = find_saved_register (selected_frame, regnum);
84 if (regnum == SP_REGNUM)
86 CORE_ADDR buffer = addr;
87 bcopy (&buffer, myaddr, sizeof (CORE_ADDR));
90 read_memory (addr, myaddr, REGISTER_RAW_SIZE (regnum));
93 read_register_bytes (REGISTER_BYTE (regnum),
94 myaddr, REGISTER_RAW_SIZE (regnum));
97 /* Return a `value' with the contents of register REGNUM
98 in its virtual format, with the type specified by
99 REGISTER_VIRTUAL_TYPE. */
102 value_of_register (regnum)
105 register CORE_ADDR addr = find_saved_register (selected_frame, regnum);
107 char raw_buffer[MAX_REGISTER_RAW_SIZE];
108 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
112 if (regnum == SP_REGNUM)
113 return value_from_long (builtin_type_int, addr);
114 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
117 read_register_bytes (REGISTER_BYTE (regnum), raw_buffer,
118 REGISTER_RAW_SIZE (regnum));
120 REGISTER_CONVERT_TO_VIRTUAL (regnum, raw_buffer, virtual_buffer);
121 val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
122 bcopy (virtual_buffer, VALUE_CONTENTS (val), REGISTER_VIRTUAL_SIZE (regnum));
123 VALUE_LVAL (val) = addr ? lval_memory : lval_register;
124 VALUE_ADDRESS (val) = addr ? addr : REGISTER_BYTE (regnum);
125 VALUE_REGNO (val) = regnum;
129 /* Low level examining and depositing of registers.
131 Note that you must call `fetch_registers' once
132 before examining or depositing any registers. */
134 char registers[REGISTER_BYTES];
136 /* Copy LEN bytes of consecutive data from registers
137 starting with the REGBYTE'th byte of register data
138 into memory at MYADDR. */
140 read_register_bytes (regbyte, myaddr, len)
145 bcopy (®isters[regbyte], myaddr, len);
148 /* Copy LEN bytes of consecutive data from memory at MYADDR
149 into registers starting with the REGBYTE'th byte of register data. */
151 write_register_bytes (regbyte, myaddr, len)
156 bcopy (myaddr, ®isters[regbyte], len);
157 if (have_inferior_p ())
158 store_inferior_registers (-1);
161 /* Return the contents of register REGNO,
162 regarding it as an integer. */
165 read_register (regno)
168 /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
169 return *(int *) ®isters[REGISTER_BYTE (regno)];
172 /* Store VALUE in the register number REGNO, regarded as an integer. */
175 write_register (regno, val)
178 /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
179 *(int *) ®isters[REGISTER_BYTE (regno)] = val;
181 if (have_inferior_p ())
182 store_inferior_registers (regno);
185 /* Record that register REGNO contains VAL.
186 This is used when the value is obtained from the inferior or core dump,
187 so there is no need to store the value there. */
190 supply_register (regno, val)
194 bcopy (val, ®isters[REGISTER_BYTE (regno)], REGISTER_RAW_SIZE (regno));
197 /* Given a struct symbol for a variable,
198 and a stack frame address, read the value of the variable
199 and return a (pointer to a) struct value containing the value. */
202 read_var_value (var, frame)
203 register struct symbol *var;
208 struct frame_info fi;
210 struct type *type = SYMBOL_TYPE (var);
211 register CORE_ADDR addr = 0;
212 int val = SYMBOL_VALUE (var);
215 if (SYMBOL_CLASS (var) == LOC_BLOCK)
216 type = lookup_function_type (type);
218 v = allocate_value (type);
219 VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
220 len = TYPE_LENGTH (type);
222 if (frame == 0) frame = selected_frame;
224 switch (SYMBOL_CLASS (var))
228 bcopy (&val, VALUE_CONTENTS (v), len);
229 VALUE_LVAL (v) = not_lval;
232 case LOC_CONST_BYTES:
233 bcopy (val, VALUE_CONTENTS (v), len);
234 VALUE_LVAL (v) = not_lval;
242 fi = get_frame_info (frame);
243 addr = val + FRAME_ARGS_ADDRESS (fi);
247 fi = get_frame_info (frame);
248 addr = val + FRAME_LOCALS_ADDRESS (fi);
252 error ("Cannot look up value of a typedef");
255 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
260 char raw_buffer[MAX_REGISTER_RAW_SIZE];
261 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
263 VALUE_REGNO (v) = val;
265 /* Locate the register's contents in a real register or in core;
266 read the data in raw format. */
268 addr = find_saved_register (frame, val);
271 /* Value is really in a register. */
273 VALUE_LVAL (v) = lval_register;
274 VALUE_ADDRESS (v) = REGISTER_BYTE (val);
276 read_register_bytes (REGISTER_BYTE (val),
277 raw_buffer, REGISTER_RAW_SIZE (val));
281 /* Value was in a register that has been saved in memory. */
283 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (val));
284 VALUE_ADDRESS (v) = addr;
287 /* Convert the raw contents to virtual contents.
288 (Just copy them if the formats are the same.) */
290 REGISTER_CONVERT_TO_VIRTUAL (val, raw_buffer, virtual_buffer);
292 if (REGISTER_CONVERTIBLE (val))
294 /* When the raw and virtual formats differ, the virtual format
295 corresponds to a specific data type. If we want that type,
296 copy the data into the value.
297 Otherwise, do a type-conversion. */
299 if (type != REGISTER_VIRTUAL_TYPE (val))
301 /* eg a variable of type `float' in a 68881 register
302 with raw type `extended' and virtual type `double'.
303 Fetch it as a `double' and then convert to `float'. */
304 v = allocate_value (REGISTER_VIRTUAL_TYPE (val));
305 bcopy (virtual_buffer, VALUE_CONTENTS (v), len);
306 v = value_cast (type, v);
309 bcopy (virtual_buffer, VALUE_CONTENTS (v), len);
313 /* Raw and virtual formats are the same for this register. */
315 union { int i; char c; } test;
316 /* If we want less than the full size, we need to
317 test for a big-endian or little-endian machine. */
319 if (test.c != 1 && len < REGISTER_RAW_SIZE (val))
321 /* Big-endian, and we want less than full size. */
322 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (val) - len;
325 bcopy (virtual_buffer + VALUE_OFFSET (v),
326 VALUE_CONTENTS (v), len);
333 read_memory (addr, VALUE_CONTENTS (v), len);
334 VALUE_ADDRESS (v) = addr;
338 /* Given a struct symbol for a variable,
339 and a stack frame address,
340 return a (pointer to a) struct value containing the variable's address. */
343 locate_var_value (var, frame)
344 register struct symbol *var;
347 register CORE_ADDR addr = 0;
348 int val = SYMBOL_VALUE (var);
349 struct frame_info fi;
351 if (frame == 0) frame = selected_frame;
353 switch (SYMBOL_CLASS (var))
356 case LOC_CONST_BYTES:
357 error ("Address requested for identifier \"%s\" which is a constant.",
361 addr = find_saved_register (frame, val);
364 union { int i; char c; } test;
365 int len = TYPE_LENGTH (SYMBOL_TYPE (var));
366 /* If var is less than the full size of register, we need to
367 test for a big-endian or little-endian machine. */
369 if (test.c != 1 && len < REGISTER_RAW_SIZE (val))
370 /* Big-endian, and we want less than full size. */
371 addr+ = REGISTER_RAW_SIZE (val) - len;
374 error ("Address requested for identifier \"%s\" which is in a register.",
383 fi = get_frame_info (frame);
384 addr = val + FRAME_ARGS_ADDRESS (fi);
388 fi = get_frame_info (frame);
389 addr = val + FRAME_LOCALS_ADDRESS (fi);
393 error ("Address requested for identifier \"%s\" which is a typedef.",
397 addr = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
401 return value_cast (lookup_pointer_type (SYMBOL_TYPE (var)),
402 value_from_long (builtin_type_long, addr));