mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-12-15 04:31:49 +08:00
46a4882b3c
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.
91 lines
3.0 KiB
C
91 lines
3.0 KiB
C
/* Language independent support for printing types for GDB, the GNU debugger.
|
|
Copyright (C) 1986-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/>. */
|
|
|
|
#ifndef TYPEPRINT_H
|
|
#define TYPEPRINT_H
|
|
|
|
enum language;
|
|
struct ui_file;
|
|
struct typedef_hash_table;
|
|
struct ext_lang_type_printers;
|
|
|
|
struct type_print_options
|
|
{
|
|
/* True means that no special printing flags should apply. */
|
|
unsigned int raw : 1;
|
|
|
|
/* True means print methods in a class. */
|
|
unsigned int print_methods : 1;
|
|
|
|
/* True means print typedefs in a class. */
|
|
unsigned int print_typedefs : 1;
|
|
|
|
/* If not NULL, a local typedef hash table used when printing a
|
|
type. */
|
|
struct typedef_hash_table *local_typedefs;
|
|
|
|
/* If not NULL, a global typedef hash table used when printing a
|
|
type. */
|
|
struct typedef_hash_table *global_typedefs;
|
|
|
|
/* The list of type printers associated with the global typedef
|
|
table. This is intentionally opaque. */
|
|
struct ext_lang_type_printers *global_printers;
|
|
};
|
|
|
|
extern const struct type_print_options type_print_raw_options;
|
|
|
|
void recursively_update_typedef_hash (struct typedef_hash_table *,
|
|
struct type *);
|
|
|
|
void add_template_parameters (struct typedef_hash_table *, struct type *);
|
|
|
|
struct typedef_hash_table *create_typedef_hash (void);
|
|
|
|
void free_typedef_hash (struct typedef_hash_table *);
|
|
|
|
struct cleanup *make_cleanup_free_typedef_hash (struct typedef_hash_table *);
|
|
|
|
struct typedef_hash_table *copy_typedef_hash (struct typedef_hash_table *);
|
|
|
|
const char *find_typedef_in_hash (const struct type_print_options *,
|
|
struct type *);
|
|
|
|
void print_type_scalar (struct type * type, LONGEST, struct ui_file *);
|
|
|
|
void c_type_print_varspec_suffix (struct type *, struct ui_file *, int,
|
|
int, int, const struct type_print_options *);
|
|
|
|
void c_type_print_args (struct type *, struct ui_file *, int, enum language,
|
|
const struct type_print_options *);
|
|
|
|
/* Print <unknown return type> to stream STREAM. */
|
|
|
|
void type_print_unknown_return_type (struct ui_file *stream);
|
|
|
|
/* Throw an error indicating that the user tried to use a symbol that
|
|
has unknown type. SYM_PRINT_NAME is the name of the symbol, to be
|
|
included in the error message. */
|
|
extern void error_unknown_type (const char *sym_print_name);
|
|
|
|
extern void val_print_not_allocated (struct ui_file *stream);
|
|
|
|
extern void val_print_not_associated (struct ui_file *stream);
|
|
|
|
#endif
|