gdb-3.3
[binutils-gdb.git] / gdb / findvar.c
1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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)
9 any later version.
10
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.
15
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. */
19
20 #include "defs.h"
21 #include "param.h"
22 #include "symtab.h"
23 #include "frame.h"
24 #include "value.h"
25
26 CORE_ADDR read_register ();
27
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. */
32
33 CORE_ADDR
34 find_saved_register (frame, regnum)
35 FRAME frame;
36 int regnum;
37 {
38 struct frame_info *fi;
39 struct frame_saved_regs saved_regs;
40
41 register FRAME frame1 = 0;
42 register CORE_ADDR addr = 0;
43
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. */
54
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
59 next frame. */
60
61
62 if (REGISTER_IN_WINDOW_P(regnum))
63 {
64 frame1 = get_next_frame (frame);
65 if (!frame1) return 0; /* Registers of this frame are
66 active. */
67
68 /* Get the SP from the next frame in; it will be this
69 current frame. */
70 if (regnum != SP_REGNUM)
71 frame1 = frame;
72
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);
77 }
78 #endif /* HAVE_REGISTER_WINDOWS */
79
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. */
84 while (1)
85 {
86 QUIT;
87 frame1 = get_prev_frame (frame1);
88 if (frame1 == 0 || frame1 == frame)
89 break;
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];
94 }
95
96 return addr;
97 }
98
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). */
102
103 void
104 read_relative_register_raw_bytes (regnum, myaddr)
105 int regnum;
106 char *myaddr;
107 {
108 register CORE_ADDR addr;
109
110 if (regnum == FP_REGNUM)
111 {
112 bcopy (&FRAME_FP(selected_frame), myaddr, sizeof (CORE_ADDR));
113 return;
114 }
115
116 addr = find_saved_register (selected_frame, regnum);
117
118 if (addr)
119 {
120 if (regnum == SP_REGNUM)
121 {
122 CORE_ADDR buffer = addr;
123 bcopy (&buffer, myaddr, sizeof (CORE_ADDR));
124 }
125 else
126 read_memory (addr, myaddr, REGISTER_RAW_SIZE (regnum));
127 return;
128 }
129 read_register_bytes (REGISTER_BYTE (regnum),
130 myaddr, REGISTER_RAW_SIZE (regnum));
131 }
132
133 /* Return a `value' with the contents of register REGNUM
134 in its virtual format, with the type specified by
135 REGISTER_VIRTUAL_TYPE. */
136
137 value
138 value_of_register (regnum)
139 int regnum;
140 {
141 register CORE_ADDR addr;
142 register value val;
143 char raw_buffer[MAX_REGISTER_RAW_SIZE];
144 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
145
146 if (! (have_inferior_p () || have_core_file_p ()))
147 error ("Can't get value of register without inferior or core file");
148
149 addr = find_saved_register (selected_frame, regnum);
150 if (addr)
151 {
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));
155 }
156 else
157 read_register_bytes (REGISTER_BYTE (regnum), raw_buffer,
158 REGISTER_RAW_SIZE (regnum));
159
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;
166 return val;
167 }
168 \f
169 /* Low level examining and depositing of registers.
170
171 Note that you must call `fetch_registers' once
172 before examining or depositing any registers. */
173
174 char registers[REGISTER_BYTES];
175
176 /* Copy LEN bytes of consecutive data from registers
177 starting with the REGBYTE'th byte of register data
178 into memory at MYADDR. */
179
180 void
181 read_register_bytes (regbyte, myaddr, len)
182 int regbyte;
183 char *myaddr;
184 int len;
185 {
186 bcopy (&registers[regbyte], myaddr, len);
187 }
188
189 /* Copy LEN bytes of consecutive data from memory at MYADDR
190 into registers starting with the REGBYTE'th byte of register data. */
191
192 void
193 write_register_bytes (regbyte, myaddr, len)
194 int regbyte;
195 char *myaddr;
196 int len;
197 {
198 bcopy (myaddr, &registers[regbyte], len);
199 if (have_inferior_p ())
200 store_inferior_registers (-1);
201 }
202
203 /* Return the contents of register REGNO,
204 regarding it as an integer. */
205
206 CORE_ADDR
207 read_register (regno)
208 int regno;
209 {
210 /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
211 return *(int *) &registers[REGISTER_BYTE (regno)];
212 }
213
214 /* Store VALUE in the register number REGNO, regarded as an integer. */
215
216 void
217 write_register (regno, val)
218 int regno, val;
219 {
220 /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
221 #if defined(sun4)
222 /* This is a no-op on a Sun 4. */
223 if (regno == 0)
224 return;
225 #endif
226
227 *(int *) &registers[REGISTER_BYTE (regno)] = val;
228
229 if (have_inferior_p ())
230 store_inferior_registers (regno);
231 }
232
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. */
236
237 void
238 supply_register (regno, val)
239 int regno;
240 char *val;
241 {
242 bcopy (val, &registers[REGISTER_BYTE (regno)], REGISTER_RAW_SIZE (regno));
243 }
244 \f
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. */
248
249 value
250 read_var_value (var, frame)
251 register struct symbol *var;
252 FRAME frame;
253 {
254 register value v;
255
256 struct frame_info *fi;
257
258 struct type *type = SYMBOL_TYPE (var);
259 register CORE_ADDR addr = 0;
260 int val = SYMBOL_VALUE (var);
261 register int len;
262
263 v = allocate_value (type);
264 VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
265 len = TYPE_LENGTH (type);
266
267 if (frame == 0) frame = selected_frame;
268
269 switch (SYMBOL_CLASS (var))
270 {
271 case LOC_CONST:
272 case LOC_LABEL:
273 bcopy (&val, VALUE_CONTENTS (v), len);
274 VALUE_LVAL (v) = not_lval;
275 return v;
276
277 case LOC_CONST_BYTES:
278 bcopy (val, VALUE_CONTENTS (v), len);
279 VALUE_LVAL (v) = not_lval;
280 return v;
281
282 case LOC_STATIC:
283 addr = val;
284 break;
285
286 case LOC_ARG:
287 fi = get_frame_info (frame);
288 addr = val + FRAME_ARGS_ADDRESS (fi);
289 break;
290
291 case LOC_REF_ARG:
292 fi = get_frame_info (frame);
293 addr = val + FRAME_ARGS_ADDRESS (fi);
294 addr = read_memory_integer (addr, sizeof (CORE_ADDR));
295 break;
296
297 case LOC_LOCAL:
298 fi = get_frame_info (frame);
299 addr = val + FRAME_LOCALS_ADDRESS (fi);
300 break;
301
302 case LOC_TYPEDEF:
303 error ("Cannot look up value of a typedef");
304
305 case LOC_BLOCK:
306 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
307 return v;
308
309 case LOC_REGISTER:
310 case LOC_REGPARM:
311 v = value_from_register (type, val, frame);
312 return v;
313 }
314
315 read_memory (addr, VALUE_CONTENTS (v), len);
316 VALUE_ADDRESS (v) = addr;
317 return v;
318 }
319
320 /* Return a value of type TYPE, stored in register REGNUM, in frame
321 FRAME. */
322
323 value
324 value_from_register (type, regnum, frame)
325 struct type *type;
326 int regnum;
327 FRAME frame;
328 {
329 char raw_buffer [MAX_REGISTER_RAW_SIZE];
330 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
331 CORE_ADDR addr;
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;
337
338 VALUE_REGNO (v) = regnum;
339
340 num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
341 ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
342 1);
343
344 if (num_storage_locs > 1)
345 {
346 /* Value spread across multiple storage locations. */
347
348 int local_regnum;
349 int mem_stor = 0, reg_stor = 0;
350 int mem_tracking = 1;
351 CORE_ADDR last_addr = 0;
352
353 value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
354
355 /* Copy all of the data out, whereever it may be. */
356
357 for (local_regnum = regnum;
358 value_bytes_copied < len;
359 (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
360 ++local_regnum))
361 {
362 int register_index = local_regnum - regnum;
363 addr = find_saved_register (frame, local_regnum);
364 if (addr == 0)
365 {
366 read_register_bytes (REGISTER_BYTE (local_regnum),
367 value_bytes + value_bytes_copied,
368 REGISTER_RAW_SIZE (local_regnum));
369 reg_stor++;
370 }
371 else
372 {
373 read_memory (addr, value_bytes + value_bytes_copied,
374 REGISTER_RAW_SIZE (local_regnum));
375 mem_stor++;
376 mem_tracking =
377 (mem_tracking
378 && (regnum == local_regnum
379 || addr == last_addr));
380 }
381 last_addr = addr;
382 }
383
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. */
388 {
389 VALUE_LVAL (v) = lval_reg_frame_relative;
390 VALUE_FRAME (v) = FRAME_FP (frame);
391 VALUE_FRAME_REGNUM (v) = regnum;
392 }
393 else if (mem_stor)
394 {
395 VALUE_LVAL (v) = lval_memory;
396 VALUE_ADDRESS (v) = find_saved_register (frame, regnum);
397 }
398 else if (reg_stor)
399 {
400 VALUE_LVAL (v) = lval_register;
401 VALUE_ADDRESS (v) = REGISTER_BYTE (regnum);
402 }
403 else
404 fatal ("value_from_register: Value not stored anywhere!");
405
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
409 endian machines. */
410
411 /* Copy into the contents section of the value. */
412 bcopy (value_bytes, VALUE_CONTENTS (v), len);
413
414 return v;
415 }
416
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. */
420
421 addr = find_saved_register (frame, regnum);
422 if (addr == 0)
423 {
424 /* Value is really in a register. */
425
426 VALUE_LVAL (v) = lval_register;
427 VALUE_ADDRESS (v) = REGISTER_BYTE (regnum);
428
429 read_register_bytes (REGISTER_BYTE (regnum),
430 raw_buffer, REGISTER_RAW_SIZE (regnum));
431 }
432 else
433 {
434 /* Value was in a register that has been saved in memory. */
435
436 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
437 VALUE_LVAL (v) = lval_memory;
438 VALUE_ADDRESS (v) = addr;
439 }
440
441 /* Convert the raw contents to virtual contents.
442 (Just copy them if the formats are the same.) */
443
444 REGISTER_CONVERT_TO_VIRTUAL (regnum, raw_buffer, virtual_buffer);
445
446 if (REGISTER_CONVERTIBLE (regnum))
447 {
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. */
452
453 if (type != REGISTER_VIRTUAL_TYPE (regnum))
454 {
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);
461 }
462 else
463 bcopy (virtual_buffer, VALUE_CONTENTS (v), len);
464 }
465 else
466 {
467 /* Raw and virtual formats are the same for this register. */
468
469 #ifdef BYTES_BIG_ENDIAN
470 if (len < REGISTER_RAW_SIZE (regnum))
471 {
472 /* Big-endian, and we want less than full size. */
473 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
474 }
475 #endif
476
477 bcopy (virtual_buffer + VALUE_OFFSET (v),
478 VALUE_CONTENTS (v), len);
479 }
480
481 return v;
482 }
483 \f
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. */
487
488 value
489 locate_var_value (var, frame)
490 register struct symbol *var;
491 FRAME frame;
492 {
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;
498
499 if (frame == 0) frame = selected_frame;
500
501 switch (SYMBOL_CLASS (var))
502 {
503 case LOC_CONST:
504 case LOC_CONST_BYTES:
505 error ("Address requested for identifier \"%s\" which is a constant.",
506 SYMBOL_NAME (var));
507
508 case LOC_REGISTER:
509 case LOC_REGPARM:
510 addr = find_saved_register (frame, val);
511 if (addr != 0)
512 {
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;
518 #endif
519 break;
520 }
521 error ("Address requested for identifier \"%s\" which is in a register.",
522 SYMBOL_NAME (var));
523
524 case LOC_STATIC:
525 case LOC_LABEL:
526 addr = val;
527 break;
528
529 case LOC_ARG:
530 fi = get_frame_info (frame);
531 addr = val + FRAME_ARGS_ADDRESS (fi);
532 break;
533
534 case LOC_REF_ARG:
535 fi = get_frame_info (frame);
536 addr = val + FRAME_ARGS_ADDRESS (fi);
537 addr = read_memory_integer (addr, sizeof (CORE_ADDR));
538 break;
539
540 case LOC_LOCAL:
541 fi = get_frame_info (frame);
542 addr = val + FRAME_LOCALS_ADDRESS (fi);
543 break;
544
545 case LOC_TYPEDEF:
546 error ("Address requested for identifier \"%s\" which is a typedef.",
547 SYMBOL_NAME (var));
548
549 case LOC_BLOCK:
550 addr = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
551 break;
552 }
553
554 /* Address of an array is of the type of address of it's elements. */
555 result_type =
556 lookup_pointer_type (TYPE_CODE (type) == TYPE_CODE_ARRAY ?
557 TYPE_TARGET_TYPE (type) : type);
558
559 return value_cast (result_type,
560 value_from_long (builtin_type_long, (LONGEST) addr));
561 }
562