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