1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2019-2024 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 "with" command.
20 load_lib completion-support.exp
24 if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
28 clean_restart $binfile
30 # Test "maint with". VALUES is a list of values. A nested "with" is
31 # performed with each combination of pair of values from this list.
32 # This exercises setting a value, and restoring it too. This is
33 # particularly important for the "special" values like "unlimited",
34 # which for example for var_uinteger maps to 0 at the user-visible
35 # level, but maps to -1 internally.
37 proc test_with {setting values} {
38 foreach val1 $values {
39 foreach val2 $values {
41 "maint with test-settings $setting $val1 -- maint with test-settings $setting $val2 -- p 1" \
47 # Test "maint with" in the error case. SETTING is the "maint set
48 # test-setting" setting to exercise. TMP_VAL is the value to set the
49 # setting to. EXPECTED_RE is the expected GDB output, which should be
50 # an error of some kind. Also checks that the setting's original
51 # value is preserved across the error.
53 proc test_with_error {setting tmp_val expected_re} {
56 with_test_prefix "$setting, $tmp_val" {
58 gdb_test_multiple "maint show test-settings $setting" \
60 -re "^maint show test-settings $setting\r\n" {
63 -re "^(.*)\r\n$gdb_prompt $" {
64 set org_val $expect_out(1,string)
70 "maint with test-settings $setting $tmp_val -- p 1" \
73 gdb_test "maint show test-settings $setting" "^$org_val" \
74 "value hasn't changed across error"
78 # Test "with" framework basics, using the internal "maint with
79 # test-settings" subcommands.
80 with_test_prefix "maint" {
81 test_with "auto-boolean" {"on" "off" "auto"}
82 test_with "boolean" {"" "on" "off" "0" "1" "enable" "disable"}
83 test_with "integer" {"0" "1" "-1" "unlimited"}
84 test_with "uinteger" {"0" "1" "unlimited"}
85 test_with "zinteger" {"0" "1" "-1"}
86 test_with "zuinteger" {"0" "1"}
87 test_with "zuinteger-unlimited" {"-1" "unlimited" "0" "1"}
88 test_with "string" {"" "foo" "\"hello world\""}
89 test_with "string-noescape" {"" "foo" "\"hello world\""}
90 test_with "filename" {"/foo" "bar/x/y"}
91 test_with "optional-filename" {"" "/foo" "bar/x/y"}
92 test_with "enum" {"xxx" "yyy"}
94 # Check the most important error conditions. E.g., empty,
95 # negative or "unlimited" values for settings that don't accept
96 # those. Exhaustive error coverage of the set/with value parsing
97 # is left to "set" testing, in gdb.base/settings.exp.
98 test_with_error "auto-boolean" "" \
99 "\"on\", \"off\" or \"auto\" expected\\."
100 test_with_error "auto-boolean" "xxx" \
101 "\"on\", \"off\" or \"auto\" expected\\."
102 test_with_error "boolean" "2" "\"on\" or \"off\" expected\\."
103 test_with_error "uinteger" "-1" "integer -1 out of range"
104 test_with_error "uinteger" "" \
105 "Argument required \\(integer to set it to, or \"unlimited\"\\)\\."
106 test_with_error "zuinteger" "-1" "integer -1 out of range"
107 test_with_error "zuinteger" "" \
108 "Argument required \\(integer to set it to\\)\\."
109 test_with_error "zuinteger-unlimited" "-2" \
110 "integer -2 out of range"
111 test_with_error "zuinteger-unlimited" "" \
112 "Argument required \\(integer to set it to, or \"unlimited\"\\)\\."
113 test_with_error "filename" "" \
114 "Argument required \\(filename to set it to\\.\\)\\."
115 test_with_error "enum" "" \
116 "Requires an argument\\. Valid arguments are xxx, yyy, zzz\\."
119 # Basic/core tests using user-visible commands.
120 with_test_prefix "basics" {
121 gdb_test "print g_s" " = {a = 1, b = 2, c = 3}"
122 gdb_test "with print pretty -- print g_s" \
131 gdb_test "with non-stop on -- show non-stop" \
132 "Controlling the inferior in non-stop mode is on\\."
133 gdb_test "show non-stop" \
134 "Controlling the inferior in non-stop mode is off\\."
137 gdb_test "with language pascal -- show language" \
138 "The current source language is \"pascal\"\\."
140 gdb_test "show language" \
141 "The current source language is \"auto; currently c\"\\."
143 gdb_test "with language ada -- print g_s" \
144 " = \\(a => 1, b => 2, c => 3\\)"
147 gdb_test "with language ada -- with language c -- print g_s" \
148 " = {a = 1, b = 2, c = 3}"
151 gdb_test "w language pascal -- show language" \
152 "The current source language is \"pascal\"\\." \
155 # An early prototype of the "with" command got this wrong.
157 "w print repeats unlimited -- w print repeats 1 -- p \"1223334444\"" \
158 " = \"1\", '2' <repeats 2 times>, '3' <repeats 3 times>, '4' <repeats 4 times>"
161 # Check a user-defined command.
162 with_test_prefix "user-defined" {
163 # A user defined command.
164 set test "define usercmd"
165 gdb_test_multiple "define usercmd" $test {
175 gdb_test "with language ada -- usercmd" \
176 " = \\(a => 1, b => 2, c => 3\\)"
180 with_test_prefix "repeat" {
181 clean_restart $binfile
183 # "with" with no command reinvokes the previous command.
184 gdb_test "with language ada" \
185 "No previous command to relaunch" \
186 "reinvoke with no previous command to relaunch"
188 gdb_test "print g_s" " = {a = 1, b = 2, c = 3}"
190 gdb_test "with language ada" \
191 " = \\(a => 1, b => 2, c => 3\\)" \
192 "reinvoke with language"
194 # Same, but with "--".
195 gdb_test "with language fortran --" \
196 " = \\( a = 1, b = 2, c = 3 \\)" \
197 "reinvoke with language and --"
199 # Repeating repeats the original "print g_s", not the last "with"
201 set test "repeat command line"
203 gdb_test_multiple "" $test {
204 -re " = {a = 1, b = 2, c = 3}\r\n$gdb_prompt $" {
211 with_test_prefix "run control" {
212 clean_restart $binfile
218 # Check "with" with a synchronous execution command.
219 gdb_test "with disassemble-next-line on -- next" \
224 with_test_prefix "errors" {
225 gdb_test "with" "Missing arguments\\."
227 # Try both an unknown root setting and an unknown prefixed
228 # setting. The errors come from different locations in the
230 gdb_test "with xxxx yyyy" \
231 "Undefined set command: \"xxxx\". Try \"help set\"\\."
232 gdb_test "with print xxxx yyyy" \
233 "Undefined set print command: \"xxxx yyyy\". Try \"help set print\"\\."
234 # Try one error case for "maint with", to make sure the right
235 # "maintenance with" prefix is shown.
236 gdb_test "maint with xxxx yyyy" \
237 "Undefined maintenance set command: \"xxxx\". Try \"help maintenance set\"\\."
239 # Try ambiguous settings.
241 "Ambiguous set command \"w\": warn-language-frame-mismatch, watchdog, width, write\\."
242 gdb_test "with print m" \
243 "Ambiguous set print command \"m\": max-depth, max-symbolic-offset, memory-tag-violations\\."
245 gdb_test "with variable xxx=1" \
246 "Cannot use this setting with the \"with\" command"
248 gdb_test "with print elements -- p 1" \
249 "Argument required \\(integer to set it to, or \"unlimited\"\\)\\."
251 gdb_test "with -- p 1" \
252 "Missing setting before '--' delimiter"
254 # Check that the setting is restored even if the command throws.
255 gdb_test "with print elements 1 -- unknowncommand" \
256 "Undefined command: \"unknowncommand\"\\. Try \"help\"\\."
257 gdb_test "show print elements" \
258 "Limit on string chars or array elements to print is 200\\."
262 with_test_prefix "completion" {
263 test_gdb_complete_unique \
267 test_gdb_complete_unique \
269 "with print elements"
271 test_gdb_complete_unique \
272 "with print elements u" \
273 "with print elements unlimited"
275 test_gdb_complete_none \
276 "with print elements unlimited "
278 test_gdb_completion_offers_commands "with print elements unlimited -- "
280 # Check that the completer nests into the nested command line's
282 test_gdb_complete_unique \
283 "with print elements unlimited -- with print ele" \
284 "with print elements unlimited -- with print elements"
286 # Check completion of "maint with". "maint with" and "with"'s
287 # completers share 99% of the code. All we need to care about
288 # here is that the completion word point is computed correctly, so
289 # any simple completion is sufficient.
290 test_gdb_complete_unique \
291 "maint with test-set" \
292 "maint with test-settings"