]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/findvar.c
Update year range in copyright notice of all files owned by the GDB project.
[thirdparty/binutils-gdb.git] / gdb / findvar.c
CommitLineData
c906108c 1/* Find a variable's value in memory, for GDB, the GNU debugger.
1bac305b 2
32d0add0 3 Copyright (C) 1986-2015 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
21#include "symtab.h"
22#include "gdbtypes.h"
23#include "frame.h"
24#include "value.h"
25#include "gdbcore.h"
26#include "inferior.h"
27#include "target.h"
c906108c 28#include "floatformat.h"
c5aa993b 29#include "symfile.h" /* for overlay functions */
4e052eda 30#include "regcache.h"
eb8bc282 31#include "user-regs.h"
fe898f56 32#include "block.h"
e0740f77 33#include "objfiles.h"
a5ee536b 34#include "language.h"
c906108c 35
9659616a
MS
36/* Basic byte-swapping routines. All 'extract' functions return a
37 host-format integer from a target-format integer at ADDR which is
38 LEN bytes long. */
c906108c
SS
39
40#if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
41 /* 8 bit characters are a pretty safe assumption these days, so we
42 assume it throughout all these swapping routines. If we had to deal with
43 9 bit characters, we would need to make len be in bits and would have
44 to re-write these routines... */
c5aa993b 45you lose
c906108c
SS
46#endif
47
a9ac8f51 48LONGEST
e17a4113
UW
49extract_signed_integer (const gdb_byte *addr, int len,
50 enum bfd_endian byte_order)
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))
8a3fe4f8
AC
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. */
e17a4113 64 if (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
e17a4113
UW
84extract_unsigned_integer (const gdb_byte *addr, int len,
85 enum bfd_endian byte_order)
c906108c
SS
86{
87 ULONGEST retval;
37611a2b
AC
88 const unsigned char *p;
89 const unsigned char *startaddr = addr;
90 const unsigned char *endaddr = startaddr + len;
c906108c
SS
91
92 if (len > (int) sizeof (ULONGEST))
8a3fe4f8
AC
93 error (_("\
94That operation is not available on integers of more than %d bytes."),
baa6f10b 95 (int) sizeof (ULONGEST));
c906108c
SS
96
97 /* Start at the most significant end of the integer, and work towards
98 the least significant. */
99 retval = 0;
e17a4113 100 if (byte_order == BFD_ENDIAN_BIG)
c906108c
SS
101 {
102 for (p = startaddr; p < endaddr; ++p)
103 retval = (retval << 8) | *p;
104 }
105 else
106 {
107 for (p = endaddr - 1; p >= startaddr; --p)
108 retval = (retval << 8) | *p;
109 }
110 return retval;
111}
112
113/* Sometimes a long long unsigned integer can be extracted as a
114 LONGEST value. This is done so that we can print these values
115 better. If this integer can be converted to a LONGEST, this
116 function returns 1 and sets *PVAL. Otherwise it returns 0. */
117
118int
0d509538 119extract_long_unsigned_integer (const gdb_byte *addr, int orig_len,
e17a4113 120 enum bfd_endian byte_order, LONGEST *pval)
c906108c 121{
0d509538
AC
122 const gdb_byte *p;
123 const gdb_byte *first_addr;
c906108c
SS
124 int len;
125
126 len = orig_len;
e17a4113 127 if (byte_order == BFD_ENDIAN_BIG)
c906108c 128 {
0d509538
AC
129 for (p = addr;
130 len > (int) sizeof (LONGEST) && p < addr + orig_len;
c906108c
SS
131 p++)
132 {
133 if (*p == 0)
134 len--;
135 else
136 break;
137 }
138 first_addr = p;
139 }
140 else
141 {
0d509538
AC
142 first_addr = addr;
143 for (p = addr + orig_len - 1;
144 len > (int) sizeof (LONGEST) && p >= addr;
c906108c
SS
145 p--)
146 {
147 if (*p == 0)
148 len--;
149 else
150 break;
151 }
152 }
153
154 if (len <= (int) sizeof (LONGEST))
155 {
156 *pval = (LONGEST) extract_unsigned_integer (first_addr,
e17a4113
UW
157 sizeof (LONGEST),
158 byte_order);
c906108c
SS
159 return 1;
160 }
161
162 return 0;
163}
164
4478b372 165
4478b372
JB
166/* Treat the bytes at BUF as a pointer of type TYPE, and return the
167 address it represents. */
168CORE_ADDR
0d509538 169extract_typed_address (const gdb_byte *buf, struct type *type)
4478b372
JB
170{
171 if (TYPE_CODE (type) != TYPE_CODE_PTR
172 && TYPE_CODE (type) != TYPE_CODE_REF)
8e65ff28 173 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
174 _("extract_typed_address: "
175 "type is not a pointer or reference"));
4478b372 176
50810684 177 return gdbarch_pointer_to_address (get_type_arch (type), type, buf);
4478b372
JB
178}
179
9659616a
MS
180/* All 'store' functions accept a host-format integer and store a
181 target-format integer at ADDR which is LEN bytes long. */
4478b372 182
c906108c 183void
e17a4113
UW
184store_signed_integer (gdb_byte *addr, int len,
185 enum bfd_endian byte_order, LONGEST val)
c906108c 186{
0d509538
AC
187 gdb_byte *p;
188 gdb_byte *startaddr = addr;
189 gdb_byte *endaddr = startaddr + len;
c906108c
SS
190
191 /* Start at the least significant end of the integer, and work towards
192 the most significant. */
e17a4113 193 if (byte_order == BFD_ENDIAN_BIG)
c906108c
SS
194 {
195 for (p = endaddr - 1; p >= startaddr; --p)
196 {
197 *p = val & 0xff;
198 val >>= 8;
199 }
200 }
201 else
202 {
203 for (p = startaddr; p < endaddr; ++p)
204 {
205 *p = val & 0xff;
206 val >>= 8;
207 }
208 }
209}
210
211void
e17a4113
UW
212store_unsigned_integer (gdb_byte *addr, int len,
213 enum bfd_endian byte_order, ULONGEST val)
c906108c
SS
214{
215 unsigned char *p;
c5aa993b 216 unsigned char *startaddr = (unsigned char *) addr;
c906108c
SS
217 unsigned char *endaddr = startaddr + len;
218
219 /* Start at the least significant end of the integer, and work towards
220 the most significant. */
e17a4113 221 if (byte_order == BFD_ENDIAN_BIG)
c906108c
SS
222 {
223 for (p = endaddr - 1; p >= startaddr; --p)
224 {
225 *p = val & 0xff;
226 val >>= 8;
227 }
228 }
229 else
230 {
231 for (p = startaddr; p < endaddr; ++p)
232 {
233 *p = val & 0xff;
234 val >>= 8;
235 }
236 }
237}
238
4478b372
JB
239/* Store the address ADDR as a pointer of type TYPE at BUF, in target
240 form. */
241void
0d509538 242store_typed_address (gdb_byte *buf, struct type *type, CORE_ADDR addr)
4478b372
JB
243{
244 if (TYPE_CODE (type) != TYPE_CODE_PTR
245 && TYPE_CODE (type) != TYPE_CODE_REF)
8e65ff28 246 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
247 _("store_typed_address: "
248 "type is not a pointer or reference"));
4478b372 249
50810684 250 gdbarch_address_to_pointer (get_type_arch (type), type, buf, addr);
4478b372
JB
251}
252
253
254
376c9600
AC
255/* Return a `value' with the contents of (virtual or cooked) register
256 REGNUM as found in the specified FRAME. The register's type is
9c5ea4d9 257 determined by register_type(). */
c906108c 258
3d6d86c6 259struct value *
376c9600 260value_of_register (int regnum, struct frame_info *frame)
c906108c 261{
e9e45075 262 struct gdbarch *gdbarch = get_frame_arch (frame);
3d6d86c6 263 struct value *reg_val;
c906108c 264
9564ee9f 265 /* User registers lie completely outside of the range of normal
0406ec40 266 registers. Catch them early so that the target never sees them. */
e9e45075
UW
267 if (regnum >= gdbarch_num_regs (gdbarch)
268 + gdbarch_num_pseudo_regs (gdbarch))
eb8bc282 269 return value_of_user_reg (regnum, frame);
0406ec40 270
d5b495b4
PA
271 reg_val = value_of_register_lazy (frame, regnum);
272 value_fetch_lazy (reg_val);
c906108c
SS
273 return reg_val;
274}
4478b372 275
9214ee5f
DJ
276/* Return a `value' with the contents of (virtual or cooked) register
277 REGNUM as found in the specified FRAME. The register's type is
278 determined by register_type(). The value is not fetched. */
279
280struct value *
281value_of_register_lazy (struct frame_info *frame, int regnum)
282{
283 struct gdbarch *gdbarch = get_frame_arch (frame);
284 struct value *reg_val;
285
286 gdb_assert (regnum < (gdbarch_num_regs (gdbarch)
287 + gdbarch_num_pseudo_regs (gdbarch)));
288
289 /* We should have a valid (i.e. non-sentinel) frame. */
290 gdb_assert (frame_id_p (get_frame_id (frame)));
291
41e8491f 292 reg_val = allocate_value_lazy (register_type (gdbarch, regnum));
9214ee5f
DJ
293 VALUE_LVAL (reg_val) = lval_register;
294 VALUE_REGNUM (reg_val) = regnum;
295 VALUE_FRAME_ID (reg_val) = get_frame_id (frame);
9214ee5f
DJ
296 return reg_val;
297}
298
4478b372
JB
299/* Given a pointer of type TYPE in target form in BUF, return the
300 address it represents. */
301CORE_ADDR
9898f801
UW
302unsigned_pointer_to_address (struct gdbarch *gdbarch,
303 struct type *type, const gdb_byte *buf)
4478b372 304{
e17a4113 305 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
bb9bcb69 306
e17a4113 307 return extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
4478b372
JB
308}
309
ac2e2ef7 310CORE_ADDR
9898f801
UW
311signed_pointer_to_address (struct gdbarch *gdbarch,
312 struct type *type, const gdb_byte *buf)
ac2e2ef7 313{
e17a4113 314 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
bb9bcb69 315
e17a4113 316 return extract_signed_integer (buf, TYPE_LENGTH (type), byte_order);
ac2e2ef7 317}
4478b372
JB
318
319/* Given an address, store it as a pointer of type TYPE in target
320 format in BUF. */
321void
9898f801
UW
322unsigned_address_to_pointer (struct gdbarch *gdbarch, struct type *type,
323 gdb_byte *buf, CORE_ADDR addr)
4478b372 324{
e17a4113 325 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
bb9bcb69 326
e17a4113 327 store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
4478b372
JB
328}
329
ac2e2ef7 330void
9898f801
UW
331address_to_signed_pointer (struct gdbarch *gdbarch, struct type *type,
332 gdb_byte *buf, CORE_ADDR addr)
ac2e2ef7 333{
e17a4113 334 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
bb9bcb69 335
e17a4113 336 store_signed_integer (buf, TYPE_LENGTH (type), byte_order, addr);
ac2e2ef7 337}
c906108c
SS
338\f
339/* Will calling read_var_value or locate_var_value on SYM end
340 up caring what frame it is being evaluated relative to? SYM must
341 be non-NULL. */
342int
fba45db2 343symbol_read_needs_frame (struct symbol *sym)
c906108c 344{
24d6c2a0
TT
345 if (SYMBOL_COMPUTED_OPS (sym) != NULL)
346 return SYMBOL_COMPUTED_OPS (sym)->read_needs_frame (sym);
347
c906108c
SS
348 switch (SYMBOL_CLASS (sym))
349 {
350 /* All cases listed explicitly so that gcc -Wall will detect it if
c5aa993b 351 we failed to consider one. */
4c2df51b 352 case LOC_COMPUTED:
24d6c2a0 353 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
4c2df51b 354
c906108c
SS
355 case LOC_REGISTER:
356 case LOC_ARG:
357 case LOC_REF_ARG:
c906108c
SS
358 case LOC_REGPARM_ADDR:
359 case LOC_LOCAL:
c906108c
SS
360 return 1;
361
362 case LOC_UNDEF:
363 case LOC_CONST:
364 case LOC_STATIC:
c906108c
SS
365 case LOC_TYPEDEF:
366
367 case LOC_LABEL:
368 /* Getting the address of a label can be done independently of the block,
c5aa993b
JM
369 even if some *uses* of that address wouldn't work so well without
370 the right frame. */
c906108c
SS
371
372 case LOC_BLOCK:
373 case LOC_CONST_BYTES:
374 case LOC_UNRESOLVED:
375 case LOC_OPTIMIZED_OUT:
376 return 0;
377 }
378 return 1;
379}
380
19630284
JB
381/* Private data to be used with minsym_lookup_iterator_cb. */
382
383struct minsym_lookup_data
384{
385 /* The name of the minimal symbol we are searching for. */
386 const char *name;
387
388 /* The field where the callback should store the minimal symbol
389 if found. It should be initialized to NULL before the search
390 is started. */
3b7344d5 391 struct bound_minimal_symbol result;
19630284
JB
392};
393
394/* A callback function for gdbarch_iterate_over_objfiles_in_search_order.
395 It searches by name for a minimal symbol within the given OBJFILE.
396 The arguments are passed via CB_DATA, which in reality is a pointer
397 to struct minsym_lookup_data. */
398
399static int
400minsym_lookup_iterator_cb (struct objfile *objfile, void *cb_data)
401{
402 struct minsym_lookup_data *data = (struct minsym_lookup_data *) cb_data;
403
3b7344d5 404 gdb_assert (data->result.minsym == NULL);
19630284
JB
405
406 data->result = lookup_minimal_symbol (data->name, NULL, objfile);
407
408 /* The iterator should stop iff a match was found. */
3b7344d5 409 return (data->result.minsym != NULL);
19630284
JB
410}
411
a5ee536b
JB
412/* A default implementation for the "la_read_var_value" hook in
413 the language vector which should work in most situations. */
c906108c 414
3d6d86c6 415struct value *
a5ee536b 416default_read_var_value (struct symbol *var, struct frame_info *frame)
c906108c 417{
52f0bd74 418 struct value *v;
c906108c
SS
419 struct type *type = SYMBOL_TYPE (var);
420 CORE_ADDR addr;
c906108c 421
41e8491f
JK
422 /* Call check_typedef on our type to make sure that, if TYPE is
423 a TYPE_CODE_TYPEDEF, its length is set to the length of the target type
424 instead of zero. However, we do not replace the typedef type by the
425 target type, because we want to keep the typedef in order to be able to
426 set the returned value type description correctly. */
427 check_typedef (type);
c906108c 428
61212c0f
UW
429 if (symbol_read_needs_frame (var))
430 gdb_assert (frame);
c906108c 431
24d6c2a0
TT
432 if (SYMBOL_COMPUTED_OPS (var) != NULL)
433 return SYMBOL_COMPUTED_OPS (var)->read_variable (var, frame);
434
c906108c
SS
435 switch (SYMBOL_CLASS (var))
436 {
437 case LOC_CONST:
1612e0c0
SA
438 if (is_dynamic_type (type))
439 {
440 /* Value is a constant byte-sequence and needs no memory access. */
441 type = resolve_dynamic_type (type, /* Unused address. */ 0);
442 }
443 /* Put the constant back in target format. */
41e8491f 444 v = allocate_value (type);
744a8059 445 store_signed_integer (value_contents_raw (v), TYPE_LENGTH (type),
e17a4113 446 gdbarch_byte_order (get_type_arch (type)),
c906108c
SS
447 (LONGEST) SYMBOL_VALUE (var));
448 VALUE_LVAL (v) = not_lval;
449 return v;
450
451 case LOC_LABEL:
452 /* Put the constant back in target format. */
41e8491f 453 v = allocate_value (type);
c906108c 454 if (overlay_debugging)
4478b372
JB
455 {
456 CORE_ADDR addr
457 = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
08be3fe3 458 SYMBOL_OBJ_SECTION (symbol_objfile (var),
e27d198c 459 var));
bb9bcb69 460
990a07ab 461 store_typed_address (value_contents_raw (v), type, addr);
4478b372 462 }
c906108c 463 else
990a07ab 464 store_typed_address (value_contents_raw (v), type,
4478b372 465 SYMBOL_VALUE_ADDRESS (var));
c906108c
SS
466 VALUE_LVAL (v) = not_lval;
467 return v;
468
469 case LOC_CONST_BYTES:
1612e0c0
SA
470 if (is_dynamic_type (type))
471 {
472 /* Value is a constant byte-sequence and needs no memory access. */
473 type = resolve_dynamic_type (type, /* Unused address. */ 0);
474 }
41e8491f 475 v = allocate_value (type);
744a8059
SP
476 memcpy (value_contents_raw (v), SYMBOL_VALUE_BYTES (var),
477 TYPE_LENGTH (type));
bb9bcb69
MS
478 VALUE_LVAL (v) = not_lval;
479 return v;
c906108c
SS
480
481 case LOC_STATIC:
482 if (overlay_debugging)
483 addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
08be3fe3 484 SYMBOL_OBJ_SECTION (symbol_objfile (var),
e27d198c 485 var));
c906108c
SS
486 else
487 addr = SYMBOL_VALUE_ADDRESS (var);
488 break;
489
c906108c 490 case LOC_ARG:
da62e633 491 addr = get_frame_args_address (frame);
c906108c 492 if (!addr)
8afd712c
JK
493 error (_("Unknown argument list address for `%s'."),
494 SYMBOL_PRINT_NAME (var));
c906108c
SS
495 addr += SYMBOL_VALUE (var);
496 break;
497
498 case LOC_REF_ARG:
f76febae
AC
499 {
500 struct value *ref;
501 CORE_ADDR argref;
bb9bcb69 502
da62e633 503 argref = get_frame_args_address (frame);
f76febae 504 if (!argref)
8afd712c
JK
505 error (_("Unknown argument list address for `%s'."),
506 SYMBOL_PRINT_NAME (var));
f76febae 507 argref += SYMBOL_VALUE (var);
00a4c844 508 ref = value_at (lookup_pointer_type (type), argref);
1aa20aa8 509 addr = value_as_address (ref);
f76febae
AC
510 break;
511 }
c906108c
SS
512
513 case LOC_LOCAL:
da62e633 514 addr = get_frame_locals_address (frame);
c906108c
SS
515 addr += SYMBOL_VALUE (var);
516 break;
517
c906108c 518 case LOC_TYPEDEF:
8afd712c
JK
519 error (_("Cannot look up value of a typedef `%s'."),
520 SYMBOL_PRINT_NAME (var));
c906108c
SS
521 break;
522
523 case LOC_BLOCK:
524 if (overlay_debugging)
41e8491f 525 addr = symbol_overlayed_address
08be3fe3
DE
526 (BLOCK_START (SYMBOL_BLOCK_VALUE (var)),
527 SYMBOL_OBJ_SECTION (symbol_objfile (var), var));
c906108c 528 else
41e8491f
JK
529 addr = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
530 break;
c906108c
SS
531
532 case LOC_REGISTER:
c906108c
SS
533 case LOC_REGPARM_ADDR:
534 {
768a979c
UW
535 int regno = SYMBOL_REGISTER_OPS (var)
536 ->register_number (var, get_frame_arch (frame));
3d6d86c6 537 struct value *regval;
c906108c 538
c906108c
SS
539 if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
540 {
541 regval = value_from_register (lookup_pointer_type (type),
c5aa993b 542 regno,
c906108c
SS
543 frame);
544
545 if (regval == NULL)
8afd712c
JK
546 error (_("Value of register variable not available for `%s'."),
547 SYMBOL_PRINT_NAME (var));
c906108c 548
1aa20aa8 549 addr = value_as_address (regval);
c906108c
SS
550 }
551 else
552 {
553 regval = value_from_register (type, regno, frame);
554
555 if (regval == NULL)
8afd712c
JK
556 error (_("Value of register variable not available for `%s'."),
557 SYMBOL_PRINT_NAME (var));
c906108c
SS
558 return regval;
559 }
560 }
561 break;
562
4c2df51b 563 case LOC_COMPUTED:
24d6c2a0 564 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
4c2df51b 565
c906108c
SS
566 case LOC_UNRESOLVED:
567 {
19630284 568 struct minsym_lookup_data lookup_data;
c906108c 569 struct minimal_symbol *msym;
e0740f77 570 struct obj_section *obj_section;
c906108c 571
19630284
JB
572 memset (&lookup_data, 0, sizeof (lookup_data));
573 lookup_data.name = SYMBOL_LINKAGE_NAME (var);
574
575 gdbarch_iterate_over_objfiles_in_search_order
08be3fe3 576 (symbol_arch (var),
19630284 577 minsym_lookup_iterator_cb, &lookup_data,
08be3fe3 578 symbol_objfile (var));
3b7344d5 579 msym = lookup_data.result.minsym;
19630284 580
c906108c 581 if (msym == NULL)
8afd712c 582 error (_("No global symbol \"%s\"."), SYMBOL_LINKAGE_NAME (var));
c906108c 583 if (overlay_debugging)
77e371c0 584 addr = symbol_overlayed_address (BMSYMBOL_VALUE_ADDRESS (lookup_data.result),
3b7344d5 585 MSYMBOL_OBJ_SECTION (lookup_data.result.objfile,
efd66ac6 586 msym));
c906108c 587 else
77e371c0 588 addr = BMSYMBOL_VALUE_ADDRESS (lookup_data.result);
e0740f77 589
3b7344d5 590 obj_section = MSYMBOL_OBJ_SECTION (lookup_data.result.objfile, msym);
e0740f77
JK
591 if (obj_section
592 && (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
593 addr = target_translate_tls_address (obj_section->objfile, addr);
c906108c
SS
594 }
595 break;
596
597 case LOC_OPTIMIZED_OUT:
a7035dbb 598 return allocate_optimized_out_value (type);
c906108c
SS
599
600 default:
8afd712c
JK
601 error (_("Cannot look up value of a botched symbol `%s'."),
602 SYMBOL_PRINT_NAME (var));
c906108c
SS
603 break;
604 }
605
08039c9e 606 v = value_at_lazy (type, addr);
c906108c
SS
607 return v;
608}
609
a5ee536b
JB
610/* Calls VAR's language la_read_var_value hook with the given arguments. */
611
612struct value *
613read_var_value (struct symbol *var, struct frame_info *frame)
614{
615 const struct language_defn *lang = language_def (SYMBOL_LANGUAGE (var));
616
617 gdb_assert (lang != NULL);
618 gdb_assert (lang->la_read_var_value != NULL);
619
620 return lang->la_read_var_value (var, frame);
621}
622
9acbedc0
UW
623/* Install default attributes for register values. */
624
625struct value *
2ed3c037
UW
626default_value_from_register (struct gdbarch *gdbarch, struct type *type,
627 int regnum, struct frame_id frame_id)
9acbedc0 628{
9acbedc0
UW
629 int len = TYPE_LENGTH (type);
630 struct value *value = allocate_value (type);
631
632 VALUE_LVAL (value) = lval_register;
2ed3c037 633 VALUE_FRAME_ID (value) = frame_id;
9acbedc0
UW
634 VALUE_REGNUM (value) = regnum;
635
636 /* Any structure stored in more than one register will always be
637 an integral number of registers. Otherwise, you need to do
638 some fiddling with the last register copied here for little
639 endian machines. */
e9e45075 640 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
9acbedc0
UW
641 && len < register_size (gdbarch, regnum))
642 /* Big-endian, and we want less than full size. */
643 set_value_offset (value, register_size (gdbarch, regnum) - len);
644 else
645 set_value_offset (value, 0);
646
647 return value;
648}
649
b56d6f31
JB
650/* VALUE must be an lval_register value. If regnum is the value's
651 associated register number, and len the length of the values type,
652 read one or more registers in FRAME, starting with register REGNUM,
2603f7ee
AB
653 until we've read LEN bytes.
654
655 If any of the registers we try to read are optimized out, then mark the
656 complete resulting value as optimized out. */
b56d6f31
JB
657
658void
659read_frame_register_value (struct value *value, struct frame_info *frame)
660{
01efb936 661 struct gdbarch *gdbarch = get_frame_arch (frame);
b56d6f31 662 int offset = 0;
01efb936 663 int reg_offset = value_offset (value);
b56d6f31 664 int regnum = VALUE_REGNUM (value);
01efb936 665 int len = TYPE_LENGTH (check_typedef (value_type (value)));
b56d6f31
JB
666
667 gdb_assert (VALUE_LVAL (value) == lval_register);
668
01efb936
UW
669 /* Skip registers wholly inside of REG_OFFSET. */
670 while (reg_offset >= register_size (gdbarch, regnum))
671 {
672 reg_offset -= register_size (gdbarch, regnum);
673 regnum++;
674 }
675
676 /* Copy the data. */
677 while (len > 0)
b56d6f31
JB
678 {
679 struct value *regval = get_frame_register_value (frame, regnum);
01efb936 680 int reg_len = TYPE_LENGTH (value_type (regval)) - reg_offset;
b56d6f31 681
22355c90
JB
682 /* If the register length is larger than the number of bytes
683 remaining to copy, then only copy the appropriate bytes. */
01efb936
UW
684 if (reg_len > len)
685 reg_len = len;
22355c90 686
01efb936 687 value_contents_copy (value, offset, regval, reg_offset, reg_len);
b56d6f31
JB
688
689 offset += reg_len;
01efb936
UW
690 len -= reg_len;
691 reg_offset = 0;
b56d6f31
JB
692 regnum++;
693 }
694}
695
00fa51f6 696/* Return a value of type TYPE, stored in register REGNUM, in frame FRAME. */
c906108c 697
3d6d86c6 698struct value *
fba45db2 699value_from_register (struct type *type, int regnum, struct frame_info *frame)
c906108c 700{
ff2e87ac 701 struct gdbarch *gdbarch = get_frame_arch (frame);
9acbedc0
UW
702 struct type *type1 = check_typedef (type);
703 struct value *v;
704
e9e45075 705 if (gdbarch_convert_register_p (gdbarch, regnum, type1))
ff2e87ac 706 {
3543a589
TT
707 int optim, unavail, ok;
708
ff2e87ac
AC
709 /* The ISA/ABI need to something weird when obtaining the
710 specified value from this register. It might need to
711 re-order non-adjacent, starting with REGNUM (see MIPS and
712 i386). It might need to convert the [float] register into
713 the corresponding [integer] type (see Alpha). The assumption
c1afe53d 714 is that gdbarch_register_to_value populates the entire value
ff2e87ac 715 including the location. */
9acbedc0
UW
716 v = allocate_value (type);
717 VALUE_LVAL (v) = lval_register;
718 VALUE_FRAME_ID (v) = get_frame_id (frame);
719 VALUE_REGNUM (v) = regnum;
8dccd430
PA
720 ok = gdbarch_register_to_value (gdbarch, frame, regnum, type1,
721 value_contents_raw (v), &optim,
722 &unavail);
3543a589
TT
723
724 if (!ok)
725 {
726 if (optim)
9a0dc9e3 727 mark_value_bytes_optimized_out (v, 0, TYPE_LENGTH (type));
3543a589
TT
728 if (unavail)
729 mark_value_bytes_unavailable (v, 0, TYPE_LENGTH (type));
730 }
ff2e87ac
AC
731 }
732 else
c906108c 733 {
9acbedc0 734 /* Construct the value. */
2ed3c037
UW
735 v = gdbarch_value_from_register (gdbarch, type,
736 regnum, get_frame_id (frame));
00fa51f6
UW
737
738 /* Get the data. */
b56d6f31 739 read_frame_register_value (v, frame);
c906108c 740 }
8dccd430 741
c906108c
SS
742 return v;
743}
ff2e87ac 744
2ed3c037
UW
745/* Return contents of register REGNUM in frame FRAME as address.
746 Will abort if register value is not available. */
0b2b0195
UW
747
748CORE_ADDR
2ed3c037 749address_from_register (int regnum, struct frame_info *frame)
0b2b0195 750{
2ed3c037
UW
751 struct gdbarch *gdbarch = get_frame_arch (frame);
752 struct type *type = builtin_type (gdbarch)->builtin_data_ptr;
0b2b0195
UW
753 struct value *value;
754 CORE_ADDR result;
755
2ed3c037
UW
756 /* This routine may be called during early unwinding, at a time
757 where the ID of FRAME is not yet known. Calling value_from_register
758 would therefore abort in get_frame_id. However, since we only need
759 a temporary value that is never used as lvalue, we actually do not
760 really need to set its VALUE_FRAME_ID. Therefore, we re-implement
eeef931a 761 the core of value_from_register, but use the null_frame_id. */
2ed3c037 762
eeef931a
UW
763 /* Some targets require a special conversion routine even for plain
764 pointer types. Avoid constructing a value object in those cases. */
765 if (gdbarch_convert_register_p (gdbarch, regnum, type))
766 {
767 gdb_byte *buf = alloca (TYPE_LENGTH (type));
768 int optim, unavail, ok;
769
770 ok = gdbarch_register_to_value (gdbarch, frame, regnum, type,
771 buf, &optim, &unavail);
772 if (!ok)
773 {
774 /* This function is used while computing a location expression.
775 Complain about the value being optimized out, rather than
776 letting value_as_address complain about some random register
777 the expression depends on not being saved. */
778 error_value_optimized_out ();
779 }
780
781 return unpack_long (type, buf);
782 }
2ed3c037
UW
783
784 value = gdbarch_value_from_register (gdbarch, type, regnum, null_frame_id);
785 read_frame_register_value (value, frame);
0b2b0195 786
901461f8
PA
787 if (value_optimized_out (value))
788 {
789 /* This function is used while computing a location expression.
790 Complain about the value being optimized out, rather than
791 letting value_as_address complain about some random register
792 the expression depends on not being saved. */
793 error_value_optimized_out ();
794 }
795
0b2b0195
UW
796 result = value_as_address (value);
797 release_value (value);
798 value_free (value);
799
800 return result;
801}
b56d6f31 802