Update copyright year range in all GDB files
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-format-string.exp
CommitLineData
3666a048 1# Copyright (C) 2009-2021 Free Software Foundation, Inc.
52093e1b
MB
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 the
17# gdb.Value.format_string () method.
18
19load_lib gdb-python.exp
20
21standard_testfile
22
23if [get_compiler_info c++] {
24 return -1
25}
26
05e682e3
SL
27# Skip all tests if Python scripting is not enabled.
28gdb_exit
29gdb_start
30if { [skip_python_tests] } { continue }
31
52093e1b
MB
32# Build inferior to language specification.
33proc build_inferior {exefile lang} {
34 global srcdir subdir srcfile testfile hex
35
36 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } {
37 untested "failed to compile in $lang mode"
38 return -1
39 }
40
41 return 0
42}
43
44# Restart GDB.
45proc prepare_gdb {exefile} {
46 global srcdir subdir srcfile testfile hex
47
48 gdb_exit
49 gdb_start
50 gdb_reinitialize_dir $srcdir/$subdir
51 gdb_load ${exefile}
52
52093e1b
MB
53 if ![runto_main] then {
54 perror "couldn't run to breakpoint"
55 return
56 }
57
58 # Load the pretty printer.
59 set remote_python_file \
60 [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
61 gdb_test_no_output "source ${remote_python_file}" "load python file"
62
63 runto_bp "break here"
64}
65
66# Set breakpoint and run to that breakpoint.
67proc runto_bp {bp} {
68 gdb_breakpoint [gdb_get_line_number $bp]
69 gdb_continue_to_breakpoint $bp
70}
71
72# Set an option using the GDB command in $set_cmd, execute $body, and then
73# restore the option using the GDB command in $unset_cmd.
74proc with_temp_option { set_cmd unset_cmd body } {
75 with_test_prefix $set_cmd {
76 gdb_test "$set_cmd" ".*"
77 uplevel 1 $body
78 gdb_test "$unset_cmd" ".*"
79 }
80}
81
82# A regular expression for a pointer.
83set default_pointer_regexp "0x\[a-fA-F0-9\]+"
84
85# A regular expression for a non-expanded C++ reference.
86#
87# Stringifying a C++ reference produces an address preceeded by a "@" in
88# Python, but, by default, the C++ reference/class is expanded by the
89# GDB print command.
90set default_ref_regexp "@${default_pointer_regexp}"
91
92# The whole content of the C variable a_big_string, i.e. the whole English
93# alphabet repeated 10 times.
94set whole_big_string ""
95set alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
96for {set i 0} {$i < 10} {incr i} {
97 append whole_big_string $alphabet
98}
99unset alphabet
100
101# Produces a potentially cut down version of $whole_big_string like GDB
102# would represent it.
103# $max is the maximum number of characters allowed in the string (but
104# the return value may contain more to accound for the extra quotes and
105# "..." added by GDB).
106proc get_cut_big_string { max } {
107 global whole_big_string
108
109 set whole_size [string length $whole_big_string]
110 if { $max > $whole_size } {
111 return "\"${whole_big_string}\""
112 }
113
114 set cut_string [string range $whole_big_string 0 [expr $max - 1]]
115 return "\"${cut_string}\"..."
116}
117
118# A dictionary mapping from C variable names to their default string
119# representation when using str () or gdb.Value.format_string () with
120# no arguments.
121# This usually matches what the print command prints if used with no
122# options, except for C++ references which are not expanded by
123# default in Python. See the comment above $default_ref_regexp.
124set default_regexp_dict [dict create \
125 "a_point_t" "Pretty Point \\(42, 12\\)" \
126 "a_point_t_pointer" $default_pointer_regexp \
127 "a_point_t_ref" "Pretty Point \\(42, 12\\)" \
128 "another_point" "Pretty Point \\(123, 456\\)" \
9ef62df0 129 "a_struct_with_union" "\\{the_union = \\{an_int = 707406378, a_char = 42 '\\*'\\}\\}" \
52093e1b
MB
130 "an_enum" "ENUM_BAR" \
131 "a_string" "${default_pointer_regexp} \"hello world\"" \
132 "a_binary_string" "${default_pointer_regexp} \"hello\"" \
133 "a_binary_string_array" "\"hello\\\\000world\"" \
134 "a_big_string" [get_cut_big_string 200] \
135 "an_array" "\\{2, 3, 5\\}" \
136 "an_array_with_repetition" "\\{1, 3 <repeats 12 times>, 5, 5, 5\\}" \
137 "a_symbol_pointer" "${default_pointer_regexp} <global_symbol>" \
138 "a_base_ref" "${default_ref_regexp}" \
139 ]
140
141# A sentinel value to pass to function to get them to use a default value
142# instead.
143# Note that we cannot use $undefined for default arguments in function
144# definitions as we would just get the literal "$undefined" string, so
145# we need to repeat the string.
146set undefined "\000UNDEFINED\000"
147
148# Return $value if it's not $undefined, otherwise return the default value
149# (from $default_regexp_dict) for the variable $var.
150proc get_value_or_default { var value } {
151 global undefined
152 if { $value != $undefined } {
153 return $value
154 }
155
156 global default_regexp_dict
157 return [dict get $default_regexp_dict $var]
158}
159
160# Check that using gdb.Value.format_string on the value representing the
161# variable $var produces $expected.
162proc check_format_string {
163 var
164 opts
165 { expected "\000UNDEFINED\000" }
166 { name "\000UNDEFINED\000" }
167 } {
168 global undefined
169
170 set expected [get_value_or_default $var $expected]
171 if { $name == $undefined } {
172 set name "${var} with option ${opts}"
173 }
174
175 gdb_test \
176 "python print (gdb.parse_and_eval ('${var}').format_string (${opts}))" \
177 $expected \
178 $name
179}
180
181# Check that printing $var with no options set, produces the expected
182# output.
183proc check_var_with_no_opts {
184 var
185 { expected "\000UNDEFINED\000" }
186 } {
187 set expected [get_value_or_default $var $expected]
188
189 with_test_prefix "${var}" {
190 check_format_string \
191 $var \
192 "" \
193 $expected \
194 "no opts"
195 # str () should behave like gdb.Value.format_string () with no args.
196 gdb_test \
197 "python print (str (gdb.parse_and_eval ('${var}')))" \
198 $expected \
199 "str"
200 }
201}
202
203# Check that printing $var with $opt set to True and set to False,
204# produces the expected output.
205proc check_var_with_bool_opt {
206 opt
207 var
208 { true_expected "\000UNDEFINED\000" }
209 { false_expected "\000UNDEFINED\000" }
210 } {
211 set true_expected [get_value_or_default $var $true_expected]
212 set false_expected [get_value_or_default $var $false_expected]
213
214 with_test_prefix "${var} with option ${opt}" {
215 # Option set to True.
216 check_format_string \
217 $var \
218 "${opt}=True" \
219 $true_expected \
220 "${opt}=true"
221 # Option set to False.
222 check_format_string \
223 $var \
224 "${opt}=False" \
225 $false_expected \
226 "${opt}=false"
227 }
228}
229
230# Test gdb.Value.format_string with no options.
231proc test_no_opts {} {
232 global current_lang
233
234 check_var_with_no_opts "a_point_t"
235 check_var_with_no_opts "a_point_t_pointer"
236 check_var_with_no_opts "another_point"
237 check_var_with_no_opts "a_struct_with_union"
238 check_var_with_no_opts "an_enum"
239 check_var_with_no_opts "a_string"
240 check_var_with_no_opts "a_binary_string"
241 check_var_with_no_opts "a_binary_string_array"
242 check_var_with_no_opts "a_big_string"
243 check_var_with_no_opts "an_array"
244 check_var_with_no_opts "an_array_with_repetition"
245 check_var_with_no_opts "a_symbol_pointer"
246
247 if { $current_lang == "c++" } {
248 # Nothing changes in all of the C++ tests because deref_refs is not
249 # True.
250 check_var_with_no_opts "a_point_t_ref"
251 check_var_with_no_opts "a_base_ref"
252 }
253}
254
255# Test the raw option for gdb.Value.format_string.
256proc test_raw {} {
257 global current_lang
258 global default_ref_regexp
259
260 check_var_with_bool_opt "raw" "a_point_t" \
261 "{x = 42, y = 12}"
262 check_var_with_bool_opt "raw" "a_point_t_pointer"
263 check_var_with_bool_opt "raw" "another_point" \
264 "{x = 123, y = 456}"
265 check_var_with_bool_opt "raw" "a_struct_with_union"
266 check_var_with_bool_opt "raw" "an_enum"
267 check_var_with_bool_opt "raw" "a_string"
268 check_var_with_bool_opt "raw" "a_binary_string"
269 check_var_with_bool_opt "raw" "a_binary_string_array"
270 check_var_with_bool_opt "raw" "a_big_string"
271 check_var_with_bool_opt "raw" "an_array"
272 check_var_with_bool_opt "raw" "an_array_with_repetition"
273 check_var_with_bool_opt "raw" "a_symbol_pointer"
274
275 if { $current_lang == "c++" } {
276 check_var_with_bool_opt "raw" "a_point_t_ref" \
277 ${default_ref_regexp}
278 check_var_with_bool_opt "raw" "a_base_ref"
279 }
280
281 with_temp_option \
282 "disable pretty-printer '' test_lookup_function" \
283 "enable pretty-printer '' test_lookup_function" {
284 check_var_with_no_opts "a_point_t" \
285 "{x = 42, y = 12}"
286 check_var_with_bool_opt "raw" "a_point_t" \
287 "{x = 42, y = 12}" \
288 "{x = 42, y = 12}"
289 }
290}
291
292# Test the pretty_arrays option for gdb.Value.format_string.
293proc test_pretty_arrays {} {
294 global current_lang
295
d5cf82c0 296 set an_array_pretty "\\{\[\r\n\]+ 2,\[\r\n\]+ 3,\[\r\n\]+ 5\[\r\n\]+\\}"
52093e1b 297 set an_array_with_repetition_pretty \
d5cf82c0 298 "\\{\[\r\n\]+ 1,\[\r\n\]+ 3 <repeats 12 times>,\[\r\n\]+ 5,\[\r\n\]+ 5,\[\r\n\]+ 5\[\r\n\]+\\}"
52093e1b
MB
299
300 check_var_with_bool_opt "pretty_arrays" "a_point_t"
301 check_var_with_bool_opt "pretty_arrays" "a_point_t_pointer"
302 check_var_with_bool_opt "pretty_arrays" "another_point"
303 check_var_with_bool_opt "pretty_arrays" "a_struct_with_union"
304 check_var_with_bool_opt "pretty_arrays" "an_enum"
305 check_var_with_bool_opt "pretty_arrays" "a_string"
306 check_var_with_bool_opt "pretty_arrays" "a_binary_string"
307 check_var_with_bool_opt "pretty_arrays" "a_binary_string_array"
308 check_var_with_bool_opt "pretty_arrays" "a_big_string"
309 check_var_with_bool_opt "pretty_arrays" "an_array" \
310 $an_array_pretty
311 check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
312 $an_array_with_repetition_pretty
313 check_var_with_bool_opt "pretty_arrays" "a_symbol_pointer"
314
315 if { $current_lang == "c++" } {
316 check_var_with_bool_opt "pretty_arrays" "a_point_t_ref"
317 check_var_with_bool_opt "pretty_arrays" "a_base_ref"
318 }
319
320 with_temp_option "set print array on" "set print array off" {
321 check_var_with_no_opts "an_array" \
322 $an_array_pretty
323 check_var_with_bool_opt "pretty_arrays" "an_array" \
324 $an_array_pretty
325
326 check_var_with_no_opts "an_array_with_repetition" \
327 $an_array_with_repetition_pretty
328 check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
329 $an_array_with_repetition_pretty
330 }
331}
332
333# Test the pretty_structs option for gdb.Value.format_string.
334proc test_pretty_structs {} {
335 global current_lang
336
337 set a_struct_with_union_pretty \
9ef62df0 338 "\\{\[\r\n\]+ the_union = \\{\[\r\n\]+ an_int = 707406378,\[\r\n\]+ a_char = 42 '\\*'\[\r\n\]+ \\}\[\r\n\]+\\}"
52093e1b
MB
339
340 check_var_with_bool_opt "pretty_structs" "a_point_t"
341 check_var_with_bool_opt "pretty_structs" "a_point_t_pointer"
342 check_var_with_bool_opt "pretty_structs" "another_point"
343 check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
344 $a_struct_with_union_pretty
345 check_var_with_bool_opt "pretty_structs" "an_enum"
346 check_var_with_bool_opt "pretty_structs" "a_string"
347 check_var_with_bool_opt "pretty_structs" "a_binary_string"
348 check_var_with_bool_opt "pretty_structs" "a_binary_string_array"
349 check_var_with_bool_opt "pretty_structs" "a_big_string"
350 check_var_with_bool_opt "pretty_structs" "an_array"
351 check_var_with_bool_opt "pretty_structs" "an_array_with_repetition"
352 check_var_with_bool_opt "pretty_structs" "a_symbol_pointer"
353
354 if { $current_lang == "c++" } {
355 check_var_with_bool_opt "pretty_structs" "a_point_t_ref"
356 check_var_with_bool_opt "pretty_structs" "a_base_ref"
357 }
358
359 with_temp_option "set print structs on" "set print structs off" {
360 check_var_with_no_opts "a_struct_with_union"
361 check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
362 $a_struct_with_union_pretty
363 }
364
365 # point_t is usually printed through the pretty printer.
366 # Try disabling it.
367 with_temp_option \
368 "disable pretty-printer '' test_lookup_function" \
369 "enable pretty-printer '' test_lookup_function" {
370 check_var_with_no_opts "a_point_t" \
371 "{x = 42, y = 12}"
372 check_var_with_bool_opt "pretty_structs" "a_point_t" \
373 "\\{\[\r\n\]+ x = 42, *\[\r\n\]+ y = 12\[\r\n\]+\\}" \
374 "{x = 42, y = 12}" \
375 }
376}
377
378# Test the array_indexes option for gdb.Value.format_string.
379proc test_array_indexes {} {
380 global current_lang
381
382 set an_array_with_indexes "\\{\\\[0\\\] = 2, \\\[1\\\] = 3, \\\[2\\\] = 5\\}"
383 set an_array_with_repetition_with_indexes \
384 "\\{\\\[0\\\] = 1, \\\[1\\\] = 3 <repeats 12 times>, \\\[13\\\] = 5, \\\[14\\\] = 5, \\\[15\\\] = 5\\}"
385
386 check_var_with_bool_opt "array_indexes" "a_point_t"
387 check_var_with_bool_opt "array_indexes" "a_point_t_pointer"
388 check_var_with_bool_opt "array_indexes" "another_point"
389 check_var_with_bool_opt "array_indexes" "a_struct_with_union"
390 check_var_with_bool_opt "array_indexes" "an_enum"
391 check_var_with_bool_opt "array_indexes" "a_string"
392 check_var_with_bool_opt "array_indexes" "a_binary_string"
393 check_var_with_bool_opt "array_indexes" "a_binary_string_array"
394 check_var_with_bool_opt "array_indexes" "a_big_string"
395 check_var_with_bool_opt "array_indexes" "an_array" \
396 $an_array_with_indexes
397 check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
398 $an_array_with_repetition_with_indexes
399 check_var_with_bool_opt "array_indexes" "a_symbol_pointer"
400
401 if { $current_lang == "c++" } {
402 check_var_with_bool_opt "array_indexes" "a_point_t_ref"
403 check_var_with_bool_opt "array_indexes" "a_base_ref"
404 }
405
406 with_temp_option \
407 "set print array-indexes on" \
408 "set print array-indexes off" {
409 check_var_with_no_opts "an_array" \
410 $an_array_with_indexes
411 check_var_with_bool_opt "array_indexes" "an_array" \
412 $an_array_with_indexes
413
414 check_var_with_no_opts "an_array_with_repetition" \
415 $an_array_with_repetition_with_indexes
416 check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
417 $an_array_with_repetition_with_indexes
418 }
419}
420
421# Test the symbols option for gdb.Value.format_string.
422proc test_symbols {} {
423 global undefined
424 global current_lang
425 global default_pointer_regexp
426
427 check_var_with_bool_opt "symbols" "a_point_t"
428 check_var_with_bool_opt "symbols" "a_point_t_pointer"
429 check_var_with_bool_opt "symbols" "another_point"
430 check_var_with_bool_opt "symbols" "a_struct_with_union"
431 check_var_with_bool_opt "symbols" "an_enum"
432 check_var_with_bool_opt "symbols" "a_string"
433 check_var_with_bool_opt "symbols" "a_binary_string"
434 check_var_with_bool_opt "symbols" "a_binary_string_array"
435 check_var_with_bool_opt "symbols" "a_big_string"
436 check_var_with_bool_opt "symbols" "an_array"
437 check_var_with_bool_opt "symbols" "an_array_with_repetition"
438 check_var_with_bool_opt "symbols" "a_symbol_pointer" \
439 $undefined \
440 $default_pointer_regexp
441
442 if { $current_lang == "c++" } {
443 check_var_with_bool_opt "symbols" "a_point_t_ref"
444 check_var_with_bool_opt "symbols" "a_base_ref"
445 }
446
447 with_temp_option "set print symbol off" "set print symbol on" {
448 check_var_with_no_opts "a_symbol_pointer" \
449 $default_pointer_regexp
450 check_var_with_bool_opt "symbols" "a_symbol_pointer" \
451 $undefined \
452 $default_pointer_regexp
453 }
454}
455
456# Test the unions option for gdb.Value.format_string.
457proc test_unions {} {
458 global undefined
459 global current_lang
460
461 check_var_with_bool_opt "unions" "a_point_t"
462 check_var_with_bool_opt "unions" "a_point_t_pointer"
463 check_var_with_bool_opt "unions" "another_point"
464 check_var_with_bool_opt "unions" "a_struct_with_union" \
465 $undefined \
466 "\\{the_union = \\{...\\}\\}"
467 check_var_with_bool_opt "unions" "an_enum"
468 check_var_with_bool_opt "unions" "a_string"
469 check_var_with_bool_opt "unions" "a_binary_string"
470 check_var_with_bool_opt "unions" "a_binary_string_array"
471 check_var_with_bool_opt "unions" "a_big_string"
472 check_var_with_bool_opt "unions" "an_array"
473 check_var_with_bool_opt "unions" "an_array_with_repetition"
474 check_var_with_bool_opt "unions" "a_symbol_pointer"
475
476 if { $current_lang == "c++" } {
477 check_var_with_bool_opt "unions" "a_point_t_ref"
478 check_var_with_bool_opt "unions" "a_base_ref"
479 }
480
481 with_temp_option "set print union off" "set print union on" {
482 check_var_with_no_opts "a_struct_with_union" \
483 "\\{the_union = \\{...\\}\\}"
484 check_var_with_bool_opt "unions" "a_struct_with_union" \
485 $undefined \
486 "\\{the_union = \\{...\\}\\}"
487 }
488}
489
4aea001f
HD
490# Test the address option for gdb.Value.format_string.
491proc test_address {} {
492 global undefined
493 global current_lang
494
495 check_var_with_bool_opt "address" "a_point_t"
496 check_var_with_bool_opt "address" "a_point_t_pointer" \
497 $undefined \
498 ""
499 check_var_with_bool_opt "address" "another_point"
500 check_var_with_bool_opt "symbols" "a_struct_with_union"
501 check_var_with_bool_opt "address" "an_enum"
502 check_var_with_bool_opt "address" "a_string" \
503 $undefined \
504 "\"hello world\""
505 check_var_with_bool_opt "address" "a_binary_string" \
506 $undefined \
507 "\"hello\""
508 check_var_with_bool_opt "address" "a_binary_string_array"
509 check_var_with_bool_opt "address" "a_big_string"
510 check_var_with_bool_opt "address" "an_array"
511 check_var_with_bool_opt "address" "an_array_with_repetition"
512 check_var_with_bool_opt "address" "a_symbol_pointer" \
513 $undefined \
514 "<global_symbol>"
515
516 if { $current_lang == "c++" } {
517 check_var_with_bool_opt "address" "a_point_t_ref"
518 check_var_with_bool_opt "address" "a_base_ref" \
519 $undefined \
520 ""
521 }
522
523 with_temp_option "set print address off" "set print address on" {
524 check_var_with_no_opts "a_string" \
525 "\"hello world\""
526 check_var_with_bool_opt "address" "a_string" \
527 $undefined \
528 "\"hello world\""
529 }
530}
531
52093e1b
MB
532# Test the deref_refs option for gdb.Value.format_string.
533proc test_deref_refs {} {
534 global current_lang
535 global default_pointer_regexp
536 global default_ref_regexp
537
538 check_var_with_bool_opt "deref_refs" "a_point_t"
539 check_var_with_bool_opt "deref_refs" "a_point_t_pointer"
540 check_var_with_bool_opt "deref_refs" "another_point"
541 check_var_with_bool_opt "deref_refs" "a_struct_with_union"
542 check_var_with_bool_opt "deref_refs" "an_enum"
543 check_var_with_bool_opt "deref_refs" "a_string"
544 check_var_with_bool_opt "deref_refs" "a_binary_string"
545 check_var_with_bool_opt "deref_refs" "a_binary_string_array"
546 check_var_with_bool_opt "deref_refs" "a_big_string"
547 check_var_with_bool_opt "deref_refs" "an_array"
548 check_var_with_bool_opt "deref_refs" "an_array_with_repetition"
549 check_var_with_bool_opt "deref_refs" "a_symbol_pointer"
550
551 if { $current_lang == "c++" } {
552 check_var_with_bool_opt "deref_refs" "a_point_t_ref"
553 check_var_with_bool_opt "deref_refs" "a_base_ref" \
934a1764 554 "${default_ref_regexp}: \\{_vptr\[.\$\]Base = ${default_pointer_regexp} <vtable for Deriv\\+16>, a = 42, static a_static_member = 2019\\}"
52093e1b
MB
555 }
556}
557
558# Test the actual_objects option for gdb.Value.format_string.
559proc test_actual_objects {} {
560 global current_lang
561
562 check_var_with_bool_opt "actual_objects" "a_point_t"
563 check_var_with_bool_opt "actual_objects" "a_point_t_pointer"
564 check_var_with_bool_opt "actual_objects" "another_point"
565 check_var_with_bool_opt "actual_objects" "a_struct_with_union"
566 check_var_with_bool_opt "actual_objects" "an_enum"
567 check_var_with_bool_opt "actual_objects" "a_string"
568 check_var_with_bool_opt "actual_objects" "a_binary_string"
569 check_var_with_bool_opt "actual_objects" "a_binary_string_array"
570 check_var_with_bool_opt "actual_objects" "a_big_string"
571 check_var_with_bool_opt "actual_objects" "an_array"
572 check_var_with_bool_opt "actual_objects" "an_array_with_repetition"
573 check_var_with_bool_opt "actual_objects" "a_symbol_pointer"
574
575 if { $current_lang == "c++" } {
576 # Nothing changes in all of the C++ tests because deref_refs is not
577 # True.
578 check_var_with_bool_opt "actual_objects" "a_point_t_ref"
579 check_var_with_bool_opt "actual_objects" "a_base_ref"
580
581 with_temp_option "set print object on" "set print object off" {
582 check_var_with_no_opts "a_point_t_ref"
583 check_var_with_bool_opt "actual_objects" "a_point_t_ref"
584
585 check_var_with_no_opts "a_base_ref"
586 check_var_with_bool_opt "actual_objects" "a_base_ref"
587 }
588 }
589}
590
591# Test the static_members option for gdb.Value.format_string.
592proc test_static_members {} {
593 global current_lang
594
595 check_var_with_bool_opt "static_members" "a_point_t"
596 check_var_with_bool_opt "static_members" "a_point_t_pointer"
597 check_var_with_bool_opt "static_members" "another_point"
598 check_var_with_bool_opt "static_members" "a_struct_with_union"
599 check_var_with_bool_opt "static_members" "an_enum"
600 check_var_with_bool_opt "static_members" "a_string"
601 check_var_with_bool_opt "static_members" "a_binary_string"
602 check_var_with_bool_opt "static_members" "a_binary_string_array"
603 check_var_with_bool_opt "static_members" "a_big_string"
604 check_var_with_bool_opt "static_members" "an_array"
605 check_var_with_bool_opt "static_members" "an_array_with_repetition"
606 check_var_with_bool_opt "static_members" "a_symbol_pointer"
607
608 if { $current_lang == "c++" } {
609 # Nothing changes in all of the C++ tests because deref_refs is not
610 # True.
611 check_var_with_bool_opt "static_members" "a_point_t_ref"
612 check_var_with_bool_opt "static_members" "a_base_ref"
613
614 with_temp_option \
615 "set print static-members off" \
616 "set print static-members on" {
617 check_var_with_no_opts "a_point_t_ref"
618 check_var_with_bool_opt "static_members" "a_point_t_ref"
619
620 check_var_with_no_opts "a_base_ref"
621 check_var_with_bool_opt "static_members" "a_base_ref"
622 }
623 }
624}
625
626# Test the max_elements option for gdb.Value.format_string.
627proc test_max_elements {} {
628 global current_lang
629 global default_pointer_regexp
630
631 # 200 is the default maximum number of elements, so setting it should
632 # not change the output.
633 set opts "max_elements=200"
634 with_test_prefix $opts {
635 check_format_string "a_point_t" $opts
636 check_format_string "a_point_t_pointer" $opts
637 check_format_string "another_point" $opts
638 check_format_string "a_struct_with_union" $opts
639 check_format_string "an_enum" $opts
640 check_format_string "a_string" $opts
641 check_format_string "a_binary_string" $opts
642 check_format_string "a_binary_string_array" $opts
643 check_format_string "a_big_string" $opts
644 check_format_string "an_array" $opts
645 check_format_string "an_array_with_repetition" $opts
646 check_format_string "a_symbol_pointer" $opts
647
648 if { $current_lang == "c++" } {
649 check_format_string "a_point_t_ref" $opts
650 check_format_string "a_base_ref" $opts
651 }
652 }
653
654 set opts "max_elements=3"
655 with_test_prefix $opts {
656 check_format_string "a_point_t" $opts
657 check_format_string "a_point_t_pointer" $opts
658 check_format_string "another_point" $opts
659 check_format_string "a_struct_with_union" $opts
660 check_format_string "an_enum" $opts
661 check_format_string "a_string" $opts \
662 "${default_pointer_regexp} \"hel\"..."
663 check_format_string "a_binary_string" $opts \
664 "${default_pointer_regexp} \"hel\"..."
665 # This will print four characters instead of three, see
666 # <https://sourceware.org/bugzilla/show_bug.cgi?id=24331>.
667 check_format_string "a_binary_string_array" $opts \
668 "\"hell\"..."
669 check_format_string "a_big_string" $opts \
670 [get_cut_big_string 3]
671 check_format_string "an_array" $opts
672 check_format_string "an_array_with_repetition" $opts \
673 "\\{1, 3 <repeats 12 times>...\\}"
674 check_format_string "a_symbol_pointer" $opts
675
676 if { $current_lang == "c++" } {
677 check_format_string "a_point_t_ref" $opts
678 check_format_string "a_base_ref" $opts
679 }
680 }
681
682 # Both 1,000 (we don't have that many elements) and 0 (unlimited) should
683 # mean no truncation.
684 foreach opts { "max_elements=1000" "max_elements=0" } {
685 with_test_prefix $opts {
686 check_format_string "a_point_t" $opts
687 check_format_string "a_point_t_pointer" $opts
688 check_format_string "another_point" $opts
689 check_format_string "a_struct_with_union" $opts
690 check_format_string "an_enum" $opts
691 check_format_string "a_string" $opts
692 check_format_string "a_binary_string" $opts
693 check_format_string "a_binary_string_array" $opts
694 check_format_string "a_big_string" $opts \
695 [get_cut_big_string 1000]
696 check_format_string "an_array" $opts
697 check_format_string "an_array_with_repetition" $opts
698 check_format_string "a_symbol_pointer" $opts
699
700 if { $current_lang == "c++" } {
701 check_format_string "a_point_t_ref" $opts
702 check_format_string "a_base_ref" $opts
703 }
704 }
705 }
706
707 with_temp_option "set print elements 4" "set print elements 200" {
708 check_format_string "a_string" "" \
709 "${default_pointer_regexp} \"hell\"..."
710 check_format_string "a_binary_string" "" \
711 "${default_pointer_regexp} \"hell\"..."
712 check_format_string "a_binary_string_array" "" \
713 "\"hell\"..."
714 check_format_string "an_array_with_repetition" "" \
715 "\\{1, 3 <repeats 12 times>...\\}"
716 }
717}
718
2e62ab40
AB
719# Test the max_depth option for gdb.Value.format_string.
720proc test_max_depth {} {
721 set opts "max_depth=-1"
722 with_test_prefix $opts {
723 check_format_string "a_struct_with_union" $opts
724 }
725 set opts "max_depth=0"
726 with_test_prefix $opts {
727 check_format_string "a_struct_with_union" $opts "\\{\.\.\.\\}"
728 }
729 set opts "max_depth=1"
730 with_test_prefix $opts {
731 check_format_string "a_struct_with_union" $opts "\\{the_union = \\{\.\.\.\\}\\}"
732 }
733 set opts "max_depth=2"
734 with_test_prefix $opts {
735 check_format_string "a_struct_with_union" $opts
736 }
737}
738
52093e1b
MB
739# Test the repeat_threshold option for gdb.Value.format_string.
740proc test_repeat_threshold {} {
741 global current_lang
742 global default_pointer_regexp
743
744 # 10 is the default threshold for repeated items, so setting it should
745 # not change the output.
746 set opts "repeat_threshold=10"
747 with_test_prefix $opts {
748 check_format_string "a_point_t" $opts
749 check_format_string "a_point_t_pointer" $opts
750 check_format_string "another_point" $opts
751 check_format_string "a_struct_with_union" $opts
752 check_format_string "an_enum" $opts
753 check_format_string "a_string" $opts
754 check_format_string "a_binary_string" $opts
755 check_format_string "a_binary_string_array" $opts
756 check_format_string "a_big_string" $opts
757 check_format_string "an_array" $opts
758 check_format_string "an_array_with_repetition" $opts
759 check_format_string "a_symbol_pointer" $opts
760
761 if { $current_lang == "c++" } {
762 check_format_string "a_point_t_ref" $opts
763 check_format_string "a_base_ref" $opts
764 }
765 }
766
767 set opts "repeat_threshold=1"
768 with_test_prefix $opts {
769 check_format_string "a_point_t" $opts
770 check_format_string "a_point_t_pointer" $opts
771 check_format_string "another_point" $opts
772 check_format_string "a_struct_with_union" $opts
773 check_format_string "an_enum" $opts
774 check_format_string "a_string" $opts \
775 "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o world\""
776 check_format_string "a_binary_string" $opts \
777 "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o\""
778 check_format_string "a_binary_string_array" $opts \
779 "\"he\", 'l' <repeats 2 times>, \"o\\\\000world\""
780 check_format_string "a_big_string" $opts
781 check_format_string "an_array" $opts
782 check_format_string "an_array_with_repetition" $opts \
783 "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
784
785 check_format_string "a_symbol_pointer" $opts
786
787 if { $current_lang == "c++" } {
788 check_format_string "a_point_t_ref" $opts
789 check_format_string "a_base_ref" $opts
790 }
791 }
792
793 set opts "repeat_threshold=3"
794 with_test_prefix $opts {
795 check_format_string "a_point_t" $opts
796 check_format_string "a_point_t_pointer" $opts
797 check_format_string "another_point" $opts
798 check_format_string "a_struct_with_union" $opts
799 check_format_string "an_enum" $opts
800 check_format_string "a_string" $opts
801 check_format_string "a_binary_string" $opts
802 check_format_string "a_binary_string_array" $opts
803 check_format_string "a_big_string" $opts
804 check_format_string "an_array" $opts
805 check_format_string "an_array_with_repetition" $opts
806 check_format_string "a_symbol_pointer" $opts
807
808 if { $current_lang == "c++" } {
809 check_format_string "a_point_t_ref" $opts
810 check_format_string "a_base_ref" $opts
811 }
812 }
813
814 # Both 100 (we don't have that many repeated elements) and 0 (unlimited)
815 # should mean no truncation.
816 foreach opts { "repeat_threshold=100" "repeat_threshold=0" } {
817 with_test_prefix $opts {
818 check_format_string "a_point_t" $opts
819 check_format_string "a_point_t_pointer" $opts
820 check_format_string "another_point" $opts
821 check_format_string "a_struct_with_union" $opts
822 check_format_string "an_enum" $opts
823 check_format_string "a_string" $opts
824 check_format_string "a_binary_string" $opts
825 check_format_string "a_binary_string_array" $opts
826 check_format_string "a_big_string" $opts
827 check_format_string "an_array" $opts
828 check_format_string "an_array_with_repetition" $opts \
829 "\\{1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5\\}"
830 check_format_string "a_symbol_pointer" $opts
831
832 if { $current_lang == "c++" } {
833 check_format_string "a_point_t_ref" $opts
834 check_format_string "a_base_ref" $opts
835 }
836 }
837 }
838
839 with_temp_option "set print repeats 1" "set print repeats 10" {
840 check_format_string "an_array_with_repetition" "" \
841 "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
842 }
843}
844
845# Test the format option for gdb.Value.format_string.
846proc test_format {} {
847 global current_lang
848 global default_pointer_regexp
849
850 # Hexadecimal.
851 set opts "format='x'"
852 with_test_prefix $opts {
853 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
854 "0x2a" \
855 "42 with option ${opts}"
856
857 check_format_string "a_point_t" $opts
858 check_format_string "a_point_t_pointer" $opts
859 check_format_string "another_point" $opts
860 check_format_string "a_struct_with_union" $opts \
9ef62df0 861 "\\{the_union = \\{an_int = 0x2a2a2a2a, a_char = 0x2a\\}\\}"
52093e1b
MB
862 check_format_string "an_enum" $opts \
863 "0x1"
864 check_format_string "a_string" $opts \
865 $default_pointer_regexp
866 check_format_string "a_binary_string" $opts \
867 $default_pointer_regexp
868 check_format_string "a_binary_string_array" $opts \
869 "\\{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0\\}"
870 check_format_string "a_big_string" $opts \
871 "\\{0x41, 0x42, 0x43, 0x44, 0x45, \[, x0-9a-f\]+\.\.\.\\}"
872 check_format_string "an_array" $opts \
873 "\\{0x2, 0x3, 0x5\\}"
874 check_format_string "an_array_with_repetition" $opts \
875 "\\{0x1, 0x3 <repeats 12 times>, 0x5, 0x5, 0x5\\}"
876 check_format_string "a_symbol_pointer" $opts \
877 $default_pointer_regexp
878
879 if { $current_lang == "c++" } {
880 check_format_string "a_point_t_ref" $opts
881 check_format_string "a_base_ref" $opts
882 }
883 }
884
885 # Decimal.
886 set opts "format='d'"
887 with_test_prefix $opts {
888 set decimal_pointer_regexp "\[0-9\]+"
889 gdb_test "python print (gdb.Value (0x2a).format_string (${opts}))" \
890 "42" \
891 "0x2a with option ${opts}"
892
893 check_format_string "a_point_t" $opts
894 check_format_string "a_point_t_pointer" $opts \
895 $decimal_pointer_regexp
896 check_format_string "another_point" $opts
897 check_format_string "a_struct_with_union" $opts \
9ef62df0 898 "\\{the_union = \\{an_int = 707406378, a_char = 42\\}\\}"
52093e1b
MB
899 check_format_string "an_enum" $opts \
900 "1"
901 check_format_string "a_string" $opts \
902 $decimal_pointer_regexp
903 check_format_string "a_binary_string" $opts \
904 $decimal_pointer_regexp
905 check_format_string "a_binary_string_array" $opts \
906 "\\{104, 101, 108, 108, 111, 0, 119, 111, 114, 108, 100, 0\\}"
907 check_format_string "a_big_string" $opts \
908 "\\{65, 66, 67, 68, 69, \[, 0-9\]+\.\.\.\\}"
909 check_format_string "an_array" $opts
910 check_format_string "an_array_with_repetition" $opts
911 check_format_string "a_symbol_pointer" $opts \
912 $decimal_pointer_regexp
913
914 if { $current_lang == "c++" } {
915 check_format_string "a_point_t_ref" $opts
916 check_format_string "a_base_ref" $opts
917 }
918 }
919}
920
921# Test mixing options.
922proc test_mixed {} {
923 global current_lang
924 global default_ref_regexp
925 global default_pointer_regexp
926
927 check_format_string "a_point_t" \
928 "raw=True, format='x'" \
929 "\\{x = 0x2a, y = 0xc\\}"
930
931 check_format_string "an_array" \
932 "array_indexes=True, pretty_arrays=True" \
d5cf82c0 933 "\\{\[\r\n\]+ \\\[0\\\] = 2,\[\r\n\]+ \\\[1\\\] = 3,\[\r\n\]+ \\\[2\\\] = 5\[\r\n\]+\\}"
52093e1b
MB
934
935 check_format_string "a_struct_with_union" \
936 "pretty_structs=True, unions=False" \
937 "\\{\[\r\n\]+ the_union = \\{\.\.\.\\}\[\r\n\]+\\}"
938
939 check_format_string "a_symbol_pointer" \
940 "symbols=False, format='d'" \
941 "\[0-9\]+"
942
943 if { $current_lang == "c++" } {
944 check_format_string "a_point_t_ref" \
945 "deref_refs=True, actual_objects=True, raw=True" \
946 "${default_ref_regexp}: \\{x = 42, y = 12\\}"
947
948 check_format_string "a_base_ref" \
949 "deref_refs=True, static_members=False" \
934a1764 950 "${default_ref_regexp}: \\{_vptr\[.\$\]Base = ${default_pointer_regexp} <vtable for Deriv\\+16>, a = 42\\}"
52093e1b
MB
951 }
952}
953
954# Test passing invalid arguments to gdb.Value.format_string.
955proc test_invalid_args {} {
956 check_format_string \
957 "a_point_t" \
958 "12" \
959 "TypeError: format_string\\(\\) takes 0 positional arguments but 1 were given.*"
960
961 check_format_string \
962 "a_point_t" \
963 "invalid=True" \
964 "TypeError: 'invalid' is an invalid keyword argument for this function.*"
965
966 check_format_string \
967 "a_point_t" \
968 "raw='hello'" \
969 "TypeError: argument 1 must be bool, not str.*"
970
971 check_format_string \
972 "a_point_t" \
973 "format='xd'" \
974 "ValueError: a single character is required.*"
975}
976
977# Run all the tests in common for both C and C++.
978proc test_all_common {} {
979 # No options.
980 test_no_opts
981 # Single options set to True/False.
982 test_raw
983 test_pretty_arrays
984 test_pretty_structs
985 test_array_indexes
986 test_symbols
987 test_unions
4aea001f 988 test_address
52093e1b
MB
989 test_deref_refs
990 test_actual_objects
991 test_static_members
992 test_max_elements
2e62ab40 993 test_max_depth
52093e1b
MB
994 test_repeat_threshold
995 test_format
996 # Multiple options mixed together.
997 test_mixed
998 # Various error conditions.
999 test_invalid_args
1000}
1001
1002# The current language ("c" or "c++" while running tests).
1003set current_lang ""
1004
1005with_test_prefix "format_string" {
1006 # Perform C Tests.
1007 if { [build_inferior "${binfile}" "c"] == 0 } {
1008 with_test_prefix "lang_c" {
1009 set current_lang "c"
1010 prepare_gdb "${binfile}"
1011 test_all_common
1012 }
1013 }
1014
1015 # Perform C++ Tests.
1016 if { [build_inferior "${binfile}-cxx" "c++"] == 0 } {
1017 with_test_prefix "lang_cpp" {
1018 set current_lang "c++"
1019 prepare_gdb "${binfile}-cxx"
1020 test_all_common
1021 }
1022 }
1023}
This page took 0.300211 seconds and 4 git commands to generate.