1 # Copyright (C) 2009-2020 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file is part of the GDB testsuite. It tests the mechanism
17 # for defining new GDB commands in Python.
19 load_lib gdb-python.exp
23 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
27 # Skip all tests if Python scripting is not enabled.
28 if { [skip_python_tests] } { continue }
30 if ![runto_main] then {
31 fail "cannot run to main."
35 # Test a simple command.
37 gdb_py_test_multiple "input simple command" \
39 "class test_cmd (gdb.Command):" "" \
40 " def __init__ (self):" "" \
41 " super (test_cmd, self).__init__ (\"test_cmd\", gdb.COMMAND_OBSCURE)" "" \
42 " def invoke (self, arg, from_tty):" "" \
43 " print (\"test_cmd output, arg = %s\" % arg)" "" \
47 gdb_test "test_cmd ugh" "test_cmd output, arg = ugh" "call simple command"
49 # Test a prefix command, and a subcommand within it.
51 gdb_py_test_multiple "input prefix command" \
53 "class prefix_cmd (gdb.Command):" "" \
54 " def __init__ (self):" "" \
55 " super (prefix_cmd, self).__init__ (\"prefix_cmd\", gdb.COMMAND_OBSCURE, gdb.COMPLETE_NONE, True)" "" \
56 " def invoke (self, arg, from_tty):" "" \
57 " print (\"prefix_cmd output, arg = %s\" % arg)" "" \
61 gdb_test "prefix_cmd ugh" "prefix_cmd output, arg = ugh" "call prefix command"
63 gdb_py_test_multiple "input subcommand" \
65 "class subcmd (gdb.Command):" "" \
66 " def __init__ (self):" "" \
67 " super (subcmd, self).__init__ (\"prefix_cmd subcmd\", gdb.COMMAND_OBSCURE)" "" \
68 " def invoke (self, arg, from_tty):" "" \
69 " print (\"subcmd output, arg = %s\" % arg)" "" \
73 gdb_test "prefix_cmd subcmd ugh" "subcmd output, arg = ugh" "call subcmd"
75 # Test prefix command using keyword arguments.
77 gdb_py_test_multiple "input prefix command, keyword arguments" \
79 "class prefix_cmd2 (gdb.Command):" "" \
80 " def __init__ (self):" "" \
81 " super (prefix_cmd2, self).__init__ (\"prefix_cmd2\", gdb.COMMAND_OBSCURE, prefix = True, completer_class = gdb.COMPLETE_FILENAME)" "" \
82 " def invoke (self, arg, from_tty):" "" \
83 " print (\"prefix_cmd2 output, arg = %s\" % arg)" "" \
87 gdb_test "prefix_cmd2 argh" "prefix_cmd2 output, arg = argh" "call prefix command, keyword arguments"
89 gdb_py_test_multiple "input subcommand under prefix_cmd2" \
91 "class subcmd (gdb.Command):" "" \
92 " def __init__ (self):" "" \
93 " super (subcmd, self).__init__ (\"prefix_cmd2 subcmd\", gdb.COMMAND_OBSCURE)" "" \
94 " def invoke (self, arg, from_tty):" "" \
95 " print (\"subcmd output, arg = %s\" % arg)" "" \
99 gdb_test "prefix_cmd2 subcmd ugh" "subcmd output, arg = ugh" "call subcmd under prefix_cmd2"
101 # Test a subcommand in an existing GDB prefix.
103 gdb_py_test_multiple "input new subcommand" \
105 "class newsubcmd (gdb.Command):" "" \
106 " def __init__ (self):" "" \
107 " super (newsubcmd, self).__init__ (\"info newsubcmd\", gdb.COMMAND_OBSCURE)" "" \
108 " def invoke (self, arg, from_tty):" "" \
109 " print (\"newsubcmd output, arg = %s\" % arg)" "" \
113 gdb_test "info newsubcmd ugh" "newsubcmd output, arg = ugh" "call newsubcmd"
115 # Test a command that throws gdb.GdbError.
117 gdb_py_test_multiple "input command to throw error" \
119 "class test_error_cmd (gdb.Command):" "" \
120 " def __init__ (self):" "" \
121 " super (test_error_cmd, self).__init__ (\"test_error_cmd\", gdb.COMMAND_OBSCURE)" "" \
122 " def invoke (self, arg, from_tty):" "" \
123 " raise gdb.GdbError ('you lose!')" "" \
124 "test_error_cmd ()" "" \
127 gdb_test "test_error_cmd ugh" "you lose!" "call error command"
129 # Test gdb.string_to_argv.
131 gdb_test "python print (gdb.string_to_argv (\"1 2 3\"))" \
132 {\['1', '2', '3'\]} \
133 "string_to_argv (\"1 2 3\")"
135 gdb_test "python print (gdb.string_to_argv (\"'1 2' 3\"))" \
137 "string_to_argv (\"'1 2' 3\")"
139 gdb_test "python print (gdb.string_to_argv ('\"1 2\" 3'))" \
141 "string_to_argv ('\"1 2\" 3')"
143 gdb_test "python print (gdb.string_to_argv ('1\\ 2 3'))" \
145 "string_to_argv ('1\\ 2 3')"
147 # Test user-defined python commands.
148 gdb_py_test_multiple "input simple user-defined command" \
150 "class test_help (gdb.Command):" "" \
151 " \"\"\"Docstring\"\"\"" "" \
152 " def __init__ (self):" "" \
153 " super (test_help, self).__init__ (\"test_help\", gdb.COMMAND_USER)" "" \
154 " def invoke (self, arg, from_tty):" "" \
155 " print (\"test_cmd output, arg = %s\" % arg)" "" \
159 gdb_test "test_help ugh" "test_cmd output, arg = ugh" "call simple user-defined command"
161 # Make sure the command shows up in `help user-defined`.
162 test_user_defined_class_help {"test_help -- Docstring[\r\n]"}
164 # Make sure the command does not show up in `show user`.
165 gdb_test "show user test_help" "Not a user command\." \
166 "don't show user-defined python command in `show user command`"
168 # Test expression completion on fields
169 gdb_py_test_multiple "expression completion command" \
171 "class expr_test (gdb.Command):" "" \
172 " def __init__ (self):" "" \
173 " super (expr_test, self).__init__ (\"expr_test\", gdb.COMMAND_USER, gdb.COMPLETE_EXPRESSION)" "" \
174 " def invoke (self, arg, from_tty):" "" \
175 " print (\"invoked on = %s\" % arg)" "" \
180 gdb_test "complete expr_test bar\." \
181 "expr_test bar\.bc.*expr_test bar\.ij.*" \
182 "test completion through complete command"
184 # Test that the "python" command is correctly recognized as
185 # inline/multi-line when entering a sequence of commands.
187 # This proc tests PR cli/21688. The PR is not language-specific, but
188 # the easiest way is just to test with Python.
189 proc test_python_inline_or_multiline { } {
191 set end "\r\n$gdb_prompt $"
193 set define_cmd_not_inline [ list \
194 [ list "if 1" " >$" "multi-line if 1" ] \
195 [ list "python" " >$" "multi-line python command" ] \
196 [ list "print ('hello')" " >$" "multi-line print" ] \
197 [ list "end" " >$" "multi-line first end" ] \
198 [ list "end" "hello$end" "multi-line last end" ] ]
200 # This also tests trailing whitespace on the command.
201 set define_cmd_alias_not_inline [ list \
202 [ list "if 1" " >$" "multi-line if 1 alias" ] \
203 [ list "py " " >$" "multi-line python command alias" ] \
204 [ list "print ('hello')" " >$" "multi-line print alias" ] \
205 [ list "end" " >$" "multi-line first end alias" ] \
206 [ list "end" "hello$end" "multi-line last end alias" ] ]
208 set define_cmd_alias_foo_not_inline [ list \
209 [ list "alias foo=python" "$end" "multi-line alias foo" ] \
210 [ list "if 1" " >$" "multi-line if 1 alias foo" ] \
211 [ list "foo " " >$" "multi-line python command alias foo" ] \
212 [ list "print ('hello')" " >$" "multi-line print alias foo" ] \
213 [ list "end" " >$" "multi-line first end alias foo" ] \
214 [ list "end" "hello$end" "multi-line last end alias foo" ] ]
216 set define_cmd_inline [ list \
217 [ list "if 1" " >$" "inline if 1" ] \
218 [ list "python print ('hello')" " >$" "inline python command" ] \
219 [ list "end" "hello$end" "inline end" ] ]
221 set define_cmd_alias_inline [ list \
222 [ list "if 1" " >$" "inline if 1 alias" ] \
223 [ list "py print ('hello')" " >$" "inline python command alias" ] \
224 [ list "end" "hello$end" "inline end alias" ] ]
226 set define_cmd_alias_foo_inline [ list \
227 [ list "if 1" " >$" "inline if 1 alias foo" ] \
228 [ list "foo print ('hello')" " >$" "inline python command alias foo" ] \
229 [ list "end" "hello$end" "inline end alias foo" ] ]
231 foreach t [list $define_cmd_not_inline \
232 $define_cmd_alias_not_inline \
233 $define_cmd_alias_foo_not_inline \
235 $define_cmd_alias_inline \
236 $define_cmd_alias_foo_inline] {
238 lassign $l command regex testmsg
239 gdb_test_multiple "$command" "$testmsg" {
248 test_python_inline_or_multiline
250 if { [readline_is_used] } {
251 set test "complete 'expr_test bar.i'"
252 send_gdb "expr_test bar\.i\t\t"
253 gdb_test_multiple "" "$test" {
254 -re "expr_test bar\.ij \\\x07$" {
256 gdb_test_multiple "" $test {
257 -re "invoked on = bar.ij.*$gdb_prompt $" {
266 # Test that interrupting pagination throws a gdb quit.
267 gdb_test_no_output "set height 10"
269 gdb_py_test_multiple "input multi-line-output command" \
271 "class test_mline (gdb.Command):" "" \
272 " \"\"\"Docstring\"\"\"" "" \
273 " def __init__ (self):" "" \
274 " super (test_mline, self).__init__ (\"test_multiline\", gdb.COMMAND_USER)" "" \
275 " def invoke (self, arg, from_tty):" "" \
276 " for f in range(20):" "" \
277 " print (\"test_multiline output\")" "" \
281 set test "verify pagination from test_multiline"
282 gdb_test_multiple "test_multiline" $test {
286 -re " for more, q to quit" {
289 -re ", c to continue without paging--$" {
295 set test "verify pagination from test_multiline: q"
296 gdb_test_multiple "test_multiline" $test {
297 -re "Error occurred in Python" {