settings.exp revision 1.1.1.3 1 # This testcase is part of GDB, the GNU debugger.
2
3 # Copyright 2019-2024 Free Software Foundation, Inc.
4
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.
9 #
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.
14 #
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/>.
17
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.
21
22 load_lib completion-support.exp
23
24 standard_testfile .c
25
26 if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
27 return -1
28 }
29
30 clean_restart
31
32 if { ![readline_is_used] } {
33 untested "no tab completion support without readline"
34 return -1
35 }
36
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}} {
56 global gdb_prompt
57
58 with_test_prefix "$show_cmd $expected_re" {
59 gdb_test "$show_cmd" $expected_re "show"
60
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
66 } else {
67 regsub -all "\"" $expected_re "\\\\\\\"" escaped_expected_re
68 }
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
75 pass $test
76 }
77 }
78
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"
84
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
92 pass $test
93 }
94 -re " = \(.*\)\r\n$gdb_prompt $" {
95 set setting_value $expect_out(1,string)
96 pass $test
97 }
98 }
99
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"
103 }
104 }
105
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"
111
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\")" \
117 "type = int"
118 }
119 }
120
121 # var_Xinteger tests. VARIANT determines which command/variant to
122 # exercise.
123 proc test-integer {variant} {
124 set set_cmd "maint set test-settings $variant"
125 set show_cmd "maint show test-settings $variant"
126
127 # A bogus value.
128 gdb_test "$set_cmd bogus" \
129 "No symbol table is loaded\\. Use the \"file\" command\\."
130
131 # Seemingly-valid but not quite valid value.
132 gdb_test "$set_cmd 1a" \
133 "Invalid number \"1a\"\\."
134
135 # Valid value followed by garbage.
136 gdb_test "$set_cmd 1 1" \
137 "A syntax error in expression, near `1'\\."
138
139 # Valid value followed by garbage.
140 gdb_test "$set_cmd 1 x" \
141 "A syntax error in expression, near `x'\\."
142
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" "integer -2 out of range"
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"
153 } else {
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"
160 }
161
162 # Regular integer is accepted.
163 gdb_test_no_output "$set_cmd 999"
164 show_setting "$show_cmd" "999"
165
166 if {$variant == "zinteger" || $variant == "zuinteger"} {
167 # 0 means 0.
168 gdb_test_no_output "$set_cmd 0"
169 show_setting "$show_cmd" "0"
170 } else {
171 # Either 0 or -1 mean unlimited. Test both the number and
172 # "unlimited". For the latter, test both full name and
173 # abbreviations.
174
175 if {$variant == "zuinteger-unlimited"} {
176 gdb_test_no_output "$set_cmd -1"
177 } else {
178 gdb_test_no_output "$set_cmd 0"
179 }
180 show_setting "$show_cmd" "unlimited"
181
182 foreach_with_prefix value {
183 "u"
184 "un"
185 "unl"
186 "unli"
187 "unlim"
188 "unlimi"
189 "unlimit"
190 "unlimite"
191 "unlimited"
192 } {
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"
197
198 gdb_test_no_output "$set_cmd $value"
199 show_setting "$show_cmd" "unlimited"
200 }
201 }
202
203 if {$variant == "zuinteger"} {
204 test_gdb_complete_multiple "maint set test-settings " "zuinteger" "" {
205 "zuinteger"
206 "zuinteger-unlimited"
207 }
208 } else {
209 test_gdb_complete_unique \
210 "$set_cmd" \
211 "$set_cmd"
212 }
213
214 if {$variant == "zinteger" || $variant == "zuinteger"} {
215 test_gdb_complete_none \
216 "$set_cmd "
217 } else {
218 test_gdb_complete_multiple "$set_cmd " "" "" {
219 "NUMBER"
220 "unlimited"
221 }
222 test_gdb_complete_none \
223 "$set_cmd 1"
224 test_gdb_complete_unique \
225 "$set_cmd u" \
226 "$set_cmd unlimited"
227 }
228
229 # Check junk after "unlimited".
230 gdb_test "$set_cmd unlimitedu" "No symbol table is loaded.*"
231
232 if {$variant == "zinteger" || $variant == "zuinteger"} {
233 gdb_test "$set_cmd unlimited u" "No symbol table is loaded.*"
234 gdb_test "$set_cmd unlimited 1" "No symbol table is loaded.*"
235 gdb_test "$set_cmd unlimited -1" "No symbol table is loaded.*"
236 } else {
237 gdb_test "$set_cmd unlimited u" "Junk after \"unlimited\": u"
238 gdb_test "$set_cmd unlimited 1" "Junk after \"unlimited\": 1"
239 gdb_test "$set_cmd unlimited -1" "Junk after \"unlimited\": -1"
240 }
241
242 test_gdb_complete_none "$set_cmd unlimited "
243 test_gdb_complete_none "$set_cmd unlimitedu"
244 test_gdb_complete_none "$set_cmd unlimited u"
245 test_gdb_complete_none "$set_cmd unlimited 1"
246 test_gdb_complete_none "$set_cmd x"
247 test_gdb_complete_none "$set_cmd x "
248 test_gdb_complete_none "$set_cmd -1"
249 test_gdb_complete_none "$set_cmd -1 "
250 test_gdb_complete_none "$set_cmd 1 "
251
252 # Check show command completion.
253 if {$variant == "zuinteger"} {
254 test_gdb_complete_multiple "maintenance show test-settings " "zuinteger" "" {
255 "zuinteger"
256 "zuinteger-unlimited"
257 }
258 } else {
259 test_gdb_complete_unique \
260 "$show_cmd" \
261 "$show_cmd"
262 }
263 test_gdb_complete_none "$show_cmd "
264 }
265
266 # boolean tests.
267 proc_with_prefix test-boolean {} {
268 # Use these variables to make sure we don't call the wrong command
269 # by mistake.
270 set set_cmd "maint set test-settings boolean"
271 set show_cmd "maint show test-settings boolean"
272
273 # A bogus value.
274 gdb_test "$set_cmd bogus" \
275 "\"on\" or \"off\" expected\\."
276
277 # Seemingly-valid but not quite valid value.
278 gdb_test "$set_cmd on1" \
279 "\"on\" or \"off\" expected\\."
280
281 # Valid value followed by garbage.
282 gdb_test "$set_cmd on 1" \
283 "\"on\" or \"off\" expected\\."
284
285 # Unlike auto-bool settings, "-1" is not accepted.
286 gdb_test "$set_cmd -1" \
287 "\"on\" or \"off\" expected\\."
288
289 # Nor "auto".
290 gdb_test "$set_cmd auto" \
291 "\"on\" or \"off\" expected\\."
292
293 # "o" is ambiguous.
294 gdb_test "$set_cmd o" \
295 "\"on\" or \"off\" expected\\."
296
297 # Various valid values. Test both full value names and
298 # abbreviations.
299
300 # Note that unlike with auto-bool, empty value implies "on".
301 foreach_with_prefix value {
302 ""
303 "on"
304 "1"
305 "y"
306 "ye"
307 "yes"
308 "e"
309 "en"
310 "ena"
311 "enab"
312 "enabl"
313 "enable"
314 } {
315 gdb_test_no_output "$set_cmd off"
316 show_setting "$show_cmd" "off"
317
318 gdb_test_no_output "$set_cmd $value"
319 show_setting "$show_cmd" "on"
320 }
321
322 check_type "test-settings boolean" "type = int"
323
324 foreach_with_prefix value {
325 "of"
326 "off"
327 "0"
328 "n"
329 "no"
330 "d"
331 "di"
332 "dis"
333 "disa"
334 "disab"
335 "disabl"
336 "disable"
337 } {
338 gdb_test_no_output "$set_cmd on"
339 show_setting "$show_cmd" "on"
340
341 gdb_test_no_output "$set_cmd $value"
342 show_setting "$show_cmd" "off"
343 }
344
345 test_gdb_complete_multiple "$set_cmd " "" "o" {
346 "off"
347 "on"
348 }
349
350 test_gdb_complete_unique \
351 "$set_cmd of" \
352 "$set_cmd off"
353
354 test_gdb_complete_none "$set_cmd x"
355
356 # Check that the show command doesn't complete anything.
357 test_gdb_complete_unique \
358 "$show_cmd" \
359 "$show_cmd"
360 test_gdb_complete_none "$show_cmd "
361 }
362
363 # auto-boolean tests.
364 proc_with_prefix test-auto-boolean {} {
365 # Use these variables to make sure we don't call the wrong command
366 # by mistake.
367 set set_cmd "maint set test-settings auto-boolean"
368 set show_cmd "maint show test-settings auto-boolean"
369
370 # A bogus value.
371 gdb_test "$set_cmd bogus" \
372 "\"on\", \"off\" or \"auto\" expected\\."
373
374 # Seemingly-valid but not quite valid value.
375 gdb_test "$set_cmd on1" \
376 "\"on\", \"off\" or \"auto\" expected\\."
377
378 # Valid value followed by garbage.
379 gdb_test "$set_cmd on 1" \
380 "\"on\", \"off\" or \"auto\" expected\\."
381
382 # "o" is ambiguous.
383 gdb_test "$set_cmd o" \
384 "\"on\", \"off\" or \"auto\" expected\\."
385
386 # Various valid values. Test both full value names and
387 # abbreviations.
388
389 foreach_with_prefix value {
390 "on"
391 "1"
392 "y"
393 "ye"
394 "yes"
395 "e"
396 "en"
397 "ena"
398 "enab"
399 "enabl"
400 "enable"
401 } {
402 gdb_test_no_output "$set_cmd off"
403 show_setting "$show_cmd" "off"
404
405 gdb_test_no_output "$set_cmd $value"
406 show_setting "$show_cmd" "on"
407 }
408
409 foreach_with_prefix value {
410 "of"
411 "off"
412 "0"
413 "n"
414 "no"
415 "d"
416 "di"
417 "dis"
418 "disa"
419 "disab"
420 "disabl"
421 "disable"
422 } {
423 gdb_test_no_output "$set_cmd on"
424 show_setting "$show_cmd" "on"
425
426 gdb_test_no_output "$set_cmd $value"
427 show_setting "$show_cmd" "off"
428 }
429
430 foreach_with_prefix value {
431 "a"
432 "au"
433 "aut"
434 "auto"
435 "-1"
436 } {
437 gdb_test_no_output "$set_cmd on"
438 show_setting "$show_cmd" "on"
439
440 gdb_test_no_output "$set_cmd $value"
441 show_setting "$show_cmd" "auto"
442 }
443
444 check_type "test-settings auto-boolean" "type = int"
445
446 # "-" is not accepted as abbreviation of "-1".
447 gdb_test "$set_cmd -" \
448 "\"on\", \"off\" or \"auto\" expected\\."
449
450 test_gdb_complete_multiple "$set_cmd " "" "" {
451 "auto"
452 "off"
453 "on"
454 }
455
456 test_gdb_complete_unique \
457 "$set_cmd of" \
458 "$set_cmd off"
459
460 test_gdb_complete_none "$set_cmd x"
461
462 # Check that the show command doesn't complete anything.
463 test_gdb_complete_unique \
464 "$show_cmd" \
465 "$show_cmd"
466 test_gdb_complete_none "$show_cmd "
467 }
468
469 # Enum option tests.
470 proc_with_prefix test-enum {} {
471 # Use these variables to make sure we don't call the wrong command
472 # by mistake.
473 set set_cmd "maint set test-settings enum"
474 set show_cmd "maint show test-settings enum"
475
476 # Missing value.
477 gdb_test "$set_cmd" \
478 "Requires an argument\\. Valid arguments are xxx, yyy, zzz\\."
479
480 # A bogus value.
481 gdb_test "$set_cmd bogus" \
482 "Undefined item: \"bogus\"."
483
484 # Seemingly-valid but not quite valid value.
485 gdb_test "$set_cmd xxx1" \
486 "Undefined item: \"xxx1\"."
487
488 # Valid value followed by garbage.
489 gdb_test "$set_cmd xxx 1" \
490 "Junk after item \"xxx\": 1"
491 # Valid value followed by garbage, with extra spaces.
492 gdb_test "$set_cmd xxx 1" \
493 "Junk after item \"xxx\": 1"
494 # Abbreviated value followed by garbage.
495 gdb_test "$set_cmd xx 1" \
496 "Junk after item \"xx\": 1"
497
498 # Various valid values. Test both full value names and
499 # abbreviations.
500 gdb_test_no_output "$set_cmd x"
501 show_setting "$show_cmd" "xxx" 0 "zzz"
502 gdb_test_no_output "$set_cmd yy"
503 show_setting "$show_cmd" "yyy" 0 "zzz"
504 gdb_test_no_output "$set_cmd zzz"
505 show_setting "$show_cmd" "zzz" 0 "yyy"
506
507 check_type "test-settings enum" "type = char \\\[4\\\]"
508
509 test_gdb_complete_multiple "$set_cmd " "" "" {
510 "xxx"
511 "yyy"
512 "zzz"
513 }
514
515 test_gdb_complete_unique \
516 "$set_cmd x" \
517 "$set_cmd xxx"
518
519 test_gdb_complete_none "$set_cmd a"
520
521 # Check that the show command doesn't complete anything.
522 test_gdb_complete_unique \
523 "$show_cmd" \
524 "$show_cmd"
525 test_gdb_complete_none "$show_cmd "
526 }
527
528 # string settings tests.
529 proc test-string {variant} {
530 global gdb_prompt
531 global srcfile binfile
532
533 # Load symbols for the completion test below.
534 clean_restart $binfile
535
536 # Use these variables to make sure we don't call the wrong command
537 # by mistake.
538 set set_cmd "maint set test-settings $variant"
539 set show_cmd "maint show test-settings $variant"
540
541 # Checks that gdb doesn't crash if we haven't set the string yet.
542 if {$variant != "filename"} {
543 # This odd expected output here is because we expect GDB to
544 # emit a single blank line as a result of this command.
545 gdb_test -nonl "$show_cmd" "^\r\n" "$show_cmd: show default"
546 } else {
547 gdb_test "$show_cmd" "/foo/bar" "$show_cmd: show default"
548 }
549
550 # A string value.
551 gdb_test_no_output "$set_cmd hello world"
552 show_setting "$show_cmd" "hello world"
553
554 check_type "test-settings $variant" "type = char \\\[\[1-9\]\[0-9\]*\\\]"
555
556 # A quoted string value.
557 if {$variant == "string"} {
558 gdb_test_no_output "$set_cmd \"hello world\""
559 show_setting "$show_cmd" "\\\\\"hello world\\\\\"" 1
560 } else {
561 gdb_test_no_output "$set_cmd \"hello world\""
562 show_setting "$show_cmd" "\"hello world\""
563 }
564
565 # Test clearing the string.
566 with_test_prefix "clear string" {
567 if {$variant == "filename"} {
568 gdb_test "$set_cmd" \
569 "Argument required \\(filename to set it to\\.\\)\\."
570
571 # Check the value didn't change.
572 show_setting "$show_cmd" "\"hello world\""
573 } else {
574 gdb_test_no_output "$set_cmd"
575 # This odd expected output here is because we expect GDB to
576 # emit a single blank line as a result of this command.
577 gdb_test -nonl "$show_cmd" "^\r\n" "$show_cmd: empty second time"
578 }
579 }
580
581 # Test completion.
582 if {$variant == "string" || $variant == "string-noescape" } {
583 # Make sure GDB doesn't try to complete on symbols, which
584 # doesn't make any sense.
585 test_gdb_complete_none "$set_cmd "
586 } else {
587 # Complete on filename.
588
589 # See comments in gdb.base/completion.exp.
590
591 # We `cd' to ${srcdir}, and then do the completion relative to
592 # the current directory.
593
594 # ${srcdir} may be a relative path. We want to make sure we
595 # end up in the right directory - so make sure we know where
596 # it is.
597 with_cwd $::srcdir {
598 set fullsrcdir [pwd]
599 }
600
601 gdb_test "cd ${fullsrcdir}" \
602 "Working directory [string_to_regexp ${fullsrcdir}].*" \
603 "cd to \${srcdir}"
604
605 set unique_file ../testsuite/gdb.base/comp-dir/subdir/dummy
606
607 test_gdb_complete_unique \
608 "$set_cmd ${unique_file}" \
609 "$set_cmd ${unique_file}.txt"
610
611 test_gdb_complete_none "$set_cmd ${unique_file}.abc"
612 }
613
614 # Check show command completion.
615 if {$variant == "string"} {
616 test_gdb_complete_multiple "maint show test-settings " "string" "" {
617 "string"
618 "string-noescape"
619 }
620 } else {
621 test_gdb_complete_unique \
622 "$show_cmd" \
623 "$show_cmd"
624 }
625 test_gdb_complete_none "$show_cmd "
626 }
627
628 # Check that $_gdb_setting & co report the correct error strings.
629 proc test-setting-error {} {
630 gdb_test {print $_gdb_setting("xxx")} \
631 "First argument of \\\$_gdb_setting must be a valid setting of the 'show' command\\."
632 gdb_test {print $_gdb_setting_str("xxx")} \
633 "First argument of \\\$_gdb_setting_str must be a valid setting of the 'show' command\\."
634
635 gdb_test {print $_gdb_maint_setting("xxx")} \
636 "First argument of \\\$_gdb_maint_setting must be a valid setting of the 'maintenance show' command\\."
637 gdb_test {print $_gdb_maint_setting_str("xxx")} \
638 "First argument of \\\$_gdb_maint_setting_str must be a valid setting of the 'maintenance show' command\\."
639 }
640
641 foreach variant {
642 uinteger
643 integer
644 zinteger
645 zuinteger
646 zuinteger-unlimited
647 } {
648 with_test_prefix "test-integer $variant" {
649 test-integer $variant
650 }
651 }
652
653 test-boolean
654 test-auto-boolean
655 test-enum
656
657 foreach variant {
658 string
659 string-noescape
660 optional-filename
661 filename
662 } {
663 with_test_prefix "test-string $variant" {
664 test-string $variant
665 }
666 }
667
668 test-setting-error
669