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
23 # Skip all tests if Python scripting is not enabled.
26 if { [skip_python_tests] } { continue }
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
52 gdb_reinitialize_dir $srcdir/$subdir
59 # Load the pretty printer.
60 set remote_python_file \
61 [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
62 gdb_test_no_output "source ${remote_python_file}" "load python file"
67 # Set breakpoint and run to that breakpoint.
69 gdb_breakpoint [gdb_get_line_number $bp]
70 gdb_continue_to_breakpoint $bp
73 # Set an option using the GDB command in $set_cmd, execute $body, and then
74 # restore the option using the GDB command in $unset_cmd.
75 proc with_temp_option { set_cmd unset_cmd body } {
76 with_test_prefix $set_cmd {
77 gdb_test "$set_cmd" ".*"
79 gdb_test "$unset_cmd" ".*"
83 # A regular expression for a pointer.
84 set default_pointer_regexp "0x\[a-fA-F0-9\]+"
86 # A regular expression for a non-expanded C++ reference.
88 # Stringifying a C++ reference produces an address preceeded by a "@" in
89 # Python, but, by default, the C++ reference/class is expanded by the
91 set default_ref_regexp "@${default_pointer_regexp}"
93 # The whole content of the C variable a_big_string, i.e. the whole English
94 # alphabet repeated 10 times.
95 set whole_big_string ""
96 set alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
97 for {set i 0} {$i < 10} {incr i} {
98 append whole_big_string $alphabet
102 # Produces a potentially cut down version of $whole_big_string like GDB
103 # would represent it.
104 # $max is the maximum number of characters allowed in the string (but
105 # the return value may contain more to accound for the extra quotes and
106 # "..." added by GDB).
107 proc get_cut_big_string { max } {
108 global whole_big_string
110 set whole_size [string length $whole_big_string]
111 if { $max > $whole_size } {
112 return "\"${whole_big_string}\""
115 set cut_string [string range $whole_big_string 0 [expr $max - 1]]
116 return "\"${cut_string}\"..."
119 # A dictionary mapping from C variable names to their default string
120 # representation when using str () or gdb.Value.format_string () with
122 # This usually matches what the print command prints if used with no
123 # options, except for C++ references which are not expanded by
124 # default in Python. See the comment above $default_ref_regexp.
125 set default_regexp_dict [dict create \
126 "a_point_t" "Pretty Point \\(42, 12\\)" \
127 "a_point_t_pointer" $default_pointer_regexp \
128 "a_point_t_ref" "Pretty Point \\(42, 12\\)" \
129 "another_point" "Pretty Point \\(123, 456\\)" \
130 "a_struct_with_point" "\\{the_point = Pretty Point \\(42, 12\\)\\}" \
131 "a_struct_with_union" "\\{the_union = \\{an_int = 707406378, a_char = 42 '\\*'\\}\\}" \
132 "an_enum" "ENUM_BAR" \
133 "a_string" "${default_pointer_regexp} \"hello world\"" \
134 "a_binary_string" "${default_pointer_regexp} \"hello\"" \
135 "a_binary_string_array" "\"hello\\\\000world\"" \
136 "a_big_string" [get_cut_big_string 200] \
137 "an_array" "\\{2, 3, 5\\}" \
138 "an_array_with_repetition" "\\{1, 3 <repeats 12 times>, 5, 5, 5\\}" \
139 "a_symbol_pointer" "${default_pointer_regexp} <global_symbol>" \
140 "a_base_ref" "${default_ref_regexp}" \
143 # A sentinel value to pass to function to get them to use a default value
145 # Note that we cannot use $undefined for default arguments in function
146 # definitions as we would just get the literal "$undefined" string, so
147 # we need to repeat the string.
148 set undefined "\000UNDEFINED\000"
150 # Return $value if it's not $undefined, otherwise return the default value
151 # (from $default_regexp_dict) for the variable $var.
152 proc get_value_or_default { var value } {
154 if { $value != $undefined } {
158 global default_regexp_dict
159 return [dict get $default_regexp_dict $var]
162 # Check that using gdb.Value.format_string on the value representing the
163 # variable $var produces $expected.
164 proc check_format_string {
167 { expected "\000UNDEFINED\000" }
168 { name "\000UNDEFINED\000" }
172 set expected [get_value_or_default $var $expected]
173 if { $name == $undefined } {
174 set name "${var} with option ${opts}"
178 "python print (gdb.parse_and_eval ('${var}').format_string (${opts}))" \
183 # Check that printing $var with no options set, produces the expected
185 proc check_var_with_no_opts {
187 { expected "\000UNDEFINED\000" }
189 set expected [get_value_or_default $var $expected]
191 with_test_prefix "${var}" {
192 check_format_string \
197 # str () should behave like gdb.Value.format_string () with no args.
199 "python print (str (gdb.parse_and_eval ('${var}')))" \
205 # Check that printing $var with $opt set to True and set to False,
206 # produces the expected output.
207 proc check_var_with_bool_opt {
210 { true_expected "\000UNDEFINED\000" }
211 { false_expected "\000UNDEFINED\000" }
213 set true_expected [get_value_or_default $var $true_expected]
214 set false_expected [get_value_or_default $var $false_expected]
216 with_test_prefix "${var} with option ${opt}" {
217 # Option set to True.
218 check_format_string \
223 # Option set to False.
224 check_format_string \
232 # Test gdb.Value.format_string with no options.
233 proc_with_prefix test_no_opts {} {
236 check_var_with_no_opts "a_point_t"
237 check_var_with_no_opts "a_point_t_pointer"
238 check_var_with_no_opts "another_point"
239 check_var_with_no_opts "a_struct_with_union"
240 check_var_with_no_opts "an_enum"
241 check_var_with_no_opts "a_string"
242 check_var_with_no_opts "a_binary_string"
243 check_var_with_no_opts "a_binary_string_array"
244 check_var_with_no_opts "a_big_string"
245 check_var_with_no_opts "an_array"
246 check_var_with_no_opts "an_array_with_repetition"
247 check_var_with_no_opts "a_symbol_pointer"
249 if { $current_lang == "c++" } {
250 # Nothing changes in all of the C++ tests because deref_refs is not
252 check_var_with_no_opts "a_point_t_ref"
253 check_var_with_no_opts "a_base_ref"
257 # Test the raw option for gdb.Value.format_string.
258 proc_with_prefix test_raw {} {
260 global default_ref_regexp
262 check_var_with_bool_opt "raw" "a_point_t" \
264 check_var_with_bool_opt "raw" "a_point_t_pointer"
265 check_var_with_bool_opt "raw" "another_point" \
267 check_var_with_bool_opt "raw" "a_struct_with_union"
268 check_var_with_bool_opt "raw" "an_enum"
269 check_var_with_bool_opt "raw" "a_string"
270 check_var_with_bool_opt "raw" "a_binary_string"
271 check_var_with_bool_opt "raw" "a_binary_string_array"
272 check_var_with_bool_opt "raw" "a_big_string"
273 check_var_with_bool_opt "raw" "an_array"
274 check_var_with_bool_opt "raw" "an_array_with_repetition"
275 check_var_with_bool_opt "raw" "a_symbol_pointer"
277 if { $current_lang == "c++" } {
278 check_var_with_bool_opt "raw" "a_point_t_ref" \
279 ${default_ref_regexp}
280 check_var_with_bool_opt "raw" "a_base_ref"
284 "disable pretty-printer '' test_lookup_function" \
285 "enable pretty-printer '' test_lookup_function" {
286 check_var_with_no_opts "a_point_t" \
288 check_var_with_bool_opt "raw" "a_point_t" \
294 # Test the pretty_arrays option for gdb.Value.format_string.
295 proc_with_prefix test_pretty_arrays {} {
298 set an_array_pretty "\\{\[\r\n\]+ 2,\[\r\n\]+ 3,\[\r\n\]+ 5\[\r\n\]+\\}"
299 set an_array_with_repetition_pretty \
300 "\\{\[\r\n\]+ 1,\[\r\n\]+ 3 <repeats 12 times>,\[\r\n\]+ 5,\[\r\n\]+ 5,\[\r\n\]+ 5\[\r\n\]+\\}"
302 check_var_with_bool_opt "pretty_arrays" "a_point_t"
303 check_var_with_bool_opt "pretty_arrays" "a_point_t_pointer"
304 check_var_with_bool_opt "pretty_arrays" "another_point"
305 check_var_with_bool_opt "pretty_arrays" "a_struct_with_union"
306 check_var_with_bool_opt "pretty_arrays" "an_enum"
307 check_var_with_bool_opt "pretty_arrays" "a_string"
308 check_var_with_bool_opt "pretty_arrays" "a_binary_string"
309 check_var_with_bool_opt "pretty_arrays" "a_binary_string_array"
310 check_var_with_bool_opt "pretty_arrays" "a_big_string"
311 check_var_with_bool_opt "pretty_arrays" "an_array" \
313 check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
314 $an_array_with_repetition_pretty
315 check_var_with_bool_opt "pretty_arrays" "a_symbol_pointer"
317 if { $current_lang == "c++" } {
318 check_var_with_bool_opt "pretty_arrays" "a_point_t_ref"
319 check_var_with_bool_opt "pretty_arrays" "a_base_ref"
322 with_temp_option "set print array on" "set print array off" {
323 check_var_with_no_opts "an_array" \
325 check_var_with_bool_opt "pretty_arrays" "an_array" \
328 check_var_with_no_opts "an_array_with_repetition" \
329 $an_array_with_repetition_pretty
330 check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
331 $an_array_with_repetition_pretty
335 # Test the pretty_structs option for gdb.Value.format_string.
336 proc_with_prefix test_pretty_structs {} {
339 set a_struct_with_union_pretty \
340 "\\{\[\r\n\]+ the_union = \\{\[\r\n\]+ an_int = 707406378,\[\r\n\]+ a_char = 42 '\\*'\[\r\n\]+ \\}\[\r\n\]+\\}"
342 check_var_with_bool_opt "pretty_structs" "a_point_t"
343 check_var_with_bool_opt "pretty_structs" "a_point_t_pointer"
344 check_var_with_bool_opt "pretty_structs" "another_point"
345 check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
346 $a_struct_with_union_pretty
347 check_var_with_bool_opt "pretty_structs" "an_enum"
348 check_var_with_bool_opt "pretty_structs" "a_string"
349 check_var_with_bool_opt "pretty_structs" "a_binary_string"
350 check_var_with_bool_opt "pretty_structs" "a_binary_string_array"
351 check_var_with_bool_opt "pretty_structs" "a_big_string"
352 check_var_with_bool_opt "pretty_structs" "an_array"
353 check_var_with_bool_opt "pretty_structs" "an_array_with_repetition"
354 check_var_with_bool_opt "pretty_structs" "a_symbol_pointer"
356 if { $current_lang == "c++" } {
357 check_var_with_bool_opt "pretty_structs" "a_point_t_ref"
358 check_var_with_bool_opt "pretty_structs" "a_base_ref"
361 with_temp_option "set print structs on" "set print structs off" {
362 check_var_with_no_opts "a_struct_with_union"
363 check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
364 $a_struct_with_union_pretty
367 # point_t is usually printed through the pretty printer.
370 "disable pretty-printer '' test_lookup_function" \
371 "enable pretty-printer '' test_lookup_function" {
372 check_var_with_no_opts "a_point_t" \
374 check_var_with_bool_opt "pretty_structs" "a_point_t" \
375 "\\{\[\r\n\]+ x = 42, *\[\r\n\]+ y = 12\[\r\n\]+\\}" \
380 # Test the array_indexes option for gdb.Value.format_string.
381 proc_with_prefix test_array_indexes {} {
384 set an_array_with_indexes "\\{\\\[0\\\] = 2, \\\[1\\\] = 3, \\\[2\\\] = 5\\}"
385 set an_array_with_repetition_with_indexes \
386 "\\{\\\[0\\\] = 1, \\\[1\\\] = 3 <repeats 12 times>, \\\[13\\\] = 5, \\\[14\\\] = 5, \\\[15\\\] = 5\\}"
388 check_var_with_bool_opt "array_indexes" "a_point_t"
389 check_var_with_bool_opt "array_indexes" "a_point_t_pointer"
390 check_var_with_bool_opt "array_indexes" "another_point"
391 check_var_with_bool_opt "array_indexes" "a_struct_with_union"
392 check_var_with_bool_opt "array_indexes" "an_enum"
393 check_var_with_bool_opt "array_indexes" "a_string"
394 check_var_with_bool_opt "array_indexes" "a_binary_string"
395 check_var_with_bool_opt "array_indexes" "a_binary_string_array"
396 check_var_with_bool_opt "array_indexes" "a_big_string"
397 check_var_with_bool_opt "array_indexes" "an_array" \
398 $an_array_with_indexes
399 check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
400 $an_array_with_repetition_with_indexes
401 check_var_with_bool_opt "array_indexes" "a_symbol_pointer"
403 if { $current_lang == "c++" } {
404 check_var_with_bool_opt "array_indexes" "a_point_t_ref"
405 check_var_with_bool_opt "array_indexes" "a_base_ref"
409 "set print array-indexes on" \
410 "set print array-indexes off" {
411 check_var_with_no_opts "an_array" \
412 $an_array_with_indexes
413 check_var_with_bool_opt "array_indexes" "an_array" \
414 $an_array_with_indexes
416 check_var_with_no_opts "an_array_with_repetition" \
417 $an_array_with_repetition_with_indexes
418 check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
419 $an_array_with_repetition_with_indexes
423 # Test the symbols option for gdb.Value.format_string.
424 proc_with_prefix test_symbols {} {
427 global default_pointer_regexp
429 check_var_with_bool_opt "symbols" "a_point_t"
430 check_var_with_bool_opt "symbols" "a_point_t_pointer"
431 check_var_with_bool_opt "symbols" "another_point"
432 check_var_with_bool_opt "symbols" "a_struct_with_union"
433 check_var_with_bool_opt "symbols" "an_enum"
434 check_var_with_bool_opt "symbols" "a_string"
435 check_var_with_bool_opt "symbols" "a_binary_string"
436 check_var_with_bool_opt "symbols" "a_binary_string_array"
437 check_var_with_bool_opt "symbols" "a_big_string"
438 check_var_with_bool_opt "symbols" "an_array"
439 check_var_with_bool_opt "symbols" "an_array_with_repetition"
440 check_var_with_bool_opt "symbols" "a_symbol_pointer" \
442 $default_pointer_regexp
444 if { $current_lang == "c++" } {
445 check_var_with_bool_opt "symbols" "a_point_t_ref"
446 check_var_with_bool_opt "symbols" "a_base_ref"
449 with_temp_option "set print symbol off" "set print symbol on" {
450 check_var_with_no_opts "a_symbol_pointer" \
451 $default_pointer_regexp
452 check_var_with_bool_opt "symbols" "a_symbol_pointer" \
454 $default_pointer_regexp
458 # Test the unions option for gdb.Value.format_string.
459 proc_with_prefix test_unions {} {
463 check_var_with_bool_opt "unions" "a_point_t"
464 check_var_with_bool_opt "unions" "a_point_t_pointer"
465 check_var_with_bool_opt "unions" "another_point"
466 check_var_with_bool_opt "unions" "a_struct_with_union" \
468 "\\{the_union = \\{...\\}\\}"
469 check_var_with_bool_opt "unions" "an_enum"
470 check_var_with_bool_opt "unions" "a_string"
471 check_var_with_bool_opt "unions" "a_binary_string"
472 check_var_with_bool_opt "unions" "a_binary_string_array"
473 check_var_with_bool_opt "unions" "a_big_string"
474 check_var_with_bool_opt "unions" "an_array"
475 check_var_with_bool_opt "unions" "an_array_with_repetition"
476 check_var_with_bool_opt "unions" "a_symbol_pointer"
478 if { $current_lang == "c++" } {
479 check_var_with_bool_opt "unions" "a_point_t_ref"
480 check_var_with_bool_opt "unions" "a_base_ref"
483 with_temp_option "set print union off" "set print union on" {
484 check_var_with_no_opts "a_struct_with_union" \
485 "\\{the_union = \\{...\\}\\}"
486 check_var_with_bool_opt "unions" "a_struct_with_union" \
488 "\\{the_union = \\{...\\}\\}"
492 # Test the address option for gdb.Value.format_string.
493 proc_with_prefix test_address {} {
497 check_var_with_bool_opt "address" "a_point_t"
498 check_var_with_bool_opt "address" "a_point_t_pointer" \
501 check_var_with_bool_opt "address" "another_point"
502 check_var_with_bool_opt "symbols" "a_struct_with_union"
503 check_var_with_bool_opt "address" "an_enum"
504 check_var_with_bool_opt "address" "a_string" \
507 check_var_with_bool_opt "address" "a_binary_string" \
510 check_var_with_bool_opt "address" "a_binary_string_array"
511 check_var_with_bool_opt "address" "a_big_string"
512 check_var_with_bool_opt "address" "an_array"
513 check_var_with_bool_opt "address" "an_array_with_repetition"
514 check_var_with_bool_opt "address" "a_symbol_pointer" \
518 if { $current_lang == "c++" } {
519 check_var_with_bool_opt "address" "a_point_t_ref"
520 check_var_with_bool_opt "address" "a_base_ref" \
525 with_temp_option "set print address off" "set print address on" {
526 check_var_with_no_opts "a_string" \
528 check_var_with_bool_opt "address" "a_string" \
534 # Test the nibbles option for gdb.Value.format_string.
535 proc_with_prefix test_nibbles {} {
538 set opts "format='t', nibbles=True"
539 with_test_prefix $opts {
540 if { $current_lang == "c" } {
541 set binary_pointer_regexp "\[ 0-1\]+"
542 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
544 "42 with option ${opts}"
546 check_format_string "a_point_t" $opts \
547 [string_to_regexp "Pretty Point (0010 1010, 1100)"]
548 check_format_string "a_point_t_pointer" $opts \
549 $binary_pointer_regexp
550 check_format_string "another_point" $opts \
551 [string_to_regexp "Pretty Point (0111 1011, 0001 1100 1000)"]
553 check_format_string "a_struct_with_union" $opts \
554 "\\{the_union = \\{an_int = 0010 1010 0010 1010 0010 1010 0010 1010, a_char = 0010 1010\\}\\}"
555 check_format_string "an_enum" $opts \
557 check_format_string "a_string" $opts \
558 $binary_pointer_regexp
559 check_format_string "a_binary_string" $opts \
560 $binary_pointer_regexp
561 check_format_string "a_binary_string_array" $opts \
562 "\\{0110 1000, 0110 0101, 0110 1100, 0110 1100, 0110 1111, 0, 0111 0111, 0110 1111, 0111 0010, 0110 1100, 0110 0100, 0\\}"
563 check_format_string "a_big_string" $opts \
564 "\\{0100 0001, 0100 0010, 0100 0011, 0100 0100, 0100 0101, \[, 0-1\]+\.\.\.\\}"
565 check_format_string "an_array" $opts \
566 "\\{0010, 0011, 0101\\}"
567 check_format_string "an_array_with_repetition" $opts \
568 "\\{0001, 0011 <repeats 12 times>, 0101, 0101, 0101\\}"
569 check_format_string "a_symbol_pointer" $opts \
570 $binary_pointer_regexp
572 if { $current_lang == "c++" } {
573 set binary_pointer_regexp "\['0-1\]+"
574 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
576 "42 with option ${opts}"
578 check_format_string "a_point_t" $opts \
579 [string_to_regexp "Pretty Point (0010'1010, 1100)"]
580 check_format_string "a_point_t_pointer" $opts \
581 $binary_pointer_regexp
582 check_format_string "another_point" $opts \
583 [string_to_regexp "Pretty Point (0111'1011, 0001'1100'1000)"]
585 check_format_string "a_struct_with_union" $opts \
586 "\\{the_union = \\{an_int = 0010'1010'0010'1010'0010'1010'0010'1010, a_char = 0010'1010\\}\\}"
587 check_format_string "an_enum" $opts \
589 check_format_string "a_string" $opts \
590 $binary_pointer_regexp
591 check_format_string "a_binary_string" $opts \
592 $binary_pointer_regexp
593 check_format_string "a_binary_string_array" $opts \
594 "\\{0110'1000, 0110'0101, 0110'1100, 0110'1100, 0110'1111, 0, 0111'0111, 0110'1111, 0111'0010, 0110'1100, 0110'0100, 0\\}"
595 check_format_string "a_big_string" $opts \
596 "\\{0100'0001, 0100'0010, 0100'0011, 0100'0100, 0100'0101, \[, '0-1\]+\.\.\.\\}"
597 check_format_string "an_array" $opts \
598 "\\{0010, 0011, 0101\\}"
599 check_format_string "an_array_with_repetition" $opts \
600 "\\{0001, 0011 <repeats 12 times>, 0101, 0101, 0101\\}"
601 check_format_string "a_symbol_pointer" $opts \
602 $binary_pointer_regexp
604 check_format_string "a_point_t_ref" $opts \
605 [string_to_regexp "Pretty Point (0010'1010, 1100)"]
606 check_format_string "a_base_ref" $opts
611 # Test the deref_refs option for gdb.Value.format_string.
612 proc_with_prefix test_deref_refs {} {
614 global default_pointer_regexp
615 global default_ref_regexp
618 check_var_with_bool_opt "deref_refs" "a_point_t"
619 check_var_with_bool_opt "deref_refs" "a_point_t_pointer"
620 check_var_with_bool_opt "deref_refs" "another_point"
621 check_var_with_bool_opt "deref_refs" "a_struct_with_union"
622 check_var_with_bool_opt "deref_refs" "an_enum"
623 check_var_with_bool_opt "deref_refs" "a_string"
624 check_var_with_bool_opt "deref_refs" "a_binary_string"
625 check_var_with_bool_opt "deref_refs" "a_binary_string_array"
626 check_var_with_bool_opt "deref_refs" "a_big_string"
627 check_var_with_bool_opt "deref_refs" "an_array"
628 check_var_with_bool_opt "deref_refs" "an_array_with_repetition"
629 check_var_with_bool_opt "deref_refs" "a_symbol_pointer"
631 if { $current_lang == "c++" } {
632 check_var_with_bool_opt "deref_refs" "a_point_t_ref"
633 check_var_with_bool_opt "deref_refs" "a_base_ref" \
634 "${default_ref_regexp}: \\{_vptr\[.\$\]Base = ${default_pointer_regexp} <vtable for Deriv\\+$decimal>, a = 42, static a_static_member = 2019\\}"
638 # Test the actual_objects option for gdb.Value.format_string.
639 proc_with_prefix test_actual_objects {} {
642 check_var_with_bool_opt "actual_objects" "a_point_t"
643 check_var_with_bool_opt "actual_objects" "a_point_t_pointer"
644 check_var_with_bool_opt "actual_objects" "another_point"
645 check_var_with_bool_opt "actual_objects" "a_struct_with_union"
646 check_var_with_bool_opt "actual_objects" "an_enum"
647 check_var_with_bool_opt "actual_objects" "a_string"
648 check_var_with_bool_opt "actual_objects" "a_binary_string"
649 check_var_with_bool_opt "actual_objects" "a_binary_string_array"
650 check_var_with_bool_opt "actual_objects" "a_big_string"
651 check_var_with_bool_opt "actual_objects" "an_array"
652 check_var_with_bool_opt "actual_objects" "an_array_with_repetition"
653 check_var_with_bool_opt "actual_objects" "a_symbol_pointer"
655 if { $current_lang == "c++" } {
656 # Nothing changes in all of the C++ tests because deref_refs is not
658 check_var_with_bool_opt "actual_objects" "a_point_t_ref"
659 check_var_with_bool_opt "actual_objects" "a_base_ref"
661 with_temp_option "set print object on" "set print object off" {
662 check_var_with_no_opts "a_point_t_ref"
663 check_var_with_bool_opt "actual_objects" "a_point_t_ref"
665 check_var_with_no_opts "a_base_ref"
666 check_var_with_bool_opt "actual_objects" "a_base_ref"
671 # Test the static_members option for gdb.Value.format_string.
672 proc_with_prefix test_static_members {} {
675 check_var_with_bool_opt "static_members" "a_point_t"
676 check_var_with_bool_opt "static_members" "a_point_t_pointer"
677 check_var_with_bool_opt "static_members" "another_point"
678 check_var_with_bool_opt "static_members" "a_struct_with_union"
679 check_var_with_bool_opt "static_members" "an_enum"
680 check_var_with_bool_opt "static_members" "a_string"
681 check_var_with_bool_opt "static_members" "a_binary_string"
682 check_var_with_bool_opt "static_members" "a_binary_string_array"
683 check_var_with_bool_opt "static_members" "a_big_string"
684 check_var_with_bool_opt "static_members" "an_array"
685 check_var_with_bool_opt "static_members" "an_array_with_repetition"
686 check_var_with_bool_opt "static_members" "a_symbol_pointer"
688 if { $current_lang == "c++" } {
689 # Nothing changes in all of the C++ tests because deref_refs is not
691 check_var_with_bool_opt "static_members" "a_point_t_ref"
692 check_var_with_bool_opt "static_members" "a_base_ref"
695 "set print static-members off" \
696 "set print static-members on" {
697 check_var_with_no_opts "a_point_t_ref"
698 check_var_with_bool_opt "static_members" "a_point_t_ref"
700 check_var_with_no_opts "a_base_ref"
701 check_var_with_bool_opt "static_members" "a_base_ref"
706 # Test the max_elements option for gdb.Value.format_string.
707 proc_with_prefix test_max_elements {} {
709 global default_pointer_regexp
711 # 200 is the default maximum number of elements, so setting it should
712 # not change the output.
713 set opts "max_elements=200"
714 with_test_prefix $opts {
715 check_format_string "a_point_t" $opts
716 check_format_string "a_point_t_pointer" $opts
717 check_format_string "another_point" $opts
718 check_format_string "a_struct_with_union" $opts
719 check_format_string "an_enum" $opts
720 check_format_string "a_string" $opts
721 check_format_string "a_binary_string" $opts
722 check_format_string "a_binary_string_array" $opts
723 check_format_string "a_big_string" $opts
724 check_format_string "an_array" $opts
725 check_format_string "an_array_with_repetition" $opts
726 check_format_string "a_symbol_pointer" $opts
728 if { $current_lang == "c++" } {
729 check_format_string "a_point_t_ref" $opts
730 check_format_string "a_base_ref" $opts
734 set opts "max_elements=3"
735 with_test_prefix $opts {
736 check_format_string "a_point_t" $opts
737 check_format_string "a_point_t_pointer" $opts
738 check_format_string "another_point" $opts
739 check_format_string "a_struct_with_union" $opts
740 check_format_string "an_enum" $opts
741 check_format_string "a_string" $opts \
742 "${default_pointer_regexp} \"hel\"..."
743 check_format_string "a_binary_string" $opts \
744 "${default_pointer_regexp} \"hel\"..."
745 # This will print four characters instead of three, see
746 # <https://sourceware.org/bugzilla/show_bug.cgi?id=24331>.
747 check_format_string "a_binary_string_array" $opts \
749 check_format_string "a_big_string" $opts \
750 [get_cut_big_string 3]
751 check_format_string "an_array" $opts
752 check_format_string "an_array_with_repetition" $opts \
753 "\\{1, 3 <repeats 12 times>...\\}"
754 check_format_string "a_symbol_pointer" $opts
756 if { $current_lang == "c++" } {
757 check_format_string "a_point_t_ref" $opts
758 check_format_string "a_base_ref" $opts
762 # Both 1,000 (we don't have that many elements) and 0 (unlimited) should
763 # mean no truncation.
764 foreach opts { "max_elements=1000" "max_elements=0" } {
765 with_test_prefix $opts {
766 check_format_string "a_point_t" $opts
767 check_format_string "a_point_t_pointer" $opts
768 check_format_string "another_point" $opts
769 check_format_string "a_struct_with_union" $opts
770 check_format_string "an_enum" $opts
771 check_format_string "a_string" $opts
772 check_format_string "a_binary_string" $opts
773 check_format_string "a_binary_string_array" $opts
774 check_format_string "a_big_string" $opts \
775 [get_cut_big_string 1000]
776 check_format_string "an_array" $opts
777 check_format_string "an_array_with_repetition" $opts
778 check_format_string "a_symbol_pointer" $opts
780 if { $current_lang == "c++" } {
781 check_format_string "a_point_t_ref" $opts
782 check_format_string "a_base_ref" $opts
787 with_temp_option "set print elements 4" "set print elements 200" {
788 check_format_string "a_string" "" \
789 "${default_pointer_regexp} \"hell\"..."
790 check_format_string "a_binary_string" "" \
791 "${default_pointer_regexp} \"hell\"..."
792 check_format_string "a_binary_string_array" "" \
794 check_format_string "an_array_with_repetition" "" \
795 "\\{1, 3 <repeats 12 times>...\\}"
799 # Test the max_depth option for gdb.Value.format_string.
800 proc_with_prefix test_max_depth {} {
801 set opts "max_depth=-1"
802 with_test_prefix $opts {
803 check_format_string "a_struct_with_union" $opts
804 check_format_string "a_point_t" $opts "Pretty Point \\(42, 12\\)"
805 check_format_string "a_struct_with_point" $opts
807 set opts "max_depth=0"
808 with_test_prefix $opts {
809 check_format_string "a_struct_with_union" $opts "\\{\.\.\.\\}"
810 check_format_string "a_point_t" $opts "Pretty Point \\(42, 12\\)"
811 check_format_string "a_struct_with_point" $opts "\\{\.\.\.\\}"
813 set opts "max_depth=1"
814 with_test_prefix $opts {
815 check_format_string "a_struct_with_union" $opts "\\{the_union = \\{\.\.\.\\}\\}"
816 check_format_string "a_point_t" $opts "Pretty Point \\(42, 12\\)"
817 check_format_string "a_struct_with_point" $opts
819 set opts "max_depth=2"
820 with_test_prefix $opts {
821 check_format_string "a_struct_with_union" $opts
822 check_format_string "a_point_t" $opts "Pretty Point \\(42, 12\\)"
823 check_format_string "a_struct_with_point" $opts
827 # Test the repeat_threshold option for gdb.Value.format_string.
828 proc_with_prefix test_repeat_threshold {} {
830 global default_pointer_regexp
832 # 10 is the default threshold for repeated items, so setting it should
833 # not change the output.
834 set opts "repeat_threshold=10"
835 with_test_prefix $opts {
836 check_format_string "a_point_t" $opts
837 check_format_string "a_point_t_pointer" $opts
838 check_format_string "another_point" $opts
839 check_format_string "a_struct_with_union" $opts
840 check_format_string "an_enum" $opts
841 check_format_string "a_string" $opts
842 check_format_string "a_binary_string" $opts
843 check_format_string "a_binary_string_array" $opts
844 check_format_string "a_big_string" $opts
845 check_format_string "an_array" $opts
846 check_format_string "an_array_with_repetition" $opts
847 check_format_string "a_symbol_pointer" $opts
849 if { $current_lang == "c++" } {
850 check_format_string "a_point_t_ref" $opts
851 check_format_string "a_base_ref" $opts
855 set opts "repeat_threshold=1"
856 with_test_prefix $opts {
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
861 check_format_string "an_enum" $opts
862 check_format_string "a_string" $opts \
863 "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o world\""
864 check_format_string "a_binary_string" $opts \
865 "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o\""
866 check_format_string "a_binary_string_array" $opts \
867 "\"he\", 'l' <repeats 2 times>, \"o\\\\000world\""
868 check_format_string "a_big_string" $opts
869 check_format_string "an_array" $opts
870 check_format_string "an_array_with_repetition" $opts \
871 "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
873 check_format_string "a_symbol_pointer" $opts
875 if { $current_lang == "c++" } {
876 check_format_string "a_point_t_ref" $opts
877 check_format_string "a_base_ref" $opts
881 set opts "repeat_threshold=3"
882 with_test_prefix $opts {
883 check_format_string "a_point_t" $opts
884 check_format_string "a_point_t_pointer" $opts
885 check_format_string "another_point" $opts
886 check_format_string "a_struct_with_union" $opts
887 check_format_string "an_enum" $opts
888 check_format_string "a_string" $opts
889 check_format_string "a_binary_string" $opts
890 check_format_string "a_binary_string_array" $opts
891 check_format_string "a_big_string" $opts
892 check_format_string "an_array" $opts
893 check_format_string "an_array_with_repetition" $opts
894 check_format_string "a_symbol_pointer" $opts
896 if { $current_lang == "c++" } {
897 check_format_string "a_point_t_ref" $opts
898 check_format_string "a_base_ref" $opts
902 # Both 100 (we don't have that many repeated elements) and 0 (unlimited)
903 # should mean no truncation.
904 foreach opts { "repeat_threshold=100" "repeat_threshold=0" } {
905 with_test_prefix $opts {
906 check_format_string "a_point_t" $opts
907 check_format_string "a_point_t_pointer" $opts
908 check_format_string "another_point" $opts
909 check_format_string "a_struct_with_union" $opts
910 check_format_string "an_enum" $opts
911 check_format_string "a_string" $opts
912 check_format_string "a_binary_string" $opts
913 check_format_string "a_binary_string_array" $opts
914 check_format_string "a_big_string" $opts
915 check_format_string "an_array" $opts
916 check_format_string "an_array_with_repetition" $opts \
917 "\\{1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5\\}"
918 check_format_string "a_symbol_pointer" $opts
920 if { $current_lang == "c++" } {
921 check_format_string "a_point_t_ref" $opts
922 check_format_string "a_base_ref" $opts
927 with_temp_option "set print repeats 1" "set print repeats 10" {
928 check_format_string "an_array_with_repetition" "" \
929 "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
933 # Test the format option for gdb.Value.format_string.
934 proc_with_prefix test_format {} {
936 global default_pointer_regexp
939 set opts "format='x'"
940 with_test_prefix $opts {
941 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
943 "42 with option ${opts}"
945 check_format_string "a_point_t" $opts \
946 "Pretty Point \\(0x2a, 0xc\\)"
947 check_format_string "a_point_t_pointer" $opts
948 check_format_string "another_point" $opts \
949 "Pretty Point \\(0x7b, 0x1c8\\)"
950 check_format_string "a_struct_with_union" $opts \
951 "\\{the_union = \\{an_int = 0x2a2a2a2a, a_char = 0x2a\\}\\}"
952 check_format_string "an_enum" $opts \
954 check_format_string "a_string" $opts \
955 $default_pointer_regexp
956 check_format_string "a_binary_string" $opts \
957 $default_pointer_regexp
958 check_format_string "a_binary_string_array" $opts \
959 "\\{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0\\}"
960 check_format_string "a_big_string" $opts \
961 "\\{0x41, 0x42, 0x43, 0x44, 0x45, \[, x0-9a-f\]+\.\.\.\\}"
962 check_format_string "an_array" $opts \
963 "\\{0x2, 0x3, 0x5\\}"
964 check_format_string "an_array_with_repetition" $opts \
965 "\\{0x1, 0x3 <repeats 12 times>, 0x5, 0x5, 0x5\\}"
966 check_format_string "a_symbol_pointer" $opts \
967 $default_pointer_regexp
969 if { $current_lang == "c++" } {
970 check_format_string "a_point_t_ref" $opts \
971 "Pretty Point \\(0x2a, 0xc\\)"
972 check_format_string "a_base_ref" $opts
977 set opts "format='t'"
978 with_test_prefix $opts {
979 set binary_pointer_regexp "\[0-1\]+"
980 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
982 "42 with option ${opts}"
984 check_format_string "a_point_t" $opts \
985 "Pretty Point \\(101010, 1100\\)"
986 check_format_string "a_point_t_pointer" $opts \
987 $binary_pointer_regexp
988 check_format_string "another_point" $opts \
989 "Pretty Point \\(1111011, 111001000\\)"
990 check_format_string "a_struct_with_union" $opts \
991 "\\{the_union = \\{an_int = 101010001010100010101000101010, a_char = 101010\\}\\}"
992 check_format_string "an_enum" $opts \
994 check_format_string "a_string" $opts \
995 $binary_pointer_regexp
996 check_format_string "a_binary_string" $opts \
997 $binary_pointer_regexp
998 check_format_string "a_binary_string_array" $opts \
999 "\\{1101000, 1100101, 1101100, 1101100, 1101111, 0, 1110111, 1101111, 1110010, 1101100, 1100100, 0\\}"
1000 check_format_string "a_big_string" $opts \
1001 "\\{1000001, 1000010, 1000011, 1000100, 1000101, \[, 0-1\]+\.\.\.\\}"
1002 check_format_string "an_array" $opts \
1004 check_format_string "an_array_with_repetition" $opts \
1005 "\\{1, 11 <repeats 12 times>, 101, 101, 101\\}"
1006 check_format_string "a_symbol_pointer" $opts \
1007 $binary_pointer_regexp
1009 if { $current_lang == "c++" } {
1010 check_format_string "a_point_t_ref" $opts \
1011 "Pretty Point \\(101010, 1100\\)"
1012 check_format_string "a_base_ref" $opts
1017 set opts "format='d'"
1018 with_test_prefix $opts {
1019 set decimal_pointer_regexp "\[0-9\]+"
1020 gdb_test "python print (gdb.Value (0x2a).format_string (${opts}))" \
1022 "0x2a with option ${opts}"
1024 check_format_string "a_point_t" $opts
1025 check_format_string "a_point_t_pointer" $opts \
1026 $decimal_pointer_regexp
1027 check_format_string "another_point" $opts
1028 check_format_string "a_struct_with_union" $opts \
1029 "\\{the_union = \\{an_int = 707406378, a_char = 42\\}\\}"
1030 check_format_string "an_enum" $opts \
1032 check_format_string "a_string" $opts \
1033 $decimal_pointer_regexp
1034 check_format_string "a_binary_string" $opts \
1035 $decimal_pointer_regexp
1036 check_format_string "a_binary_string_array" $opts \
1037 "\\{104, 101, 108, 108, 111, 0, 119, 111, 114, 108, 100, 0\\}"
1038 check_format_string "a_big_string" $opts \
1039 "\\{65, 66, 67, 68, 69, \[, 0-9\]+\.\.\.\\}"
1040 check_format_string "an_array" $opts
1041 check_format_string "an_array_with_repetition" $opts
1042 check_format_string "a_symbol_pointer" $opts \
1043 $decimal_pointer_regexp
1045 if { $current_lang == "c++" } {
1046 check_format_string "a_point_t_ref" $opts
1047 check_format_string "a_base_ref" $opts
1052 # Test mixing options.
1053 proc_with_prefix test_mixed {} {
1055 global default_ref_regexp
1056 global default_pointer_regexp
1059 check_format_string "a_point_t" \
1060 "raw=True, format='x'" \
1061 "\\{x = 0x2a, y = 0xc\\}"
1063 check_format_string "an_array" \
1064 "array_indexes=True, pretty_arrays=True" \
1065 "\\{\[\r\n\]+ \\\[0\\\] = 2,\[\r\n\]+ \\\[1\\\] = 3,\[\r\n\]+ \\\[2\\\] = 5\[\r\n\]+\\}"
1067 check_format_string "a_struct_with_union" \
1068 "pretty_structs=True, unions=False" \
1069 "\\{\[\r\n\]+ the_union = \\{\.\.\.\\}\[\r\n\]+\\}"
1071 check_format_string "a_symbol_pointer" \
1072 "symbols=False, format='d'" \
1075 if { $current_lang == "c++" } {
1076 check_format_string "a_point_t_ref" \
1077 "deref_refs=True, actual_objects=True, raw=True" \
1078 "${default_ref_regexp}: \\{x = 42, y = 12\\}"
1080 check_format_string "a_base_ref" \
1081 "deref_refs=True, static_members=False" \
1082 "${default_ref_regexp}: \\{_vptr\[.\$\]Base = ${default_pointer_regexp} <vtable for Deriv\\+$decimal>, a = 42\\}"
1086 # Test passing invalid arguments to gdb.Value.format_string.
1087 proc_with_prefix test_invalid_args {} {
1088 check_format_string \
1091 "TypeError: format_string\\(\\) takes 0 positional arguments but 1 were given.*"
1093 check_format_string \
1096 "TypeError: 'invalid' is an invalid keyword argument for this function.*"
1098 check_format_string \
1101 "TypeError: argument 1 must be bool, not str.*"
1103 check_format_string \
1106 "ValueError: a single character is required.*"
1109 # Check the styling argument to format_string. This function needs to
1110 # be called with TERM set such that styling can be applied.
1111 proc test_styling {} {
1112 gdb_test "python print(gdb.parse_and_eval(\"a_point_t\").format_string(styling=True, raw=True))" \
1113 "{[style x variable] = 42, [style y variable] = 12}"
1116 # Test the gdb.print_options API.
1117 proc test_print_options {} {
1118 gdb_test_no_output "set print elements 500"
1119 gdb_test "python print(gdb.print_options()\['max_elements'\])" "500" \
1120 "examine max elements"
1121 gdb_test "python print('format' in gdb.print_options())" "False" \
1124 check_format_string "a_point_t" "format='t', nibbles=True" \
1125 "Pretty Point \\(0010.1010, 1100\\)" \
1126 "print in binary to fetch options"
1127 gdb_test "python print(saved_options\['format'\] == 't')" "True" \
1129 gdb_test "python print(saved_options\['nibbles'\])" "True" \
1132 check_format_string "a_point_t" "summary=True" \
1134 "print in summary mode"
1135 gdb_test "python print(saved_options\['summary'\])" "True" \
1139 # Run all the tests in common for both C and C++.
1140 proc_with_prefix test_all_common {} {
1143 # Single options set to True/False.
1157 test_repeat_threshold
1159 # Multiple options mixed together.
1161 # Various error conditions.
1166 # The current language ("c" or "c++" while running tests).
1169 with_test_prefix "format_string" {
1171 if { [build_inferior "${binfile}" "c"] == 0 } {
1172 with_test_prefix "lang_c" {
1173 save_vars { env(TERM) } {
1174 # We run all of these tests in an environment where styling
1175 # could work, but we only expect the final call to
1176 # test_styling to actually produce any styled output.
1178 set current_lang "c"
1179 prepare_gdb "${binfile}"
1186 # Perform C++ Tests.
1187 if { [build_inferior "${binfile}-cxx" "c++"] == 0 } {
1188 with_test_prefix "lang_cpp" {
1189 set current_lang "c++"
1190 prepare_gdb "${binfile}-cxx"