1 /* Low level packing and unpacking of values for GDB, the GNU Debugger.
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005 Free
5 Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 #include "gdb_string.h"
37 #include "gdb_assert.h"
41 /* Prototypes for exported functions. */
43 void _initialize_values (void);
45 /* Prototypes for local functions. */
47 static void show_values (char *, int);
49 static void show_convenience (char *, int);
52 /* The value-history records all the values printed
53 by print commands during this session. Each chunk
54 records 60 consecutive values. The first chunk on
55 the chain records the most recent values.
56 The total number of values is in value_history_count. */
58 #define VALUE_HISTORY_CHUNK 60
60 struct value_history_chunk
62 struct value_history_chunk
*next
;
63 struct value
*values
[VALUE_HISTORY_CHUNK
];
66 /* Chain of chunks now in use. */
68 static struct value_history_chunk
*value_history_chain
;
70 static int value_history_count
; /* Abs number of last entry stored */
72 /* List of all value objects currently allocated
73 (except for those released by calls to release_value)
74 This is so they can be freed after each command. */
76 static struct value
*all_values
;
78 /* Allocate a value that has the correct length for type TYPE. */
81 allocate_value (struct type
*type
)
84 struct type
*atype
= check_typedef (type
);
86 val
= (struct value
*) xzalloc (sizeof (struct value
) + TYPE_LENGTH (atype
));
87 val
->next
= all_values
;
90 val
->enclosing_type
= type
;
91 VALUE_LVAL (val
) = not_lval
;
92 VALUE_ADDRESS (val
) = 0;
93 VALUE_FRAME_ID (val
) = null_frame_id
;
97 VALUE_REGNUM (val
) = -1;
99 val
->optimized_out
= 0;
100 val
->embedded_offset
= 0;
101 val
->pointed_to_offset
= 0;
106 /* Allocate a value that has the correct length
107 for COUNT repetitions type TYPE. */
110 allocate_repeat_value (struct type
*type
, int count
)
112 int low_bound
= current_language
->string_lower_bound
; /* ??? */
113 /* FIXME-type-allocation: need a way to free this type when we are
115 struct type
*range_type
116 = create_range_type ((struct type
*) NULL
, builtin_type_int
,
117 low_bound
, count
+ low_bound
- 1);
118 /* FIXME-type-allocation: need a way to free this type when we are
120 return allocate_value (create_array_type ((struct type
*) NULL
,
124 /* Accessor methods. */
127 value_type (struct value
*value
)
133 value_offset (struct value
*value
)
135 return value
->offset
;
139 value_bitpos (struct value
*value
)
141 return value
->bitpos
;
145 value_bitsize (struct value
*value
)
147 return value
->bitsize
;
151 value_contents_raw (struct value
*value
)
153 return value
->aligner
.contents
+ value
->embedded_offset
;
157 value_contents_all_raw (struct value
*value
)
159 return value
->aligner
.contents
;
163 value_enclosing_type (struct value
*value
)
165 return value
->enclosing_type
;
169 value_contents_all (struct value
*value
)
172 value_fetch_lazy (value
);
173 return value
->aligner
.contents
;
177 value_lazy (struct value
*value
)
183 set_value_lazy (struct value
*value
, int val
)
189 value_contents (struct value
*value
)
191 return value_contents_writeable (value
);
195 value_contents_writeable (struct value
*value
)
198 value_fetch_lazy (value
);
199 return value
->aligner
.contents
;
203 value_optimized_out (struct value
*value
)
205 return value
->optimized_out
;
209 set_value_optimized_out (struct value
*value
, int val
)
211 value
->optimized_out
= val
;
215 value_embedded_offset (struct value
*value
)
217 return value
->embedded_offset
;
221 set_value_embedded_offset (struct value
*value
, int val
)
223 value
->embedded_offset
= val
;
227 value_pointed_to_offset (struct value
*value
)
229 return value
->pointed_to_offset
;
233 set_value_pointed_to_offset (struct value
*value
, int val
)
235 value
->pointed_to_offset
= val
;
238 /* Return a mark in the value chain. All values allocated after the
239 mark is obtained (except for those released) are subject to being freed
240 if a subsequent value_free_to_mark is passed the mark. */
247 /* Free all values allocated since MARK was obtained by value_mark
248 (except for those released). */
250 value_free_to_mark (struct value
*mark
)
255 for (val
= all_values
; val
&& val
!= mark
; val
= next
)
263 /* Free all the values that have been allocated (except for those released).
264 Called after each command, successful or not. */
267 free_all_values (void)
272 for (val
= all_values
; val
; val
= next
)
281 /* Remove VAL from the chain all_values
282 so it will not be freed automatically. */
285 release_value (struct value
*val
)
289 if (all_values
== val
)
291 all_values
= val
->next
;
295 for (v
= all_values
; v
; v
= v
->next
)
305 /* Release all values up to mark */
307 value_release_to_mark (struct value
*mark
)
312 for (val
= next
= all_values
; next
; next
= next
->next
)
313 if (next
->next
== mark
)
315 all_values
= next
->next
;
323 /* Return a copy of the value ARG.
324 It contains the same contents, for same memory address,
325 but it's a different block of storage. */
328 value_copy (struct value
*arg
)
330 struct type
*encl_type
= value_enclosing_type (arg
);
331 struct value
*val
= allocate_value (encl_type
);
332 val
->type
= arg
->type
;
333 VALUE_LVAL (val
) = VALUE_LVAL (arg
);
334 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg
);
335 val
->offset
= arg
->offset
;
336 val
->bitpos
= arg
->bitpos
;
337 val
->bitsize
= arg
->bitsize
;
338 VALUE_FRAME_ID (val
) = VALUE_FRAME_ID (arg
);
339 VALUE_REGNUM (val
) = VALUE_REGNUM (arg
);
340 val
->lazy
= arg
->lazy
;
341 val
->optimized_out
= arg
->optimized_out
;
342 val
->embedded_offset
= value_embedded_offset (arg
);
343 val
->pointed_to_offset
= arg
->pointed_to_offset
;
344 val
->modifiable
= arg
->modifiable
;
345 if (!value_lazy (val
))
347 memcpy (value_contents_all_raw (val
), value_contents_all_raw (arg
),
348 TYPE_LENGTH (value_enclosing_type (arg
)));
354 /* Access to the value history. */
356 /* Record a new value in the value history.
357 Returns the absolute history index of the entry.
358 Result of -1 indicates the value was not saved; otherwise it is the
359 value history index of this new item. */
362 record_latest_value (struct value
*val
)
366 /* We don't want this value to have anything to do with the inferior anymore.
367 In particular, "set $1 = 50" should not affect the variable from which
368 the value was taken, and fast watchpoints should be able to assume that
369 a value on the value history never changes. */
370 if (value_lazy (val
))
371 value_fetch_lazy (val
);
372 /* We preserve VALUE_LVAL so that the user can find out where it was fetched
373 from. This is a bit dubious, because then *&$1 does not just return $1
374 but the current contents of that location. c'est la vie... */
378 /* Here we treat value_history_count as origin-zero
379 and applying to the value being stored now. */
381 i
= value_history_count
% VALUE_HISTORY_CHUNK
;
384 struct value_history_chunk
*new
385 = (struct value_history_chunk
*)
386 xmalloc (sizeof (struct value_history_chunk
));
387 memset (new->values
, 0, sizeof new->values
);
388 new->next
= value_history_chain
;
389 value_history_chain
= new;
392 value_history_chain
->values
[i
] = val
;
394 /* Now we regard value_history_count as origin-one
395 and applying to the value just stored. */
397 return ++value_history_count
;
400 /* Return a copy of the value in the history with sequence number NUM. */
403 access_value_history (int num
)
405 struct value_history_chunk
*chunk
;
410 absnum
+= value_history_count
;
415 error ("The history is empty.");
417 error ("There is only one value in the history.");
419 error ("History does not go back to $$%d.", -num
);
421 if (absnum
> value_history_count
)
422 error ("History has not yet reached $%d.", absnum
);
426 /* Now absnum is always absolute and origin zero. */
428 chunk
= value_history_chain
;
429 for (i
= (value_history_count
- 1) / VALUE_HISTORY_CHUNK
- absnum
/ VALUE_HISTORY_CHUNK
;
433 return value_copy (chunk
->values
[absnum
% VALUE_HISTORY_CHUNK
]);
436 /* Clear the value history entirely.
437 Must be done when new symbol tables are loaded,
438 because the type pointers become invalid. */
441 clear_value_history (void)
443 struct value_history_chunk
*next
;
447 while (value_history_chain
)
449 for (i
= 0; i
< VALUE_HISTORY_CHUNK
; i
++)
450 if ((val
= value_history_chain
->values
[i
]) != NULL
)
452 next
= value_history_chain
->next
;
453 xfree (value_history_chain
);
454 value_history_chain
= next
;
456 value_history_count
= 0;
460 show_values (char *num_exp
, int from_tty
)
468 /* "info history +" should print from the stored position.
469 "info history <exp>" should print around value number <exp>. */
470 if (num_exp
[0] != '+' || num_exp
[1] != '\0')
471 num
= parse_and_eval_long (num_exp
) - 5;
475 /* "info history" means print the last 10 values. */
476 num
= value_history_count
- 9;
482 for (i
= num
; i
< num
+ 10 && i
<= value_history_count
; i
++)
484 val
= access_value_history (i
);
485 printf_filtered ("$%d = ", i
);
486 value_print (val
, gdb_stdout
, 0, Val_pretty_default
);
487 printf_filtered ("\n");
490 /* The next "info history +" should start after what we just printed. */
493 /* Hitting just return after this command should do the same thing as
494 "info history +". If num_exp is null, this is unnecessary, since
495 "info history +" is not useful after "info history". */
496 if (from_tty
&& num_exp
)
503 /* Internal variables. These are variables within the debugger
504 that hold values assigned by debugger commands.
505 The user refers to them with a '$' prefix
506 that does not appear in the variable names stored internally. */
508 static struct internalvar
*internalvars
;
510 /* Look up an internal variable with name NAME. NAME should not
511 normally include a dollar sign.
513 If the specified internal variable does not exist,
514 one is created, with a void value. */
517 lookup_internalvar (char *name
)
519 struct internalvar
*var
;
521 for (var
= internalvars
; var
; var
= var
->next
)
522 if (strcmp (var
->name
, name
) == 0)
525 var
= (struct internalvar
*) xmalloc (sizeof (struct internalvar
));
526 var
->name
= concat (name
, NULL
);
527 var
->value
= allocate_value (builtin_type_void
);
528 release_value (var
->value
);
529 var
->next
= internalvars
;
535 value_of_internalvar (struct internalvar
*var
)
539 val
= value_copy (var
->value
);
540 if (value_lazy (val
))
541 value_fetch_lazy (val
);
542 VALUE_LVAL (val
) = lval_internalvar
;
543 VALUE_INTERNALVAR (val
) = var
;
548 set_internalvar_component (struct internalvar
*var
, int offset
, int bitpos
,
549 int bitsize
, struct value
*newval
)
551 bfd_byte
*addr
= value_contents_writeable (var
->value
) + offset
;
554 modify_field (addr
, value_as_long (newval
),
557 memcpy (addr
, value_contents (newval
), TYPE_LENGTH (value_type (newval
)));
561 set_internalvar (struct internalvar
*var
, struct value
*val
)
563 struct value
*newval
;
565 newval
= value_copy (val
);
566 newval
->modifiable
= 1;
568 /* Force the value to be fetched from the target now, to avoid problems
569 later when this internalvar is referenced and the target is gone or
571 if (value_lazy (newval
))
572 value_fetch_lazy (newval
);
574 /* Begin code which must not call error(). If var->value points to
575 something free'd, an error() obviously leaves a dangling pointer.
576 But we also get a danling pointer if var->value points to
577 something in the value chain (i.e., before release_value is
578 called), because after the error free_all_values will get called before
582 release_value (newval
);
583 /* End code which must not call error(). */
587 internalvar_name (struct internalvar
*var
)
592 /* Free all internalvars. Done when new symtabs are loaded,
593 because that makes the values invalid. */
596 clear_internalvars (void)
598 struct internalvar
*var
;
603 internalvars
= var
->next
;
611 show_convenience (char *ignore
, int from_tty
)
613 struct internalvar
*var
;
616 for (var
= internalvars
; var
; var
= var
->next
)
622 printf_filtered ("$%s = ", var
->name
);
623 value_print (var
->value
, gdb_stdout
, 0, Val_pretty_default
);
624 printf_filtered ("\n");
627 printf_unfiltered ("No debugger convenience variables now defined.\n\
628 Convenience variables have names starting with \"$\";\n\
629 use \"set\" as in \"set $foo = 5\" to define them.\n");
632 /* Extract a value as a C number (either long or double).
633 Knows how to convert fixed values to double, or
634 floating values to long.
635 Does not deallocate the value. */
638 value_as_long (struct value
*val
)
640 /* This coerces arrays and functions, which is necessary (e.g.
641 in disassemble_command). It also dereferences references, which
642 I suspect is the most logical thing to do. */
643 val
= coerce_array (val
);
644 return unpack_long (value_type (val
), value_contents (val
));
648 value_as_double (struct value
*val
)
653 foo
= unpack_double (value_type (val
), value_contents (val
), &inv
);
655 error ("Invalid floating value found in program.");
658 /* Extract a value as a C pointer. Does not deallocate the value.
659 Note that val's type may not actually be a pointer; value_as_long
660 handles all the cases. */
662 value_as_address (struct value
*val
)
664 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
665 whether we want this to be true eventually. */
667 /* ADDR_BITS_REMOVE is wrong if we are being called for a
668 non-address (e.g. argument to "signal", "info break", etc.), or
669 for pointers to char, in which the low bits *are* significant. */
670 return ADDR_BITS_REMOVE (value_as_long (val
));
673 /* There are several targets (IA-64, PowerPC, and others) which
674 don't represent pointers to functions as simply the address of
675 the function's entry point. For example, on the IA-64, a
676 function pointer points to a two-word descriptor, generated by
677 the linker, which contains the function's entry point, and the
678 value the IA-64 "global pointer" register should have --- to
679 support position-independent code. The linker generates
680 descriptors only for those functions whose addresses are taken.
682 On such targets, it's difficult for GDB to convert an arbitrary
683 function address into a function pointer; it has to either find
684 an existing descriptor for that function, or call malloc and
685 build its own. On some targets, it is impossible for GDB to
686 build a descriptor at all: the descriptor must contain a jump
687 instruction; data memory cannot be executed; and code memory
690 Upon entry to this function, if VAL is a value of type `function'
691 (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then
692 VALUE_ADDRESS (val) is the address of the function. This is what
693 you'll get if you evaluate an expression like `main'. The call
694 to COERCE_ARRAY below actually does all the usual unary
695 conversions, which includes converting values of type `function'
696 to `pointer to function'. This is the challenging conversion
697 discussed above. Then, `unpack_long' will convert that pointer
698 back into an address.
700 So, suppose the user types `disassemble foo' on an architecture
701 with a strange function pointer representation, on which GDB
702 cannot build its own descriptors, and suppose further that `foo'
703 has no linker-built descriptor. The address->pointer conversion
704 will signal an error and prevent the command from running, even
705 though the next step would have been to convert the pointer
706 directly back into the same address.
708 The following shortcut avoids this whole mess. If VAL is a
709 function, just return its address directly. */
710 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_FUNC
711 || TYPE_CODE (value_type (val
)) == TYPE_CODE_METHOD
)
712 return VALUE_ADDRESS (val
);
714 val
= coerce_array (val
);
716 /* Some architectures (e.g. Harvard), map instruction and data
717 addresses onto a single large unified address space. For
718 instance: An architecture may consider a large integer in the
719 range 0x10000000 .. 0x1000ffff to already represent a data
720 addresses (hence not need a pointer to address conversion) while
721 a small integer would still need to be converted integer to
722 pointer to address. Just assume such architectures handle all
723 integer conversions in a single function. */
727 I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we
728 must admonish GDB hackers to make sure its behavior matches the
729 compiler's, whenever possible.
731 In general, I think GDB should evaluate expressions the same way
732 the compiler does. When the user copies an expression out of
733 their source code and hands it to a `print' command, they should
734 get the same value the compiler would have computed. Any
735 deviation from this rule can cause major confusion and annoyance,
736 and needs to be justified carefully. In other words, GDB doesn't
737 really have the freedom to do these conversions in clever and
740 AndrewC pointed out that users aren't complaining about how GDB
741 casts integers to pointers; they are complaining that they can't
742 take an address from a disassembly listing and give it to `x/i'.
743 This is certainly important.
745 Adding an architecture method like integer_to_address() certainly
746 makes it possible for GDB to "get it right" in all circumstances
747 --- the target has complete control over how things get done, so
748 people can Do The Right Thing for their target without breaking
749 anyone else. The standard doesn't specify how integers get
750 converted to pointers; usually, the ABI doesn't either, but
751 ABI-specific code is a more reasonable place to handle it. */
753 if (TYPE_CODE (value_type (val
)) != TYPE_CODE_PTR
754 && TYPE_CODE (value_type (val
)) != TYPE_CODE_REF
755 && gdbarch_integer_to_address_p (current_gdbarch
))
756 return gdbarch_integer_to_address (current_gdbarch
, value_type (val
),
757 value_contents (val
));
759 return unpack_long (value_type (val
), value_contents (val
));
763 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
764 as a long, or as a double, assuming the raw data is described
765 by type TYPE. Knows how to convert different sizes of values
766 and can convert between fixed and floating point. We don't assume
767 any alignment for the raw data. Return value is in host byte order.
769 If you want functions and arrays to be coerced to pointers, and
770 references to be dereferenced, call value_as_long() instead.
772 C++: It is assumed that the front-end has taken care of
773 all matters concerning pointers to members. A pointer
774 to member which reaches here is considered to be equivalent
775 to an INT (or some size). After all, it is only an offset. */
778 unpack_long (struct type
*type
, const char *valaddr
)
780 enum type_code code
= TYPE_CODE (type
);
781 int len
= TYPE_LENGTH (type
);
782 int nosign
= TYPE_UNSIGNED (type
);
784 if (current_language
->la_language
== language_scm
785 && is_scmvalue_type (type
))
786 return scm_unpack (type
, valaddr
, TYPE_CODE_INT
);
790 case TYPE_CODE_TYPEDEF
:
791 return unpack_long (check_typedef (type
), valaddr
);
796 case TYPE_CODE_RANGE
:
798 return extract_unsigned_integer (valaddr
, len
);
800 return extract_signed_integer (valaddr
, len
);
803 return extract_typed_floating (valaddr
, type
);
807 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
808 whether we want this to be true eventually. */
809 return extract_typed_address (valaddr
, type
);
811 case TYPE_CODE_MEMBER
:
812 error ("not implemented: member types in unpack_long");
815 error ("Value can't be converted to integer.");
817 return 0; /* Placate lint. */
820 /* Return a double value from the specified type and address.
821 INVP points to an int which is set to 0 for valid value,
822 1 for invalid value (bad float format). In either case,
823 the returned double is OK to use. Argument is in target
824 format, result is in host format. */
827 unpack_double (struct type
*type
, const char *valaddr
, int *invp
)
833 *invp
= 0; /* Assume valid. */
834 CHECK_TYPEDEF (type
);
835 code
= TYPE_CODE (type
);
836 len
= TYPE_LENGTH (type
);
837 nosign
= TYPE_UNSIGNED (type
);
838 if (code
== TYPE_CODE_FLT
)
840 /* NOTE: cagney/2002-02-19: There was a test here to see if the
841 floating-point value was valid (using the macro
842 INVALID_FLOAT). That test/macro have been removed.
844 It turns out that only the VAX defined this macro and then
845 only in a non-portable way. Fixing the portability problem
846 wouldn't help since the VAX floating-point code is also badly
847 bit-rotten. The target needs to add definitions for the
848 methods TARGET_FLOAT_FORMAT and TARGET_DOUBLE_FORMAT - these
849 exactly describe the target floating-point format. The
850 problem here is that the corresponding floatformat_vax_f and
851 floatformat_vax_d values these methods should be set to are
852 also not defined either. Oops!
854 Hopefully someone will add both the missing floatformat
855 definitions and the new cases for floatformat_is_valid (). */
857 if (!floatformat_is_valid (floatformat_from_type (type
), valaddr
))
863 return extract_typed_floating (valaddr
, type
);
867 /* Unsigned -- be sure we compensate for signed LONGEST. */
868 return (ULONGEST
) unpack_long (type
, valaddr
);
872 /* Signed -- we are OK with unpack_long. */
873 return unpack_long (type
, valaddr
);
877 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
878 as a CORE_ADDR, assuming the raw data is described by type TYPE.
879 We don't assume any alignment for the raw data. Return value is in
882 If you want functions and arrays to be coerced to pointers, and
883 references to be dereferenced, call value_as_address() instead.
885 C++: It is assumed that the front-end has taken care of
886 all matters concerning pointers to members. A pointer
887 to member which reaches here is considered to be equivalent
888 to an INT (or some size). After all, it is only an offset. */
891 unpack_pointer (struct type
*type
, const char *valaddr
)
893 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
894 whether we want this to be true eventually. */
895 return unpack_long (type
, valaddr
);
899 /* Get the value of the FIELDN'th field (which must be static) of
900 TYPE. Return NULL if the field doesn't exist or has been
904 value_static_field (struct type
*type
, int fieldno
)
906 struct value
*retval
;
908 if (TYPE_FIELD_STATIC_HAS_ADDR (type
, fieldno
))
910 retval
= value_at (TYPE_FIELD_TYPE (type
, fieldno
),
911 TYPE_FIELD_STATIC_PHYSADDR (type
, fieldno
));
915 char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
916 struct symbol
*sym
= lookup_symbol (phys_name
, 0, VAR_DOMAIN
, 0, NULL
);
919 /* With some compilers, e.g. HP aCC, static data members are reported
920 as non-debuggable symbols */
921 struct minimal_symbol
*msym
= lookup_minimal_symbol (phys_name
, NULL
, NULL
);
926 retval
= value_at (TYPE_FIELD_TYPE (type
, fieldno
),
927 SYMBOL_VALUE_ADDRESS (msym
));
932 /* SYM should never have a SYMBOL_CLASS which will require
933 read_var_value to use the FRAME parameter. */
934 if (symbol_read_needs_frame (sym
))
935 warning ("static field's value depends on the current "
936 "frame - bad debug info?");
937 retval
= read_var_value (sym
, NULL
);
939 if (retval
&& VALUE_LVAL (retval
) == lval_memory
)
940 SET_FIELD_PHYSADDR (TYPE_FIELD (type
, fieldno
),
941 VALUE_ADDRESS (retval
));
946 /* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE.
947 You have to be careful here, since the size of the data area for the value
948 is set by the length of the enclosing type. So if NEW_ENCL_TYPE is bigger
949 than the old enclosing type, you have to allocate more space for the data.
950 The return value is a pointer to the new version of this value structure. */
953 value_change_enclosing_type (struct value
*val
, struct type
*new_encl_type
)
955 if (TYPE_LENGTH (new_encl_type
) <= TYPE_LENGTH (value_enclosing_type (val
)))
957 val
->enclosing_type
= new_encl_type
;
962 struct value
*new_val
;
965 new_val
= (struct value
*) xrealloc (val
, sizeof (struct value
) + TYPE_LENGTH (new_encl_type
));
967 new_val
->enclosing_type
= new_encl_type
;
969 /* We have to make sure this ends up in the same place in the value
970 chain as the original copy, so it's clean-up behavior is the same.
971 If the value has been released, this is a waste of time, but there
972 is no way to tell that in advance, so... */
974 if (val
!= all_values
)
976 for (prev
= all_values
; prev
!= NULL
; prev
= prev
->next
)
978 if (prev
->next
== val
)
980 prev
->next
= new_val
;
990 /* Given a value ARG1 (offset by OFFSET bytes)
991 of a struct or union type ARG_TYPE,
992 extract and return the value of one of its (non-static) fields.
993 FIELDNO says which field. */
996 value_primitive_field (struct value
*arg1
, int offset
,
997 int fieldno
, struct type
*arg_type
)
1002 CHECK_TYPEDEF (arg_type
);
1003 type
= TYPE_FIELD_TYPE (arg_type
, fieldno
);
1005 /* Handle packed fields */
1007 if (TYPE_FIELD_BITSIZE (arg_type
, fieldno
))
1009 v
= value_from_longest (type
,
1010 unpack_field_as_long (arg_type
,
1011 value_contents (arg1
)
1014 v
->bitpos
= TYPE_FIELD_BITPOS (arg_type
, fieldno
) % 8;
1015 v
->bitsize
= TYPE_FIELD_BITSIZE (arg_type
, fieldno
);
1016 v
->offset
= value_offset (arg1
) + offset
1017 + TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
1019 else if (fieldno
< TYPE_N_BASECLASSES (arg_type
))
1021 /* This field is actually a base subobject, so preserve the
1022 entire object's contents for later references to virtual
1024 v
= allocate_value (value_enclosing_type (arg1
));
1026 if (value_lazy (arg1
))
1027 set_value_lazy (v
, 1);
1029 memcpy (value_contents_all_raw (v
), value_contents_all_raw (arg1
),
1030 TYPE_LENGTH (value_enclosing_type (arg1
)));
1031 v
->offset
= value_offset (arg1
);
1032 v
->embedded_offset
= (offset
+ value_embedded_offset (arg1
)
1033 + TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8);
1037 /* Plain old data member */
1038 offset
+= TYPE_FIELD_BITPOS (arg_type
, fieldno
) / 8;
1039 v
= allocate_value (type
);
1040 if (value_lazy (arg1
))
1041 set_value_lazy (v
, 1);
1043 memcpy (value_contents_raw (v
),
1044 value_contents_raw (arg1
) + offset
,
1045 TYPE_LENGTH (type
));
1046 v
->offset
= (value_offset (arg1
) + offset
1047 + value_embedded_offset (arg1
));
1049 VALUE_LVAL (v
) = VALUE_LVAL (arg1
);
1050 if (VALUE_LVAL (arg1
) == lval_internalvar
)
1051 VALUE_LVAL (v
) = lval_internalvar_component
;
1052 VALUE_ADDRESS (v
) = VALUE_ADDRESS (arg1
);
1053 VALUE_REGNUM (v
) = VALUE_REGNUM (arg1
);
1054 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (arg1
);
1055 /* VALUE_OFFSET (v) = VALUE_OFFSET (arg1) + offset
1056 + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8; */
1060 /* Given a value ARG1 of a struct or union type,
1061 extract and return the value of one of its (non-static) fields.
1062 FIELDNO says which field. */
1065 value_field (struct value
*arg1
, int fieldno
)
1067 return value_primitive_field (arg1
, 0, fieldno
, value_type (arg1
));
1070 /* Return a non-virtual function as a value.
1071 F is the list of member functions which contains the desired method.
1072 J is an index into F which provides the desired method.
1074 We only use the symbol for its address, so be happy with either a
1075 full symbol or a minimal symbol.
1079 value_fn_field (struct value
**arg1p
, struct fn_field
*f
, int j
, struct type
*type
,
1083 struct type
*ftype
= TYPE_FN_FIELD_TYPE (f
, j
);
1084 char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
1086 struct minimal_symbol
*msym
;
1088 sym
= lookup_symbol (physname
, 0, VAR_DOMAIN
, 0, NULL
);
1095 gdb_assert (sym
== NULL
);
1096 msym
= lookup_minimal_symbol (physname
, NULL
, NULL
);
1101 v
= allocate_value (ftype
);
1104 VALUE_ADDRESS (v
) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
1108 VALUE_ADDRESS (v
) = SYMBOL_VALUE_ADDRESS (msym
);
1113 if (type
!= value_type (*arg1p
))
1114 *arg1p
= value_ind (value_cast (lookup_pointer_type (type
),
1115 value_addr (*arg1p
)));
1117 /* Move the `this' pointer according to the offset.
1118 VALUE_OFFSET (*arg1p) += offset;
1126 /* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
1129 Extracting bits depends on endianness of the machine. Compute the
1130 number of least significant bits to discard. For big endian machines,
1131 we compute the total number of bits in the anonymous object, subtract
1132 off the bit count from the MSB of the object to the MSB of the
1133 bitfield, then the size of the bitfield, which leaves the LSB discard
1134 count. For little endian machines, the discard count is simply the
1135 number of bits from the LSB of the anonymous object to the LSB of the
1138 If the field is signed, we also do sign extension. */
1141 unpack_field_as_long (struct type
*type
, const char *valaddr
, int fieldno
)
1145 int bitpos
= TYPE_FIELD_BITPOS (type
, fieldno
);
1146 int bitsize
= TYPE_FIELD_BITSIZE (type
, fieldno
);
1148 struct type
*field_type
;
1150 val
= extract_unsigned_integer (valaddr
+ bitpos
/ 8, sizeof (val
));
1151 field_type
= TYPE_FIELD_TYPE (type
, fieldno
);
1152 CHECK_TYPEDEF (field_type
);
1154 /* Extract bits. See comment above. */
1156 if (BITS_BIG_ENDIAN
)
1157 lsbcount
= (sizeof val
* 8 - bitpos
% 8 - bitsize
);
1159 lsbcount
= (bitpos
% 8);
1162 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
1163 If the field is signed, and is negative, then sign extend. */
1165 if ((bitsize
> 0) && (bitsize
< 8 * (int) sizeof (val
)))
1167 valmask
= (((ULONGEST
) 1) << bitsize
) - 1;
1169 if (!TYPE_UNSIGNED (field_type
))
1171 if (val
& (valmask
^ (valmask
>> 1)))
1180 /* Modify the value of a bitfield. ADDR points to a block of memory in
1181 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1182 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1183 indicate which bits (in target bit order) comprise the bitfield.
1184 Requires 0 < BITSIZE <= lbits, 0 <= BITPOS+BITSIZE <= lbits, and
1185 0 <= BITPOS, where lbits is the size of a LONGEST in bits. */
1188 modify_field (char *addr
, LONGEST fieldval
, int bitpos
, int bitsize
)
1191 ULONGEST mask
= (ULONGEST
) -1 >> (8 * sizeof (ULONGEST
) - bitsize
);
1193 /* If a negative fieldval fits in the field in question, chop
1194 off the sign extension bits. */
1195 if ((~fieldval
& ~(mask
>> 1)) == 0)
1198 /* Warn if value is too big to fit in the field in question. */
1199 if (0 != (fieldval
& ~mask
))
1201 /* FIXME: would like to include fieldval in the message, but
1202 we don't have a sprintf_longest. */
1203 warning ("Value does not fit in %d bits.", bitsize
);
1205 /* Truncate it, otherwise adjoining fields may be corrupted. */
1209 oword
= extract_unsigned_integer (addr
, sizeof oword
);
1211 /* Shifting for bit field depends on endianness of the target machine. */
1212 if (BITS_BIG_ENDIAN
)
1213 bitpos
= sizeof (oword
) * 8 - bitpos
- bitsize
;
1215 oword
&= ~(mask
<< bitpos
);
1216 oword
|= fieldval
<< bitpos
;
1218 store_unsigned_integer (addr
, sizeof oword
, oword
);
1221 /* Convert C numbers into newly allocated values */
1224 value_from_longest (struct type
*type
, LONGEST num
)
1226 struct value
*val
= allocate_value (type
);
1227 enum type_code code
;
1230 code
= TYPE_CODE (type
);
1231 len
= TYPE_LENGTH (type
);
1235 case TYPE_CODE_TYPEDEF
:
1236 type
= check_typedef (type
);
1239 case TYPE_CODE_CHAR
:
1240 case TYPE_CODE_ENUM
:
1241 case TYPE_CODE_BOOL
:
1242 case TYPE_CODE_RANGE
:
1243 store_signed_integer (value_contents_raw (val
), len
, num
);
1248 store_typed_address (value_contents_raw (val
), type
, (CORE_ADDR
) num
);
1252 error ("Unexpected type (%d) encountered for integer constant.", code
);
1258 /* Create a value representing a pointer of type TYPE to the address
1261 value_from_pointer (struct type
*type
, CORE_ADDR addr
)
1263 struct value
*val
= allocate_value (type
);
1264 store_typed_address (value_contents_raw (val
), type
, addr
);
1269 /* Create a value for a string constant to be stored locally
1270 (not in the inferior's memory space, but in GDB memory).
1271 This is analogous to value_from_longest, which also does not
1272 use inferior memory. String shall NOT contain embedded nulls. */
1275 value_from_string (char *ptr
)
1278 int len
= strlen (ptr
);
1279 int lowbound
= current_language
->string_lower_bound
;
1280 struct type
*string_char_type
;
1281 struct type
*rangetype
;
1282 struct type
*stringtype
;
1284 rangetype
= create_range_type ((struct type
*) NULL
,
1286 lowbound
, len
+ lowbound
- 1);
1287 string_char_type
= language_string_char_type (current_language
,
1289 stringtype
= create_array_type ((struct type
*) NULL
,
1292 val
= allocate_value (stringtype
);
1293 memcpy (value_contents_raw (val
), ptr
, len
);
1298 value_from_double (struct type
*type
, DOUBLEST num
)
1300 struct value
*val
= allocate_value (type
);
1301 struct type
*base_type
= check_typedef (type
);
1302 enum type_code code
= TYPE_CODE (base_type
);
1303 int len
= TYPE_LENGTH (base_type
);
1305 if (code
== TYPE_CODE_FLT
)
1307 store_typed_floating (value_contents_raw (val
), base_type
, num
);
1310 error ("Unexpected type encountered for floating constant.");
1316 coerce_ref (struct value
*arg
)
1318 struct type
*value_type_arg_tmp
= check_typedef (value_type (arg
));
1319 if (TYPE_CODE (value_type_arg_tmp
) == TYPE_CODE_REF
)
1320 arg
= value_at_lazy (TYPE_TARGET_TYPE (value_type_arg_tmp
),
1321 unpack_pointer (value_type (arg
),
1322 value_contents (arg
)));
1327 coerce_array (struct value
*arg
)
1329 arg
= coerce_ref (arg
);
1330 if (current_language
->c_style_arrays
1331 && TYPE_CODE (value_type (arg
)) == TYPE_CODE_ARRAY
)
1332 arg
= value_coerce_array (arg
);
1333 if (TYPE_CODE (value_type (arg
)) == TYPE_CODE_FUNC
)
1334 arg
= value_coerce_function (arg
);
1339 coerce_number (struct value
*arg
)
1341 arg
= coerce_array (arg
);
1342 arg
= coerce_enum (arg
);
1347 coerce_enum (struct value
*arg
)
1349 if (TYPE_CODE (check_typedef (value_type (arg
))) == TYPE_CODE_ENUM
)
1350 arg
= value_cast (builtin_type_unsigned_int
, arg
);
1355 /* Should we use DEPRECATED_EXTRACT_STRUCT_VALUE_ADDRESS instead of
1356 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc and TYPE
1357 is the type (which is known to be struct, union or array).
1359 On most machines, the struct convention is used unless we are
1360 using gcc and the type is of a special size. */
1361 /* As of about 31 Mar 93, GCC was changed to be compatible with the
1362 native compiler. GCC 2.3.3 was the last release that did it the
1363 old way. Since gcc2_compiled was not changed, we have no
1364 way to correctly win in all cases, so we just do the right thing
1365 for gcc1 and for gcc2 after this change. Thus it loses for gcc
1366 2.0-2.3.3. This is somewhat unfortunate, but changing gcc2_compiled
1367 would cause more chaos than dealing with some struct returns being
1369 /* NOTE: cagney/2004-06-13: Deleted check for "gcc_p". GCC 1.x is
1373 generic_use_struct_convention (int gcc_p
, struct type
*value_type
)
1375 return !(TYPE_LENGTH (value_type
) == 1
1376 || TYPE_LENGTH (value_type
) == 2
1377 || TYPE_LENGTH (value_type
) == 4
1378 || TYPE_LENGTH (value_type
) == 8);
1381 /* Return true if the function returning the specified type is using
1382 the convention of returning structures in memory (passing in the
1383 address as a hidden first parameter). GCC_P is nonzero if compiled
1387 using_struct_return (struct type
*value_type
, int gcc_p
)
1389 enum type_code code
= TYPE_CODE (value_type
);
1391 if (code
== TYPE_CODE_ERROR
)
1392 error ("Function return type unknown.");
1394 if (code
== TYPE_CODE_VOID
)
1395 /* A void return value is never in memory. See also corresponding
1396 code in "print_return_value". */
1399 /* Probe the architecture for the return-value convention. */
1400 return (gdbarch_return_value (current_gdbarch
, value_type
,
1402 != RETURN_VALUE_REGISTER_CONVENTION
);
1406 _initialize_values (void)
1408 add_cmd ("convenience", no_class
, show_convenience
,
1409 "Debugger convenience (\"$foo\") variables.\n\
1410 These variables are created when you assign them values;\n\
1411 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1412 A few convenience variables are given values automatically:\n\
1413 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1414 \"$__\" holds the contents of the last address examined with \"x\".",
1417 add_cmd ("values", no_class
, show_values
,
1418 "Elements of value history around item number IDX (or last ten).",