The list of changes is too long to fit in the cvs log (since it truncates!).
[deliverable/binutils-gdb.git] / gdb / findvar.c
CommitLineData
bd5635a1
RP
1/* Find a variable's value in memory, for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6GDB is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GDB is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GDB; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include <stdio.h>
21#include "defs.h"
22#include "param.h"
23#include "symtab.h"
24#include "frame.h"
25#include "value.h"
26#include "gdbcore.h"
27#include "inferior.h"
28#include "target.h"
29
30#if !defined (GET_SAVED_REGISTER)
31
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
37CORE_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 if (frame == 0) /* No regs saved if want current frame */
49 return 0;
50
51#ifdef HAVE_REGISTER_WINDOWS
52 /* We assume that a register in a register window will only be saved
53 in one place (since the name changes and/or disappears as you go
54 towards inner frames), so we only call get_frame_saved_regs on
55 the current frame. This is directly in contradiction to the
56 usage below, which assumes that registers used in a frame must be
57 saved in a lower (more interior) frame. This change is a result
58 of working on a register window machine; get_frame_saved_regs
59 always returns the registers saved within a frame, within the
60 context (register namespace) of that frame. */
61
62 /* However, note that we don't want this to return anything if
63 nothing is saved (if there's a frame inside of this one). Also,
64 callers to this routine asking for the stack pointer want the
65 stack pointer saved for *this* frame; this is returned from the
66 next frame. */
67
68
69 if (REGISTER_IN_WINDOW_P(regnum))
70 {
71 frame1 = get_next_frame (frame);
72 if (!frame1) return 0; /* Registers of this frame are
73 active. */
74
75 /* Get the SP from the next frame in; it will be this
76 current frame. */
77 if (regnum != SP_REGNUM)
78 frame1 = frame;
79
80 fi = get_frame_info (frame1);
81 get_frame_saved_regs (fi, &saved_regs);
82 return saved_regs.regs[regnum]; /* ... which might be zero */
83 }
84#endif /* HAVE_REGISTER_WINDOWS */
85
86 /* Note that this next routine assumes that registers used in
87 frame x will be saved only in the frame that x calls and
88 frames interior to it. This is not true on the sparc, but the
89 above macro takes care of it, so we should be all right. */
90 while (1)
91 {
92 QUIT;
93 frame1 = get_prev_frame (frame1);
94 if (frame1 == 0 || frame1 == frame)
95 break;
96 fi = get_frame_info (frame1);
97 get_frame_saved_regs (fi, &saved_regs);
98 if (saved_regs.regs[regnum])
99 addr = saved_regs.regs[regnum];
100 }
101
102 return addr;
103}
104
105/* Find register number REGNUM relative to FRAME and put its
106 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
107 was optimized out (and thus can't be fetched). Set *LVAL to
108 lval_memory, lval_register, or not_lval, depending on whether the
109 value was fetched from memory, from a register, or in a strange
110 and non-modifiable way (e.g. a frame pointer which was calculated
111 rather than fetched). Set *ADDRP to the address, either in memory
112 on as a REGISTER_BYTE offset into the registers array.
113
114 Note that this implementation never sets *LVAL to not_lval. But
115 it can be replaced by defining GET_SAVED_REGISTER and supplying
116 your own.
117
118 The argument RAW_BUFFER must point to aligned memory. */
119void
120get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
121 char *raw_buffer;
122 int *optimized;
123 CORE_ADDR *addrp;
124 FRAME frame;
125 int regnum;
126 enum lval_type *lval;
127{
128 CORE_ADDR addr;
129 /* Normal systems don't optimize out things with register numbers. */
130 if (optimized != NULL)
131 *optimized = 0;
132 addr = find_saved_register (frame, regnum);
133 if (addr != NULL)
134 {
135 if (lval != NULL)
136 *lval = lval_memory;
137 if (regnum == SP_REGNUM)
138 {
139 if (raw_buffer != NULL)
140 *(CORE_ADDR *)raw_buffer = addr;
141 if (addrp != NULL)
142 *addrp = 0;
143 return;
144 }
145 if (raw_buffer != NULL)
146 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
147 }
148 else
149 {
150 if (lval != NULL)
151 *lval = lval_register;
152 addr = REGISTER_BYTE (regnum);
153 if (raw_buffer != NULL)
154 read_register_gen (regnum, raw_buffer);
155 }
156 if (addrp != NULL)
157 *addrp = addr;
158}
159#endif /* GET_SAVED_REGISTER. */
160
161/* Copy the bytes of register REGNUM, relative to the current stack frame,
162 into our memory at MYADDR, in target byte order.
163 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
164
165 Returns 1 if could not be read, 0 if could. */
166
167int
168read_relative_register_raw_bytes (regnum, myaddr)
169 int regnum;
170 char *myaddr;
171{
172 int optim;
173 if (regnum == FP_REGNUM && selected_frame)
174 {
175 bcopy (&FRAME_FP(selected_frame), myaddr, sizeof (CORE_ADDR));
176 SWAP_TARGET_AND_HOST (myaddr, sizeof (CORE_ADDR)); /* in target order */
177 return 0;
178 }
179
e1ce8aa5 180 get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, selected_frame,
bd5635a1
RP
181 regnum, (enum lval_type *)NULL);
182 return optim;
183}
184
185/* Return a `value' with the contents of register REGNUM
186 in its virtual format, with the type specified by
187 REGISTER_VIRTUAL_TYPE. */
188
189value
190value_of_register (regnum)
191 int regnum;
192{
193 CORE_ADDR addr;
194 int optim;
195 register value val;
196 char raw_buffer[MAX_REGISTER_RAW_SIZE];
197 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
198 enum lval_type lval;
199
200 get_saved_register (raw_buffer, &optim, &addr,
201 selected_frame, regnum, &lval);
202
203 target_convert_to_virtual (regnum, raw_buffer, virtual_buffer);
204 val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
205 bcopy (virtual_buffer, VALUE_CONTENTS_RAW (val),
206 REGISTER_VIRTUAL_SIZE (regnum));
207 VALUE_LVAL (val) = lval;
208 VALUE_ADDRESS (val) = addr;
209 VALUE_REGNO (val) = regnum;
210 VALUE_OPTIMIZED_OUT (val) = optim;
211 return val;
212}
213\f
214/* Low level examining and depositing of registers.
215
216 The caller is responsible for making
217 sure that the inferior is stopped before calling the fetching routines,
218 or it will get garbage. (a change from GDB version 3, in which
219 the caller got the value from the last stop). */
220
221/* Contents of the registers in target byte order.
222 We allocate some extra slop since we do a lot of bcopy's around `registers',
223 and failing-soft is better than failing hard. */
224char registers[REGISTER_BYTES + /* SLOP */ 256];
225
226/* Nonzero if that register has been fetched. */
227char register_valid[NUM_REGS];
228
229/* Indicate that registers may have changed, so invalidate the cache. */
230void
231registers_changed ()
232{
233 int i;
234 for (i = 0; i < NUM_REGS; i++)
235 register_valid[i] = 0;
236}
237
238/* Indicate that all registers have been fetched, so mark them all valid. */
239void
240registers_fetched ()
241{
242 int i;
243 for (i = 0; i < NUM_REGS; i++)
244 register_valid[i] = 1;
245}
246
247/* Copy LEN bytes of consecutive data from registers
248 starting with the REGBYTE'th byte of register data
249 into memory at MYADDR. */
250
251void
252read_register_bytes (regbyte, myaddr, len)
253 int regbyte;
254 char *myaddr;
255 int len;
256{
257 /* Fetch all registers. */
258 int i;
259 for (i = 0; i < NUM_REGS; i++)
260 if (!register_valid[i])
261 {
262 target_fetch_registers (-1);
263 break;
264 }
265 if (myaddr != NULL)
266 bcopy (&registers[regbyte], myaddr, len);
267}
268
269/* Read register REGNO into memory at MYADDR, which must be large enough
f2ebc25f
JK
270 for REGISTER_RAW_BYTES (REGNO). Target byte-order.
271 If the register is known to be the size of a CORE_ADDR or smaller,
272 read_register can be used instead. */
bd5635a1
RP
273void
274read_register_gen (regno, myaddr)
275 int regno;
276 char *myaddr;
277{
278 if (!register_valid[regno])
279 target_fetch_registers (regno);
280 bcopy (&registers[REGISTER_BYTE (regno)], myaddr, REGISTER_RAW_SIZE (regno));
281}
282
283/* Copy LEN bytes of consecutive data from memory at MYADDR
284 into registers starting with the REGBYTE'th byte of register data. */
285
286void
287write_register_bytes (regbyte, myaddr, len)
288 int regbyte;
289 char *myaddr;
290 int len;
291{
292 /* Make sure the entire registers array is valid. */
293 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
294 bcopy (myaddr, &registers[regbyte], len);
295 target_store_registers (-1);
296}
297
298/* Return the contents of register REGNO, regarding it as an integer. */
299
300CORE_ADDR
301read_register (regno)
302 int regno;
303{
304 int reg;
305 if (!register_valid[regno])
306 target_fetch_registers (regno);
307 /* FIXME, this loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
308 reg = *(int *) &registers[REGISTER_BYTE (regno)];
309 SWAP_TARGET_AND_HOST (&reg, sizeof (int));
310 return reg;
311}
312
313/* Registers we shouldn't try to store. */
314#if !defined (CANNOT_STORE_REGISTER)
315#define CANNOT_STORE_REGISTER(regno) 0
316#endif
317
318/* Store VALUE in the register number REGNO, regarded as an integer. */
319
320void
321write_register (regno, val)
322 int regno, val;
323{
324 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
325 the registers array if something writes to this register. */
326 if (CANNOT_STORE_REGISTER (regno))
327 return;
328
329 SWAP_TARGET_AND_HOST (&val, sizeof (int));
330
331 target_prepare_to_store ();
332
333 register_valid [regno] = 1;
334 /* FIXME, this loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
335 /* FIXME, this depends on REGISTER_BYTE (regno) being aligned for host */
336 *(int *) &registers[REGISTER_BYTE (regno)] = val;
337
338 target_store_registers (regno);
339}
340
341/* Record that register REGNO contains VAL.
342 This is used when the value is obtained from the inferior or core dump,
343 so there is no need to store the value there. */
344
345void
346supply_register (regno, val)
347 int regno;
348 char *val;
349{
350 register_valid[regno] = 1;
351 bcopy (val, &registers[REGISTER_BYTE (regno)], REGISTER_RAW_SIZE (regno));
352}
353\f
354/* Given a struct symbol for a variable,
355 and a stack frame id, read the value of the variable
356 and return a (pointer to a) struct value containing the value.
777bef06
JK
357 If the variable cannot be found, return a zero pointer.
358 If FRAME is NULL, use the selected_frame. */
bd5635a1
RP
359
360value
361read_var_value (var, frame)
362 register struct symbol *var;
363 FRAME frame;
364{
365 register value v;
366 struct frame_info *fi;
367 struct type *type = SYMBOL_TYPE (var);
368 CORE_ADDR addr;
bd5635a1
RP
369 register int len;
370
371 v = allocate_value (type);
372 VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
373 len = TYPE_LENGTH (type);
374
375 if (frame == 0) frame = selected_frame;
376
377 switch (SYMBOL_CLASS (var))
378 {
379 case LOC_CONST:
e1ce8aa5 380 bcopy (&SYMBOL_VALUE (var), VALUE_CONTENTS_RAW (v), len);
bd5635a1
RP
381 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (v), len);
382 VALUE_LVAL (v) = not_lval;
383 return v;
384
385 case LOC_LABEL:
386 addr = SYMBOL_VALUE_ADDRESS (var);
387 bcopy (&addr, VALUE_CONTENTS_RAW (v), len);
388 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (v), len);
389 VALUE_LVAL (v) = not_lval;
390 return v;
391
392 case LOC_CONST_BYTES:
393 addr = SYMBOL_VALUE_ADDRESS (var);
394 bcopy (addr, VALUE_CONTENTS_RAW (v), len);
395 VALUE_LVAL (v) = not_lval;
396 return v;
397
398 case LOC_STATIC:
399 case LOC_EXTERNAL:
400 addr = SYMBOL_VALUE_ADDRESS (var);
401 break;
402
403/* Nonzero if a struct which is located in a register or a LOC_ARG
404 really contains
405 the address of the struct, not the struct itself. GCC_P is nonzero
406 if the function was compiled with GCC. */
407#if !defined (REG_STRUCT_HAS_ADDR)
408#define REG_STRUCT_HAS_ADDR(gcc_p) 0
409#endif
410
411 case LOC_ARG:
412 fi = get_frame_info (frame);
777bef06
JK
413 if (fi == NULL)
414 return 0;
bd5635a1
RP
415 addr = FRAME_ARGS_ADDRESS (fi);
416 if (!addr) {
417 return 0;
418 }
419 addr += SYMBOL_VALUE (var);
420 break;
421
422 case LOC_REF_ARG:
423 fi = get_frame_info (frame);
777bef06
JK
424 if (fi == NULL)
425 return 0;
bd5635a1
RP
426 addr = FRAME_ARGS_ADDRESS (fi);
427 if (!addr) {
428 return 0;
429 }
430 addr += SYMBOL_VALUE (var);
e1ce8aa5 431 read_memory (addr, &addr, sizeof (CORE_ADDR));
bd5635a1
RP
432 break;
433
434 case LOC_LOCAL:
435 case LOC_LOCAL_ARG:
436 fi = get_frame_info (frame);
777bef06
JK
437 if (fi == NULL)
438 return 0;
bd5635a1
RP
439 addr = SYMBOL_VALUE (var) + FRAME_LOCALS_ADDRESS (fi);
440 break;
441
442 case LOC_TYPEDEF:
443 error ("Cannot look up value of a typedef");
444 break;
445
446 case LOC_BLOCK:
447 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
448 return v;
449
450 case LOC_REGISTER:
451 case LOC_REGPARM:
452 {
777bef06 453 struct block *b;
bd5635a1 454
777bef06
JK
455 if (frame == NULL)
456 return 0;
457 b = get_frame_block (frame);
458
bd5635a1
RP
459 v = value_from_register (type, SYMBOL_VALUE (var), frame);
460
e1ce8aa5 461 if (REG_STRUCT_HAS_ADDR (BLOCK_GCC_COMPILED (b))
bd5635a1
RP
462 && TYPE_CODE (type) == TYPE_CODE_STRUCT)
463 addr = *(CORE_ADDR *)VALUE_CONTENTS (v);
464 else
465 return v;
466 }
467 break;
468
469 default:
470 error ("Cannot look up value of a botched symbol.");
471 break;
472 }
473
474 VALUE_ADDRESS (v) = addr;
475 VALUE_LAZY (v) = 1;
476 return v;
477}
478
479/* Return a value of type TYPE, stored in register REGNUM, in frame
480 FRAME. */
481
482value
483value_from_register (type, regnum, frame)
484 struct type *type;
485 int regnum;
486 FRAME frame;
487{
488 char raw_buffer [MAX_REGISTER_RAW_SIZE];
489 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
490 CORE_ADDR addr;
491 int optim;
492 value v = allocate_value (type);
493 int len = TYPE_LENGTH (type);
494 char *value_bytes = 0;
495 int value_bytes_copied = 0;
496 int num_storage_locs;
497 enum lval_type lval;
498
499 VALUE_REGNO (v) = regnum;
500
501 num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
502 ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
503 1);
504
505 if (num_storage_locs > 1)
506 {
507 /* Value spread across multiple storage locations. */
508
509 int local_regnum;
510 int mem_stor = 0, reg_stor = 0;
511 int mem_tracking = 1;
512 CORE_ADDR last_addr = 0;
513 CORE_ADDR first_addr;
514
515 value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
516
517 /* Copy all of the data out, whereever it may be. */
518
519 for (local_regnum = regnum;
520 value_bytes_copied < len;
521 (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
522 ++local_regnum))
523 {
524 get_saved_register (value_bytes + value_bytes_copied,
525 &optim,
526 &addr,
527 frame,
528 local_regnum,
529 &lval);
530 if (lval == lval_register)
531 reg_stor++;
532 else
533 {
534 mem_stor++;
535
536 if (regnum == local_regnum)
537 first_addr = addr;
538
539 mem_tracking =
540 (mem_tracking
541 && (regnum == local_regnum
542 || addr == last_addr));
543 }
544 last_addr = addr;
545 }
546
547 if ((reg_stor && mem_stor)
548 || (mem_stor && !mem_tracking))
549 /* Mixed storage; all of the hassle we just went through was
550 for some good purpose. */
551 {
552 VALUE_LVAL (v) = lval_reg_frame_relative;
553 VALUE_FRAME (v) = FRAME_FP (frame);
554 VALUE_FRAME_REGNUM (v) = regnum;
555 }
556 else if (mem_stor)
557 {
558 VALUE_LVAL (v) = lval_memory;
559 VALUE_ADDRESS (v) = first_addr;
560 }
561 else if (reg_stor)
562 {
563 VALUE_LVAL (v) = lval_register;
564 VALUE_ADDRESS (v) = first_addr;
565 }
566 else
567 fatal ("value_from_register: Value not stored anywhere!");
568
569 VALUE_OPTIMIZED_OUT (v) = optim;
570
571 /* Any structure stored in more than one register will always be
572 an integral number of registers. Otherwise, you'd need to do
573 some fiddling with the last register copied here for little
574 endian machines. */
575
576 /* Copy into the contents section of the value. */
577 bcopy (value_bytes, VALUE_CONTENTS_RAW (v), len);
578
579 return v;
580 }
581
582 /* Data is completely contained within a single register. Locate the
583 register's contents in a real register or in core;
584 read the data in raw format. */
585
586 get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
587 VALUE_OPTIMIZED_OUT (v) = optim;
588 VALUE_LVAL (v) = lval;
589 VALUE_ADDRESS (v) = addr;
590
591 /* Convert the raw contents to virtual contents.
592 (Just copy them if the formats are the same.) */
593
594 target_convert_to_virtual (regnum, raw_buffer, virtual_buffer);
595
596 if (REGISTER_CONVERTIBLE (regnum))
597 {
598 /* When the raw and virtual formats differ, the virtual format
599 corresponds to a specific data type. If we want that type,
600 copy the data into the value.
601 Otherwise, do a type-conversion. */
602
603 if (type != REGISTER_VIRTUAL_TYPE (regnum))
604 {
605 /* eg a variable of type `float' in a 68881 register
606 with raw type `extended' and virtual type `double'.
607 Fetch it as a `double' and then convert to `float'. */
608 v = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
609 bcopy (virtual_buffer, VALUE_CONTENTS_RAW (v), len);
610 v = value_cast (type, v);
611 }
612 else
613 bcopy (virtual_buffer, VALUE_CONTENTS_RAW (v), len);
614 }
615 else
616 {
617 /* Raw and virtual formats are the same for this register. */
618
619#if TARGET_BYTE_ORDER == BIG_ENDIAN
620 if (len < REGISTER_RAW_SIZE (regnum))
621 {
622 /* Big-endian, and we want less than full size. */
623 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
624 }
625#endif
626
627 bcopy (virtual_buffer + VALUE_OFFSET (v),
628 VALUE_CONTENTS_RAW (v), len);
629 }
630
631 return v;
632}
633\f
634/* Given a struct symbol for a variable,
635 and a stack frame id,
636 return a (pointer to a) struct value containing the variable's address. */
637
638value
639locate_var_value (var, frame)
640 register struct symbol *var;
641 FRAME frame;
642{
643 CORE_ADDR addr = 0;
644 struct type *type = SYMBOL_TYPE (var);
645 struct type *result_type;
646 value lazy_value;
647
648 /* Evaluate it first; if the result is a memory address, we're fine.
649 Lazy evaluation pays off here. */
650
651 lazy_value = read_var_value (var, frame);
652 if (lazy_value == 0)
653 error ("Address of \"%s\" is unknown.", SYMBOL_NAME (var));
654
655 if (VALUE_LAZY (lazy_value))
656 {
657 addr = VALUE_ADDRESS (lazy_value);
658
659 /* C++: The "address" of a reference should yield the address
660 * of the object pointed to. So force an extra de-reference. */
661
662 if (TYPE_CODE (type) == TYPE_CODE_REF)
663 {
664 char *buf = alloca (TYPE_LENGTH (type));
665 read_memory (addr, buf, TYPE_LENGTH (type));
e1ce8aa5 666 addr = unpack_pointer (type, buf);
bd5635a1
RP
667 type = TYPE_TARGET_TYPE (type);
668 }
669
670 /* Address of an array is of the type of address of it's elements. */
671 result_type =
672 lookup_pointer_type (TYPE_CODE (type) == TYPE_CODE_ARRAY ?
673 TYPE_TARGET_TYPE (type) : type);
674
675 return value_cast (result_type,
676 value_from_long (builtin_type_long, (LONGEST) addr));
677 }
678
679 /* Not a memory address; check what the problem was. */
680 switch (VALUE_LVAL (lazy_value))
681 {
682 case lval_register:
683 case lval_reg_frame_relative:
684 error ("Address requested for identifier \"%s\" which is in a register.",
685 SYMBOL_NAME (var));
686 break;
687
688 default:
689 error ("Can't take address of \"%s\" which isn't an lvalue.",
690 SYMBOL_NAME (var));
691 break;
692 }
693 return 0; /* For lint -- never reached */
694}
This page took 0.05133 seconds and 4 git commands to generate.