Update copyright year range in all GDB files.
[binutils-gdb.git] / gdb / testsuite / gdb.python / py-prettyprint.exp
blob65e8489bf87dcf23be68485b670334afd4a0d602
1 # Copyright (C) 2008-2020 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 # This file is part of the GDB testsuite.  It tests Python-based
17 # pretty-printing for the CLI.
19 load_lib gdb-python.exp
21 standard_testfile
23 # Start with a fresh gdb.
24 gdb_exit
25 gdb_start
27 # Skip all tests if Python scripting is not enabled.
28 if { [skip_python_tests] } { continue }
30 proc run_lang_tests {exefile lang} {
31     global srcdir subdir srcfile testfile hex
32     if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } {
33         untested "failed to compile in $lang mode"
34         return -1
35     }
37     set nl "\[\r\n\]+"
39     # Start with a fresh gdb.
40     gdb_exit
41     gdb_start
42     gdb_reinitialize_dir $srcdir/$subdir
43     gdb_load ${exefile}
45     if ![runto_main ] then {
46         perror "couldn't run to breakpoint"
47         return
48     }
50     gdb_test_no_output "set print pretty on"
52     gdb_breakpoint [gdb_get_line_number "break to inspect" ${testfile}.c]
53     gdb_continue_to_breakpoint "break to inspect" ".*break to inspect.*"
56     set remote_python_file [gdb_remote_download host \
57                                 ${srcdir}/${subdir}/${testfile}.py]
59     gdb_test_no_output "source ${remote_python_file}" "load python file"
60     
61     gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>"
62     gdb_test "print ssa\[1\]" " = a=< a=<5> b=<$hex>> b=< a=<6> b=<$hex>>"
63     gdb_test "print ssa" " = {a=< a=<3> b=<$hex>> b=< a=<4> b=<$hex>>, a=< a=<5> b=<$hex>> b=< a=<6> b=<$hex>>}"
64     
65     gdb_test "print arraystruct" " = {$nl *y = 7, *$nl *x = { a=<23> b=<$hex>,  a=<24> b=<$hex>} *$nl *}"
67     # Test that when a pretty-printer returns a gdb.Value in its to_string, we
68     # call the pretty-printer of that value too.
69     gdb_test "print tsrvw" " = Inner to_string 1989"
71     if {$lang == "c++"} {
72         gdb_test "print cps" "=  a=<8> b=<$hex>"
73         gdb_test "print cpss" " = {$nl *zss = 9, *$nl *s =  a=<10> b=<$hex>$nl}"
74         gdb_test "print cpssa\[0\]" " = {$nl *zss = 11, *$nl *s =  a=<12> b=<$hex>$nl}"
75         gdb_test "print cpssa\[1\]" " = {$nl *zss = 13, *$nl *s =  a=<14> b=<$hex>$nl}"
76         gdb_test "print cpssa" " = {{$nl *zss = 11, *$nl *s =  a=<12> b=<$hex>$nl *}, {$nl *zss = 13, *$nl *s =  a=<14> b=<$hex>$nl *}}"
77         gdb_test "print sss" "= a=<15> b=< a=<8> b=<$hex>>"
78         gdb_test "print ref" "= a=<15> b=< a=<8> b=<$hex>>"
79         gdb_test "print derived" \
80             " = \{.*<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.*"
81         gdb_test "print ns " "\"embedded\\\\000null\\\\000string\"" \
82             "print ns with default element limit"
83         gdb_test_no_output "set print elements 3"
84         gdb_test "print ns" "emb\.\.\.." \
85             "print ns with element limit of 3"
86         gdb_test_no_output "set print elements 10"
87         gdb_test "print ns" "embedded\\\\000n\.\.\.." \
88             "print ns with element limit of 10"
89         gdb_test_no_output "set print elements 200"
90     }
92     if { ![is_address_zero_readable] } {
93         gdb_test "print ns2" \
94             ".error reading variable: Cannot access memory at address 0x0."
95     }
97     gdb_test "print x" " = \"this is x\""
98     gdb_test "print cstring" " = \"const string\""
100     gdb_test "print estring" " = \"embedded x\\\\201\\\\202\\\\203\\\\204\""
101     if { ![is_address_zero_readable] } {
102             gdb_test "print estring3" \
103                 " = <error reading variable: Cannot create a lazy string with address 0x0, and a non-zero length.>"
104     }
106     gdb_test_no_output "python pp_ls_encoding = 'UTF-8'"
107     gdb_test "print estring2" "\"embedded \", <incomplete sequence \\\\302>"
109     gdb_test_no_output "set python print-stack full"
110     gdb_test "print hint_error" "Exception: hint failed\r\nhint_error_val"
112     gdb_test "print c" " = container \"container\" with 2 elements = {$nl *.0. = 23,$nl *.1. = 72$nl}" \
113         "print c, pretty printing on, default display hint"
115     gdb_test_no_output "set variable c.is_map_p=1"
116     gdb_test "print c" " = container \"container\" with 2 elements = \{$nl  \\\[23\\\] = 72$nl\}" \
117         "print c, pretty printing on, display hint is now map"
119     gdb_test "print nstype" " = {$nl *.0. = 7,$nl *.1. = 42$nl}"
121     gdb_test_no_output "set print pretty off"
122     gdb_test "print nstype" " = {.0. = 7, .1. = 42}" \
123         "print nstype on one line"
125     # Now we have pretty printing turned off, try printing 'c' again.
126     gdb_test "print c" " = container \"container\" with 2 elements = \{\\\[23\\\] = 72\}" \
127         "print c, pretty printing off, display hint is now map"
129     gdb_test_no_output "set variable c.is_map_p=0"
130     gdb_test "print c" " = container \"container\" with 2 elements = \{\\\[0\\\] = 23, \\\[1\\\] = 72\}" \
131         "print c, pretty printing off, default display hint"
133     # Check that GDB doesn't lose typedefs when looking for a printer.
134     gdb_test "print an_int" " = -1"
135     gdb_test "print (int) an_int" " = -1"
136     gdb_test "print (int_type) an_int" " = type=int_type, val=-1"
138     gdb_test "print an_int_type" " = type=int_type, val=1"
139     gdb_test "print (int_type) an_int_type" " = type=int_type, val=1"
141     gdb_test "print an_int_type2" " = type=int_type2, val=2"
142     gdb_test "print (int) an_int_type2" " = 2"
143     gdb_test "print (int_type) an_int_type2" " = type=int_type, val=2"
144     gdb_test "print (int_type2) an_int_type2" " = type=int_type2, val=2"
146     # PR python/16047: it is ok for a pretty printer not to have a
147     # to_string method.
148     gdb_test "print (int_type3) an_int_type2" " = {s = 27}"
150     gdb_continue_to_end
153 with_test_prefix c {
154     if { [run_lang_tests "${binfile}" "c"] == -1 } {
155         return
156     }
158 with_test_prefix c++ {
159     if { [run_lang_tests "${binfile}-cxx" "c++"] == -1 } {
160         return
161     }
164 # Run various other tests.
166 # Start with a fresh gdb.
167 gdb_exit
168 gdb_start
169 gdb_reinitialize_dir $srcdir/$subdir
170 gdb_load ${binfile}
172 if ![runto_main ] then {
173     perror "couldn't run to breakpoint"
174     return
177 set remote_python_file [gdb_remote_download host \
178                             ${srcdir}/${subdir}/${testfile}.py]
180 gdb_test_no_output "source ${remote_python_file}" "load python file"
182 gdb_breakpoint [gdb_get_line_number "eval-break"]
183 gdb_continue_to_breakpoint "eval-break" ".* eval-break .*"
185 gdb_test "info locals" "eval9 = eval=<123456789>"
187 gdb_breakpoint [gdb_get_line_number "break to inspect" ${testfile}.c ]
188 gdb_continue_to_breakpoint "break to inspect" ".*break to inspect.*"
190 gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>" \
191     "print ss enabled #1"
193 gdb_test_no_output "python disable_lookup_function ()"
195 gdb_test "print ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}" \
196     "print ss disabled"
198 gdb_test_no_output "python enable_lookup_function ()"
200 gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>" \
201     "print ss enabled #2"
203 gdb_test "print -raw-values -- ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}" \
204     "print -raw-values -- ss"
206 gdb_test "print -raw-values on -- ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}" \
207     "print -raw-values on -- ss"
209 gdb_test "with print raw-values -- print ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}" \
210     "with print raw-values -- print ss"
212 # Test interaction between /r format and raw-values option:
213 #   When /r is not present, raw-values option tells to bypass (or not) the pretty printers.
214 #     (these cases are tested above).
215 #   When /r is present, it must override the option raw-values off.
216 gdb_test "print /r ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}"
218 gdb_test "with print raw-values off -- print /r ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}"
220 gdb_test "print -raw-values off -- /r ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}"