1# Copyright (C) 2010-2020 Free Software Foundation, Inc. 2 3# This program is free software; you can redistribute it and/or modify 4# it under the terms of the GNU General Public License as published by 5# the Free Software Foundation; either version 3 of the License, or 6# (at your option) any later version. 7# 8# This program is distributed in the hope that it will be useful, 9# but WITHOUT ANY WARRANTY; without even the implied warranty of 10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11# GNU General Public License for more details. 12# 13# You should have received a copy of the GNU General Public License 14# along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16# This file is part of the GDB testsuite. 17# It tests gdb.parameter and gdb.Parameter. 18 19load_lib gdb-python.exp 20 21# Start with a fresh gdb. 22gdb_exit 23gdb_start 24gdb_reinitialize_dir $srcdir/$subdir 25 26# Skip all tests if Python scripting is not enabled. 27if { [skip_python_tests] } { continue } 28 29# We use "." here instead of ":" so that this works on win32 too. 30if { [is_remote host] } { 31 # Don't match $srcdir/$subdir because proc gdb_reinitialize_dir 32 # doesn't set search directories on remote host. 33 set directories ".*\\\$cdir.\\\$cwd" 34} else { 35 set escaped_directory [string_to_regexp "$srcdir/$subdir"] 36 set directories "$escaped_directory.\\\$cdir.\\\$cwd" 37} 38gdb_test "python print (gdb.parameter ('directories'))" $directories 39 40# Test a simple boolean parameter. 41with_test_prefix "boolean parameter" { 42 gdb_test_multiline "Simple gdb booleanparameter" \ 43 "python" "" \ 44 "class TestParam (gdb.Parameter):" "" \ 45 " \"\"\"When enabled, test param does something useful. When disabled, does nothing.\"\"\"" "" \ 46 " show_doc = \"Show the state of the boolean test-param\"" ""\ 47 " set_doc = \"Set the state of the boolean test-param\"" "" \ 48 " def get_show_string (self, pvalue):" ""\ 49 " return \"The state of the Test Parameter is \" + pvalue" ""\ 50 " def get_set_string (self):" ""\ 51 " val = \"on\"" ""\ 52 " if (self.value == False):" ""\ 53 " val = \"off\"" ""\ 54 " return \"Test Parameter has been set to \" + val" ""\ 55 " def __init__ (self, name):" "" \ 56 " super (TestParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)" "" \ 57 " self.value = True" "" \ 58 "test_param = TestParam ('print test-param')" ""\ 59 "end" 60 61 gdb_test "python print (test_param.value)" "True" \ 62 "test boolean parameter value is True" 63 gdb_test "show print test-param" \ 64 "The state of the Test Parameter is on.*" "show parameter on" 65 gdb_test "set print test-param off" \ 66 "Test Parameter has been set to off" "turn off parameter" 67 gdb_test "show print test-param" \ 68 "The state of the Test Parameter is off.*" "show parameter off" 69 gdb_test "python print (test_param.value)" "False" \ 70 "test boolean parameter value is False" 71 gdb_test "help show print test-param" \ 72 "Show the state of the boolean test-param.*" "test show help" 73 gdb_test "help set print test-param" \ 74 "Set the state of the boolean test-param.*" "test set help" 75 gdb_test "help set print" \ 76 "set print test-param -- Set the state of the boolean test-param.*" \ 77 "test general help" 78} 79 80# Test an enum parameter. 81with_test_prefix "enum parameter" { 82 gdb_test_multiline "enum gdb parameter" \ 83 "python" "" \ 84 "class TestEnumParam (gdb.Parameter):" "" \ 85 " \"\"\"When set, test param does something useful. When disabled, does nothing.\"\"\"" "" \ 86 " show_doc = \"Show the state of the enum\"" ""\ 87 " set_doc = \"Set the state of the enum\"" "" \ 88 " def get_show_string (self, pvalue):" ""\ 89 " return \"The state of the enum is \" + pvalue" ""\ 90 " def get_set_string (self):" ""\ 91 " return \"The state of the enum has been set to \" + self.value" ""\ 92 " def __init__ (self, name):" "" \ 93 " super (TestEnumParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_ENUM, \[\"one\", \"two\"\])" "" \ 94 " self.value = \"one\"" "" \ 95 "test_enum_param = TestEnumParam ('print test-enum-param')" ""\ 96 "end" 97 98 gdb_test "python print (test_enum_param.value)" "one" \ 99 "test enum parameter value is one" 100 gdb_test "show print test-enum-param" \ 101 "The state of the enum is one.*" \ 102 "show parameter is initial value" 103 gdb_test "set print test-enum-param two" \ 104 "The state of the enum has been set to two" "set enum to two" 105 gdb_test "show print test-enum-param" \ 106 "The state of the enum is two.*" "show parameter is new value" 107 gdb_test "python print (test_enum_param.value)" "two" \ 108 "test enum parameter value is two" 109 gdb_test "set print test-enum-param three" \ 110 "Undefined item: \"three\".*" "set invalid enum parameter" 111} 112 113# Test a file parameter. 114with_test_prefix "file parameter" { 115 gdb_test_multiline "file gdb parameter" \ 116 "python" "" \ 117 "class TestFileParam (gdb.Parameter):" "" \ 118 " \"\"\"When set, test param does something useful. When disabled, does nothing.\"\"\"" "" \ 119 " show_doc = \"Show the name of the file\"" ""\ 120 " set_doc = \"Set the name of the file\"" "" \ 121 " def get_show_string (self, pvalue):" ""\ 122 " return \"The name of the file is \" + pvalue" ""\ 123 " def get_set_string (self):" ""\ 124 " return \"The name of the file has been changed to \" + self.value" ""\ 125 " def __init__ (self, name):" "" \ 126 " super (TestFileParam, self).__init__ (name, gdb.COMMAND_FILES, gdb.PARAM_FILENAME)" "" \ 127 " self.value = \"foo.txt\"" "" \ 128 "test_file_param = TestFileParam ('test-file-param')" ""\ 129 "end" 130 131 gdb_test "python print (test_file_param.value)" "foo.txt" \ 132 "test file parameter value" 133 gdb_test "show test-file-param" \ 134 "The name of the file is foo.txt.*" "show initial file value" 135 gdb_test "set test-file-param bar.txt" \ 136 "The name of the file has been changed to bar.txt" \ 137 "set new file parameter" 1 138 gdb_test "show test-file-param" \ 139 "The name of the file is bar.txt.*" "show new file value" 140 gdb_test "python print (test_file_param.value)" \ 141 "bar.txt" "test new file parameter value" 142 gdb_test "set test-file-param" "Argument required.*" 143} 144 145# Test a parameter that is not documented. 146with_test_prefix "undocumented parameter" { 147 gdb_test_multiline "Simple gdb booleanparameter" \ 148 "python" "" \ 149 "class TestUndocParam (gdb.Parameter):" "" \ 150 " def get_show_string (self, pvalue):" ""\ 151 " return \"The state of the Test Parameter is \" + pvalue" ""\ 152 " def get_set_string (self):" ""\ 153 " val = \"on\"" ""\ 154 " if (self.value == False):" ""\ 155 " val = \"off\"" ""\ 156 " return \"Test Parameter has been set to \" + val" ""\ 157 " def __init__ (self, name):" "" \ 158 " super (TestUndocParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)" "" \ 159 " self.value = True" "" \ 160 "test_undoc_param = TestUndocParam ('print test-undoc-param')" ""\ 161 "end" 162 163 gdb_test "show print test-undoc-param" \ 164 "The state of the Test Parameter is on.*" "show parameter on" 165 gdb_test "set print test-undoc-param off" \ 166 "Test Parameter has been set to off" "turn off parameter" 167 gdb_test "show print test-undoc-param" \ 168 "The state of the Test Parameter is off.*" "show parameter off" 169 gdb_test "python print (test_undoc_param.value)" \ 170 "False" "test undocumented parameter value is False" 171 gdb_test "help show print test-undoc-param" \ 172 "This command is not documented.*" "test show help" 173 gdb_test "help set print test-undoc-param" \ 174 "This command is not documented.*" "test set help" 175 gdb_test "help set print" \ 176 "set print test-undoc-param -- This command is not documented.*" \ 177 "test general help" 178} 179 180# Test a parameter that is not documented in any way.. 181with_test_prefix "really undocumented parameter" { 182 gdb_test_multiline "Simple gdb booleanparameter" \ 183 "python" "" \ 184 "class TestNodocParam (gdb.Parameter):" "" \ 185 " def __init__ (self, name):" "" \ 186 " super (TestNodocParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)" "" \ 187 " self.value = True" "" \ 188 "test_nodoc_param = TestNodocParam ('print test-nodoc-param')" ""\ 189 "end" 190 191 gdb_test "show print test-nodoc-param" \ 192 "This command is not documented.*" "show parameter on" 193 gdb_test_no_output "set print test-nodoc-param off" \ 194 "turn off parameter" 195 gdb_test "show print test-nodoc-param" \ 196 "This command is not documented.*.*" "show parameter off" 197 gdb_test "python print (test_nodoc_param.value)" \ 198 "False" "test really undocumented parameter value is False" 199 gdb_test "help show print test-nodoc-param" \ 200 "This command is not documented.*" "test show help" 201 gdb_test "help set print test-nodoc-param" \ 202 "This command is not documented.*" "test set help" 203 gdb_test "help set print" \ 204 "set print test-nodoc-param -- This command is not documented.*" \ 205 "test general help" 206} 207 208# Test deprecated API. Do not use in your own implementations. 209with_test_prefix "deprecated API parameter" { 210 gdb_test_multiline "Simple gdb booleanparameter" \ 211 "python" "" \ 212 "class TestParam (gdb.Parameter):" "" \ 213 " \"\"\"When enabled, test param does something useful. When disabled, does nothing.\"\"\"" "" \ 214 " show_doc = \"State of the Test Parameter\"" ""\ 215 " set_doc = \"Set the state of the Test Parameter\"" "" \ 216 " def __init__ (self, name):" "" \ 217 " super (TestParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)" "" \ 218 " self.value = True" "" \ 219 "test_param = TestParam ('print test-param')" ""\ 220 "end" 221 222 gdb_test "python print (test_param.value)" "True" \ 223 "test deprecated API parameter value is True" 224 gdb_test "show print test-param" \ 225 "State of the Test Parameter on.*" "show parameter on" 226 gdb_test_no_output "set print test-param off" "turn off parameter" 227 gdb_test "show print test-param" \ 228 "State of the Test Parameter off.*" "show parameter off" 229 gdb_test "python print (test_param.value)" "False" \ 230 "test deprecated API parameter value is False" 231 gdb_test "help show print test-param" \ 232 "State of the Test Parameter.*" "test show help" 233 gdb_test "help set print test-param" \ 234 "Set the state of the Test Parameter.*" "test set help" 235 gdb_test "help set print" \ 236 "set print test-param -- Set the state of the Test Parameter.*" \ 237 "test general help" 238} 239 240foreach kind {PARAM_ZUINTEGER PARAM_ZUINTEGER_UNLIMITED} { 241 gdb_test_multiline "Simple gdb $kind" \ 242 "python" "" \ 243 "class TestNodocParam (gdb.Parameter):" "" \ 244 " def __init__ (self, name):" "" \ 245 " super (TestNodocParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.$kind)" "" \ 246 " self.value = 0" "" \ 247 "test_param_$kind = TestNodocParam ('test-$kind')" "" \ 248 "end" 249 250 gdb_test "python print(gdb.parameter('test-$kind'))" "0" 251 252 gdb_test "python test_param_$kind.value = -5" "RuntimeError: Range exceeded.*" 253 254 if {$kind == "PARAM_ZUINTEGER"} { 255 gdb_test "python test_param_$kind.value = -1" "RuntimeError: Range exceeded.*" 256 } else { 257 gdb_test_no_output "python test_param_$kind.value = -1" \ 258 "check that PARAM_ZUINTEGER value can be set to -1" 259 gdb_test "python print(gdb.parameter('test-$kind'))" "-1" \ 260 "check that PARAM_ZUINTEGER value is -1 after setting" 261 } 262} 263 264gdb_test_multiline "Throwing gdb parameter" \ 265 "python" "" \ 266 "class TestThrowParam (gdb.Parameter):" "" \ 267 " def __init__ (self, name):" "" \ 268 " super (TestThrowParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_STRING)" "" \ 269 " self.value = True" "" \ 270 " def get_set_string (self):" "" \ 271 " raise gdb.GdbError('Ordinary gdb error')" "" \ 272 "test_throw_param = TestThrowParam ('print test-throw-param')" ""\ 273 "end" 274 275gdb_test "set print test-throw-param whoops" \ 276 "Ordinary gdb error" \ 277 "gdb.GdbError does not show Python stack" 278