1 # Copyright (C) 2008-2014 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 # exposing values to Python.
19 load_lib gdb-python.exp
21 standard_testfile python.c python-1.c
23 if {[build_executable $testfile.exp $testfile \
24 [list $srcfile $srcfile2] debug] == -1} {
28 # Start with a fresh gdb.
31 gdb_reinitialize_dir $srcdir/$subdir
33 # Do this instead of the skip_python_check.
34 # We want to do some tests when Python is not present.
35 gdb_test_multiple "python print (23)" "verify python support" {
36 -re "not supported.*$gdb_prompt $" {
37 unsupported "python support is disabled"
39 # If Python is not supported, verify that sourcing a python script
41 gdb_test "source $srcdir/$subdir/source2.py" \
42 "Error in sourced command file:.*" \
43 "source source2.py when python disabled"
46 -re "$gdb_prompt $" {}
49 gdb_py_test_multiple "multi-line python command" \
54 gdb_py_test_multiple "show python command" \
55 "define zzq" "Type commands for definition of .* just \"end\"\\.*" \
60 "show user zzq" "User command \"zzq\":.* python.*print \\(23\\).* end"
62 gdb_py_test_multiple "indented multi-line python command" \
65 " print ('hello, world!')" "" \
69 gdb_test "source $srcdir/$subdir/source2.py" "yes" "source source2.py"
71 gdb_test "source -s source2.py" "yes" "source -s source2.py"
73 gdb_test "python print (gdb.current_objfile())" "None"
74 gdb_test "python print (gdb.objfiles())" "\\\[\\\]"
76 # Test http://bugs.python.org/issue4434 workaround in configure.ac
77 gdb_test "python import itertools; print ('IMPOR'+'TED')" "IMPORTED" "pythonX.Y/lib-dynload/*.so"
80 "python x = gdb.execute('printf \"%d\", 23', to_string = True)"
81 gdb_test "python print (x)" "23"
84 gdb_py_test_multiple "post event insertion" \
87 "class Foo(object):" "" \
88 " def __call__(self):" "" \
89 " global someVal" "" \
91 "gdb.post_event(Foo())" "" \
94 gdb_test "python print (someVal)" "1" "test post event execution"
95 gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" "Test non callable class"
97 # Test (no) pagination of the executed command.
98 gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
100 gdb_test_no_output "set height $lines"
102 set test "verify pagination beforehand"
103 gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
104 -re "---Type <return>" {
107 -re " to continue, or q <return>" {
114 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination beforehand: q"
116 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"
118 set test "verify pagination afterwards"
119 gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
120 -re "---Type <return>" {
123 -re " to continue, or q <return>" {
130 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination afterwards: q"
132 gdb_test_no_output "set height 0"
134 gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout"
136 gdb_test "python print (a)" ".*aliases -- Aliases of other commands.*" "verify help to uiout"
138 # Test PR 12212, using InfThread.selected_thread() when no inferior is
140 gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1
141 gdb_test "python print (nothread == None)" "True" "Ensure that no threads are returned"
143 gdb_py_test_multiple "register atexit function" \
146 "def printit(arg):" "" \
148 "atexit.register(printit, 'good bye world')" "" \
153 -re "good bye world" {
154 pass "atexit handling"
157 fail "atexit handling"
161 # Start with a fresh gdb.
162 clean_restart ${testfile}
164 # The following tests require execution.
166 if ![runto_main] then {
167 fail "Can't run to main"
171 runto [gdb_get_line_number "Break to end."]
173 # Test gdb.decode_line.
174 gdb_test "python gdb.decode_line(\"main.c:43\")" \
175 "gdb.error: No source file named main.c.*" "test decode_line no source named main"
177 gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1
178 gdb_test "python print (len(symtab))" "2" "Test decode_line current location"
179 gdb_test "python print (symtab\[0\])" "None" "Test decode_line expression parse"
180 gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line current location"
181 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*gdb.python/python.c.*" "Test decode_line current locationn filename"
182 gdb_test "python print (symtab\[1\]\[0\].line)" "22" "Test decode_line current location line number"
184 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1
185 gdb_test "python print (len(symtab))" "2" "Test decode_line python.c:26 length"
186 gdb_test "python print (symtab\[0\])" "if foo" "Test decode_line expression parse"
187 gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line python.c:26 length"
188 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*gdb.python/python.c.*" "Test decode_line python.c:26 filename"
189 gdb_test "python print (symtab\[1\]\[0\].line)" "26" "Test decode_line python.c:26 line number"
191 gdb_test "python gdb.decode_line(\"randomfunc\")" \
192 "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc"
193 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1
194 gdb_test "python print (len(symtab))" "2" "Test decode_line func1 length"
195 gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line func1 length"
196 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*gdb.python/python-1.c.*" "Test decode_line func1 filename"
197 gdb_test "python print (symtab\[1\]\[0\].line)" "19" "Test decode_line func1 line number"
198 gdb_py_test_silent_cmd {python symtab = gdb.decode_line ("func1,func2")} \
199 "test decode_line func1,func2" 1
200 gdb_test {python print (symtab[0])} ",func2" "stop at comma in linespec"
203 gdb_test "python print (sys.stderr)" ".*gdb.GdbOutputErrorFile (instance|object) at.*" "Test stderr location"
204 gdb_test "python print (sys.stdout)" ".*gdb.GdbOutputFile (instance|object) at.*" "Test stdout location"
205 gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "Test default write"
206 gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "Test stderr write"
207 gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "Test stdout write"
208 gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "Test stdlog write"
210 # Turn on full stack printing for subsequent tests.
211 gdb_py_test_silent_cmd "set python print-stack full" \
212 "Set print-stack full for prompt tests" 1
214 # Test prompt substituion
216 gdb_py_test_multiple "prompt substitution" \
218 "someCounter = 0" "" \
219 "def prompt(current):" "" \
220 " global someCounter" "" \
221 " if (current == \"testfake \"):" "" \
223 " someCounter = someCounter + 1" "" \
224 " return \"py prompt \" + str (someCounter) + \" \"" "" \
227 gdb_py_test_multiple "prompt substitution readline" \
230 "def program_prompt(current):" "" \
231 " global pCounter" "" \
232 " if (current == \">\"):" "" \
233 " pCounter = pCounter + 1" "" \
234 " return \"python line \" + str (pCounter) + \": \"" "" \
238 set newprompt "py prompt 1"
239 set newprompt2 "py prompt 2"
240 set testfake "testfake"
242 gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" {
243 -re "\[\r\n\]$newprompt $" {
248 gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" {
249 -re "\[\r\n\]$testfake $" {
250 pass "set prompt testfake"
254 gdb_test_multiple "show prompt" "show testfake prompt" {
255 -re "Gdb's prompt is \"$testfake \"..* $" {
256 pass "show prompt shows guarded prompt"
260 gdb_test_multiple "set prompt blah " "set blah in GDB" {
261 -re "\[\r\n\]$newprompt2 $" {
262 pass "set prompt blah overriden"
266 gdb_test_multiple "python gdb.prompt_hook = None" "Delete hook" {
267 -re "\[\r\n\]$newprompt2 $" {
268 pass "Delete old hook"
272 gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" {
273 -re "\[\r\n\]$gdb_prompt $" {
274 pass "set default prompt"
278 gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" {
279 -re "\[\r\n\]$gdb_prompt $" {
280 pass "set programming hook"
284 gdb_test_multiple "python" "test we ignore substituion for seconday prompts" {
286 pass "readline secondary are not substituted"
290 gdb_test_multiple "end" "end programming" {
291 -re "\[\r\n\]$gdb_prompt $" {
292 pass "end programming"
296 gdb_py_test_multiple "prompt substitution readline" \
298 "import gdb.command.prompt" "" \
301 gdb_test_multiple "set extended-prompt one two three " \
302 "set basic extended prompt" {
303 -re "\[\r\n\]one two three $" {
304 pass "set basic extended prompt"
308 gdb_test_multiple "set extended-prompt \\w " \
309 "set extended prompt working directory" {
310 -re "\[\r\n\].*gdb.*testsuite.* $" {
311 pass "set extended prompt working directory"
315 gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \
316 "set extended prompt parameter" {
317 -re "\[\r\n\]some param full $" {
318 pass "set extended prompt parameter"
322 # Start with a fresh gdb.
323 clean_restart ${testfile}
325 # The following tests require execution.
327 if ![runto_main] then {
328 fail "Can't run to main"
332 # print-stack settings
333 gdb_test "show python print-stack" \
334 "The mode of Python stack printing on error is \"message\".*" \
335 "Test print-stack show setting. Default is message."
336 gdb_py_test_silent_cmd "set python print-stack full" \
337 "Test print-stack set setting to full" 1
338 gdb_test "show python print-stack" \
339 "The mode of Python stack printing on error is \"full\".*" \
340 "Test print-stack show setting to full"
341 gdb_py_test_silent_cmd "set python print-stack none" \
342 "Test print-stack set setting to none" 1
343 gdb_test "show python print-stack" \
344 "The mode of Python stack printing on error is \"none\".*" \
345 "Test print-stack show setting to none"
347 gdb_py_test_silent_cmd "set python print-stack message" \
348 "Test print-stack set setting to message" 1
350 gdb_py_test_multiple "prompt substitution readline" \
353 "def error_prompt(current):" "" \
354 " raise RuntimeError(\"Python exception called\")" "" \
357 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
358 -re "Python Exception (exceptions.RuntimeError|<(type 'exceptions.|class ')RuntimeError'>) Python exception called.*" {
363 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
364 "set the hook to default" 1
366 gdb_py_test_silent_cmd "set python print-stack full" \
367 "set print-stack full for prompt error test" 1
369 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
370 -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*" {
375 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
376 "set the hook to default" 1
378 # Start with a fresh gdb.
379 clean_restart ${testfile}
381 # The following tests require execution.
383 if ![runto_main] then {
384 fail "Can't run to main"
388 runto [gdb_get_line_number "Break at func2 call site."]
390 gdb_py_test_silent_cmd "python line = gdb.selected_frame().find_sal().line" "Get line number of func2 call site" 1
391 gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line == line)" "True" "Test find_pc_line at func2 call site"
393 gdb_py_test_silent_cmd "step" "Step into func2" 1
394 gdb_py_test_silent_cmd "up" "Step out of func2" 1
396 gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line > line)" "True" "Test find_pc_line with resume address"