]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/py-cmd.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-cmd.exp
1 # Copyright (C) 2009-2023 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. It tests the mechanism
17 # for defining new GDB commands in Python.
18
19 load_lib gdb-python.exp
20
21 standard_testfile
22
23 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
24 return -1
25 }
26
27 # Skip all tests if Python scripting is not enabled.
28 if { [skip_python_tests] } { continue }
29
30 if {![runto_main]} {
31 return 0
32 }
33
34 # Test a simple command.
35
36 gdb_test_multiline "input simple command" \
37 "python" "" \
38 "class test_cmd (gdb.Command):" "" \
39 " def __init__ (self):" "" \
40 " super (test_cmd, self).__init__ (\"test_cmd\", gdb.COMMAND_OBSCURE)" "" \
41 " def invoke (self, arg, from_tty):" "" \
42 " print (\"test_cmd output, arg = %s\" % arg)" "" \
43 "test_cmd ()" "" \
44 "end" ""
45
46 gdb_test "test_cmd ugh" "test_cmd output, arg = ugh" "call simple command"
47
48 # Test a prefix command, and a subcommand within it.
49
50 gdb_test_multiline "input prefix command" \
51 "python" "" \
52 "class prefix_cmd (gdb.Command):" "" \
53 " def __init__ (self):" "" \
54 " super (prefix_cmd, self).__init__ (\"prefix_cmd\", gdb.COMMAND_OBSCURE, gdb.COMPLETE_NONE, True)" "" \
55 " def invoke (self, arg, from_tty):" "" \
56 " print (\"prefix_cmd output, arg = %s\" % arg)" "" \
57 "prefix_cmd ()" "" \
58 "end" ""
59
60 gdb_test "prefix_cmd ugh" "prefix_cmd output, arg = ugh" "call prefix command"
61
62 gdb_test_multiline "input subcommand" \
63 "python" "" \
64 "class subcmd (gdb.Command):" "" \
65 " def __init__ (self):" "" \
66 " super (subcmd, self).__init__ (\"prefix_cmd subcmd\", gdb.COMMAND_OBSCURE)" "" \
67 " def invoke (self, arg, from_tty):" "" \
68 " print (\"subcmd output, arg = %s\" % arg)" "" \
69 "subcmd ()" "" \
70 "end" ""
71
72 gdb_test "prefix_cmd subcmd ugh" "subcmd output, arg = ugh" "call subcmd"
73
74 # Test prefix command using keyword arguments.
75
76 gdb_test_multiline "input prefix command, keyword arguments" \
77 "python" "" \
78 "class prefix_cmd2 (gdb.Command):" "" \
79 " def __init__ (self):" "" \
80 " super (prefix_cmd2, self).__init__ (\"prefix_cmd2\", gdb.COMMAND_OBSCURE, prefix = True, completer_class = gdb.COMPLETE_FILENAME)" "" \
81 " def invoke (self, arg, from_tty):" "" \
82 " print (\"prefix_cmd2 output, arg = %s\" % arg)" "" \
83 "prefix_cmd2 ()" "" \
84 "end" ""
85
86 gdb_test "prefix_cmd2 argh" "prefix_cmd2 output, arg = argh" "call prefix command, keyword arguments"
87
88 gdb_test_multiline "input subcommand under prefix_cmd2" \
89 "python" "" \
90 "class subcmd (gdb.Command):" "" \
91 " def __init__ (self):" "" \
92 " super (subcmd, self).__init__ (\"prefix_cmd2 subcmd\", gdb.COMMAND_OBSCURE)" "" \
93 " def invoke (self, arg, from_tty):" "" \
94 " print (\"subcmd output, arg = %s\" % arg)" "" \
95 "subcmd ()" "" \
96 "end" ""
97
98 gdb_test "prefix_cmd2 subcmd ugh" "subcmd output, arg = ugh" "call subcmd under prefix_cmd2"
99
100 # Test a subcommand in an existing GDB prefix.
101
102 gdb_test_multiline "input new subcommand" \
103 "python" "" \
104 "class newsubcmd (gdb.Command):" "" \
105 " def __init__ (self):" "" \
106 " super (newsubcmd, self).__init__ (\"info newsubcmd\", gdb.COMMAND_OBSCURE)" "" \
107 " def invoke (self, arg, from_tty):" "" \
108 " print (\"newsubcmd output, arg = %s\" % arg)" "" \
109 "newsubcmd ()" "" \
110 "end" ""
111
112 gdb_test "info newsubcmd ugh" "newsubcmd output, arg = ugh" "call newsubcmd"
113
114 # Test a command that throws gdb.GdbError.
115
116 gdb_test_multiline "input command to throw error" \
117 "python" "" \
118 "class test_error_cmd (gdb.Command):" "" \
119 " def __init__ (self):" "" \
120 " super (test_error_cmd, self).__init__ (\"test_error_cmd\", gdb.COMMAND_OBSCURE)" "" \
121 " def invoke (self, arg, from_tty):" "" \
122 " raise gdb.GdbError ('you lose!')" "" \
123 "test_error_cmd ()" "" \
124 "end" ""
125
126 gdb_test "test_error_cmd ugh" "you lose!" "call error command"
127
128 # Test gdb.string_to_argv.
129
130 gdb_test "python print (gdb.string_to_argv (\"1 2 3\"))" \
131 {\['1', '2', '3'\]} \
132 "string_to_argv (\"1 2 3\")"
133
134 gdb_test "python print (gdb.string_to_argv (\"'1 2' 3\"))" \
135 {\['1 2', '3'\]} \
136 "string_to_argv (\"'1 2' 3\")"
137
138 gdb_test "python print (gdb.string_to_argv ('\"1 2\" 3'))" \
139 {\['1 2', '3'\]} \
140 "string_to_argv ('\"1 2\" 3')"
141
142 gdb_test "python print (gdb.string_to_argv ('1\\ 2 3'))" \
143 {\['1 2', '3'\]} \
144 "string_to_argv ('1\\ 2 3')"
145
146 # Test user-defined python commands.
147 gdb_test_multiline "input simple user-defined command" \
148 "python" "" \
149 "class test_help (gdb.Command):" "" \
150 " \"\"\"Docstring\"\"\"" "" \
151 " def __init__ (self):" "" \
152 " super (test_help, self).__init__ (\"test_help\", gdb.COMMAND_USER)" "" \
153 " def invoke (self, arg, from_tty):" "" \
154 " print (\"test_cmd output, arg = %s\" % arg)" "" \
155 "test_help ()" "" \
156 "end" ""
157
158 gdb_test "test_help ugh" "test_cmd output, arg = ugh" "call simple user-defined command"
159
160 # Make sure the command shows up in `help user-defined`.
161 test_user_defined_class_help {"test_help -- Docstring[\r\n]"}
162
163 # Make sure the command does not show up in `show user`.
164 gdb_test "show user test_help" "Not a user command\." \
165 "don't show user-defined python command in `show user command`"
166
167 # Test expression completion on fields
168 gdb_test_multiline "expression completion command" \
169 "python" "" \
170 "class expr_test (gdb.Command):" "" \
171 " def __init__ (self):" "" \
172 " super (expr_test, self).__init__ (\"expr_test\", gdb.COMMAND_USER, gdb.COMPLETE_EXPRESSION)" "" \
173 " def invoke (self, arg, from_tty):" "" \
174 " print (\"invoked on = %s\" % arg)" "" \
175 "expr_test ()" "" \
176 "end" ""
177
178
179 gdb_test "complete expr_test bar\." \
180 "expr_test bar\.bc.*expr_test bar\.ij.*" \
181 "test completion through complete command"
182
183 # Test that the "python" command is correctly recognized as
184 # inline/multi-line when entering a sequence of commands.
185 #
186 # This proc tests PR cli/21688. The PR is not language-specific, but
187 # the easiest way is just to test with Python.
188 proc test_python_inline_or_multiline { } {
189 global gdb_prompt
190 set end "\r\n$gdb_prompt $"
191
192 set define_cmd_not_inline [ list \
193 [ list "if 1" " >$" "multi-line if 1" ] \
194 [ list "python" " >$" "multi-line python command" ] \
195 [ list "print ('hello')" " >$" "multi-line print" ] \
196 [ list "end" " >$" "multi-line first end" ] \
197 [ list "end" "hello$end" "multi-line last end" ] ]
198
199 # This also tests trailing whitespace on the command.
200 set define_cmd_alias_not_inline [ list \
201 [ list "if 1" " >$" "multi-line if 1 alias" ] \
202 [ list "py " " >$" "multi-line python command alias" ] \
203 [ list "print ('hello')" " >$" "multi-line print alias" ] \
204 [ list "end" " >$" "multi-line first end alias" ] \
205 [ list "end" "hello$end" "multi-line last end alias" ] ]
206
207 set define_cmd_alias_foo_not_inline [ list \
208 [ list "alias foo=python" "$end" "multi-line alias foo" ] \
209 [ list "if 1" " >$" "multi-line if 1 alias foo" ] \
210 [ list "foo " " >$" "multi-line python command alias foo" ] \
211 [ list "print ('hello')" " >$" "multi-line print alias foo" ] \
212 [ list "end" " >$" "multi-line first end alias foo" ] \
213 [ list "end" "hello$end" "multi-line last end alias foo" ] ]
214
215 set define_cmd_inline [ list \
216 [ list "if 1" " >$" "inline if 1" ] \
217 [ list "python print ('hello')" " >$" "inline python command" ] \
218 [ list "end" "hello$end" "inline end" ] ]
219
220 set define_cmd_alias_inline [ list \
221 [ list "if 1" " >$" "inline if 1 alias" ] \
222 [ list "py print ('hello')" " >$" "inline python command alias" ] \
223 [ list "end" "hello$end" "inline end alias" ] ]
224
225 set define_cmd_alias_foo_inline [ list \
226 [ list "if 1" " >$" "inline if 1 alias foo" ] \
227 [ list "foo print ('hello')" " >$" "inline python command alias foo" ] \
228 [ list "end" "hello$end" "inline end alias foo" ] ]
229
230 foreach t [list $define_cmd_not_inline \
231 $define_cmd_alias_not_inline \
232 $define_cmd_alias_foo_not_inline \
233 $define_cmd_inline \
234 $define_cmd_alias_inline \
235 $define_cmd_alias_foo_inline] {
236 foreach l $t {
237 lassign $l command regex testmsg
238 gdb_test_multiple "$command" "$testmsg" {
239 -re "$regex" {
240 pass "$testmsg"
241 }
242 }
243 }
244 }
245 }
246
247 test_python_inline_or_multiline
248
249 if { [readline_is_used] } {
250 set test "complete 'expr_test bar.i'"
251 send_gdb "expr_test bar\.i\t\t"
252 gdb_test_multiple "" "$test" {
253 -re "expr_test bar\.ij \\\x07$" {
254 send_gdb "\n"
255 gdb_test_multiple "" $test {
256 -re "invoked on = bar.ij.*$gdb_prompt $" {
257 pass "$test"
258 }
259 }
260 }
261 }
262 }
263
264
265 # Test that interrupting pagination throws a gdb quit.
266 gdb_test_no_output "set height 10"
267
268 gdb_test_multiline "input multi-line-output command" \
269 "python" "" \
270 "class test_mline (gdb.Command):" "" \
271 " \"\"\"Docstring\"\"\"" "" \
272 " def __init__ (self):" "" \
273 " super (test_mline, self).__init__ (\"test_multiline\", gdb.COMMAND_USER)" "" \
274 " def invoke (self, arg, from_tty):" "" \
275 " for f in range(20):" "" \
276 " print (\"test_multiline output\")" "" \
277 "test_mline ()" "" \
278 "end" ""
279
280 set test "verify pagination from test_multiline"
281 gdb_test_multiple "test_multiline" $test {
282 -re "--Type <RET>" {
283 exp_continue
284 }
285 -re " for more, q to quit" {
286 exp_continue
287 }
288 -re ", c to continue without paging--$" {
289 pass $test
290 }
291 }
292
293 send_gdb "q\n"
294 set test "verify pagination from test_multiline: q"
295 gdb_test_multiple "test_multiline" $test {
296 -re "Error occurred in Python" {
297 fail $test
298 }
299 -re "Quit" {
300 pass $test
301 }
302 }
303
304 # Test command redefining itself
305
306 proc_with_prefix test_command_redefining_itself {} {
307 # Start with a fresh gdb
308 clean_restart
309
310
311 gdb_test_multiline "input command redefining itself" \
312 "python" "" \
313 "class redefine_cmd (gdb.Command):" "" \
314 " def __init__ (self, msg):" "" \
315 " super (redefine_cmd, self).__init__ (\"redefine_cmd\", gdb.COMMAND_OBSCURE)" "" \
316 " self._msg = msg" "" \
317 " def invoke (self, arg, from_tty):" "" \
318 " print (\"redefine_cmd output, msg = %s\" % self._msg)" "" \
319 " redefine_cmd (arg)" "" \
320 "redefine_cmd (\"XXX\")" "" \
321 "end" ""
322
323 gdb_test "redefine_cmd AAA" \
324 "redefine_cmd output, msg = XXX" \
325 "call command redefining itself 1"
326
327 gdb_test "redefine_cmd BBB" \
328 "redefine_cmd output, msg = AAA" \
329 "call command redefining itself 2"
330 }
331
332 test_command_redefining_itself