xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.python/py-format-string.exp (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
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
19load_lib gdb-python.exp
20
21standard_testfile
22
23if [get_compiler_info c++] {
24    return -1
25}
26
27# Skip all tests if Python scripting is not enabled.
28gdb_exit
29gdb_start
30if { [skip_python_tests] } { continue }
31
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
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\\)" \
129  "a_struct_with_union"		"\\{the_union = \\{an_int = 707406378, a_char = 42 '\\*'\\}\\}" \
130  "an_enum"			"ENUM_BAR" \
131  "a_string"			"${default_pointer_regexp} \"hello world\"" \
132  "a_binary_string"		"${default_pointer_regexp} \"hello\"" \
133  "a_binary_string_array"	"\"hello\\\\000world\"" \
134  "a_big_string"		[get_cut_big_string 200] \
135  "an_array"			"\\{2, 3, 5\\}" \
136  "an_array_with_repetition"	"\\{1, 3 <repeats 12 times>, 5, 5, 5\\}" \
137  "a_symbol_pointer"		"${default_pointer_regexp} <global_symbol>" \
138  "a_base_ref"			"${default_ref_regexp}" \
139  ]
140
141# A sentinel value to pass to function to get them to use a default value
142# instead.
143# Note that we cannot use $undefined for default arguments in function
144# definitions as we would just get the literal "$undefined" string, so
145# we need to repeat the string.
146set undefined "\000UNDEFINED\000"
147
148# Return $value if it's not $undefined, otherwise return the default value
149# (from $default_regexp_dict) for the variable $var.
150proc get_value_or_default { var value } {
151  global undefined
152  if { $value != $undefined } {
153    return $value
154  }
155
156  global default_regexp_dict
157  return [dict get $default_regexp_dict $var]
158}
159
160# Check that using gdb.Value.format_string on the value representing the
161# variable $var produces $expected.
162proc check_format_string {
163	var
164	opts
165	{ expected "\000UNDEFINED\000" }
166	{ name "\000UNDEFINED\000" }
167  } {
168  global undefined
169
170  set expected [get_value_or_default $var $expected]
171  if { $name == $undefined } {
172    set name "${var} with option ${opts}"
173  }
174
175  gdb_test \
176    "python print (gdb.parse_and_eval ('${var}').format_string (${opts}))" \
177    $expected \
178    $name
179}
180
181# Check that printing $var with no options set, produces the expected
182# output.
183proc check_var_with_no_opts {
184	var
185	{ expected "\000UNDEFINED\000" }
186  } {
187  set expected [get_value_or_default $var $expected]
188
189  with_test_prefix "${var}" {
190    check_format_string \
191      $var \
192      "" \
193      $expected \
194      "no opts"
195    # str () should behave like gdb.Value.format_string () with no args.
196    gdb_test \
197      "python print (str (gdb.parse_and_eval ('${var}')))" \
198      $expected \
199      "str"
200  }
201}
202
203# Check that printing $var with $opt set to True and set to False,
204# produces the expected output.
205proc check_var_with_bool_opt {
206	opt
207	var
208	{ true_expected  "\000UNDEFINED\000" }
209	{ false_expected "\000UNDEFINED\000" }
210  } {
211  set true_expected  [get_value_or_default $var $true_expected]
212  set false_expected [get_value_or_default $var $false_expected]
213
214  with_test_prefix "${var} with option ${opt}" {
215    # Option set to True.
216    check_format_string \
217      $var \
218      "${opt}=True" \
219      $true_expected \
220      "${opt}=true"
221    # Option set to False.
222    check_format_string \
223      $var \
224      "${opt}=False" \
225      $false_expected \
226      "${opt}=false"
227  }
228}
229
230# Test gdb.Value.format_string with no options.
231proc test_no_opts {} {
232  global current_lang
233
234  check_var_with_no_opts "a_point_t"
235  check_var_with_no_opts "a_point_t_pointer"
236  check_var_with_no_opts "another_point"
237  check_var_with_no_opts "a_struct_with_union"
238  check_var_with_no_opts "an_enum"
239  check_var_with_no_opts "a_string"
240  check_var_with_no_opts "a_binary_string"
241  check_var_with_no_opts "a_binary_string_array"
242  check_var_with_no_opts "a_big_string"
243  check_var_with_no_opts "an_array"
244  check_var_with_no_opts "an_array_with_repetition"
245  check_var_with_no_opts "a_symbol_pointer"
246
247  if { $current_lang == "c++" } {
248    # Nothing changes in all of the C++ tests because deref_refs is not
249    # True.
250    check_var_with_no_opts "a_point_t_ref"
251    check_var_with_no_opts "a_base_ref"
252  }
253}
254
255# Test the raw option for gdb.Value.format_string.
256proc test_raw {} {
257  global current_lang
258  global default_ref_regexp
259
260  check_var_with_bool_opt "raw" "a_point_t" \
261    "{x = 42, y = 12}"
262  check_var_with_bool_opt "raw" "a_point_t_pointer"
263  check_var_with_bool_opt "raw" "another_point" \
264    "{x = 123, y = 456}"
265  check_var_with_bool_opt "raw" "a_struct_with_union"
266  check_var_with_bool_opt "raw" "an_enum"
267  check_var_with_bool_opt "raw" "a_string"
268  check_var_with_bool_opt "raw" "a_binary_string"
269  check_var_with_bool_opt "raw" "a_binary_string_array"
270  check_var_with_bool_opt "raw" "a_big_string"
271  check_var_with_bool_opt "raw" "an_array"
272  check_var_with_bool_opt "raw" "an_array_with_repetition"
273  check_var_with_bool_opt "raw" "a_symbol_pointer"
274
275  if { $current_lang == "c++" } {
276    check_var_with_bool_opt "raw" "a_point_t_ref" \
277      ${default_ref_regexp}
278    check_var_with_bool_opt "raw" "a_base_ref"
279  }
280
281  with_temp_option \
282	"disable pretty-printer '' test_lookup_function" \
283	"enable pretty-printer '' test_lookup_function" {
284    check_var_with_no_opts "a_point_t" \
285      "{x = 42, y = 12}"
286    check_var_with_bool_opt "raw" "a_point_t" \
287      "{x = 42, y = 12}" \
288      "{x = 42, y = 12}"
289  }
290}
291
292# Test the pretty_arrays option for gdb.Value.format_string.
293proc test_pretty_arrays {} {
294  global current_lang
295
296  set an_array_pretty "\\{\[\r\n\]+  2,\[\r\n\]+  3,\[\r\n\]+  5\[\r\n\]+\\}"
297  set an_array_with_repetition_pretty \
298    "\\{\[\r\n\]+  1,\[\r\n\]+  3 <repeats 12 times>,\[\r\n\]+  5,\[\r\n\]+  5,\[\r\n\]+  5\[\r\n\]+\\}"
299
300  check_var_with_bool_opt "pretty_arrays" "a_point_t"
301  check_var_with_bool_opt "pretty_arrays" "a_point_t_pointer"
302  check_var_with_bool_opt "pretty_arrays" "another_point"
303  check_var_with_bool_opt "pretty_arrays" "a_struct_with_union"
304  check_var_with_bool_opt "pretty_arrays" "an_enum"
305  check_var_with_bool_opt "pretty_arrays" "a_string"
306  check_var_with_bool_opt "pretty_arrays" "a_binary_string"
307  check_var_with_bool_opt "pretty_arrays" "a_binary_string_array"
308  check_var_with_bool_opt "pretty_arrays" "a_big_string"
309  check_var_with_bool_opt "pretty_arrays" "an_array" \
310    $an_array_pretty
311  check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
312    $an_array_with_repetition_pretty
313  check_var_with_bool_opt "pretty_arrays" "a_symbol_pointer"
314
315  if { $current_lang == "c++" } {
316    check_var_with_bool_opt "pretty_arrays" "a_point_t_ref"
317    check_var_with_bool_opt "pretty_arrays" "a_base_ref"
318  }
319
320  with_temp_option "set print array on" "set print array off" {
321    check_var_with_no_opts "an_array" \
322      $an_array_pretty
323    check_var_with_bool_opt "pretty_arrays" "an_array" \
324      $an_array_pretty
325
326    check_var_with_no_opts "an_array_with_repetition" \
327      $an_array_with_repetition_pretty
328    check_var_with_bool_opt "pretty_arrays" "an_array_with_repetition" \
329      $an_array_with_repetition_pretty
330  }
331}
332
333# Test the pretty_structs option for gdb.Value.format_string.
334proc test_pretty_structs {} {
335  global current_lang
336
337  set a_struct_with_union_pretty \
338    "\\{\[\r\n\]+  the_union = \\{\[\r\n\]+    an_int = 707406378,\[\r\n\]+    a_char = 42 '\\*'\[\r\n\]+  \\}\[\r\n\]+\\}"
339
340  check_var_with_bool_opt "pretty_structs" "a_point_t"
341  check_var_with_bool_opt "pretty_structs" "a_point_t_pointer"
342  check_var_with_bool_opt "pretty_structs" "another_point"
343  check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
344    $a_struct_with_union_pretty
345  check_var_with_bool_opt "pretty_structs" "an_enum"
346  check_var_with_bool_opt "pretty_structs" "a_string"
347  check_var_with_bool_opt "pretty_structs" "a_binary_string"
348  check_var_with_bool_opt "pretty_structs" "a_binary_string_array"
349  check_var_with_bool_opt "pretty_structs" "a_big_string"
350  check_var_with_bool_opt "pretty_structs" "an_array"
351  check_var_with_bool_opt "pretty_structs" "an_array_with_repetition"
352  check_var_with_bool_opt "pretty_structs" "a_symbol_pointer"
353
354  if { $current_lang == "c++" } {
355    check_var_with_bool_opt "pretty_structs" "a_point_t_ref"
356    check_var_with_bool_opt "pretty_structs" "a_base_ref"
357  }
358
359  with_temp_option "set print structs on" "set print structs off" {
360    check_var_with_no_opts "a_struct_with_union"
361    check_var_with_bool_opt "pretty_structs" "a_struct_with_union" \
362      $a_struct_with_union_pretty
363  }
364
365  # point_t is usually printed through the pretty printer.
366  # Try disabling it.
367  with_temp_option \
368	"disable pretty-printer '' test_lookup_function" \
369	"enable pretty-printer '' test_lookup_function" {
370    check_var_with_no_opts "a_point_t" \
371      "{x = 42, y = 12}"
372    check_var_with_bool_opt "pretty_structs" "a_point_t" \
373      "\\{\[\r\n\]+  x = 42, *\[\r\n\]+  y = 12\[\r\n\]+\\}" \
374      "{x = 42, y = 12}" \
375  }
376}
377
378# Test the array_indexes option for gdb.Value.format_string.
379proc test_array_indexes {} {
380  global current_lang
381
382  set an_array_with_indexes "\\{\\\[0\\\] = 2, \\\[1\\\] = 3, \\\[2\\\] = 5\\}"
383  set an_array_with_repetition_with_indexes \
384    "\\{\\\[0\\\] = 1, \\\[1\\\] = 3 <repeats 12 times>, \\\[13\\\] = 5, \\\[14\\\] = 5, \\\[15\\\] = 5\\}"
385
386  check_var_with_bool_opt "array_indexes" "a_point_t"
387  check_var_with_bool_opt "array_indexes" "a_point_t_pointer"
388  check_var_with_bool_opt "array_indexes" "another_point"
389  check_var_with_bool_opt "array_indexes" "a_struct_with_union"
390  check_var_with_bool_opt "array_indexes" "an_enum"
391  check_var_with_bool_opt "array_indexes" "a_string"
392  check_var_with_bool_opt "array_indexes" "a_binary_string"
393  check_var_with_bool_opt "array_indexes" "a_binary_string_array"
394  check_var_with_bool_opt "array_indexes" "a_big_string"
395  check_var_with_bool_opt "array_indexes" "an_array" \
396    $an_array_with_indexes
397  check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
398    $an_array_with_repetition_with_indexes
399  check_var_with_bool_opt "array_indexes" "a_symbol_pointer"
400
401  if { $current_lang == "c++" } {
402    check_var_with_bool_opt "array_indexes" "a_point_t_ref"
403    check_var_with_bool_opt "array_indexes" "a_base_ref"
404  }
405
406  with_temp_option \
407	"set print array-indexes on" \
408	"set print array-indexes off" {
409    check_var_with_no_opts "an_array" \
410      $an_array_with_indexes
411    check_var_with_bool_opt "array_indexes" "an_array" \
412      $an_array_with_indexes
413
414    check_var_with_no_opts "an_array_with_repetition" \
415      $an_array_with_repetition_with_indexes
416    check_var_with_bool_opt "array_indexes" "an_array_with_repetition" \
417      $an_array_with_repetition_with_indexes
418  }
419}
420
421# Test the symbols option for gdb.Value.format_string.
422proc test_symbols {} {
423  global undefined
424  global current_lang
425  global default_pointer_regexp
426
427  check_var_with_bool_opt "symbols" "a_point_t"
428  check_var_with_bool_opt "symbols" "a_point_t_pointer"
429  check_var_with_bool_opt "symbols" "another_point"
430  check_var_with_bool_opt "symbols" "a_struct_with_union"
431  check_var_with_bool_opt "symbols" "an_enum"
432  check_var_with_bool_opt "symbols" "a_string"
433  check_var_with_bool_opt "symbols" "a_binary_string"
434  check_var_with_bool_opt "symbols" "a_binary_string_array"
435  check_var_with_bool_opt "symbols" "a_big_string"
436  check_var_with_bool_opt "symbols" "an_array"
437  check_var_with_bool_opt "symbols" "an_array_with_repetition"
438  check_var_with_bool_opt "symbols" "a_symbol_pointer" \
439    $undefined \
440    $default_pointer_regexp
441
442  if { $current_lang == "c++" } {
443    check_var_with_bool_opt "symbols" "a_point_t_ref"
444    check_var_with_bool_opt "symbols" "a_base_ref"
445  }
446
447  with_temp_option "set print symbol off" "set print symbol on" {
448    check_var_with_no_opts "a_symbol_pointer" \
449      $default_pointer_regexp
450    check_var_with_bool_opt "symbols" "a_symbol_pointer" \
451      $undefined \
452      $default_pointer_regexp
453  }
454}
455
456# Test the unions option for gdb.Value.format_string.
457proc test_unions {} {
458  global undefined
459  global current_lang
460
461  check_var_with_bool_opt "unions" "a_point_t"
462  check_var_with_bool_opt "unions" "a_point_t_pointer"
463  check_var_with_bool_opt "unions" "another_point"
464  check_var_with_bool_opt "unions" "a_struct_with_union" \
465    $undefined \
466    "\\{the_union = \\{...\\}\\}"
467  check_var_with_bool_opt "unions" "an_enum"
468  check_var_with_bool_opt "unions" "a_string"
469  check_var_with_bool_opt "unions" "a_binary_string"
470  check_var_with_bool_opt "unions" "a_binary_string_array"
471  check_var_with_bool_opt "unions" "a_big_string"
472  check_var_with_bool_opt "unions" "an_array"
473  check_var_with_bool_opt "unions" "an_array_with_repetition"
474  check_var_with_bool_opt "unions" "a_symbol_pointer"
475
476  if { $current_lang == "c++" } {
477    check_var_with_bool_opt "unions" "a_point_t_ref"
478    check_var_with_bool_opt "unions" "a_base_ref"
479  }
480
481  with_temp_option "set print union off" "set print union on" {
482    check_var_with_no_opts "a_struct_with_union" \
483      "\\{the_union = \\{...\\}\\}"
484    check_var_with_bool_opt "unions" "a_struct_with_union" \
485      $undefined \
486      "\\{the_union = \\{...\\}\\}"
487  }
488}
489
490# Test the deref_refs option for gdb.Value.format_string.
491proc test_deref_refs {} {
492  global current_lang
493  global default_pointer_regexp
494  global default_ref_regexp
495
496  check_var_with_bool_opt "deref_refs" "a_point_t"
497  check_var_with_bool_opt "deref_refs" "a_point_t_pointer"
498  check_var_with_bool_opt "deref_refs" "another_point"
499  check_var_with_bool_opt "deref_refs" "a_struct_with_union"
500  check_var_with_bool_opt "deref_refs" "an_enum"
501  check_var_with_bool_opt "deref_refs" "a_string"
502  check_var_with_bool_opt "deref_refs" "a_binary_string"
503  check_var_with_bool_opt "deref_refs" "a_binary_string_array"
504  check_var_with_bool_opt "deref_refs" "a_big_string"
505  check_var_with_bool_opt "deref_refs" "an_array"
506  check_var_with_bool_opt "deref_refs" "an_array_with_repetition"
507  check_var_with_bool_opt "deref_refs" "a_symbol_pointer"
508
509  if { $current_lang == "c++" } {
510    check_var_with_bool_opt "deref_refs" "a_point_t_ref"
511    check_var_with_bool_opt "deref_refs" "a_base_ref" \
512      "${default_ref_regexp}: \\{_vptr\\.Base = ${default_pointer_regexp} <vtable for Deriv\\+16>, a = 42, static a_static_member = 2019\\}"
513  }
514}
515
516# Test the actual_objects option for gdb.Value.format_string.
517proc test_actual_objects {} {
518  global current_lang
519
520  check_var_with_bool_opt "actual_objects" "a_point_t"
521  check_var_with_bool_opt "actual_objects" "a_point_t_pointer"
522  check_var_with_bool_opt "actual_objects" "another_point"
523  check_var_with_bool_opt "actual_objects" "a_struct_with_union"
524  check_var_with_bool_opt "actual_objects" "an_enum"
525  check_var_with_bool_opt "actual_objects" "a_string"
526  check_var_with_bool_opt "actual_objects" "a_binary_string"
527  check_var_with_bool_opt "actual_objects" "a_binary_string_array"
528  check_var_with_bool_opt "actual_objects" "a_big_string"
529  check_var_with_bool_opt "actual_objects" "an_array"
530  check_var_with_bool_opt "actual_objects" "an_array_with_repetition"
531  check_var_with_bool_opt "actual_objects" "a_symbol_pointer"
532
533  if { $current_lang == "c++" } {
534    # Nothing changes in all of the C++ tests because deref_refs is not
535    # True.
536    check_var_with_bool_opt "actual_objects" "a_point_t_ref"
537    check_var_with_bool_opt "actual_objects" "a_base_ref"
538
539    with_temp_option "set print object on" "set print object off" {
540      check_var_with_no_opts "a_point_t_ref"
541      check_var_with_bool_opt "actual_objects" "a_point_t_ref"
542
543      check_var_with_no_opts "a_base_ref"
544      check_var_with_bool_opt "actual_objects" "a_base_ref"
545    }
546  }
547}
548
549# Test the static_members option for gdb.Value.format_string.
550proc test_static_members {} {
551  global current_lang
552
553  check_var_with_bool_opt "static_members" "a_point_t"
554  check_var_with_bool_opt "static_members" "a_point_t_pointer"
555  check_var_with_bool_opt "static_members" "another_point"
556  check_var_with_bool_opt "static_members" "a_struct_with_union"
557  check_var_with_bool_opt "static_members" "an_enum"
558  check_var_with_bool_opt "static_members" "a_string"
559  check_var_with_bool_opt "static_members" "a_binary_string"
560  check_var_with_bool_opt "static_members" "a_binary_string_array"
561  check_var_with_bool_opt "static_members" "a_big_string"
562  check_var_with_bool_opt "static_members" "an_array"
563  check_var_with_bool_opt "static_members" "an_array_with_repetition"
564  check_var_with_bool_opt "static_members" "a_symbol_pointer"
565
566  if { $current_lang == "c++" } {
567    # Nothing changes in all of the C++ tests because deref_refs is not
568    # True.
569    check_var_with_bool_opt "static_members" "a_point_t_ref"
570    check_var_with_bool_opt "static_members" "a_base_ref"
571
572    with_temp_option \
573	"set print static-members off" \
574	"set print static-members on" {
575      check_var_with_no_opts "a_point_t_ref"
576      check_var_with_bool_opt "static_members" "a_point_t_ref"
577
578      check_var_with_no_opts "a_base_ref"
579      check_var_with_bool_opt "static_members" "a_base_ref"
580    }
581  }
582}
583
584# Test the max_elements option for gdb.Value.format_string.
585proc test_max_elements {} {
586  global current_lang
587  global default_pointer_regexp
588
589  # 200 is the default maximum number of elements, so setting it should
590  # not change the output.
591  set opts "max_elements=200"
592  with_test_prefix $opts {
593    check_format_string "a_point_t" $opts
594    check_format_string "a_point_t_pointer" $opts
595    check_format_string "another_point" $opts
596    check_format_string "a_struct_with_union" $opts
597    check_format_string "an_enum" $opts
598    check_format_string "a_string" $opts
599    check_format_string "a_binary_string" $opts
600    check_format_string "a_binary_string_array" $opts
601    check_format_string "a_big_string" $opts
602    check_format_string "an_array" $opts
603    check_format_string "an_array_with_repetition" $opts
604    check_format_string "a_symbol_pointer" $opts
605
606    if { $current_lang == "c++" } {
607      check_format_string "a_point_t_ref" $opts
608      check_format_string "a_base_ref" $opts
609    }
610  }
611
612  set opts "max_elements=3"
613  with_test_prefix $opts {
614    check_format_string "a_point_t" $opts
615    check_format_string "a_point_t_pointer" $opts
616    check_format_string "another_point" $opts
617    check_format_string "a_struct_with_union" $opts
618    check_format_string "an_enum" $opts
619    check_format_string "a_string" $opts \
620      "${default_pointer_regexp} \"hel\"..."
621    check_format_string "a_binary_string" $opts \
622      "${default_pointer_regexp} \"hel\"..."
623    # This will print four characters instead of three, see
624    # <https://sourceware.org/bugzilla/show_bug.cgi?id=24331>.
625    check_format_string "a_binary_string_array" $opts \
626      "\"hell\"..."
627    check_format_string "a_big_string" $opts \
628      [get_cut_big_string 3]
629    check_format_string "an_array" $opts
630    check_format_string "an_array_with_repetition" $opts \
631      "\\{1, 3 <repeats 12 times>...\\}"
632    check_format_string "a_symbol_pointer" $opts
633
634    if { $current_lang == "c++" } {
635      check_format_string "a_point_t_ref" $opts
636      check_format_string "a_base_ref" $opts
637    }
638  }
639
640  # Both 1,000 (we don't have that many elements) and 0 (unlimited) should
641  # mean no truncation.
642  foreach opts { "max_elements=1000" "max_elements=0" } {
643    with_test_prefix $opts {
644      check_format_string "a_point_t" $opts
645      check_format_string "a_point_t_pointer" $opts
646      check_format_string "another_point" $opts
647      check_format_string "a_struct_with_union" $opts
648      check_format_string "an_enum" $opts
649      check_format_string "a_string" $opts
650      check_format_string "a_binary_string" $opts
651      check_format_string "a_binary_string_array" $opts
652      check_format_string "a_big_string" $opts \
653        [get_cut_big_string 1000]
654      check_format_string "an_array" $opts
655      check_format_string "an_array_with_repetition" $opts
656      check_format_string "a_symbol_pointer" $opts
657
658      if { $current_lang == "c++" } {
659        check_format_string "a_point_t_ref" $opts
660        check_format_string "a_base_ref" $opts
661      }
662    }
663  }
664
665  with_temp_option "set print elements 4" "set print elements 200" {
666    check_format_string "a_string" "" \
667      "${default_pointer_regexp} \"hell\"..."
668    check_format_string "a_binary_string" "" \
669      "${default_pointer_regexp} \"hell\"..."
670    check_format_string "a_binary_string_array" "" \
671      "\"hell\"..."
672    check_format_string "an_array_with_repetition" "" \
673      "\\{1, 3 <repeats 12 times>...\\}"
674  }
675}
676
677# Test the max_depth option for gdb.Value.format_string.
678proc test_max_depth {} {
679    set opts "max_depth=-1"
680    with_test_prefix $opts {
681	check_format_string "a_struct_with_union" $opts
682    }
683    set opts "max_depth=0"
684    with_test_prefix $opts {
685	check_format_string "a_struct_with_union" $opts "\\{\.\.\.\\}"
686    }
687    set opts "max_depth=1"
688    with_test_prefix $opts {
689	check_format_string "a_struct_with_union" $opts "\\{the_union = \\{\.\.\.\\}\\}"
690    }
691    set opts "max_depth=2"
692    with_test_prefix $opts {
693	check_format_string "a_struct_with_union" $opts
694    }
695}
696
697# Test the repeat_threshold option for gdb.Value.format_string.
698proc test_repeat_threshold {} {
699  global current_lang
700  global default_pointer_regexp
701
702  # 10 is the default threshold for repeated items, so setting it should
703  # not change the output.
704  set opts "repeat_threshold=10"
705  with_test_prefix $opts {
706    check_format_string "a_point_t" $opts
707    check_format_string "a_point_t_pointer" $opts
708    check_format_string "another_point" $opts
709    check_format_string "a_struct_with_union" $opts
710    check_format_string "an_enum" $opts
711    check_format_string "a_string" $opts
712    check_format_string "a_binary_string" $opts
713    check_format_string "a_binary_string_array" $opts
714    check_format_string "a_big_string" $opts
715    check_format_string "an_array" $opts
716    check_format_string "an_array_with_repetition" $opts
717    check_format_string "a_symbol_pointer" $opts
718
719    if { $current_lang == "c++" } {
720      check_format_string "a_point_t_ref" $opts
721      check_format_string "a_base_ref" $opts
722    }
723  }
724
725  set opts "repeat_threshold=1"
726  with_test_prefix $opts {
727    check_format_string "a_point_t" $opts
728    check_format_string "a_point_t_pointer" $opts
729    check_format_string "another_point" $opts
730    check_format_string "a_struct_with_union" $opts
731    check_format_string "an_enum" $opts
732    check_format_string "a_string" $opts \
733      "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o world\""
734    check_format_string "a_binary_string" $opts \
735      "${default_pointer_regexp} \"he\", 'l' <repeats 2 times>, \"o\""
736    check_format_string "a_binary_string_array" $opts \
737      "\"he\", 'l' <repeats 2 times>, \"o\\\\000world\""
738    check_format_string "a_big_string" $opts
739    check_format_string "an_array" $opts
740    check_format_string "an_array_with_repetition" $opts \
741      "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
742
743    check_format_string "a_symbol_pointer" $opts
744
745    if { $current_lang == "c++" } {
746      check_format_string "a_point_t_ref" $opts
747      check_format_string "a_base_ref" $opts
748    }
749  }
750
751  set opts "repeat_threshold=3"
752  with_test_prefix $opts {
753    check_format_string "a_point_t" $opts
754    check_format_string "a_point_t_pointer" $opts
755    check_format_string "another_point" $opts
756    check_format_string "a_struct_with_union" $opts
757    check_format_string "an_enum" $opts
758    check_format_string "a_string" $opts
759    check_format_string "a_binary_string" $opts
760    check_format_string "a_binary_string_array" $opts
761    check_format_string "a_big_string" $opts
762    check_format_string "an_array" $opts
763    check_format_string "an_array_with_repetition" $opts
764    check_format_string "a_symbol_pointer" $opts
765
766    if { $current_lang == "c++" } {
767      check_format_string "a_point_t_ref" $opts
768      check_format_string "a_base_ref" $opts
769    }
770  }
771
772  # Both 100 (we don't have that many repeated elements) and 0 (unlimited)
773  # should mean no truncation.
774  foreach opts { "repeat_threshold=100" "repeat_threshold=0" } {
775    with_test_prefix $opts {
776      check_format_string "a_point_t" $opts
777      check_format_string "a_point_t_pointer" $opts
778      check_format_string "another_point" $opts
779      check_format_string "a_struct_with_union" $opts
780      check_format_string "an_enum" $opts
781      check_format_string "a_string" $opts
782      check_format_string "a_binary_string" $opts
783      check_format_string "a_binary_string_array" $opts
784      check_format_string "a_big_string" $opts
785      check_format_string "an_array" $opts
786      check_format_string "an_array_with_repetition" $opts \
787        "\\{1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5\\}"
788      check_format_string "a_symbol_pointer" $opts
789
790      if { $current_lang == "c++" } {
791        check_format_string "a_point_t_ref" $opts
792        check_format_string "a_base_ref" $opts
793      }
794    }
795  }
796
797  with_temp_option "set print repeats 1" "set print repeats 10" {
798    check_format_string "an_array_with_repetition" "" \
799      "\\{1, 3 <repeats 12 times>, 5 <repeats 3 times>\\}"
800  }
801}
802
803# Test the format option for gdb.Value.format_string.
804proc test_format {} {
805  global current_lang
806  global default_pointer_regexp
807
808  # Hexadecimal.
809  set opts "format='x'"
810  with_test_prefix $opts {
811    gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
812      "0x2a" \
813      "42 with option ${opts}"
814
815    check_format_string "a_point_t" $opts
816    check_format_string "a_point_t_pointer" $opts
817    check_format_string "another_point" $opts
818    check_format_string "a_struct_with_union" $opts \
819      "\\{the_union = \\{an_int = 0x2a2a2a2a, a_char = 0x2a\\}\\}"
820    check_format_string "an_enum" $opts \
821      "0x1"
822    check_format_string "a_string" $opts \
823      $default_pointer_regexp
824    check_format_string "a_binary_string" $opts \
825      $default_pointer_regexp
826    check_format_string "a_binary_string_array" $opts \
827      "\\{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0\\}"
828    check_format_string "a_big_string" $opts \
829      "\\{0x41, 0x42, 0x43, 0x44, 0x45, \[, x0-9a-f\]+\.\.\.\\}"
830    check_format_string "an_array" $opts \
831      "\\{0x2, 0x3, 0x5\\}"
832    check_format_string "an_array_with_repetition" $opts \
833      "\\{0x1, 0x3 <repeats 12 times>, 0x5, 0x5, 0x5\\}"
834    check_format_string "a_symbol_pointer" $opts \
835      $default_pointer_regexp
836
837    if { $current_lang == "c++" } {
838      check_format_string "a_point_t_ref" $opts
839      check_format_string "a_base_ref" $opts
840    }
841  }
842
843  # Decimal.
844  set opts "format='d'"
845  with_test_prefix $opts {
846    set decimal_pointer_regexp "\[0-9\]+"
847    gdb_test "python print (gdb.Value (0x2a).format_string (${opts}))" \
848      "42" \
849      "0x2a with option ${opts}"
850
851    check_format_string "a_point_t" $opts
852    check_format_string "a_point_t_pointer" $opts \
853      $decimal_pointer_regexp
854    check_format_string "another_point" $opts
855    check_format_string "a_struct_with_union" $opts \
856      "\\{the_union = \\{an_int = 707406378, a_char = 42\\}\\}"
857    check_format_string "an_enum" $opts \
858      "1"
859    check_format_string "a_string" $opts \
860      $decimal_pointer_regexp
861    check_format_string "a_binary_string" $opts \
862      $decimal_pointer_regexp
863    check_format_string "a_binary_string_array" $opts \
864      "\\{104, 101, 108, 108, 111, 0, 119, 111, 114, 108, 100, 0\\}"
865    check_format_string "a_big_string" $opts \
866      "\\{65, 66, 67, 68, 69, \[, 0-9\]+\.\.\.\\}"
867    check_format_string "an_array" $opts
868    check_format_string "an_array_with_repetition" $opts
869    check_format_string "a_symbol_pointer" $opts \
870      $decimal_pointer_regexp
871
872    if { $current_lang == "c++" } {
873      check_format_string "a_point_t_ref" $opts
874      check_format_string "a_base_ref" $opts
875    }
876  }
877}
878
879# Test mixing options.
880proc test_mixed {} {
881  global current_lang
882  global default_ref_regexp
883  global default_pointer_regexp
884
885  check_format_string "a_point_t" \
886    "raw=True, format='x'" \
887    "\\{x = 0x2a, y = 0xc\\}"
888
889  check_format_string "an_array" \
890    "array_indexes=True, pretty_arrays=True" \
891    "\\{\[\r\n\]+  \\\[0\\\] = 2,\[\r\n\]+  \\\[1\\\] = 3,\[\r\n\]+  \\\[2\\\] = 5\[\r\n\]+\\}"
892
893  check_format_string "a_struct_with_union" \
894    "pretty_structs=True, unions=False" \
895    "\\{\[\r\n\]+  the_union = \\{\.\.\.\\}\[\r\n\]+\\}"
896
897  check_format_string "a_symbol_pointer" \
898    "symbols=False, format='d'" \
899    "\[0-9\]+"
900
901  if { $current_lang == "c++" } {
902    check_format_string "a_point_t_ref" \
903      "deref_refs=True, actual_objects=True, raw=True" \
904      "${default_ref_regexp}: \\{x = 42, y = 12\\}"
905
906    check_format_string "a_base_ref" \
907      "deref_refs=True, static_members=False" \
908      "${default_ref_regexp}: \\{_vptr\\.Base = ${default_pointer_regexp} <vtable for Deriv\\+16>, a = 42\\}"
909  }
910}
911
912# Test passing invalid arguments to gdb.Value.format_string.
913proc test_invalid_args {} {
914  check_format_string \
915    "a_point_t" \
916    "12" \
917    "TypeError: format_string\\(\\) takes 0 positional arguments but 1 were given.*"
918
919  check_format_string \
920    "a_point_t" \
921    "invalid=True" \
922    "TypeError: 'invalid' is an invalid keyword argument for this function.*"
923
924  check_format_string \
925    "a_point_t" \
926    "raw='hello'" \
927    "TypeError: argument 1 must be bool, not str.*"
928
929  check_format_string \
930    "a_point_t" \
931    "format='xd'" \
932    "ValueError: a single character is required.*"
933}
934
935# Run all the tests in common for both C and C++.
936proc test_all_common {} {
937  # No options.
938  test_no_opts
939  # Single options set to True/False.
940  test_raw
941  test_pretty_arrays
942  test_pretty_structs
943  test_array_indexes
944  test_symbols
945  test_unions
946  test_deref_refs
947  test_actual_objects
948  test_static_members
949  test_max_elements
950  test_max_depth
951  test_repeat_threshold
952  test_format
953  # Multiple options mixed together.
954  test_mixed
955  # Various error conditions.
956  test_invalid_args
957}
958
959# The current language ("c" or "c++" while running tests).
960set current_lang ""
961
962with_test_prefix "format_string" {
963  # Perform C Tests.
964  if { [build_inferior "${binfile}" "c"] == 0 } {
965    with_test_prefix "lang_c" {
966      set current_lang "c"
967      prepare_gdb "${binfile}"
968      test_all_common
969    }
970  }
971
972  # Perform C++ Tests.
973  if { [build_inferior "${binfile}-cxx" "c++"] == 0 } {
974    with_test_prefix "lang_cpp" {
975      set current_lang "c++"
976      prepare_gdb "${binfile}-cxx"
977      test_all_common
978    }
979  }
980}
981