2018-01-01 05:43:02 +01:00
# Copyright (C) 2008-2018 Free Software Foundation, Inc.
2009-05-28 03:05:14 +02:00
# 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/>.
# This file is part of the GDB testsuite. It tests Python-based
# pretty-printing for the CLI.
2010-10-01 19:03:50 +02:00
load_lib gdb-python.exp
* gdb.python/lib-types.exp: Use standard_testfile,
prepare_for_testing.
* gdb.python/py-block.exp: Use standard_testfile.
* gdb.python/py-breakpoint.exp: Use standard_testfile.
* gdb.python/py-events.exp: Use standard_testfile,
standard_output_file.
* gdb.python/py-evsignal.exp: Use standard_testfile.
* gdb.python/py-evethreads.exp: Use standard_testfile.
* gdb.python/py-explore-cc.exp: Use standard_testfile.
* gdb.python/py-explore.exp: Use standard_testfile.
* gdb.python/py-finish-breakpoint.exp: Use standard_testfile,
standard_output_file.
* gdb.python/py-finish-breakpoint2.exp: Use standard_testfile,
prepare_for_testing.
* gdb.python/py-frame-inline.exp: Use standard_testfile.
* gdb.python/py-frame.exp: Use standard_testfile.
* gdb.python/py-inferior.exp: Use standard_testfile.
* gdb.python/py-infthread.exp: Use standard_testfile.
* gdb.python/py-mi.exp: Use standard_testfile.
* gdb.python/py-objfile-script.exp: Use standard_testfile,
build_executable.
* gdb.python/py-objfile.exp: Use standard_testfile.
* gdb.python/py-pp-maint.exp: Use standard_testfile,
prepare_for_testing.
* gdb.python/py-prettyprint.exp: Use standard_testfile.
* gdb.python/py-progspace.exp: Use standard_testfile,
build_executable.
* gdb.python/py-prompt.exp: Use standard_testfile,
build_executable.
* gdb.python/py-section-script.exp: Use standard_testfile,
build_executable.
* gdb.python/py-shared.exp: Use standard_testfile,
standard_output_file, clean_restart.
* gdb.python/py-symbol.exp: Use standard_output_file,
prepare_for_testing.
* gdb.python/py-symtab.exp: Use standard_output_file,
prepare_for_testing
* gdb.python/py-template.exp: Use standard_testfile.
* gdb.python/py-type.exp: Use standard_testfile.
* gdb.python/py-value-cc.exp: Use standard_testfile.
* gdb.python/py-value.exp: Use standard_testfile.
* gdb.python/python.exp: Use standard_testfile, build_executable.
2012-06-22 19:59:33 +02:00
standard_testfile
2009-05-28 03:05:14 +02:00
# Start with a fresh gdb.
gdb_exit
gdb_start
2010-02-24 12:11:17 +01:00
# Skip all tests if Python scripting is not enabled.
if { [skip_python_tests] } { continue }
2009-05-28 03:05:14 +02:00
2011-07-26 20:38:55 +02:00
proc run_lang_tests {exefile lang} {
global srcdir subdir srcfile testfile hex
if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } {
2016-12-01 21:47:50 +01:00
untested "failed to compile in $lang mode"
2009-05-28 03:05:14 +02:00
return -1
}
set nl "\[\r\n\]+"
# Start with a fresh gdb.
gdb_exit
gdb_start
gdb_reinitialize_dir $srcdir/$subdir
2011-07-26 20:38:55 +02:00
gdb_load ${exefile}
2009-05-28 03:05:14 +02:00
if ![runto_main ] then {
perror "couldn't run to breakpoint"
return
}
2010-06-02 23:50:56 +02:00
gdb_test_no_output "set print pretty on"
2009-05-28 03:05:14 +02:00
gdb_test "b [gdb_get_line_number {break to inspect} ${testfile}.c ]" \
".*Breakpoint.*"
gdb_test "continue" ".*Breakpoint.*"
2009-11-12 16:15:26 +01:00
2013-08-22 15:51:08 +02:00
set remote_python_file [gdb_remote_download host \
${srcdir}/${subdir}/${testfile}.py]
2009-11-12 16:15:26 +01:00
2017-01-26 22:12:12 +01:00
gdb_test_no_output "source ${remote_python_file}" "load python file"
2009-05-28 03:05:14 +02:00
gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>"
gdb_test "print ssa\[1\]" " = a=< a=<5> b=<$hex>> b=< a=<6> b=<$hex>>"
gdb_test "print ssa" " = {a=< a=<3> b=<$hex>> b=< a=<4> b=<$hex>>, a=< a=<5> b=<$hex>> b=< a=<6> b=<$hex>>}"
2010-07-14 16:13:55 +02:00
gdb_test "print arraystruct" " = {$nl *y = 7, *$nl *x = { a=<23> b=<$hex>, a=<24> b=<$hex>} *$nl *}"
MI: Allow non-raw varobj evaluation
Make the MI variable object expression evaluation, with the
-var-evaluate-expression command, recursively call pretty printers, to
match the output of normal expression printing.
Consider the following code:
struct Foo { int val; };
struct Wrapper { Foo foo; };
int main() {
Wrapper w;
w.foo.val = 23;
}
and this pretty printer file:
import gdb.printing
class FooPrinter:
def __init__(self, val):
self.val = val
def to_string(self):
return "Foo" + str(self.val["val"])
class WrapperPrinter:
def __init__(self, val):
self.val = val
def to_string(self):
return self.val["foo"]
test_printer = gdb.printing.RegexpCollectionPrettyPrinter("test")
test_printer.add_printer('Foo', '^Foo$', FooPrinter)
test_printer.add_printer('Wrapper', '^Wrapper$', WrapperPrinter)
gdb.printing.register_pretty_printer(None, test_printer)
Setting a breakpoint at the end of the function, we call the following commands:
-enable-pretty-printing
^done
-var-create var_w @ w
^done,name="var_w",numchild="0",value="{val = 23}",type="Wrapper",dynamic="1",has_more="0"
-var-create var_w_foo @ w.foo
^done,name="var_w_foo",numchild="0",value="Foo23",type="Foo",dynamic="1",has_more="0"
-var-evaluate-expression var_w
^done,value="{val = 23}"
-var-evaluate-expression var_w_foo
^done,value="Foo23"
-data-evaluate-expression w
^done,value="Foo23"
-data-evaluate-expression w.foo
^done,value="Foo23"
So, in the -var-evaluate-expression var_w case, we print the "raw" value
of w.foo, while in the -data-evaluate-expression w case, we print the
pretty printed w.foo value. After this patch, all of the above print
"Foo23".
gdb/ChangeLog:
* varobj.c (varobj_formatted_print_options): Allow recursive
pretty printing if pretty printing is enabled.
gdb/testsuite/ChangeLog:
* gdb.python/py-prettyprint.c
(struct to_string_returns_value_inner,
struct to_string_returns_value_wrapper): New.
(main): Add tsrvw variable.
* gdb.python/py-prettyprint.py (ToStringReturnsValueInner,
ToStringReturnsValueWrapper): New classes.
(register_pretty_printers): Register new pretty-printers.
* gdb.python/py-prettyprint.exp (run_lang_tests): Test printing
recursive pretty printer.
* gdb.python/py-mi.exp: Likewise.
2018-02-02 05:23:28 +01:00
# Test that when a pretty-printer returns a gdb.Value in its to_string, we
# call the pretty-printer of that value too.
gdb_test "print tsrvw" " = Inner to_string 1989"
2009-05-28 03:05:14 +02:00
if {$lang == "c++"} {
gdb_test "print cps" "= a=<8> b=<$hex>"
gdb_test "print cpss" " = {$nl *zss = 9, *$nl *s = a=<10> b=<$hex>$nl}"
gdb_test "print cpssa\[0\]" " = {$nl *zss = 11, *$nl *s = a=<12> b=<$hex>$nl}"
gdb_test "print cpssa\[1\]" " = {$nl *zss = 13, *$nl *s = a=<14> b=<$hex>$nl}"
gdb_test "print cpssa" " = {{$nl *zss = 11, *$nl *s = a=<12> b=<$hex>$nl *}, {$nl *zss = 13, *$nl *s = a=<14> b=<$hex>$nl *}}"
gdb_test "print sss" "= a=<15> b=< a=<8> b=<$hex>>"
gdb_test "print ref" "= a=<15> b=< a=<8> b=<$hex>>"
gdb_test "print derived" \
" = \{.*<Vbase1> = pp class name: Vbase1.*<Vbase2> = \{.*<VirtualTest> = pp value variable is: 1,.*members of Vbase2:.*_vptr.Vbase2 = $hex.*<Vbase3> = \{.*members of Vbase3.*members of Derived:.*value = 2.*"
2009-07-10 12:35:17 +02:00
gdb_test "print ns " "\"embedded\\\\000null\\\\000string\""
2009-09-21 11:39:53 +02:00
gdb_py_test_silent_cmd "set print elements 3" "" 1
gdb_test "print ns" "emb\.\.\.."
gdb_py_test_silent_cmd "set print elements 10" "" 1
gdb_test "print ns" "embedded\\\\000n\.\.\.."
gdb_py_test_silent_cmd "set print elements 200" "" 1
2009-05-28 03:05:14 +02:00
}
2014-08-07 10:09:38 +02:00
if { ![is_address_zero_readable] } {
gdb_test "print ns2" \
".error reading variable: Cannot access memory at address 0x0."
}
2010-11-12 21:49:43 +01:00
2009-11-13 18:17:57 +01:00
gdb_test "print x" " = \"this is x\""
gdb_test "print cstring" " = \"const string\""
2009-05-28 03:05:14 +02:00
2011-01-26 21:58:49 +01:00
gdb_test "print estring" " = \"embedded x\\\\201\\\\202\\\\203\\\\204\""
2015-06-25 19:33:14 +02:00
if { ![is_address_zero_readable] } {
gdb_test "print estring3" \
" = <error reading variable: Cannot create a lazy string with address 0x0, and a non-zero length.>"
}
2010-10-18 21:14:03 +02:00
gdb_test_no_output "python pp_ls_encoding = 'UTF-8'"
gdb_test "print estring2" "\"embedded \", <incomplete sequence \\\\302>"
2011-12-16 16:55:40 +01:00
gdb_test_no_output "set python print-stack full"
2011-04-11 19:40:41 +02:00
gdb_test "print hint_error" "Exception: hint failed\r\nhint_error_val"
2009-11-13 18:17:57 +01:00
gdb_test "print c" " = container \"container\" with 2 elements = {$nl *.0. = 23,$nl *.1. = 72$nl}"
2009-05-28 03:05:14 +02:00
2010-04-14 14:02:46 +02:00
gdb_test "print nstype" " = {$nl *.0. = 7,$nl *.1. = 42$nl}"
2011-01-26 21:58:49 +01:00
gdb_test_no_output "set print pretty off"
gdb_test "print nstype" " = {.0. = 7, .1. = 42}" \
"print nstype on one line"
Fix type casts losing typedefs and reimplement "whatis" typedef stripping
(Ref: https://sourceware.org/ml/gdb/2017-06/msg00020.html)
Assuming int_t is a typedef to int:
typedef int int_t;
gdb currently loses this expression's typedef:
(gdb) p (int_t) 0
$1 = 0
(gdb) whatis $1
type = int
or:
(gdb) whatis (int_t) 0
type = int
or, to get "whatis" out of the way:
(gdb) maint print type (int_t) 0
...
name 'int'
code 0x8 (TYPE_CODE_INT)
...
This prevents a type printer for "int_t" kicking in, with e.g.:
(gdb) p (int_t) 0
From the manual, we can see that that "whatis (int_t) 0" command
invocation should have printed "type = int_t":
If @var{arg} is a variable or an expression, @code{whatis} prints its
literal type as it is used in the source code. If the type was
defined using a @code{typedef}, @code{whatis} will @emph{not} print
the data type underlying the @code{typedef}.
(...)
If @var{arg} is a type name that was defined using @code{typedef},
@code{whatis} @dfn{unrolls} only one level of that @code{typedef}.
That one-level stripping is currently done here, in
gdb/eval.c:evaluate_subexp_standard, handling OP_TYPE:
...
else if (noside == EVAL_AVOID_SIDE_EFFECTS)
{
struct type *type = exp->elts[pc + 1].type;
/* If this is a typedef, then find its immediate target. We
use check_typedef to resolve stubs, but we ignore its
result because we do not want to dig past all
typedefs. */
check_typedef (type);
if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
type = TYPE_TARGET_TYPE (type);
return allocate_value (type);
}
However, this stripping is reachable in both:
#1 - (gdb) whatis (int_t)0 # ARG is an expression with a cast to
# typedef type.
#2 - (gdb) whatis int_t # ARG is a type name.
while only case #2 should strip the typedef. Removing that code from
evaluate_subexp_standard is part of the fix. Instead, we make the
"whatis" command implementation itself strip one level of typedefs
when the command argument is a type name.
We then run into another problem, also fixed by this commit:
value_cast always drops any typedefs of the destination type.
With all that fixed, "whatis (int_t) 0" now works as expected:
(gdb) whatis int_t
type = int
(gdb) whatis (int_t)0
type = int_t
value_cast has many different exit/convertion paths, for handling many
different kinds of casts/conversions, and most of them had to be
tweaked to construct the value of the right "to" type. The new tests
try to exercise most of it, by trying castin of many different
combinations of types. With:
$ make check TESTS="*/whatis-ptype*.exp */gnu_vector.exp */dfp-test.exp"
... due to combinatorial explosion, the testsuite results for the
tests above alone grow like:
- # of expected passes 246
+ # of expected passes 3811
You'll note that the tests exposed one GCC buglet, filed here:
Missing DW_AT_type in DW_TAG_typedef of "typedef of typedef of void"
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81267
gdb/ChangeLog:
2017-08-21 Pedro Alves <palves@redhat.com>
* eval.c (evaluate_subexp_standard) <OP_TYPE>: Don't dig past
typedefs.
* typeprint.c (whatis_exp): If handling "whatis", and expression
is OP_TYPE, strip one typedef level. Otherwise don't strip
typedefs here.
* valops.c (value_cast): Save "to" type before resolving
stubs/typedefs. Use that type as resulting value's type.
gdb/testsuite/ChangeLog:
2017-08-21 Pedro Alves <palves@redhat.com>
* gdb.base/dfp-test.c
(d32_t, d64_t, d128_t, d32_t2, d64_t2, d128_t2, v_d32_t, v_d64_t)
(v_d128_t, v_d32_t2, v_d64_t2, v_d128_t2): New.
* gdb.base/dfp-test.exp: Add whatis/ptype/cast tests.
* gdb.base/gnu_vector.exp: Add whatis/ptype/cast tests.
* gdb.base/whatis-ptype-typedefs.c: New.
* gdb.base/whatis-ptype-typedefs.exp: New.
* gdb.python/py-prettyprint.c (int_type, int_type2): New typedefs.
(an_int, an_int_type, an_int_type2): New globals.
* gdb.python/py-prettyprint.exp (run_lang_tests): Add tests
involving typedefs and cast expressions.
* gdb.python/py-prettyprint.py (class pp_int_typedef): New.
(lookup_typedefs_function): New.
(typedefs_pretty_printers_dict): New.
(top level): Register lookup_typedefs_function in
gdb.pretty_printers.
2017-08-21 12:34:32 +02:00
# Check that GDB doesn't lose typedefs when looking for a printer.
gdb_test "print an_int" " = -1"
gdb_test "print (int) an_int" " = -1"
gdb_test "print (int_type) an_int" " = type=int_type, val=-1"
gdb_test "print an_int_type" " = type=int_type, val=1"
gdb_test "print (int_type) an_int_type" " = type=int_type, val=1"
gdb_test "print an_int_type2" " = type=int_type2, val=2"
gdb_test "print (int) an_int_type2" " = 2"
gdb_test "print (int_type) an_int_type2" " = type=int_type, val=2"
gdb_test "print (int_type2) an_int_type2" " = type=int_type2, val=2"
2011-03-07 17:03:04 +01:00
gdb_continue_to_end
2009-05-28 03:05:14 +02:00
}
2015-09-09 20:42:52 +02:00
if { [run_lang_tests "${binfile}" "c"] == -1 } {
return
}
if { [run_lang_tests "${binfile}-cxx" "c++"] == -1 } {
return
}
2010-06-04 20:18:28 +02:00
# Run various other tests.
# Start with a fresh gdb.
gdb_exit
gdb_start
gdb_reinitialize_dir $srcdir/$subdir
gdb_load ${binfile}
if ![runto_main ] then {
perror "couldn't run to breakpoint"
return
}
2013-08-22 15:51:08 +02:00
set remote_python_file [gdb_remote_download host \
${srcdir}/${subdir}/${testfile}.py]
2010-06-04 20:18:28 +02:00
2017-01-26 22:12:12 +01:00
gdb_test_no_output "source ${remote_python_file}" "load python file"
2010-06-04 20:18:28 +02:00
2012-07-16 21:15:39 +02:00
gdb_breakpoint [gdb_get_line_number "eval-break"]
gdb_continue_to_breakpoint "eval-break" ".* eval-break .*"
gdb_test "info locals" "eval9 = eval=<123456789>"
gdb_test "b [gdb_get_line_number {break to inspect} ${testfile}.c ]" \
".*Breakpoint.*"
gdb_test "continue" ".*Breakpoint.*"
2010-06-04 20:18:28 +02:00
gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>" \
"print ss enabled #1"
2010-06-10 21:48:20 +02:00
gdb_test_no_output "python disable_lookup_function ()"
2010-06-04 20:18:28 +02:00
gdb_test "print ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}" \
"print ss disabled"
2010-06-10 21:48:20 +02:00
gdb_test_no_output "python enable_lookup_function ()"
2010-06-04 20:18:28 +02:00
gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>" \
"print ss enabled #2"