]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.python/py-prettyprint.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-prettyprint.exp
CommitLineData
213516ef 1# Copyright (C) 2008-2023 Free Software Foundation, Inc.
a6bac58e
TT
2
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.
7#
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.
12#
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/>.
15
16# This file is part of the GDB testsuite. It tests Python-based
17# pretty-printing for the CLI.
18
a2c09bd0
DE
19load_lib gdb-python.exp
20
b4a58790 21standard_testfile
a6bac58e
TT
22
23# Start with a fresh gdb.
24gdb_exit
25gdb_start
f6bbabf0
PM
26
27# Skip all tests if Python scripting is not enabled.
28if { [skip_python_tests] } { continue }
a6bac58e 29
db8e4570
UW
30proc run_lang_tests {exefile lang} {
31 global srcdir subdir srcfile testfile hex
32 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } {
84c93cd5 33 untested "failed to compile in $lang mode"
a6bac58e
TT
34 return -1
35 }
36
37 set nl "\[\r\n\]+"
38
39 # Start with a fresh gdb.
40 gdb_exit
41 gdb_start
42 gdb_reinitialize_dir $srcdir/$subdir
db8e4570 43 gdb_load ${exefile}
a6bac58e 44
b0e16ca5 45 if {![runto_main]} {
a6bac58e
TT
46 return
47 }
48
89493308 49 gdb_test_no_output "set print pretty on"
a6bac58e 50
3714a195
AB
51 gdb_breakpoint [gdb_get_line_number "break to inspect" ${testfile}.c]
52 gdb_continue_to_breakpoint "break to inspect" ".*break to inspect.*"
53
f24fce77 54
8448e842
TT
55 set remote_python_file [gdb_remote_download host \
56 ${srcdir}/${subdir}/${testfile}.py]
f24fce77 57
72ca0410 58 gdb_test_no_output "source ${remote_python_file}" "load python file"
a6bac58e
TT
59
60 gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>"
61 gdb_test "print ssa\[1\]" " = a=< a=<5> b=<$hex>> b=< a=<6> b=<$hex>>"
62 gdb_test "print ssa" " = {a=< a=<3> b=<$hex>> b=< a=<4> b=<$hex>>, a=< a=<5> b=<$hex>> b=< a=<6> b=<$hex>>}"
63
91158a56
TT
64 gdb_test "print arraystruct" " = {$nl *y = 7, *$nl *x = { a=<23> b=<$hex>, a=<24> b=<$hex>} *$nl *}"
65
0625771b
LS
66 # Test that when a pretty-printer returns a gdb.Value in its to_string, we
67 # call the pretty-printer of that value too.
68 gdb_test "print tsrvw" " = Inner to_string 1989"
69
a6bac58e
TT
70 if {$lang == "c++"} {
71 gdb_test "print cps" "= a=<8> b=<$hex>"
72 gdb_test "print cpss" " = {$nl *zss = 9, *$nl *s = a=<10> b=<$hex>$nl}"
73 gdb_test "print cpssa\[0\]" " = {$nl *zss = 11, *$nl *s = a=<12> b=<$hex>$nl}"
74 gdb_test "print cpssa\[1\]" " = {$nl *zss = 13, *$nl *s = a=<14> b=<$hex>$nl}"
75 gdb_test "print cpssa" " = {{$nl *zss = 11, *$nl *s = a=<12> b=<$hex>$nl *}, {$nl *zss = 13, *$nl *s = a=<14> b=<$hex>$nl *}}"
76 gdb_test "print sss" "= a=<15> b=< a=<8> b=<$hex>>"
77 gdb_test "print ref" "= a=<15> b=< a=<8> b=<$hex>>"
78 gdb_test "print derived" \
79 " = \{.*<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.*"
3714a195
AB
80 gdb_test "print ns " "\"embedded\\\\000null\\\\000string\"" \
81 "print ns with default element limit"
82 gdb_test_no_output "set print elements 3"
83 gdb_test "print ns" "emb\.\.\.." \
84 "print ns with element limit of 3"
85 gdb_test_no_output "set print elements 10"
86 gdb_test "print ns" "embedded\\\\000n\.\.\.." \
87 "print ns with element limit of 10"
88 gdb_test_no_output "set print elements 200"
a6bac58e
TT
89 }
90
20c6f1e1
YQ
91 if { ![is_address_zero_readable] } {
92 gdb_test "print ns2" \
93 ".error reading variable: Cannot access memory at address 0x0."
94 }
621c8364 95
269f82e5
PP
96 gdb_test "print x" " = \"this is x\""
97 gdb_test "print cstring" " = \"const string\""
a6bac58e 98
a81766d8 99 gdb_test "print estring" " = \"embedded x\\\\201\\\\202\\\\203\\\\204\""
bf7b9cd9
SM
100 if { ![is_address_zero_readable] } {
101 gdb_test "print estring3" \
102 " = <error reading variable: Cannot create a lazy string with address 0x0, and a non-zero length.>"
103 }
3a772aa4
TT
104
105 gdb_test_no_output "python pp_ls_encoding = 'UTF-8'"
106 gdb_test "print estring2" "\"embedded \", <incomplete sequence \\\\302>"
107
80b6e756 108 gdb_test_no_output "set python print-stack full"
e1ab1f9c
JK
109 gdb_test "print hint_error" "Exception: hint failed\r\nhint_error_val"
110
9f9aa852
AB
111 gdb_test "print c" " = container \"container\" with 2 elements = {$nl *.0. = 23,$nl *.1. = 72$nl}" \
112 "print c, pretty printing on, default display hint"
113
b1f3973b
AB
114 gdb_test_no_output "set variable c.is_array_p=1"
115 gdb_test "print c" " = container \"container\" with 2 elements = \\{23, 72\\}" \
116 "print c, pretty printing on, display hint is now array"
117
118 # Setting is_map_p while _is_array_p is also set will cause the
119 # display_hint method to raise an exception (see py-prettyprint.py).
9f9aa852 120 gdb_test_no_output "set variable c.is_map_p=1"
b1f3973b
AB
121
122 # This test checks what happens when the display hint throws an
123 # error. GDB just treats this as though the display hint was
124 # None.
125 set py_exception \
126 [multi_line \
127 "Traceback\[^\r\n\]+" \
128 "\\s+File \"\[^\r\n\]+/py-prettyprint.py\", line \[0-9\]+, in display_hint" \
13123da8 129 "\\s+raise Exception\[^\r\n\]+" \
b1f3973b
AB
130 "Exception: invalid object state found in display_hint"]
131 gdb_test "print c" \
132 [multi_line \
133 " = ${py_exception}" \
134 "container \"container\" with 2 elements = {" \
135 "\\s+\\\[0\\\] = 23," \
136 "\\s+\\\[1\\\] = 72" \
137 "}"] \
138 "print c, pretty printing on, exception raised from display_hint"
139
140 # Unset is_array_p so that display_hint no longer raises an
141 # exception.
142 gdb_test_no_output "set variable c.is_array_p=0"
143
9f9aa852
AB
144 gdb_test "print c" " = container \"container\" with 2 elements = \{$nl \\\[23\\\] = 72$nl\}" \
145 "print c, pretty printing on, display hint is now map"
a6bac58e 146
79f283fe 147 gdb_test "print nstype" " = {$nl *.0. = 7,$nl *.1. = 42$nl}"
a81766d8
TT
148
149 gdb_test_no_output "set print pretty off"
150 gdb_test "print nstype" " = {.0. = 7, .1. = 42}" \
151 "print nstype on one line"
152
9f9aa852
AB
153 # Now we have pretty printing turned off, try printing 'c' again.
154 gdb_test "print c" " = container \"container\" with 2 elements = \{\\\[23\\\] = 72\}" \
155 "print c, pretty printing off, display hint is now map"
156
157 gdb_test_no_output "set variable c.is_map_p=0"
158 gdb_test "print c" " = container \"container\" with 2 elements = \{\\\[0\\\] = 23, \\\[1\\\] = 72\}" \
159 "print c, pretty printing off, default display hint"
160
c973d0aa
PA
161 # Check that GDB doesn't lose typedefs when looking for a printer.
162 gdb_test "print an_int" " = -1"
163 gdb_test "print (int) an_int" " = -1"
164 gdb_test "print (int_type) an_int" " = type=int_type, val=-1"
165
166 gdb_test "print an_int_type" " = type=int_type, val=1"
167 gdb_test "print (int_type) an_int_type" " = type=int_type, val=1"
168
169 gdb_test "print an_int_type2" " = type=int_type2, val=2"
170 gdb_test "print (int) an_int_type2" " = 2"
171 gdb_test "print (int_type) an_int_type2" " = type=int_type, val=2"
172 gdb_test "print (int_type2) an_int_type2" " = type=int_type2, val=2"
173
332cf4c9
TT
174 # PR python/16047: it is ok for a pretty printer not to have a
175 # to_string method.
176 gdb_test "print (int_type3) an_int_type2" " = {s = 27}"
177
fda326dd 178 gdb_continue_to_end
a6bac58e
TT
179}
180
1aac008f
TT
181with_test_prefix c {
182 if { [run_lang_tests "${binfile}" "c"] == -1 } {
183 return
184 }
e70b6e8e 185}
1aac008f
TT
186with_test_prefix c++ {
187 if { [run_lang_tests "${binfile}-cxx" "c++"] == -1 } {
188 return
189 }
e70b6e8e 190}
967cf477
DE
191
192# Run various other tests.
193
967cf477
DE
194# Start with a fresh gdb.
195gdb_exit
196gdb_start
197gdb_reinitialize_dir $srcdir/$subdir
198gdb_load ${binfile}
199
b0e16ca5 200if {![runto_main]} {
967cf477
DE
201 return
202}
203
8448e842
TT
204set remote_python_file [gdb_remote_download host \
205 ${srcdir}/${subdir}/${testfile}.py]
967cf477 206
72ca0410 207gdb_test_no_output "source ${remote_python_file}" "load python file"
967cf477 208
8f043999
JK
209gdb_breakpoint [gdb_get_line_number "eval-break"]
210gdb_continue_to_breakpoint "eval-break" ".* eval-break .*"
211
212gdb_test "info locals" "eval9 = eval=<123456789>"
213
3714a195
AB
214gdb_breakpoint [gdb_get_line_number "break to inspect" ${testfile}.c ]
215gdb_continue_to_breakpoint "break to inspect" ".*break to inspect.*"
8f043999 216
967cf477
DE
217gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>" \
218 "print ss enabled #1"
219
f6978de9 220gdb_test_no_output "python disable_lookup_function ()"
967cf477
DE
221
222gdb_test "print ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}" \
223 "print ss disabled"
224
f6978de9 225gdb_test_no_output "python enable_lookup_function ()"
967cf477
DE
226
227gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>" \
228 "print ss enabled #2"
d8edc8b7
PW
229
230gdb_test "print -raw-values -- ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}" \
231 "print -raw-values -- ss"
232
233gdb_test "print -raw-values on -- ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}" \
234 "print -raw-values on -- ss"
235
236gdb_test "with print raw-values -- print ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}" \
237 "with print raw-values -- print ss"
238
239# Test interaction between /r format and raw-values option:
240# When /r is not present, raw-values option tells to bypass (or not) the pretty printers.
241# (these cases are tested above).
242# When /r is present, it must override the option raw-values off.
243gdb_test "print /r ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}"
244
245gdb_test "with print raw-values off -- print /r ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}"
246
247gdb_test "print -raw-values off -- /r ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}"
248
249