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 v
= allocate_value (type
);
216 VALUE_LVAL (v
) = lval_memory
; /* The most likely possibility. */
217 len
= TYPE_LENGTH (type
);
219 if (frame
== 0) frame
= selected_frame
;
221 switch (SYMBOL_CLASS (var
))
225 bcopy (&val
, VALUE_CONTENTS (v
), len
);
226 VALUE_LVAL (v
) = not_lval
;
229 case LOC_CONST_BYTES
:
230 bcopy (val
, VALUE_CONTENTS (v
), len
);
231 VALUE_LVAL (v
) = not_lval
;
239 fi
= get_frame_info (frame
);
240 addr
= val
+ FRAME_ARGS_ADDRESS (fi
);
244 fi
= get_frame_info (frame
);
245 addr
= val
+ FRAME_LOCALS_ADDRESS (fi
);
249 error ("Cannot look up value of a typedef");
252 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (var
));
257 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
258 char virtual_buffer
[MAX_REGISTER_VIRTUAL_SIZE
];
260 VALUE_REGNO (v
) = val
;
262 /* Locate the register's contents in a real register or in core;
263 read the data in raw format. */
265 addr
= find_saved_register (frame
, val
);
268 /* Value is really in a register. */
270 VALUE_LVAL (v
) = lval_register
;
271 VALUE_ADDRESS (v
) = REGISTER_BYTE (val
);
273 read_register_bytes (REGISTER_BYTE (val
),
274 raw_buffer
, REGISTER_RAW_SIZE (val
));
278 /* Value was in a register that has been saved in memory. */
280 read_memory (addr
, raw_buffer
, REGISTER_RAW_SIZE (val
));
281 VALUE_ADDRESS (v
) = addr
;
284 /* Convert the raw contents to virtual contents.
285 (Just copy them if the formats are the same.) */
287 REGISTER_CONVERT_TO_VIRTUAL (val
, raw_buffer
, virtual_buffer
);
289 if (REGISTER_CONVERTIBLE (val
))
291 /* When the raw and virtual formats differ, the virtual format
292 corresponds to a specific data type. If we want that type,
293 copy the data into the value.
294 Otherwise, do a type-conversion. */
296 if (type
!= REGISTER_VIRTUAL_TYPE (val
))
298 /* eg a variable of type `float' in a 68881 register
299 with raw type `extended' and virtual type `double'.
300 Fetch it as a `double' and then convert to `float'. */
301 v
= allocate_value (REGISTER_VIRTUAL_TYPE (val
));
302 bcopy (virtual_buffer
, VALUE_CONTENTS (v
), len
);
303 v
= value_cast (type
, v
);
306 bcopy (virtual_buffer
, VALUE_CONTENTS (v
), len
);
310 /* Raw and virtual formats are the same for this register. */
312 union { int i
; char c
; } test
;
313 /* If we want less than the full size, we need to
314 test for a big-endian or little-endian machine. */
316 if (test
.c
!= 1 && len
< REGISTER_RAW_SIZE (val
))
318 /* Big-endian, and we want less than full size. */
319 VALUE_OFFSET (v
) = REGISTER_RAW_SIZE (val
) - len
;
322 bcopy (virtual_buffer
+ VALUE_OFFSET (v
),
323 VALUE_CONTENTS (v
), len
);
330 read_memory (addr
, VALUE_CONTENTS (v
), len
);
331 VALUE_ADDRESS (v
) = addr
;
335 /* Given a struct symbol for a variable,
336 and a stack frame address,
337 return a (pointer to a) struct value containing the variable's address. */
340 locate_var_value (var
, frame
)
341 register struct symbol
*var
;
344 register CORE_ADDR addr
= 0;
345 int val
= SYMBOL_VALUE (var
);
346 struct frame_info fi
;
347 struct type
*type
= SYMBOL_TYPE (var
);
349 if (frame
== 0) frame
= selected_frame
;
351 switch (SYMBOL_CLASS (var
))
354 case LOC_CONST_BYTES
:
355 error ("Address requested for identifier \"%s\" which is a constant.",
359 addr
= find_saved_register (frame
, val
);
362 union { int i
; char c
; } test
;
363 int len
= TYPE_LENGTH (type
);
364 /* If var is less than the full size of register, we need to
365 test for a big-endian or little-endian machine. */
367 if (test
.c
!= 1 && len
< REGISTER_RAW_SIZE (val
))
368 /* Big-endian, and we want less than full size. */
369 addr
+= REGISTER_RAW_SIZE (val
) - len
;
372 error ("Address requested for identifier \"%s\" which is in a register.",
381 fi
= get_frame_info (frame
);
382 addr
= val
+ FRAME_ARGS_ADDRESS (fi
);
386 fi
= get_frame_info (frame
);
387 addr
= val
+ FRAME_LOCALS_ADDRESS (fi
);
391 error ("Address requested for identifier \"%s\" which is a typedef.",
395 addr
= BLOCK_START (SYMBOL_BLOCK_VALUE (var
));
399 return value_cast (lookup_pointer_type (type
),
400 value_from_long (builtin_type_long
, addr
));