1 # Copyright
(C
) 2009-2022 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 the
17 # gdb.Value.format_string
() method.
19 load_lib gdb
-python.exp
23 if [get_compiler_info c
++] {
27 # Skip all tests
if Python scripting is not enabled.
30 if { [skip_python_tests
] } { continue }
32 # Build inferior to language specification.
33 proc build_inferior
{exefile lang
} {
34 global srcdir subdir srcfile testfile hex
36 set flags
[list debug $lang
]
38 lappend flags additional_flags
=-std
=c99
41 if { [gdb_compile
"${srcdir}/${subdir}/${srcfile}" "${exefile}" \
42 executable $flags
] != "" } {
43 untested
"failed to compile in $lang mode"
51 proc prepare_gdb
{exefile
} {
52 global srcdir subdir srcfile testfile hex
56 gdb_reinitialize_dir $srcdir
/$subdir
59 if ![runto_main
] then {
60 perror
"couldn't run to breakpoint"
64 #
Load the pretty printer.
65 set remote_python_file \
66 [gdb_remote_download host $
{srcdir
}/$
{subdir
}/$
{testfile
}.py
]
67 gdb_test_no_output
"source ${remote_python_file}" "load python file"
72 #
Set breakpoint and run to that breakpoint.
74 gdb_breakpoint
[gdb_get_line_number $bp
]
75 gdb_continue_to_breakpoint $bp
78 #
Set an option using the GDB command in $set_cmd
, execute $body
, and
then
79 # restore the option using the GDB command in $unset_cmd.
80 proc with_temp_option
{ set_cmd unset_cmd body
} {
81 with_test_prefix $set_cmd
{
82 gdb_test
"$set_cmd" ".*"
84 gdb_test
"$unset_cmd" ".*"
88 # A regular expression
for a pointer.
89 set default_pointer_regexp
"0x\[a-fA-F0-9\]+"
91 # A regular expression
for a non
-expanded C
++ reference.
93 # Stringifying a C
++ reference produces an address preceeded by a
"@" in
94 # Python
, but
, by default
, the C
++ reference
/class is expanded by the
96 set default_ref_regexp
"@${default_pointer_regexp}"
98 # The whole content of the C
variable a_big_string
, i.e. the whole English
99 # alphabet repeated
10 times.
100 set whole_big_string
""
101 set alphabet
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
102 for {set i
0} {$i
< 10} {incr i
} {
103 append whole_big_string $alphabet
107 # Produces a potentially cut down version of $whole_big_string like GDB
108 # would represent it.
109 # $
max is the maximum number of characters allowed in the string
(but
110 # the
return value may contain more to accound
for the extra quotes and
111 #
"..." added by GDB).
112 proc get_cut_big_string
{ max } {
113 global whole_big_string
115 set whole_size
[string length $whole_big_string
]
116 if { $
max > $whole_size
} {
117 return "\"${whole_big_string}\""
120 set cut_string
[string range $whole_big_string
0 [expr $
max - 1]]
121 return "\"${cut_string}\"..."
124 # A dictionary mapping from C
variable names to their default string
125 # representation when using str
() or gdb.Value.format_string
() with
127 # This usually matches what the print command prints
if used with no
128 # options
, except
for C
++ references which are not expanded by
129 # default in Python. See the comment above $default_ref_regexp.
130 set default_regexp_dict
[dict create \
131 "a_point_t" "Pretty Point \\(42, 12\\)" \
132 "a_point_t_pointer" $default_pointer_regexp \
133 "a_point_t_ref" "Pretty Point \\(42, 12\\)" \
134 "another_point" "Pretty Point \\(123, 456\\)" \
135 "a_struct_with_point" "\\{the_point = Pretty Point \\(42, 12\\)\\}" \
136 "a_struct_with_union" "\\{the_union = \\{an_int = 707406378, a_char = 42 '\\*'\\}\\}" \
137 "an_enum" "ENUM_BAR" \
138 "a_string" "${default_pointer_regexp} \"hello world\"" \
139 "a_binary_string" "${default_pointer_regexp} \"hello\"" \
140 "a_binary_string_array" "\"hello\\\\000world\"" \
141 "a_big_string" [get_cut_big_string 200] \
142 "an_array" "\\{2, 3, 5\\}" \
143 "an_array_with_repetition" "\\{1, 3 <repeats 12 times>, 5, 5, 5\\}" \
144 "a_symbol_pointer" "${default_pointer_regexp} <global_symbol>" \
145 "a_base_ref" "${default_ref_regexp}" \
148 # A sentinel value to pass to function to
get them to use a default value
150 # Note that we cannot use $undefined
for default arguments in function
151 # definitions as we would just
get the literal
"$undefined" string, so
152 # we need to repeat the string.
153 set undefined
"\000UNDEFINED\000"
155 #
Return $value
if it
's not $undefined, otherwise return the default value
156 # (from $default_regexp_dict) for the variable $var.
157 proc get_value_or_default { var value } {
159 if { $value != $undefined } {
163 global default_regexp_dict
164 return [dict get $default_regexp_dict $var]
167 # Check that using gdb.Value.format_string on the value representing the
168 # variable $var produces $expected.
169 proc check_format_string {
172 { expected "\000UNDEFINED\000" }
173 { name "\000UNDEFINED\000" }
177 set expected [get_value_or_default $var $expected]
178 if { $name == $undefined } {
179 set name "${var} with option ${opts}"
183 "python print (gdb.parse_and_eval ('$
{var
}').format_string (${opts}))" \
188 # Check that printing $var with no options set, produces the expected
190 proc check_var_with_no_opts {
192 { expected "\000UNDEFINED\000" }
194 set expected [get_value_or_default $var $expected]
196 with_test_prefix "${var}" {
197 check_format_string \
202 # str () should behave like gdb.Value.format_string () with no args.
204 "python print (str (gdb.parse_and_eval ('$
{var
}')))" \
210 # Check that printing $var with $opt set to True and set to False,
211 # produces the expected output.
212 proc check_var_with_bool_opt {
215 { true_expected "\000UNDEFINED\000" }
216 { false_expected "\000UNDEFINED\000" }
218 set true_expected [get_value_or_default $var $true_expected]
219 set false_expected [get_value_or_default $var $false_expected]
221 with_test_prefix "${var} with option ${opt}" {
222 # Option set to True.
223 check_format_string \
228 # Option set to False.
229 check_format_string \
237 # Test gdb.Value.format_string with no options.
238 proc_with_prefix test_no_opts {} {
241 check_var_with_no_opts "a_point_t"
242 check_var_with_no_opts "a_point_t_pointer"
243 check_var_with_no_opts "another_point"
244 check_var_with_no_opts "a_struct_with_union"
245 check_var_with_no_opts "an_enum"
246 check_var_with_no_opts "a_string"
247 check_var_with_no_opts "a_binary_string"
248 check_var_with_no_opts "a_binary_string_array"
249 check_var_with_no_opts "a_big_string"
250 check_var_with_no_opts "an_array"
251 check_var_with_no_opts "an_array_with_repetition"
252 check_var_with_no_opts "a_symbol_pointer"
254 if { $current_lang == "c++" } {
255 # Nothing changes in all of the C++ tests because deref_refs is not
257 check_var_with_no_opts "a_point_t_ref"
258 check_var_with_no_opts "a_base_ref"
262 # Test the raw option for gdb.Value.format_string.
263 proc_with_prefix test_raw {} {
265 global default_ref_regexp
267 check_var_with_bool_opt "raw" "a_point_t" \
269 check_var_with_bool_opt "raw" "a_point_t_pointer"
270 check_var_with_bool_opt "raw" "another_point" \
272 check_var_with_bool_opt "raw" "a_struct_with_union"
273 check_var_with_bool_opt "raw" "an_enum"
274 check_var_with_bool_opt "raw" "a_string"
275 check_var_with_bool_opt "raw" "a_binary_string"
276 check_var_with_bool_opt "raw" "a_binary_string_array"
277 check_var_with_bool_opt "raw" "a_big_string"
278 check_var_with_bool_opt "raw" "an_array"
279 check_var_with_bool_opt "raw" "an_array_with_repetition"
280 check_var_with_bool_opt "raw" "a_symbol_pointer"
282 if { $current_lang == "c++" } {
283 check_var_with_bool_opt "raw" "a_point_t_ref" \
284 ${default_ref_regexp}
285 check_var_with_bool_opt "raw" "a_base_ref"
289 "disable pretty-printer '' test_lookup_function" \
290 "enable pretty-printer '' test_lookup_function" {
291 check_var_with_no_opts "a_point_t" \
293 check_var_with_bool_opt "raw" "a_point_t" \
299 # Test the pretty_arrays option for gdb.Value.format_string.
300 proc_with_prefix test_pretty_arrays {} {
303 set an_array_pretty "\\{\[\r\n\]+ 2,\[\r\n\]+ 3,\[\r\n\]+ 5\[\r\n\]+\\}"
304 set an_array_with_repetition_pretty \
305 "\\{\[\r\n\]+ 1,\[\r\n\]+ 3 <repeats 12 times>,\[\r\n\]+ 5,\[\r\n\]+ 5,\[\r\n\]+ 5\[\r\n\]+\\}"
307 check_var_with_bool_opt "pretty_arrays" "a_point_t"
308 check_var_with_bool_opt "pretty_arrays" "a_point_t_pointer"
309 check_var_with_bool_opt "pretty_arrays" "another_point"
310 check_var_with_bool_opt "pretty_arrays" "a_struct_with_union"
311 check_var_with_bool_opt "pretty_arrays" "an_enum"
312 check_var_with_bool_opt "pretty_arrays" "a_string"
313 check_var_with_bool_opt "pretty_arrays" "a_binary_string"
314 check_var_with_bool_opt "pretty_arrays" "a_binary_string_array"
315 check_var_with_bool_opt "pretty_arrays" "a_big_string"
316 check_var_with_bool_opt "pretty_arrays" "an_array" \
318 check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
319 $an_array_with_repetition_pretty
320 check_var_with_bool_opt "pretty_arrays" "a_symbol_pointer"
322 if { $current_lang == "c++" } {
323 check_var_with_bool_opt "pretty_arrays" "a_point_t_ref"
324 check_var_with_bool_opt "pretty_arrays" "a_base_ref"
327 with_temp_option "set print array on" "set print array off" {
328 check_var_with_no_opts "an_array" \
330 check_var_with_bool_opt "pretty_arrays" "an_array" \
333 check_var_with_no_opts "an_array_with_repetition" \
334 $an_array_with_repetition_pretty
335 check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
336 $an_array_with_repetition_pretty
340 # Test the pretty_structs option for gdb.Value.format_string.
341 proc_with_prefix test_pretty_structs {} {
344 set a_struct_with_union_pretty \
345 "\\{\[\r\n\]+ the_union = \\{\[\r\n\]+ an_int = 707406378,\[\r\n\]+ a_char = 42 '\\*'\[\r\n\]+ \\}\[\r\n\]+\\}"
347 check_var_with_bool_opt "pretty_structs" "a_point_t"
348 check_var_with_bool_opt "pretty_structs" "a_point_t_pointer"
349 check_var_with_bool_opt "pretty_structs" "another_point"
350 check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
351 $a_struct_with_union_pretty
352 check_var_with_bool_opt "pretty_structs" "an_enum"
353 check_var_with_bool_opt "pretty_structs" "a_string"
354 check_var_with_bool_opt "pretty_structs" "a_binary_string"
355 check_var_with_bool_opt "pretty_structs" "a_binary_string_array"
356 check_var_with_bool_opt "pretty_structs" "a_big_string"
357 check_var_with_bool_opt "pretty_structs" "an_array"
358 check_var_with_bool_opt "pretty_structs" "an_array_with_repetition"
359 check_var_with_bool_opt "pretty_structs" "a_symbol_pointer"
361 if { $current_lang == "c++" } {
362 check_var_with_bool_opt "pretty_structs" "a_point_t_ref"
363 check_var_with_bool_opt "pretty_structs" "a_base_ref"
366 with_temp_option "set print structs on" "set print structs off" {
367 check_var_with_no_opts "a_struct_with_union"
368 check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
369 $a_struct_with_union_pretty
372 # point_t is usually printed through the pretty printer.
375 "disable pretty-printer '' test_lookup_function" \
376 "enable pretty-printer '' test_lookup_function" {
377 check_var_with_no_opts "a_point_t" \
379 check_var_with_bool_opt "pretty_structs" "a_point_t" \
380 "\\{\[\r\n\]+ x = 42, *\[\r\n\]+ y = 12\[\r\n\]+\\}" \
385 # Test the array_indexes option for gdb.Value.format_string.
386 proc_with_prefix test_array_indexes {} {
389 set an_array_with_indexes "\\{\\\[0\\\] = 2, \\\[1\\\] = 3, \\\[2\\\] = 5\\}"
390 set an_array_with_repetition_with_indexes \
391 "\\{\\\[0\\\] = 1, \\\[1\\\] = 3 <repeats 12 times>, \\\[13\\\] = 5, \\\[14\\\] = 5, \\\[15\\\] = 5\\}"
393 check_var_with_bool_opt "array_indexes" "a_point_t"
394 check_var_with_bool_opt "array_indexes" "a_point_t_pointer"
395 check_var_with_bool_opt "array_indexes" "another_point"
396 check_var_with_bool_opt "array_indexes" "a_struct_with_union"
397 check_var_with_bool_opt "array_indexes" "an_enum"
398 check_var_with_bool_opt "array_indexes" "a_string"
399 check_var_with_bool_opt "array_indexes" "a_binary_string"
400 check_var_with_bool_opt "array_indexes" "a_binary_string_array"
401 check_var_with_bool_opt "array_indexes" "a_big_string"
402 check_var_with_bool_opt "array_indexes" "an_array" \
403 $an_array_with_indexes
404 check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
405 $an_array_with_repetition_with_indexes
406 check_var_with_bool_opt "array_indexes" "a_symbol_pointer"
408 if { $current_lang == "c++" } {
409 check_var_with_bool_opt "array_indexes" "a_point_t_ref"
410 check_var_with_bool_opt "array_indexes" "a_base_ref"
414 "set print array-indexes on" \
415 "set print array-indexes off" {
416 check_var_with_no_opts "an_array" \
417 $an_array_with_indexes
418 check_var_with_bool_opt "array_indexes" "an_array" \
419 $an_array_with_indexes
421 check_var_with_no_opts "an_array_with_repetition" \
422 $an_array_with_repetition_with_indexes
423 check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
424 $an_array_with_repetition_with_indexes
428 # Test the symbols option for gdb.Value.format_string.
429 proc_with_prefix test_symbols {} {
432 global default_pointer_regexp
434 check_var_with_bool_opt "symbols" "a_point_t"
435 check_var_with_bool_opt "symbols" "a_point_t_pointer"
436 check_var_with_bool_opt "symbols" "another_point"
437 check_var_with_bool_opt "symbols" "a_struct_with_union"
438 check_var_with_bool_opt "symbols" "an_enum"
439 check_var_with_bool_opt "symbols" "a_string"
440 check_var_with_bool_opt "symbols" "a_binary_string"
441 check_var_with_bool_opt "symbols" "a_binary_string_array"
442 check_var_with_bool_opt "symbols" "a_big_string"
443 check_var_with_bool_opt "symbols" "an_array"
444 check_var_with_bool_opt "symbols" "an_array_with_repetition"
445 check_var_with_bool_opt "symbols" "a_symbol_pointer" \
447 $default_pointer_regexp
449 if { $current_lang == "c++" } {
450 check_var_with_bool_opt "symbols" "a_point_t_ref"
451 check_var_with_bool_opt "symbols" "a_base_ref"
454 with_temp_option "set print symbol off" "set print symbol on" {
455 check_var_with_no_opts "a_symbol_pointer" \
456 $default_pointer_regexp
457 check_var_with_bool_opt "symbols" "a_symbol_pointer" \
459 $default_pointer_regexp
463 # Test the unions option for gdb.Value.format_string.
464 proc_with_prefix test_unions {} {
468 check_var_with_bool_opt "unions" "a_point_t"
469 check_var_with_bool_opt "unions" "a_point_t_pointer"
470 check_var_with_bool_opt "unions" "another_point"
471 check_var_with_bool_opt "unions" "a_struct_with_union" \
473 "\\{the_union = \\{...\\}\\}"
474 check_var_with_bool_opt "unions" "an_enum"
475 check_var_with_bool_opt "unions" "a_string"
476 check_var_with_bool_opt "unions" "a_binary_string"
477 check_var_with_bool_opt "unions" "a_binary_string_array"
478 check_var_with_bool_opt "unions" "a_big_string"
479 check_var_with_bool_opt "unions" "an_array"
480 check_var_with_bool_opt "unions" "an_array_with_repetition"
481 check_var_with_bool_opt "unions" "a_symbol_pointer"
483 if { $current_lang == "c++" } {
484 check_var_with_bool_opt "unions" "a_point_t_ref"
485 check_var_with_bool_opt "unions" "a_base_ref"
488 with_temp_option "set print union off" "set print union on" {
489 check_var_with_no_opts "a_struct_with_union" \
490 "\\{the_union = \\{...\\}\\}"
491 check_var_with_bool_opt "unions" "a_struct_with_union" \
493 "\\{the_union = \\{...\\}\\}"
497 # Test the address option for gdb.Value.format_string.
498 proc_with_prefix test_address {} {
502 check_var_with_bool_opt "address" "a_point_t"
503 check_var_with_bool_opt "address" "a_point_t_pointer" \
506 check_var_with_bool_opt "address" "another_point"
507 check_var_with_bool_opt "symbols" "a_struct_with_union"
508 check_var_with_bool_opt "address" "an_enum"
509 check_var_with_bool_opt "address" "a_string" \
512 check_var_with_bool_opt "address" "a_binary_string" \
515 check_var_with_bool_opt "address" "a_binary_string_array"
516 check_var_with_bool_opt "address" "a_big_string"
517 check_var_with_bool_opt "address" "an_array"
518 check_var_with_bool_opt "address" "an_array_with_repetition"
519 check_var_with_bool_opt "address" "a_symbol_pointer" \
523 if { $current_lang == "c++" } {
524 check_var_with_bool_opt "address" "a_point_t_ref"
525 check_var_with_bool_opt "address" "a_base_ref" \
530 with_temp_option "set print address off" "set print address on" {
531 check_var_with_no_opts "a_string" \
533 check_var_with_bool_opt "address" "a_string" \
539 # Test the deref_refs option for gdb.Value.format_string.
540 proc_with_prefix test_deref_refs {} {
542 global default_pointer_regexp
543 global default_ref_regexp
546 check_var_with_bool_opt "deref_refs" "a_point_t"
547 check_var_with_bool_opt "deref_refs" "a_point_t_pointer"
548 check_var_with_bool_opt "deref_refs" "another_point"
549 check_var_with_bool_opt "deref_refs" "a_struct_with_union"
550 check_var_with_bool_opt "deref_refs" "an_enum"
551 check_var_with_bool_opt "deref_refs" "a_string"
552 check_var_with_bool_opt "deref_refs" "a_binary_string"
553 check_var_with_bool_opt "deref_refs" "a_binary_string_array"
554 check_var_with_bool_opt "deref_refs" "a_big_string"
555 check_var_with_bool_opt "deref_refs" "an_array"
556 check_var_with_bool_opt "deref_refs" "an_array_with_repetition"
557 check_var_with_bool_opt "deref_refs" "a_symbol_pointer"
559 if { $current_lang == "c++" } {
560 check_var_with_bool_opt "deref_refs" "a_point_t_ref"
561 check_var_with_bool_opt "deref_refs" "a_base_ref" \
562 "${default_ref_regexp}: \\{_vptr\[.\$\]Base = ${default_pointer_regexp} <vtable for Deriv\\+$decimal>, a = 42, static a_static_member = 2019\\}"
566 # Test the actual_objects option for gdb.Value.format_string.
567 proc_with_prefix test_actual_objects {} {
570 check_var_with_bool_opt "actual_objects" "a_point_t"
571 check_var_with_bool_opt "actual_objects" "a_point_t_pointer"
572 check_var_with_bool_opt "actual_objects" "another_point"
573 check_var_with_bool_opt "actual_objects" "a_struct_with_union"
574 check_var_with_bool_opt "actual_objects" "an_enum"
575 check_var_with_bool_opt "actual_objects" "a_string"
576 check_var_with_bool_opt "actual_objects" "a_binary_string"
577 check_var_with_bool_opt "actual_objects" "a_binary_string_array"
578 check_var_with_bool_opt "actual_objects" "a_big_string"
579 check_var_with_bool_opt "actual_objects" "an_array"
580 check_var_with_bool_opt "actual_objects" "an_array_with_repetition"
581 check_var_with_bool_opt "actual_objects" "a_symbol_pointer"
583 if { $current_lang == "c++" } {
584 # Nothing changes in all of the C++ tests because deref_refs is not
586 check_var_with_bool_opt "actual_objects" "a_point_t_ref"
587 check_var_with_bool_opt "actual_objects" "a_base_ref"
589 with_temp_option "set print object on" "set print object off" {
590 check_var_with_no_opts "a_point_t_ref"
591 check_var_with_bool_opt "actual_objects" "a_point_t_ref"
593 check_var_with_no_opts "a_base_ref"
594 check_var_with_bool_opt "actual_objects" "a_base_ref"
599 # Test the static_members option for gdb.Value.format_string.
600 proc_with_prefix test_static_members {} {
603 check_var_with_bool_opt "static_members" "a_point_t"
604 check_var_with_bool_opt "static_members" "a_point_t_pointer"
605 check_var_with_bool_opt "static_members" "another_point"
606 check_var_with_bool_opt "static_members" "a_struct_with_union"
607 check_var_with_bool_opt "static_members" "an_enum"
608 check_var_with_bool_opt "static_members" "a_string"
609 check_var_with_bool_opt "static_members" "a_binary_string"
610 check_var_with_bool_opt "static_members" "a_binary_string_array"
611 check_var_with_bool_opt "static_members" "a_big_string"
612 check_var_with_bool_opt "static_members" "an_array"
613 check_var_with_bool_opt "static_members" "an_array_with_repetition"
614 check_var_with_bool_opt "static_members" "a_symbol_pointer"
616 if { $current_lang == "c++" } {
617 # Nothing changes in all of the C++ tests because deref_refs is not
619 check_var_with_bool_opt "static_members" "a_point_t_ref"
620 check_var_with_bool_opt "static_members" "a_base_ref"
623 "set print static-members off" \
624 "set print static-members on" {
625 check_var_with_no_opts "a_point_t_ref"
626 check_var_with_bool_opt "static_members" "a_point_t_ref"
628 check_var_with_no_opts "a_base_ref"
629 check_var_with_bool_opt "static_members" "a_base_ref"
634 # Test the max_elements option for gdb.Value.format_string.
635 proc_with_prefix test_max_elements {} {
637 global default_pointer_regexp
639 # 200 is the default maximum number of elements, so setting it should
640 # not change the output.
641 set opts "max_elements=200"
642 with_test_prefix $opts {
643 check_format_string "a_point_t" $opts
644 check_format_string "a_point_t_pointer" $opts
645 check_format_string "another_point" $opts
646 check_format_string "a_struct_with_union" $opts
647 check_format_string "an_enum" $opts
648 check_format_string "a_string" $opts
649 check_format_string "a_binary_string" $opts
650 check_format_string "a_binary_string_array" $opts
651 check_format_string "a_big_string" $opts
652 check_format_string "an_array" $opts
653 check_format_string "an_array_with_repetition" $opts
654 check_format_string "a_symbol_pointer" $opts
656 if { $current_lang == "c++" } {
657 check_format_string "a_point_t_ref" $opts
658 check_format_string "a_base_ref" $opts
662 set opts "max_elements=3"
663 with_test_prefix $opts {
664 check_format_string "a_point_t" $opts
665 check_format_string "a_point_t_pointer" $opts
666 check_format_string "another_point" $opts
667 check_format_string "a_struct_with_union" $opts
668 check_format_string "an_enum" $opts
669 check_format_string "a_string" $opts \
670 "${default_pointer_regexp} \"hel\"..."
671 check_format_string "a_binary_string" $opts \
672 "${default_pointer_regexp} \"hel\"..."
673 # This will print four characters instead of three, see
674 # <https://sourceware.org/bugzilla/show_bug.cgi?id=24331>.
675 check_format_string "a_binary_string_array" $opts \
677 check_format_string "a_big_string" $opts \
678 [get_cut_big_string 3]
679 check_format_string "an_array" $opts
680 check_format_string "an_array_with_repetition" $opts \
681 "\\{1, 3 <repeats 12 times>...\\}"
682 check_format_string "a_symbol_pointer" $opts
684 if { $current_lang == "c++" } {
685 check_format_string "a_point_t_ref" $opts
686 check_format_string "a_base_ref" $opts
690 # Both 1,000 (we don't have that many elements
) and
0 (unlimited
) should
691 # mean no truncation.
692 foreach opts
{ "max_elements=1000" "max_elements=0" } {
693 with_test_prefix $opts
{
694 check_format_string
"a_point_t" $opts
695 check_format_string
"a_point_t_pointer" $opts
696 check_format_string
"another_point" $opts
697 check_format_string
"a_struct_with_union" $opts
698 check_format_string
"an_enum" $opts
699 check_format_string
"a_string" $opts
700 check_format_string
"a_binary_string" $opts
701 check_format_string
"a_binary_string_array" $opts
702 check_format_string
"a_big_string" $opts \
703 [get_cut_big_string
1000]
704 check_format_string
"an_array" $opts
705 check_format_string
"an_array_with_repetition" $opts
706 check_format_string
"a_symbol_pointer" $opts
708 if { $current_lang
== "c++" } {
709 check_format_string
"a_point_t_ref" $opts
710 check_format_string
"a_base_ref" $opts
715 with_temp_option
"set print elements 4" "set print elements 200" {
716 check_format_string
"a_string" "" \
717 "${default_pointer_regexp} \"hell\"..."
718 check_format_string
"a_binary_string" "" \
719 "${default_pointer_regexp} \"hell\"..."
720 check_format_string
"a_binary_string_array" "" \
722 check_format_string
"an_array_with_repetition" "" \
723 "\\{1, 3 <repeats 12 times>...\\}"
727 # Test the max_depth option
for gdb.Value.format_string.
728 proc_with_prefix test_max_depth
{} {
729 set opts
"max_depth=-1"
730 with_test_prefix $opts
{
731 check_format_string
"a_struct_with_union" $opts
732 check_format_string
"a_point_t" $opts "Pretty Point \\(42, 12\\)"
733 check_format_string
"a_struct_with_point" $opts
735 set opts
"max_depth=0"
736 with_test_prefix $opts
{
737 check_format_string
"a_struct_with_union" $opts "\\{\.\.\.\\}"
738 check_format_string
"a_point_t" $opts "Pretty Point \\(42, 12\\)"
739 check_format_string
"a_struct_with_point" $opts "\\{\.\.\.\\}"
741 set opts
"max_depth=1"
742 with_test_prefix $opts
{
743 check_format_string
"a_struct_with_union" $opts "\\{the_union = \\{\.\.\.\\}\\}"
744 check_format_string
"a_point_t" $opts "Pretty Point \\(42, 12\\)"
745 check_format_string
"a_struct_with_point" $opts
747 set opts
"max_depth=2"
748 with_test_prefix $opts
{
749 check_format_string
"a_struct_with_union" $opts
750 check_format_string
"a_point_t" $opts "Pretty Point \\(42, 12\\)"
751 check_format_string
"a_struct_with_point" $opts
755 # Test the repeat_threshold option
for gdb.Value.format_string.
756 proc_with_prefix test_repeat_threshold
{} {
758 global default_pointer_regexp
760 #
10 is the default threshold
for repeated items
, so setting it should
761 # not change the output.
762 set opts
"repeat_threshold=10"
763 with_test_prefix $opts
{
764 check_format_string
"a_point_t" $opts
765 check_format_string
"a_point_t_pointer" $opts
766 check_format_string
"another_point" $opts
767 check_format_string
"a_struct_with_union" $opts
768 check_format_string
"an_enum" $opts
769 check_format_string
"a_string" $opts
770 check_format_string
"a_binary_string" $opts
771 check_format_string
"a_binary_string_array" $opts
772 check_format_string
"a_big_string" $opts
773 check_format_string
"an_array" $opts
774 check_format_string
"an_array_with_repetition" $opts
775 check_format_string
"a_symbol_pointer" $opts
777 if { $current_lang
== "c++" } {
778 check_format_string
"a_point_t_ref" $opts
779 check_format_string
"a_base_ref" $opts
783 set opts
"repeat_threshold=1"
784 with_test_prefix $opts
{
785 check_format_string
"a_point_t" $opts
786 check_format_string
"a_point_t_pointer" $opts
787 check_format_string
"another_point" $opts
788 check_format_string
"a_struct_with_union" $opts
789 check_format_string
"an_enum" $opts
790 check_format_string
"a_string" $opts \
791 "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o world\""
792 check_format_string
"a_binary_string" $opts \
793 "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o\""
794 check_format_string
"a_binary_string_array" $opts \
795 "\"he\", 'l' <repeats 2 times>, \"o\\\\000world\""
796 check_format_string
"a_big_string" $opts
797 check_format_string
"an_array" $opts
798 check_format_string
"an_array_with_repetition" $opts \
799 "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
801 check_format_string
"a_symbol_pointer" $opts
803 if { $current_lang
== "c++" } {
804 check_format_string
"a_point_t_ref" $opts
805 check_format_string
"a_base_ref" $opts
809 set opts
"repeat_threshold=3"
810 with_test_prefix $opts
{
811 check_format_string
"a_point_t" $opts
812 check_format_string
"a_point_t_pointer" $opts
813 check_format_string
"another_point" $opts
814 check_format_string
"a_struct_with_union" $opts
815 check_format_string
"an_enum" $opts
816 check_format_string
"a_string" $opts
817 check_format_string
"a_binary_string" $opts
818 check_format_string
"a_binary_string_array" $opts
819 check_format_string
"a_big_string" $opts
820 check_format_string
"an_array" $opts
821 check_format_string
"an_array_with_repetition" $opts
822 check_format_string
"a_symbol_pointer" $opts
824 if { $current_lang
== "c++" } {
825 check_format_string
"a_point_t_ref" $opts
826 check_format_string
"a_base_ref" $opts
830 # Both
100 (we don
't have that many repeated elements) and 0 (unlimited)
831 # should mean no truncation.
832 foreach opts { "repeat_threshold=100" "repeat_threshold=0" } {
833 with_test_prefix $opts {
834 check_format_string "a_point_t" $opts
835 check_format_string "a_point_t_pointer" $opts
836 check_format_string "another_point" $opts
837 check_format_string "a_struct_with_union" $opts
838 check_format_string "an_enum" $opts
839 check_format_string "a_string" $opts
840 check_format_string "a_binary_string" $opts
841 check_format_string "a_binary_string_array" $opts
842 check_format_string "a_big_string" $opts
843 check_format_string "an_array" $opts
844 check_format_string "an_array_with_repetition" $opts \
845 "\\{1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5\\}"
846 check_format_string "a_symbol_pointer" $opts
848 if { $current_lang == "c++" } {
849 check_format_string "a_point_t_ref" $opts
850 check_format_string "a_base_ref" $opts
855 with_temp_option "set print repeats 1" "set print repeats 10" {
856 check_format_string "an_array_with_repetition" "" \
857 "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
861 # Test the format option for gdb.Value.format_string.
862 proc_with_prefix test_format {} {
864 global default_pointer_regexp
867 set opts "format='x
'"
868 with_test_prefix $opts {
869 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
871 "42 with option ${opts}"
873 check_format_string "a_point_t" $opts
874 check_format_string "a_point_t_pointer" $opts
875 check_format_string "another_point" $opts
876 check_format_string "a_struct_with_union" $opts \
877 "\\{the_union = \\{an_int = 0x2a2a2a2a, a_char = 0x2a\\}\\}"
878 check_format_string "an_enum" $opts \
880 check_format_string "a_string" $opts \
881 $default_pointer_regexp
882 check_format_string "a_binary_string" $opts \
883 $default_pointer_regexp
884 check_format_string "a_binary_string_array" $opts \
885 "\\{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0\\}"
886 check_format_string "a_big_string" $opts \
887 "\\{0x41, 0x42, 0x43, 0x44, 0x45, \[, x0-9a-f\]+\.\.\.\\}"
888 check_format_string "an_array" $opts \
889 "\\{0x2, 0x3, 0x5\\}"
890 check_format_string "an_array_with_repetition" $opts \
891 "\\{0x1, 0x3 <repeats 12 times>, 0x5, 0x5, 0x5\\}"
892 check_format_string "a_symbol_pointer" $opts \
893 $default_pointer_regexp
895 if { $current_lang == "c++" } {
896 check_format_string "a_point_t_ref" $opts
897 check_format_string "a_base_ref" $opts
902 set opts "format='t
'"
903 with_test_prefix $opts {
904 set binary_pointer_regexp "\[0-1\]+"
905 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
907 "42 with option ${opts}"
909 check_format_string "a_point_t" $opts
910 check_format_string "a_point_t_pointer" $opts \
911 $binary_pointer_regexp
912 check_format_string "another_point" $opts
913 check_format_string "a_struct_with_union" $opts \
914 "\\{the_union = \\{an_int = 101010001010100010101000101010, a_char = 101010\\}\\}"
915 check_format_string "an_enum" $opts \
917 check_format_string "a_string" $opts \
918 $binary_pointer_regexp
919 check_format_string "a_binary_string" $opts \
920 $binary_pointer_regexp
921 check_format_string "a_binary_string_array" $opts \
922 "\\{1101000, 1100101, 1101100, 1101100, 1101111, 0, 1110111, 1101111, 1110010, 1101100, 1100100, 0\\}"
923 check_format_string "a_big_string" $opts \
924 "\\{1000001, 1000010, 1000011, 1000100, 1000101, \[, 0-1\]+\.\.\.\\}"
925 check_format_string "an_array" $opts \
927 check_format_string "an_array_with_repetition" $opts \
928 "\\{1, 11 <repeats 12 times>, 101, 101, 101\\}"
929 check_format_string "a_symbol_pointer" $opts \
930 $binary_pointer_regexp
932 if { $current_lang == "c++" } {
933 check_format_string "a_point_t_ref" $opts
934 check_format_string "a_base_ref" $opts
939 set opts "format='d
'"
940 with_test_prefix $opts {
941 set decimal_pointer_regexp "\[0-9\]+"
942 gdb_test "python print (gdb.Value (0x2a).format_string (${opts}))" \
944 "0x2a with option ${opts}"
946 check_format_string "a_point_t" $opts
947 check_format_string "a_point_t_pointer" $opts \
948 $decimal_pointer_regexp
949 check_format_string "another_point" $opts
950 check_format_string "a_struct_with_union" $opts \
951 "\\{the_union = \\{an_int = 707406378, a_char = 42\\}\\}"
952 check_format_string "an_enum" $opts \
954 check_format_string "a_string" $opts \
955 $decimal_pointer_regexp
956 check_format_string "a_binary_string" $opts \
957 $decimal_pointer_regexp
958 check_format_string "a_binary_string_array" $opts \
959 "\\{104, 101, 108, 108, 111, 0, 119, 111, 114, 108, 100, 0\\}"
960 check_format_string "a_big_string" $opts \
961 "\\{65, 66, 67, 68, 69, \[, 0-9\]+\.\.\.\\}"
962 check_format_string "an_array" $opts
963 check_format_string "an_array_with_repetition" $opts
964 check_format_string "a_symbol_pointer" $opts \
965 $decimal_pointer_regexp
967 if { $current_lang == "c++" } {
968 check_format_string "a_point_t_ref" $opts
969 check_format_string "a_base_ref" $opts
974 # Test mixing options.
975 proc_with_prefix test_mixed {} {
977 global default_ref_regexp
978 global default_pointer_regexp
981 check_format_string "a_point_t" \
982 "raw=True, format='x
'" \
983 "\\{x = 0x2a, y = 0xc\\}"
985 check_format_string "an_array" \
986 "array_indexes=True, pretty_arrays=True" \
987 "\\{\[\r\n\]+ \\\[0\\\] = 2,\[\r\n\]+ \\\[1\\\] = 3,\[\r\n\]+ \\\[2\\\] = 5\[\r\n\]+\\}"
989 check_format_string "a_struct_with_union" \
990 "pretty_structs=True, unions=False" \
991 "\\{\[\r\n\]+ the_union = \\{\.\.\.\\}\[\r\n\]+\\}"
993 check_format_string "a_symbol_pointer" \
994 "symbols=False, format='d
'" \
997 if { $current_lang == "c++" } {
998 check_format_string "a_point_t_ref" \
999 "deref_refs=True, actual_objects=True, raw=True" \
1000 "${default_ref_regexp}: \\{x = 42, y = 12\\}"
1002 check_format_string "a_base_ref" \
1003 "deref_refs=True, static_members=False" \
1004 "${default_ref_regexp}: \\{_vptr\[.\$\]Base = ${default_pointer_regexp} <vtable for Deriv\\+$decimal>, a = 42\\}"
1008 # Test passing invalid arguments to gdb.Value.format_string.
1009 proc_with_prefix test_invalid_args {} {
1010 check_format_string \
1013 "TypeError: format_string\\(\\) takes 0 positional arguments but 1 were given.*"
1015 check_format_string \
1018 "TypeError: 'invalid
' is an invalid keyword argument for this function.*"
1020 check_format_string \
1023 "TypeError: argument 1 must be bool, not str.*"
1025 check_format_string \
1028 "ValueError: a single character is required.*"
1031 # Check the styling argument to format_string. This function needs to
1032 # be called with TERM set such that styling can be applied.
1033 proc test_styling {} {
1034 gdb_test "python print(gdb.parse_and_eval(\"a_point_t\").format_string(styling=True, raw=True))" \
1035 "{[style x variable] = 42, [style y variable] = 12}"
1038 # Run all the tests in common for both C and C++.
1039 proc_with_prefix test_all_common {} {
1042 # Single options set to True/False.
1055 test_repeat_threshold
1057 # Multiple options mixed together.
1059 # Various error conditions.
1063 # The current language ("c" or "c++" while running tests).
1066 with_test_prefix "format_string" {
1068 if { [build_inferior "${binfile}" "c"] == 0 } {
1069 with_test_prefix "lang_c" {
1070 save_vars { env(TERM) } {
1071 # We run all of these tests in an environment where styling
1072 # could work, but we only expect the final call to
1073 # test_styling to actually produce any styled output.
1075 set current_lang "c"
1076 prepare_gdb "${binfile}"
1083 # Perform C++ Tests.
1084 if { [build_inferior "${binfile}-cxx" "c++"] == 0 } {
1085 with_test_prefix "lang_cpp" {
1086 set current_lang "c++"
1087 prepare_gdb "${binfile}-cxx"