mirror of
https://github.com/espressif/binutils-gdb.git
synced 2025-06-12 19:38:28 +08:00

An earlier commit made GDB no longer assume no-debug-info functions return int. This commit gives the same treatment to variables. Currently, you can end misled by GDB over output like this: (gdb) p var $1 = -1 (gdb) p /x var $2 = 0xffffffff until you realize that GDB is assuming that the variable is an "int", because: (gdb) ptype var type = <data variable, no debug info> You may try to fix it by casting, but that doesn't really help: (gdb) p /x (unsigned long long) var $3 = 0xffffffffffffffff # incorrect ^^ That's incorrect output, because the variable was defined like this: uint64_t var = 0x7fffffffffffffff; ^^ What happened is that with the cast, GDB did an int -> 'unsigned long long' conversion instead of reinterpreting the variable as the cast-to type. To get at the variable properly you have to reinterpret the variable's address manually instead, with either: (gdb) p /x *(unsigned long long *) &var $4 = 0x7fffffffffffffff (gdb) p /x {unsigned long long} &var $5 = 0x7fffffffffffffff After this commit GDB does it for you. This is what you'll get instead: (gdb) p var 'var' has unknown type; cast it to its declared type (gdb) p /x (unsigned long long) var $1 = 0x7fffffffffffffff As in the functions patch, the "compile" machinery doesn't currently have the cast-to type handy, so it continues assuming no-debug variables have int type, though now at least it warns. The change to gdb.cp/m-static.exp deserves an explanation: - gdb_test "print 'gnu_obj_1::method()::sintvar'" "\\$\[0-9\]+ = 4" \ + gdb_test "print (int) 'gnu_obj_1::method()::sintvar'" "\\$\[0-9\]+ = 4" \ That's printing the "sintvar" function local static of the "gnu_obj_1::method()" method. The problem with that test is that that "'S::method()::static_var'" syntax doesn't really work in C++ as you'd expect. The way to make it work correctly currently is to quote the method part, not the whole expression, like: (gdb) print 'gnu_obj_1::method()'::sintvar If you wrap the whole expression in quotes, like in m-static.exp, what really happens is that the parser considers the whole string as a symbol name, but there's no debug symbol with that name. However, local statics have linkage and are given a mangled name that demangles to the same string as the full expression, so that's what GDB prints. After this commit, and without the cast, the print in m-static.exp would error out saying that the variable has unknown type: (gdb) p 'gnu_obj_1::method()::sintvar' 'gnu_obj_1::method()::sintvar' has unknown type; cast it to its declared type TBC, if currently (even before this series) you try to print any function local static variable of type other than int, you'll get bogus results. You can see that with m-static.cc as is, even. Printing the "svar" local, which is a boolean (1 byte) still prints as "int" (4 bytes): (gdb) p 'gnu_obj_1::method()::svar' $1 = 1 (gdb) ptype 'gnu_obj_1::method()::svar' type = <data variable, no debug info> This probably prints some random bogus value on big endian machines. If 'svar' was of some aggregate type (etc.) we'd still print it as int, so the problem would have been more obvious... After this commit, you'll get instead: (gdb) p 'gnu_obj_1::method()::svar' 'gnu_obj_1::method()::svar' has unknown type; cast it to its declared type ... so at least GDB is no longer misleading. Making GDB find the real local static debug symbol is the subject of the following patches. In the end, it'll all "Just Work". gdb/ChangeLog: 2017-09-04 Pedro Alves <palves@redhat.com> * ax-gdb.c: Include "typeprint.h". (gen_expr_for_cast): New function. (gen_expr) <OP_CAST, OP_CAST_TYPE>: Use it. <OP_VAR_VALUE, OP_MSYM_VAR_VALUE>: Error out if the variable's type is unknown. * dwarf2read.c (new_symbol_full): Fallback to int instead of nodebug_data_symbol. * eval.c: Include "typeprint.h". (evaluate_subexp_standard) <OP_VAR_VALUE, OP_VAR_MSYM_VALUE>: Error out if symbol has unknown type. <UNOP_CAST, UNOP_CAST_TYPE>: Common bits factored out to evaluate_subexp_for_cast. (evaluate_subexp_for_address, evaluate_subexp_for_sizeof): Handle OP_VAR_MSYM_VALUE. (evaluate_subexp_for_cast): New function. * gdbtypes.c (init_nodebug_var_type): New function. (objfile_type): Use it to initialize types of variables with no debug info. * typeprint.c (error_unknown_type): New. * typeprint.h (error_unknown_type): New declaration. * compile/compile-c-types.c (convert_type_basic): Handle TYPE_CODE_ERROR; warn and fallback to int for variables with unknown type. gdb/testsuite/ChangeLog: 2017-09-04 Pedro Alves <palves@redhat.com> * gdb.asm/asm-source.exp: Add casts to int. * gdb.base/nodebug.c (dataglobal8, dataglobal32_1, dataglobal32_2) (dataglobal64_1, dataglobal64_2): New globals. * gdb.base/nodebug.exp: Test different expressions involving the new globals, with print, whatis and ptype. Add casts to int. * gdb.base/solib-display.exp: Add casts to int. * gdb.compile/compile-ifunc.exp: Expect warning. Add cast to int. * gdb.cp/m-static.exp: Add cast to int. * gdb.dwarf2/dw2-skip-prologue.exp: Add cast to int. * gdb.threads/tls-nodebug.exp: Check that gdb errors out printing tls variable with no debug info without a cast. Test with a cast to int too. * gdb.trace/entry-values.exp: Add casts.
470 lines
13 KiB
C
470 lines
13 KiB
C
/* Convert types from GDB to GCC
|
||
|
||
Copyright (C) 2014-2017 Free Software Foundation, Inc.
|
||
|
||
This file is part of GDB.
|
||
|
||
This program is free software; you can redistribute it and/or modify
|
||
it under the terms of the GNU General Public License as published by
|
||
the Free Software Foundation; either version 3 of the License, or
|
||
(at your option) any later version.
|
||
|
||
This program is distributed in the hope that it will be useful,
|
||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
GNU General Public License for more details.
|
||
|
||
You should have received a copy of the GNU General Public License
|
||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||
|
||
|
||
#include "defs.h"
|
||
#include "gdbtypes.h"
|
||
#include "compile-internal.h"
|
||
#include "objfiles.h"
|
||
|
||
/* An object that maps a gdb type to a gcc type. */
|
||
|
||
struct type_map_instance
|
||
{
|
||
/* The gdb type. */
|
||
|
||
struct type *type;
|
||
|
||
/* The corresponding gcc type handle. */
|
||
|
||
gcc_type gcc_type_handle;
|
||
};
|
||
|
||
/* Hash a type_map_instance. */
|
||
|
||
static hashval_t
|
||
hash_type_map_instance (const void *p)
|
||
{
|
||
const struct type_map_instance *inst = (const struct type_map_instance *) p;
|
||
|
||
return htab_hash_pointer (inst->type);
|
||
}
|
||
|
||
/* Check two type_map_instance objects for equality. */
|
||
|
||
static int
|
||
eq_type_map_instance (const void *a, const void *b)
|
||
{
|
||
const struct type_map_instance *insta = (const struct type_map_instance *) a;
|
||
const struct type_map_instance *instb = (const struct type_map_instance *) b;
|
||
|
||
return insta->type == instb->type;
|
||
}
|
||
|
||
|
||
|
||
/* Insert an entry into the type map associated with CONTEXT that maps
|
||
from the gdb type TYPE to the gcc type GCC_TYPE. It is ok for a
|
||
given type to be inserted more than once, provided that the exact
|
||
same association is made each time. This simplifies how type
|
||
caching works elsewhere in this file -- see how struct type caching
|
||
is handled. */
|
||
|
||
static void
|
||
insert_type (struct compile_c_instance *context, struct type *type,
|
||
gcc_type gcc_type)
|
||
{
|
||
struct type_map_instance inst, *add;
|
||
void **slot;
|
||
|
||
inst.type = type;
|
||
inst.gcc_type_handle = gcc_type;
|
||
slot = htab_find_slot (context->type_map, &inst, INSERT);
|
||
|
||
add = (struct type_map_instance *) *slot;
|
||
/* The type might have already been inserted in order to handle
|
||
recursive types. */
|
||
if (add != NULL && add->gcc_type_handle != gcc_type)
|
||
error (_("Unexpected type id from GCC, check you use recent enough GCC."));
|
||
|
||
if (add == NULL)
|
||
{
|
||
add = XNEW (struct type_map_instance);
|
||
*add = inst;
|
||
*slot = add;
|
||
}
|
||
}
|
||
|
||
/* Convert a pointer type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_pointer (struct compile_c_instance *context, struct type *type)
|
||
{
|
||
gcc_type target = convert_type (context, TYPE_TARGET_TYPE (type));
|
||
|
||
return C_CTX (context)->c_ops->build_pointer_type (C_CTX (context),
|
||
target);
|
||
}
|
||
|
||
/* Convert an array type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_array (struct compile_c_instance *context, struct type *type)
|
||
{
|
||
gcc_type element_type;
|
||
struct type *range = TYPE_INDEX_TYPE (type);
|
||
|
||
element_type = convert_type (context, TYPE_TARGET_TYPE (type));
|
||
|
||
if (TYPE_LOW_BOUND_KIND (range) != PROP_CONST)
|
||
return C_CTX (context)->c_ops->error (C_CTX (context),
|
||
_("array type with non-constant"
|
||
" lower bound is not supported"));
|
||
if (TYPE_LOW_BOUND (range) != 0)
|
||
return C_CTX (context)->c_ops->error (C_CTX (context),
|
||
_("cannot convert array type with "
|
||
"non-zero lower bound to C"));
|
||
|
||
if (TYPE_HIGH_BOUND_KIND (range) == PROP_LOCEXPR
|
||
|| TYPE_HIGH_BOUND_KIND (range) == PROP_LOCLIST)
|
||
{
|
||
gcc_type result;
|
||
|
||
if (TYPE_VECTOR (type))
|
||
return C_CTX (context)->c_ops->error (C_CTX (context),
|
||
_("variably-sized vector type"
|
||
" is not supported"));
|
||
|
||
std::string upper_bound
|
||
= c_get_range_decl_name (&TYPE_RANGE_DATA (range)->high);
|
||
result = C_CTX (context)->c_ops->build_vla_array_type (C_CTX (context),
|
||
element_type,
|
||
upper_bound.c_str ());
|
||
return result;
|
||
}
|
||
else
|
||
{
|
||
LONGEST low_bound, high_bound, count;
|
||
|
||
if (get_array_bounds (type, &low_bound, &high_bound) == 0)
|
||
count = -1;
|
||
else
|
||
{
|
||
gdb_assert (low_bound == 0); /* Ensured above. */
|
||
count = high_bound + 1;
|
||
}
|
||
|
||
if (TYPE_VECTOR (type))
|
||
return C_CTX (context)->c_ops->build_vector_type (C_CTX (context),
|
||
element_type,
|
||
count);
|
||
return C_CTX (context)->c_ops->build_array_type (C_CTX (context),
|
||
element_type, count);
|
||
}
|
||
}
|
||
|
||
/* Convert a struct or union type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_struct_or_union (struct compile_c_instance *context, struct type *type)
|
||
{
|
||
int i;
|
||
gcc_type result;
|
||
|
||
/* First we create the resulting type and enter it into our hash
|
||
table. This lets recursive types work. */
|
||
if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
|
||
result = C_CTX (context)->c_ops->build_record_type (C_CTX (context));
|
||
else
|
||
{
|
||
gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
|
||
result = C_CTX (context)->c_ops->build_union_type (C_CTX (context));
|
||
}
|
||
insert_type (context, type, result);
|
||
|
||
for (i = 0; i < TYPE_NFIELDS (type); ++i)
|
||
{
|
||
gcc_type field_type;
|
||
unsigned long bitsize = TYPE_FIELD_BITSIZE (type, i);
|
||
|
||
field_type = convert_type (context, TYPE_FIELD_TYPE (type, i));
|
||
if (bitsize == 0)
|
||
bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type, i));
|
||
C_CTX (context)->c_ops->build_add_field (C_CTX (context), result,
|
||
TYPE_FIELD_NAME (type, i),
|
||
field_type,
|
||
bitsize,
|
||
TYPE_FIELD_BITPOS (type, i));
|
||
}
|
||
|
||
C_CTX (context)->c_ops->finish_record_or_union (C_CTX (context), result,
|
||
TYPE_LENGTH (type));
|
||
return result;
|
||
}
|
||
|
||
/* Convert an enum type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_enum (struct compile_c_instance *context, struct type *type)
|
||
{
|
||
gcc_type int_type, result;
|
||
int i;
|
||
struct gcc_c_context *ctx = C_CTX (context);
|
||
|
||
int_type = ctx->c_ops->int_type (ctx,
|
||
TYPE_UNSIGNED (type),
|
||
TYPE_LENGTH (type));
|
||
|
||
result = ctx->c_ops->build_enum_type (ctx, int_type);
|
||
for (i = 0; i < TYPE_NFIELDS (type); ++i)
|
||
{
|
||
ctx->c_ops->build_add_enum_constant (ctx,
|
||
result,
|
||
TYPE_FIELD_NAME (type, i),
|
||
TYPE_FIELD_ENUMVAL (type, i));
|
||
}
|
||
|
||
ctx->c_ops->finish_enum_type (ctx, result);
|
||
|
||
return result;
|
||
}
|
||
|
||
/* Convert a function type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_func (struct compile_c_instance *context, struct type *type)
|
||
{
|
||
int i;
|
||
gcc_type result, return_type;
|
||
struct gcc_type_array array;
|
||
int is_varargs = TYPE_VARARGS (type) || !TYPE_PROTOTYPED (type);
|
||
|
||
struct type *target_type = TYPE_TARGET_TYPE (type);
|
||
|
||
/* Functions with no debug info have no return type. Ideally we'd
|
||
want to fallback to the type of the cast just before the
|
||
function, like GDB's built-in expression parser, but we don't
|
||
have access to that type here. For now, fallback to int, like
|
||
GDB's parser used to do. */
|
||
if (target_type == NULL)
|
||
{
|
||
if (TYPE_OBJFILE_OWNED (type))
|
||
target_type = objfile_type (TYPE_OWNER (type).objfile)->builtin_int;
|
||
else
|
||
target_type = builtin_type (TYPE_OWNER (type).gdbarch)->builtin_int;
|
||
warning (_("function has unknown return type; assuming int"));
|
||
}
|
||
|
||
/* This approach means we can't make self-referential function
|
||
types. Those are impossible in C, though. */
|
||
return_type = convert_type (context, target_type);
|
||
|
||
array.n_elements = TYPE_NFIELDS (type);
|
||
array.elements = XNEWVEC (gcc_type, TYPE_NFIELDS (type));
|
||
for (i = 0; i < TYPE_NFIELDS (type); ++i)
|
||
array.elements[i] = convert_type (context, TYPE_FIELD_TYPE (type, i));
|
||
|
||
result = C_CTX (context)->c_ops->build_function_type (C_CTX (context),
|
||
return_type,
|
||
&array, is_varargs);
|
||
xfree (array.elements);
|
||
|
||
return result;
|
||
}
|
||
|
||
/* Convert an integer type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_int (struct compile_c_instance *context, struct type *type)
|
||
{
|
||
return C_CTX (context)->c_ops->int_type (C_CTX (context),
|
||
TYPE_UNSIGNED (type),
|
||
TYPE_LENGTH (type));
|
||
}
|
||
|
||
/* Convert a floating-point type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_float (struct compile_c_instance *context, struct type *type)
|
||
{
|
||
return C_CTX (context)->c_ops->float_type (C_CTX (context),
|
||
TYPE_LENGTH (type));
|
||
}
|
||
|
||
/* Convert the 'void' type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_void (struct compile_c_instance *context, struct type *type)
|
||
{
|
||
return C_CTX (context)->c_ops->void_type (C_CTX (context));
|
||
}
|
||
|
||
/* Convert a boolean type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_bool (struct compile_c_instance *context, struct type *type)
|
||
{
|
||
return C_CTX (context)->c_ops->bool_type (C_CTX (context));
|
||
}
|
||
|
||
/* Convert a qualified type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_qualified (struct compile_c_instance *context, struct type *type)
|
||
{
|
||
struct type *unqual = make_unqualified_type (type);
|
||
gcc_type unqual_converted;
|
||
gcc_qualifiers_flags quals = 0;
|
||
|
||
unqual_converted = convert_type (context, unqual);
|
||
|
||
if (TYPE_CONST (type))
|
||
quals |= GCC_QUALIFIER_CONST;
|
||
if (TYPE_VOLATILE (type))
|
||
quals |= GCC_QUALIFIER_VOLATILE;
|
||
if (TYPE_RESTRICT (type))
|
||
quals |= GCC_QUALIFIER_RESTRICT;
|
||
|
||
return C_CTX (context)->c_ops->build_qualified_type (C_CTX (context),
|
||
unqual_converted,
|
||
quals);
|
||
}
|
||
|
||
/* Convert a complex type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_complex (struct compile_c_instance *context, struct type *type)
|
||
{
|
||
gcc_type base = convert_type (context, TYPE_TARGET_TYPE (type));
|
||
|
||
return C_CTX (context)->c_ops->build_complex_type (C_CTX (context), base);
|
||
}
|
||
|
||
/* A helper function which knows how to convert most types from their
|
||
gdb representation to the corresponding gcc form. This examines
|
||
the TYPE and dispatches to the appropriate conversion function. It
|
||
returns the gcc type. */
|
||
|
||
static gcc_type
|
||
convert_type_basic (struct compile_c_instance *context, struct type *type)
|
||
{
|
||
/* If we are converting a qualified type, first convert the
|
||
unqualified type and then apply the qualifiers. */
|
||
if ((TYPE_INSTANCE_FLAGS (type) & (TYPE_INSTANCE_FLAG_CONST
|
||
| TYPE_INSTANCE_FLAG_VOLATILE
|
||
| TYPE_INSTANCE_FLAG_RESTRICT)) != 0)
|
||
return convert_qualified (context, type);
|
||
|
||
switch (TYPE_CODE (type))
|
||
{
|
||
case TYPE_CODE_PTR:
|
||
return convert_pointer (context, type);
|
||
|
||
case TYPE_CODE_ARRAY:
|
||
return convert_array (context, type);
|
||
|
||
case TYPE_CODE_STRUCT:
|
||
case TYPE_CODE_UNION:
|
||
return convert_struct_or_union (context, type);
|
||
|
||
case TYPE_CODE_ENUM:
|
||
return convert_enum (context, type);
|
||
|
||
case TYPE_CODE_FUNC:
|
||
return convert_func (context, type);
|
||
|
||
case TYPE_CODE_INT:
|
||
return convert_int (context, type);
|
||
|
||
case TYPE_CODE_FLT:
|
||
return convert_float (context, type);
|
||
|
||
case TYPE_CODE_VOID:
|
||
return convert_void (context, type);
|
||
|
||
case TYPE_CODE_BOOL:
|
||
return convert_bool (context, type);
|
||
|
||
case TYPE_CODE_COMPLEX:
|
||
return convert_complex (context, type);
|
||
|
||
case TYPE_CODE_ERROR:
|
||
{
|
||
/* Ideally, if we get here due to a cast expression, we'd use
|
||
the cast-to type as the variable's type, like GDB's
|
||
built-in parser does. For now, assume "int" like GDB's
|
||
built-in parser used to do, but at least warn. */
|
||
struct type *fallback;
|
||
if (TYPE_OBJFILE_OWNED (type))
|
||
fallback = objfile_type (TYPE_OWNER (type).objfile)->builtin_int;
|
||
else
|
||
fallback = builtin_type (TYPE_OWNER (type).gdbarch)->builtin_int;
|
||
warning (_("variable has unknown type; assuming int"));
|
||
return convert_int (context, fallback);
|
||
}
|
||
}
|
||
|
||
return C_CTX (context)->c_ops->error (C_CTX (context),
|
||
_("cannot convert gdb type "
|
||
"to gcc type"));
|
||
}
|
||
|
||
/* See compile-internal.h. */
|
||
|
||
gcc_type
|
||
convert_type (struct compile_c_instance *context, struct type *type)
|
||
{
|
||
struct type_map_instance inst, *found;
|
||
gcc_type result;
|
||
|
||
/* We don't ever have to deal with typedefs in this code, because
|
||
those are only needed as symbols by the C compiler. */
|
||
type = check_typedef (type);
|
||
|
||
inst.type = type;
|
||
found = (struct type_map_instance *) htab_find (context->type_map, &inst);
|
||
if (found != NULL)
|
||
return found->gcc_type_handle;
|
||
|
||
result = convert_type_basic (context, type);
|
||
insert_type (context, type, result);
|
||
return result;
|
||
}
|
||
|
||
|
||
|
||
/* Delete the compiler instance C. */
|
||
|
||
static void
|
||
delete_instance (struct compile_instance *c)
|
||
{
|
||
struct compile_c_instance *context = (struct compile_c_instance *) c;
|
||
|
||
context->base.fe->ops->destroy (context->base.fe);
|
||
htab_delete (context->type_map);
|
||
if (context->symbol_err_map != NULL)
|
||
htab_delete (context->symbol_err_map);
|
||
xfree (context);
|
||
}
|
||
|
||
/* See compile-internal.h. */
|
||
|
||
struct compile_instance *
|
||
new_compile_instance (struct gcc_c_context *fe)
|
||
{
|
||
struct compile_c_instance *result = XCNEW (struct compile_c_instance);
|
||
|
||
result->base.fe = &fe->base;
|
||
result->base.destroy = delete_instance;
|
||
result->base.gcc_target_options = ("-std=gnu11"
|
||
/* Otherwise the .o file may need
|
||
"_Unwind_Resume" and
|
||
"__gcc_personality_v0". */
|
||
" -fno-exceptions");
|
||
|
||
result->type_map = htab_create_alloc (10, hash_type_map_instance,
|
||
eq_type_map_instance,
|
||
xfree, xcalloc, xfree);
|
||
|
||
fe->c_ops->set_callbacks (fe, gcc_convert_symbol,
|
||
gcc_symbol_address, result);
|
||
|
||
return &result->base;
|
||
}
|