aboutsummaryrefslogtreecommitdiff
path: root/gdb/ax-gdb.c
diff options
context:
space:
mode:
authorPedro Alves <palves@redhat.com>2017-09-04 20:21:15 +0100
committerPedro Alves <palves@redhat.com>2017-09-04 20:21:15 +0100
commit46a4882b3c7d9ec981568b8b13a3c9c39c8f8e61 (patch)
tree77ce5110d97ebc03e6aefd50bab539e4fcb037f5 /gdb/ax-gdb.c
parentfe13dfecbf7d5a9ba3a5d9f52e33e0ddacb39bcc (diff)
downloadbinutils-46a4882b3c7d9ec981568b8b13a3c9c39c8f8e61.zip
binutils-46a4882b3c7d9ec981568b8b13a3c9c39c8f8e61.tar.gz
binutils-46a4882b3c7d9ec981568b8b13a3c9c39c8f8e61.tar.bz2
Stop assuming no-debug-info variables have type int
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.
Diffstat (limited to 'gdb/ax-gdb.c')
-rw-r--r--gdb/ax-gdb.c50
1 files changed, 44 insertions, 6 deletions
diff --git a/gdb/ax-gdb.c b/gdb/ax-gdb.c
index 3fdf81a..2b5e481 100644
--- a/gdb/ax-gdb.c
+++ b/gdb/ax-gdb.c
@@ -42,7 +42,7 @@
#include "linespec.h"
#include "location.h"
#include "objfiles.h"
-
+#include "typeprint.h"
#include "valprint.h"
#include "c-lang.h"
@@ -1765,6 +1765,40 @@ gen_sizeof (struct expression *exp, union exp_element **pc,
}
+/* Generate bytecode for a cast to TO_TYPE. Advance *PC over the
+ subexpression. */
+
+static void
+gen_expr_for_cast (struct expression *exp, union exp_element **pc,
+ struct agent_expr *ax, struct axs_value *value,
+ struct type *to_type)
+{
+ enum exp_opcode op = (*pc)[0].opcode;
+
+ /* Don't let symbols be handled with gen_expr because that throws an
+ "unknown type" error for no-debug data symbols. Instead, we want
+ the cast to reinterpret such symbols. */
+ if (op == OP_VAR_MSYM_VALUE || op == OP_VAR_VALUE)
+ {
+ if (op == OP_VAR_VALUE)
+ {
+ gen_var_ref (ax, value, (*pc)[2].symbol);
+
+ if (value->optimized_out)
+ error (_("`%s' has been optimized out, cannot use"),
+ SYMBOL_PRINT_NAME ((*pc)[2].symbol));
+ }
+ else
+ gen_msym_var_ref (ax, value, (*pc)[2].msymbol, (*pc)[1].objfile);
+ if (TYPE_CODE (value->type) == TYPE_CODE_ERROR)
+ value->type = to_type;
+ (*pc) += 4;
+ }
+ else
+ gen_expr (exp, pc, ax, value);
+ gen_cast (ax, value, to_type);
+}
+
/* Generating bytecode from GDB expressions: general recursive thingy */
/* XXX: i18n */
@@ -1978,11 +2012,18 @@ gen_expr (struct expression *exp, union exp_element **pc,
error (_("`%s' has been optimized out, cannot use"),
SYMBOL_PRINT_NAME ((*pc)[2].symbol));
+ if (TYPE_CODE (value->type) == TYPE_CODE_ERROR)
+ error_unknown_type (SYMBOL_PRINT_NAME ((*pc)[2].symbol));
+
(*pc) += 4;
break;
case OP_VAR_MSYM_VALUE:
gen_msym_var_ref (ax, value, (*pc)[2].msymbol, (*pc)[1].objfile);
+
+ if (TYPE_CODE (value->type) == TYPE_CODE_ERROR)
+ error_unknown_type (MSYMBOL_PRINT_NAME ((*pc)[2].msymbol));
+
(*pc) += 4;
break;
@@ -2043,8 +2084,7 @@ gen_expr (struct expression *exp, union exp_element **pc,
struct type *type = (*pc)[1].type;
(*pc) += 3;
- gen_expr (exp, pc, ax, value);
- gen_cast (ax, value, type);
+ gen_expr_for_cast (exp, pc, ax, value, type);
}
break;
@@ -2059,9 +2099,7 @@ gen_expr (struct expression *exp, union exp_element **pc,
val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
type = value_type (val);
*pc = &exp->elts[offset];
-
- gen_expr (exp, pc, ax, value);
- gen_cast (ax, value, type);
+ gen_expr_for_cast (exp, pc, ax, value, type);
}
break;