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