1 # Copyright (C) 2009-2023 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
21 require allow_python_tests
28 # Build inferior to language specification.
29 proc build_inferior {exefile lang} {
30 global srcdir subdir srcfile testfile hex
32 set flags [list debug $lang]
34 lappend flags additional_flags=-std=c99
37 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" \
38 executable $flags] != "" } {
39 untested "failed to compile in $lang mode"
47 proc prepare_gdb {exefile} {
48 global srcdir subdir srcfile testfile hex
50 clean_restart $exefile
56 # Load the pretty printer.
57 set remote_python_file \
58 [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
59 gdb_test_no_output "source ${remote_python_file}" "load python file"
64 # Set breakpoint and run to that breakpoint.
66 gdb_breakpoint [gdb_get_line_number $bp]
67 gdb_continue_to_breakpoint $bp
70 # Set an option using the GDB command in $set_cmd, execute $body, and then
71 # restore the option using the GDB command in $unset_cmd.
72 proc with_temp_option { set_cmd unset_cmd body } {
73 with_test_prefix $set_cmd {
74 gdb_test "$set_cmd" ".*"
76 gdb_test "$unset_cmd" ".*"
80 # A regular expression for a pointer.
81 set default_pointer_regexp "0x\[a-fA-F0-9\]+"
83 # A regular expression for a non-expanded C++ reference.
85 # Stringifying a C++ reference produces an address preceeded by a "@" in
86 # Python, but, by default, the C++ reference/class is expanded by the
88 set default_ref_regexp "@${default_pointer_regexp}"
90 # The whole content of the C variable a_big_string, i.e. the whole English
91 # alphabet repeated 10 times.
92 set whole_big_string ""
93 set alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
94 for {set i 0} {$i < 10} {incr i} {
95 append whole_big_string $alphabet
99 # Produces a potentially cut down version of $whole_big_string like GDB
100 # would represent it.
101 # $max is the maximum number of characters allowed in the string (but
102 # the return value may contain more to accound for the extra quotes and
103 # "..." added by GDB).
104 proc get_cut_big_string { max } {
105 global whole_big_string
107 set whole_size [string length $whole_big_string]
108 if { $max > $whole_size } {
109 return "\"${whole_big_string}\""
112 set cut_string [string range $whole_big_string 0 [expr $max - 1]]
113 return "\"${cut_string}\"..."
116 # A dictionary mapping from C variable names to their default string
117 # representation when using str () or gdb.Value.format_string () with
119 # This usually matches what the print command prints if used with no
120 # options, except for C++ references which are not expanded by
121 # default in Python. See the comment above $default_ref_regexp.
122 set default_regexp_dict [dict create \
123 "a_point_t" "Pretty Point \\(42, 12\\)" \
124 "a_point_t_pointer" $default_pointer_regexp \
125 "a_point_t_ref" "Pretty Point \\(42, 12\\)" \
126 "another_point" "Pretty Point \\(123, 456\\)" \
127 "a_struct_with_point" "\\{the_point = Pretty Point \\(42, 12\\)\\}" \
128 "a_struct_with_union" "\\{the_union = \\{an_int = 707406378, a_char = 42 '\\*'\\}\\}" \
129 "an_enum" "ENUM_BAR" \
130 "a_string" "${default_pointer_regexp} \"hello world\"" \
131 "a_binary_string" "${default_pointer_regexp} \"hello\"" \
132 "a_binary_string_array" "\"hello\\\\000world\"" \
133 "a_big_string" [get_cut_big_string 200] \
134 "an_array" "\\{2, 3, 5\\}" \
135 "an_array_with_repetition" "\\{1, 3 <repeats 12 times>, 5, 5, 5\\}" \
136 "a_symbol_pointer" "${default_pointer_regexp} <global_symbol>" \
137 "a_base_ref" "${default_ref_regexp}" \
140 # A sentinel value to pass to function to get them to use a default value
142 # Note that we cannot use $undefined for default arguments in function
143 # definitions as we would just get the literal "$undefined" string, so
144 # we need to repeat the string.
145 set undefined "\000UNDEFINED\000"
147 # Return $value if it's not $undefined, otherwise return the default value
148 # (from $default_regexp_dict) for the variable $var.
149 proc get_value_or_default { var value } {
151 if { $value != $undefined } {
155 global default_regexp_dict
156 return [dict get $default_regexp_dict $var]
159 # Check that using gdb.Value.format_string on the value representing the
160 # variable $var produces $expected.
161 proc check_format_string {
164 { expected "\000UNDEFINED\000" }
165 { name "\000UNDEFINED\000" }
169 set expected [get_value_or_default $var $expected]
170 if { $name == $undefined } {
171 set name "${var} with option ${opts}"
175 "python print (gdb.parse_and_eval ('${var}').format_string (${opts}))" \
180 # Check that printing $var with no options set, produces the expected
182 proc check_var_with_no_opts {
184 { expected "\000UNDEFINED\000" }
186 set expected [get_value_or_default $var $expected]
188 with_test_prefix "${var}" {
189 check_format_string \
194 # str () should behave like gdb.Value.format_string () with no args.
196 "python print (str (gdb.parse_and_eval ('${var}')))" \
202 # Check that printing $var with $opt set to True and set to False,
203 # produces the expected output.
204 proc check_var_with_bool_opt {
207 { true_expected "\000UNDEFINED\000" }
208 { false_expected "\000UNDEFINED\000" }
210 set true_expected [get_value_or_default $var $true_expected]
211 set false_expected [get_value_or_default $var $false_expected]
213 with_test_prefix "${var} with option ${opt}" {
214 # Option set to True.
215 check_format_string \
220 # Option set to False.
221 check_format_string \
229 # Test gdb.Value.format_string with no options.
230 proc_with_prefix test_no_opts {} {
233 check_var_with_no_opts "a_point_t"
234 check_var_with_no_opts "a_point_t_pointer"
235 check_var_with_no_opts "another_point"
236 check_var_with_no_opts "a_struct_with_union"
237 check_var_with_no_opts "an_enum"
238 check_var_with_no_opts "a_string"
239 check_var_with_no_opts "a_binary_string"
240 check_var_with_no_opts "a_binary_string_array"
241 check_var_with_no_opts "a_big_string"
242 check_var_with_no_opts "an_array"
243 check_var_with_no_opts "an_array_with_repetition"
244 check_var_with_no_opts "a_symbol_pointer"
246 if { $current_lang == "c++" } {
247 # Nothing changes in all of the C++ tests because deref_refs is not
249 check_var_with_no_opts "a_point_t_ref"
250 check_var_with_no_opts "a_base_ref"
254 # Test the raw option for gdb.Value.format_string.
255 proc_with_prefix test_raw {} {
257 global default_ref_regexp
259 check_var_with_bool_opt "raw" "a_point_t" \
261 check_var_with_bool_opt "raw" "a_point_t_pointer"
262 check_var_with_bool_opt "raw" "another_point" \
264 check_var_with_bool_opt "raw" "a_struct_with_union"
265 check_var_with_bool_opt "raw" "an_enum"
266 check_var_with_bool_opt "raw" "a_string"
267 check_var_with_bool_opt "raw" "a_binary_string"
268 check_var_with_bool_opt "raw" "a_binary_string_array"
269 check_var_with_bool_opt "raw" "a_big_string"
270 check_var_with_bool_opt "raw" "an_array"
271 check_var_with_bool_opt "raw" "an_array_with_repetition"
272 check_var_with_bool_opt "raw" "a_symbol_pointer"
274 if { $current_lang == "c++" } {
275 check_var_with_bool_opt "raw" "a_point_t_ref" \
276 ${default_ref_regexp}
277 check_var_with_bool_opt "raw" "a_base_ref"
281 "disable pretty-printer '' test_lookup_function" \
282 "enable pretty-printer '' test_lookup_function" {
283 check_var_with_no_opts "a_point_t" \
285 check_var_with_bool_opt "raw" "a_point_t" \
291 # Test the pretty_arrays option for gdb.Value.format_string.
292 proc_with_prefix test_pretty_arrays {} {
295 set an_array_pretty "\\{\[\r\n\]+ 2,\[\r\n\]+ 3,\[\r\n\]+ 5\[\r\n\]+\\}"
296 set an_array_with_repetition_pretty \
297 "\\{\[\r\n\]+ 1,\[\r\n\]+ 3 <repeats 12 times>,\[\r\n\]+ 5,\[\r\n\]+ 5,\[\r\n\]+ 5\[\r\n\]+\\}"
299 check_var_with_bool_opt "pretty_arrays" "a_point_t"
300 check_var_with_bool_opt "pretty_arrays" "a_point_t_pointer"
301 check_var_with_bool_opt "pretty_arrays" "another_point"
302 check_var_with_bool_opt "pretty_arrays" "a_struct_with_union"
303 check_var_with_bool_opt "pretty_arrays" "an_enum"
304 check_var_with_bool_opt "pretty_arrays" "a_string"
305 check_var_with_bool_opt "pretty_arrays" "a_binary_string"
306 check_var_with_bool_opt "pretty_arrays" "a_binary_string_array"
307 check_var_with_bool_opt "pretty_arrays" "a_big_string"
308 check_var_with_bool_opt "pretty_arrays" "an_array" \
310 check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
311 $an_array_with_repetition_pretty
312 check_var_with_bool_opt "pretty_arrays" "a_symbol_pointer"
314 if { $current_lang == "c++" } {
315 check_var_with_bool_opt "pretty_arrays" "a_point_t_ref"
316 check_var_with_bool_opt "pretty_arrays" "a_base_ref"
319 with_temp_option "set print array on" "set print array off" {
320 check_var_with_no_opts "an_array" \
322 check_var_with_bool_opt "pretty_arrays" "an_array" \
325 check_var_with_no_opts "an_array_with_repetition" \
326 $an_array_with_repetition_pretty
327 check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
328 $an_array_with_repetition_pretty
332 # Test the pretty_structs option for gdb.Value.format_string.
333 proc_with_prefix test_pretty_structs {} {
336 set a_struct_with_union_pretty \
337 "\\{\[\r\n\]+ the_union = \\{\[\r\n\]+ an_int = 707406378,\[\r\n\]+ a_char = 42 '\\*'\[\r\n\]+ \\}\[\r\n\]+\\}"
339 check_var_with_bool_opt "pretty_structs" "a_point_t"
340 check_var_with_bool_opt "pretty_structs" "a_point_t_pointer"
341 check_var_with_bool_opt "pretty_structs" "another_point"
342 check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
343 $a_struct_with_union_pretty
344 check_var_with_bool_opt "pretty_structs" "an_enum"
345 check_var_with_bool_opt "pretty_structs" "a_string"
346 check_var_with_bool_opt "pretty_structs" "a_binary_string"
347 check_var_with_bool_opt "pretty_structs" "a_binary_string_array"
348 check_var_with_bool_opt "pretty_structs" "a_big_string"
349 check_var_with_bool_opt "pretty_structs" "an_array"
350 check_var_with_bool_opt "pretty_structs" "an_array_with_repetition"
351 check_var_with_bool_opt "pretty_structs" "a_symbol_pointer"
353 if { $current_lang == "c++" } {
354 check_var_with_bool_opt "pretty_structs" "a_point_t_ref"
355 check_var_with_bool_opt "pretty_structs" "a_base_ref"
358 with_temp_option "set print structs on" "set print structs off" {
359 check_var_with_no_opts "a_struct_with_union"
360 check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
361 $a_struct_with_union_pretty
364 # point_t is usually printed through the pretty printer.
367 "disable pretty-printer '' test_lookup_function" \
368 "enable pretty-printer '' test_lookup_function" {
369 check_var_with_no_opts "a_point_t" \
371 check_var_with_bool_opt "pretty_structs" "a_point_t" \
372 "\\{\[\r\n\]+ x = 42, *\[\r\n\]+ y = 12\[\r\n\]+\\}" \
377 # Test the array_indexes option for gdb.Value.format_string.
378 proc_with_prefix test_array_indexes {} {
381 set an_array_with_indexes "\\{\\\[0\\\] = 2, \\\[1\\\] = 3, \\\[2\\\] = 5\\}"
382 set an_array_with_repetition_with_indexes \
383 "\\{\\\[0\\\] = 1, \\\[1\\\] = 3 <repeats 12 times>, \\\[13\\\] = 5, \\\[14\\\] = 5, \\\[15\\\] = 5\\}"
385 check_var_with_bool_opt "array_indexes" "a_point_t"
386 check_var_with_bool_opt "array_indexes" "a_point_t_pointer"
387 check_var_with_bool_opt "array_indexes" "another_point"
388 check_var_with_bool_opt "array_indexes" "a_struct_with_union"
389 check_var_with_bool_opt "array_indexes" "an_enum"
390 check_var_with_bool_opt "array_indexes" "a_string"
391 check_var_with_bool_opt "array_indexes" "a_binary_string"
392 check_var_with_bool_opt "array_indexes" "a_binary_string_array"
393 check_var_with_bool_opt "array_indexes" "a_big_string"
394 check_var_with_bool_opt "array_indexes" "an_array" \
395 $an_array_with_indexes
396 check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
397 $an_array_with_repetition_with_indexes
398 check_var_with_bool_opt "array_indexes" "a_symbol_pointer"
400 if { $current_lang == "c++" } {
401 check_var_with_bool_opt "array_indexes" "a_point_t_ref"
402 check_var_with_bool_opt "array_indexes" "a_base_ref"
406 "set print array-indexes on" \
407 "set print array-indexes off" {
408 check_var_with_no_opts "an_array" \
409 $an_array_with_indexes
410 check_var_with_bool_opt "array_indexes" "an_array" \
411 $an_array_with_indexes
413 check_var_with_no_opts "an_array_with_repetition" \
414 $an_array_with_repetition_with_indexes
415 check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
416 $an_array_with_repetition_with_indexes
420 # Test the symbols option for gdb.Value.format_string.
421 proc_with_prefix test_symbols {} {
424 global default_pointer_regexp
426 check_var_with_bool_opt "symbols" "a_point_t"
427 check_var_with_bool_opt "symbols" "a_point_t_pointer"
428 check_var_with_bool_opt "symbols" "another_point"
429 check_var_with_bool_opt "symbols" "a_struct_with_union"
430 check_var_with_bool_opt "symbols" "an_enum"
431 check_var_with_bool_opt "symbols" "a_string"
432 check_var_with_bool_opt "symbols" "a_binary_string"
433 check_var_with_bool_opt "symbols" "a_binary_string_array"
434 check_var_with_bool_opt "symbols" "a_big_string"
435 check_var_with_bool_opt "symbols" "an_array"
436 check_var_with_bool_opt "symbols" "an_array_with_repetition"
437 check_var_with_bool_opt "symbols" "a_symbol_pointer" \
439 $default_pointer_regexp
441 if { $current_lang == "c++" } {
442 check_var_with_bool_opt "symbols" "a_point_t_ref"
443 check_var_with_bool_opt "symbols" "a_base_ref"
446 with_temp_option "set print symbol off" "set print symbol on" {
447 check_var_with_no_opts "a_symbol_pointer" \
448 $default_pointer_regexp
449 check_var_with_bool_opt "symbols" "a_symbol_pointer" \
451 $default_pointer_regexp
455 # Test the unions option for gdb.Value.format_string.
456 proc_with_prefix test_unions {} {
460 check_var_with_bool_opt "unions" "a_point_t"
461 check_var_with_bool_opt "unions" "a_point_t_pointer"
462 check_var_with_bool_opt "unions" "another_point"
463 check_var_with_bool_opt "unions" "a_struct_with_union" \
465 "\\{the_union = \\{...\\}\\}"
466 check_var_with_bool_opt "unions" "an_enum"
467 check_var_with_bool_opt "unions" "a_string"
468 check_var_with_bool_opt "unions" "a_binary_string"
469 check_var_with_bool_opt "unions" "a_binary_string_array"
470 check_var_with_bool_opt "unions" "a_big_string"
471 check_var_with_bool_opt "unions" "an_array"
472 check_var_with_bool_opt "unions" "an_array_with_repetition"
473 check_var_with_bool_opt "unions" "a_symbol_pointer"
475 if { $current_lang == "c++" } {
476 check_var_with_bool_opt "unions" "a_point_t_ref"
477 check_var_with_bool_opt "unions" "a_base_ref"
480 with_temp_option "set print union off" "set print union on" {
481 check_var_with_no_opts "a_struct_with_union" \
482 "\\{the_union = \\{...\\}\\}"
483 check_var_with_bool_opt "unions" "a_struct_with_union" \
485 "\\{the_union = \\{...\\}\\}"
489 # Test the address option for gdb.Value.format_string.
490 proc_with_prefix test_address {} {
494 check_var_with_bool_opt "address" "a_point_t"
495 check_var_with_bool_opt "address" "a_point_t_pointer" \
498 check_var_with_bool_opt "address" "another_point"
499 check_var_with_bool_opt "symbols" "a_struct_with_union"
500 check_var_with_bool_opt "address" "an_enum"
501 check_var_with_bool_opt "address" "a_string" \
504 check_var_with_bool_opt "address" "a_binary_string" \
507 check_var_with_bool_opt "address" "a_binary_string_array"
508 check_var_with_bool_opt "address" "a_big_string"
509 check_var_with_bool_opt "address" "an_array"
510 check_var_with_bool_opt "address" "an_array_with_repetition"
511 check_var_with_bool_opt "address" "a_symbol_pointer" \
515 if { $current_lang == "c++" } {
516 check_var_with_bool_opt "address" "a_point_t_ref"
517 check_var_with_bool_opt "address" "a_base_ref" \
522 with_temp_option "set print address off" "set print address on" {
523 check_var_with_no_opts "a_string" \
525 check_var_with_bool_opt "address" "a_string" \
531 # Test the nibbles option for gdb.Value.format_string.
532 proc_with_prefix test_nibbles {} {
535 set opts "format='t', nibbles=True"
536 with_test_prefix $opts {
537 if { $current_lang == "c" } {
538 set binary_pointer_regexp "\[ 0-1\]+"
539 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
541 "42 with option ${opts}"
543 check_format_string "a_point_t" $opts \
544 [string_to_regexp "Pretty Point (0010 1010, 1100)"]
545 check_format_string "a_point_t_pointer" $opts \
546 $binary_pointer_regexp
547 check_format_string "another_point" $opts \
548 [string_to_regexp "Pretty Point (0111 1011, 0001 1100 1000)"]
550 check_format_string "a_struct_with_union" $opts \
551 "\\{the_union = \\{an_int = 0010 1010 0010 1010 0010 1010 0010 1010, a_char = 0010 1010\\}\\}"
552 check_format_string "an_enum" $opts \
554 check_format_string "a_string" $opts \
555 $binary_pointer_regexp
556 check_format_string "a_binary_string" $opts \
557 $binary_pointer_regexp
558 check_format_string "a_binary_string_array" $opts \
559 "\\{0110 1000, 0110 0101, 0110 1100, 0110 1100, 0110 1111, 0, 0111 0111, 0110 1111, 0111 0010, 0110 1100, 0110 0100, 0\\}"
560 check_format_string "a_big_string" $opts \
561 "\\{0100 0001, 0100 0010, 0100 0011, 0100 0100, 0100 0101, \[, 0-1\]+\.\.\.\\}"
562 check_format_string "an_array" $opts \
563 "\\{0010, 0011, 0101\\}"
564 check_format_string "an_array_with_repetition" $opts \
565 "\\{0001, 0011 <repeats 12 times>, 0101, 0101, 0101\\}"
566 check_format_string "a_symbol_pointer" $opts \
567 $binary_pointer_regexp
569 if { $current_lang == "c++" } {
570 set binary_pointer_regexp "\['0-1\]+"
571 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
573 "42 with option ${opts}"
575 check_format_string "a_point_t" $opts \
576 [string_to_regexp "Pretty Point (0010'1010, 1100)"]
577 check_format_string "a_point_t_pointer" $opts \
578 $binary_pointer_regexp
579 check_format_string "another_point" $opts \
580 [string_to_regexp "Pretty Point (0111'1011, 0001'1100'1000)"]
582 check_format_string "a_struct_with_union" $opts \
583 "\\{the_union = \\{an_int = 0010'1010'0010'1010'0010'1010'0010'1010, a_char = 0010'1010\\}\\}"
584 check_format_string "an_enum" $opts \
586 check_format_string "a_string" $opts \
587 $binary_pointer_regexp
588 check_format_string "a_binary_string" $opts \
589 $binary_pointer_regexp
590 check_format_string "a_binary_string_array" $opts \
591 "\\{0110'1000, 0110'0101, 0110'1100, 0110'1100, 0110'1111, 0, 0111'0111, 0110'1111, 0111'0010, 0110'1100, 0110'0100, 0\\}"
592 check_format_string "a_big_string" $opts \
593 "\\{0100'0001, 0100'0010, 0100'0011, 0100'0100, 0100'0101, \[, '0-1\]+\.\.\.\\}"
594 check_format_string "an_array" $opts \
595 "\\{0010, 0011, 0101\\}"
596 check_format_string "an_array_with_repetition" $opts \
597 "\\{0001, 0011 <repeats 12 times>, 0101, 0101, 0101\\}"
598 check_format_string "a_symbol_pointer" $opts \
599 $binary_pointer_regexp
601 check_format_string "a_point_t_ref" $opts \
602 [string_to_regexp "Pretty Point (0010'1010, 1100)"]
603 check_format_string "a_base_ref" $opts
608 # Test the deref_refs option for gdb.Value.format_string.
609 proc_with_prefix test_deref_refs {} {
611 global default_pointer_regexp
612 global default_ref_regexp
615 check_var_with_bool_opt "deref_refs" "a_point_t"
616 check_var_with_bool_opt "deref_refs" "a_point_t_pointer"
617 check_var_with_bool_opt "deref_refs" "another_point"
618 check_var_with_bool_opt "deref_refs" "a_struct_with_union"
619 check_var_with_bool_opt "deref_refs" "an_enum"
620 check_var_with_bool_opt "deref_refs" "a_string"
621 check_var_with_bool_opt "deref_refs" "a_binary_string"
622 check_var_with_bool_opt "deref_refs" "a_binary_string_array"
623 check_var_with_bool_opt "deref_refs" "a_big_string"
624 check_var_with_bool_opt "deref_refs" "an_array"
625 check_var_with_bool_opt "deref_refs" "an_array_with_repetition"
626 check_var_with_bool_opt "deref_refs" "a_symbol_pointer"
628 if { $current_lang == "c++" } {
629 check_var_with_bool_opt "deref_refs" "a_point_t_ref"
630 check_var_with_bool_opt "deref_refs" "a_base_ref" \
631 "${default_ref_regexp}: \\{_vptr\[.\$\]Base = ${default_pointer_regexp} <vtable for Deriv\\+$decimal>, a = 42, static a_static_member = 2019\\}"
635 # Test the actual_objects option for gdb.Value.format_string.
636 proc_with_prefix test_actual_objects {} {
639 check_var_with_bool_opt "actual_objects" "a_point_t"
640 check_var_with_bool_opt "actual_objects" "a_point_t_pointer"
641 check_var_with_bool_opt "actual_objects" "another_point"
642 check_var_with_bool_opt "actual_objects" "a_struct_with_union"
643 check_var_with_bool_opt "actual_objects" "an_enum"
644 check_var_with_bool_opt "actual_objects" "a_string"
645 check_var_with_bool_opt "actual_objects" "a_binary_string"
646 check_var_with_bool_opt "actual_objects" "a_binary_string_array"
647 check_var_with_bool_opt "actual_objects" "a_big_string"
648 check_var_with_bool_opt "actual_objects" "an_array"
649 check_var_with_bool_opt "actual_objects" "an_array_with_repetition"
650 check_var_with_bool_opt "actual_objects" "a_symbol_pointer"
652 if { $current_lang == "c++" } {
653 # Nothing changes in all of the C++ tests because deref_refs is not
655 check_var_with_bool_opt "actual_objects" "a_point_t_ref"
656 check_var_with_bool_opt "actual_objects" "a_base_ref"
658 with_temp_option "set print object on" "set print object off" {
659 check_var_with_no_opts "a_point_t_ref"
660 check_var_with_bool_opt "actual_objects" "a_point_t_ref"
662 check_var_with_no_opts "a_base_ref"
663 check_var_with_bool_opt "actual_objects" "a_base_ref"
668 # Test the static_members option for gdb.Value.format_string.
669 proc_with_prefix test_static_members {} {
672 check_var_with_bool_opt "static_members" "a_point_t"
673 check_var_with_bool_opt "static_members" "a_point_t_pointer"
674 check_var_with_bool_opt "static_members" "another_point"
675 check_var_with_bool_opt "static_members" "a_struct_with_union"
676 check_var_with_bool_opt "static_members" "an_enum"
677 check_var_with_bool_opt "static_members" "a_string"
678 check_var_with_bool_opt "static_members" "a_binary_string"
679 check_var_with_bool_opt "static_members" "a_binary_string_array"
680 check_var_with_bool_opt "static_members" "a_big_string"
681 check_var_with_bool_opt "static_members" "an_array"
682 check_var_with_bool_opt "static_members" "an_array_with_repetition"
683 check_var_with_bool_opt "static_members" "a_symbol_pointer"
685 if { $current_lang == "c++" } {
686 # Nothing changes in all of the C++ tests because deref_refs is not
688 check_var_with_bool_opt "static_members" "a_point_t_ref"
689 check_var_with_bool_opt "static_members" "a_base_ref"
692 "set print static-members off" \
693 "set print static-members on" {
694 check_var_with_no_opts "a_point_t_ref"
695 check_var_with_bool_opt "static_members" "a_point_t_ref"
697 check_var_with_no_opts "a_base_ref"
698 check_var_with_bool_opt "static_members" "a_base_ref"
703 # Test the max_elements option for gdb.Value.format_string.
704 # SETTING is the print setting to verify, either "elements"
705 # or "characters". UNLIMITED is the numeric value to use
706 # for the "unlimited" setting.
708 proc test_max_string_one { setting unlimited } {
710 global default_pointer_regexp
712 # 200 is the default maximum number of elements, so setting it should
713 # not change the output.
714 set opts "max_$setting=200"
715 with_test_prefix $opts {
716 check_format_string "a_point_t" $opts
717 check_format_string "a_point_t_pointer" $opts
718 check_format_string "another_point" $opts
719 check_format_string "a_struct_with_union" $opts
720 check_format_string "an_enum" $opts
721 check_format_string "a_string" $opts
722 check_format_string "a_binary_string" $opts
723 check_format_string "a_binary_string_array" $opts
724 check_format_string "a_big_string" $opts
725 if { $setting == "elements" } {
726 check_format_string "an_array" $opts
727 check_format_string "an_array_with_repetition" $opts
729 check_format_string "a_symbol_pointer" $opts
731 if { $current_lang == "c++" } {
732 check_format_string "a_point_t_ref" $opts
733 check_format_string "a_base_ref" $opts
737 set opts "max_$setting=3"
738 with_test_prefix $opts {
739 check_format_string "a_point_t" $opts
740 check_format_string "a_point_t_pointer" $opts
741 check_format_string "another_point" $opts
742 check_format_string "a_struct_with_union" $opts
743 check_format_string "an_enum" $opts
744 check_format_string "a_string" $opts \
745 "${default_pointer_regexp} \"hel\"..."
746 check_format_string "a_binary_string" $opts \
747 "${default_pointer_regexp} \"hel\"..."
748 # This will print four characters instead of three, see
749 # <https://sourceware.org/bugzilla/show_bug.cgi?id=24331>.
750 check_format_string "a_binary_string_array" $opts \
752 check_format_string "a_big_string" $opts \
753 [get_cut_big_string 3]
754 if { $setting == "elements"} {
755 check_format_string "an_array" $opts
756 check_format_string "an_array_with_repetition" $opts \
757 "\\{1, 3 <repeats 12 times>...\\}"
759 check_format_string "a_symbol_pointer" $opts
761 if { $current_lang == "c++" } {
762 check_format_string "a_point_t_ref" $opts
763 check_format_string "a_base_ref" $opts
767 # Both 1,000 (we don't have that many elements) and unlimited should
768 # mean no truncation.
769 foreach opts [list "max_$setting=1000" "max_$setting=$unlimited"] {
770 with_test_prefix $opts {
771 check_format_string "a_point_t" $opts
772 check_format_string "a_point_t_pointer" $opts
773 check_format_string "another_point" $opts
774 check_format_string "a_struct_with_union" $opts
775 check_format_string "an_enum" $opts
776 check_format_string "a_string" $opts
777 check_format_string "a_binary_string" $opts
778 check_format_string "a_binary_string_array" $opts
779 check_format_string "a_big_string" $opts \
780 [get_cut_big_string 1000]
781 if { $setting == "elements"} {
782 check_format_string "an_array" $opts
783 check_format_string "an_array_with_repetition" $opts
785 check_format_string "a_symbol_pointer" $opts
787 if { $current_lang == "c++" } {
788 check_format_string "a_point_t_ref" $opts
789 check_format_string "a_base_ref" $opts
794 with_temp_option "set print $setting 4" "set print $setting 200" {
795 check_format_string "a_string" "" \
796 "${default_pointer_regexp} \"hell\"..."
797 check_format_string "a_binary_string" "" \
798 "${default_pointer_regexp} \"hell\"..."
799 check_format_string "a_binary_string_array" "" \
801 if { $setting == "elements"} {
802 check_format_string "an_array_with_repetition" "" \
803 "\\{1, 3 <repeats 12 times>...\\}"
808 proc_with_prefix test_max_string {} {
809 foreach_with_prefix setting { "elements" "characters" } {
810 test_max_string_one $setting \
811 [string map {elements 0 characters 4294967295} $setting]
815 # Test the max_depth option for gdb.Value.format_string.
816 proc_with_prefix test_max_depth {} {
817 set opts "max_depth=-1"
818 with_test_prefix $opts {
819 check_format_string "a_struct_with_union" $opts
820 check_format_string "a_point_t" $opts "Pretty Point \\(42, 12\\)"
821 check_format_string "a_struct_with_point" $opts
823 set opts "max_depth=0"
824 with_test_prefix $opts {
825 check_format_string "a_struct_with_union" $opts "\\{\.\.\.\\}"
826 check_format_string "a_point_t" $opts "Pretty Point \\(42, 12\\)"
827 check_format_string "a_struct_with_point" $opts "\\{\.\.\.\\}"
829 set opts "max_depth=1"
830 with_test_prefix $opts {
831 check_format_string "a_struct_with_union" $opts "\\{the_union = \\{\.\.\.\\}\\}"
832 check_format_string "a_point_t" $opts "Pretty Point \\(42, 12\\)"
833 check_format_string "a_struct_with_point" $opts
835 set opts "max_depth=2"
836 with_test_prefix $opts {
837 check_format_string "a_struct_with_union" $opts
838 check_format_string "a_point_t" $opts "Pretty Point \\(42, 12\\)"
839 check_format_string "a_struct_with_point" $opts
843 # Test the repeat_threshold option for gdb.Value.format_string.
844 proc_with_prefix test_repeat_threshold {} {
846 global default_pointer_regexp
848 # 10 is the default threshold for repeated items, so setting it should
849 # not change the output.
850 set opts "repeat_threshold=10"
851 with_test_prefix $opts {
852 check_format_string "a_point_t" $opts
853 check_format_string "a_point_t_pointer" $opts
854 check_format_string "another_point" $opts
855 check_format_string "a_struct_with_union" $opts
856 check_format_string "an_enum" $opts
857 check_format_string "a_string" $opts
858 check_format_string "a_binary_string" $opts
859 check_format_string "a_binary_string_array" $opts
860 check_format_string "a_big_string" $opts
861 check_format_string "an_array" $opts
862 check_format_string "an_array_with_repetition" $opts
863 check_format_string "a_symbol_pointer" $opts
865 if { $current_lang == "c++" } {
866 check_format_string "a_point_t_ref" $opts
867 check_format_string "a_base_ref" $opts
871 set opts "repeat_threshold=1"
872 with_test_prefix $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 check_format_string "an_enum" $opts
878 check_format_string "a_string" $opts \
879 "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o world\""
880 check_format_string "a_binary_string" $opts \
881 "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o\""
882 check_format_string "a_binary_string_array" $opts \
883 "\"he\", 'l' <repeats 2 times>, \"o\\\\000world\""
884 check_format_string "a_big_string" $opts
885 check_format_string "an_array" $opts
886 check_format_string "an_array_with_repetition" $opts \
887 "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
889 check_format_string "a_symbol_pointer" $opts
891 if { $current_lang == "c++" } {
892 check_format_string "a_point_t_ref" $opts
893 check_format_string "a_base_ref" $opts
897 set opts "repeat_threshold=3"
898 with_test_prefix $opts {
899 check_format_string "a_point_t" $opts
900 check_format_string "a_point_t_pointer" $opts
901 check_format_string "another_point" $opts
902 check_format_string "a_struct_with_union" $opts
903 check_format_string "an_enum" $opts
904 check_format_string "a_string" $opts
905 check_format_string "a_binary_string" $opts
906 check_format_string "a_binary_string_array" $opts
907 check_format_string "a_big_string" $opts
908 check_format_string "an_array" $opts
909 check_format_string "an_array_with_repetition" $opts
910 check_format_string "a_symbol_pointer" $opts
912 if { $current_lang == "c++" } {
913 check_format_string "a_point_t_ref" $opts
914 check_format_string "a_base_ref" $opts
918 # Both 100 (we don't have that many repeated elements) and 0 (unlimited)
919 # should mean no truncation.
920 foreach opts { "repeat_threshold=100" "repeat_threshold=0" } {
921 with_test_prefix $opts {
922 check_format_string "a_point_t" $opts
923 check_format_string "a_point_t_pointer" $opts
924 check_format_string "another_point" $opts
925 check_format_string "a_struct_with_union" $opts
926 check_format_string "an_enum" $opts
927 check_format_string "a_string" $opts
928 check_format_string "a_binary_string" $opts
929 check_format_string "a_binary_string_array" $opts
930 check_format_string "a_big_string" $opts
931 check_format_string "an_array" $opts
932 check_format_string "an_array_with_repetition" $opts \
933 "\\{1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5\\}"
934 check_format_string "a_symbol_pointer" $opts
936 if { $current_lang == "c++" } {
937 check_format_string "a_point_t_ref" $opts
938 check_format_string "a_base_ref" $opts
943 with_temp_option "set print repeats 1" "set print repeats 10" {
944 check_format_string "an_array_with_repetition" "" \
945 "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
949 # Test the format option for gdb.Value.format_string.
950 proc_with_prefix test_format {} {
952 global default_pointer_regexp
955 set opts "format='x'"
956 with_test_prefix $opts {
957 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
959 "42 with option ${opts}"
961 check_format_string "a_point_t" $opts \
962 "Pretty Point \\(0x2a, 0xc\\)"
963 check_format_string "a_point_t_pointer" $opts
964 check_format_string "another_point" $opts \
965 "Pretty Point \\(0x7b, 0x1c8\\)"
966 check_format_string "a_struct_with_union" $opts \
967 "\\{the_union = \\{an_int = 0x2a2a2a2a, a_char = 0x2a\\}\\}"
968 check_format_string "an_enum" $opts \
970 check_format_string "a_string" $opts \
971 $default_pointer_regexp
972 check_format_string "a_binary_string" $opts \
973 $default_pointer_regexp
974 check_format_string "a_binary_string_array" $opts \
975 "\\{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0\\}"
976 check_format_string "a_big_string" $opts \
977 "\\{0x41, 0x42, 0x43, 0x44, 0x45, \[, x0-9a-f\]+\.\.\.\\}"
978 check_format_string "an_array" $opts \
979 "\\{0x2, 0x3, 0x5\\}"
980 check_format_string "an_array_with_repetition" $opts \
981 "\\{0x1, 0x3 <repeats 12 times>, 0x5, 0x5, 0x5\\}"
982 check_format_string "a_symbol_pointer" $opts \
983 $default_pointer_regexp
985 if { $current_lang == "c++" } {
986 check_format_string "a_point_t_ref" $opts \
987 "Pretty Point \\(0x2a, 0xc\\)"
988 check_format_string "a_base_ref" $opts
993 set opts "format='t'"
994 with_test_prefix $opts {
995 set binary_pointer_regexp "\[0-1\]+"
996 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
998 "42 with option ${opts}"
1000 check_format_string "a_point_t" $opts \
1001 "Pretty Point \\(101010, 1100\\)"
1002 check_format_string "a_point_t_pointer" $opts \
1003 $binary_pointer_regexp
1004 check_format_string "another_point" $opts \
1005 "Pretty Point \\(1111011, 111001000\\)"
1006 check_format_string "a_struct_with_union" $opts \
1007 "\\{the_union = \\{an_int = 101010001010100010101000101010, a_char = 101010\\}\\}"
1008 check_format_string "an_enum" $opts \
1010 check_format_string "a_string" $opts \
1011 $binary_pointer_regexp
1012 check_format_string "a_binary_string" $opts \
1013 $binary_pointer_regexp
1014 check_format_string "a_binary_string_array" $opts \
1015 "\\{1101000, 1100101, 1101100, 1101100, 1101111, 0, 1110111, 1101111, 1110010, 1101100, 1100100, 0\\}"
1016 check_format_string "a_big_string" $opts \
1017 "\\{1000001, 1000010, 1000011, 1000100, 1000101, \[, 0-1\]+\.\.\.\\}"
1018 check_format_string "an_array" $opts \
1020 check_format_string "an_array_with_repetition" $opts \
1021 "\\{1, 11 <repeats 12 times>, 101, 101, 101\\}"
1022 check_format_string "a_symbol_pointer" $opts \
1023 $binary_pointer_regexp
1025 if { $current_lang == "c++" } {
1026 check_format_string "a_point_t_ref" $opts \
1027 "Pretty Point \\(101010, 1100\\)"
1028 check_format_string "a_base_ref" $opts
1033 set opts "format='d'"
1034 with_test_prefix $opts {
1035 set decimal_pointer_regexp "\[0-9\]+"
1036 gdb_test "python print (gdb.Value (0x2a).format_string (${opts}))" \
1038 "0x2a with option ${opts}"
1040 check_format_string "a_point_t" $opts
1041 check_format_string "a_point_t_pointer" $opts \
1042 $decimal_pointer_regexp
1043 check_format_string "another_point" $opts
1044 check_format_string "a_struct_with_union" $opts \
1045 "\\{the_union = \\{an_int = 707406378, a_char = 42\\}\\}"
1046 check_format_string "an_enum" $opts \
1048 check_format_string "a_string" $opts \
1049 $decimal_pointer_regexp
1050 check_format_string "a_binary_string" $opts \
1051 $decimal_pointer_regexp
1052 check_format_string "a_binary_string_array" $opts \
1053 "\\{104, 101, 108, 108, 111, 0, 119, 111, 114, 108, 100, 0\\}"
1054 check_format_string "a_big_string" $opts \
1055 "\\{65, 66, 67, 68, 69, \[, 0-9\]+\.\.\.\\}"
1056 check_format_string "an_array" $opts
1057 check_format_string "an_array_with_repetition" $opts
1058 check_format_string "a_symbol_pointer" $opts \
1059 $decimal_pointer_regexp
1061 if { $current_lang == "c++" } {
1062 check_format_string "a_point_t_ref" $opts
1063 check_format_string "a_base_ref" $opts
1068 # Test mixing options.
1069 proc_with_prefix test_mixed {} {
1071 global default_ref_regexp
1072 global default_pointer_regexp
1075 check_format_string "a_point_t" \
1076 "raw=True, format='x'" \
1077 "\\{x = 0x2a, y = 0xc\\}"
1079 check_format_string "an_array" \
1080 "array_indexes=True, pretty_arrays=True" \
1081 "\\{\[\r\n\]+ \\\[0\\\] = 2,\[\r\n\]+ \\\[1\\\] = 3,\[\r\n\]+ \\\[2\\\] = 5\[\r\n\]+\\}"
1083 check_format_string "a_struct_with_union" \
1084 "pretty_structs=True, unions=False" \
1085 "\\{\[\r\n\]+ the_union = \\{\.\.\.\\}\[\r\n\]+\\}"
1087 check_format_string "a_symbol_pointer" \
1088 "symbols=False, format='d'" \
1091 if { $current_lang == "c++" } {
1092 check_format_string "a_point_t_ref" \
1093 "deref_refs=True, actual_objects=True, raw=True" \
1094 "${default_ref_regexp}: \\{x = 42, y = 12\\}"
1096 check_format_string "a_base_ref" \
1097 "deref_refs=True, static_members=False" \
1098 "${default_ref_regexp}: \\{_vptr\[.\$\]Base = ${default_pointer_regexp} <vtable for Deriv\\+$decimal>, a = 42\\}"
1102 # Test passing invalid arguments to gdb.Value.format_string.
1103 proc_with_prefix test_invalid_args {} {
1104 check_format_string \
1107 "TypeError: format_string\\(\\) takes 0 positional arguments but 1 were given.*"
1109 check_format_string \
1112 "TypeError: 'invalid' is an invalid keyword argument for this function.*"
1114 check_format_string \
1117 "TypeError: argument 1 must be bool, not str.*"
1119 check_format_string \
1122 "ValueError: a single character is required.*"
1125 # Check the styling argument to format_string. This function needs to
1126 # be called with TERM set such that styling can be applied.
1127 proc test_styling {} {
1128 gdb_test "python print(gdb.parse_and_eval(\"a_point_t\").format_string(styling=True, raw=True))" \
1129 "{[style x variable] = 42, [style y variable] = 12}"
1132 # Test the gdb.print_options API.
1133 proc test_print_options {} {
1134 gdb_test_no_output "set print elements 500"
1135 gdb_test "python print(gdb.print_options()\['max_elements'\])" "500" \
1136 "examine max elements"
1137 gdb_test "python print('format' in gdb.print_options())" "False" \
1140 check_format_string "a_point_t" "format='t', nibbles=True" \
1141 "Pretty Point \\(0010.1010, 1100\\)" \
1142 "print in binary to fetch options"
1143 gdb_test "python print(saved_options\['format'\] == 't')" "True" \
1145 gdb_test "python print(saved_options\['nibbles'\])" "True" \
1148 check_format_string "a_point_t" "summary=True" \
1150 "print in summary mode"
1151 gdb_test "python print(saved_options\['summary'\])" "True" \
1155 # Run all the tests in common for both C and C++.
1156 proc_with_prefix test_all_common {} {
1159 # Single options set to True/False.
1173 test_repeat_threshold
1175 # Multiple options mixed together.
1177 # Various error conditions.
1182 # The current language ("c" or "c++" while running tests).
1185 with_test_prefix "format_string" {
1187 if { [build_inferior "${binfile}" "c"] == 0 } {
1188 with_test_prefix "lang_c" {
1189 save_vars { env(TERM) } {
1190 # We run all of these tests in an environment where styling
1191 # could work, but we only expect the final call to
1192 # test_styling to actually produce any styled output.
1194 set current_lang "c"
1195 prepare_gdb "${binfile}"
1202 # Perform C++ Tests.
1203 if { [build_inferior "${binfile}-cxx" "c++"] == 0 } {
1204 with_test_prefix "lang_cpp" {
1205 set current_lang "c++"
1206 prepare_gdb "${binfile}-cxx"