Update copyright year range in all GDB files.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-format-string.exp
1 # Copyright (C) 2009-2020 Free Software Foundation, Inc.
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
19 load_lib gdb-python.exp
20
21 standard_testfile
22
23 if [get_compiler_info c++] {
24 return -1
25 }
26
27 # Build inferior to language specification.
28 proc build_inferior {exefile lang} {
29 global srcdir subdir srcfile testfile hex
30
31 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } {
32 untested "failed to compile in $lang mode"
33 return -1
34 }
35
36 return 0
37 }
38
39 # Restart GDB.
40 proc prepare_gdb {exefile} {
41 global srcdir subdir srcfile testfile hex
42
43 gdb_exit
44 gdb_start
45 gdb_reinitialize_dir $srcdir/$subdir
46 gdb_load ${exefile}
47
48 # Skip all tests if Python scripting is not enabled.
49 if { [skip_python_tests] } { continue }
50
51 if ![runto_main] then {
52 perror "couldn't run to breakpoint"
53 return
54 }
55
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"
60
61 runto_bp "break here"
62 }
63
64 # Set breakpoint and run to that breakpoint.
65 proc runto_bp {bp} {
66 gdb_breakpoint [gdb_get_line_number $bp]
67 gdb_continue_to_breakpoint $bp
68 }
69
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" ".*"
75 uplevel 1 $body
76 gdb_test "$unset_cmd" ".*"
77 }
78 }
79
80 # A regular expression for a pointer.
81 set default_pointer_regexp "0x\[a-fA-F0-9\]+"
82
83 # A regular expression for a non-expanded C++ reference.
84 #
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
87 # GDB print command.
88 set default_ref_regexp "@${default_pointer_regexp}"
89
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
96 }
97 unset alphabet
98
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
106
107 set whole_size [string length $whole_big_string]
108 if { $max > $whole_size } {
109 return "\"${whole_big_string}\""
110 }
111
112 set cut_string [string range $whole_big_string 0 [expr $max - 1]]
113 return "\"${cut_string}\"..."
114 }
115
116 # A dictionary mapping from C variable names to their default string
117 # representation when using str () or gdb.Value.format_string () with
118 # no arguments.
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}" \
137 ]
138
139 # A sentinel value to pass to function to get them to use a default value
140 # instead.
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"
145
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 } {
149 global undefined
150 if { $value != $undefined } {
151 return $value
152 }
153
154 global default_regexp_dict
155 return [dict get $default_regexp_dict $var]
156 }
157
158 # Check that using gdb.Value.format_string on the value representing the
159 # variable $var produces $expected.
160 proc check_format_string {
161 var
162 opts
163 { expected "\000UNDEFINED\000" }
164 { name "\000UNDEFINED\000" }
165 } {
166 global undefined
167
168 set expected [get_value_or_default $var $expected]
169 if { $name == $undefined } {
170 set name "${var} with option ${opts}"
171 }
172
173 gdb_test \
174 "python print (gdb.parse_and_eval ('${var}').format_string (${opts}))" \
175 $expected \
176 $name
177 }
178
179 # Check that printing $var with no options set, produces the expected
180 # output.
181 proc check_var_with_no_opts {
182 var
183 { expected "\000UNDEFINED\000" }
184 } {
185 set expected [get_value_or_default $var $expected]
186
187 with_test_prefix "${var}" {
188 check_format_string \
189 $var \
190 "" \
191 $expected \
192 "no opts"
193 # str () should behave like gdb.Value.format_string () with no args.
194 gdb_test \
195 "python print (str (gdb.parse_and_eval ('${var}')))" \
196 $expected \
197 "str"
198 }
199 }
200
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 {
204 opt
205 var
206 { true_expected "\000UNDEFINED\000" }
207 { false_expected "\000UNDEFINED\000" }
208 } {
209 set true_expected [get_value_or_default $var $true_expected]
210 set false_expected [get_value_or_default $var $false_expected]
211
212 with_test_prefix "${var} with option ${opt}" {
213 # Option set to True.
214 check_format_string \
215 $var \
216 "${opt}=True" \
217 $true_expected \
218 "${opt}=true"
219 # Option set to False.
220 check_format_string \
221 $var \
222 "${opt}=False" \
223 $false_expected \
224 "${opt}=false"
225 }
226 }
227
228 # Test gdb.Value.format_string with no options.
229 proc test_no_opts {} {
230 global current_lang
231
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"
244
245 if { $current_lang == "c++" } {
246 # Nothing changes in all of the C++ tests because deref_refs is not
247 # True.
248 check_var_with_no_opts "a_point_t_ref"
249 check_var_with_no_opts "a_base_ref"
250 }
251 }
252
253 # Test the raw option for gdb.Value.format_string.
254 proc test_raw {} {
255 global current_lang
256 global default_ref_regexp
257
258 check_var_with_bool_opt "raw" "a_point_t" \
259 "{x = 42, y = 12}"
260 check_var_with_bool_opt "raw" "a_point_t_pointer"
261 check_var_with_bool_opt "raw" "another_point" \
262 "{x = 123, y = 456}"
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"
272
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"
277 }
278
279 with_temp_option \
280 "disable pretty-printer '' test_lookup_function" \
281 "enable pretty-printer '' test_lookup_function" {
282 check_var_with_no_opts "a_point_t" \
283 "{x = 42, y = 12}"
284 check_var_with_bool_opt "raw" "a_point_t" \
285 "{x = 42, y = 12}" \
286 "{x = 42, y = 12}"
287 }
288 }
289
290 # Test the pretty_arrays option for gdb.Value.format_string.
291 proc test_pretty_arrays {} {
292 global current_lang
293
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\\}"
297
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" \
308 $an_array_pretty
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"
312
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"
316 }
317
318 with_temp_option "set print array on" "set print array off" {
319 check_var_with_no_opts "an_array" \
320 $an_array_pretty
321 check_var_with_bool_opt "pretty_arrays" "an_array" \
322 $an_array_pretty
323
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
328 }
329 }
330
331 # Test the pretty_structs option for gdb.Value.format_string.
332 proc test_pretty_structs {} {
333 global current_lang
334
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\]+\\}"
337
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"
351
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"
355 }
356
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
361 }
362
363 # point_t is usually printed through the pretty printer.
364 # Try disabling it.
365 with_temp_option \
366 "disable pretty-printer '' test_lookup_function" \
367 "enable pretty-printer '' test_lookup_function" {
368 check_var_with_no_opts "a_point_t" \
369 "{x = 42, y = 12}"
370 check_var_with_bool_opt "pretty_structs" "a_point_t" \
371 "\\{\[\r\n\]+ x = 42, *\[\r\n\]+ y = 12\[\r\n\]+\\}" \
372 "{x = 42, y = 12}" \
373 }
374 }
375
376 # Test the array_indexes option for gdb.Value.format_string.
377 proc test_array_indexes {} {
378 global current_lang
379
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\\}"
383
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"
398
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"
402 }
403
404 with_temp_option \
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
411
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
416 }
417 }
418
419 # Test the symbols option for gdb.Value.format_string.
420 proc test_symbols {} {
421 global undefined
422 global current_lang
423 global default_pointer_regexp
424
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" \
437 $undefined \
438 $default_pointer_regexp
439
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"
443 }
444
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" \
449 $undefined \
450 $default_pointer_regexp
451 }
452 }
453
454 # Test the unions option for gdb.Value.format_string.
455 proc test_unions {} {
456 global undefined
457 global current_lang
458
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" \
463 $undefined \
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"
473
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"
477 }
478
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" \
483 $undefined \
484 "\\{the_union = \\{...\\}\\}"
485 }
486 }
487
488 # Test the deref_refs option for gdb.Value.format_string.
489 proc test_deref_refs {} {
490 global current_lang
491 global default_pointer_regexp
492 global default_ref_regexp
493
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"
506
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\\}"
511 }
512 }
513
514 # Test the actual_objects option for gdb.Value.format_string.
515 proc test_actual_objects {} {
516 global current_lang
517
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"
530
531 if { $current_lang == "c++" } {
532 # Nothing changes in all of the C++ tests because deref_refs is not
533 # True.
534 check_var_with_bool_opt "actual_objects" "a_point_t_ref"
535 check_var_with_bool_opt "actual_objects" "a_base_ref"
536
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"
540
541 check_var_with_no_opts "a_base_ref"
542 check_var_with_bool_opt "actual_objects" "a_base_ref"
543 }
544 }
545 }
546
547 # Test the static_members option for gdb.Value.format_string.
548 proc test_static_members {} {
549 global current_lang
550
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"
563
564 if { $current_lang == "c++" } {
565 # Nothing changes in all of the C++ tests because deref_refs is not
566 # True.
567 check_var_with_bool_opt "static_members" "a_point_t_ref"
568 check_var_with_bool_opt "static_members" "a_base_ref"
569
570 with_temp_option \
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"
575
576 check_var_with_no_opts "a_base_ref"
577 check_var_with_bool_opt "static_members" "a_base_ref"
578 }
579 }
580 }
581
582 # Test the max_elements option for gdb.Value.format_string.
583 proc test_max_elements {} {
584 global current_lang
585 global default_pointer_regexp
586
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
603
604 if { $current_lang == "c++" } {
605 check_format_string "a_point_t_ref" $opts
606 check_format_string "a_base_ref" $opts
607 }
608 }
609
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 \
624 "\"hell\"..."
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
631
632 if { $current_lang == "c++" } {
633 check_format_string "a_point_t_ref" $opts
634 check_format_string "a_base_ref" $opts
635 }
636 }
637
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
655
656 if { $current_lang == "c++" } {
657 check_format_string "a_point_t_ref" $opts
658 check_format_string "a_base_ref" $opts
659 }
660 }
661 }
662
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" "" \
669 "\"hell\"..."
670 check_format_string "an_array_with_repetition" "" \
671 "\\{1, 3 <repeats 12 times>...\\}"
672 }
673 }
674
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
680 }
681 set opts "max_depth=0"
682 with_test_prefix $opts {
683 check_format_string "a_struct_with_union" $opts "\\{\.\.\.\\}"
684 }
685 set opts "max_depth=1"
686 with_test_prefix $opts {
687 check_format_string "a_struct_with_union" $opts "\\{the_union = \\{\.\.\.\\}\\}"
688 }
689 set opts "max_depth=2"
690 with_test_prefix $opts {
691 check_format_string "a_struct_with_union" $opts
692 }
693 }
694
695 # Test the repeat_threshold option for gdb.Value.format_string.
696 proc test_repeat_threshold {} {
697 global current_lang
698 global default_pointer_regexp
699
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
716
717 if { $current_lang == "c++" } {
718 check_format_string "a_point_t_ref" $opts
719 check_format_string "a_base_ref" $opts
720 }
721 }
722
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>\\}"
740
741 check_format_string "a_symbol_pointer" $opts
742
743 if { $current_lang == "c++" } {
744 check_format_string "a_point_t_ref" $opts
745 check_format_string "a_base_ref" $opts
746 }
747 }
748
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
763
764 if { $current_lang == "c++" } {
765 check_format_string "a_point_t_ref" $opts
766 check_format_string "a_base_ref" $opts
767 }
768 }
769
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
787
788 if { $current_lang == "c++" } {
789 check_format_string "a_point_t_ref" $opts
790 check_format_string "a_base_ref" $opts
791 }
792 }
793 }
794
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>\\}"
798 }
799 }
800
801 # Test the format option for gdb.Value.format_string.
802 proc test_format {} {
803 global current_lang
804 global default_pointer_regexp
805
806 # Hexadecimal.
807 set opts "format='x'"
808 with_test_prefix $opts {
809 gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
810 "0x2a" \
811 "42 with option ${opts}"
812
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 \
819 "0x1"
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
834
835 if { $current_lang == "c++" } {
836 check_format_string "a_point_t_ref" $opts
837 check_format_string "a_base_ref" $opts
838 }
839 }
840
841 # Decimal.
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}))" \
846 "42" \
847 "0x2a with option ${opts}"
848
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 \
856 "1"
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
869
870 if { $current_lang == "c++" } {
871 check_format_string "a_point_t_ref" $opts
872 check_format_string "a_base_ref" $opts
873 }
874 }
875 }
876
877 # Test mixing options.
878 proc test_mixed {} {
879 global current_lang
880 global default_ref_regexp
881 global default_pointer_regexp
882
883 check_format_string "a_point_t" \
884 "raw=True, format='x'" \
885 "\\{x = 0x2a, y = 0xc\\}"
886
887 check_format_string "an_array" \
888 "array_indexes=True, pretty_arrays=True" \
889 " \\{\\\[0\\\] = 2,\[\r\n\]+ \\\[1\\\] = 3,\[\r\n\]+ \\\[2\\\] = 5\\}"
890
891 check_format_string "a_struct_with_union" \
892 "pretty_structs=True, unions=False" \
893 "\\{\[\r\n\]+ the_union = \\{\.\.\.\\}\[\r\n\]+\\}"
894
895 check_format_string "a_symbol_pointer" \
896 "symbols=False, format='d'" \
897 "\[0-9\]+"
898
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\\}"
903
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\\}"
907 }
908 }
909
910 # Test passing invalid arguments to gdb.Value.format_string.
911 proc test_invalid_args {} {
912 check_format_string \
913 "a_point_t" \
914 "12" \
915 "TypeError: format_string\\(\\) takes 0 positional arguments but 1 were given.*"
916
917 check_format_string \
918 "a_point_t" \
919 "invalid=True" \
920 "TypeError: 'invalid' is an invalid keyword argument for this function.*"
921
922 check_format_string \
923 "a_point_t" \
924 "raw='hello'" \
925 "TypeError: argument 1 must be bool, not str.*"
926
927 check_format_string \
928 "a_point_t" \
929 "format='xd'" \
930 "ValueError: a single character is required.*"
931 }
932
933 # Run all the tests in common for both C and C++.
934 proc test_all_common {} {
935 # No options.
936 test_no_opts
937 # Single options set to True/False.
938 test_raw
939 test_pretty_arrays
940 test_pretty_structs
941 test_array_indexes
942 test_symbols
943 test_unions
944 test_deref_refs
945 test_actual_objects
946 test_static_members
947 test_max_elements
948 test_max_depth
949 test_repeat_threshold
950 test_format
951 # Multiple options mixed together.
952 test_mixed
953 # Various error conditions.
954 test_invalid_args
955 }
956
957 # The current language ("c" or "c++" while running tests).
958 set current_lang ""
959
960 with_test_prefix "format_string" {
961 # Perform C Tests.
962 if { [build_inferior "${binfile}" "c"] == 0 } {
963 with_test_prefix "lang_c" {
964 set current_lang "c"
965 prepare_gdb "${binfile}"
966 test_all_common
967 }
968 }
969
970 # Perform C++ Tests.
971 if { [build_inferior "${binfile}-cxx" "c++"] == 0 } {
972 with_test_prefix "lang_cpp" {
973 set current_lang "c++"
974 prepare_gdb "${binfile}-cxx"
975 test_all_common
976 }
977 }
978 }
This page took 0.070743 seconds and 4 git commands to generate.