1 # Copyright (C) 2009-2020 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 # Build inferior to language specification.
28 proc build_inferior {exefile lang} {
29 global srcdir subdir srcfile testfile hex
31 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } {
32 untested "failed to compile in $lang mode"
40 proc prepare_gdb {exefile} {
41 global srcdir subdir srcfile testfile hex
45 gdb_reinitialize_dir $srcdir/$subdir
48 # Skip all tests if Python scripting is not enabled.
49 if { [skip_python_tests] } { continue }
51 if ![runto_main] then {
52 perror "couldn't run to breakpoint"
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_union" "\\{the_union = \\{an_int = 707406378, a_char = 42 '\\*'\\}\\}" \
128 "an_enum" "ENUM_BAR" \
129 "a_string" "${default_pointer_regexp} \"hello world\"" \
130 "a_binary_string" "${default_pointer_regexp} \"hello\"" \
131 "a_binary_string_array" "\"hello\\\\000world\"" \
132 "a_big_string" [get_cut_big_string 200] \
133 "an_array" "\\{2, 3, 5\\}" \
134 "an_array_with_repetition" "\\{1, 3 <repeats 12 times>, 5, 5, 5\\}" \
135 "a_symbol_pointer" "${default_pointer_regexp} <global_symbol>" \
136 "a_base_ref" "${default_ref_regexp}" \
139 # A sentinel value to pass to function to get them to use a default value
141 # Note that we cannot use $undefined for default arguments in function
142 # definitions as we would just get the literal "$undefined" string, so
143 # we need to repeat the string.
144 set undefined "\000UNDEFINED\000"
146 # Return $value if it's not $undefined, otherwise return the default value
147 # (from $default_regexp_dict) for the variable $var.
148 proc get_value_or_default { var value } {
150 if { $value != $undefined } {
154 global default_regexp_dict
155 return [dict get $default_regexp_dict $var]
158 # Check that using gdb.Value.format_string on the value representing the
159 # variable $var produces $expected.
160 proc check_format_string {
163 { expected "\000UNDEFINED\000" }
164 { name "\000UNDEFINED\000" }
168 set expected [get_value_or_default $var $expected]
169 if { $name == $undefined } {
170 set name "${var} with option ${opts}"
174 "python print (gdb.parse_and_eval ('${var}').format_string (${opts}))" \
179 # Check that printing $var with no options set, produces the expected
181 proc check_var_with_no_opts {
183 { expected "\000UNDEFINED\000" }
185 set expected [get_value_or_default $var $expected]
187 with_test_prefix "${var}" {
188 check_format_string \
193 # str () should behave like gdb.Value.format_string () with no args.
195 "python print (str (gdb.parse_and_eval ('${var}')))" \
201 # Check that printing $var with $opt set to True and set to False,
202 # produces the expected output.
203 proc check_var_with_bool_opt {
206 { true_expected "\000UNDEFINED\000" }
207 { false_expected "\000UNDEFINED\000" }
209 set true_expected [get_value_or_default $var $true_expected]
210 set false_expected [get_value_or_default $var $false_expected]
212 with_test_prefix "${var} with option ${opt}" {
213 # Option set to True.
214 check_format_string \
219 # Option set to False.
220 check_format_string \
228 # Test gdb.Value.format_string with no options.
229 proc test_no_opts {} {
232 check_var_with_no_opts "a_point_t"
233 check_var_with_no_opts "a_point_t_pointer"
234 check_var_with_no_opts "another_point"
235 check_var_with_no_opts "a_struct_with_union"
236 check_var_with_no_opts "an_enum"
237 check_var_with_no_opts "a_string"
238 check_var_with_no_opts "a_binary_string"
239 check_var_with_no_opts "a_binary_string_array"
240 check_var_with_no_opts "a_big_string"
241 check_var_with_no_opts "an_array"
242 check_var_with_no_opts "an_array_with_repetition"
243 check_var_with_no_opts "a_symbol_pointer"
245 if { $current_lang == "c++" } {
246 # Nothing changes in all of the C++ tests because deref_refs is not
248 check_var_with_no_opts "a_point_t_ref"
249 check_var_with_no_opts "a_base_ref"
253 # Test the raw option for gdb.Value.format_string.
256 global default_ref_regexp
258 check_var_with_bool_opt "raw" "a_point_t" \
260 check_var_with_bool_opt "raw" "a_point_t_pointer"
261 check_var_with_bool_opt "raw" "another_point" \
263 check_var_with_bool_opt "raw" "a_struct_with_union"
264 check_var_with_bool_opt "raw" "an_enum"
265 check_var_with_bool_opt "raw" "a_string"
266 check_var_with_bool_opt "raw" "a_binary_string"
267 check_var_with_bool_opt "raw" "a_binary_string_array"
268 check_var_with_bool_opt "raw" "a_big_string"
269 check_var_with_bool_opt "raw" "an_array"
270 check_var_with_bool_opt "raw" "an_array_with_repetition"
271 check_var_with_bool_opt "raw" "a_symbol_pointer"
273 if { $current_lang == "c++" } {
274 check_var_with_bool_opt "raw" "a_point_t_ref" \
275 ${default_ref_regexp}
276 check_var_with_bool_opt "raw" "a_base_ref"
280 "disable pretty-printer '' test_lookup_function" \
281 "enable pretty-printer '' test_lookup_function" {
282 check_var_with_no_opts "a_point_t" \
284 check_var_with_bool_opt "raw" "a_point_t" \
290 # Test the pretty_arrays option for gdb.Value.format_string.
291 proc test_pretty_arrays {} {
294 set an_array_pretty " \\{2,\[\r\n\]+ 3,\[\r\n\]+ 5\\}"
295 set an_array_with_repetition_pretty \
296 " \\{1,\[\r\n\]+ 3 <repeats 12 times>,\[\r\n\]+ 5,\[\r\n\]+ 5,\[\r\n\]+ 5\\}"
298 check_var_with_bool_opt "pretty_arrays" "a_point_t"
299 check_var_with_bool_opt "pretty_arrays" "a_point_t_pointer"
300 check_var_with_bool_opt "pretty_arrays" "another_point"
301 check_var_with_bool_opt "pretty_arrays" "a_struct_with_union"
302 check_var_with_bool_opt "pretty_arrays" "an_enum"
303 check_var_with_bool_opt "pretty_arrays" "a_string"
304 check_var_with_bool_opt "pretty_arrays" "a_binary_string"
305 check_var_with_bool_opt "pretty_arrays" "a_binary_string_array"
306 check_var_with_bool_opt "pretty_arrays" "a_big_string"
307 check_var_with_bool_opt "pretty_arrays" "an_array" \
309 check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
310 $an_array_with_repetition_pretty
311 check_var_with_bool_opt "pretty_arrays" "a_symbol_pointer"
313 if { $current_lang == "c++" } {
314 check_var_with_bool_opt "pretty_arrays" "a_point_t_ref"
315 check_var_with_bool_opt "pretty_arrays" "a_base_ref"
318 with_temp_option "set print array on" "set print array off" {
319 check_var_with_no_opts "an_array" \
321 check_var_with_bool_opt "pretty_arrays" "an_array" \
324 check_var_with_no_opts "an_array_with_repetition" \
325 $an_array_with_repetition_pretty
326 check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
327 $an_array_with_repetition_pretty
331 # Test the pretty_structs option for gdb.Value.format_string.
332 proc test_pretty_structs {} {
335 set a_struct_with_union_pretty \
336 "\\{\[\r\n\]+ the_union = \\{\[\r\n\]+ an_int = 707406378,\[\r\n\]+ a_char = 42 '\\*'\[\r\n\]+ \\}\[\r\n\]+\\}"
338 check_var_with_bool_opt "pretty_structs" "a_point_t"
339 check_var_with_bool_opt "pretty_structs" "a_point_t_pointer"
340 check_var_with_bool_opt "pretty_structs" "another_point"
341 check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
342 $a_struct_with_union_pretty
343 check_var_with_bool_opt "pretty_structs" "an_enum"
344 check_var_with_bool_opt "pretty_structs" "a_string"
345 check_var_with_bool_opt "pretty_structs" "a_binary_string"
346 check_var_with_bool_opt "pretty_structs" "a_binary_string_array"
347 check_var_with_bool_opt "pretty_structs" "a_big_string"
348 check_var_with_bool_opt "pretty_structs" "an_array"
349 check_var_with_bool_opt "pretty_structs" "an_array_with_repetition"
350 check_var_with_bool_opt "pretty_structs" "a_symbol_pointer"
352 if { $current_lang == "c++" } {
353 check_var_with_bool_opt "pretty_structs" "a_point_t_ref"
354 check_var_with_bool_opt "pretty_structs" "a_base_ref"
357 with_temp_option "set print structs on" "set print structs off" {
358 check_var_with_no_opts "a_struct_with_union"
359 check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
360 $a_struct_with_union_pretty
363 # point_t is usually printed through the pretty printer.
366 "disable pretty-printer '' test_lookup_function" \
367 "enable pretty-printer '' test_lookup_function" {
368 check_var_with_no_opts "a_point_t" \
370 check_var_with_bool_opt "pretty_structs" "a_point_t" \
371 "\\{\[\r\n\]+ x = 42, *\[\r\n\]+ y = 12\[\r\n\]+\\}" \
376 # Test the array_indexes option for gdb.Value.format_string.
377 proc test_array_indexes {} {
380 set an_array_with_indexes "\\{\\\[0\\\] = 2, \\\[1\\\] = 3, \\\[2\\\] = 5\\}"
381 set an_array_with_repetition_with_indexes \
382 "\\{\\\[0\\\] = 1, \\\[1\\\] = 3 <repeats 12 times>, \\\[13\\\] = 5, \\\[14\\\] = 5, \\\[15\\\] = 5\\}"
384 check_var_with_bool_opt "array_indexes" "a_point_t"
385 check_var_with_bool_opt "array_indexes" "a_point_t_pointer"
386 check_var_with_bool_opt "array_indexes" "another_point"
387 check_var_with_bool_opt "array_indexes" "a_struct_with_union"
388 check_var_with_bool_opt "array_indexes" "an_enum"
389 check_var_with_bool_opt "array_indexes" "a_string"
390 check_var_with_bool_opt "array_indexes" "a_binary_string"
391 check_var_with_bool_opt "array_indexes" "a_binary_string_array"
392 check_var_with_bool_opt "array_indexes" "a_big_string"
393 check_var_with_bool_opt "array_indexes" "an_array" \
394 $an_array_with_indexes
395 check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
396 $an_array_with_repetition_with_indexes
397 check_var_with_bool_opt "array_indexes" "a_symbol_pointer"
399 if { $current_lang == "c++" } {
400 check_var_with_bool_opt "array_indexes" "a_point_t_ref"
401 check_var_with_bool_opt "array_indexes" "a_base_ref"
405 "set print array-indexes on" \
406 "set print array-indexes off" {
407 check_var_with_no_opts "an_array" \
408 $an_array_with_indexes
409 check_var_with_bool_opt "array_indexes" "an_array" \
410 $an_array_with_indexes
412 check_var_with_no_opts "an_array_with_repetition" \
413 $an_array_with_repetition_with_indexes
414 check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
415 $an_array_with_repetition_with_indexes
419 # Test the symbols option for gdb.Value.format_string.
420 proc test_symbols {} {
423 global default_pointer_regexp
425 check_var_with_bool_opt "symbols" "a_point_t"
426 check_var_with_bool_opt "symbols" "a_point_t_pointer"
427 check_var_with_bool_opt "symbols" "another_point"
428 check_var_with_bool_opt "symbols" "a_struct_with_union"
429 check_var_with_bool_opt "symbols" "an_enum"
430 check_var_with_bool_opt "symbols" "a_string"
431 check_var_with_bool_opt "symbols" "a_binary_string"
432 check_var_with_bool_opt "symbols" "a_binary_string_array"
433 check_var_with_bool_opt "symbols" "a_big_string"
434 check_var_with_bool_opt "symbols" "an_array"
435 check_var_with_bool_opt "symbols" "an_array_with_repetition"
436 check_var_with_bool_opt "symbols" "a_symbol_pointer" \
438 $default_pointer_regexp
440 if { $current_lang == "c++" } {
441 check_var_with_bool_opt "symbols" "a_point_t_ref"
442 check_var_with_bool_opt "symbols" "a_base_ref"
445 with_temp_option "set print symbol off" "set print symbol on" {
446 check_var_with_no_opts "a_symbol_pointer" \
447 $default_pointer_regexp
448 check_var_with_bool_opt "symbols" "a_symbol_pointer" \
450 $default_pointer_regexp
454 # Test the unions option for gdb.Value.format_string.
455 proc test_unions {} {
459 check_var_with_bool_opt "unions" "a_point_t"
460 check_var_with_bool_opt "unions" "a_point_t_pointer"
461 check_var_with_bool_opt "unions" "another_point"
462 check_var_with_bool_opt "unions" "a_struct_with_union" \
464 "\\{the_union = \\{...\\}\\}"
465 check_var_with_bool_opt "unions" "an_enum"
466 check_var_with_bool_opt "unions" "a_string"
467 check_var_with_bool_opt "unions" "a_binary_string"
468 check_var_with_bool_opt "unions" "a_binary_string_array"
469 check_var_with_bool_opt "unions" "a_big_string"
470 check_var_with_bool_opt "unions" "an_array"
471 check_var_with_bool_opt "unions" "an_array_with_repetition"
472 check_var_with_bool_opt "unions" "a_symbol_pointer"
474 if { $current_lang == "c++" } {
475 check_var_with_bool_opt "unions" "a_point_t_ref"
476 check_var_with_bool_opt "unions" "a_base_ref"
479 with_temp_option "set print union off" "set print union on" {
480 check_var_with_no_opts "a_struct_with_union" \
481 "\\{the_union = \\{...\\}\\}"
482 check_var_with_bool_opt "unions" "a_struct_with_union" \
484 "\\{the_union = \\{...\\}\\}"
488 # Test the deref_refs option for gdb.Value.format_string.
489 proc test_deref_refs {} {
491 global default_pointer_regexp
492 global default_ref_regexp
494 check_var_with_bool_opt "deref_refs" "a_point_t"
495 check_var_with_bool_opt "deref_refs" "a_point_t_pointer"
496 check_var_with_bool_opt "deref_refs" "another_point"
497 check_var_with_bool_opt "deref_refs" "a_struct_with_union"
498 check_var_with_bool_opt "deref_refs" "an_enum"
499 check_var_with_bool_opt "deref_refs" "a_string"
500 check_var_with_bool_opt "deref_refs" "a_binary_string"
501 check_var_with_bool_opt "deref_refs" "a_binary_string_array"
502 check_var_with_bool_opt "deref_refs" "a_big_string"
503 check_var_with_bool_opt "deref_refs" "an_array"
504 check_var_with_bool_opt "deref_refs" "an_array_with_repetition"
505 check_var_with_bool_opt "deref_refs" "a_symbol_pointer"
507 if { $current_lang == "c++" } {
508 check_var_with_bool_opt "deref_refs" "a_point_t_ref"
509 check_var_with_bool_opt "deref_refs" "a_base_ref" \
510 "${default_ref_regexp}: \\{_vptr\\.Base = ${default_pointer_regexp} <vtable for Deriv\\+16>, a = 42, static a_static_member = 2019\\}"
514 # Test the actual_objects option for gdb.Value.format_string.
515 proc test_actual_objects {} {
518 check_var_with_bool_opt "actual_objects" "a_point_t"
519 check_var_with_bool_opt "actual_objects" "a_point_t_pointer"
520 check_var_with_bool_opt "actual_objects" "another_point"
521 check_var_with_bool_opt "actual_objects" "a_struct_with_union"
522 check_var_with_bool_opt "actual_objects" "an_enum"
523 check_var_with_bool_opt "actual_objects" "a_string"
524 check_var_with_bool_opt "actual_objects" "a_binary_string"
525 check_var_with_bool_opt "actual_objects" "a_binary_string_array"
526 check_var_with_bool_opt "actual_objects" "a_big_string"
527 check_var_with_bool_opt "actual_objects" "an_array"
528 check_var_with_bool_opt "actual_objects" "an_array_with_repetition"
529 check_var_with_bool_opt "actual_objects" "a_symbol_pointer"
531 if { $current_lang == "c++" } {
532 # Nothing changes in all of the C++ tests because deref_refs is not
534 check_var_with_bool_opt "actual_objects" "a_point_t_ref"
535 check_var_with_bool_opt "actual_objects" "a_base_ref"
537 with_temp_option "set print object on" "set print object off" {
538 check_var_with_no_opts "a_point_t_ref"
539 check_var_with_bool_opt "actual_objects" "a_point_t_ref"
541 check_var_with_no_opts "a_base_ref"
542 check_var_with_bool_opt "actual_objects" "a_base_ref"
547 # Test the static_members option for gdb.Value.format_string.
548 proc test_static_members {} {
551 check_var_with_bool_opt "static_members" "a_point_t"
552 check_var_with_bool_opt "static_members" "a_point_t_pointer"
553 check_var_with_bool_opt "static_members" "another_point"
554 check_var_with_bool_opt "static_members" "a_struct_with_union"
555 check_var_with_bool_opt "static_members" "an_enum"
556 check_var_with_bool_opt "static_members" "a_string"
557 check_var_with_bool_opt "static_members" "a_binary_string"
558 check_var_with_bool_opt "static_members" "a_binary_string_array"
559 check_var_with_bool_opt "static_members" "a_big_string"
560 check_var_with_bool_opt "static_members" "an_array"
561 check_var_with_bool_opt "static_members" "an_array_with_repetition"
562 check_var_with_bool_opt "static_members" "a_symbol_pointer"
564 if { $current_lang == "c++" } {
565 # Nothing changes in all of the C++ tests because deref_refs is not
567 check_var_with_bool_opt "static_members" "a_point_t_ref"
568 check_var_with_bool_opt "static_members" "a_base_ref"
571 "set print static-members off" \
572 "set print static-members on" {
573 check_var_with_no_opts "a_point_t_ref"
574 check_var_with_bool_opt "static_members" "a_point_t_ref"
576 check_var_with_no_opts "a_base_ref"
577 check_var_with_bool_opt "static_members" "a_base_ref"
582 # Test the max_elements option for gdb.Value.format_string.
583 proc test_max_elements {} {
585 global default_pointer_regexp
587 # 200 is the default maximum number of elements, so setting it should
588 # not change the output.
589 set opts "max_elements=200"
590 with_test_prefix $opts {
591 check_format_string "a_point_t" $opts
592 check_format_string "a_point_t_pointer" $opts
593 check_format_string "another_point" $opts
594 check_format_string "a_struct_with_union" $opts
595 check_format_string "an_enum" $opts
596 check_format_string "a_string" $opts
597 check_format_string "a_binary_string" $opts
598 check_format_string "a_binary_string_array" $opts
599 check_format_string "a_big_string" $opts
600 check_format_string "an_array" $opts
601 check_format_string "an_array_with_repetition" $opts
602 check_format_string "a_symbol_pointer" $opts
604 if { $current_lang == "c++" } {
605 check_format_string "a_point_t_ref" $opts
606 check_format_string "a_base_ref" $opts
610 set opts "max_elements=3"
611 with_test_prefix $opts {
612 check_format_string "a_point_t" $opts
613 check_format_string "a_point_t_pointer" $opts
614 check_format_string "another_point" $opts
615 check_format_string "a_struct_with_union" $opts
616 check_format_string "an_enum" $opts
617 check_format_string "a_string" $opts \
618 "${default_pointer_regexp} \"hel\"..."
619 check_format_string "a_binary_string" $opts \
620 "${default_pointer_regexp} \"hel\"..."
621 # This will print four characters instead of three, see
622 # <https://sourceware.org/bugzilla/show_bug.cgi?id=24331>.
623 check_format_string "a_binary_string_array" $opts \
625 check_format_string "a_big_string" $opts \
626 [get_cut_big_string 3]
627 check_format_string "an_array" $opts
628 check_format_string "an_array_with_repetition" $opts \
629 "\\{1, 3 <repeats 12 times>...\\}"
630 check_format_string "a_symbol_pointer" $opts
632 if { $current_lang == "c++" } {
633 check_format_string "a_point_t_ref" $opts
634 check_format_string "a_base_ref" $opts
638 # Both 1,000 (we don't have that many elements) and 0 (unlimited) should
639 # mean no truncation.
640 foreach opts { "max_elements=1000" "max_elements=0" } {
641 with_test_prefix $opts {
642 check_format_string "a_point_t" $opts
643 check_format_string "a_point_t_pointer" $opts
644 check_format_string "another_point" $opts
645 check_format_string "a_struct_with_union" $opts
646 check_format_string "an_enum" $opts
647 check_format_string "a_string" $opts
648 check_format_string "a_binary_string" $opts
649 check_format_string "a_binary_string_array" $opts
650 check_format_string "a_big_string" $opts \
651 [get_cut_big_string 1000]
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
663 with_temp_option "set print elements 4" "set print elements 200" {
664 check_format_string "a_string" "" \
665 "${default_pointer_regexp} \"hell\"..."
666 check_format_string "a_binary_string" "" \
667 "${default_pointer_regexp} \"hell\"..."
668 check_format_string "a_binary_string_array" "" \
670 check_format_string "an_array_with_repetition" "" \
671 "\\{1, 3 <repeats 12 times>...\\}"
675 # Test the max_depth option for gdb.Value.format_string.
676 proc test_max_depth {} {
677 set opts "max_depth=-1"
678 with_test_prefix $opts {
679 check_format_string "a_struct_with_union" $opts
681 set opts "max_depth=0"
682 with_test_prefix $opts {
683 check_format_string "a_struct_with_union" $opts "\\{\.\.\.\\}"
685 set opts "max_depth=1"
686 with_test_prefix $opts {
687 check_format_string "a_struct_with_union" $opts "\\{the_union = \\{\.\.\.\\}\\}"
689 set opts "max_depth=2"
690 with_test_prefix $opts {
691 check_format_string "a_struct_with_union" $opts
695 # Test the repeat_threshold option for gdb.Value.format_string.
696 proc test_repeat_threshold {} {
698 global default_pointer_regexp
700 # 10 is the default threshold for repeated items, so setting it should
701 # not change the output.
702 set opts "repeat_threshold=10"
703 with_test_prefix $opts {
704 check_format_string "a_point_t" $opts
705 check_format_string "a_point_t_pointer" $opts
706 check_format_string "another_point" $opts
707 check_format_string "a_struct_with_union" $opts
708 check_format_string "an_enum" $opts
709 check_format_string "a_string" $opts
710 check_format_string "a_binary_string" $opts
711 check_format_string "a_binary_string_array" $opts
712 check_format_string "a_big_string" $opts
713 check_format_string "an_array" $opts
714 check_format_string "an_array_with_repetition" $opts
715 check_format_string "a_symbol_pointer" $opts
717 if { $current_lang == "c++" } {
718 check_format_string "a_point_t_ref" $opts
719 check_format_string "a_base_ref" $opts
723 set opts "repeat_threshold=1"
724 with_test_prefix $opts {
725 check_format_string "a_point_t" $opts
726 check_format_string "a_point_t_pointer" $opts
727 check_format_string "another_point" $opts
728 check_format_string "a_struct_with_union" $opts
729 check_format_string "an_enum" $opts
730 check_format_string "a_string" $opts \
731 "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o world\""
732 check_format_string "a_binary_string" $opts \
733 "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o\""
734 check_format_string "a_binary_string_array" $opts \
735 "\"he\", 'l' <repeats 2 times>, \"o\\\\000world\""
736 check_format_string "a_big_string" $opts
737 check_format_string "an_array" $opts
738 check_format_string "an_array_with_repetition" $opts \
739 "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
741 check_format_string "a_symbol_pointer" $opts
743 if { $current_lang == "c++" } {
744 check_format_string "a_point_t_ref" $opts
745 check_format_string "a_base_ref" $opts
749 set opts "repeat_threshold=3"
750 with_test_prefix $opts {
751 check_format_string "a_point_t" $opts
752 check_format_string "a_point_t_pointer" $opts
753 check_format_string "another_point" $opts
754 check_format_string "a_struct_with_union" $opts
755 check_format_string "an_enum" $opts
756 check_format_string "a_string" $opts
757 check_format_string "a_binary_string" $opts
758 check_format_string "a_binary_string_array" $opts
759 check_format_string "a_big_string" $opts
760 check_format_string "an_array" $opts
761 check_format_string "an_array_with_repetition" $opts
762 check_format_string "a_symbol_pointer" $opts
764 if { $current_lang == "c++" } {
765 check_format_string "a_point_t_ref" $opts
766 check_format_string "a_base_ref" $opts
770 # Both 100 (we don't have that many repeated elements) and 0 (unlimited)
771 # should mean no truncation.
772 foreach opts { "repeat_threshold=100" "repeat_threshold=0" } {
773 with_test_prefix $opts {
774 check_format_string "a_point_t" $opts
775 check_format_string "a_point_t_pointer" $opts
776 check_format_string "another_point" $opts
777 check_format_string "a_struct_with_union" $opts
778 check_format_string "an_enum" $opts
779 check_format_string "a_string" $opts
780 check_format_string "a_binary_string" $opts
781 check_format_string "a_binary_string_array" $opts
782 check_format_string "a_big_string" $opts
783 check_format_string "an_array" $opts
784 check_format_string "an_array_with_repetition" $opts \
785 "\\{1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5\\}"
786 check_format_string "a_symbol_pointer" $opts
788 if { $current_lang == "c++" } {
789 check_format_string "a_point_t_ref" $opts
790 check_format_string "a_base_ref" $opts
795 with_temp_option "set print repeats 1" "set print repeats 10" {
796 check_format_string "an_array_with_repetition" "" \
797 "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
801 # Test the format option for gdb.Value.format_string.
802 proc test_format {} {
804 global default_pointer_regexp
807 set opts "format='x'"
808 with_test_prefix $opts {
809 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
811 "42 with option ${opts}"
813 check_format_string "a_point_t" $opts
814 check_format_string "a_point_t_pointer" $opts
815 check_format_string "another_point" $opts
816 check_format_string "a_struct_with_union" $opts \
817 "\\{the_union = \\{an_int = 0x2a2a2a2a, a_char = 0x2a\\}\\}"
818 check_format_string "an_enum" $opts \
820 check_format_string "a_string" $opts \
821 $default_pointer_regexp
822 check_format_string "a_binary_string" $opts \
823 $default_pointer_regexp
824 check_format_string "a_binary_string_array" $opts \
825 "\\{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0\\}"
826 check_format_string "a_big_string" $opts \
827 "\\{0x41, 0x42, 0x43, 0x44, 0x45, \[, x0-9a-f\]+\.\.\.\\}"
828 check_format_string "an_array" $opts \
829 "\\{0x2, 0x3, 0x5\\}"
830 check_format_string "an_array_with_repetition" $opts \
831 "\\{0x1, 0x3 <repeats 12 times>, 0x5, 0x5, 0x5\\}"
832 check_format_string "a_symbol_pointer" $opts \
833 $default_pointer_regexp
835 if { $current_lang == "c++" } {
836 check_format_string "a_point_t_ref" $opts
837 check_format_string "a_base_ref" $opts
842 set opts "format='d'"
843 with_test_prefix $opts {
844 set decimal_pointer_regexp "\[0-9\]+"
845 gdb_test "python print (gdb.Value (0x2a).format_string (${opts}))" \
847 "0x2a with option ${opts}"
849 check_format_string "a_point_t" $opts
850 check_format_string "a_point_t_pointer" $opts \
851 $decimal_pointer_regexp
852 check_format_string "another_point" $opts
853 check_format_string "a_struct_with_union" $opts \
854 "\\{the_union = \\{an_int = 707406378, a_char = 42\\}\\}"
855 check_format_string "an_enum" $opts \
857 check_format_string "a_string" $opts \
858 $decimal_pointer_regexp
859 check_format_string "a_binary_string" $opts \
860 $decimal_pointer_regexp
861 check_format_string "a_binary_string_array" $opts \
862 "\\{104, 101, 108, 108, 111, 0, 119, 111, 114, 108, 100, 0\\}"
863 check_format_string "a_big_string" $opts \
864 "\\{65, 66, 67, 68, 69, \[, 0-9\]+\.\.\.\\}"
865 check_format_string "an_array" $opts
866 check_format_string "an_array_with_repetition" $opts
867 check_format_string "a_symbol_pointer" $opts \
868 $decimal_pointer_regexp
870 if { $current_lang == "c++" } {
871 check_format_string "a_point_t_ref" $opts
872 check_format_string "a_base_ref" $opts
877 # Test mixing options.
880 global default_ref_regexp
881 global default_pointer_regexp
883 check_format_string "a_point_t" \
884 "raw=True, format='x'" \
885 "\\{x = 0x2a, y = 0xc\\}"
887 check_format_string "an_array" \
888 "array_indexes=True, pretty_arrays=True" \
889 " \\{\\\[0\\\] = 2,\[\r\n\]+ \\\[1\\\] = 3,\[\r\n\]+ \\\[2\\\] = 5\\}"
891 check_format_string "a_struct_with_union" \
892 "pretty_structs=True, unions=False" \
893 "\\{\[\r\n\]+ the_union = \\{\.\.\.\\}\[\r\n\]+\\}"
895 check_format_string "a_symbol_pointer" \
896 "symbols=False, format='d'" \
899 if { $current_lang == "c++" } {
900 check_format_string "a_point_t_ref" \
901 "deref_refs=True, actual_objects=True, raw=True" \
902 "${default_ref_regexp}: \\{x = 42, y = 12\\}"
904 check_format_string "a_base_ref" \
905 "deref_refs=True, static_members=False" \
906 "${default_ref_regexp}: \\{_vptr\\.Base = ${default_pointer_regexp} <vtable for Deriv\\+16>, a = 42\\}"
910 # Test passing invalid arguments to gdb.Value.format_string.
911 proc test_invalid_args {} {
912 check_format_string \
915 "TypeError: format_string\\(\\) takes 0 positional arguments but 1 were given.*"
917 check_format_string \
920 "TypeError: 'invalid' is an invalid keyword argument for this function.*"
922 check_format_string \
925 "TypeError: argument 1 must be bool, not str.*"
927 check_format_string \
930 "ValueError: a single character is required.*"
933 # Run all the tests in common for both C and C++.
934 proc test_all_common {} {
937 # Single options set to True/False.
949 test_repeat_threshold
951 # Multiple options mixed together.
953 # Various error conditions.
957 # The current language ("c" or "c++" while running tests).
960 with_test_prefix "format_string" {
962 if { [build_inferior "${binfile}" "c"] == 0 } {
963 with_test_prefix "lang_c" {
965 prepare_gdb "${binfile}"
971 if { [build_inferior "${binfile}-cxx" "c++"] == 0 } {
972 with_test_prefix "lang_cpp" {
973 set current_lang "c++"
974 prepare_gdb "${binfile}-cxx"