Home | History | Annotate | Line # | Download | only in gdb.base
with.exp revision 1.1.1.2
      1 # This testcase is part of GDB, the GNU debugger.
      2 
      3 # Copyright 2019-2023 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 "with" command.
     19 
     20 load_lib completion-support.exp
     21 
     22 standard_testfile .c
     23 
     24 if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
     25     return -1
     26 }
     27 
     28 clean_restart $binfile
     29 
     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.
     36 
     37 proc test_with {setting values} {
     38     foreach val1 $values {
     39 	foreach val2 $values {
     40 	    gdb_test \
     41 		"maint with test-settings $setting $val1 -- maint with test-settings $setting $val2 -- p 1" \
     42 		" = 1"
     43 	}
     44     }
     45 }
     46 
     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.
     52 
     53 proc test_with_error {setting tmp_val expected_re} {
     54     global gdb_prompt
     55 
     56     with_test_prefix "$setting, $tmp_val" {
     57 	set test "save org value"
     58 	set org_val ""
     59 	gdb_test_multiple "maint show test-settings $setting" $test {
     60 	    -re "(.*)\r\n$gdb_prompt $" {
     61 		set org_val $expect_out(1,string)
     62 		pass $test
     63 	    }
     64 	}
     65 
     66 	gdb_test \
     67 	    "maint with test-settings $setting $tmp_val -- p 1" \
     68 	    $expected_re
     69 
     70 	gdb_test "maint show test-settings $setting" "^$org_val" \
     71 	    "value hasn't changed across error"
     72     }
     73 }
     74 
     75 # Test "with" framework basics, using the internal "maint with
     76 # test-settings" subcommands.
     77 with_test_prefix "maint" {
     78     test_with "auto-boolean" {"on" "off" "auto"}
     79     test_with "boolean" {"" "on" "off" "0" "1" "enable" "disable"}
     80     test_with "integer" {"0" "1" "-1" "unlimited"}
     81     test_with "uinteger" {"0" "1" "unlimited"}
     82     test_with "zinteger" {"0" "1" "-1"}
     83     test_with "zuinteger" {"0" "1"}
     84     test_with "zuinteger-unlimited" {"-1" "unlimited" "0" "1"}
     85     test_with "string" {"" "foo" "\"hello world\""}
     86     test_with "string-noescape" {"" "foo" "\"hello world\""}
     87     test_with "filename" {"/foo" "bar/x/y"}
     88     test_with "optional-filename" {"" "/foo" "bar/x/y"}
     89     test_with "enum" {"xxx" "yyy"}
     90 
     91     # Check the most important error conditions.  E.g., empty,
     92     # negative or "unlimited" values for settings that don't accept
     93     # those.  Exhaustive error coverage of the set/with value parsing
     94     # is left to "set" testing, in gdb.base/settings.exp.
     95     test_with_error "auto-boolean" "" \
     96 	"\"on\", \"off\" or \"auto\" expected\\."
     97     test_with_error "auto-boolean" "xxx" \
     98 	"\"on\", \"off\" or \"auto\" expected\\."
     99     test_with_error "boolean" "2" "\"on\" or \"off\" expected\\."
    100     test_with_error "uinteger" "-1" "integer -1 out of range"
    101     test_with_error "uinteger" "" \
    102 	"Argument required \\(integer to set it to, or \"unlimited\"\\)\\."
    103     test_with_error "zuinteger" "-1" "integer -1 out of range"
    104     test_with_error "zuinteger" "" \
    105 	"Argument required \\(integer to set it to\\)\\."
    106     test_with_error "zuinteger-unlimited" "-2" \
    107 	"only -1 is allowed to set as unlimited"
    108     test_with_error "zuinteger-unlimited" "" \
    109 	"Argument required \\(integer to set it to, or \"unlimited\"\\)\\."
    110     test_with_error "filename" "" \
    111 	"Argument required \\(filename to set it to\\.\\)\\."
    112     test_with_error "enum" "" \
    113 	"Requires an argument\\. Valid arguments are xxx, yyy, zzz\\."
    114 }
    115 
    116 # Basic/core tests using user-visible commands.
    117 with_test_prefix "basics" {
    118     gdb_test "print g_s" " = {a = 1, b = 2, c = 3}"
    119     gdb_test "with print pretty -- print g_s" \
    120 	[multi_line  \
    121 	     " = {" \
    122 	     "  a = 1," \
    123 	     "  b = 2," \
    124 	     "  c = 3" \
    125 	     "}"]
    126 
    127     # A boolean setting.
    128     gdb_test "with non-stop on -- show non-stop" \
    129 	"Controlling the inferior in non-stop mode is on\\."
    130     gdb_test "show non-stop" \
    131 	"Controlling the inferior in non-stop mode is off\\."
    132 
    133     # Language.
    134     gdb_test "with language pascal -- show language" \
    135 	"The current source language is \"pascal\"\\."
    136 
    137     gdb_test "show language" \
    138 	"The current source language is \"auto; currently c\"\\."
    139 
    140     gdb_test "with language ada -- print g_s" \
    141 	" = \\(a => 1, b => 2, c => 3\\)"
    142 
    143     # Nested "with"s.
    144     gdb_test "with language ada -- with language c -- print g_s" \
    145 	" = {a = 1, b = 2, c = 3}"
    146 
    147     # "w" alias.
    148     gdb_test "w language pascal -- show language" \
    149 	"The current source language is \"pascal\"\\." \
    150 	"w alias works"
    151 
    152     # An early prototype of the "with" command got this wrong.
    153     gdb_test \
    154 	"w print repeats unlimited -- w print repeats 1 -- p \"1223334444\"" \
    155 	" = \"1\", '2' <repeats 2 times>, '3' <repeats 3 times>, '4' <repeats 4 times>"
    156 }
    157 
    158 # Check a user-defined command.
    159 with_test_prefix "user-defined" {
    160     # A user defined command.
    161     set test "define usercmd"
    162     gdb_test_multiple "define usercmd" $test {
    163 	-re "End with"  {
    164 	    gdb_test \
    165 		[multi_line_input \
    166 		     {print g_s} \
    167 		     {end}] \
    168 		"" \
    169 		$test
    170 	}
    171     }
    172     gdb_test "with language ada -- usercmd" \
    173 	" = \\(a => 1, b => 2, c => 3\\)"
    174 }
    175 
    176 # Check repeating.
    177 with_test_prefix "repeat" {
    178     clean_restart $binfile
    179 
    180     # "with" with no command reinvokes the previous command.
    181     gdb_test "with language ada" \
    182 	"No previous command to relaunch" \
    183 	"reinvoke with no previous command to relaunch"
    184 
    185     gdb_test "print g_s" " = {a = 1, b = 2, c = 3}"
    186 
    187     gdb_test "with language ada" \
    188 	" = \\(a => 1, b => 2, c => 3\\)" \
    189 	"reinvoke with language"
    190 
    191     # Same, but with "--".
    192     gdb_test "with language fortran --" \
    193 	" = \\( a = 1, b = 2, c = 3 \\)" \
    194 	"reinvoke with language and --"
    195 
    196     # Repeating repeats the original "print g_s", not the last "with"
    197     # command.
    198     set test "repeat command line"
    199     send_gdb "\n"
    200     gdb_test_multiple "" $test {
    201 	-re " = {a = 1, b = 2, c = 3}\r\n$gdb_prompt $" {
    202 	    pass $test
    203 	}
    204     }
    205 }
    206 
    207 # Basic run control.
    208 with_test_prefix "run control" {
    209     clean_restart $binfile
    210 
    211     if ![runto_main] {
    212 	return
    213     }
    214 
    215     # Check "with" with a synchronous execution command.
    216     gdb_test "with disassemble-next-line on -- next" \
    217 	"return 0;.*=>.*"
    218 }
    219 
    220 # Check errors.
    221 with_test_prefix "errors" {
    222     gdb_test "with" "Missing arguments\\."
    223 
    224     # Try both an unknown root setting and an unknown prefixed
    225     # setting.  The errors come from different locations in the
    226     # sources.
    227     gdb_test "with xxxx yyyy" \
    228 	"Undefined set command: \"xxxx\".  Try \"help set\"\\."
    229     gdb_test "with print xxxx yyyy" \
    230 	"Undefined set print command: \"xxxx yyyy\".  Try \"help set print\"\\."
    231     # Try one error case for "maint with", to make sure the right
    232     # "maintenance with" prefix is shown.
    233     gdb_test "maint with xxxx yyyy" \
    234 	"Undefined maintenance set command: \"xxxx\".  Try \"help maintenance set\"\\."
    235 
    236     # Try ambiguous settings.
    237     gdb_test "with w" \
    238 	"Ambiguous set command \"w\": watchdog, width, write\\."
    239     gdb_test "with print m" \
    240 	"Ambiguous set print command \"m\": max-depth, max-symbolic-offset, memory-tag-violations\\."
    241 
    242     gdb_test "with variable xxx=1" \
    243 	"Cannot use this setting with the \"with\" command"
    244 
    245     gdb_test "with print elements -- p 1" \
    246 	"Argument required \\(integer to set it to, or \"unlimited\"\\)\\."
    247 
    248     gdb_test "with -- p 1" \
    249 	"Missing setting before '--' delimiter"
    250 
    251     # Check that the setting is restored even if the command throws.
    252     gdb_test "with print elements 1 -- unknowncommand" \
    253 	"Undefined command: \"unknowncommand\"\\.  Try \"help\"\\."
    254     gdb_test "show print elements" \
    255 	"Limit on string chars or array elements to print is 200\\."
    256 }
    257 
    258 # Check completion.
    259 with_test_prefix "completion" {
    260     test_gdb_complete_unique \
    261 	"with pri" \
    262 	"with print"
    263 
    264     test_gdb_complete_unique \
    265 	"with print ele" \
    266 	"with print elements"
    267 
    268     test_gdb_complete_unique \
    269 	"with print elements u" \
    270 	"with print elements unlimited"
    271 
    272     test_gdb_complete_none \
    273 	"with print elements unlimited "
    274 
    275     test_gdb_completion_offers_commands "with print elements unlimited -- "
    276 
    277     # Check that the completer nests into the nested command line's
    278     # completer.
    279     test_gdb_complete_unique \
    280 	"with print elements unlimited -- with print ele" \
    281 	"with print elements unlimited -- with print elements"
    282 
    283     # Check completion of "maint with".  "maint with" and "with"'s
    284     # completers share 99% of the code.  All we need to care about
    285     # here is that the completion word point is computed correctly, so
    286     # any simple completion is sufficient.
    287     test_gdb_complete_unique \
    288 	"maint with test-set" \
    289 	"maint with test-settings"
    290 }
    291