1 # Copyright (C) 2008-2012 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 # Skip all tests if Python scripting is not enabled.
34 if { [skip_python_tests] } { continue }
36 gdb_test_multiple "python print 23" "verify python support" {
37 -re "not supported.*$gdb_prompt $" {
38 unsupported "python support is disabled"
40 # If Python is not supported, verify that sourcing a python script
42 gdb_test "source $srcdir/$subdir/source2.py" \
43 "Error in sourced command file:.*" \
44 "source source2.py when python disabled"
47 -re "$gdb_prompt $" {}
50 gdb_py_test_multiple "multi-line python command" \
55 gdb_py_test_multiple "show python command" \
56 "define zzq" "Type commands for definition of .* just \"end\"\\.*" \
61 "show user zzq" "User command \"zzq\":.* python.*print 23.* end"
63 gdb_py_test_multiple "indented multi-line python command" \
66 " print 'hello, world!'" "" \
70 gdb_test "source $srcdir/$subdir/source2.py" "yes" "source source2.py"
72 gdb_test "source -s source2.py" "yes" "source -s source2.py"
74 gdb_test "python print gdb.current_objfile()" "None"
75 gdb_test "python print gdb.objfiles()" "\\\[\\\]"
77 # Test http://bugs.python.org/issue4434 workaround in configure.ac
78 gdb_test "python import itertools; print 'IMPOR'+'TED'" "IMPORTED" "pythonX.Y/lib-dynload/*.so"
81 "python x = gdb.execute('printf \"%d\", 23', to_string = True)"
82 gdb_test "python print x" "23"
85 gdb_py_test_multiple "post event insertion" \
88 "class Foo(object):" "" \
89 " def __call__(self):" "" \
90 " global someVal" "" \
92 "gdb.post_event(Foo())" "" \
95 gdb_test "python print someVal" "1" "test post event execution"
96 gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" "Test non callable class"
98 # Test (no) pagination of the executed command.
99 gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
101 gdb_test_no_output "set height $lines"
103 set test "verify pagination beforehand"
104 gdb_test_multiple "python print \"\\n\" * $lines" $test {
105 -re "---Type <return>" {
108 -re " to continue, or q <return>" {
115 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination beforehand: q"
117 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"
119 set test "verify pagination afterwards"
120 gdb_test_multiple "python print \"\\n\" * $lines" $test {
121 -re "---Type <return>" {
124 -re " to continue, or q <return>" {
131 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination afterwards: q"
133 gdb_test_no_output "set height 0"
135 gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout"
137 gdb_test "python print a" ".*aliases -- Aliases of other commands.*" "verify help to uiout"
139 # Test PR 12212, using InfThread.selected_thread() when no inferior is
141 gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1
142 gdb_test "python print nothread == None" "True" "Ensure that no threads are returned"
144 # Start with a fresh gdb.
145 clean_restart ${testfile}
147 # The following tests require execution.
149 if ![runto_main] then {
150 fail "Can't run to main"
154 runto [gdb_get_line_number "Break to end."]
156 # Test gdb.decode_line.
157 gdb_test "python gdb.decode_line(\"main.c:43\")" \
158 "gdb.error: No source file named main.c.*" "test decode_line no source named main"
160 gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1
161 gdb_test "python print len(symtab)" "2" "Test decode_line current location"
162 gdb_test "python print symtab\[0\]" "None" "Test decode_line expression parse"
163 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line current location"
164 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python.c.*" "Test decode_line current locationn filename"
165 gdb_test "python print symtab\[1\]\[0\].line" "22" "Test decode_line current location line number"
167 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1
168 gdb_test "python print len(symtab)" "2" "Test decode_line python.c:26 length"
169 gdb_test "python print symtab\[0\]" "if foo" "Test decode_line expression parse"
170 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line python.c:26 length"
171 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python.c.*" "Test decode_line python.c:26 filename"
172 gdb_test "python print symtab\[1\]\[0\].line" "26" "Test decode_line python.c:26 line number"
174 gdb_test "python gdb.decode_line(\"randomfunc\")" \
175 "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc"
176 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1
177 gdb_test "python print len(symtab)" "2" "Test decode_line func1 length"
178 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line func1 length"
179 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python-1.c.*" "Test decode_line func1 filename"
180 gdb_test "python print symtab\[1\]\[0\].line" "19" "Test decode_line func1 line number"
181 gdb_py_test_silent_cmd {python symtab = gdb.decode_line ("func1,func2")} \
182 "test decode_line func1,func2" 1
183 gdb_test {python print symtab[0]} ",func2" "stop at comma in linespec"
186 gdb_test "python print sys.stderr" ".*gdb.GdbOutputErrorFile instance at.*" "Test stderr location"
187 gdb_test "python print sys.stdout" ".*gdb.GdbOutputFile instance at.*" "Test stdout location"
188 gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "Test default write"
189 gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "Test stderr write"
190 gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "Test stdout write"
191 gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "Test stdlog write"
194 gdb_test "show python print-stack" \
195 "The mode of Python stack printing on error is \"message\".*" \
196 "Test print-backtrace show setting. Default is message."
197 gdb_py_test_silent_cmd "set python print-stack full" \
198 "Test print-backtrace set setting" 1
199 gdb_test "show python print-stack" \
200 "The mode of Python stack printing on error is \"full\".*" \
201 "Test print-backtrace show setting to full."
203 # Test prompt substituion
205 gdb_py_test_multiple "prompt substitution" \
207 "someCounter = 0" "" \
208 "def prompt(current):" "" \
209 " global someCounter" "" \
210 " if (current == \"testfake \"):" "" \
212 " someCounter = someCounter + 1" "" \
213 " return \"py prompt \" + str (someCounter) + \" \"" "" \
216 gdb_py_test_multiple "prompt substitution readline" \
219 "def program_prompt(current):" "" \
220 " global pCounter" "" \
221 " if (current == \">\"):" "" \
222 " pCounter = pCounter + 1" "" \
223 " return \"python line \" + str (pCounter) + \": \"" "" \
227 set newprompt "py prompt 1"
228 set newprompt2 "py prompt 2"
229 set testfake "testfake"
231 gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" {
232 -re "\[\r\n\]$newprompt $" {
237 gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" {
238 -re "\[\r\n\]$testfake $" {
239 pass "set prompt testfake"
243 gdb_test_multiple "show prompt" "show testfake prompt" {
244 -re "Gdb's prompt is \"$testfake \"..* $" {
245 pass "show prompt shows guarded prompt"
249 gdb_test_multiple "set prompt blah " "set blah in GDB" {
250 -re "\[\r\n\]$newprompt2 $" {
251 pass "set prompt blah overriden"
255 gdb_test_multiple "python gdb.prompt_hook = None" "Delete hook" {
256 -re "\[\r\n\]$newprompt2 $" {
257 pass "Delete old hook"
261 gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" {
262 -re "\[\r\n\]$gdb_prompt $" {
263 pass "set default prompt"
267 gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" {
268 -re "\[\r\n\]$gdb_prompt $" {
269 pass "set programming hook"
273 gdb_test_multiple "python" "test we ignore substituion for seconday prompts" {
275 pass "readline secondary are not substituted"
279 gdb_test_multiple "end" "end programming" {
280 -re "\[\r\n\]$gdb_prompt $" {
281 pass "end programming"
285 gdb_py_test_multiple "prompt substitution readline" \
287 "import gdb.command.prompt" "" \
290 gdb_test_multiple "set extended-prompt one two three " \
291 "set basic extended prompt" {
292 -re "\[\r\n\]one two three $" {
293 pass "set basic extended prompt"
297 gdb_test_multiple "set extended-prompt \\w " \
298 "set extended prompt working directory" {
299 -re "\[\r\n\].*gdb.*testsuite.* $" {
300 pass "set extended prompt working directory"
304 gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \
305 "set extended prompt parameter" {
306 -re "\[\r\n\]some param full $" {
307 pass "set extended prompt parameter"
311 # Start with a fresh gdb.
312 clean_restart ${testfile}
314 # The following tests require execution.
316 if ![runto_main] then {
317 fail "Can't run to main"
321 # print-stack settings
322 gdb_test "show python print-stack" \
323 "The mode of Python stack printing on error is \"message\".*" \
324 "Test print-backtrace show setting. Default is message."
325 gdb_py_test_silent_cmd "set python print-stack full" \
326 "Test print-backtrace set setting" 1
327 gdb_test "show python print-stack" \
328 "The mode of Python stack printing on error is \"full\".*" \
329 "Test print-backtrace show setting to full."
330 gdb_py_test_silent_cmd "set python print-stack none" \
331 "Test print-backtrace set setting" 1
332 gdb_test "show python print-stack" \
333 "The mode of Python stack printing on error is \"none\".*" \
334 "Test print-backtrace show setting to none."
336 gdb_py_test_silent_cmd "set python print-stack message" \
337 "Test print-backtrace set setting" 1
339 gdb_py_test_multiple "prompt substitution readline" \
342 "def error_prompt(current):" "" \
343 " raise RuntimeError(\"Python exception called\")" "" \
346 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
347 -re "Python Exception <type 'exceptions.RuntimeError'> Python exception called.*" {
352 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
353 "set the hook to default" 1
355 gdb_py_test_silent_cmd "set python print-stack full" \
356 "Test print-backtrace set setting" 1
358 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
359 -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*" {
364 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
365 "set the hook to default" 1
367 # Start with a fresh gdb.
368 clean_restart ${testfile}
370 # The following tests require execution.
372 if ![runto_main] then {
373 fail "Can't run to main"
377 runto [gdb_get_line_number "Break at func2 call site."]
379 gdb_py_test_silent_cmd "python line = gdb.selected_frame().find_sal().line" "Get line number of func2 call site" 1
380 gdb_test "python print gdb.find_pc_line(gdb.selected_frame().pc()).line == line" "True" "Test find_pc_line at func2 call site"
382 gdb_py_test_silent_cmd "step" "Step into func2" 1
383 gdb_py_test_silent_cmd "up" "Step out of func2" 1
385 gdb_test "python print gdb.find_pc_line(gdb.selected_frame().pc()).line > line" "True" "Test find_pc_line with resume address"