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:
29 load_lib completion-support.exp
33 if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
39 if { ![readline_is_used] } {
40 untested "no tab completion support without readline"
44 # Check the completion result, as returned by the "maintenance show
45 # test-options-completion-result" command. TEST is used as test name.
46 proc check_completion_result {expected test} {
47 gdb_test "maintenance show test-options-completion-result" \
49 "$test: res=$expected"
52 # Like test_gdb_complete_unique, but the expected output is expected
53 # to be the input line. I.e., the line is already complete. We're
54 # just checking whether GDB recognizes the option and auto-appends a
56 proc test_completer_recognizes {res input_line} {
57 set expected_re [string_to_regexp $input_line]
58 test_gdb_complete_unique $input_line $expected_re
59 check_completion_result $res $input_line
62 # Wrapper around test_gdb_complete_multiple that also checks the
63 # completion result is RES.
64 proc res_test_gdb_complete_multiple {res cmd_prefix completion_word args} {
65 test_gdb_complete_multiple $cmd_prefix $completion_word {*}$args
66 check_completion_result $res "$cmd_prefix$completion_word"
69 # Wrapper around test_gdb_complete_none that also checks the
70 # completion result is RES.
71 proc res_test_gdb_complete_none { res input_line } {
72 test_gdb_complete_none $input_line
73 check_completion_result $res "$input_line"
76 # Wrapper around test_gdb_complete_unique that also checks the
77 # completion result is RES.
78 proc res_test_gdb_complete_unique { res input_line args} {
79 test_gdb_complete_unique $input_line {*}$args
80 check_completion_result $res "$input_line"
83 # Make a full command name from VARIANT. VARIANT is either
84 # "require-delimiter", "unknown-is-error" or "unknown-is-operand".
85 proc make_cmd {variant} {
86 return "maint test-options $variant"
89 # Return a string for the expected result of running "maint
90 # test-options xxx", with no flag/option set. OPERAND is the expected
92 proc expect_none {operand} {
93 return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -- $operand"
96 # Return a string for the expected result of running "maint
97 # test-options xxx", with -flag set. OPERAND is the expected operand.
98 proc expect_flag {operand} {
99 return "-flag 1 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -- $operand"
102 # Return a string for the expected result of running "maint
103 # test-options xxx", with -bool set. OPERAND is the expected operand.
104 proc expect_bool {operand} {
105 return "-flag 0 -xx1 0 -xx2 0 -bool 1 -enum xxx -uint 0 -zuint-unl 0 -- $operand"
108 # Return a string for the expected result of running "maint
109 # test-options xxx", with one of the integer options set to $VAL.
110 # OPTION determines which option to expect set. OPERAND is the
112 proc expect_integer {option val operand} {
113 if {$option == "uinteger"} {
114 return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint $val -zuint-unl 0 -- $operand"
115 } elseif {$option == "zuinteger-unlimited"} {
116 return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl $val -- $operand"
118 error "unsupported option: $option"
129 "-zuinteger-unlimited"
132 # Basic option-machinery + "print" command integration tests.
133 proc_with_prefix test-print {{prefix ""}} {
136 # Completing "print" with no argument completes on symbols only,
137 # no options are offered. Since we haven't loaded any symbols,
138 # the match list should be empty.
139 test_gdb_complete_none "${prefix}print "
141 # OTOH, completing at "-" should list all options.
142 test_gdb_complete_multiple "${prefix}print " "-" "" {
159 clean_restart $binfile
162 fail "cannot run to main"
166 # Mix options and format.
167 gdb_test "${prefix}print -pretty -- /x 1" " = 0x1"
169 # Smoke test that options actually work.
170 gdb_test "${prefix}print -pretty -- g_s" \
178 test_gdb_complete_unique \
179 "${prefix}print xxx" \
180 "${prefix}print xxx1"
181 test_gdb_complete_unique \
182 "${prefix}print -- xxx" \
183 "${prefix}print -- xxx1"
185 # Error messages when testing with "compile" are different from
186 # the error messages gdb's internal parser throws. This procedure
187 # hides the difference. EXPECTED_RE is only considered when not
188 # testing with "compile".
189 proc test_invalid_expression {cmd expected_re} {
192 if {$prefix != "compile "} {
193 gdb_test $cmd $expected_re
195 # Error messages depend on compiler version, so we just
196 # look for the last line indicating a failure.
197 gdb_test $cmd "Compilation failed\\."
201 # Check that '-XXX' without a "--" is handled as an
203 gdb_test "${prefix}print -1" " = -1"
204 test_invalid_expression \
205 "${prefix}print --1" \
206 "Left operand of assignment is not an lvalue\\."
207 test_invalid_expression \
208 "${prefix}print -object" \
209 "No symbol \"object\".*"
211 # Test printing with options and no expression.
212 set test "${prefix}print -object --"
213 if {$prefix != "compile "} {
214 # Regular "print" repeats the last history value.
215 gdb_test $test " = -1"
217 # "compile print" starts a multiline expression.
218 gdb_test_multiple $test $test {
220 gdb_test "-1\nend" " = -1" \
226 # Check that everything after "-- " is treated as an
227 # expression, not confused with an option.
228 test_invalid_expression \
229 "${prefix}print -- -address" \
231 gdb_test "${prefix}print -- -1" " = -1"
232 test_invalid_expression \
233 "${prefix}print -- --1" \
234 "Left operand of assignment is not an lvalue\\."
237 # Basic option-machinery + "backtrace" command integration tests.
238 proc_with_prefix test-backtrace {} {
241 test_gdb_complete_unique "backtrace" "backtrace"
242 test_gdb_complete_none "backtrace "
244 gdb_test "backtrace -" "Ambiguous option at: -"
245 gdb_test "backtrace --" "No stack\\."
246 gdb_test "backtrace -- -" "No stack\\."
248 test_gdb_complete_multiple "backtrace " "-" "" {
256 "-raw-frame-arguments"
260 clean_restart $binfile
263 fail "cannot run to main"
267 # COUNT in "backtrace COUNT" is parsed as an expression. Check
268 # that we complete expressions.
270 test_gdb_complete_unique \
274 test_gdb_complete_unique \
278 test_gdb_complete_unique \
279 "backtrace 1 + xxx" \
282 test_gdb_complete_unique \
283 "backtrace (1 + xxx" \
284 "backtrace (1 + xxx1"
287 # Miscellaneous tests.
288 proc_with_prefix test-misc {variant} {
291 set cmd [make_cmd $variant]
293 # Call test command with no arguments at all.
294 gdb_test "$cmd" [expect_none ""]
296 # Now with a single dash.
297 if {$variant == "require-delimiter"} {
298 gdb_test "$cmd -" [expect_none "-"]
300 gdb_test "$cmd -" "Ambiguous option at: -"
303 # Completing at "-" should list all options.
304 res_test_gdb_complete_multiple "1" "$cmd " "-" "" $all_options
306 # Now with a double dash.
307 gdb_test "$cmd --" [expect_none ""]
309 # "--" is recognized by options completer, gdb auto-appends a
311 test_completer_recognizes 1 "$cmd --"
313 # Now with a double dash, plus a dash as operand.
314 gdb_test "$cmd -- -" [expect_none "-"]
315 res_test_gdb_complete_none "0 -" "$cmd -- -"
317 # Completing an unambiguous option just appends an empty space.
318 test_completer_recognizes 1 "$cmd -flag"
320 # Try running an ambiguous option.
321 if {$variant == "require-delimiter"} {
322 gdb_test "$cmd -xx" [expect_none "-xx"]
324 gdb_test "$cmd -xx" "Ambiguous option at: -xx"
327 # Check that options are not case insensitive.
328 gdb_test "$cmd -flag --" [expect_flag ""]
330 # Check how the different modes behave on unknown option, with a
332 gdb_test "$cmd -FLAG --" \
333 "Unrecognized option at: -FLAG --"
335 # Check how the different modes behave on unknown option, without
337 if {$variant == "unknown-is-error"} {
338 gdb_test "$cmd -FLAG" \
339 "Unrecognized option at: -FLAG"
341 gdb_test "$cmd -FLAG" [expect_none "-FLAG"]
344 # Test parsing stops at a negative integer.
345 gdb_test "$cmd -1 --" \
346 "Unrecognized option at: -1 --"
347 gdb_test "$cmd -2 --" \
348 "Unrecognized option at: -2 --"
352 proc_with_prefix test-flag {variant} {
355 set cmd [make_cmd $variant]
357 # Completing a flag just appends a space.
358 test_completer_recognizes 1 "$cmd -flag"
360 # Add a dash, and all options should be shown.
361 test_gdb_complete_multiple "$cmd -flag " "-" "" $all_options
363 # Basic smoke tests of accepted / not accepted values.
365 # Check all the different variants a bool option may be specified.
366 if {$variant == "require-delimiter"} {
367 gdb_test "$cmd -flag 999" [expect_none "-flag 999"]
369 gdb_test "$cmd -flag 999" [expect_flag "999"]
371 gdb_test "$cmd -flag -- 999" [expect_flag "999"]
373 # If the "--" separator is present, then GDB errors out if the
374 # flag option is passed some value -- check that too.
375 gdb_test "$cmd -flag xxx 999 --" "Unrecognized option at: xxx 999 --"
376 gdb_test "$cmd -flag o 999 --" "Unrecognized option at: o 999 --"
377 gdb_test "$cmd -flag 1 999 --" "Unrecognized option at: 1 999 --"
379 # Extract twice the same flag, separated by one space.
380 gdb_test "$cmd -flag -flag -- non flags args" \
381 [expect_flag "non flags args"]
383 # Extract twice the same flag, separated by one space.
384 gdb_test "$cmd -xx1 -xx2 -xx1 -xx2 -xx1 -- non flags args" \
385 "-flag 0 -xx1 1 -xx2 1 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -- non flags args"
387 # Extract 2 known flags in front of unknown flags.
388 gdb_test "$cmd -xx1 -xx2 -a -b -c -xx1 --" \
389 "Unrecognized option at: -a -b -c -xx1 --"
391 # Check that combined flags are not recognised.
392 gdb_test "$cmd -xx1 -xx1xx2 -xx1 --" \
393 "Unrecognized option at: -xx1xx2 -xx1 --"
395 # Make sure the completer don't confuse a flag option with a
396 # boolean option. Specifically, "o" should not complete to
399 if {$variant == "require-delimiter"} {
400 res_test_gdb_complete_none "1" "$cmd -flag o"
402 gdb_test "$cmd -flag o" [expect_none "-flag o"]
404 res_test_gdb_complete_none "0 o" "$cmd -flag o"
406 gdb_test "$cmd -flag o" [expect_flag "o"]
410 # Boolean option tests.
411 proc_with_prefix test-boolean {variant} {
414 set cmd [make_cmd $variant]
416 # Boolean option's values are optional -- "on" is implied. Check
419 # - For require-delimiter commands, completing after a boolean
420 # option lists all other options, plus "on/off". This is
421 # because operands won't be processed until we see a "--"
424 # - For !require-delimiter commands, completing after a boolean
425 # option completes as an operand, since that will tend to be
426 # more common than typing "on/off".
427 # E.g., "frame apply all -past-main COMMAND".
429 if {$variant == "require-delimiter"} {
430 res_test_gdb_complete_multiple 1 "$cmd -bool " "" "" {
437 "-zuinteger-unlimited"
442 res_test_gdb_complete_none "0 " "$cmd -bool "
445 # Add another dash, and "on/off" are no longer offered:
446 res_test_gdb_complete_multiple 1 "$cmd -bool " "-" "" $all_options
448 # Basic smoke tests of accepted / not accepted values.
450 # The command accepts all of "1/0/enable/disable/yes/no" too, even
451 # though like the "set" command, we don't offer those as
452 # completion candidates if you complete right after the boolean
453 # command's name, like:
455 # (gdb) maint test-options require-delimiter -bool [TAB]
458 # However, the completer does recognize them if you start typing
460 foreach value {"0" "1"} {
461 test_completer_recognizes 1 "$cmd -bool $value"
463 foreach value {"of" "off"} {
464 res_test_gdb_complete_unique 1 \
465 "$cmd -bool $value" \
468 foreach value {"y" "ye" "yes"} {
469 res_test_gdb_complete_unique 1 \
470 "$cmd -bool $value" \
473 foreach value {"n" "no"} {
474 res_test_gdb_complete_unique 1 \
475 "$cmd -bool $value" \
486 res_test_gdb_complete_unique 1 \
487 "$cmd -bool $value" \
499 res_test_gdb_complete_unique 1 \
500 "$cmd -bool $value" \
504 if {$variant == "require-delimiter"} {
505 res_test_gdb_complete_none "1" "$cmd -bool xxx"
507 res_test_gdb_complete_none "0 xxx" "$cmd -bool xxx"
510 # The command accepts abbreviations of "enable/disable/yes/no",
511 # even though we don't offer those for completion.
521 gdb_test "$cmd -bool $value --" [expect_bool ""]
534 gdb_test "$cmd -bool $value --" [expect_none ""]
537 if {$variant == "require-delimiter"} {
538 gdb_test "$cmd -bool 999" [expect_none "-bool 999"]
540 gdb_test "$cmd -bool 999" [expect_bool "999"]
542 gdb_test "$cmd -bool -- 999" [expect_bool "999"]
544 # Since "on" is implied after a boolean option, for
545 # !require-delimiter commands, anything that is not
546 # yes/no/1/0/on/off/enable/disable should be considered as the raw
547 # input after the last option. Also check "o", which might look
548 # like "on" or "off", but it's treated the same.
550 foreach arg {"xxx" "o"} {
551 if {$variant == "require-delimiter"} {
552 gdb_test "$cmd -bool $arg" [expect_none "-bool $arg"]
554 gdb_test "$cmd -bool $arg" [expect_bool "$arg"]
557 # Also try -1. "unknown-is-error" commands error out saying that
558 # that's not a valid option.
559 if {$variant == "require-delimiter"} {
560 gdb_test "$cmd -bool -1" \
561 [expect_none "-bool -1"]
562 } elseif {$variant == "unknown-is-error"} {
563 gdb_test "$cmd -bool -1" \
564 "Unrecognized option at: -1"
566 gdb_test "$cmd -bool -1" [expect_bool "-1"]
569 # OTOH, if the "--" separator is present, then GDB errors out if
570 # the boolean option is passed an invalid value -- check that too.
571 gdb_test "$cmd -bool -1 999 --" \
572 "Unrecognized option at: -1 999 --"
573 gdb_test "$cmd -bool xxx 999 --" \
574 "Value given for `-bool' is not a boolean: xxx"
575 gdb_test "$cmd -bool o 999 --" \
576 "Value given for `-bool' is not a boolean: o"
578 # Completing after a boolean option + "o" does list "on/off",
580 if {$variant == "require-delimiter"} {
581 res_test_gdb_complete_multiple 1 "$cmd -bool " "o" "" {
586 res_test_gdb_complete_multiple "0 o" "$cmd -bool " "o" "" {
593 # Uinteger option tests. OPTION is which integer option we're
594 # testing. Can be "uinteger" or "zuinteger-unlimited".
595 proc_with_prefix test-uinteger {variant option} {
598 set cmd "[make_cmd $variant] -$option"
600 # Test completing a uinteger option:
601 res_test_gdb_complete_multiple 1 "$cmd " "" "" {
606 # NUMBER above is just a placeholder, make sure we don't complete
607 # it as a valid option.
608 res_test_gdb_complete_none 1 "$cmd NU"
610 # "unlimited" is valid though.
611 res_test_gdb_complete_unique 1 \
615 # Basic smoke test of accepted / not accepted values.
616 gdb_test "$cmd 1 -- 999" [expect_integer $option "1" "999"]
617 gdb_test "$cmd unlimited -- 999" \
618 [expect_integer $option "unlimited" "999"]
619 if {$option == "zuinteger-unlimited"} {
620 gdb_test "$cmd -1 --" [expect_integer $option "unlimited" ""]
621 gdb_test "$cmd 0 --" [expect_integer $option "0" ""]
623 gdb_test "$cmd -1 --" "integer -1 out of range"
624 gdb_test "$cmd 0 --" [expect_integer $option "unlimited" ""]
626 gdb_test "$cmd xxx --" \
627 "Expected integer at: xxx --"
628 gdb_test "$cmd unlimitedx --" \
629 "Expected integer at: unlimitedx --"
631 # Don't offer completions until we're past the
632 # -uinteger/-zuinteger-unlimited argument.
633 res_test_gdb_complete_none 1 "$cmd 1"
635 # A number of invalid values.
636 foreach value {"x" "x " "1a" "1a " "1-" "1- " "unlimitedx"} {
637 res_test_gdb_complete_none 1 "$cmd $value"
641 if {$option == "uinteger"} {
642 # -1 is invalid uinteger.
643 foreach value {"-1" "-1 "} {
644 res_test_gdb_complete_none 1 "$cmd $value"
647 # -1 is valid for zuinteger-unlimited.
648 res_test_gdb_complete_none 1 "$cmd -1"
649 if {$variant == "require-delimiter"} {
650 res_test_gdb_complete_multiple 1 "$cmd -1 " "" "-" $all_options
652 res_test_gdb_complete_none "0 " "$cmd -1 "
656 # Check that after a fully parsed option:
658 # - for require-delimiter commands, completion offers all
661 # - for !require-delimiter commands, completion offers nothing
663 if {$variant == "require-delimiter"} {
664 res_test_gdb_complete_multiple 1 "$cmd 1 " "" "-" $all_options
666 res_test_gdb_complete_none "0 " "$cmd 1 "
669 # Test completing non-option arguments after "-uinteger 1 ".
670 foreach operand {"x" "x " "1a" "1a " "1-" "1- "} {
671 if {$variant == "require-delimiter"} {
672 res_test_gdb_complete_none 1 "$cmd 1 $operand"
674 res_test_gdb_complete_none "0 $operand" "$cmd 1 $operand"
677 # These look like options, but they aren't.
678 foreach operand {"-1" "-1 "} {
679 if {$variant == "unknown-is-operand"} {
680 res_test_gdb_complete_none "0 $operand" "$cmd 1 $operand"
682 res_test_gdb_complete_none 1 "$cmd 1 $operand"
688 proc_with_prefix test-enum {variant} {
689 set cmd [make_cmd $variant]
691 res_test_gdb_complete_multiple 1 "$cmd -enum " "" "" {
697 # Check that "-" where a value is expected does not show the
698 # command's options. I.e., an enum's value is not optional.
699 # Check both completion and running the command.
700 res_test_gdb_complete_none 1 "$cmd -enum -"
701 gdb_test "$cmd -enum --"\
702 "Requires an argument. Valid arguments are xxx, yyy, zzz\\."
704 # Try passing an undefined item to an enum option.
705 gdb_test "$cmd -enum www --" "Undefined item: \"www\"."
708 # Run the options framework tests first.
709 foreach_with_prefix cmd {
717 foreach subcmd {"uinteger" "zuinteger-unlimited" } {
718 test-uinteger $cmd $subcmd
723 # Run the print integration tests.
726 # Same for "compile print".
727 if ![skip_compile_feature_tests] {
728 test-print "compile "
731 # Basic "backtrace" integration tests.