Commit | Line | Data |
---|---|---|
88b9d363 | 1 | # Copyright (C) 2010-2022 Free Software Foundation, Inc. |
99e7ae30 DE |
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 | ||
b521dba8 DE |
16 | # This file is part of the GDB testsuite. |
17 | # It tests gdb.parameter and gdb.Parameter. | |
99e7ae30 | 18 | |
99e7ae30 DE |
19 | load_lib gdb-python.exp |
20 | ||
21 | # Start with a fresh gdb. | |
22 | gdb_exit | |
23 | gdb_start | |
24 | gdb_reinitialize_dir $srcdir/$subdir | |
25 | ||
26 | # Skip all tests if Python scripting is not enabled. | |
27 | if { [skip_python_tests] } { continue } | |
28 | ||
29 | # We use "." here instead of ":" so that this works on win32 too. | |
10c5f0a8 YQ |
30 | if { [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 { | |
2631b16a AW |
35 | set escaped_directory [string_to_regexp "$srcdir/$subdir"] |
36 | set directories "$escaped_directory.\\\$cdir.\\\$cwd" | |
10c5f0a8 YQ |
37 | } |
38 | gdb_test "python print (gdb.parameter ('directories'))" $directories | |
b521dba8 | 39 | |
79c02443 AB |
40 | # Check we can correctly read the data-directory parameter. First, |
41 | # grab the value as read directly from the GDB CLI. | |
42 | set dd "" | |
43 | gdb_test_multiple "show data-directory" \ | |
44 | "find the initial data-directory value" { | |
45 | -re -wrap "GDB's data directory is \"(\[^\r\n\]+)\"\\." { | |
46 | set dd $expect_out(1,string) | |
47 | pass $gdb_test_name | |
48 | } | |
49 | } | |
50 | ||
51 | # Now print the data-directory from Python. | |
52 | gdb_test "python print (gdb.parameter ('data-directory'))" $dd | |
53 | ||
54 | # Next change the data-directory to a relative path. Internally GDB | |
55 | # will resolve this to an absolute path, which Python should then see. | |
56 | # | |
57 | # GDB is currently running in '...../build/gdb/testsuite/' and the | |
58 | # test output is being written to: | |
59 | # ...../build/gdb/testsuite/outputs/gdb.python/py-parameter/ | |
60 | # | |
61 | # So create the relative path './outputs/gdb.python/py-parameter/' and | |
62 | # set the data-directory to that, we should then see the absolute path. | |
63 | ||
64 | set abs_path_to_output_dir [standard_output_file ""] | |
65 | set abs_path_to_cwd $objdir | |
66 | set rel_path_to_output_dir \ | |
67 | [file join "." [string replace ${abs_path_to_output_dir} 0 \ | |
68 | [string length ${abs_path_to_cwd}] ""]] | |
69 | gdb_test_no_output "set data-directory ${rel_path_to_output_dir}" | |
70 | ||
71 | gdb_test "python print (gdb.parameter ('data-directory'))" \ | |
72 | ${abs_path_to_output_dir} \ | |
73 | "python sees absolute version of data-directory path" | |
74 | ||
75 | # While we're here, check we see the correct path at GDB's CLI. | |
76 | gdb_test "show data-directory" \ | |
77 | "GDB's data directory is \"${abs_path_to_output_dir}\"\\." \ | |
78 | "check modified data-directory at the CLI" | |
79 | ||
80 | # Now lets set the data-directory back to what it was initially. | |
fd5c30cd AB |
81 | gdb_test_no_output "set data-directory ${dd}" \ |
82 | "set data-directory back to its original value" | |
79c02443 AB |
83 | |
84 | # And check we see the restored value at CLI and from Python. | |
85 | gdb_test "show data-directory" \ | |
86 | "GDB's data directory is \"${dd}\"\\." \ | |
87 | "check original data-directory was restored at the CLI" | |
88 | ||
89 | gdb_test "python print (gdb.parameter ('data-directory'))" ${dd} \ | |
90 | "python sees restored data-directory value" | |
91 | ||
b521dba8 | 92 | # Test a simple boolean parameter. |
082cce05 | 93 | with_test_prefix "boolean parameter" { |
2a17c803 | 94 | gdb_test_multiline "Simple gdb booleanparameter" \ |
082cce05 AB |
95 | "python" "" \ |
96 | "class TestParam (gdb.Parameter):" "" \ | |
97 | " \"\"\"When enabled, test param does something useful. When disabled, does nothing.\"\"\"" "" \ | |
98 | " show_doc = \"Show the state of the boolean test-param\"" ""\ | |
99 | " set_doc = \"Set the state of the boolean test-param\"" "" \ | |
100 | " def get_show_string (self, pvalue):" ""\ | |
101 | " return \"The state of the Test Parameter is \" + pvalue" ""\ | |
102 | " def get_set_string (self):" ""\ | |
103 | " val = \"on\"" ""\ | |
104 | " if (self.value == False):" ""\ | |
105 | " val = \"off\"" ""\ | |
106 | " return \"Test Parameter has been set to \" + val" ""\ | |
107 | " def __init__ (self, name):" "" \ | |
108 | " super (TestParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)" "" \ | |
109 | " self.value = True" "" \ | |
110 | "test_param = TestParam ('print test-param')" ""\ | |
111 | "end" | |
ecec24e6 | 112 | |
082cce05 AB |
113 | gdb_test "python print (test_param.value)" "True" \ |
114 | "test boolean parameter value is True" | |
115 | gdb_test "show print test-param" \ | |
116 | "The state of the Test Parameter is on.*" "show parameter on" | |
117 | gdb_test "set print test-param off" \ | |
118 | "Test Parameter has been set to off" "turn off parameter" | |
119 | gdb_test "show print test-param" \ | |
120 | "The state of the Test Parameter is off.*" "show parameter off" | |
121 | gdb_test "python print (test_param.value)" "False" \ | |
122 | "test boolean parameter value is False" | |
123 | gdb_test "help show print test-param" \ | |
124 | "Show the state of the boolean test-param.*" "test show help" | |
125 | gdb_test "help set print test-param" \ | |
126 | "Set the state of the boolean test-param.*" "test set help" | |
127 | gdb_test "help set print" \ | |
128 | "set print test-param -- Set the state of the boolean test-param.*" \ | |
129 | "test general help" | |
130 | } | |
b521dba8 DE |
131 | |
132 | # Test an enum parameter. | |
082cce05 | 133 | with_test_prefix "enum parameter" { |
2a17c803 | 134 | gdb_test_multiline "enum gdb parameter" \ |
082cce05 AB |
135 | "python" "" \ |
136 | "class TestEnumParam (gdb.Parameter):" "" \ | |
137 | " \"\"\"When set, test param does something useful. When disabled, does nothing.\"\"\"" "" \ | |
138 | " show_doc = \"Show the state of the enum\"" ""\ | |
139 | " set_doc = \"Set the state of the enum\"" "" \ | |
140 | " def get_show_string (self, pvalue):" ""\ | |
141 | " return \"The state of the enum is \" + pvalue" ""\ | |
142 | " def get_set_string (self):" ""\ | |
143 | " return \"The state of the enum has been set to \" + self.value" ""\ | |
144 | " def __init__ (self, name):" "" \ | |
145 | " super (TestEnumParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_ENUM, \[\"one\", \"two\"\])" "" \ | |
146 | " self.value = \"one\"" "" \ | |
147 | "test_enum_param = TestEnumParam ('print test-enum-param')" ""\ | |
148 | "end" | |
149 | ||
150 | gdb_test "python print (test_enum_param.value)" "one" \ | |
151 | "test enum parameter value is one" | |
152 | gdb_test "show print test-enum-param" \ | |
153 | "The state of the enum is one.*" \ | |
154 | "show parameter is initial value" | |
155 | gdb_test "set print test-enum-param two" \ | |
156 | "The state of the enum has been set to two" "set enum to two" | |
157 | gdb_test "show print test-enum-param" \ | |
158 | "The state of the enum is two.*" "show parameter is new value" | |
159 | gdb_test "python print (test_enum_param.value)" "two" \ | |
160 | "test enum parameter value is two" | |
161 | gdb_test "set print test-enum-param three" \ | |
162 | "Undefined item: \"three\".*" "set invalid enum parameter" | |
163 | } | |
b521dba8 DE |
164 | |
165 | # Test a file parameter. | |
082cce05 | 166 | with_test_prefix "file parameter" { |
2a17c803 | 167 | gdb_test_multiline "file gdb parameter" \ |
082cce05 AB |
168 | "python" "" \ |
169 | "class TestFileParam (gdb.Parameter):" "" \ | |
170 | " \"\"\"When set, test param does something useful. When disabled, does nothing.\"\"\"" "" \ | |
171 | " show_doc = \"Show the name of the file\"" ""\ | |
172 | " set_doc = \"Set the name of the file\"" "" \ | |
173 | " def get_show_string (self, pvalue):" ""\ | |
174 | " return \"The name of the file is \" + pvalue" ""\ | |
175 | " def get_set_string (self):" ""\ | |
176 | " return \"The name of the file has been changed to \" + self.value" ""\ | |
177 | " def __init__ (self, name):" "" \ | |
178 | " super (TestFileParam, self).__init__ (name, gdb.COMMAND_FILES, gdb.PARAM_FILENAME)" "" \ | |
179 | " self.value = \"foo.txt\"" "" \ | |
180 | "test_file_param = TestFileParam ('test-file-param')" ""\ | |
181 | "end" | |
182 | ||
183 | gdb_test "python print (test_file_param.value)" "foo.txt" \ | |
184 | "test file parameter value" | |
185 | gdb_test "show test-file-param" \ | |
186 | "The name of the file is foo.txt.*" "show initial file value" | |
187 | gdb_test "set test-file-param bar.txt" \ | |
188 | "The name of the file has been changed to bar.txt" \ | |
189 | "set new file parameter" 1 | |
190 | gdb_test "show test-file-param" \ | |
191 | "The name of the file is bar.txt.*" "show new file value" | |
192 | gdb_test "python print (test_file_param.value)" \ | |
193 | "bar.txt" "test new file parameter value" | |
194 | gdb_test "set test-file-param" "Argument required.*" | |
195 | } | |
b521dba8 | 196 | |
ecec24e6 | 197 | # Test a parameter that is not documented. |
082cce05 | 198 | with_test_prefix "undocumented parameter" { |
2a17c803 | 199 | gdb_test_multiline "Simple gdb booleanparameter" \ |
082cce05 AB |
200 | "python" "" \ |
201 | "class TestUndocParam (gdb.Parameter):" "" \ | |
202 | " def get_show_string (self, pvalue):" ""\ | |
203 | " return \"The state of the Test Parameter is \" + pvalue" ""\ | |
204 | " def get_set_string (self):" ""\ | |
205 | " val = \"on\"" ""\ | |
206 | " if (self.value == False):" ""\ | |
207 | " val = \"off\"" ""\ | |
208 | " return \"Test Parameter has been set to \" + val" ""\ | |
209 | " def __init__ (self, name):" "" \ | |
210 | " super (TestUndocParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)" "" \ | |
211 | " self.value = True" "" \ | |
212 | "test_undoc_param = TestUndocParam ('print test-undoc-param')" ""\ | |
213 | "end" | |
214 | ||
215 | gdb_test "show print test-undoc-param" \ | |
216 | "The state of the Test Parameter is on.*" "show parameter on" | |
217 | gdb_test "set print test-undoc-param off" \ | |
218 | "Test Parameter has been set to off" "turn off parameter" | |
219 | gdb_test "show print test-undoc-param" \ | |
220 | "The state of the Test Parameter is off.*" "show parameter off" | |
221 | gdb_test "python print (test_undoc_param.value)" \ | |
222 | "False" "test undocumented parameter value is False" | |
223 | gdb_test "help show print test-undoc-param" \ | |
224 | "This command is not documented.*" "test show help" | |
225 | gdb_test "help set print test-undoc-param" \ | |
226 | "This command is not documented.*" "test set help" | |
227 | gdb_test "help set print" \ | |
228 | "set print test-undoc-param -- This command is not documented.*" \ | |
229 | "test general help" | |
230 | } | |
ecec24e6 PM |
231 | |
232 | # Test a parameter that is not documented in any way.. | |
082cce05 | 233 | with_test_prefix "really undocumented parameter" { |
2a17c803 | 234 | gdb_test_multiline "Simple gdb booleanparameter" \ |
082cce05 AB |
235 | "python" "" \ |
236 | "class TestNodocParam (gdb.Parameter):" "" \ | |
237 | " def __init__ (self, name):" "" \ | |
238 | " super (TestNodocParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)" "" \ | |
239 | " self.value = True" "" \ | |
240 | "test_nodoc_param = TestNodocParam ('print test-nodoc-param')" ""\ | |
241 | "end" | |
242 | ||
243 | gdb_test "show print test-nodoc-param" \ | |
244 | "This command is not documented.*" "show parameter on" | |
245 | gdb_test_no_output "set print test-nodoc-param off" \ | |
246 | "turn off parameter" | |
247 | gdb_test "show print test-nodoc-param" \ | |
248 | "This command is not documented.*.*" "show parameter off" | |
249 | gdb_test "python print (test_nodoc_param.value)" \ | |
250 | "False" "test really undocumented parameter value is False" | |
251 | gdb_test "help show print test-nodoc-param" \ | |
252 | "This command is not documented.*" "test show help" | |
253 | gdb_test "help set print test-nodoc-param" \ | |
254 | "This command is not documented.*" "test set help" | |
255 | gdb_test "help set print" \ | |
256 | "set print test-nodoc-param -- This command is not documented.*" \ | |
257 | "test general help" | |
258 | } | |
ecec24e6 PM |
259 | |
260 | # Test deprecated API. Do not use in your own implementations. | |
082cce05 | 261 | with_test_prefix "deprecated API parameter" { |
2a17c803 | 262 | gdb_test_multiline "Simple gdb booleanparameter" \ |
082cce05 AB |
263 | "python" "" \ |
264 | "class TestParam (gdb.Parameter):" "" \ | |
265 | " \"\"\"When enabled, test param does something useful. When disabled, does nothing.\"\"\"" "" \ | |
266 | " show_doc = \"State of the Test Parameter\"" ""\ | |
267 | " set_doc = \"Set the state of the Test Parameter\"" "" \ | |
268 | " def __init__ (self, name):" "" \ | |
269 | " super (TestParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)" "" \ | |
270 | " self.value = True" "" \ | |
271 | "test_param = TestParam ('print test-param')" ""\ | |
272 | "end" | |
273 | ||
274 | gdb_test "python print (test_param.value)" "True" \ | |
275 | "test deprecated API parameter value is True" | |
276 | gdb_test "show print test-param" \ | |
277 | "State of the Test Parameter on.*" "show parameter on" | |
278 | gdb_test_no_output "set print test-param off" "turn off parameter" | |
279 | gdb_test "show print test-param" \ | |
280 | "State of the Test Parameter off.*" "show parameter off" | |
281 | gdb_test "python print (test_param.value)" "False" \ | |
282 | "test deprecated API parameter value is False" | |
283 | gdb_test "help show print test-param" \ | |
284 | "State of the Test Parameter.*" "test show help" | |
285 | gdb_test "help set print test-param" \ | |
286 | "Set the state of the Test Parameter.*" "test set help" | |
287 | gdb_test "help set print" \ | |
288 | "set print test-param -- Set the state of the Test Parameter.*" \ | |
289 | "test general help" | |
290 | } | |
0489430a TT |
291 | |
292 | foreach kind {PARAM_ZUINTEGER PARAM_ZUINTEGER_UNLIMITED} { | |
2a17c803 | 293 | gdb_test_multiline "Simple gdb $kind" \ |
0489430a TT |
294 | "python" "" \ |
295 | "class TestNodocParam (gdb.Parameter):" "" \ | |
296 | " def __init__ (self, name):" "" \ | |
297 | " super (TestNodocParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.$kind)" "" \ | |
298 | " self.value = 0" "" \ | |
299 | "test_param_$kind = TestNodocParam ('test-$kind')" "" \ | |
300 | "end" | |
301 | ||
302 | gdb_test "python print(gdb.parameter('test-$kind'))" "0" | |
303 | ||
304 | gdb_test "python test_param_$kind.value = -5" "RuntimeError: Range exceeded.*" | |
305 | ||
306 | if {$kind == "PARAM_ZUINTEGER"} { | |
307 | gdb_test "python test_param_$kind.value = -1" "RuntimeError: Range exceeded.*" | |
308 | } else { | |
77d3c63b TT |
309 | gdb_test_no_output "python test_param_$kind.value = -1" \ |
310 | "check that PARAM_ZUINTEGER value can be set to -1" | |
0489430a TT |
311 | gdb_test "python print(gdb.parameter('test-$kind'))" "-1" \ |
312 | "check that PARAM_ZUINTEGER value is -1 after setting" | |
313 | } | |
314 | } | |
ae778caf | 315 | |
2a17c803 | 316 | gdb_test_multiline "Throwing gdb parameter" \ |
ae778caf TT |
317 | "python" "" \ |
318 | "class TestThrowParam (gdb.Parameter):" "" \ | |
319 | " def __init__ (self, name):" "" \ | |
320 | " super (TestThrowParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_STRING)" "" \ | |
321 | " self.value = True" "" \ | |
322 | " def get_set_string (self):" "" \ | |
323 | " raise gdb.GdbError('Ordinary gdb error')" "" \ | |
324 | "test_throw_param = TestThrowParam ('print test-throw-param')" ""\ | |
325 | "end" | |
326 | ||
327 | gdb_test "set print test-throw-param whoops" \ | |
328 | "Ordinary gdb error" \ | |
329 | "gdb.GdbError does not show Python stack" |