py-mi.exp revision 1.10 1 # Copyright (C) 2008-2023 Free Software Foundation, Inc.
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 MI.
18
19 load_lib mi-support.exp
20 set MIFLAGS "-i=mi2"
21
22 gdb_exit
23 if [mi_gdb_start] {
24 return
25 }
26
27 standard_testfile py-prettyprint.c
28 set pyfile py-prettyprint.py
29 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug additional_flags=-DMI}] != "" } {
30 untested "failed to compile"
31 return -1
32 }
33
34 mi_delete_breakpoints
35 mi_gdb_reinitialize_dir $srcdir/$subdir
36 mi_gdb_load ${binfile}
37
38 if {[lsearch -exact [mi_get_features] python] < 0} {
39 unsupported "python support is disabled"
40 return -1
41 }
42
43 mi_runto_main
44
45 set remote_python_file [gdb_remote_download host ${srcdir}/${subdir}/${pyfile}]
46
47 set cmd "source ${remote_python_file}"
48 set re [string_list_to_regexp & {"} $cmd \\ n {"} \r\n ^ done]
49 mi_gdb_test $cmd $re "load python file"
50
51 mi_continue_to_line [gdb_get_line_number {MI breakpoint here} ${srcfile}] \
52 "step to breakpoint"
53
54 with_test_prefix "varobj container" {
55 mi_create_floating_varobj container c \
56 "create container varobj, no pretty-printing"
57
58 mi_list_varobj_children container {
59 { container.name name 1 string }
60 { container.len len 0 int }
61 { container.elements elements 1 "int ." }
62 } "examine container children=0, no pretty-printing"
63
64 mi_delete_varobj container "delete varobj"
65 }
66
67 with_test_prefix "varobj nscont" {
68 mi_create_floating_varobj nscont nstype \
69 "create nscont varobj, no pretty-printing"
70
71 mi_list_varobj_children nscont {
72 { nscont.len len 0 int }
73 { nscont.elements elements 1 "int ." }
74 } "examine nscont children=0, no pretty-printing"
75
76 mi_delete_varobj nscont "delete varobj"
77 }
78
79 mi_gdb_test "-enable-pretty-printing" "\\^done"
80
81 mi_create_varobj_checked string string_1 \
82 "struct string_repr" \
83 "create string_1 varobj"
84
85 mi_create_varobj_checked lstring estring \
86 "struct lazystring" \
87 "create estring varobj"
88
89 mi_gdb_test "-data-evaluate-expression \"string_1 = string_2\"" ".*" \
90 "assign string_1 from string_2"
91
92 mi_gdb_test "-var-update string" \
93 "\\^done,changelist=\\\[{name=\"string\",in_scope=\"true\",type_changed=\"false\",dynamic=\"1\",has_more=\"0\"}\\\]" \
94 "update string varobj after assignment"
95
96 with_test_prefix "varobj container" {
97 # The "elements" field of "c" is still empty, so the attribute
98 # "has_more" is expected to be zero.
99 mi_create_dynamic_varobj container c 0 \
100 "create container varobj"
101
102 mi_list_varobj_children container {
103 } "examine container children=0"
104
105 mi_next "next over update 1"
106
107 mi_varobj_update_dynamic container "varobj update 1" {
108 type_changed false new_num_children 1 dynamic 1 has_more 0
109 } {
110 } {
111 { name {container.\[0\]} exp {\[0\]} numchild 0 type int }
112 }
113
114 mi_next "next over update 2"
115
116 mi_varobj_update_dynamic container "varobj update 2" {
117 type_changed false new_num_children 2 dynamic 1 has_more 0
118 } {
119 } {
120 { name {container.\[1\]} exp {\[1\]} numchild 0 type int }
121 }
122
123 mi_gdb_test "-var-set-visualizer container None" \
124 "\\^done" \
125 "clear visualizer"
126
127 mi_gdb_test "-var-update container" \
128 "\\^done,changelist=\\\[\\\]" \
129 "varobj update after clearing"
130
131 mi_gdb_test "-var-set-visualizer container gdb.default_visualizer" \
132 "\\^done" \
133 "choose default visualizer"
134
135 mi_varobj_update_dynamic container "varobj update after choosing default" {
136 type_changed false new_num_children 2 dynamic 1 has_more 0
137 } {
138 } {
139 { name {container.\[0\]} exp {\[0\]} numchild 0 type int }
140 { name {container.\[1\]} exp {\[1\]} numchild 0 type int }
141 }
142
143 mi_gdb_test "-var-set-visualizer container ContainerPrinter" \
144 "\\^done" \
145 "choose visualizer using expression"
146
147 mi_varobj_update_dynamic container \
148 "varobj update after choosing via expression" {
149 type_changed false new_num_children 2 dynamic 1 has_more 0
150 } {
151 } {
152 { name {container.\[0\]} exp {\[0\]} numchild 0 type int }
153 { name {container.\[1\]} exp {\[1\]} numchild 0 type int }
154 }
155
156 mi_list_varobj_children_range container 1 2 2 {
157 { {container.\[1\]} {\[1\]} 0 int }
158 } "list varobj children after selecting child range"
159
160 mi_list_varobj_children_range container -1 -1 2 {
161 { {container.\[0\]} {\[0\]} 0 int }
162 { {container.\[1\]} {\[1\]} 0 int }
163 } "list varobj children after resetting child range"
164
165 mi_next "next over update 3"
166
167 mi_gdb_test "-var-set-update-range container 0 1" \
168 "\\^done" \
169 "set update range"
170
171 # This should truncate the list.
172 mi_list_varobj_children container {
173 { {container.\[0\]} {\[0\]} 0 int }
174 } "list children after setting update range"
175
176 # This should return just the items in [1,2).
177 mi_list_varobj_children_range container 1 2 2 {
178 { {container.\[1\]} {\[1\]} 0 int }
179 } "list selected children after setting range"
180
181 # This should not be affected by the previous list-children request.
182 mi_list_varobj_children container {
183 { {container.\[0\]} {\[0\]} 0 int }
184 } "list children after listing selected range"
185
186 mi_next "next over update 4"
187
188 # This should only show the first child, because the update range has
189 # been set.
190 mi_varobj_update_dynamic container \
191 "update after next with restricted range" {
192 type_changed false new_num_children 1 dynamic 1 has_more 1
193 } {
194 { name {container.\[0\]} in_scope true type_changed false has_more 0 }
195 } {
196 }
197
198 mi_gdb_test "-var-set-update-range container 3 4" \
199 "\\^done" \
200 "set update range with non-zero start"
201
202 # Elements were updated but should not be reported.
203 mi_varobj_update_dynamic container \
204 "update varobj with change outside selected range" {
205 type_changed false new_num_children 3 dynamic 1 has_more 0
206 } {
207 } {
208 }
209 }
210
211 mi_next "next over update 5"
212
213 # Regression test: examine an object that has no children, then update
214 # it to ensure that we don't print the children.
215 mi_create_dynamic_varobj container2 c2 0 \
216 "create second container varobj"
217
218 mi_gdb_test "-var-update container2" \
219 "\\^done,changelist=.." \
220 "update varobj, no children requested"
221
222 mi_next "next over update 6"
223
224 # Now container2 has an element -- and an update should mention that
225 # it has_more. But, because we did not request children, we still
226 # should not actually see them.
227 mi_varobj_update_dynamic container2 \
228 "update varobj 2, no children requested" {
229 type_changed false dynamic 1 has_more 1
230 } {} {}
231
232 mi_continue_to_line \
233 [gdb_get_line_number {MI outer breakpoint here} ${srcfile}] \
234 "step to first outer breakpoint"
235
236 mi_create_dynamic_varobj outer outer 1 \
237 "create outer varobj"
238
239 mi_list_varobj_children outer {
240 { outer.s s 2 "struct substruct" }
241 { outer.x x 0 "int" }
242 } "list children of outer"
243
244 mi_list_varobj_children outer.s {
245 { outer.s.a a 0 int }
246 { outer.s.b b 0 int }
247 } "list children of outer.s"
248
249 mi_next "next over outer update"
250
251 mi_gdb_test "-var-update outer" \
252 ".done,changelist=.{name=\"outer.s.a\",in_scope=\"true\",type_changed=\"false\",has_more=\"0\"}." \
253 "update after updating element of outer"
254
255 mi_continue_to_line \
256 [gdb_get_line_number {Another MI breakpoint} ${srcfile}] \
257 "step to second breakpoint"
258
259 mi_varobj_update_with_type_change container int 0 "update after type change"
260
261
262 mi_continue_to_line \
263 [gdb_get_line_number {break to inspect struct and union} ${srcfile}] \
264 "step to second outer breakpoint"
265
266 with_test_prefix "varobj nscont" {
267 mi_create_dynamic_varobj nscont nstype 1 \
268 "create nstype varobj"
269
270 mi_list_varobj_children nscont {
271 { {nscont.\[0\]} {\[0\]} 0 int }
272 { {nscont.\[1\]} {\[1\]} 0 int }
273 } "list children after setting update range"
274
275 mi_gdb_test "-var-set-visualizer nscont None" \
276 "\\^done" \
277 "clear visualizer"
278
279 mi_gdb_test "-var-update nscont" \
280 "\\^done,changelist=\\\[\\\]" \
281 "varobj update after clearing"
282
283 mi_gdb_test "-var-set-visualizer nscont gdb.default_visualizer" \
284 "\\^done" \
285 "choose default visualizer"
286 }
287
288 set cmd "python exception_flag = True"
289 set re [string_list_to_regexp & {"} $cmd \\ n {"} \r\n ^ done]
290 mi_gdb_test $cmd $re
291
292 mi_create_dynamic_varobj nstype2 nstype2 1 \
293 "create nstype2 varobj"
294
295 mi_list_varobj_children nstype2 {
296 { {nstype2.<error at 0>} {<error at 0>} 6 {char \[6\]} }
297 } "list children after setting exception flag"
298
299 mi_create_varobj me me \
300 "create me varobj"
301
302 mi_gdb_test "-var-evaluate-expression me" \
303 "\\^done,value=\"<error reading variable: Cannot access memory.>.*\"" \
304 "evaluate me varobj"
305
306 # Regression test for python/14836.
307 mi_create_dynamic_varobj children_as_list children_as_list 1 \
308 "printer whose children are returned as a list"
309
310 # Test that when a pretty-printer returns a gdb.Value in its to_string, we call
311 # the pretty-printer of that value too.
312 mi_create_varobj_checked tsrvw tsrvw \
313 "struct to_string_returns_value_wrapper" \
314 "create tsrvw varobj"
315 mi_check_varobj_value tsrvw "Inner to_string 1989" "check tsrvw varobj value"
316 mi_gdb_test "-data-evaluate-expression tsrvw" \
317 "\\^done,value=\"Inner to_string 1989\"" \
318 "check tsrvw expression value"
319
320 # Regression test for bug 14741.
321 mi_continue_to_line \
322 [gdb_get_line_number {breakpoint bug 14741} ${srcfile}] \
323 "step to breakpoint for bug 14741"
324
325 mi_create_dynamic_varobj c c 1 \
326 "create varobj for c"
327
328 mi_gdb_test "-var-set-visualizer c ArrayPrinter" \
329 "\\^done" \
330 "choose array visualizer for c"
331
332 mi_list_varobj_children c {
333 { {c.\[0\]} {\[0\]} 0 int }
334 } "list children of c"
335
336 mi_next "next over change of array element"
337
338 mi_gdb_test "-var-update c" \
339 "\\^done,changelist=\\\[{name=\"c.\\\[0\\\]\",in_scope=\"true\",type_changed=\"false\",has_more=\"0\"}\\\]" \
340 "update varobj after element change"
341
342 # C++ MI tests
343 gdb_exit
344 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}-cxx" \
345 executable {debug c++ additional_flags=-DMI}] != "" } {
346 untested "failed to compile in C++ mode"
347 return -1
348 }
349
350 if [mi_gdb_start] {
351 return
352 }
353 mi_delete_breakpoints
354 mi_gdb_reinitialize_dir $srcdir/$subdir
355 mi_gdb_load ${binfile}-cxx
356
357 if {[lsearch -exact [mi_get_features] python] < 0} {
358 unsupported "python support is disabled"
359 return -1
360 }
361
362 with_test_prefix "varobj fake" {
363 mi_runto_main
364 mi_continue_to_line \
365 [gdb_get_line_number {break to inspect struct and union} ${srcfile}] \
366 "step to breakpoint"
367
368 # Test python/12531. Install visualizer on a cplus_fake_child.
369 mi_create_varobj fake fake \
370 "create fake varobj"
371
372 mi_list_varobj_children fake {
373 { fake.private private 1 }
374 } "list children of fake"
375
376 mi_list_varobj_children fake.private {
377 { fake.private.sname sname 0 int }
378 } "list children fake.private"
379
380 mi_gdb_test "-var-set-visualizer fake.private gdb.default_visualizer" \
381 "\\^done" "Install visualizer on a cplus_fake_child"
382 }
383