Commit | Line | Data |
---|---|---|
61baf725 | 1 | # Copyright (C) 2009-2017 Free Software Foundation, Inc. |
6db7e351 TJB |
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 | ||
f39a75d0 | 19 | load_lib gdb-python.exp |
6db7e351 | 20 | |
92e32e33 | 21 | standard_testfile |
6db7e351 | 22 | |
5b362f04 | 23 | if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } { |
92e32e33 PM |
24 | return -1 |
25 | } | |
6db7e351 | 26 | |
f6bbabf0 PM |
27 | # Skip all tests if Python scripting is not enabled. |
28 | if { [skip_python_tests] } { continue } | |
6db7e351 | 29 | |
92e32e33 | 30 | if ![runto_main] then { |
bc6c7af4 | 31 | fail "cannot run to main." |
92e32e33 PM |
32 | return 0 |
33 | } | |
34 | ||
6db7e351 TJB |
35 | # Test a simple command. |
36 | ||
37 | gdb_py_test_multiple "input simple command" \ | |
38 | "python" "" \ | |
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):" "" \ | |
9325cb04 | 43 | " print (\"test_cmd output, arg = %s\" % arg)" "" \ |
6db7e351 TJB |
44 | "test_cmd ()" "" \ |
45 | "end" "" | |
46 | ||
47 | gdb_test "test_cmd ugh" "test_cmd output, arg = ugh" "call simple command" | |
48 | ||
49 | # Test a prefix command, and a subcommand within it. | |
50 | ||
51 | gdb_py_test_multiple "input prefix command" \ | |
52 | "python" "" \ | |
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):" "" \ | |
9325cb04 | 57 | " print (\"prefix_cmd output, arg = %s\" % arg)" "" \ |
6db7e351 TJB |
58 | "prefix_cmd ()" "" \ |
59 | "end" "" | |
60 | ||
61 | gdb_test "prefix_cmd ugh" "prefix_cmd output, arg = ugh" "call prefix command" | |
62 | ||
63 | gdb_py_test_multiple "input subcommand" \ | |
64 | "python" "" \ | |
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):" "" \ | |
9325cb04 | 69 | " print (\"subcmd output, arg = %s\" % arg)" "" \ |
6db7e351 TJB |
70 | "subcmd ()" "" \ |
71 | "end" "" | |
72 | ||
73 | gdb_test "prefix_cmd subcmd ugh" "subcmd output, arg = ugh" "call subcmd" | |
74 | ||
cc924cad TJB |
75 | # Test prefix command using keyword arguments. |
76 | ||
77 | gdb_py_test_multiple "input prefix command, keyword arguments" \ | |
78 | "python" "" \ | |
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):" "" \ | |
9325cb04 | 83 | " print (\"prefix_cmd2 output, arg = %s\" % arg)" "" \ |
cc924cad TJB |
84 | "prefix_cmd2 ()" "" \ |
85 | "end" "" | |
86 | ||
87 | gdb_test "prefix_cmd2 argh" "prefix_cmd2 output, arg = argh" "call prefix command, keyword arguments" | |
88 | ||
89 | gdb_py_test_multiple "input subcommand under prefix_cmd2" \ | |
90 | "python" "" \ | |
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):" "" \ | |
9325cb04 | 95 | " print (\"subcmd output, arg = %s\" % arg)" "" \ |
cc924cad TJB |
96 | "subcmd ()" "" \ |
97 | "end" "" | |
98 | ||
99 | gdb_test "prefix_cmd2 subcmd ugh" "subcmd output, arg = ugh" "call subcmd under prefix_cmd2" | |
100 | ||
6db7e351 TJB |
101 | # Test a subcommand in an existing GDB prefix. |
102 | ||
103 | gdb_py_test_multiple "input new subcommand" \ | |
104 | "python" "" \ | |
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):" "" \ | |
9325cb04 | 109 | " print (\"newsubcmd output, arg = %s\" % arg)" "" \ |
6db7e351 TJB |
110 | "newsubcmd ()" "" \ |
111 | "end" "" | |
112 | ||
113 | gdb_test "info newsubcmd ugh" "newsubcmd output, arg = ugh" "call newsubcmd" | |
07ca107c DE |
114 | |
115 | # Test a command that throws gdb.GdbError. | |
116 | ||
117 | gdb_py_test_multiple "input command to throw error" \ | |
118 | "python" "" \ | |
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 ()" "" \ | |
125 | "end" "" | |
126 | ||
127 | gdb_test "test_error_cmd ugh" "you lose!" "call error command" | |
128 | ||
129 | # Test gdb.string_to_argv. | |
130 | ||
9325cb04 | 131 | gdb_test "python print (gdb.string_to_argv (\"1 2 3\"))" \ |
07ca107c DE |
132 | {\['1', '2', '3'\]} \ |
133 | "string_to_argv (\"1 2 3\")" | |
134 | ||
9325cb04 | 135 | gdb_test "python print (gdb.string_to_argv (\"'1 2' 3\"))" \ |
07ca107c DE |
136 | {\['1 2', '3'\]} \ |
137 | "string_to_argv (\"'1 2' 3\")" | |
138 | ||
9325cb04 | 139 | gdb_test "python print (gdb.string_to_argv ('\"1 2\" 3'))" \ |
07ca107c DE |
140 | {\['1 2', '3'\]} \ |
141 | "string_to_argv ('\"1 2\" 3')" | |
142 | ||
9325cb04 | 143 | gdb_test "python print (gdb.string_to_argv ('1\\ 2 3'))" \ |
07ca107c DE |
144 | {\['1 2', '3'\]} \ |
145 | "string_to_argv ('1\\ 2 3')" | |
7d74f244 DE |
146 | |
147 | # Test user-defined python commands. | |
148 | gdb_py_test_multiple "input simple user-defined command" \ | |
149 | "python" "" \ | |
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):" "" \ | |
9325cb04 | 155 | " print (\"test_cmd output, arg = %s\" % arg)" "" \ |
7d74f244 DE |
156 | "test_help ()" "" \ |
157 | "end" "" | |
158 | ||
159 | gdb_test "test_help ugh" "test_cmd output, arg = ugh" "call simple user-defined command" | |
160 | ||
161 | # Make sure the command shows up in `help user-defined`. | |
162 | gdb_test "help user-defined" "User-defined commands.\[\r\n\]+The commands in this class are those defined by the user.\[\r\n\]+Use the \"define\" command to define a command.\[\r\n\]+\[\r\n\]+List of commands:\[\r\n\]+\[\r\n\]+test_help -- Docstring\[\r\n\]+\[\r\n\]+Type \"help\" followed by command name for full documentation.\[\r\n\]+Type \"apropos word\" to search for commands related to \"word\".\[\r\n\]+Command name abbreviations are allowed if unambiguous.\[\r\n\]+" "see user-defined command in `help user-defined`" | |
92e32e33 | 163 | |
a9f116cb GKB |
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`" | |
167 | ||
92e32e33 PM |
168 | # Test expression completion on fields |
169 | gdb_py_test_multiple "expression completion command" \ | |
170 | "python" "" \ | |
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)" "" \ | |
176 | "expr_test ()" "" \ | |
177 | "end" "" | |
178 | ||
179 | ||
180 | gdb_test "complete expr_test bar\." \ | |
181 | "expr_test bar\.bc.*expr_test bar\.ij.*" \ | |
bb95117e | 182 | "test completion through complete command" |
92e32e33 | 183 | |
51ed89aa SDJ |
184 | # Test that the "python" command is correctly recognized as |
185 | # inline/multi-line when entering a sequence of commands. | |
186 | # | |
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 { } { | |
dc4bde35 SDJ |
190 | global gdb_prompt |
191 | ||
51ed89aa SDJ |
192 | set define_cmd_not_inline { |
193 | { "if 1" " >$" "multi-line if 1" } | |
194 | { "python" " >$" "multi-line python command" } | |
195 | { "print ('hello')" " >$" "multi-line print" } | |
196 | { "end" " >$" "multi-line first end" } | |
197 | { "end" "hello\r\n" "multi-line last end" } } | |
198 | ||
dc4bde35 SDJ |
199 | # This also tests trailing whitespace on the command. |
200 | set define_cmd_alias_not_inline { | |
201 | { "if 1" " >$" "multi-line if 1 alias" } | |
202 | { "py " " >$" "multi-line python command alias" } | |
203 | { "print ('hello')" " >$" "multi-line print alias" } | |
204 | { "end" " >$" "multi-line first end alias" } | |
205 | { "end" "hello\r\n" "multi-line last end alias" } } | |
206 | ||
207 | set define_cmd_alias_foo_not_inline { | |
208 | { "alias foo=python" "\r\n" "multi-line alias foo" } | |
209 | { "if 1" " >$" "multi-line if 1 alias foo" } | |
210 | { "foo " " >$" "multi-line python command alias foo" } | |
211 | { "print ('hello')" " >$" "multi-line print alias foo" } | |
212 | { "end" " >$" "multi-line first end alias foo" } | |
213 | { "end" "hello\r\n" "multi-line last end alias foo" } } | |
214 | ||
51ed89aa SDJ |
215 | set define_cmd_inline { |
216 | { "if 1" " >$" "inline if 1" } | |
217 | { "python print ('hello')" " >$" "inline python command" } | |
218 | { "end" "hello\r\n" "inline end" } } | |
219 | ||
dc4bde35 SDJ |
220 | set define_cmd_alias_inline { |
221 | { "if 1" " >$" "inline if 1 alias" } | |
222 | { "py print ('hello')" " >$" "inline python command alias" } | |
223 | { "end" "hello\r\n" "inline end alias" } } | |
224 | ||
225 | set define_cmd_alias_foo_inline { | |
226 | { "if 1" " >$" "inline if 1 alias foo" } | |
227 | { "foo print ('hello')" " >$" "inline python command alias foo" } | |
228 | { "end" "hello\r\n" "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] { | |
51ed89aa SDJ |
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 | ||
0d4d0e77 YQ |
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 | } | |
92e32e33 PM |
259 | } |
260 | } | |
261 | } | |
262 | } |