1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2019 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 gdb::option framework.
20 # The test uses the "maintenance test-options" subcommands to exercise
21 # TAB-completion and option processing.
23 # It also tests option integration in various commands, including:
32 load_lib completion-support.exp
36 if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
42 if { ![readline_is_used] } {
43 untested "no tab completion support without readline"
47 # Check the completion result, as returned by the "maintenance show
48 # test-options-completion-result" command. TEST is used as test name.
49 proc check_completion_result {expected test} {
50 gdb_test "maintenance show test-options-completion-result" \
52 "$test: res=$expected"
55 # Like test_gdb_complete_unique, but the expected output is expected
56 # to be the input line. I.e., the line is already complete. We're
57 # just checking whether GDB recognizes the option and auto-appends a
59 proc test_completer_recognizes {res input_line} {
60 set expected_re [string_to_regexp $input_line]
61 test_gdb_complete_unique $input_line $expected_re
62 check_completion_result $res $input_line
65 # Wrapper around test_gdb_complete_multiple that also checks the
66 # completion result is RES.
67 proc res_test_gdb_complete_multiple {res cmd_prefix completion_word args} {
68 test_gdb_complete_multiple $cmd_prefix $completion_word {*}$args
69 check_completion_result $res "$cmd_prefix$completion_word"
72 # Wrapper around test_gdb_complete_none that also checks the
73 # completion result is RES.
74 proc res_test_gdb_complete_none { res input_line } {
75 test_gdb_complete_none $input_line
76 check_completion_result $res "$input_line"
79 # Wrapper around test_gdb_complete_unique that also checks the
80 # completion result is RES.
81 proc res_test_gdb_complete_unique { res input_line args} {
82 test_gdb_complete_unique $input_line {*}$args
83 check_completion_result $res "$input_line"
86 # Make a full command name from VARIANT. VARIANT is either
87 # "require-delimiter", "unknown-is-error" or "unknown-is-operand".
88 proc make_cmd {variant} {
89 return "maint test-options $variant"
92 # Return a string for the expected result of running "maint
93 # test-options xxx", with no flag/option set. OPERAND is the expected
95 proc expect_none {operand} {
96 return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -- $operand"
99 # Return a string for the expected result of running "maint
100 # test-options xxx", with -flag set. OPERAND is the expected operand.
101 proc expect_flag {operand} {
102 return "-flag 1 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -- $operand"
105 # Return a string for the expected result of running "maint
106 # test-options xxx", with -bool set. OPERAND is the expected operand.
107 proc expect_bool {operand} {
108 return "-flag 0 -xx1 0 -xx2 0 -bool 1 -enum xxx -uint 0 -zuint-unl 0 -- $operand"
111 # Return a string for the expected result of running "maint
112 # test-options xxx", with one of the integer options set to $VAL.
113 # OPTION determines which option to expect set. OPERAND is the
115 proc expect_integer {option val operand} {
116 if {$option == "uinteger"} {
117 return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint $val -zuint-unl 0 -- $operand"
118 } elseif {$option == "zuinteger-unlimited"} {
119 return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl $val -- $operand"
121 error "unsupported option: $option"
132 "-zuinteger-unlimited"
135 # Basic option-machinery + "print" command integration tests.
136 proc_with_prefix test-print {{prefix ""}} {
139 # Completing "print" with no argument completes on symbols only,
140 # no options are offered. Since we haven't loaded any symbols,
141 # the match list should be empty.
142 test_gdb_complete_none "${prefix}print "
144 # OTOH, completing at "-" should list all options.
145 test_gdb_complete_multiple "${prefix}print " "-" "" {
162 clean_restart $binfile
165 fail "cannot run to main"
169 # Mix options and format.
170 gdb_test "${prefix}print -pretty -- /x 1" " = 0x1"
172 # Smoke test that options actually work.
173 gdb_test "${prefix}print -pretty -- g_s" \
181 test_gdb_complete_unique \
182 "${prefix}print xxx" \
183 "${prefix}print xxx1"
184 test_gdb_complete_unique \
185 "${prefix}print -- xxx" \
186 "${prefix}print -- xxx1"
188 # Error messages when testing with "compile" are different from
189 # the error messages gdb's internal parser throws. This procedure
190 # hides the difference. EXPECTED_RE is only considered when not
191 # testing with "compile".
192 proc test_invalid_expression {cmd expected_re} {
195 if {$prefix != "compile "} {
196 gdb_test $cmd $expected_re
198 # Error messages depend on compiler version, so we just
199 # look for the last line indicating a failure.
200 gdb_test $cmd "Compilation failed\\."
204 # Check that '-XXX' without a "--" is handled as an
206 gdb_test "${prefix}print -1" " = -1"
207 test_invalid_expression \
208 "${prefix}print --1" \
209 "Left operand of assignment is not an lvalue\\."
210 test_invalid_expression \
211 "${prefix}print -object" \
212 "No symbol \"object\".*"
214 # Test printing with options and no expression.
215 set test "${prefix}print -object --"
216 if {$prefix != "compile "} {
217 # Regular "print" repeats the last history value.
218 gdb_test $test " = -1"
220 # "compile print" starts a multiline expression.
221 gdb_test_multiple $test $test {
223 gdb_test "-1\nend" " = -1" \
229 # Check that everything after "-- " is treated as an
230 # expression, not confused with an option.
231 test_invalid_expression \
232 "${prefix}print -- -address" \
234 gdb_test "${prefix}print -- -1" " = -1"
235 test_invalid_expression \
236 "${prefix}print -- --1" \
237 "Left operand of assignment is not an lvalue\\."
240 # Basic option-machinery + "backtrace" command integration tests.
241 proc_with_prefix test-backtrace {} {
244 test_gdb_complete_unique "backtrace" "backtrace"
245 test_gdb_complete_none "backtrace "
247 gdb_test "backtrace -" "Ambiguous option at: -"
248 gdb_test "backtrace --" "No stack\\."
249 gdb_test "backtrace -- -" "No stack\\."
251 test_gdb_complete_multiple "backtrace " "-" "" {
259 "-raw-frame-arguments"
262 # Test that we complete the qualifiers, if there's any.
263 test_gdb_complete_unique \
266 test_gdb_complete_unique \
269 test_gdb_complete_unique \
271 "backtrace no-filters"
274 clean_restart $binfile
277 fail "cannot run to main"
281 # COUNT in "backtrace COUNT" is parsed as an expression. Check
282 # that we complete expressions.
284 test_gdb_complete_unique \
288 test_gdb_complete_unique \
292 test_gdb_complete_unique \
293 "backtrace 1 + xxx" \
296 test_gdb_complete_unique \
297 "backtrace (1 + xxx" \
298 "backtrace (1 + xxx1"
301 # Basic option-machinery + "frame apply" command integration tests.
302 proc_with_prefix test-frame-apply {} {
303 test_gdb_complete_unique "frame apply all" "frame apply all"
305 gdb_test "frame apply level 0-" \
306 "Please specify a command to apply on the selected frames"
307 test_gdb_complete_none "frame apply level 0-"
312 "frame apply level 0"
316 test_gdb_completion_offers_commands "$cmd "
318 # tfaas is silent on command error by design. This procedure
319 # hides that aspect. EXPECTED_RE is only considered when not
320 # testing with "faas"/"tfaas".
321 proc test_error_cmd {cmd arg expected_re} {
322 if {$cmd == "tfaas"} {
323 gdb_test_no_output "$cmd$arg"
325 gdb_test "$cmd$arg" $expected_re
328 # Same, but for tests where both "faas" and "tfaas" are
329 # expected to be silent.
330 proc test_error_cmd2 {cmd arg expected_re} {
331 if {$cmd == "tfaas" || $cmd == "faas"} {
332 gdb_test_no_output "$cmd$arg"
334 gdb_test "$cmd$arg" $expected_re
338 test_error_cmd $cmd " -" "Ambiguous option at: -"
339 test_gdb_complete_multiple "$cmd " "-" "" {
347 with_test_prefix "no-trailing-space" {
348 test_error_cmd $cmd " --" \
349 "Please specify a command to apply on the selected frames"
350 test_gdb_complete_unique "$cmd --" "$cmd --"
353 with_test_prefix "trailing-space" {
354 test_error_cmd $cmd " -- " \
355 "Please specify a command to apply on the selected frames"
356 test_gdb_completion_offers_commands "$cmd -- "
359 # '-' is a valid TUI command.
360 test_error_cmd2 $cmd " -- -" \
361 "Cannot enable the TUI when output is not a terminal"
362 test_gdb_complete_unique \
366 test_error_cmd2 $cmd " -foo" \
367 "Undefined command: \"-foo\". Try \"help\"\\."
368 test_gdb_complete_none "$cmd -foo"
370 test_gdb_completion_offers_commands "$cmd -s "
374 # Miscellaneous tests.
375 proc_with_prefix test-misc {variant} {
378 set cmd [make_cmd $variant]
380 # Call test command with no arguments at all.
381 gdb_test "$cmd" [expect_none ""]
383 # Now with a single dash.
384 if {$variant == "require-delimiter"} {
385 gdb_test "$cmd -" [expect_none "-"]
387 gdb_test "$cmd -" "Ambiguous option at: -"
390 # Completing at "-" should list all options.
391 res_test_gdb_complete_multiple "1" "$cmd " "-" "" $all_options
393 # Now with a double dash.
394 gdb_test "$cmd --" [expect_none ""]
396 # "--" is recognized by options completer, gdb auto-appends a
398 test_completer_recognizes 1 "$cmd --"
400 # Now with a double dash, plus a dash as operand.
401 gdb_test "$cmd -- -" [expect_none "-"]
402 res_test_gdb_complete_none "0 -" "$cmd -- -"
404 # Completing an unambiguous option just appends an empty space.
405 test_completer_recognizes 1 "$cmd -flag"
407 # Try running an ambiguous option.
408 if {$variant == "require-delimiter"} {
409 gdb_test "$cmd -xx" [expect_none "-xx"]
411 gdb_test "$cmd -xx" "Ambiguous option at: -xx"
414 # Check that options are not case insensitive.
415 gdb_test "$cmd -flag --" [expect_flag ""]
417 # Check how the different modes behave on unknown option, with a
419 gdb_test "$cmd -FLAG --" \
420 "Unrecognized option at: -FLAG --"
422 # Check how the different modes behave on unknown option, without
424 if {$variant == "unknown-is-error"} {
425 gdb_test "$cmd -FLAG" \
426 "Unrecognized option at: -FLAG"
428 gdb_test "$cmd -FLAG" [expect_none "-FLAG"]
431 # Test parsing stops at a negative integer.
432 gdb_test "$cmd -1 --" \
433 "Unrecognized option at: -1 --"
434 gdb_test "$cmd -2 --" \
435 "Unrecognized option at: -2 --"
439 proc_with_prefix test-flag {variant} {
442 set cmd [make_cmd $variant]
444 # Completing a flag just appends a space.
445 test_completer_recognizes 1 "$cmd -flag"
447 # Add a dash, and all options should be shown.
448 test_gdb_complete_multiple "$cmd -flag " "-" "" $all_options
450 # Basic smoke tests of accepted / not accepted values.
452 # Check all the different variants a bool option may be specified.
453 if {$variant == "require-delimiter"} {
454 gdb_test "$cmd -flag 999" [expect_none "-flag 999"]
456 gdb_test "$cmd -flag 999" [expect_flag "999"]
458 gdb_test "$cmd -flag -- 999" [expect_flag "999"]
460 # If the "--" separator is present, then GDB errors out if the
461 # flag option is passed some value -- check that too.
462 gdb_test "$cmd -flag xxx 999 --" "Unrecognized option at: xxx 999 --"
463 gdb_test "$cmd -flag o 999 --" "Unrecognized option at: o 999 --"
464 gdb_test "$cmd -flag 1 999 --" "Unrecognized option at: 1 999 --"
466 # Extract twice the same flag, separated by one space.
467 gdb_test "$cmd -flag -flag -- non flags args" \
468 [expect_flag "non flags args"]
470 # Extract twice the same flag, separated by one space.
471 gdb_test "$cmd -xx1 -xx2 -xx1 -xx2 -xx1 -- non flags args" \
472 "-flag 0 -xx1 1 -xx2 1 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -- non flags args"
474 # Extract 2 known flags in front of unknown flags.
475 gdb_test "$cmd -xx1 -xx2 -a -b -c -xx1 --" \
476 "Unrecognized option at: -a -b -c -xx1 --"
478 # Check that combined flags are not recognised.
479 gdb_test "$cmd -xx1 -xx1xx2 -xx1 --" \
480 "Unrecognized option at: -xx1xx2 -xx1 --"
482 # Make sure the completer don't confuse a flag option with a
483 # boolean option. Specifically, "o" should not complete to
486 if {$variant == "require-delimiter"} {
487 res_test_gdb_complete_none "1" "$cmd -flag o"
489 gdb_test "$cmd -flag o" [expect_none "-flag o"]
491 res_test_gdb_complete_none "0 o" "$cmd -flag o"
493 gdb_test "$cmd -flag o" [expect_flag "o"]
497 # Boolean option tests.
498 proc_with_prefix test-boolean {variant} {
501 set cmd [make_cmd $variant]
503 # Boolean option's values are optional -- "on" is implied. Check
506 # - For require-delimiter commands, completing after a boolean
507 # option lists all other options, plus "on/off". This is
508 # because operands won't be processed until we see a "--"
511 # - For !require-delimiter commands, completing after a boolean
512 # option completes as an operand, since that will tend to be
513 # more common than typing "on/off".
514 # E.g., "frame apply all -past-main COMMAND".
516 if {$variant == "require-delimiter"} {
517 res_test_gdb_complete_multiple 1 "$cmd -bool " "" "" {
524 "-zuinteger-unlimited"
529 res_test_gdb_complete_none "0 " "$cmd -bool "
532 # Add another dash, and "on/off" are no longer offered:
533 res_test_gdb_complete_multiple 1 "$cmd -bool " "-" "" $all_options
535 # Basic smoke tests of accepted / not accepted values.
537 # The command accepts all of "1/0/enable/disable/yes/no" too, even
538 # though like the "set" command, we don't offer those as
539 # completion candidates if you complete right after the boolean
540 # command's name, like:
542 # (gdb) maint test-options require-delimiter -bool [TAB]
545 # However, the completer does recognize them if you start typing
547 foreach value {"0" "1"} {
548 test_completer_recognizes 1 "$cmd -bool $value"
550 foreach value {"of" "off"} {
551 res_test_gdb_complete_unique 1 \
552 "$cmd -bool $value" \
555 foreach value {"y" "ye" "yes"} {
556 res_test_gdb_complete_unique 1 \
557 "$cmd -bool $value" \
560 foreach value {"n" "no"} {
561 res_test_gdb_complete_unique 1 \
562 "$cmd -bool $value" \
573 res_test_gdb_complete_unique 1 \
574 "$cmd -bool $value" \
586 res_test_gdb_complete_unique 1 \
587 "$cmd -bool $value" \
591 if {$variant == "require-delimiter"} {
592 res_test_gdb_complete_none "1" "$cmd -bool xxx"
594 res_test_gdb_complete_none "0 xxx" "$cmd -bool xxx"
597 # The command accepts abbreviations of "enable/disable/yes/no",
598 # even though we don't offer those for completion.
608 gdb_test "$cmd -bool $value --" [expect_bool ""]
621 gdb_test "$cmd -bool $value --" [expect_none ""]
624 if {$variant == "require-delimiter"} {
625 gdb_test "$cmd -bool 999" [expect_none "-bool 999"]
627 gdb_test "$cmd -bool 999" [expect_bool "999"]
629 gdb_test "$cmd -bool -- 999" [expect_bool "999"]
631 # Since "on" is implied after a boolean option, for
632 # !require-delimiter commands, anything that is not
633 # yes/no/1/0/on/off/enable/disable should be considered as the raw
634 # input after the last option. Also check "o", which might look
635 # like "on" or "off", but it's treated the same.
637 foreach arg {"xxx" "o"} {
638 if {$variant == "require-delimiter"} {
639 gdb_test "$cmd -bool $arg" [expect_none "-bool $arg"]
641 gdb_test "$cmd -bool $arg" [expect_bool "$arg"]
644 # Also try -1. "unknown-is-error" commands error out saying that
645 # that's not a valid option.
646 if {$variant == "require-delimiter"} {
647 gdb_test "$cmd -bool -1" \
648 [expect_none "-bool -1"]
649 } elseif {$variant == "unknown-is-error"} {
650 gdb_test "$cmd -bool -1" \
651 "Unrecognized option at: -1"
653 gdb_test "$cmd -bool -1" [expect_bool "-1"]
656 # OTOH, if the "--" separator is present, then GDB errors out if
657 # the boolean option is passed an invalid value -- check that too.
658 gdb_test "$cmd -bool -1 999 --" \
659 "Unrecognized option at: -1 999 --"
660 gdb_test "$cmd -bool xxx 999 --" \
661 "Value given for `-bool' is not a boolean: xxx"
662 gdb_test "$cmd -bool o 999 --" \
663 "Value given for `-bool' is not a boolean: o"
665 # Completing after a boolean option + "o" does list "on/off",
667 if {$variant == "require-delimiter"} {
668 res_test_gdb_complete_multiple 1 "$cmd -bool " "o" "" {
673 res_test_gdb_complete_multiple "0 o" "$cmd -bool " "o" "" {
680 # Uinteger option tests. OPTION is which integer option we're
681 # testing. Can be "uinteger" or "zuinteger-unlimited".
682 proc_with_prefix test-uinteger {variant option} {
685 set cmd "[make_cmd $variant] -$option"
687 # Test completing a uinteger option:
688 res_test_gdb_complete_multiple 1 "$cmd " "" "" {
693 # NUMBER above is just a placeholder, make sure we don't complete
694 # it as a valid option.
695 res_test_gdb_complete_none 1 "$cmd NU"
697 # "unlimited" is valid though.
698 res_test_gdb_complete_unique 1 \
702 # Basic smoke test of accepted / not accepted values.
703 gdb_test "$cmd 1 -- 999" [expect_integer $option "1" "999"]
704 gdb_test "$cmd unlimited -- 999" \
705 [expect_integer $option "unlimited" "999"]
706 if {$option == "zuinteger-unlimited"} {
707 gdb_test "$cmd -1 --" [expect_integer $option "unlimited" ""]
708 gdb_test "$cmd 0 --" [expect_integer $option "0" ""]
710 gdb_test "$cmd -1 --" "integer -1 out of range"
711 gdb_test "$cmd 0 --" [expect_integer $option "unlimited" ""]
713 gdb_test "$cmd xxx --" \
714 "Expected integer at: xxx --"
715 gdb_test "$cmd unlimitedx --" \
716 "Expected integer at: unlimitedx --"
718 # Don't offer completions until we're past the
719 # -uinteger/-zuinteger-unlimited argument.
720 res_test_gdb_complete_none 1 "$cmd 1"
722 # A number of invalid values.
723 foreach value {"x" "x " "1a" "1a " "1-" "1- " "unlimitedx"} {
724 res_test_gdb_complete_none 1 "$cmd $value"
728 if {$option == "uinteger"} {
729 # -1 is invalid uinteger.
730 foreach value {"-1" "-1 "} {
731 res_test_gdb_complete_none 1 "$cmd $value"
734 # -1 is valid for zuinteger-unlimited.
735 res_test_gdb_complete_none 1 "$cmd -1"
736 if {$variant == "require-delimiter"} {
737 res_test_gdb_complete_multiple 1 "$cmd -1 " "" "-" $all_options
739 res_test_gdb_complete_none "0 " "$cmd -1 "
743 # Check that after a fully parsed option:
745 # - for require-delimiter commands, completion offers all
748 # - for !require-delimiter commands, completion offers nothing
750 if {$variant == "require-delimiter"} {
751 res_test_gdb_complete_multiple 1 "$cmd 1 " "" "-" $all_options
753 res_test_gdb_complete_none "0 " "$cmd 1 "
756 # Test completing non-option arguments after "-uinteger 1 ".
757 foreach operand {"x" "x " "1a" "1a " "1-" "1- "} {
758 if {$variant == "require-delimiter"} {
759 res_test_gdb_complete_none 1 "$cmd 1 $operand"
761 res_test_gdb_complete_none "0 $operand" "$cmd 1 $operand"
764 # These look like options, but they aren't.
765 foreach operand {"-1" "-1 "} {
766 if {$variant == "unknown-is-operand"} {
767 res_test_gdb_complete_none "0 $operand" "$cmd 1 $operand"
769 res_test_gdb_complete_none 1 "$cmd 1 $operand"
775 proc_with_prefix test-enum {variant} {
776 set cmd [make_cmd $variant]
778 res_test_gdb_complete_multiple 1 "$cmd -enum " "" "" {
784 # Check that "-" where a value is expected does not show the
785 # command's options. I.e., an enum's value is not optional.
786 # Check both completion and running the command.
787 res_test_gdb_complete_none 1 "$cmd -enum -"
788 gdb_test "$cmd -enum --"\
789 "Requires an argument. Valid arguments are xxx, yyy, zzz\\."
791 # Try passing an undefined item to an enum option.
792 gdb_test "$cmd -enum www --" "Undefined item: \"www\"."
795 # Run the options framework tests first.
796 foreach_with_prefix cmd {
804 foreach subcmd {"uinteger" "zuinteger-unlimited" } {
805 test-uinteger $cmd $subcmd
810 # Run the print integration tests, both as "standalone", and under
811 # "frame apply". The latter checks that the "frame apply ... COMMAND"
812 # commands recurse the completion machinery for COMMAND completion
818 "frame apply level 0 "
823 # Same for "compile print". Not really a wrapper prefix command like
824 # "frame apply", but similar enough that we test pretty much the same
826 if ![skip_compile_feature_tests] {
827 test-print "compile "
830 # Basic "backtrace" integration tests.
833 # Basic "frame apply" integration tests.