1 /* Low level packing and unpacking of values for GDB, the GNU Debugger.
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 Free 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 3 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, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_string.h"
34 #include "gdb_assert.h"
40 /* Prototypes for exported functions. */
42 void _initialize_values (void);
46 /* Type of value; either not an lval, or one of the various
47 different possible kinds of lval. */
50 /* Is it modifiable? Only relevant if lval != not_lval. */
53 /* Location of value (if lval). */
56 /* If lval == lval_memory, this is the address in the inferior.
57 If lval == lval_register, this is the byte offset into the
58 registers structure. */
61 /* Pointer to internal variable. */
62 struct internalvar *internalvar;
65 /* Describes offset of a value within lval of a structure in bytes.
66 If lval == lval_memory, this is an offset to the address. If
67 lval == lval_register, this is a further offset from
68 location.address within the registers structure. Note also the
69 member embedded_offset below. */
72 /* Only used for bitfields; number of bits contained in them. */
75 /* Only used for bitfields; position of start of field. For
76 gdbarch_bits_big_endian=0 targets, it is the position of the LSB. For
77 gdbarch_bits_big_endian=1 targets, it is the position of the MSB. */
80 /* Frame register value is relative to. This will be described in
81 the lval enum above as "lval_register". */
82 struct frame_id frame_id;
84 /* Type of the value. */
87 /* If a value represents a C++ object, then the `type' field gives
88 the object's compile-time type. If the object actually belongs
89 to some class derived from `type', perhaps with other base
90 classes and additional members, then `type' is just a subobject
91 of the real thing, and the full object is probably larger than
94 If `type' is a dynamic class (i.e. one with a vtable), then GDB
95 can actually determine the object's run-time type by looking at
96 the run-time type information in the vtable. When this
97 information is available, we may elect to read in the entire
98 object, for several reasons:
100 - When printing the value, the user would probably rather see the
101 full object, not just the limited portion apparent from the
104 - If `type' has virtual base classes, then even printing `type'
105 alone may require reaching outside the `type' portion of the
106 object to wherever the virtual base class has been stored.
108 When we store the entire object, `enclosing_type' is the run-time
109 type -- the complete object -- and `embedded_offset' is the
110 offset of `type' within that larger type, in bytes. The
111 value_contents() macro takes `embedded_offset' into account, so
112 most GDB code continues to see the `type' portion of the value,
113 just as the inferior would.
115 If `type' is a pointer to an object, then `enclosing_type' is a
116 pointer to the object's run-time type, and `pointed_to_offset' is
117 the offset in bytes from the full object to the pointed-to object
118 -- that is, the value `embedded_offset' would have if we followed
119 the pointer and fetched the complete object. (I don't really see
120 the point. Why not just determine the run-time type when you
121 indirect, and avoid the special case? The contents don't matter
122 until you indirect anyway.)
124 If we're not doing anything fancy, `enclosing_type' is equal to
125 `type', and `embedded_offset' is zero, so everything works
127 struct type *enclosing_type;
129 int pointed_to_offset;
131 /* Values are stored in a chain, so that they can be deleted easily
132 over calls to the inferior. Values assigned to internal
133 variables or put into the value history are taken off this
137 /* Register number if the value is from a register. */
140 /* If zero, contents of this value are in the contents field. If
141 nonzero, contents are in inferior. If the lval field is lval_memory,
142 the contents are in inferior memory at location.address plus offset.
143 The lval field may also be lval_register.
145 WARNING: This field is used by the code which handles watchpoints
146 (see breakpoint.c) to decide whether a particular value can be
147 watched by hardware watchpoints. If the lazy flag is set for
148 some member of a value chain, it is assumed that this member of
149 the chain doesn't need to be watched as part of watching the
150 value itself. This is how GDB avoids watching the entire struct
151 or array when the user wants to watch a single struct member or
152 array element. If you ever change the way lazy flag is set and
153 reset, be sure to consider this use as well! */
156 /* If nonzero, this is the value of a variable which does not
157 actually exist in the program. */
160 /* If value is a variable, is it initialized or not. */
163 /* Actual contents of the value. For use of this value; setting it
164 uses the stuff above. Not valid if lazy is nonzero. Target
165 byte-order. We force it to be aligned properly for any possible
166 value. Note that a value therefore extends beyond what is
170 gdb_byte contents[1];
171 DOUBLEST force_doublest_align;
172 LONGEST force_longest_align;
173 CORE_ADDR force_core_addr_align;
174 void *force_pointer_align;
176 /* Do not add any new members here -- contents above will trash
180 /* Prototypes for local functions. */
182 static void show_values (char *, int);
184 static void show_convenience (char *, int);
187 /* The value-history records all the values printed
188 by print commands during this session. Each chunk
189 records 60 consecutive values. The first chunk on
190 the chain records the most recent values.
191 The total number of values is in value_history_count. */
193 #define VALUE_HISTORY_CHUNK 60
195 struct value_history_chunk
197 struct value_history_chunk *next;
198 struct value *values[VALUE_HISTORY_CHUNK];
201 /* Chain of chunks now in use. */
203 static struct value_history_chunk *value_history_chain;
205 static int value_history_count; /* Abs number of last entry stored */
207 /* List of all value objects currently allocated
208 (except for those released by calls to release_value)
209 This is so they can be freed after each command. */
211 static struct value *all_values;
213 /* Allocate a value that has the correct length for type TYPE. */
216 allocate_value (struct type *type)
219 struct type *atype = check_typedef (type);
221 val = (struct value *) xzalloc (sizeof (struct value) + TYPE_LENGTH (atype));
222 val->next = all_values;
225 val->enclosing_type = type;
226 VALUE_LVAL (val) = not_lval;
227 VALUE_ADDRESS (val) = 0;
228 VALUE_FRAME_ID (val) = null_frame_id;
232 VALUE_REGNUM (val) = -1;
234 val->optimized_out = 0;
235 val->embedded_offset = 0;
236 val->pointed_to_offset = 0;
238 val->initialized = 1; /* Default to initialized. */
242 /* Allocate a value that has the correct length
243 for COUNT repetitions type TYPE. */
246 allocate_repeat_value (struct type *type, int count)
248 int low_bound = current_language->string_lower_bound; /* ??? */
249 /* FIXME-type-allocation: need a way to free this type when we are
251 struct type *range_type
252 = create_range_type ((struct type *) NULL, builtin_type_int,
253 low_bound, count + low_bound - 1);
254 /* FIXME-type-allocation: need a way to free this type when we are
256 return allocate_value (create_array_type ((struct type *) NULL,
260 /* Accessor methods. */
263 value_next (struct value *value)
269 value_type (struct value *value)
274 deprecated_set_value_type (struct value *value, struct type *type)
280 value_offset (struct value *value)
282 return value->offset;
285 set_value_offset (struct value *value, int offset)
287 value->offset = offset;
291 value_bitpos (struct value *value)
293 return value->bitpos;
296 set_value_bitpos (struct value *value, int bit)
302 value_bitsize (struct value *value)
304 return value->bitsize;
307 set_value_bitsize (struct value *value, int bit)
309 value->bitsize = bit;
313 value_contents_raw (struct value *value)
315 return value->aligner.contents + value->embedded_offset;
319 value_contents_all_raw (struct value *value)
321 return value->aligner.contents;
325 value_enclosing_type (struct value *value)
327 return value->enclosing_type;
331 value_contents_all (struct value *value)
334 value_fetch_lazy (value);
335 return value->aligner.contents;
339 value_lazy (struct value *value)
345 set_value_lazy (struct value *value, int val)
351 value_contents (struct value *value)
353 return value_contents_writeable (value);
357 value_contents_writeable (struct value *value)
360 value_fetch_lazy (value);
361 return value_contents_raw (value);
364 /* Return non-zero if VAL1 and VAL2 have the same contents. Note that
365 this function is different from value_equal; in C the operator ==
366 can return 0 even if the two values being compared are equal. */
369 value_contents_equal (struct value *val1, struct value *val2)
375 type1 = check_typedef (value_type (val1));
376 type2 = check_typedef (value_type (val2));
377 len = TYPE_LENGTH (type1);
378 if (len != TYPE_LENGTH (type2))
381 return (memcmp (value_contents (val1), value_contents (val2), len) == 0);
385 value_optimized_out (struct value *value)
387 return value->optimized_out;
391 set_value_optimized_out (struct value *value, int val)
393 value->optimized_out = val;
397 value_embedded_offset (struct value *value)
399 return value->embedded_offset;
403 set_value_embedded_offset (struct value *value, int val)
405 value->embedded_offset = val;
409 value_pointed_to_offset (struct value *value)
411 return value->pointed_to_offset;
415 set_value_pointed_to_offset (struct value *value, int val)
417 value->pointed_to_offset = val;
421 deprecated_value_lval_hack (struct value *value)
427 deprecated_value_address_hack (struct value *value)
429 return &value->location.address;
432 struct internalvar **
433 deprecated_value_internalvar_hack (struct value *value)
435 return &value->location.internalvar;
439 deprecated_value_frame_id_hack (struct value *value)
441 return &value->frame_id;
445 deprecated_value_regnum_hack (struct value *value)
447 return &value->regnum;
451 deprecated_value_modifiable (struct value *value)
453 return value->modifiable;
456 deprecated_set_value_modifiable (struct value *value, int modifiable)
458 value->modifiable = modifiable;
461 /* Return a mark in the value chain. All values allocated after the
462 mark is obtained (except for those released) are subject to being freed
463 if a subsequent value_free_to_mark is passed the mark. */
470 /* Free all values allocated since MARK was obtained by value_mark
471 (except for those released). */
473 value_free_to_mark (struct value *mark)
478 for (val = all_values; val && val != mark; val = next)
486 /* Free all the values that have been allocated (except for those released).
487 Called after each command, successful or not. */
490 free_all_values (void)
495 for (val = all_values; val; val = next)
504 /* Remove VAL from the chain all_values
505 so it will not be freed automatically. */
508 release_value (struct value *val)
512 if (all_values == val)
514 all_values = val->next;
518 for (v = all_values; v; v = v->next)
528 /* Release all values up to mark */
530 value_release_to_mark (struct value *mark)
535 for (val = next = all_values; next; next = next->next)
536 if (next->next == mark)
538 all_values = next->next;
546 /* Return a copy of the value ARG.
547 It contains the same contents, for same memory address,
548 but it's a different block of storage. */
551 value_copy (struct value *arg)
553 struct type *encl_type = value_enclosing_type (arg);
554 struct value *val = allocate_value (encl_type);
555 val->type = arg->type;
556 VALUE_LVAL (val) = VALUE_LVAL (arg);
557 val->location = arg->location;
558 val->offset = arg->offset;
559 val->bitpos = arg->bitpos;
560 val->bitsize = arg->bitsize;
561 VALUE_FRAME_ID (val) = VALUE_FRAME_ID (arg);
562 VALUE_REGNUM (val) = VALUE_REGNUM (arg);
563 val->lazy = arg->lazy;
564 val->optimized_out = arg->optimized_out;
565 val->embedded_offset = value_embedded_offset (arg);
566 val->pointed_to_offset = arg->pointed_to_offset;
567 val->modifiable = arg->modifiable;
568 if (!value_lazy (val))
570 memcpy (value_contents_all_raw (val), value_contents_all_raw (arg),
571 TYPE_LENGTH (value_enclosing_type (arg)));
577 /* Access to the value history. */
579 /* Record a new value in the value history.
580 Returns the absolute history index of the entry.
581 Result of -1 indicates the value was not saved; otherwise it is the
582 value history index of this new item. */
585 record_latest_value (struct value *val)
589 /* We don't want this value to have anything to do with the inferior anymore.
590 In particular, "set $1 = 50" should not affect the variable from which
591 the value was taken, and fast watchpoints should be able to assume that
592 a value on the value history never changes. */
593 if (value_lazy (val))
594 value_fetch_lazy (val);
595 /* We preserve VALUE_LVAL so that the user can find out where it was fetched
596 from. This is a bit dubious, because then *&$1 does not just return $1
597 but the current contents of that location. c'est la vie... */
601 /* Here we treat value_history_count as origin-zero
602 and applying to the value being stored now. */
604 i = value_history_count % VALUE_HISTORY_CHUNK;
607 struct value_history_chunk *new
608 = (struct value_history_chunk *)
609 xmalloc (sizeof (struct value_history_chunk));
610 memset (new->values, 0, sizeof new->values);
611 new->next = value_history_chain;
612 value_history_chain = new;
615 value_history_chain->values[i] = val;
617 /* Now we regard value_history_count as origin-one
618 and applying to the value just stored. */
620 return ++value_history_count;
623 /* Return a copy of the value in the history with sequence number NUM. */
626 access_value_history (int num)
628 struct value_history_chunk *chunk;
633 absnum += value_history_count;
638 error (_("The history is empty."));
640 error (_("There is only one value in the history."));
642 error (_("History does not go back to $$%d."), -num);
644 if (absnum > value_history_count)
645 error (_("History has not yet reached $%d."), absnum);
649 /* Now absnum is always absolute and origin zero. */
651 chunk = value_history_chain;
652 for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
656 return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
660 show_values (char *num_exp, int from_tty)
668 /* "info history +" should print from the stored position.
669 "info history <exp>" should print around value number <exp>. */
670 if (num_exp[0] != '+' || num_exp[1] != '\0')
671 num = parse_and_eval_long (num_exp) - 5;
675 /* "info history" means print the last 10 values. */
676 num = value_history_count - 9;
682 for (i = num; i < num + 10 && i <= value_history_count; i++)
684 val = access_value_history (i);
685 printf_filtered (("$%d = "), i);
686 value_print (val, gdb_stdout, 0, Val_pretty_default);
687 printf_filtered (("\n"));
690 /* The next "info history +" should start after what we just printed. */
693 /* Hitting just return after this command should do the same thing as
694 "info history +". If num_exp is null, this is unnecessary, since
695 "info history +" is not useful after "info history". */
696 if (from_tty && num_exp)
703 /* Internal variables. These are variables within the debugger
704 that hold values assigned by debugger commands.
705 The user refers to them with a '$' prefix
706 that does not appear in the variable names stored internally. */
708 static struct internalvar *internalvars;
710 /* If the variable does not already exist create it and give it the value given.
711 If no value is given then the default is zero. */
713 init_if_undefined_command (char* args, int from_tty)
715 struct internalvar* intvar;
717 /* Parse the expression - this is taken from set_command(). */
718 struct expression *expr = parse_expression (args);
719 register struct cleanup *old_chain =
720 make_cleanup (free_current_contents, &expr);
722 /* Validate the expression.
723 Was the expression an assignment?
724 Or even an expression at all? */
725 if (expr->nelts == 0 || expr->elts[0].opcode != BINOP_ASSIGN)
726 error (_("Init-if-undefined requires an assignment expression."));
728 /* Extract the variable from the parsed expression.
729 In the case of an assign the lvalue will be in elts[1] and elts[2]. */
730 if (expr->elts[1].opcode != OP_INTERNALVAR)
731 error (_("The first parameter to init-if-undefined should be a GDB variable."));
732 intvar = expr->elts[2].internalvar;
734 /* Only evaluate the expression if the lvalue is void.
735 This may still fail if the expresssion is invalid. */
736 if (TYPE_CODE (value_type (intvar->value)) == TYPE_CODE_VOID)
737 evaluate_expression (expr);
739 do_cleanups (old_chain);
743 /* Look up an internal variable with name NAME. NAME should not
744 normally include a dollar sign.
746 If the specified internal variable does not exist,
747 the return value is NULL. */
750 lookup_only_internalvar (char *name)
752 struct internalvar *var;
754 for (var = internalvars; var; var = var->next)
755 if (strcmp (var->name, name) == 0)
762 /* Create an internal variable with name NAME and with a void value.
763 NAME should not normally include a dollar sign. */
766 create_internalvar (char *name)
768 struct internalvar *var;
769 var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
770 var->name = concat (name, (char *)NULL);
771 var->value = allocate_value (builtin_type_void);
772 var->endian = gdbarch_byte_order (current_gdbarch);
773 release_value (var->value);
774 var->next = internalvars;
780 /* Look up an internal variable with name NAME. NAME should not
781 normally include a dollar sign.
783 If the specified internal variable does not exist,
784 one is created, with a void value. */
787 lookup_internalvar (char *name)
789 struct internalvar *var;
791 var = lookup_only_internalvar (name);
795 return create_internalvar (name);
799 value_of_internalvar (struct internalvar *var)
805 val = value_copy (var->value);
806 if (value_lazy (val))
807 value_fetch_lazy (val);
808 VALUE_LVAL (val) = lval_internalvar;
809 VALUE_INTERNALVAR (val) = var;
811 /* Values are always stored in the target's byte order. When connected to a
812 target this will most likely always be correct, so there's normally no
813 need to worry about it.
815 However, internal variables can be set up before the target endian is
816 known and so may become out of date. Fix it up before anybody sees.
818 Internal variables usually hold simple scalar values, and we can
819 correct those. More complex values (e.g. structures and floating
820 point types) are left alone, because they would be too complicated
823 if (var->endian != gdbarch_byte_order (current_gdbarch))
825 gdb_byte *array = value_contents_raw (val);
826 struct type *type = check_typedef (value_enclosing_type (val));
827 switch (TYPE_CODE (type))
831 /* Reverse the bytes. */
832 for (i = 0, j = TYPE_LENGTH (type) - 1; i < j; i++, j--)
846 set_internalvar_component (struct internalvar *var, int offset, int bitpos,
847 int bitsize, struct value *newval)
849 gdb_byte *addr = value_contents_writeable (var->value) + offset;
852 modify_field (addr, value_as_long (newval),
855 memcpy (addr, value_contents (newval), TYPE_LENGTH (value_type (newval)));
859 set_internalvar (struct internalvar *var, struct value *val)
861 struct value *newval;
863 newval = value_copy (val);
864 newval->modifiable = 1;
866 /* Force the value to be fetched from the target now, to avoid problems
867 later when this internalvar is referenced and the target is gone or
869 if (value_lazy (newval))
870 value_fetch_lazy (newval);
872 /* Begin code which must not call error(). If var->value points to
873 something free'd, an error() obviously leaves a dangling pointer.
874 But we also get a danling pointer if var->value points to
875 something in the value chain (i.e., before release_value is
876 called), because after the error free_all_values will get called before
880 var->endian = gdbarch_byte_order (current_gdbarch);
881 release_value (newval);
882 /* End code which must not call error(). */
886 internalvar_name (struct internalvar *var)
891 /* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to
892 prevent cycles / duplicates. */
895 preserve_one_value (struct value *value, struct objfile *objfile,
898 if (TYPE_OBJFILE (value->type) == objfile)
899 value->type = copy_type_recursive (objfile, value->type, copied_types);
901 if (TYPE_OBJFILE (value->enclosing_type) == objfile)
902 value->enclosing_type = copy_type_recursive (objfile,
903 value->enclosing_type,
907 /* Update the internal variables and value history when OBJFILE is
908 discarded; we must copy the types out of the objfile. New global types
909 will be created for every convenience variable which currently points to
910 this objfile's types, and the convenience variables will be adjusted to
911 use the new global types. */
914 preserve_values (struct objfile *objfile)
917 struct value_history_chunk *cur;
918 struct internalvar *var;
921 /* Create the hash table. We allocate on the objfile's obstack, since
922 it is soon to be deleted. */
923 copied_types = create_copied_types_hash (objfile);
925 for (cur = value_history_chain; cur; cur = cur->next)
926 for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
928 preserve_one_value (cur->values[i], objfile, copied_types);
930 for (var = internalvars; var; var = var->next)
931 preserve_one_value (var->value, objfile, copied_types);
933 htab_delete (copied_types);
937 show_convenience (char *ignore, int from_tty)
939 struct internalvar *var;
942 for (var = internalvars; var; var = var->next)
948 printf_filtered (("$%s = "), var->name);
949 value_print (value_of_internalvar (var), gdb_stdout,
950 0, Val_pretty_default);
951 printf_filtered (("\n"));
954 printf_unfiltered (_("\
955 No debugger convenience variables now defined.\n\
956 Convenience variables have names starting with \"$\";\n\
957 use \"set\" as in \"set $foo = 5\" to define them.\n"));
960 /* Extract a value as a C number (either long or double).
961 Knows how to convert fixed values to double, or
962 floating values to long.
963 Does not deallocate the value. */
966 value_as_long (struct value *val)
968 /* This coerces arrays and functions, which is necessary (e.g.
969 in disassemble_command). It also dereferences references, which
970 I suspect is the most logical thing to do. */
971 val = coerce_array (val);
972 return unpack_long (value_type (val), value_contents (val));
976 value_as_double (struct value *val)
981 foo = unpack_double (value_type (val), value_contents (val), &inv);
983 error (_("Invalid floating value found in program."));
987 /* Extract a value as a C pointer. Does not deallocate the value.
988 Note that val's type may not actually be a pointer; value_as_long
989 handles all the cases. */
991 value_as_address (struct value *val)
993 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
994 whether we want this to be true eventually. */
996 /* gdbarch_addr_bits_remove is wrong if we are being called for a
997 non-address (e.g. argument to "signal", "info break", etc.), or
998 for pointers to char, in which the low bits *are* significant. */
999 return gdbarch_addr_bits_remove (current_gdbarch, value_as_long (val));
1002 /* There are several targets (IA-64, PowerPC, and others) which
1003 don't represent pointers to functions as simply the address of
1004 the function's entry point. For example, on the IA-64, a
1005 function pointer points to a two-word descriptor, generated by
1006 the linker, which contains the function's entry point, and the
1007 value the IA-64 "global pointer" register should have --- to
1008 support position-independent code. The linker generates
1009 descriptors only for those functions whose addresses are taken.
1011 On such targets, it's difficult for GDB to convert an arbitrary
1012 function address into a function pointer; it has to either find
1013 an existing descriptor for that function, or call malloc and
1014 build its own. On some targets, it is impossible for GDB to
1015 build a descriptor at all: the descriptor must contain a jump
1016 instruction; data memory cannot be executed; and code memory
1019 Upon entry to this function, if VAL is a value of type `function'
1020 (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then
1021 VALUE_ADDRESS (val) is the address of the function. This is what
1022 you'll get if you evaluate an expression like `main'. The call
1023 to COERCE_ARRAY below actually does all the usual unary
1024 conversions, which includes converting values of type `function'
1025 to `pointer to function'. This is the challenging conversion
1026 discussed above. Then, `unpack_long' will convert that pointer
1027 back into an address.
1029 So, suppose the user types `disassemble foo' on an architecture
1030 with a strange function pointer representation, on which GDB
1031 cannot build its own descriptors, and suppose further that `foo'
1032 has no linker-built descriptor. The address->pointer conversion
1033 will signal an error and prevent the command from running, even
1034 though the next step would have been to convert the pointer
1035 directly back into the same address.
1037 The following shortcut avoids this whole mess. If VAL is a
1038 function, just return its address directly. */
1039 if (TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
1040 || TYPE_CODE (value_type (val)) == TYPE_CODE_METHOD)
1041 return VALUE_ADDRESS (val);
1043 val = coerce_array (val);
1045 /* Some architectures (e.g. Harvard), map instruction and data
1046 addresses onto a single large unified address space. For
1047 instance: An architecture may consider a large integer in the
1048 range 0x10000000 .. 0x1000ffff to already represent a data
1049 addresses (hence not need a pointer to address conversion) while
1050 a small integer would still need to be converted integer to
1051 pointer to address. Just assume such architectures handle all
1052 integer conversions in a single function. */
1056 I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we
1057 must admonish GDB hackers to make sure its behavior matches the
1058 compiler's, whenever possible.
1060 In general, I think GDB should evaluate expressions the same way
1061 the compiler does. When the user copies an expression out of
1062 their source code and hands it to a `print' command, they should
1063 get the same value the compiler would have computed. Any
1064 deviation from this rule can cause major confusion and annoyance,
1065 and needs to be justified carefully. In other words, GDB doesn't
1066 really have the freedom to do these conversions in clever and
1069 AndrewC pointed out that users aren't complaining about how GDB
1070 casts integers to pointers; they are complaining that they can't
1071 take an address from a disassembly listing and give it to `x/i'.
1072 This is certainly important.
1074 Adding an architecture method like integer_to_address() certainly
1075 makes it possible for GDB to "get it right" in all circumstances
1076 --- the target has complete control over how things get done, so
1077 people can Do The Right Thing for their target without breaking
1078 anyone else. The standard doesn't specify how integers get
1079 converted to pointers; usually, the ABI doesn't either, but
1080 ABI-specific code is a more reasonable place to handle it. */
1082 if (TYPE_CODE (value_type (val)) != TYPE_CODE_PTR
1083 && TYPE_CODE (value_type (val)) != TYPE_CODE_REF
1084 && gdbarch_integer_to_address_p (current_gdbarch))
1085 return gdbarch_integer_to_address (current_gdbarch, value_type (val),
1086 value_contents (val));
1088 return unpack_long (value_type (val), value_contents (val));
1092 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
1093 as a long, or as a double, assuming the raw data is described
1094 by type TYPE. Knows how to convert different sizes of values
1095 and can convert between fixed and floating point. We don't assume
1096 any alignment for the raw data. Return value is in host byte order.
1098 If you want functions and arrays to be coerced to pointers, and
1099 references to be dereferenced, call value_as_long() instead.
1101 C++: It is assumed that the front-end has taken care of
1102 all matters concerning pointers to members. A pointer
1103 to member which reaches here is considered to be equivalent
1104 to an INT (or some size). After all, it is only an offset. */
1107 unpack_long (struct type *type, const gdb_byte *valaddr)
1109 enum type_code code = TYPE_CODE (type);
1110 int len = TYPE_LENGTH (type);
1111 int nosign = TYPE_UNSIGNED (type);
1115 case TYPE_CODE_TYPEDEF:
1116 return unpack_long (check_typedef (type), valaddr);
1117 case TYPE_CODE_ENUM:
1118 case TYPE_CODE_FLAGS:
1119 case TYPE_CODE_BOOL:
1121 case TYPE_CODE_CHAR:
1122 case TYPE_CODE_RANGE:
1123 case TYPE_CODE_MEMBERPTR:
1125 return extract_unsigned_integer (valaddr, len);
1127 return extract_signed_integer (valaddr, len);
1130 return extract_typed_floating (valaddr, type);
1132 case TYPE_CODE_DECFLOAT:
1133 /* libdecnumber has a function to convert from decimal to integer, but
1134 it doesn't work when the decimal number has a fractional part. */
1135 return decimal_to_doublest (valaddr, len);
1139 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
1140 whether we want this to be true eventually. */
1141 return extract_typed_address (valaddr, type);
1144 error (_("Value can't be converted to integer."));
1146 return 0; /* Placate lint. */
1149 /* Return a double value from the specified type and address.
1150 INVP points to an int which is set to 0 for valid value,
1151 1 for invalid value (bad float format). In either case,
1152 the returned double is OK to use. Argument is in target
1153 format, result is in host format. */
1156 unpack_double (struct type *type, const gdb_byte *valaddr, int *invp)
1158 enum type_code code;
1162 *invp = 0; /* Assume valid. */
1163 CHECK_TYPEDEF (type);
1164 code = TYPE_CODE (type);
1165 len = TYPE_LENGTH (type);
1166 nosign = TYPE_UNSIGNED (type);
1167 if (code == TYPE_CODE_FLT)
1169 /* NOTE: cagney/2002-02-19: There was a test here to see if the
1170 floating-point value was valid (using the macro
1171 INVALID_FLOAT). That test/macro have been removed.
1173 It turns out that only the VAX defined this macro and then
1174 only in a non-portable way. Fixing the portability problem
1175 wouldn't help since the VAX floating-point code is also badly
1176 bit-rotten. The target needs to add definitions for the
1177 methods gdbarch_float_format and gdbarch_double_format - these
1178 exactly describe the target floating-point format. The
1179 problem here is that the corresponding floatformat_vax_f and
1180 floatformat_vax_d values these methods should be set to are
1181 also not defined either. Oops!
1183 Hopefully someone will add both the missing floatformat
1184 definitions and the new cases for floatformat_is_valid (). */
1186 if (!floatformat_is_valid (floatformat_from_type (type), valaddr))
1192 return extract_typed_floating (valaddr, type);
1194 else if (code == TYPE_CODE_DECFLOAT)
1195 return decimal_to_doublest (valaddr, len);
1198 /* Unsigned -- be sure we compensate for signed LONGEST. */
1199 return (ULONGEST) unpack_long (type, valaddr);
1203 /* Signed -- we are OK with unpack_long. */
1204 return unpack_long (type, valaddr);
1208 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
1209 as a CORE_ADDR, assuming the raw data is described by type TYPE.
1210 We don't assume any alignment for the raw data. Return value is in
1213 If you want functions and arrays to be coerced to pointers, and
1214 references to be dereferenced, call value_as_address() instead.
1216 C++: It is assumed that the front-end has taken care of
1217 all matters concerning pointers to members. A pointer
1218 to member which reaches here is considered to be equivalent
1219 to an INT (or some size). After all, it is only an offset. */
1222 unpack_pointer (struct type *type, const gdb_byte *valaddr)
1224 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
1225 whether we want this to be true eventually. */
1226 return unpack_long (type, valaddr);
1230 /* Get the value of the FIELDN'th field (which must be static) of
1231 TYPE. Return NULL if the field doesn't exist or has been
1235 value_static_field (struct type *type, int fieldno)
1237 struct value *retval;
1239 if (TYPE_FIELD_STATIC_HAS_ADDR (type, fieldno))
1241 retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
1242 TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1246 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
1247 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0, NULL);
1250 /* With some compilers, e.g. HP aCC, static data members are reported
1251 as non-debuggable symbols */
1252 struct minimal_symbol *msym = lookup_minimal_symbol (phys_name, NULL, NULL);
1257 retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
1258 SYMBOL_VALUE_ADDRESS (msym));
1263 /* SYM should never have a SYMBOL_CLASS which will require
1264 read_var_value to use the FRAME parameter. */
1265 if (symbol_read_needs_frame (sym))
1266 warning (_("static field's value depends on the current "
1267 "frame - bad debug info?"));
1268 retval = read_var_value (sym, NULL);
1270 if (retval && VALUE_LVAL (retval) == lval_memory)
1271 SET_FIELD_PHYSADDR (TYPE_FIELD (type, fieldno),
1272 VALUE_ADDRESS (retval));
1277 /* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE.
1278 You have to be careful here, since the size of the data area for the value
1279 is set by the length of the enclosing type. So if NEW_ENCL_TYPE is bigger
1280 than the old enclosing type, you have to allocate more space for the data.
1281 The return value is a pointer to the new version of this value structure. */
1284 value_change_enclosing_type (struct value *val, struct type *new_encl_type)
1286 if (TYPE_LENGTH (new_encl_type) <= TYPE_LENGTH (value_enclosing_type (val)))
1288 val->enclosing_type = new_encl_type;
1293 struct value *new_val;
1296 new_val = (struct value *) xrealloc (val, sizeof (struct value) + TYPE_LENGTH (new_encl_type));
1298 new_val->enclosing_type = new_encl_type;
1300 /* We have to make sure this ends up in the same place in the value
1301 chain as the original copy, so it's clean-up behavior is the same.
1302 If the value has been released, this is a waste of time, but there
1303 is no way to tell that in advance, so... */
1305 if (val != all_values)
1307 for (prev = all_values; prev != NULL; prev = prev->next)
1309 if (prev->next == val)
1311 prev->next = new_val;
1321 /* Given a value ARG1 (offset by OFFSET bytes)
1322 of a struct or union type ARG_TYPE,
1323 extract and return the value of one of its (non-static) fields.
1324 FIELDNO says which field. */
1327 value_primitive_field (struct value *arg1, int offset,
1328 int fieldno, struct type *arg_type)
1333 CHECK_TYPEDEF (arg_type);
1334 type = TYPE_FIELD_TYPE (arg_type, fieldno);
1336 /* Handle packed fields */
1338 if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
1340 v = value_from_longest (type,
1341 unpack_field_as_long (arg_type,
1342 value_contents (arg1)
1345 v->bitpos = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
1346 v->bitsize = TYPE_FIELD_BITSIZE (arg_type, fieldno);
1347 v->offset = value_offset (arg1) + offset
1348 + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
1350 else if (fieldno < TYPE_N_BASECLASSES (arg_type))
1352 /* This field is actually a base subobject, so preserve the
1353 entire object's contents for later references to virtual
1355 v = allocate_value (value_enclosing_type (arg1));
1357 if (VALUE_LVAL (arg1) == lval_memory && value_lazy (arg1))
1358 set_value_lazy (v, 1);
1360 memcpy (value_contents_all_raw (v), value_contents_all_raw (arg1),
1361 TYPE_LENGTH (value_enclosing_type (arg1)));
1362 v->offset = value_offset (arg1);
1363 v->embedded_offset = (offset + value_embedded_offset (arg1)
1364 + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8);
1368 /* Plain old data member */
1369 offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
1370 v = allocate_value (type);
1371 if (VALUE_LVAL (arg1) == lval_memory && value_lazy (arg1))
1372 set_value_lazy (v, 1);
1374 memcpy (value_contents_raw (v),
1375 value_contents_raw (arg1) + offset,
1376 TYPE_LENGTH (type));
1377 v->offset = (value_offset (arg1) + offset
1378 + value_embedded_offset (arg1));
1380 VALUE_LVAL (v) = VALUE_LVAL (arg1);
1381 if (VALUE_LVAL (arg1) == lval_internalvar)
1382 VALUE_LVAL (v) = lval_internalvar_component;
1383 v->location = arg1->location;
1384 VALUE_REGNUM (v) = VALUE_REGNUM (arg1);
1385 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (arg1);
1389 /* Given a value ARG1 of a struct or union type,
1390 extract and return the value of one of its (non-static) fields.
1391 FIELDNO says which field. */
1394 value_field (struct value *arg1, int fieldno)
1396 return value_primitive_field (arg1, 0, fieldno, value_type (arg1));
1399 /* Return a non-virtual function as a value.
1400 F is the list of member functions which contains the desired method.
1401 J is an index into F which provides the desired method.
1403 We only use the symbol for its address, so be happy with either a
1404 full symbol or a minimal symbol.
1408 value_fn_field (struct value **arg1p, struct fn_field *f, int j, struct type *type,
1412 struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
1413 char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
1415 struct minimal_symbol *msym;
1417 sym = lookup_symbol (physname, 0, VAR_DOMAIN, 0, NULL);
1424 gdb_assert (sym == NULL);
1425 msym = lookup_minimal_symbol (physname, NULL, NULL);
1430 v = allocate_value (ftype);
1433 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1437 /* The minimal symbol might point to a function descriptor;
1438 resolve it to the actual code address instead. */
1439 struct objfile *objfile = msymbol_objfile (msym);
1440 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1443 = gdbarch_convert_from_func_ptr_addr
1444 (gdbarch, SYMBOL_VALUE_ADDRESS (msym), ¤t_target);
1449 if (type != value_type (*arg1p))
1450 *arg1p = value_ind (value_cast (lookup_pointer_type (type),
1451 value_addr (*arg1p)));
1453 /* Move the `this' pointer according to the offset.
1454 VALUE_OFFSET (*arg1p) += offset;
1462 /* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
1465 Extracting bits depends on endianness of the machine. Compute the
1466 number of least significant bits to discard. For big endian machines,
1467 we compute the total number of bits in the anonymous object, subtract
1468 off the bit count from the MSB of the object to the MSB of the
1469 bitfield, then the size of the bitfield, which leaves the LSB discard
1470 count. For little endian machines, the discard count is simply the
1471 number of bits from the LSB of the anonymous object to the LSB of the
1474 If the field is signed, we also do sign extension. */
1477 unpack_field_as_long (struct type *type, const gdb_byte *valaddr, int fieldno)
1481 int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
1482 int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
1484 struct type *field_type;
1486 val = extract_unsigned_integer (valaddr + bitpos / 8, sizeof (val));
1487 field_type = TYPE_FIELD_TYPE (type, fieldno);
1488 CHECK_TYPEDEF (field_type);
1490 /* Extract bits. See comment above. */
1492 if (gdbarch_bits_big_endian (current_gdbarch))
1493 lsbcount = (sizeof val * 8 - bitpos % 8 - bitsize);
1495 lsbcount = (bitpos % 8);
1498 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
1499 If the field is signed, and is negative, then sign extend. */
1501 if ((bitsize > 0) && (bitsize < 8 * (int) sizeof (val)))
1503 valmask = (((ULONGEST) 1) << bitsize) - 1;
1505 if (!TYPE_UNSIGNED (field_type))
1507 if (val & (valmask ^ (valmask >> 1)))
1516 /* Modify the value of a bitfield. ADDR points to a block of memory in
1517 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1518 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1519 indicate which bits (in target bit order) comprise the bitfield.
1520 Requires 0 < BITSIZE <= lbits, 0 <= BITPOS+BITSIZE <= lbits, and
1521 0 <= BITPOS, where lbits is the size of a LONGEST in bits. */
1524 modify_field (gdb_byte *addr, LONGEST fieldval, int bitpos, int bitsize)
1527 ULONGEST mask = (ULONGEST) -1 >> (8 * sizeof (ULONGEST) - bitsize);
1529 /* If a negative fieldval fits in the field in question, chop
1530 off the sign extension bits. */
1531 if ((~fieldval & ~(mask >> 1)) == 0)
1534 /* Warn if value is too big to fit in the field in question. */
1535 if (0 != (fieldval & ~mask))
1537 /* FIXME: would like to include fieldval in the message, but
1538 we don't have a sprintf_longest. */
1539 warning (_("Value does not fit in %d bits."), bitsize);
1541 /* Truncate it, otherwise adjoining fields may be corrupted. */
1545 oword = extract_unsigned_integer (addr, sizeof oword);
1547 /* Shifting for bit field depends on endianness of the target machine. */
1548 if (gdbarch_bits_big_endian (current_gdbarch))
1549 bitpos = sizeof (oword) * 8 - bitpos - bitsize;
1551 oword &= ~(mask << bitpos);
1552 oword |= fieldval << bitpos;
1554 store_unsigned_integer (addr, sizeof oword, oword);
1557 /* Pack NUM into BUF using a target format of TYPE. */
1560 pack_long (gdb_byte *buf, struct type *type, LONGEST num)
1564 type = check_typedef (type);
1565 len = TYPE_LENGTH (type);
1567 switch (TYPE_CODE (type))
1570 case TYPE_CODE_CHAR:
1571 case TYPE_CODE_ENUM:
1572 case TYPE_CODE_FLAGS:
1573 case TYPE_CODE_BOOL:
1574 case TYPE_CODE_RANGE:
1575 case TYPE_CODE_MEMBERPTR:
1576 store_signed_integer (buf, len, num);
1581 store_typed_address (buf, type, (CORE_ADDR) num);
1585 error (_("Unexpected type (%d) encountered for integer constant."),
1591 /* Convert C numbers into newly allocated values. */
1594 value_from_longest (struct type *type, LONGEST num)
1596 struct value *val = allocate_value (type);
1598 pack_long (value_contents_raw (val), type, num);
1604 /* Create a value representing a pointer of type TYPE to the address
1607 value_from_pointer (struct type *type, CORE_ADDR addr)
1609 struct value *val = allocate_value (type);
1610 store_typed_address (value_contents_raw (val), type, addr);
1615 /* Create a value for a string constant to be stored locally
1616 (not in the inferior's memory space, but in GDB memory).
1617 This is analogous to value_from_longest, which also does not
1618 use inferior memory. String shall NOT contain embedded nulls. */
1621 value_from_string (char *ptr)
1624 int len = strlen (ptr);
1625 int lowbound = current_language->string_lower_bound;
1626 struct type *string_char_type;
1627 struct type *rangetype;
1628 struct type *stringtype;
1630 rangetype = create_range_type ((struct type *) NULL,
1632 lowbound, len + lowbound - 1);
1633 string_char_type = language_string_char_type (current_language,
1635 stringtype = create_array_type ((struct type *) NULL,
1638 val = allocate_value (stringtype);
1639 memcpy (value_contents_raw (val), ptr, len);
1644 value_from_double (struct type *type, DOUBLEST num)
1646 struct value *val = allocate_value (type);
1647 struct type *base_type = check_typedef (type);
1648 enum type_code code = TYPE_CODE (base_type);
1649 int len = TYPE_LENGTH (base_type);
1651 if (code == TYPE_CODE_FLT)
1653 store_typed_floating (value_contents_raw (val), base_type, num);
1656 error (_("Unexpected type encountered for floating constant."));
1662 value_from_decfloat (struct type *type, const gdb_byte *dec)
1664 struct value *val = allocate_value (type);
1666 memcpy (value_contents_raw (val), dec, TYPE_LENGTH (type));
1672 coerce_ref (struct value *arg)
1674 struct type *value_type_arg_tmp = check_typedef (value_type (arg));
1675 if (TYPE_CODE (value_type_arg_tmp) == TYPE_CODE_REF)
1676 arg = value_at_lazy (TYPE_TARGET_TYPE (value_type_arg_tmp),
1677 unpack_pointer (value_type (arg),
1678 value_contents (arg)));
1683 coerce_array (struct value *arg)
1685 arg = coerce_ref (arg);
1686 if (current_language->c_style_arrays
1687 && TYPE_CODE (value_type (arg)) == TYPE_CODE_ARRAY)
1688 arg = value_coerce_array (arg);
1689 if (TYPE_CODE (value_type (arg)) == TYPE_CODE_FUNC)
1690 arg = value_coerce_function (arg);
1695 coerce_number (struct value *arg)
1697 arg = coerce_array (arg);
1698 arg = coerce_enum (arg);
1703 coerce_enum (struct value *arg)
1705 if (TYPE_CODE (check_typedef (value_type (arg))) == TYPE_CODE_ENUM)
1706 arg = value_cast (builtin_type_unsigned_int, arg);
1711 /* Return true if the function returning the specified type is using
1712 the convention of returning structures in memory (passing in the
1713 address as a hidden first parameter). */
1716 using_struct_return (struct type *func_type, struct type *value_type)
1718 enum type_code code = TYPE_CODE (value_type);
1720 if (code == TYPE_CODE_ERROR)
1721 error (_("Function return type unknown."));
1723 if (code == TYPE_CODE_VOID)
1724 /* A void return value is never in memory. See also corresponding
1725 code in "print_return_value". */
1728 /* Probe the architecture for the return-value convention. */
1729 return (gdbarch_return_value (current_gdbarch, func_type, value_type,
1731 != RETURN_VALUE_REGISTER_CONVENTION);
1734 /* Set the initialized field in a value struct. */
1737 set_value_initialized (struct value *val, int status)
1739 val->initialized = status;
1742 /* Return the initialized field in a value struct. */
1745 value_initialized (struct value *val)
1747 return val->initialized;
1751 _initialize_values (void)
1753 add_cmd ("convenience", no_class, show_convenience, _("\
1754 Debugger convenience (\"$foo\") variables.\n\
1755 These variables are created when you assign them values;\n\
1756 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\
1758 A few convenience variables are given values automatically:\n\
1759 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1760 \"$__\" holds the contents of the last address examined with \"x\"."),
1763 add_cmd ("values", no_class, show_values,
1764 _("Elements of value history around item number IDX (or last ten)."),
1767 add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\
1768 Initialize a convenience variable if necessary.\n\
1769 init-if-undefined VARIABLE = EXPRESSION\n\
1770 Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
1771 exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
1772 VARIABLE is already initialized."));