1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2019-2021 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Test the set/show commands framework. The test uses the
19 # "maintenance test-settings set/show xxx" subcommands to exercise
20 # TAB-completion and setting processing.
22 load_lib completion-support.exp
26 if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
32 if { ![readline_is_used] } {
33 untested "no tab completion support without readline"
37 # Test the show command SHOW_CMD. EXPECTED_RE is the expected output.
38 # Also verifies that $_gdb_maint_setting_str produces an equivalent output,
39 # matching it with EXPECTED_RE. EXPECTED_RE double quotes are escaped
40 # unless EXPECTED_RE_ESCAPED is true, indicating the quotes in EXPECTED_RE
41 # are already escaped.
42 # The value for the setting corresponding to SHOW_CMD is then reset
43 # to RESET_VALUE, then set again to the value given by $_gdb_maint_setting_str
44 # and $_gdb_maint_setting. The default value of RESET_VALUE (0) should work for
45 # most settings. Note that we do not check that RESET_VALUE differs from
46 # the expected value, as we assume different values will be verified.
47 # The setting value must still be the one in force before calling show_setting.
48 # In other words, this verifies that
49 # maint set test-settings <some_setting> $_gdb_maint_setting_str(<some_setting>)
50 # maint set test-settings <some_setting> $_gdb_maint_setting(<some_setting>)
51 # do not change the setting value.
52 # This procedure makes it easier to make the test
53 # name/message unique, since we test the "show" commands many times.
54 # EXPECTED_RE is made part of the test name.
55 proc show_setting {show_cmd expected_re {expected_re_escaped 0} {reset_value 0}} {
58 with_test_prefix "$show_cmd $expected_re" {
59 gdb_test "$show_cmd" $expected_re "show"
61 # Remove the first two words (such as "maint show") to have the
62 # setting name to use for $_gdb_maint_setting_str.
63 regsub "\[^ \]+ +\[^ \]+ +\(.*\)" $show_cmd "\\1" maint_setting
64 if {$expected_re_escaped} {
65 set escaped_expected_re $expected_re
67 regsub -all "\"" $expected_re "\\\\\\\"" escaped_expected_re
69 set test "print \$_gdb_maint_setting_str"
70 set setting_str_value "xxxYYYxxx"
71 gdb_test_multiple "print \$_gdb_maint_setting_str(\"$maint_setting\")" $test {
72 -re " = \"\($escaped_expected_re\)\".*$gdb_prompt $" {
73 set setting_str_value $expect_out(1,string)
74 regsub -all "\\\\" $expect_out(1,string) "" setting_str_value
79 # Change the setting value to RESET_VALUE, set it back to setting_str_value
80 # and check we still have the original value.
81 gdb_test_no_output "maintenance set $maint_setting $reset_value" "str reset $reset_value"
82 gdb_test_no_output "maintenance set $maint_setting $setting_str_value" "str set again"
83 gdb_test "$show_cmd" $expected_re "str show after reset+set again"
85 # Same test, but with value captured from $_gdb_maint_setting.
86 set test "print \$_gdb_maint_setting"
87 set setting_value "xxxYYYxxx"
88 gdb_test_multiple "print \$_gdb_maint_setting(\"$maint_setting\")" $test {
89 -re " = \"\(.*\)\".*$gdb_prompt $" {
90 set setting_value $expect_out(1,string)
91 regsub -all "\\\\" $expect_out(1,string) "" setting_value
94 -re " = \(.*\)\r\n$gdb_prompt $" {
95 set setting_value $expect_out(1,string)
100 gdb_test_no_output "maintenance set $maint_setting $reset_value" "reset $reset_value"
101 gdb_test_no_output "maintenance set $maint_setting $setting_value" "set again"
102 gdb_test "$show_cmd" $expected_re "show after reset+set again"
106 # Verifies that $_gdb_setting (SETTING) gives a value whose ptype matches EXPECTED.
107 proc check_type {setting expected} {
108 with_test_prefix "check_type $setting $expected" {
109 gdb_test "print \$_gdb_maint_setting(\"$setting\")"
110 gdb_test "ptype $" "$expected"
112 # Currently, GDB ptype <internal function> always tells it is type int.
113 # ptype should better report an error such as:
114 # "No type information for GDB functions"
115 # Test 'type int', so as to make it fail if ptype is changed.
116 gdb_test "ptype \$_gdb_maint_setting(\"$setting\")" \
121 # var_Xinteger tests. VARIANT determines which command/variant to
123 proc test-integer {variant} {
124 set set_cmd "maint set test-settings $variant"
125 set show_cmd "maint show test-settings $variant"
128 gdb_test "$set_cmd bogus" \
129 "No symbol table is loaded\\. Use the \"file\" command\\."
131 # Seemingly-valid but not quite valid value.
132 gdb_test "$set_cmd 1a" \
133 "Invalid number \"1a\"\\."
135 # Valid value followed by garbage.
136 gdb_test "$set_cmd 1 1" \
137 "A syntax error in expression, near `1'\\."
139 # Valid value followed by garbage.
140 gdb_test "$set_cmd 1 x" \
141 "A syntax error in expression, near `x'\\."
143 if {$variant == "zuinteger-unlimited"} {
144 # -1 means unlimited. Other negative values are rejected. -1
145 # -is tested further below, along the "unlimited" tests.
146 gdb_test "$set_cmd -2" "only -1 is allowed to set as unlimited"
147 check_type "test-settings $variant" "type = int"
148 } elseif {$variant == "uinteger" || $variant == "zuinteger"} {
149 # Negative values are not accepted.
150 gdb_test "$set_cmd -1" "integer -1 out of range"
151 gdb_test "$set_cmd -2" "integer -2 out of range"
152 check_type "test-settings $variant" "type = unsigned int"
154 # Negative values are not accepted.
155 gdb_test_no_output "$set_cmd -1"
156 show_setting "$show_cmd" "-1"
157 gdb_test_no_output "$set_cmd -2"
158 show_setting "$show_cmd" "-2"
159 check_type "test-settings $variant" "type = int"
162 # Regular integer is accepted.
163 gdb_test_no_output "$set_cmd 999"
164 show_setting "$show_cmd" "999"
166 if {$variant == "zinteger" || $variant == "zuinteger"} {
168 gdb_test_no_output "$set_cmd 0"
169 show_setting "$show_cmd" "0"
171 # Either 0 or -1 mean unlimited. Test both the number and
172 # "unlimited". For the latter, test both full name and
175 if {$variant == "zuinteger-unlimited"} {
176 gdb_test_no_output "$set_cmd -1"
178 gdb_test_no_output "$set_cmd 0"
180 show_setting "$show_cmd" "unlimited"
182 foreach_with_prefix value {
193 # Alternate between integer and unlimited, to make sure the
194 # setting really took effect.
195 gdb_test_no_output "$set_cmd 1"
196 show_setting "$show_cmd" "1"
198 gdb_test_no_output "$set_cmd $value"
199 show_setting "$show_cmd" "unlimited"
203 if {$variant == "zuinteger"} {
204 test_gdb_complete_multiple "maint set test-settings " "zuinteger" "" {
206 "zuinteger-unlimited"
209 test_gdb_complete_unique \
214 if {$variant == "zinteger" || $variant == "zuinteger"} {
215 test_gdb_complete_none \
218 test_gdb_complete_unique \
223 # Check junk after "unlimited".
224 gdb_test "$set_cmd unlimitedu" "No symbol table is loaded.*"
226 if {$variant == "zinteger" || $variant == "zuinteger"} {
227 gdb_test "$set_cmd unlimited u" "No symbol table is loaded.*"
228 gdb_test "$set_cmd unlimited 1" "No symbol table is loaded.*"
229 gdb_test "$set_cmd unlimited -1" "No symbol table is loaded.*"
231 gdb_test "$set_cmd unlimited u" "Junk after \"unlimited\": u"
232 gdb_test "$set_cmd unlimited 1" "Junk after \"unlimited\": 1"
233 gdb_test "$set_cmd unlimited -1" "Junk after \"unlimited\": -1"
236 test_gdb_complete_none "$set_cmd unlimited "
237 test_gdb_complete_none "$set_cmd unlimitedu"
238 test_gdb_complete_none "$set_cmd unlimited u"
239 test_gdb_complete_none "$set_cmd unlimited 1"
240 test_gdb_complete_none "$set_cmd x"
241 test_gdb_complete_none "$set_cmd x "
242 test_gdb_complete_none "$set_cmd -1"
243 test_gdb_complete_none "$set_cmd -1 "
244 test_gdb_complete_none "$set_cmd 1 "
246 # Check show command completion.
247 if {$variant == "zuinteger"} {
248 test_gdb_complete_multiple "maintenance show test-settings " "zuinteger" "" {
250 "zuinteger-unlimited"
253 test_gdb_complete_unique \
257 test_gdb_complete_none "$show_cmd "
261 proc_with_prefix test-boolean {} {
262 # Use these variables to make sure we don't call the wrong command
264 set set_cmd "maint set test-settings boolean"
265 set show_cmd "maint show test-settings boolean"
268 gdb_test "$set_cmd bogus" \
269 "\"on\" or \"off\" expected\\."
271 # Seemingly-valid but not quite valid value.
272 gdb_test "$set_cmd on1" \
273 "\"on\" or \"off\" expected\\."
275 # Valid value followed by garbage.
276 gdb_test "$set_cmd on 1" \
277 "\"on\" or \"off\" expected\\."
279 # Unlike auto-bool settings, "-1" is not accepted.
280 gdb_test "$set_cmd -1" \
281 "\"on\" or \"off\" expected\\."
284 gdb_test "$set_cmd auto" \
285 "\"on\" or \"off\" expected\\."
288 gdb_test "$set_cmd o" \
289 "\"on\" or \"off\" expected\\."
291 # Various valid values. Test both full value names and
294 # Note that unlike with auto-bool, empty value implies "on".
295 foreach_with_prefix value {
309 gdb_test_no_output "$set_cmd off"
310 show_setting "$show_cmd" "off"
312 gdb_test_no_output "$set_cmd $value"
313 show_setting "$show_cmd" "on"
316 check_type "test-settings boolean" "type = int"
318 foreach_with_prefix value {
332 gdb_test_no_output "$set_cmd on"
333 show_setting "$show_cmd" "on"
335 gdb_test_no_output "$set_cmd $value"
336 show_setting "$show_cmd" "off"
339 test_gdb_complete_multiple "$set_cmd " "" "o" {
344 test_gdb_complete_unique \
348 test_gdb_complete_none "$set_cmd x"
350 # Check that the show command doesn't complete anything.
351 test_gdb_complete_unique \
354 test_gdb_complete_none "$show_cmd "
357 # auto-boolean tests.
358 proc_with_prefix test-auto-boolean {} {
359 # Use these variables to make sure we don't call the wrong command
361 set set_cmd "maint set test-settings auto-boolean"
362 set show_cmd "maint show test-settings auto-boolean"
365 gdb_test "$set_cmd bogus" \
366 "\"on\", \"off\" or \"auto\" expected\\."
368 # Seemingly-valid but not quite valid value.
369 gdb_test "$set_cmd on1" \
370 "\"on\", \"off\" or \"auto\" expected\\."
372 # Valid value followed by garbage.
373 gdb_test "$set_cmd on 1" \
374 "\"on\", \"off\" or \"auto\" expected\\."
377 gdb_test "$set_cmd o" \
378 "\"on\", \"off\" or \"auto\" expected\\."
380 # Various valid values. Test both full value names and
383 foreach_with_prefix value {
396 gdb_test_no_output "$set_cmd off"
397 show_setting "$show_cmd" "off"
399 gdb_test_no_output "$set_cmd $value"
400 show_setting "$show_cmd" "on"
403 foreach_with_prefix value {
417 gdb_test_no_output "$set_cmd on"
418 show_setting "$show_cmd" "on"
420 gdb_test_no_output "$set_cmd $value"
421 show_setting "$show_cmd" "off"
424 foreach_with_prefix value {
431 gdb_test_no_output "$set_cmd on"
432 show_setting "$show_cmd" "on"
434 gdb_test_no_output "$set_cmd $value"
435 show_setting "$show_cmd" "auto"
438 check_type "test-settings auto-boolean" "type = int"
440 # "-" is not accepted as abbreviation of "-1".
441 gdb_test "$set_cmd -" \
442 "\"on\", \"off\" or \"auto\" expected\\."
444 test_gdb_complete_multiple "$set_cmd " "" "" {
450 test_gdb_complete_unique \
454 test_gdb_complete_none "$set_cmd x"
456 # Check that the show command doesn't complete anything.
457 test_gdb_complete_unique \
460 test_gdb_complete_none "$show_cmd "
464 proc_with_prefix test-enum {} {
465 # Use these variables to make sure we don't call the wrong command
467 set set_cmd "maint set test-settings enum"
468 set show_cmd "maint show test-settings enum"
471 gdb_test "$set_cmd" \
472 "Requires an argument\\. Valid arguments are xxx, yyy, zzz\\."
475 gdb_test "$set_cmd bogus" \
476 "Undefined item: \"bogus\"."
478 # Seemingly-valid but not quite valid value.
479 gdb_test "$set_cmd xxx1" \
480 "Undefined item: \"xxx1\"."
482 # Valid value followed by garbage.
483 gdb_test "$set_cmd xxx 1" \
484 "Junk after item \"xxx\": 1"
485 # Valid value followed by garbage, with extra spaces.
486 gdb_test "$set_cmd xxx 1" \
487 "Junk after item \"xxx\": 1"
488 # Abbreviated value followed by garbage.
489 gdb_test "$set_cmd xx 1" \
490 "Junk after item \"xx\": 1"
492 # Various valid values. Test both full value names and
494 gdb_test_no_output "$set_cmd x"
495 show_setting "$show_cmd" "xxx" 0 "zzz"
496 gdb_test_no_output "$set_cmd yy"
497 show_setting "$show_cmd" "yyy" 0 "zzz"
498 gdb_test_no_output "$set_cmd zzz"
499 show_setting "$show_cmd" "zzz" 0 "yyy"
501 check_type "test-settings enum" "type = char \\\[3\\\]"
503 test_gdb_complete_multiple "$set_cmd " "" "" {
509 test_gdb_complete_unique \
513 test_gdb_complete_none "$set_cmd a"
515 # Check that the show command doesn't complete anything.
516 test_gdb_complete_unique \
519 test_gdb_complete_none "$show_cmd "
522 # string settings tests.
523 proc test-string {variant} {
525 global srcfile binfile
527 # Load symbols for the completion test below.
528 clean_restart $binfile
530 # Use these variables to make sure we don't call the wrong command
532 set set_cmd "maint set test-settings $variant"
533 set show_cmd "maint show test-settings $variant"
535 # Checks that gdb doesn't crash if we haven't set the string yet.
536 if {$variant != "filename"} {
537 gdb_test "$show_cmd" "^$show_cmd\r\n" "$show_cmd: show default"
539 gdb_test "$show_cmd" "/foo/bar" "$show_cmd: show default"
543 gdb_test_no_output "$set_cmd hello world"
544 show_setting "$show_cmd" "hello world"
546 check_type "test-settings $variant" "type = char \\\[\[1-9\]\[0-9\]*\\\]"
548 # A quoted string value.
549 if {$variant == "string"} {
550 gdb_test_no_output "$set_cmd \"hello world\""
551 show_setting "$show_cmd" "\\\\\"hello world\\\\\"" 1
553 gdb_test_no_output "$set_cmd \"hello world\""
554 show_setting "$show_cmd" "\"hello world\""
557 # Test clearing the string.
558 with_test_prefix "clear string" {
559 if {$variant == "filename"} {
560 gdb_test "$set_cmd" \
561 "Argument required \\(filename to set it to\\.\\)\\."
563 # Check the value didn't change.
564 show_setting "$show_cmd" "\"hello world\""
566 gdb_test_no_output "$set_cmd"
567 gdb_test "$show_cmd" \
568 "^$show_cmd\r\n" "$show_cmd: empty second time"
573 if {$variant == "string" || $variant == "string-noescape" } {
574 # Make sure GDB doesn't try to complete on symbols, which
575 # doesn't make any sense.
576 test_gdb_complete_none "$set_cmd "
578 # Complete on filename.
580 # See comments in gdb.base/completion.exp.
582 # We `cd' to ${srcdir}, and then do the completion relative to
583 # the current directory.
585 # ${srcdir} may be a relative path. We want to make sure we
586 # end up in the right directory - so make sure we know where
594 gdb_test "cd ${fullsrcdir}" \
595 "Working directory [string_to_regexp ${fullsrcdir}].*" \
598 set unique_file ../testsuite/gdb.base/comp-dir/subdir/dummy
600 test_gdb_complete_unique \
601 "$set_cmd ${unique_file}" \
602 "$set_cmd ${unique_file}.txt"
604 test_gdb_complete_none "$set_cmd ${unique_file}.abc"
607 # Check show command completion.
608 if {$variant == "string"} {
609 test_gdb_complete_multiple "maint show test-settings " "string" "" {
614 test_gdb_complete_unique \
618 test_gdb_complete_none "$show_cmd "
628 with_test_prefix "test-integer $variant" {
629 test-integer $variant
643 with_test_prefix "test-string $variant" {