PR python/17372 - Python hangs when displaying help()
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / python.exp
1 # Copyright (C) 2008-2014 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 # exposing values to Python.
18
19 load_lib gdb-python.exp
20
21 standard_testfile python.c python-1.c
22
23 if {[build_executable $testfile.exp $testfile \
24 [list $srcfile $srcfile2] debug] == -1} {
25 return -1
26 }
27
28 # Start with a fresh gdb.
29 gdb_exit
30 gdb_start
31 gdb_reinitialize_dir $srcdir/$subdir
32
33 set remote_source2_py [gdb_remote_download host \
34 ${srcdir}/${subdir}/source2.py]
35
36 # Do this instead of the skip_python_check.
37 # We want to do some tests when Python is not present.
38 gdb_test_multiple "python print (23)" "verify python support" {
39 -re "not supported.*$gdb_prompt $" {
40 unsupported "python support is disabled"
41
42 # If Python is not supported, verify that sourcing a python script
43 # causes an error.
44 gdb_test "source $remote_source2_py" \
45 "Error in sourced command file:.*" \
46 "source source2.py when python disabled"
47
48 # Verify multi-line python commands cause an error.
49 gdb_py_test_multiple "multi-line python command" \
50 "python" "" \
51 "print (23)" "" \
52 "end" "not supported.*"
53
54 return -1
55 }
56 -re "$gdb_prompt $" {}
57 }
58
59 gdb_py_test_multiple "multi-line python command" \
60 "python" "" \
61 "print (23)" "" \
62 "end" "23"
63
64 # Spawn interactive Python help from a multi-line command, thus, after
65 # a secondary prompt.
66
67 with_test_prefix "python interactive help" {
68 set test "python; help(); end"
69 gdb_test_multiple "python\nhelp()\nend" $test {
70 -re "online help utility.*help> $" {
71 pass $test
72
73 # The "quit" must be seen on the output. A buggy GDB
74 # would not display it.
75 gdb_test "quit" "^quit.*leaving help.*" "quit help"
76 }
77 }
78 }
79
80 gdb_py_test_multiple "show python command" \
81 "define zzq" "Type commands for definition of .* just \"end\"\\.*" \
82 "python" "" \
83 "print (23)" "" \
84 "end" "" \
85 "end" "" \
86 "show user zzq" "User command \"zzq\":.* python.*print \\(23\\).* end"
87
88 gdb_py_test_multiple "indented multi-line python command" \
89 "python" "" \
90 "def foo ():" "" \
91 " print ('hello, world!')" "" \
92 "foo ()" "" \
93 "end" "hello, world!"
94
95 gdb_test "source $remote_source2_py" "yes" "source source2.py"
96
97 gdb_test "source -s source2.py" "yes" "source -s source2.py"
98
99 gdb_test "python print (gdb.current_objfile())" "None"
100 gdb_test "python print (gdb.objfiles())" "\\\[\\\]"
101
102 # Test http://bugs.python.org/issue4434 workaround in configure.ac
103 gdb_test "python import itertools; print ('IMPOR'+'TED')" "IMPORTED" "pythonX.Y/lib-dynload/*.so"
104
105 gdb_test_no_output \
106 "python x = gdb.execute('printf \"%d\", 23', to_string = True)"
107 gdb_test "python print (x)" "23"
108
109 # Test post_event.
110 gdb_py_test_multiple "post event insertion" \
111 "python" "" \
112 "someVal = 0" "" \
113 "class Foo(object):" "" \
114 " def __call__(self):" "" \
115 " global someVal" "" \
116 " someVal += 1" "" \
117 "gdb.post_event(Foo())" "" \
118 "end" ""
119
120 gdb_test "python print (someVal)" "1" "test post event execution"
121 gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" "Test non callable class"
122
123 # Test (no) pagination of the executed command.
124 gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
125 set lines 10
126 gdb_test_no_output "set height $lines"
127
128 set test "verify pagination beforehand"
129 gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
130 -re "---Type <return>" {
131 exp_continue
132 }
133 -re " to continue, or q <return>" {
134 exp_continue
135 }
136 -re " to quit---$" {
137 pass $test
138 }
139 }
140 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination beforehand: q"
141
142 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"
143
144 set test "verify pagination afterwards"
145 gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
146 -re "---Type <return>" {
147 exp_continue
148 }
149 -re " to continue, or q <return>" {
150 exp_continue
151 }
152 -re " to quit---$" {
153 pass $test
154 }
155 }
156 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination afterwards: q"
157
158 gdb_test_no_output "set height 0"
159
160 gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout"
161
162 gdb_test "python print (a)" ".*aliases -- Aliases of other commands.*" "verify help to uiout"
163
164 # Test PR 12212, using InfThread.selected_thread() when no inferior is
165 # loaded.
166 gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1
167 gdb_test "python print (nothread == None)" "True" "Ensure that no threads are returned"
168
169 gdb_py_test_multiple "register atexit function" \
170 "python" "" \
171 "import atexit" "" \
172 "def printit(arg):" "" \
173 " print (arg)" "" \
174 "atexit.register(printit, 'good bye world')" "" \
175 "end" ""
176
177 send_gdb "quit\n"
178 gdb_expect {
179 -re "good bye world" {
180 pass "atexit handling"
181 }
182 default {
183 fail "atexit handling"
184 }
185 }
186
187 # Start with a fresh gdb.
188 clean_restart ${testfile}
189
190 # The following tests require execution.
191
192 if ![runto_main] then {
193 fail "Can't run to main"
194 return 0
195 }
196
197 set lineno [gdb_get_line_number "Break to end."]
198 runto $lineno
199
200 # Test gdb.decode_line.
201 gdb_test "python gdb.decode_line(\"main.c:43\")" \
202 "gdb.error: No source file named main.c.*" "test decode_line no source named main"
203
204 gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1
205 gdb_test "python print (len(symtab))" "2" "Test decode_line current location"
206 gdb_test "python print (symtab\[0\])" "None" "Test decode_line expression parse"
207 gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line current location"
208
209 if { [is_remote host] } {
210 set python_c [string_to_regexp "python.c"]
211 } else {
212 set python_c [string_to_regexp "gdb.python/python.c"]
213 }
214 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_c}" "Test decode_line current location filename"
215 gdb_test "python print (symtab\[1\]\[0\].line)" "$lineno" "Test decode_line current location line number"
216
217 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1
218 gdb_test "python print (len(symtab))" "2" "Test decode_line python.c:26 length"
219 gdb_test "python print (symtab\[0\])" "if foo" "Test decode_line expression parse"
220 gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line python.c:26 length"
221 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_c}" "Test decode_line python.c:26 filename"
222 gdb_test "python print (symtab\[1\]\[0\].line)" "26" "Test decode_line python.c:26 line number"
223
224 gdb_test "python gdb.decode_line(\"randomfunc\")" \
225 "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc"
226 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1
227 gdb_test "python print (len(symtab))" "2" "Test decode_line func1 length"
228 gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line func1 length"
229
230 if { [is_remote host] } {
231 set python_1_c [string_to_regexp "python-1.c"]
232 } else {
233 set python_1_c [string_to_regexp "gdb.python/python-1.c"]
234 }
235 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_1_c}" "Test decode_line func1 filename"
236 gdb_test "python print (symtab\[1\]\[0\].line)" "19" "Test decode_line func1 line number"
237 gdb_py_test_silent_cmd {python symtab = gdb.decode_line ("func1,func2")} \
238 "test decode_line func1,func2" 1
239 gdb_test {python print (symtab[0])} ",func2" "stop at comma in linespec"
240
241 # gdb.write
242 gdb_test "python print (sys.stderr)" ".*gdb.GdbOutputErrorFile (instance|object) at.*" "Test stderr location"
243 gdb_test "python print (sys.stdout)" ".*gdb.GdbOutputFile (instance|object) at.*" "Test stdout location"
244 gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "Test default write"
245 gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "Test stderr write"
246 gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "Test stdout write"
247 gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "Test stdlog write"
248
249 # Turn on full stack printing for subsequent tests.
250 gdb_py_test_silent_cmd "set python print-stack full" \
251 "Set print-stack full for prompt tests" 1
252
253 # Test prompt substituion
254
255 gdb_py_test_multiple "prompt substitution" \
256 "python" "" \
257 "someCounter = 0" "" \
258 "def prompt(current):" "" \
259 " global someCounter" "" \
260 " if (current == \"testfake \"):" "" \
261 " return None" "" \
262 " someCounter = someCounter + 1" "" \
263 " return \"py prompt \" + str (someCounter) + \" \"" "" \
264 "end" ""
265
266 gdb_py_test_multiple "prompt substitution readline" \
267 "python" "" \
268 "pCounter = 0" "" \
269 "def program_prompt(current):" "" \
270 " global pCounter" "" \
271 " if (current == \">\"):" "" \
272 " pCounter = pCounter + 1" "" \
273 " return \"python line \" + str (pCounter) + \": \"" "" \
274 " return None" "" \
275 "end" ""
276
277 set newprompt "py prompt 1"
278 set newprompt2 "py prompt 2"
279 set testfake "testfake"
280
281 gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" {
282 -re "\[\r\n\]$newprompt $" {
283 pass "set hook"
284 }
285 }
286
287 gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" {
288 -re "\[\r\n\]$testfake $" {
289 pass "set prompt testfake"
290 }
291 }
292
293 gdb_test_multiple "show prompt" "show testfake prompt" {
294 -re "Gdb's prompt is \"$testfake \"..* $" {
295 pass "show prompt shows guarded prompt"
296 }
297 }
298
299 gdb_test_multiple "set prompt blah " "set blah in GDB" {
300 -re "\[\r\n\]$newprompt2 $" {
301 pass "set prompt blah overriden"
302 }
303 }
304
305 gdb_test_multiple "python gdb.prompt_hook = None" "Delete hook" {
306 -re "\[\r\n\]$newprompt2 $" {
307 pass "Delete old hook"
308 }
309 }
310
311 gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" {
312 -re "\[\r\n\]$gdb_prompt $" {
313 pass "set default prompt"
314 }
315 }
316
317 gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" {
318 -re "\[\r\n\]$gdb_prompt $" {
319 pass "set programming hook"
320 }
321 }
322
323 gdb_test_multiple "python" "test we ignore substituion for seconday prompts" {
324 -re "\r\n>$" {
325 pass "readline secondary are not substituted"
326 }
327 }
328
329 gdb_test_multiple "end" "end programming" {
330 -re "\[\r\n\]$gdb_prompt $" {
331 pass "end programming"
332 }
333 }
334
335 gdb_py_test_multiple "prompt substitution readline" \
336 "python" "" \
337 "import gdb.command.prompt" "" \
338 "end" ""
339
340 gdb_test_multiple "set extended-prompt one two three " \
341 "set basic extended prompt" {
342 -re "\[\r\n\]one two three $" {
343 pass "set basic extended prompt"
344 }
345 }
346
347 gdb_test_multiple "set extended-prompt \\w " \
348 "set extended prompt working directory" {
349 -re "\[\r\n\].*gdb.*testsuite.* $" {
350 pass "set extended prompt working directory"
351 }
352 }
353
354 gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \
355 "set extended prompt parameter" {
356 -re "\[\r\n\]some param full $" {
357 pass "set extended prompt parameter"
358 }
359 }
360
361 # Start with a fresh gdb.
362 clean_restart ${testfile}
363
364 # The following tests require execution.
365
366 if ![runto_main] then {
367 fail "Can't run to main"
368 return 0
369 }
370
371 # print-stack settings
372 gdb_test "show python print-stack" \
373 "The mode of Python stack printing on error is \"message\".*" \
374 "Test print-stack show setting. Default is message."
375 gdb_py_test_silent_cmd "set python print-stack full" \
376 "Test print-stack set setting to full" 1
377 gdb_test "show python print-stack" \
378 "The mode of Python stack printing on error is \"full\".*" \
379 "Test print-stack show setting to full"
380 gdb_py_test_silent_cmd "set python print-stack none" \
381 "Test print-stack set setting to none" 1
382 gdb_test "show python print-stack" \
383 "The mode of Python stack printing on error is \"none\".*" \
384 "Test print-stack show setting to none"
385
386 gdb_py_test_silent_cmd "set python print-stack message" \
387 "Test print-stack set setting to message" 1
388
389 gdb_py_test_multiple "prompt substitution readline" \
390 "python" "" \
391 "pCounter = 0" "" \
392 "def error_prompt(current):" "" \
393 " raise RuntimeError(\"Python exception called\")" "" \
394 "end" ""
395
396 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
397 -re "Python Exception (exceptions.RuntimeError|<(type 'exceptions.|class ')RuntimeError'>) Python exception called.*" {
398 pass "set hook"
399 }
400 }
401
402 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
403 "set the hook to default" 1
404
405 gdb_py_test_silent_cmd "set python print-stack full" \
406 "set print-stack full for prompt error test" 1
407
408 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
409 -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*" {
410 pass "set hook"
411 }
412 }
413
414 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
415 "set the hook to default" 1
416
417 # Start with a fresh gdb.
418 clean_restart ${testfile}
419
420 # The following tests require execution.
421
422 if ![runto_main] then {
423 fail "Can't run to main"
424 return 0
425 }
426
427 runto [gdb_get_line_number "Break at func2 call site."]
428
429 gdb_py_test_silent_cmd "python line = gdb.selected_frame().find_sal().line" "Get line number of func2 call site" 1
430 gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line == line)" "True" "Test find_pc_line at func2 call site"
431
432 gdb_py_test_silent_cmd "step" "Step into func2" 1
433 gdb_py_test_silent_cmd "up" "Step out of func2" 1
434
435 gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line > line)" "True" "Test find_pc_line with resume address"
This page took 0.053364 seconds and 4 git commands to generate.