* d10v-dis.c: Fix formatting.
[deliverable/binutils-gdb.git] / gdb / findvar.c
CommitLineData
c906108c 1/* Find a variable's value in memory, for GDB, the GNU debugger.
b6ba6518
KB
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001
c906108c
SS
4 Free Software Foundation, Inc.
5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23#include "defs.h"
24#include "symtab.h"
25#include "gdbtypes.h"
26#include "frame.h"
27#include "value.h"
28#include "gdbcore.h"
29#include "inferior.h"
30#include "target.h"
31#include "gdb_string.h"
32#include "floatformat.h"
c5aa993b 33#include "symfile.h" /* for overlay functions */
4e052eda 34#include "regcache.h"
c906108c
SS
35
36/* This is used to indicate that we don't know the format of the floating point
37 number. Typically, this is useful for native ports, where the actual format
38 is irrelevant, since no conversions will be taking place. */
39
40const struct floatformat floatformat_unknown;
41
c906108c
SS
42/* Basic byte-swapping routines. GDB has needed these for a long time...
43 All extract a target-format integer at ADDR which is LEN bytes long. */
44
45#if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
46 /* 8 bit characters are a pretty safe assumption these days, so we
47 assume it throughout all these swapping routines. If we had to deal with
48 9 bit characters, we would need to make len be in bits and would have
49 to re-write these routines... */
c5aa993b 50you lose
c906108c
SS
51#endif
52
a9ac8f51
AC
53LONGEST
54extract_signed_integer (void *addr, int len)
c906108c
SS
55{
56 LONGEST retval;
57 unsigned char *p;
c5aa993b 58 unsigned char *startaddr = (unsigned char *) addr;
c906108c
SS
59 unsigned char *endaddr = startaddr + len;
60
61 if (len > (int) sizeof (LONGEST))
62 error ("\
63That operation is not available on integers of more than %d bytes.",
64 sizeof (LONGEST));
65
66 /* Start at the most significant end of the integer, and work towards
67 the least significant. */
68 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
69 {
70 p = startaddr;
71 /* Do the sign extension once at the start. */
c5aa993b 72 retval = ((LONGEST) * p ^ 0x80) - 0x80;
c906108c
SS
73 for (++p; p < endaddr; ++p)
74 retval = (retval << 8) | *p;
75 }
76 else
77 {
78 p = endaddr - 1;
79 /* Do the sign extension once at the start. */
c5aa993b 80 retval = ((LONGEST) * p ^ 0x80) - 0x80;
c906108c
SS
81 for (--p; p >= startaddr; --p)
82 retval = (retval << 8) | *p;
83 }
84 return retval;
85}
86
87ULONGEST
a9ac8f51 88extract_unsigned_integer (void *addr, int len)
c906108c
SS
89{
90 ULONGEST retval;
91 unsigned char *p;
c5aa993b 92 unsigned char *startaddr = (unsigned char *) addr;
c906108c
SS
93 unsigned char *endaddr = startaddr + len;
94
95 if (len > (int) sizeof (ULONGEST))
96 error ("\
97That operation is not available on integers of more than %d bytes.",
98 sizeof (ULONGEST));
99
100 /* Start at the most significant end of the integer, and work towards
101 the least significant. */
102 retval = 0;
103 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
104 {
105 for (p = startaddr; p < endaddr; ++p)
106 retval = (retval << 8) | *p;
107 }
108 else
109 {
110 for (p = endaddr - 1; p >= startaddr; --p)
111 retval = (retval << 8) | *p;
112 }
113 return retval;
114}
115
116/* Sometimes a long long unsigned integer can be extracted as a
117 LONGEST value. This is done so that we can print these values
118 better. If this integer can be converted to a LONGEST, this
119 function returns 1 and sets *PVAL. Otherwise it returns 0. */
120
121int
a9ac8f51 122extract_long_unsigned_integer (void *addr, int orig_len, LONGEST *pval)
c906108c
SS
123{
124 char *p, *first_addr;
125 int len;
126
127 len = orig_len;
128 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
129 {
130 for (p = (char *) addr;
131 len > (int) sizeof (LONGEST) && p < (char *) addr + orig_len;
132 p++)
133 {
134 if (*p == 0)
135 len--;
136 else
137 break;
138 }
139 first_addr = p;
140 }
141 else
142 {
143 first_addr = (char *) addr;
144 for (p = (char *) addr + orig_len - 1;
145 len > (int) sizeof (LONGEST) && p >= (char *) addr;
146 p--)
147 {
148 if (*p == 0)
149 len--;
150 else
151 break;
152 }
153 }
154
155 if (len <= (int) sizeof (LONGEST))
156 {
157 *pval = (LONGEST) extract_unsigned_integer (first_addr,
158 sizeof (LONGEST));
159 return 1;
160 }
161
162 return 0;
163}
164
4478b372
JB
165
166/* Treat the LEN bytes at ADDR as a target-format address, and return
167 that address. ADDR is a buffer in the GDB process, not in the
168 inferior.
169
170 This function should only be used by target-specific code. It
171 assumes that a pointer has the same representation as that thing's
172 address represented as an integer. Some machines use word
173 addresses, or similarly munged things, for certain types of
174 pointers, so that assumption doesn't hold everywhere.
175
176 Common code should use extract_typed_address instead, or something
177 else based on POINTER_TO_ADDRESS. */
178
c906108c 179CORE_ADDR
a9ac8f51 180extract_address (void *addr, int len)
c906108c
SS
181{
182 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
183 whether we want this to be true eventually. */
c5aa993b 184 return (CORE_ADDR) extract_unsigned_integer (addr, len);
c906108c
SS
185}
186
4478b372 187
4478b372
JB
188/* Treat the bytes at BUF as a pointer of type TYPE, and return the
189 address it represents. */
190CORE_ADDR
191extract_typed_address (void *buf, struct type *type)
192{
193 if (TYPE_CODE (type) != TYPE_CODE_PTR
194 && TYPE_CODE (type) != TYPE_CODE_REF)
8e65ff28
AC
195 internal_error (__FILE__, __LINE__,
196 "extract_typed_address: "
4478b372
JB
197 "type is not a pointer or reference");
198
199 return POINTER_TO_ADDRESS (type, buf);
200}
201
202
c906108c 203void
a9ac8f51 204store_signed_integer (void *addr, int len, LONGEST val)
c906108c
SS
205{
206 unsigned char *p;
c5aa993b 207 unsigned char *startaddr = (unsigned char *) addr;
c906108c
SS
208 unsigned char *endaddr = startaddr + len;
209
210 /* Start at the least significant end of the integer, and work towards
211 the most significant. */
212 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
213 {
214 for (p = endaddr - 1; p >= startaddr; --p)
215 {
216 *p = val & 0xff;
217 val >>= 8;
218 }
219 }
220 else
221 {
222 for (p = startaddr; p < endaddr; ++p)
223 {
224 *p = val & 0xff;
225 val >>= 8;
226 }
227 }
228}
229
230void
a9ac8f51 231store_unsigned_integer (void *addr, int len, ULONGEST val)
c906108c
SS
232{
233 unsigned char *p;
c5aa993b 234 unsigned char *startaddr = (unsigned char *) addr;
c906108c
SS
235 unsigned char *endaddr = startaddr + len;
236
237 /* Start at the least significant end of the integer, and work towards
238 the most significant. */
239 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
240 {
241 for (p = endaddr - 1; p >= startaddr; --p)
242 {
243 *p = val & 0xff;
244 val >>= 8;
245 }
246 }
247 else
248 {
249 for (p = startaddr; p < endaddr; ++p)
250 {
251 *p = val & 0xff;
252 val >>= 8;
253 }
254 }
255}
256
4478b372
JB
257/* Store the address VAL as a LEN-byte value in target byte order at
258 ADDR. ADDR is a buffer in the GDB process, not in the inferior.
259
260 This function should only be used by target-specific code. It
261 assumes that a pointer has the same representation as that thing's
262 address represented as an integer. Some machines use word
263 addresses, or similarly munged things, for certain types of
264 pointers, so that assumption doesn't hold everywhere.
265
266 Common code should use store_typed_address instead, or something else
267 based on ADDRESS_TO_POINTER. */
c906108c 268void
a9ac8f51 269store_address (void *addr, int len, LONGEST val)
c906108c 270{
c906108c
SS
271 store_unsigned_integer (addr, len, val);
272}
4478b372
JB
273
274
4478b372
JB
275/* Store the address ADDR as a pointer of type TYPE at BUF, in target
276 form. */
277void
278store_typed_address (void *buf, struct type *type, CORE_ADDR addr)
279{
280 if (TYPE_CODE (type) != TYPE_CODE_PTR
281 && TYPE_CODE (type) != TYPE_CODE_REF)
8e65ff28
AC
282 internal_error (__FILE__, __LINE__,
283 "store_typed_address: "
4478b372
JB
284 "type is not a pointer or reference");
285
286 ADDRESS_TO_POINTER (type, buf, addr);
287}
288
289
290
c906108c 291\f
c906108c
SS
292/* Extract a floating-point number from a target-order byte-stream at ADDR.
293 Returns the value as type DOUBLEST.
294
295 If the host and target formats agree, we just copy the raw data into the
296 appropriate type of variable and return, letting the host increase precision
297 as necessary. Otherwise, we call the conversion routine and let it do the
298 dirty work. */
299
300DOUBLEST
3db87ba3 301extract_floating (void *addr, int len)
c906108c
SS
302{
303 DOUBLEST dretval;
304
3db87ba3 305 if (len * TARGET_CHAR_BIT == TARGET_FLOAT_BIT)
c906108c
SS
306 {
307 if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT)
308 {
309 float retval;
310
311 memcpy (&retval, addr, sizeof (retval));
312 return retval;
313 }
314 else
315 floatformat_to_doublest (TARGET_FLOAT_FORMAT, addr, &dretval);
316 }
3db87ba3 317 else if (len * TARGET_CHAR_BIT == TARGET_DOUBLE_BIT)
c906108c
SS
318 {
319 if (HOST_DOUBLE_FORMAT == TARGET_DOUBLE_FORMAT)
320 {
321 double retval;
322
323 memcpy (&retval, addr, sizeof (retval));
324 return retval;
325 }
326 else
327 floatformat_to_doublest (TARGET_DOUBLE_FORMAT, addr, &dretval);
328 }
3db87ba3 329 else if (len * TARGET_CHAR_BIT == TARGET_LONG_DOUBLE_BIT)
c906108c
SS
330 {
331 if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT)
332 {
333 DOUBLEST retval;
334
335 memcpy (&retval, addr, sizeof (retval));
336 return retval;
337 }
338 else
339 floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT, addr, &dretval);
340 }
341 else
342 {
343 error ("Can't deal with a floating point number of %d bytes.", len);
344 }
345
346 return dretval;
347}
348
349void
3db87ba3 350store_floating (void *addr, int len, DOUBLEST val)
c906108c 351{
3db87ba3 352 if (len * TARGET_CHAR_BIT == TARGET_FLOAT_BIT)
c906108c
SS
353 {
354 if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT)
355 {
356 float floatval = val;
357
358 memcpy (addr, &floatval, sizeof (floatval));
359 }
360 else
361 floatformat_from_doublest (TARGET_FLOAT_FORMAT, &val, addr);
362 }
3db87ba3 363 else if (len * TARGET_CHAR_BIT == TARGET_DOUBLE_BIT)
c906108c
SS
364 {
365 if (HOST_DOUBLE_FORMAT == TARGET_DOUBLE_FORMAT)
366 {
367 double doubleval = val;
368
369 memcpy (addr, &doubleval, sizeof (doubleval));
370 }
371 else
372 floatformat_from_doublest (TARGET_DOUBLE_FORMAT, &val, addr);
373 }
3db87ba3 374 else if (len * TARGET_CHAR_BIT == TARGET_LONG_DOUBLE_BIT)
c906108c
SS
375 {
376 if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT)
377 memcpy (addr, &val, sizeof (val));
378 else
379 floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT, &val, addr);
380 }
381 else
382 {
383 error ("Can't deal with a floating point number of %d bytes.", len);
384 }
385}
c906108c
SS
386
387/* Return a `value' with the contents of register REGNUM
388 in its virtual format, with the type specified by
389 REGISTER_VIRTUAL_TYPE.
390
391 NOTE: returns NULL if register value is not available.
392 Caller will check return value or die! */
393
394value_ptr
fba45db2 395value_of_register (int regnum)
c906108c
SS
396{
397 CORE_ADDR addr;
398 int optim;
399 register value_ptr reg_val;
e6cbd02a 400 char *raw_buffer = (char*) alloca (MAX_REGISTER_RAW_SIZE);
c906108c
SS
401 enum lval_type lval;
402
403 get_saved_register (raw_buffer, &optim, &addr,
404 selected_frame, regnum, &lval);
405
32178cab 406 if (register_cached (regnum) < 0)
c5aa993b 407 return NULL; /* register value not available */
c906108c
SS
408
409 reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
410
411 /* Convert raw data to virtual format if necessary. */
412
c906108c
SS
413 if (REGISTER_CONVERTIBLE (regnum))
414 {
415 REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
416 raw_buffer, VALUE_CONTENTS_RAW (reg_val));
417 }
392a587b
JM
418 else if (REGISTER_RAW_SIZE (regnum) == REGISTER_VIRTUAL_SIZE (regnum))
419 memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
420 REGISTER_RAW_SIZE (regnum));
c906108c 421 else
8e65ff28
AC
422 internal_error (__FILE__, __LINE__,
423 "Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
96baa820
JM
424 REGISTER_NAME (regnum),
425 regnum,
426 REGISTER_RAW_SIZE (regnum),
427 REGISTER_VIRTUAL_SIZE (regnum));
c906108c
SS
428 VALUE_LVAL (reg_val) = lval;
429 VALUE_ADDRESS (reg_val) = addr;
430 VALUE_REGNO (reg_val) = regnum;
431 VALUE_OPTIMIZED_OUT (reg_val) = optim;
432 return reg_val;
433}
4478b372
JB
434
435/* Given a pointer of type TYPE in target form in BUF, return the
436 address it represents. */
437CORE_ADDR
ac2e2ef7 438unsigned_pointer_to_address (struct type *type, void *buf)
4478b372
JB
439{
440 return extract_address (buf, TYPE_LENGTH (type));
441}
442
ac2e2ef7
AC
443CORE_ADDR
444signed_pointer_to_address (struct type *type, void *buf)
445{
446 return extract_signed_integer (buf, TYPE_LENGTH (type));
447}
4478b372
JB
448
449/* Given an address, store it as a pointer of type TYPE in target
450 format in BUF. */
451void
ac2e2ef7 452unsigned_address_to_pointer (struct type *type, void *buf, CORE_ADDR addr)
4478b372
JB
453{
454 store_address (buf, TYPE_LENGTH (type), addr);
455}
456
ac2e2ef7
AC
457void
458address_to_signed_pointer (struct type *type, void *buf, CORE_ADDR addr)
459{
460 store_signed_integer (buf, TYPE_LENGTH (type), addr);
461}
c906108c
SS
462\f
463/* Will calling read_var_value or locate_var_value on SYM end
464 up caring what frame it is being evaluated relative to? SYM must
465 be non-NULL. */
466int
fba45db2 467symbol_read_needs_frame (struct symbol *sym)
c906108c
SS
468{
469 switch (SYMBOL_CLASS (sym))
470 {
471 /* All cases listed explicitly so that gcc -Wall will detect it if
c5aa993b 472 we failed to consider one. */
c906108c
SS
473 case LOC_REGISTER:
474 case LOC_ARG:
475 case LOC_REF_ARG:
476 case LOC_REGPARM:
477 case LOC_REGPARM_ADDR:
478 case LOC_LOCAL:
479 case LOC_LOCAL_ARG:
480 case LOC_BASEREG:
481 case LOC_BASEREG_ARG:
482 case LOC_THREAD_LOCAL_STATIC:
483 return 1;
484
485 case LOC_UNDEF:
486 case LOC_CONST:
487 case LOC_STATIC:
488 case LOC_INDIRECT:
489 case LOC_TYPEDEF:
490
491 case LOC_LABEL:
492 /* Getting the address of a label can be done independently of the block,
c5aa993b
JM
493 even if some *uses* of that address wouldn't work so well without
494 the right frame. */
c906108c
SS
495
496 case LOC_BLOCK:
497 case LOC_CONST_BYTES:
498 case LOC_UNRESOLVED:
499 case LOC_OPTIMIZED_OUT:
500 return 0;
501 }
502 return 1;
503}
504
505/* Given a struct symbol for a variable,
506 and a stack frame id, read the value of the variable
507 and return a (pointer to a) struct value containing the value.
508 If the variable cannot be found, return a zero pointer.
509 If FRAME is NULL, use the selected_frame. */
510
511value_ptr
fba45db2 512read_var_value (register struct symbol *var, struct frame_info *frame)
c906108c
SS
513{
514 register value_ptr v;
515 struct type *type = SYMBOL_TYPE (var);
516 CORE_ADDR addr;
517 register int len;
518
519 v = allocate_value (type);
520 VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
521 VALUE_BFD_SECTION (v) = SYMBOL_BFD_SECTION (var);
522
523 len = TYPE_LENGTH (type);
524
c5aa993b
JM
525 if (frame == NULL)
526 frame = selected_frame;
c906108c
SS
527
528 switch (SYMBOL_CLASS (var))
529 {
530 case LOC_CONST:
531 /* Put the constant back in target format. */
532 store_signed_integer (VALUE_CONTENTS_RAW (v), len,
533 (LONGEST) SYMBOL_VALUE (var));
534 VALUE_LVAL (v) = not_lval;
535 return v;
536
537 case LOC_LABEL:
538 /* Put the constant back in target format. */
539 if (overlay_debugging)
4478b372
JB
540 {
541 CORE_ADDR addr
542 = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
543 SYMBOL_BFD_SECTION (var));
544 store_typed_address (VALUE_CONTENTS_RAW (v), type, addr);
545 }
c906108c 546 else
4478b372
JB
547 store_typed_address (VALUE_CONTENTS_RAW (v), type,
548 SYMBOL_VALUE_ADDRESS (var));
c906108c
SS
549 VALUE_LVAL (v) = not_lval;
550 return v;
551
552 case LOC_CONST_BYTES:
553 {
554 char *bytes_addr;
555 bytes_addr = SYMBOL_VALUE_BYTES (var);
556 memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
557 VALUE_LVAL (v) = not_lval;
558 return v;
559 }
560
561 case LOC_STATIC:
562 if (overlay_debugging)
563 addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
564 SYMBOL_BFD_SECTION (var));
565 else
566 addr = SYMBOL_VALUE_ADDRESS (var);
567 break;
568
569 case LOC_INDIRECT:
570 /* The import slot does not have a real address in it from the
571 dynamic loader (dld.sl on HP-UX), if the target hasn't begun
c5aa993b 572 execution yet, so check for that. */
c906108c 573 if (!target_has_execution)
c5aa993b 574 error ("\
c906108c
SS
575Attempt to access variable defined in different shared object or load module when\n\
576addresses have not been bound by the dynamic loader. Try again when executable is running.");
c5aa993b 577
c906108c
SS
578 addr = SYMBOL_VALUE_ADDRESS (var);
579 addr = read_memory_unsigned_integer
580 (addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
581 break;
582
583 case LOC_ARG:
584 if (frame == NULL)
585 return 0;
586 addr = FRAME_ARGS_ADDRESS (frame);
587 if (!addr)
588 return 0;
589 addr += SYMBOL_VALUE (var);
590 break;
591
592 case LOC_REF_ARG:
593 if (frame == NULL)
594 return 0;
595 addr = FRAME_ARGS_ADDRESS (frame);
596 if (!addr)
597 return 0;
598 addr += SYMBOL_VALUE (var);
599 addr = read_memory_unsigned_integer
600 (addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
601 break;
602
603 case LOC_LOCAL:
604 case LOC_LOCAL_ARG:
605 if (frame == NULL)
606 return 0;
607 addr = FRAME_LOCALS_ADDRESS (frame);
608 addr += SYMBOL_VALUE (var);
609 break;
610
611 case LOC_BASEREG:
612 case LOC_BASEREG_ARG:
c906108c
SS
613 case LOC_THREAD_LOCAL_STATIC:
614 {
9ed10b08 615 value_ptr regval;
c5aa993b 616
9ed10b08
ND
617 regval = value_from_register (lookup_pointer_type (type),
618 SYMBOL_BASEREG (var), frame);
619 if (regval == NULL)
620 error ("Value of base register not available.");
621 addr = value_as_pointer (regval);
c5aa993b
JM
622 addr += SYMBOL_VALUE (var);
623 break;
c906108c 624 }
c5aa993b 625
c906108c
SS
626 case LOC_TYPEDEF:
627 error ("Cannot look up value of a typedef");
628 break;
629
630 case LOC_BLOCK:
631 if (overlay_debugging)
c5aa993b 632 VALUE_ADDRESS (v) = symbol_overlayed_address
c906108c
SS
633 (BLOCK_START (SYMBOL_BLOCK_VALUE (var)), SYMBOL_BFD_SECTION (var));
634 else
635 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
636 return v;
637
638 case LOC_REGISTER:
639 case LOC_REGPARM:
640 case LOC_REGPARM_ADDR:
641 {
642 struct block *b;
643 int regno = SYMBOL_VALUE (var);
644 value_ptr regval;
645
646 if (frame == NULL)
647 return 0;
648 b = get_frame_block (frame);
649
650 if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
651 {
652 regval = value_from_register (lookup_pointer_type (type),
c5aa993b 653 regno,
c906108c
SS
654 frame);
655
656 if (regval == NULL)
657 error ("Value of register variable not available.");
658
c5aa993b 659 addr = value_as_pointer (regval);
c906108c
SS
660 VALUE_LVAL (v) = lval_memory;
661 }
662 else
663 {
664 regval = value_from_register (type, regno, frame);
665
666 if (regval == NULL)
667 error ("Value of register variable not available.");
668 return regval;
669 }
670 }
671 break;
672
673 case LOC_UNRESOLVED:
674 {
675 struct minimal_symbol *msym;
676
677 msym = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL);
678 if (msym == NULL)
679 return 0;
680 if (overlay_debugging)
681 addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym),
682 SYMBOL_BFD_SECTION (msym));
683 else
684 addr = SYMBOL_VALUE_ADDRESS (msym);
685 }
686 break;
687
688 case LOC_OPTIMIZED_OUT:
689 VALUE_LVAL (v) = not_lval;
690 VALUE_OPTIMIZED_OUT (v) = 1;
691 return v;
692
693 default:
694 error ("Cannot look up value of a botched symbol.");
695 break;
696 }
697
698 VALUE_ADDRESS (v) = addr;
699 VALUE_LAZY (v) = 1;
700 return v;
701}
702
703/* Return a value of type TYPE, stored in register REGNUM, in frame
0f2c5ba5 704 FRAME.
c906108c
SS
705
706 NOTE: returns NULL if register value is not available.
707 Caller will check return value or die! */
708
709value_ptr
fba45db2 710value_from_register (struct type *type, int regnum, struct frame_info *frame)
c906108c 711{
e6cbd02a 712 char *raw_buffer = (char*) alloca (MAX_REGISTER_RAW_SIZE);
c906108c
SS
713 CORE_ADDR addr;
714 int optim;
715 value_ptr v = allocate_value (type);
716 char *value_bytes = 0;
717 int value_bytes_copied = 0;
718 int num_storage_locs;
719 enum lval_type lval;
720 int len;
721
722 CHECK_TYPEDEF (type);
723 len = TYPE_LENGTH (type);
724
725 VALUE_REGNO (v) = regnum;
726
727 num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
728 ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
729 1);
730
731 if (num_storage_locs > 1
732#ifdef GDB_TARGET_IS_H8500
733 || TYPE_CODE (type) == TYPE_CODE_PTR
734#endif
c5aa993b 735 )
c906108c
SS
736 {
737 /* Value spread across multiple storage locations. */
c5aa993b 738
c906108c
SS
739 int local_regnum;
740 int mem_stor = 0, reg_stor = 0;
741 int mem_tracking = 1;
742 CORE_ADDR last_addr = 0;
743 CORE_ADDR first_addr = 0;
744
745 value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
746
747 /* Copy all of the data out, whereever it may be. */
748
749#ifdef GDB_TARGET_IS_H8500
750/* This piece of hideosity is required because the H8500 treats registers
751 differently depending upon whether they are used as pointers or not. As a
752 pointer, a register needs to have a page register tacked onto the front.
753 An alternate way to do this would be to have gcc output different register
754 numbers for the pointer & non-pointer form of the register. But, it
755 doesn't, so we're stuck with this. */
756
757 if (TYPE_CODE (type) == TYPE_CODE_PTR
758 && len > 2)
759 {
760 int page_regnum;
761
762 switch (regnum)
763 {
c5aa993b
JM
764 case R0_REGNUM:
765 case R1_REGNUM:
766 case R2_REGNUM:
767 case R3_REGNUM:
c906108c
SS
768 page_regnum = SEG_D_REGNUM;
769 break;
c5aa993b
JM
770 case R4_REGNUM:
771 case R5_REGNUM:
c906108c
SS
772 page_regnum = SEG_E_REGNUM;
773 break;
c5aa993b
JM
774 case R6_REGNUM:
775 case R7_REGNUM:
c906108c
SS
776 page_regnum = SEG_T_REGNUM;
777 break;
778 }
779
780 value_bytes[0] = 0;
781 get_saved_register (value_bytes + 1,
782 &optim,
783 &addr,
784 frame,
785 page_regnum,
786 &lval);
787
32178cab 788 if (register_cached (page_regnum) == -1)
c906108c
SS
789 return NULL; /* register value not available */
790
791 if (lval == lval_register)
792 reg_stor++;
793 else
794 mem_stor++;
795 first_addr = addr;
796 last_addr = addr;
797
798 get_saved_register (value_bytes + 2,
799 &optim,
800 &addr,
801 frame,
802 regnum,
803 &lval);
804
32178cab 805 if (register_cached (regnum) == -1)
c906108c
SS
806 return NULL; /* register value not available */
807
808 if (lval == lval_register)
809 reg_stor++;
810 else
811 {
812 mem_stor++;
813 mem_tracking = mem_tracking && (addr == last_addr);
814 }
815 last_addr = addr;
816 }
817 else
c5aa993b 818#endif /* GDB_TARGET_IS_H8500 */
c906108c
SS
819 for (local_regnum = regnum;
820 value_bytes_copied < len;
821 (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
822 ++local_regnum))
823 {
824 get_saved_register (value_bytes + value_bytes_copied,
825 &optim,
826 &addr,
827 frame,
828 local_regnum,
829 &lval);
830
32178cab 831 if (register_cached (local_regnum) == -1)
c5aa993b 832 return NULL; /* register value not available */
c906108c
SS
833
834 if (regnum == local_regnum)
835 first_addr = addr;
836 if (lval == lval_register)
837 reg_stor++;
838 else
839 {
840 mem_stor++;
c5aa993b 841
c906108c
SS
842 mem_tracking =
843 (mem_tracking
844 && (regnum == local_regnum
845 || addr == last_addr));
846 }
847 last_addr = addr;
848 }
849
850 if ((reg_stor && mem_stor)
851 || (mem_stor && !mem_tracking))
852 /* Mixed storage; all of the hassle we just went through was
853 for some good purpose. */
854 {
855 VALUE_LVAL (v) = lval_reg_frame_relative;
856 VALUE_FRAME (v) = FRAME_FP (frame);
857 VALUE_FRAME_REGNUM (v) = regnum;
858 }
859 else if (mem_stor)
860 {
861 VALUE_LVAL (v) = lval_memory;
862 VALUE_ADDRESS (v) = first_addr;
863 }
864 else if (reg_stor)
865 {
866 VALUE_LVAL (v) = lval_register;
867 VALUE_ADDRESS (v) = first_addr;
868 }
869 else
8e65ff28
AC
870 internal_error (__FILE__, __LINE__,
871 "value_from_register: Value not stored anywhere!");
c906108c
SS
872
873 VALUE_OPTIMIZED_OUT (v) = optim;
874
875 /* Any structure stored in more than one register will always be
c5aa993b
JM
876 an integral number of registers. Otherwise, you'd need to do
877 some fiddling with the last register copied here for little
878 endian machines. */
c906108c
SS
879
880 /* Copy into the contents section of the value. */
881 memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
882
883 /* Finally do any conversion necessary when extracting this
884 type from more than one register. */
885#ifdef REGISTER_CONVERT_TO_TYPE
c5aa993b 886 REGISTER_CONVERT_TO_TYPE (regnum, type, VALUE_CONTENTS_RAW (v));
c906108c
SS
887#endif
888 return v;
889 }
890
891 /* Data is completely contained within a single register. Locate the
892 register's contents in a real register or in core;
893 read the data in raw format. */
894
895 get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
896
32178cab 897 if (register_cached (regnum) == -1)
c5aa993b 898 return NULL; /* register value not available */
c906108c
SS
899
900 VALUE_OPTIMIZED_OUT (v) = optim;
901 VALUE_LVAL (v) = lval;
902 VALUE_ADDRESS (v) = addr;
903
904 /* Convert raw data to virtual format if necessary. */
c5aa993b 905
c906108c
SS
906 if (REGISTER_CONVERTIBLE (regnum))
907 {
908 REGISTER_CONVERT_TO_VIRTUAL (regnum, type,
909 raw_buffer, VALUE_CONTENTS_RAW (v));
910 }
911 else
c906108c
SS
912 {
913 /* Raw and virtual formats are the same for this register. */
914
915 if (TARGET_BYTE_ORDER == BIG_ENDIAN && len < REGISTER_RAW_SIZE (regnum))
916 {
c5aa993b 917 /* Big-endian, and we want less than full size. */
c906108c
SS
918 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
919 }
920
921 memcpy (VALUE_CONTENTS_RAW (v), raw_buffer + VALUE_OFFSET (v), len);
922 }
c5aa993b 923
c906108c
SS
924 return v;
925}
926\f
927/* Given a struct symbol for a variable or function,
928 and a stack frame id,
929 return a (pointer to a) struct value containing the properly typed
930 address. */
931
932value_ptr
fba45db2 933locate_var_value (register struct symbol *var, struct frame_info *frame)
c906108c
SS
934{
935 CORE_ADDR addr = 0;
936 struct type *type = SYMBOL_TYPE (var);
937 value_ptr lazy_value;
938
939 /* Evaluate it first; if the result is a memory address, we're fine.
940 Lazy evaluation pays off here. */
941
942 lazy_value = read_var_value (var, frame);
943 if (lazy_value == 0)
944 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
945
946 if (VALUE_LAZY (lazy_value)
947 || TYPE_CODE (type) == TYPE_CODE_FUNC)
948 {
949 value_ptr val;
950
951 addr = VALUE_ADDRESS (lazy_value);
4478b372 952 val = value_from_pointer (lookup_pointer_type (type), addr);
c906108c
SS
953 VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (lazy_value);
954 return val;
955 }
956
957 /* Not a memory address; check what the problem was. */
c5aa993b 958 switch (VALUE_LVAL (lazy_value))
c906108c
SS
959 {
960 case lval_register:
961 case lval_reg_frame_relative:
962 error ("Address requested for identifier \"%s\" which is in a register.",
963 SYMBOL_SOURCE_NAME (var));
964 break;
965
966 default:
967 error ("Can't take address of \"%s\" which isn't an lvalue.",
968 SYMBOL_SOURCE_NAME (var));
969 break;
970 }
c5aa993b 971 return 0; /* For lint -- never reached */
c906108c 972}
This page took 0.148287 seconds and 4 git commands to generate.