Commit | Line | Data |
---|---|---|
7b6bb8da | 1 | # Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
d57a3c85 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 | # exposing values to Python. | |
18 | ||
19 | if $tracelevel then { | |
20 | strace $tracelevel | |
21 | } | |
22 | ||
a2c09bd0 DE |
23 | load_lib gdb-python.exp |
24 | ||
cb2e07a6 PM |
25 | set testfile "python" |
26 | set srcfile ${testfile}.c | |
e7fbb131 | 27 | set srcfile1 ${testfile}-1.c |
cb2e07a6 PM |
28 | set binfile ${objdir}/${subdir}/${testfile} |
29 | ||
e7fbb131 PA |
30 | if { [gdb_compile "${srcdir}/${subdir}/${srcfile} ${srcdir}/${subdir}/${srcfile1}" \ |
31 | ${binfile} executable {debug}] != "" } { | |
cb2e07a6 PM |
32 | untested "Could not compile $binfile." |
33 | return -1 | |
34 | } | |
d57a3c85 | 35 | |
cb2e07a6 | 36 | # Start with a fresh gdb. |
d57a3c85 TJB |
37 | gdb_exit |
38 | gdb_start | |
39 | gdb_reinitialize_dir $srcdir/$subdir | |
40 | ||
cb2e07a6 PM |
41 | # Skip all tests if Python scripting is not enabled. |
42 | if { [skip_python_tests] } { continue } | |
43 | ||
d57a3c85 TJB |
44 | gdb_test_multiple "python print 23" "verify python support" { |
45 | -re "not supported.*$gdb_prompt $" { | |
46 | unsupported "python support is disabled" | |
9e14a9ba JB |
47 | |
48 | # If Python is not supported, verify that sourcing a python script | |
49 | # causes an error. | |
4af31552 TT |
50 | gdb_test "source $srcdir/$subdir/source2.py" \ |
51 | "Error in sourced command file:.*" \ | |
52 | "source source2.py when python disabled" | |
d57a3c85 TJB |
53 | return -1 |
54 | } | |
55 | -re "$gdb_prompt $" {} | |
56 | } | |
57 | ||
d57a3c85 TJB |
58 | gdb_py_test_multiple "multi-line python command" \ |
59 | "python" "" \ | |
60 | "print 23" "" \ | |
61 | "end" "23" | |
62 | ||
63 | gdb_py_test_multiple "show python command" \ | |
64 | "define zzq" "Type commands for definition of .* just \"end\"\\.*" \ | |
65 | "python" "" \ | |
66 | "print 23" "" \ | |
67 | "end" "" \ | |
68 | "end" "" \ | |
adb483fe | 69 | "show user zzq" "User command \"zzq\":.* python.*print 23.* end" |
311a4e6b TJB |
70 | |
71 | gdb_py_test_multiple "indented multi-line python command" \ | |
72 | "python" "" \ | |
73 | "def foo ():" "" \ | |
74 | " print 'hello, world!'" "" \ | |
75 | "foo ()" "" \ | |
76 | "end" "hello, world!" | |
89c73ade | 77 | |
4af31552 | 78 | gdb_test "source $srcdir/$subdir/source2.py" "yes" "source source2.py" |
9e14a9ba | 79 | |
d234ef5c DE |
80 | gdb_test "source -s source2.py" "yes" "source -s source2.py" |
81 | ||
89c73ade TT |
82 | gdb_test "python print gdb.current_objfile()" "None" |
83 | gdb_test "python print gdb.objfiles()" "\\\[\\\]" | |
3bebe2f2 JK |
84 | |
85 | # Test http://bugs.python.org/issue4434 workaround in configure.ac | |
86 | gdb_test "python import itertools; print 'IMPOR'+'TED'" "IMPORTED" "pythonX.Y/lib-dynload/*.so" | |
bc9f0842 TT |
87 | |
88 | gdb_test_no_output \ | |
89 | "python x = gdb.execute('printf \"%d\", 23', to_string = True)" | |
90 | gdb_test "python print x" "23" | |
5da1313b | 91 | |
ca5c20b6 PM |
92 | # Test post_event. |
93 | gdb_py_test_multiple "post event insertion" \ | |
94 | "python" "" \ | |
95 | "someVal = 0" "" \ | |
70ebf4ed | 96 | "class Foo(object):" "" \ |
ca5c20b6 PM |
97 | " def __call__(self):" "" \ |
98 | " global someVal" "" \ | |
99 | " someVal += 1" "" \ | |
100 | "gdb.post_event(Foo())" "" \ | |
101 | "end" "" | |
102 | ||
103 | gdb_test "python print someVal" "1" "test post event execution" | |
104 | gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" "Test non callable class" | |
105 | ||
5da1313b JK |
106 | # Test (no) pagination of the executed command. |
107 | gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.} | |
108 | set lines 10 | |
109 | gdb_test_no_output "set height $lines" | |
110 | ||
111 | set test "verify pagination beforehand" | |
112 | gdb_test_multiple "python print \"\\n\" * $lines" $test { | |
c5cb204f MP |
113 | -re "---Type <return>" { |
114 | exp_continue | |
115 | } | |
116 | -re " to continue, or q <return>" { | |
117 | exp_continue | |
118 | } | |
119 | -re " to quit---$" { | |
5da1313b JK |
120 | pass $test |
121 | } | |
122 | } | |
123 | gdb_test "q" "Quit" "verify pagination beforehand: q" | |
124 | ||
125 | gdb_test "python if gdb.execute('python print \"\\\\n\" * $lines', to_string=True) == \"\\n\" * [expr $lines + 1]: print \"yes\"" "yes" "gdb.execute does not page" | |
126 | ||
127 | set test "verify pagination afterwards" | |
128 | gdb_test_multiple "python print \"\\n\" * $lines" $test { | |
c5cb204f MP |
129 | -re "---Type <return>" { |
130 | exp_continue | |
131 | } | |
132 | -re " to continue, or q <return>" { | |
133 | exp_continue | |
134 | } | |
135 | -re " to quit---$" { | |
5da1313b JK |
136 | pass $test |
137 | } | |
138 | } | |
139 | gdb_test "q" "Quit" "verify pagination afterwards: q" | |
cb2e07a6 | 140 | |
8d4d924b JK |
141 | gdb_test_no_output "set height 0" |
142 | ||
143 | gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout" | |
144 | ||
145 | gdb_test "python print a" ".*aliases -- Aliases of other commands.*" "verify help to uiout" | |
146 | ||
ea976c60 PM |
147 | # Test PR 12212, using InfThread.selected_thread() when no inferior is |
148 | # loaded. | |
149 | gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1 | |
150 | gdb_test "python print nothread == None" "True" "Ensure that no threads are returned" | |
151 | ||
cb2e07a6 PM |
152 | # Start with a fresh gdb. |
153 | clean_restart ${testfile} | |
154 | ||
155 | # The following tests require execution. | |
156 | ||
157 | if ![runto_main] then { | |
158 | fail "Can't run to main" | |
159 | return 0 | |
160 | } | |
161 | ||
162 | runto [gdb_get_line_number "Break to end."] | |
163 | ||
164 | # Test gdb.decode_line. | |
165 | gdb_test "python gdb.decode_line(\"main.c:43\")" \ | |
621c8364 | 166 | "gdb.error: No source file named main.c.*" "test decode_line no source named main" |
cb2e07a6 PM |
167 | |
168 | gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1 | |
169 | gdb_test "python print len(symtab)" "2" "Test decode_line current location" | |
170 | gdb_test "python print symtab\[0\]" "None" "Test decode_line expression parse" | |
171 | gdb_test "python print len(symtab\[1\])" "1" "Test decode_line current location" | |
facde0e5 | 172 | gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python.c.*" "Test decode_line current locationn filename" |
cb2e07a6 PM |
173 | gdb_test "python print symtab\[1\]\[0\].line" "22" "Test decode_line current location line number" |
174 | ||
175 | gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1 | |
176 | gdb_test "python print len(symtab)" "2" "Test decode_line python.c:26 length" | |
177 | gdb_test "python print symtab\[0\]" "if foo" "Test decode_line expression parse" | |
178 | gdb_test "python print len(symtab\[1\])" "1" "Test decode_line python.c:26 length" | |
facde0e5 | 179 | gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python.c.*" "Test decode_line python.c:26 filename" |
cb2e07a6 PM |
180 | gdb_test "python print symtab\[1\]\[0\].line" "26" "Test decode_line python.c:26 line number" |
181 | ||
182 | gdb_test "python gdb.decode_line(\"randomfunc\")" \ | |
621c8364 | 183 | "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc" |
cb2e07a6 PM |
184 | gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1 |
185 | gdb_test "python print len(symtab)" "2" "Test decode_line func1 length" | |
186 | gdb_test "python print len(symtab\[1\])" "1" "Test decode_line func1 length" | |
facde0e5 | 187 | gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python-1.c.*" "Test decode_line func1 filename" |
cb2e07a6 | 188 | gdb_test "python print symtab\[1\]\[0\].line" "19" "Test decode_line func1 line number" |
99c3dc11 PM |
189 | |
190 | # gdb.write | |
191 | gdb_test "python print sys.stderr" ".*__main__.GdbOutputErrorFile instance at.*" "Test stderr location" | |
192 | gdb_test "python print sys.stdout" ".*__main__.GdbOutputFile instance at.*" "Test stdout location" | |
193 | gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "Test default write" | |
194 | gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "Test stderr write" | |
195 | gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "Test stdout write" | |
196 | gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "Test stdlog write" | |
713389e0 | 197 | |
80b6e756 | 198 | # print-stack |
713389e0 | 199 | gdb_test "show python print-stack" \ |
80b6e756 PM |
200 | "The mode of Python stack printing on error is \"message\".*" \ |
201 | "Test print-backtrace show setting. Default is message." | |
202 | gdb_py_test_silent_cmd "set python print-stack full" \ | |
713389e0 PM |
203 | "Test print-backtrace set setting" 1 |
204 | gdb_test "show python print-stack" \ | |
80b6e756 PM |
205 | "The mode of Python stack printing on error is \"full\".*" \ |
206 | "Test print-backtrace show setting to full." | |
d17b6f81 PM |
207 | |
208 | # Test prompt substituion | |
209 | ||
210 | gdb_py_test_multiple "prompt substitution" \ | |
211 | "python" "" \ | |
212 | "someCounter = 0" "" \ | |
213 | "def prompt(current):" "" \ | |
214 | " global someCounter" "" \ | |
215 | " if (current == \"testfake \"):" "" \ | |
216 | " return None" "" \ | |
217 | " someCounter = someCounter + 1" "" \ | |
218 | " return \"py prompt \" + str (someCounter) + \" \"" "" \ | |
219 | "end" "" | |
220 | ||
221 | gdb_py_test_multiple "prompt substitution readline" \ | |
222 | "python" "" \ | |
223 | "pCounter = 0" "" \ | |
224 | "def program_prompt(current):" "" \ | |
225 | " global pCounter" "" \ | |
226 | " if (current == \">\"):" "" \ | |
227 | " pCounter = pCounter + 1" "" \ | |
228 | " return \"python line \" + str (pCounter) + \": \"" "" \ | |
229 | " return None" "" \ | |
230 | "end" "" | |
231 | ||
232 | set newprompt "py prompt 1" | |
233 | set newprompt2 "py prompt 2" | |
234 | set testfake "testfake" | |
235 | ||
236 | gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" { | |
237 | -re "\[\r\n\]$newprompt $" { | |
238 | pass "set hook" | |
239 | } | |
240 | } | |
241 | ||
242 | gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" { | |
243 | -re "\[\r\n\]$testfake $" { | |
244 | pass "set prompt testfake" | |
245 | } | |
246 | } | |
247 | ||
248 | gdb_test_multiple "show prompt" "show testfake prompt" { | |
249 | -re "Gdb's prompt is \"$testfake \"..* $" { | |
250 | pass "show prompt shows guarded prompt" | |
251 | } | |
252 | } | |
253 | ||
254 | gdb_test_multiple "set prompt blah " "set blah in GDB" { | |
255 | -re "\[\r\n\]$newprompt2 $" { | |
256 | pass "set prompt blah overriden" | |
257 | } | |
258 | } | |
259 | ||
260 | gdb_test_multiple "python gdb.prompt_hook = None" "Delete hook" { | |
261 | -re "\[\r\n\]$newprompt2 $" { | |
262 | pass "Delete old hook" | |
263 | } | |
264 | } | |
265 | ||
266 | gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" { | |
267 | -re "\[\r\n\]$gdb_prompt $" { | |
268 | pass "set default prompt" | |
269 | } | |
270 | } | |
271 | ||
272 | gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" { | |
273 | -re "\[\r\n\]$gdb_prompt $" { | |
274 | pass "set programming hook" | |
275 | } | |
276 | } | |
277 | ||
278 | gdb_test_multiple "python" "test we ignore substituion for seconday prompts" { | |
279 | -re "\r\n>$" { | |
280 | pass "readline secondary are not substituted" | |
281 | } | |
282 | } | |
283 | ||
284 | gdb_test_multiple "end" "end programming" { | |
285 | -re "\[\r\n\]$gdb_prompt $" { | |
286 | pass "end programming" | |
287 | } | |
288 | } | |
fa3a4f15 PM |
289 | |
290 | gdb_py_test_multiple "prompt substitution readline" \ | |
291 | "python" "" \ | |
292 | "import gdb.command.prompt" "" \ | |
293 | "end" "" | |
294 | ||
295 | gdb_test_multiple "set extended-prompt one two three " \ | |
296 | "set basic extended prompt" { | |
297 | -re "\[\r\n\]one two three $" { | |
298 | pass "set basic extended prompt" | |
299 | } | |
300 | } | |
301 | ||
302 | gdb_test_multiple "set extended-prompt \\w " \ | |
303 | "set extended prompt working directory" { | |
304 | -re "\[\r\n\].*gdb.*testsuite.* $" { | |
305 | pass "set extended prompt working directory" | |
306 | } | |
307 | } | |
308 | ||
309 | gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \ | |
310 | "set extended prompt parameter" { | |
80b6e756 | 311 | -re "\[\r\n\]some param full $" { |
fa3a4f15 PM |
312 | pass "set extended prompt parameter" |
313 | } | |
314 | } | |
80b6e756 PM |
315 | |
316 | # Start with a fresh gdb. | |
317 | clean_restart ${testfile} | |
318 | ||
319 | # The following tests require execution. | |
320 | ||
321 | if ![runto_main] then { | |
322 | fail "Can't run to main" | |
323 | return 0 | |
324 | } | |
325 | ||
326 | # print-stack settings | |
327 | gdb_test "show python print-stack" \ | |
328 | "The mode of Python stack printing on error is \"message\".*" \ | |
329 | "Test print-backtrace show setting. Default is message." | |
330 | gdb_py_test_silent_cmd "set python print-stack full" \ | |
331 | "Test print-backtrace set setting" 1 | |
332 | gdb_test "show python print-stack" \ | |
333 | "The mode of Python stack printing on error is \"full\".*" \ | |
334 | "Test print-backtrace show setting to full." | |
335 | gdb_py_test_silent_cmd "set python print-stack none" \ | |
336 | "Test print-backtrace set setting" 1 | |
337 | gdb_test "show python print-stack" \ | |
338 | "The mode of Python stack printing on error is \"none\".*" \ | |
339 | "Test print-backtrace show setting to none." | |
340 | ||
341 | gdb_py_test_silent_cmd "set python print-stack message" \ | |
342 | "Test print-backtrace set setting" 1 | |
343 | ||
344 | gdb_py_test_multiple "prompt substitution readline" \ | |
345 | "python" "" \ | |
346 | "pCounter = 0" "" \ | |
347 | "def error_prompt(current):" "" \ | |
348 | " raise RuntimeError(\"Python exception called\")" "" \ | |
349 | "end" "" | |
350 | ||
351 | gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" { | |
352 | -re "Python Exception <type 'exceptions.RuntimeError'> Python exception called.*" { | |
353 | pass "set hook" | |
354 | } | |
355 | } | |
356 | ||
357 | gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \ | |
358 | "set the hook to default" 1 | |
359 | ||
360 | gdb_py_test_silent_cmd "set python print-stack full" \ | |
361 | "Test print-backtrace set setting" 1 | |
362 | ||
363 | gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" { | |
364 | -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*" { | |
365 | pass "set hook" | |
366 | } | |
367 | } | |
368 | ||
369 | gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \ | |
370 | "set the hook to default" 1 |