]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/findvar.c
gdb-2.5.1
[thirdparty/binutils-gdb.git] / gdb / findvar.c
CommitLineData
7b4ac7e1 1/* Find a variable's value in memory, for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
3
4GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5WARRANTY. No author or distributor accepts responsibility to anyone
6for the consequences of using it or for whether it serves any
7particular purpose or works at all, unless he says so in writing.
8Refer to the GDB General Public License for full details.
9
10Everyone is granted permission to copy, modify and redistribute GDB,
11but only under the conditions described in the GDB General Public
12License. A copy of this license is supposed to have been given to you
13along with GDB so you can know your rights and responsibilities. It
14should be in a file named COPYING. Among other things, the copyright
15notice and this notice must be preserved on all copies.
16
17In other words, go ahead and share GDB, but don't try to stop
18anyone else from sharing it farther. Help stamp out software hoarding!
19*/
20
21#include "defs.h"
22#include "initialize.h"
23#include "param.h"
24#include "symtab.h"
25#include "frame.h"
26#include "value.h"
27
28CORE_ADDR read_register ();
29
30START_FILE
31\f
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. */
36
37static CORE_ADDR
38find_saved_register (frame, regnum)
39 FRAME frame;
40 int regnum;
41{
42 struct frame_info fi;
43 struct frame_saved_regs saved_regs;
44
45 register FRAME frame1 = 0;
46 register CORE_ADDR addr = 0;
47
48 while (1)
49 {
50 QUIT;
51 fi = get_prev_frame_info (frame1);
52 if (fi.frame == 0 || fi.frame == frame)
53 break;
54 get_frame_saved_regs (&fi, &saved_regs);
55 if (saved_regs.regs[regnum])
56 addr = saved_regs.regs[regnum];
57 frame1 = fi.frame;
58 }
59
60 return addr;
61}
62
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). */
66
67void
68read_relative_register_raw_bytes (regnum, myaddr)
69 int regnum;
70 char *myaddr;
71{
72 register CORE_ADDR addr;
73
74 if (regnum == FP_REGNUM)
75 {
76 bcopy (&selected_frame, myaddr, sizeof (CORE_ADDR));
77 return;
78 }
79
80 addr = find_saved_register (selected_frame, regnum);
81
82 if (addr)
83 {
84 if (regnum == SP_REGNUM)
85 {
86 CORE_ADDR buffer = addr;
87 bcopy (&buffer, myaddr, sizeof (CORE_ADDR));
88 }
89 else
90 read_memory (addr, myaddr, REGISTER_RAW_SIZE (regnum));
91 return;
92 }
93 read_register_bytes (REGISTER_BYTE (regnum),
94 myaddr, REGISTER_RAW_SIZE (regnum));
95}
96
97/* Return a `value' with the contents of register REGNUM
98 in its virtual format, with the type specified by
99 REGISTER_VIRTUAL_TYPE. */
100
101value
102value_of_register (regnum)
103 int regnum;
104{
105 register CORE_ADDR addr = find_saved_register (selected_frame, regnum);
106 register value val;
107 char raw_buffer[MAX_REGISTER_RAW_SIZE];
108 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
109
110 if (addr)
111 {
112 if (regnum == SP_REGNUM)
113 return value_from_long (builtin_type_int, addr);
114 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
115 }
116 else
117 read_register_bytes (REGISTER_BYTE (regnum), raw_buffer,
118 REGISTER_RAW_SIZE (regnum));
119
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;
126 return val;
127}
128\f
129/* Low level examining and depositing of registers.
130
131 Note that you must call `fetch_registers' once
132 before examining or depositing any registers. */
133
134char registers[REGISTER_BYTES];
135
136/* Copy LEN bytes of consecutive data from registers
137 starting with the REGBYTE'th byte of register data
138 into memory at MYADDR. */
139
140read_register_bytes (regbyte, myaddr, len)
141 int regbyte;
142 char *myaddr;
143 int len;
144{
145 bcopy (&registers[regbyte], myaddr, len);
146}
147
148/* Copy LEN bytes of consecutive data from memory at MYADDR
149 into registers starting with the REGBYTE'th byte of register data. */
150
151write_register_bytes (regbyte, myaddr, len)
152 int regbyte;
153 char *myaddr;
154 int len;
155{
156 bcopy (myaddr, &registers[regbyte], len);
157 if (have_inferior_p ())
158 store_inferior_registers (-1);
159}
160
161/* Return the contents of register REGNO,
162 regarding it as an integer. */
163
164CORE_ADDR
165read_register (regno)
166 int regno;
167{
168 /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
169 return *(int *) &registers[REGISTER_BYTE (regno)];
170}
171
172/* Store VALUE in the register number REGNO, regarded as an integer. */
173
174void
175write_register (regno, val)
176 int regno, val;
177{
178 /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
179 *(int *) &registers[REGISTER_BYTE (regno)] = val;
180
181 if (have_inferior_p ())
182 store_inferior_registers (regno);
183}
184
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. */
188
189void
190supply_register (regno, val)
191 int regno;
192 char *val;
193{
194 bcopy (val, &registers[REGISTER_BYTE (regno)], REGISTER_RAW_SIZE (regno));
195}
196\f
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. */
200
201value
202read_var_value (var, frame)
203 register struct symbol *var;
204 FRAME frame;
205{
206 register value v;
207
208 struct frame_info fi;
209
210 struct type *type = SYMBOL_TYPE (var);
211 register CORE_ADDR addr = 0;
212 int val = SYMBOL_VALUE (var);
213 register int len;
214
215 if (SYMBOL_CLASS (var) == LOC_BLOCK)
632ea0cc 216 type = lookup_function_type (type, 0);
7b4ac7e1 217
218 v = allocate_value (type);
219 VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
220 len = TYPE_LENGTH (type);
221
222 if (frame == 0) frame = selected_frame;
223
224 switch (SYMBOL_CLASS (var))
225 {
226 case LOC_CONST:
227 case LOC_LABEL:
228 bcopy (&val, VALUE_CONTENTS (v), len);
229 VALUE_LVAL (v) = not_lval;
230 return v;
231
232 case LOC_CONST_BYTES:
233 bcopy (val, VALUE_CONTENTS (v), len);
234 VALUE_LVAL (v) = not_lval;
235 return v;
236
237 case LOC_STATIC:
238 addr = val;
239 break;
240
241 case LOC_ARG:
242 fi = get_frame_info (frame);
243 addr = val + FRAME_ARGS_ADDRESS (fi);
244 break;
245
246 case LOC_LOCAL:
247 fi = get_frame_info (frame);
248 addr = val + FRAME_LOCALS_ADDRESS (fi);
249 break;
250
251 case LOC_TYPEDEF:
252 error ("Cannot look up value of a typedef");
253
254 case LOC_BLOCK:
255 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
256 return v;
257
258 case LOC_REGISTER:
259 {
260 char raw_buffer[MAX_REGISTER_RAW_SIZE];
261 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
262
263 VALUE_REGNO (v) = val;
264
265 /* Locate the register's contents in a real register or in core;
266 read the data in raw format. */
267
268 addr = find_saved_register (frame, val);
269 if (addr == 0)
270 {
271 /* Value is really in a register. */
272
273 VALUE_LVAL (v) = lval_register;
274 VALUE_ADDRESS (v) = REGISTER_BYTE (val);
275
276 read_register_bytes (REGISTER_BYTE (val),
277 raw_buffer, REGISTER_RAW_SIZE (val));
278 }
279 else
280 {
281 /* Value was in a register that has been saved in memory. */
282
283 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (val));
284 VALUE_ADDRESS (v) = addr;
285 }
286
287 /* Convert the raw contents to virtual contents.
288 (Just copy them if the formats are the same.) */
289
290 REGISTER_CONVERT_TO_VIRTUAL (val, raw_buffer, virtual_buffer);
291
292 if (REGISTER_CONVERTIBLE (val))
293 {
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. */
298
299 if (type != REGISTER_VIRTUAL_TYPE (val))
300 {
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);
307 }
308 else
309 bcopy (virtual_buffer, VALUE_CONTENTS (v), len);
310 }
311 else
312 {
313 /* Raw and virtual formats are the same for this register. */
314
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. */
318 test.i = 1;
319 if (test.c != 1 && len < REGISTER_RAW_SIZE (val))
320 {
321 /* Big-endian, and we want less than full size. */
322 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (val) - len;
323 }
324
325 bcopy (virtual_buffer + VALUE_OFFSET (v),
326 VALUE_CONTENTS (v), len);
327 }
328
329 return v;
330 }
331 }
332
333 read_memory (addr, VALUE_CONTENTS (v), len);
334 VALUE_ADDRESS (v) = addr;
335 return v;
336}
337\f
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. */
341
342value
343locate_var_value (var, frame)
344 register struct symbol *var;
345 FRAME frame;
346{
347 register CORE_ADDR addr = 0;
348 int val = SYMBOL_VALUE (var);
349 struct frame_info fi;
350
351 if (frame == 0) frame = selected_frame;
352
353 switch (SYMBOL_CLASS (var))
354 {
355 case LOC_CONST:
356 case LOC_CONST_BYTES:
357 error ("Address requested for identifier \"%s\" which is a constant.",
358 SYMBOL_NAME (var));
359
360 case LOC_REGISTER:
361 addr = find_saved_register (frame, val);
362 if (addr != 0)
363 {
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. */
368 test.i = 1;
369 if (test.c != 1 && len < REGISTER_RAW_SIZE (val))
370 /* Big-endian, and we want less than full size. */
632ea0cc 371 addr += REGISTER_RAW_SIZE (val) - len;
7b4ac7e1 372 break;
373 }
374 error ("Address requested for identifier \"%s\" which is in a register.",
375 SYMBOL_NAME (var));
376
377 case LOC_STATIC:
378 case LOC_LABEL:
379 addr = val;
380 break;
381
382 case LOC_ARG:
383 fi = get_frame_info (frame);
384 addr = val + FRAME_ARGS_ADDRESS (fi);
385 break;
386
387 case LOC_LOCAL:
388 fi = get_frame_info (frame);
389 addr = val + FRAME_LOCALS_ADDRESS (fi);
390 break;
391
392 case LOC_TYPEDEF:
393 error ("Address requested for identifier \"%s\" which is a typedef.",
394 SYMBOL_NAME (var));
395
396 case LOC_BLOCK:
397 addr = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
398 break;
399 }
400
401 return value_cast (lookup_pointer_type (SYMBOL_TYPE (var)),
402 value_from_long (builtin_type_long, addr));
403}
404
405static
406initialize ()
407{}
408
409END_FILE