X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=gdb%2Fvalue.c;h=381318b987c36cee05ea58ff09c361b962fa424c;hb=c37f7098e9cb05b2574cf82e6ae299530ed407ba;hp=65a5aa93745ef21b99a9c3f3efda05a1ddccf283;hpb=4ea48cc1cd4875e570047f5aff70387018a56645;p=deliverable%2Fbinutils-gdb.git diff --git a/gdb/value.c b/gdb/value.c index 65a5aa9374..381318b987 100644 --- a/gdb/value.c +++ b/gdb/value.c @@ -2,7 +2,7 @@ Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, - 2009 Free Software Foundation, Inc. + 2009, 2010 Free Software Foundation, Inc. This file is part of GDB. @@ -196,6 +196,10 @@ struct value /* If value is a variable, is it initialized or not. */ int initialized; + /* If value is from the stack. If this is set, read_stack will be + used instead of read_memory to enable extra caching. */ + int stack; + /* Actual contents of the value. Target byte-order. NULL or not valid if lazy is nonzero. */ gdb_byte *contents; @@ -250,7 +254,14 @@ struct value * allocate_value_lazy (struct type *type) { struct value *val; - struct type *atype = check_typedef (type); + + /* Call check_typedef on our type to make sure that, if TYPE + is a TYPE_CODE_TYPEDEF, its length is set to the length + of the target type instead of zero. However, we do not + replace the typedef type by the target type, because we want + to keep the typedef in order to be able to set the VAL's type + description correctly. */ + check_typedef (type); val = (struct value *) xzalloc (sizeof (struct value)); val->contents = NULL; @@ -293,6 +304,7 @@ struct value * allocate_value (struct type *type) { struct value *val = allocate_value_lazy (type); + allocate_value_contents (val); val->lazy = 0; return val; @@ -309,32 +321,8 @@ allocate_repeat_value (struct type *type, int count) done with it. */ struct type *array_type = lookup_array_range_type (type, low_bound, count + low_bound - 1); - return allocate_value (array_type); -} - -/* Needed if another module needs to maintain its on list of values. */ -void -value_prepend_to_list (struct value **head, struct value *val) -{ - val->next = *head; - *head = val; -} -/* Needed if another module needs to maintain its on list of values. */ -void -value_remove_from_list (struct value **head, struct value *val) -{ - struct value *prev; - - if (*head == val) - *head = (*head)->next; - else - for (prev = *head; prev->next; prev = prev->next) - if (prev->next == val) - { - prev->next = val->next; - break; - } + return allocate_value (array_type); } struct value * @@ -343,6 +331,7 @@ allocate_computed_value (struct type *type, void *closure) { struct value *v = allocate_value (type); + VALUE_LVAL (v) = lval_computed; v->location.computed.funcs = funcs; v->location.computed.closure = closure; @@ -360,7 +349,7 @@ value_next (struct value *value) } struct type * -value_type (struct value *value) +value_type (const struct value *value) { return value->type; } @@ -371,7 +360,7 @@ deprecated_set_value_type (struct value *value, struct type *type) } int -value_offset (struct value *value) +value_offset (const struct value *value) { return value->offset; } @@ -382,7 +371,7 @@ set_value_offset (struct value *value, int offset) } int -value_bitpos (struct value *value) +value_bitpos (const struct value *value) { return value->bitpos; } @@ -393,7 +382,7 @@ set_value_bitpos (struct value *value, int bit) } int -value_bitsize (struct value *value) +value_bitsize (const struct value *value) { return value->bitsize; } @@ -429,14 +418,29 @@ value_enclosing_type (struct value *value) return value->enclosing_type; } +static void +require_not_optimized_out (struct value *value) +{ + if (value->optimized_out) + error (_("value has been optimized out")); +} + const gdb_byte * -value_contents_all (struct value *value) +value_contents_for_printing (struct value *value) { if (value->lazy) value_fetch_lazy (value); return value->contents; } +const gdb_byte * +value_contents_all (struct value *value) +{ + const gdb_byte *result = value_contents_for_printing (value); + require_not_optimized_out (value); + return result; +} + int value_lazy (struct value *value) { @@ -449,10 +453,24 @@ set_value_lazy (struct value *value, int val) value->lazy = val; } +int +value_stack (struct value *value) +{ + return value->stack; +} + +void +set_value_stack (struct value *value, int val) +{ + value->stack = val; +} + const gdb_byte * value_contents (struct value *value) { - return value_contents_writeable (value); + const gdb_byte *result = value_contents_writeable (value); + require_not_optimized_out (value); + return result; } gdb_byte * @@ -495,6 +513,29 @@ set_value_optimized_out (struct value *value, int val) value->optimized_out = val; } +int +value_entirely_optimized_out (const struct value *value) +{ + if (!value->optimized_out) + return 0; + if (value->lval != lval_computed + || !value->location.computed.funcs->check_validity) + return 1; + return !value->location.computed.funcs->check_any_valid (value); +} + +int +value_bits_valid (const struct value *value, int offset, int length) +{ + if (value == NULL || !value->optimized_out) + return 1; + if (value->lval != lval_computed + || !value->location.computed.funcs->check_validity) + return 0; + return value->location.computed.funcs->check_validity (value, offset, + length); +} + int value_embedded_offset (struct value *value) { @@ -528,9 +569,9 @@ value_computed_funcs (struct value *v) } void * -value_computed_closure (struct value *v) +value_computed_closure (const struct value *v) { - gdb_assert (VALUE_LVAL (v) == lval_computed); + gdb_assert (v->lval == lval_computed); return v->location.computed.closure; } @@ -663,7 +704,8 @@ value_free_to_mark (struct value *mark) } /* Free all the values that have been allocated (except for those released). - Called after each command, successful or not. */ + Call after each command, successful or not. + In practice this is called before each command, which is sufficient. */ void free_all_values (void) @@ -680,6 +722,20 @@ free_all_values (void) all_values = 0; } +/* Frees all the elements in a chain of values. */ + +void +free_value_chain (struct value *v) +{ + struct value *next; + + for (; v; v = next) + { + next = value_next (v); + value_free (v); + } +} + /* Remove VAL from the chain all_values so it will not be freed automatically. */ @@ -691,6 +747,7 @@ release_value (struct value *val) if (all_values == val) { all_values = val->next; + val->next = NULL; return; } @@ -699,6 +756,7 @@ release_value (struct value *val) if (v->next == val) { v->next = val->next; + val->next = NULL; break; } } @@ -768,16 +826,37 @@ value_copy (struct value *arg) return val; } +/* Return a version of ARG that is non-lvalue. */ + +struct value * +value_non_lval (struct value *arg) +{ + if (VALUE_LVAL (arg) != not_lval) + { + struct type *enc_type = value_enclosing_type (arg); + struct value *val = allocate_value (enc_type); + + memcpy (value_contents_all_raw (val), value_contents_all (arg), + TYPE_LENGTH (enc_type)); + val->type = arg->type; + set_value_embedded_offset (val, value_embedded_offset (arg)); + set_value_pointed_to_offset (val, value_pointed_to_offset (arg)); + return val; + } + return arg; +} + void -set_value_component_location (struct value *component, struct value *whole) +set_value_component_location (struct value *component, + const struct value *whole) { - if (VALUE_LVAL (whole) == lval_internalvar) + if (whole->lval == lval_internalvar) VALUE_LVAL (component) = lval_internalvar_component; else - VALUE_LVAL (component) = VALUE_LVAL (whole); + VALUE_LVAL (component) = whole->lval; component->location = whole->location; - if (VALUE_LVAL (whole) == lval_computed) + if (whole->lval == lval_computed) { struct lval_funcs *funcs = whole->location.computed.funcs; @@ -818,7 +897,8 @@ record_latest_value (struct value *val) if (i == 0) { struct value_history_chunk *new - = (struct value_history_chunk *) + = (struct value_history_chunk *) + xmalloc (sizeof (struct value_history_chunk)); memset (new->values, 0, sizeof new->values); new->next = value_history_chain; @@ -895,6 +975,7 @@ show_values (char *num_exp, int from_tty) for (i = num; i < num + 10 && i <= value_history_count; i++) { struct value_print_options opts; + val = access_value_history (i); printf_filtered (("$%d = "), i); get_user_print_options (&opts); @@ -945,8 +1026,11 @@ struct internalvar /* The internal variable holds a GDB internal convenience function. */ INTERNALVAR_FUNCTION, - /* The variable holds a simple scalar value. */ - INTERNALVAR_SCALAR, + /* The variable holds an integer value. */ + INTERNALVAR_INTEGER, + + /* The variable holds a pointer value. */ + INTERNALVAR_POINTER, /* The variable holds a GDB-provided string. */ INTERNALVAR_STRING, @@ -969,19 +1053,22 @@ struct internalvar int canonical; } fn; - /* A scalar value used with INTERNALVAR_SCALAR. */ + /* An integer value used with INTERNALVAR_INTEGER. */ struct { /* If type is non-NULL, it will be used as the type to generate a value for this internal variable. If type is NULL, a default integer type for the architecture is used. */ struct type *type; - union - { - LONGEST l; /* Used with TYPE_CODE_INT and NULL types. */ - CORE_ADDR a; /* Used with TYPE_CODE_PTR types. */ - } val; - } scalar; + LONGEST val; + } integer; + + /* A pointer value used with INTERNALVAR_POINTER. */ + struct + { + struct type *type; + CORE_ADDR val; + } pointer; /* A string value used with INTERNALVAR_STRING. */ char *string; @@ -1049,6 +1136,7 @@ struct internalvar * create_internalvar (const char *name) { struct internalvar *var; + var = (struct internalvar *) xmalloc (sizeof (struct internalvar)); var->name = concat (name, (char *)NULL); var->kind = INTERNALVAR_VOID; @@ -1066,6 +1154,7 @@ struct internalvar * create_internalvar_type_lazy (char *name, internalvar_make_value fun) { struct internalvar *var = create_internalvar (name); + var->kind = INTERNALVAR_MAKE_VALUE; var->u.make_value = fun; return var; @@ -1107,16 +1196,16 @@ value_of_internalvar (struct gdbarch *gdbarch, struct internalvar *var) val = allocate_value (builtin_type (gdbarch)->internal_fn); break; - case INTERNALVAR_SCALAR: - if (!var->u.scalar.type) + case INTERNALVAR_INTEGER: + if (!var->u.integer.type) val = value_from_longest (builtin_type (gdbarch)->builtin_int, - var->u.scalar.val.l); - else if (TYPE_CODE (var->u.scalar.type) == TYPE_CODE_INT) - val = value_from_longest (var->u.scalar.type, var->u.scalar.val.l); - else if (TYPE_CODE (var->u.scalar.type) == TYPE_CODE_PTR) - val = value_from_pointer (var->u.scalar.type, var->u.scalar.val.a); + var->u.integer.val); else - internal_error (__FILE__, __LINE__, "bad type"); + val = value_from_longest (var->u.integer.type, var->u.integer.val); + break; + + case INTERNALVAR_POINTER: + val = value_from_pointer (var->u.pointer.type, var->u.pointer.val); break; case INTERNALVAR_STRING: @@ -1170,14 +1259,9 @@ get_internalvar_integer (struct internalvar *var, LONGEST *result) { switch (var->kind) { - case INTERNALVAR_SCALAR: - if (var->u.scalar.type == NULL - || TYPE_CODE (var->u.scalar.type) == TYPE_CODE_INT) - { - *result = var->u.scalar.val.l; - return 1; - } - /* Fall through. */ + case INTERNALVAR_INTEGER: + *result = var->u.integer.val; + return 1; default: return 0; @@ -1249,15 +1333,15 @@ set_internalvar (struct internalvar *var, struct value *val) break; case TYPE_CODE_INT: - new_kind = INTERNALVAR_SCALAR; - new_data.scalar.type = value_type (val); - new_data.scalar.val.l = value_as_long (val); + new_kind = INTERNALVAR_INTEGER; + new_data.integer.type = value_type (val); + new_data.integer.val = value_as_long (val); break; case TYPE_CODE_PTR: - new_kind = INTERNALVAR_SCALAR; - new_data.scalar.type = value_type (val); - new_data.scalar.val.a = value_as_address (val); + new_kind = INTERNALVAR_POINTER; + new_data.pointer.type = value_type (val); + new_data.pointer.val = value_as_address (val); break; default: @@ -1294,9 +1378,9 @@ set_internalvar_integer (struct internalvar *var, LONGEST l) /* Clean up old contents. */ clear_internalvar (var); - var->kind = INTERNALVAR_SCALAR; - var->u.scalar.type = NULL; - var->u.scalar.val.l = l; + var->kind = INTERNALVAR_INTEGER; + var->u.integer.type = NULL; + var->u.integer.val = l; } void @@ -1354,6 +1438,7 @@ create_internal_function (const char *name, internal_function_fn handler, void *cookie) { struct internal_function *ifn = XNEW (struct internal_function); + ifn->name = xstrdup (name); ifn->handler = handler; ifn->cookie = cookie; @@ -1430,7 +1515,7 @@ add_internal_function (const char *name, const char *doc, /* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to prevent cycles / duplicates. */ -static void +void preserve_one_value (struct value *value, struct objfile *objfile, htab_t copied_types) { @@ -1451,10 +1536,16 @@ preserve_one_internalvar (struct internalvar *var, struct objfile *objfile, { switch (var->kind) { - case INTERNALVAR_SCALAR: - if (var->u.scalar.type && TYPE_OBJFILE (var->u.scalar.type) == objfile) - var->u.scalar.type - = copy_type_recursive (objfile, var->u.scalar.type, copied_types); + case INTERNALVAR_INTEGER: + if (var->u.integer.type && TYPE_OBJFILE (var->u.integer.type) == objfile) + var->u.integer.type + = copy_type_recursive (objfile, var->u.integer.type, copied_types); + break; + + case INTERNALVAR_POINTER: + if (TYPE_OBJFILE (var->u.pointer.type) == objfile) + var->u.pointer.type + = copy_type_recursive (objfile, var->u.pointer.type, copied_types); break; case INTERNALVAR_VALUE: @@ -1475,7 +1566,6 @@ preserve_values (struct objfile *objfile) htab_t copied_types; struct value_history_chunk *cur; struct internalvar *var; - struct value *val; int i; /* Create the hash table. We allocate on the objfile's obstack, since @@ -1490,8 +1580,7 @@ preserve_values (struct objfile *objfile) for (var = internalvars; var; var = var->next) preserve_one_internalvar (var, objfile, copied_types); - for (val = values_in_python; val; val = val->next) - preserve_one_value (val, objfile, copied_types); + preserve_python_values (objfile, copied_types); htab_delete (copied_types); } @@ -1797,7 +1886,7 @@ unpack_pointer (struct type *type, const gdb_byte *valaddr) } -/* Get the value of the FIELDN'th field (which must be static) of +/* Get the value of the FIELDNO'th field (which must be static) of TYPE. Return NULL if the field doesn't exist or has been optimized out. */ @@ -1806,41 +1895,41 @@ value_static_field (struct type *type, int fieldno) { struct value *retval; - if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR) + switch (TYPE_FIELD_LOC_KIND (type, fieldno)) { - retval = value_at (TYPE_FIELD_TYPE (type, fieldno), - TYPE_FIELD_STATIC_PHYSADDR (type, fieldno)); - } - else + case FIELD_LOC_KIND_PHYSADDR: + retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno), + TYPE_FIELD_STATIC_PHYSADDR (type, fieldno)); + break; + case FIELD_LOC_KIND_PHYSNAME: { char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno); + /*TYPE_FIELD_NAME (type, fieldno);*/ struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0); + if (sym == NULL) { - /* With some compilers, e.g. HP aCC, static data members are reported - as non-debuggable symbols */ - struct minimal_symbol *msym = lookup_minimal_symbol (phys_name, NULL, NULL); + /* With some compilers, e.g. HP aCC, static data members are + reported as non-debuggable symbols */ + struct minimal_symbol *msym = lookup_minimal_symbol (phys_name, + NULL, NULL); + if (!msym) return NULL; else { - retval = value_at (TYPE_FIELD_TYPE (type, fieldno), - SYMBOL_VALUE_ADDRESS (msym)); + retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno), + SYMBOL_VALUE_ADDRESS (msym)); } } else - { - /* SYM should never have a SYMBOL_CLASS which will require - read_var_value to use the FRAME parameter. */ - if (symbol_read_needs_frame (sym)) - warning (_("static field's value depends on the current " - "frame - bad debug info?")); - retval = read_var_value (sym, NULL); - } - if (retval && VALUE_LVAL (retval) == lval_memory) - SET_FIELD_PHYSADDR (TYPE_FIELD (type, fieldno), - value_address (retval)); + retval = value_of_variable (sym, NULL); + break; + } + default: + gdb_assert_not_reached ("unexpected field location kind"); } + return retval; } @@ -1876,6 +1965,14 @@ value_primitive_field (struct value *arg1, int offset, CHECK_TYPEDEF (arg_type); type = TYPE_FIELD_TYPE (arg_type, fieldno); + /* Call check_typedef on our type to make sure that, if TYPE + is a TYPE_CODE_TYPEDEF, its length is set to the length + of the target type instead of zero. However, we do not + replace the typedef type by the target type, because we want + to keep the typedef in order to be able to print the type + description correctly. */ + check_typedef (type); + /* Handle packed fields */ if (TYPE_FIELD_BITSIZE (arg_type, fieldno)) @@ -1896,8 +1993,9 @@ value_primitive_field (struct value *arg1, int offset, v->bitpos = bitpos % container_bitsize; else v->bitpos = bitpos % 8; - v->offset = value_offset (arg1) + value_embedded_offset (arg1) - + (bitpos - v->bitpos) / 8; + v->offset = (value_embedded_offset (arg1) + + offset + + (bitpos - v->bitpos) / 8); v->parent = arg1; value_incref (v->parent); if (!value_lazy (arg1)) @@ -2049,15 +2147,23 @@ unpack_bits_as_long (struct type *field_type, const gdb_byte *valaddr, ULONGEST val; ULONGEST valmask; int lsbcount; + int bytes_read; - val = extract_unsigned_integer (valaddr + bitpos / 8, - sizeof (val), byte_order); + /* Read the minimum number of bytes required; there may not be + enough bytes to read an entire ULONGEST. */ CHECK_TYPEDEF (field_type); + if (bitsize) + bytes_read = ((bitpos % 8) + bitsize + 7) / 8; + else + bytes_read = TYPE_LENGTH (field_type); + + val = extract_unsigned_integer (valaddr + bitpos / 8, + bytes_read, byte_order); /* Extract bits. See comment above. */ if (gdbarch_bits_big_endian (get_type_arch (field_type))) - lsbcount = (sizeof val * 8 - bitpos % 8 - bitsize); + lsbcount = (bytes_read * 8 - bitpos % 8 - bitsize); else lsbcount = (bitpos % 8); val >>= lsbcount; @@ -2171,6 +2277,43 @@ pack_long (gdb_byte *buf, struct type *type, LONGEST num) } +/* Pack NUM into BUF using a target format of TYPE. */ + +void +pack_unsigned_long (gdb_byte *buf, struct type *type, ULONGEST num) +{ + int len; + enum bfd_endian byte_order; + + type = check_typedef (type); + len = TYPE_LENGTH (type); + byte_order = gdbarch_byte_order (get_type_arch (type)); + + switch (TYPE_CODE (type)) + { + case TYPE_CODE_INT: + case TYPE_CODE_CHAR: + case TYPE_CODE_ENUM: + case TYPE_CODE_FLAGS: + case TYPE_CODE_BOOL: + case TYPE_CODE_RANGE: + case TYPE_CODE_MEMBERPTR: + store_unsigned_integer (buf, len, byte_order, num); + break; + + case TYPE_CODE_REF: + case TYPE_CODE_PTR: + store_typed_address (buf, type, (CORE_ADDR) num); + break; + + default: + error (_("\ +Unexpected type (%d) encountered for unsigned integer constant."), + TYPE_CODE (type)); + } +} + + /* Convert C numbers into newly allocated values. */ struct value * @@ -2179,6 +2322,18 @@ value_from_longest (struct type *type, LONGEST num) struct value *val = allocate_value (type); pack_long (value_contents_raw (val), type, num); + return val; +} + + +/* Convert C unsigned numbers into newly allocated values. */ + +struct value * +value_from_ulongest (struct type *type, ULONGEST num) +{ + struct value *val = allocate_value (type); + + pack_unsigned_long (value_contents_raw (val), type, num); return val; } @@ -2190,7 +2345,8 @@ struct value * value_from_pointer (struct type *type, CORE_ADDR addr) { struct value *val = allocate_value (type); - store_typed_address (value_contents_raw (val), type, addr); + + store_typed_address (value_contents_raw (val), check_typedef (type), addr); return val; } @@ -2205,6 +2361,7 @@ value_from_contents_and_address (struct type *type, CORE_ADDR address) { struct value *v = allocate_value (type); + if (valaddr == NULL) set_value_lazy (v, 1); else @@ -2220,7 +2377,6 @@ value_from_double (struct type *type, DOUBLEST num) struct value *val = allocate_value (type); struct type *base_type = check_typedef (type); enum type_code code = TYPE_CODE (base_type); - int len = TYPE_LENGTH (base_type); if (code == TYPE_CODE_FLT) { @@ -2238,7 +2394,6 @@ value_from_decfloat (struct type *type, const gdb_byte *dec) struct value *val = allocate_value (type); memcpy (value_contents_raw (val), dec, TYPE_LENGTH (type)); - return val; } @@ -2246,6 +2401,7 @@ struct value * coerce_ref (struct value *arg) { struct type *value_type_arg_tmp = check_typedef (value_type (arg)); + if (TYPE_CODE (value_type_arg_tmp) == TYPE_CODE_REF) arg = value_at_lazy (TYPE_TARGET_TYPE (value_type_arg_tmp), unpack_pointer (value_type (arg), @@ -2264,7 +2420,7 @@ coerce_array (struct value *arg) switch (TYPE_CODE (type)) { case TYPE_CODE_ARRAY: - if (current_language->c_style_arrays) + if (!TYPE_VECTOR (type) && current_language->c_style_arrays) arg = value_coerce_array (arg); break; case TYPE_CODE_FUNC: