1 # Copyright (C) 2008, 2009, 2010, 2011 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.
23 load_lib gdb-python.exp
26 set srcfile ${testfile}.c
27 set srcfile1 ${testfile}-1.c
28 set binfile ${objdir}/${subdir}/${testfile}
30 if { [gdb_compile "${srcdir}/${subdir}/${srcfile} ${srcdir}/${subdir}/${srcfile1}" \
31 ${binfile} executable {debug}] != "" } {
32 untested "Could not compile $binfile."
36 # Start with a fresh gdb.
39 gdb_reinitialize_dir $srcdir/$subdir
41 # Skip all tests if Python scripting is not enabled.
42 if { [skip_python_tests] } { continue }
44 gdb_test_multiple "python print 23" "verify python support" {
45 -re "not supported.*$gdb_prompt $" {
46 unsupported "python support is disabled"
48 # If Python is not supported, verify that sourcing a python script
50 gdb_test "source $srcdir/$subdir/source2.py" "Error in sourced command file:.*"
53 -re "$gdb_prompt $" {}
56 gdb_py_test_multiple "multi-line python command" \
61 gdb_py_test_multiple "show python command" \
62 "define zzq" "Type commands for definition of .* just \"end\"\\.*" \
67 "show user zzq" "User command \"zzq\":.* python.*print 23.* end"
69 gdb_py_test_multiple "indented multi-line python command" \
72 " print 'hello, world!'" "" \
76 gdb_test "source $srcdir/$subdir/source2.py" "yes"
78 gdb_test "python print gdb.current_objfile()" "None"
79 gdb_test "python print gdb.objfiles()" "\\\[\\\]"
81 # Test http://bugs.python.org/issue4434 workaround in configure.ac
82 gdb_test "python import itertools; print 'IMPOR'+'TED'" "IMPORTED" "pythonX.Y/lib-dynload/*.so"
85 "python x = gdb.execute('printf \"%d\", 23', to_string = True)"
86 gdb_test "python print x" "23"
89 gdb_py_test_multiple "post event insertion" \
92 "class Foo(object):" "" \
93 " def __call__(self):" "" \
94 " global someVal" "" \
96 "gdb.post_event(Foo())" "" \
99 gdb_test "python print someVal" "1" "test post event execution"
100 gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" "Test non callable class"
102 # Test (no) pagination of the executed command.
103 gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
105 gdb_test_no_output "set height $lines"
107 set test "verify pagination beforehand"
108 gdb_test_multiple "python print \"\\n\" * $lines" $test {
109 -re "---Type <return>" {
112 -re " to continue, or q <return>" {
119 gdb_test "q" "Quit" "verify pagination beforehand: q"
121 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"
123 set test "verify pagination afterwards"
124 gdb_test_multiple "python print \"\\n\" * $lines" $test {
125 -re "---Type <return>" {
128 -re " to continue, or q <return>" {
135 gdb_test "q" "Quit" "verify pagination afterwards: q"
137 gdb_test_no_output "set height 0"
139 gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout"
141 gdb_test "python print a" ".*aliases -- Aliases of other commands.*" "verify help to uiout"
143 # Test PR 12212, using InfThread.selected_thread() when no inferior is
145 gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1
146 gdb_test "python print nothread == None" "True" "Ensure that no threads are returned"
148 # Start with a fresh gdb.
149 clean_restart ${testfile}
151 # The following tests require execution.
153 if ![runto_main] then {
154 fail "Can't run to main"
158 runto [gdb_get_line_number "Break to end."]
160 # Test gdb.decode_line.
161 gdb_test "python gdb.decode_line(\"main.c:43\")" \
162 "gdb.error: No source file named main.c.*" "test decode_line no source named main"
164 gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1
165 gdb_test "python print len(symtab)" "2" "Test decode_line current location"
166 gdb_test "python print symtab\[0\]" "None" "Test decode_line expression parse"
167 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line current location"
168 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python.c.*" "Test decode_line current locationn filename"
169 gdb_test "python print symtab\[1\]\[0\].line" "22" "Test decode_line current location line number"
171 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1
172 gdb_test "python print len(symtab)" "2" "Test decode_line python.c:26 length"
173 gdb_test "python print symtab\[0\]" "if foo" "Test decode_line expression parse"
174 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line python.c:26 length"
175 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python.c.*" "Test decode_line python.c:26 filename"
176 gdb_test "python print symtab\[1\]\[0\].line" "26" "Test decode_line python.c:26 line number"
178 gdb_test "python gdb.decode_line(\"randomfunc\")" \
179 "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc"
180 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1
181 gdb_test "python print len(symtab)" "2" "Test decode_line func1 length"
182 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line func1 length"
183 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python-1.c.*" "Test decode_line func1 filename"
184 gdb_test "python print symtab\[1\]\[0\].line" "19" "Test decode_line func1 line number"
187 gdb_test "python print sys.stderr" ".*__main__.GdbOutputErrorFile instance at.*" "Test stderr location"
188 gdb_test "python print sys.stdout" ".*__main__.GdbOutputFile instance at.*" "Test stdout location"
189 gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "Test default write"
190 gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "Test stderr write"
191 gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "Test stdout write"
192 gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "Test stdlog write"
194 # Deprecate maint set/show python print-stack
195 gdb_test "maint show python print-stack" \
196 "Warning: command 'maintenance show python print-stack' is deprecated.*Use 'show python print-stack'.*" \
197 "Test deprecation maint show warning."
198 gdb_test "maint set python print-stack off" \
199 "Warning: command 'maintenance set python print-stack' is deprecated.*Use 'set python print-stack'.*" \
200 "Test deprecation maint set warning."
201 gdb_test "show python print-stack" \
202 "Whether Python stack will be printed on error is off.*" \
203 "Test print-backtrace show setting. Default off."
204 gdb_py_test_silent_cmd "set python print-stack on" \
205 "Test print-backtrace set setting" 1
206 gdb_test "show python print-stack" \
207 "Whether Python stack will be printed on error is on.*" \
209 # Test prompt substituion
211 gdb_py_test_multiple "prompt substitution" \
213 "someCounter = 0" "" \
214 "def prompt(current):" "" \
215 " global someCounter" "" \
216 " if (current == \"testfake \"):" "" \
218 " someCounter = someCounter + 1" "" \
219 " return \"py prompt \" + str (someCounter) + \" \"" "" \
222 gdb_py_test_multiple "prompt substitution readline" \
225 "def program_prompt(current):" "" \
226 " global pCounter" "" \
227 " if (current == \">\"):" "" \
228 " pCounter = pCounter + 1" "" \
229 " return \"python line \" + str (pCounter) + \": \"" "" \
233 set newprompt "py prompt 1"
234 set newprompt2 "py prompt 2"
235 set testfake "testfake"
237 gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" {
238 -re "\[\r\n\]$newprompt $" {
243 gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" {
244 -re "\[\r\n\]$testfake $" {
245 pass "set prompt testfake"
249 gdb_test_multiple "show prompt" "show testfake prompt" {
250 -re "Gdb's prompt is \"$testfake \"..* $" {
251 pass "show prompt shows guarded prompt"
255 gdb_test_multiple "set prompt blah " "set blah in GDB" {
256 -re "\[\r\n\]$newprompt2 $" {
257 pass "set prompt blah overriden"
261 gdb_test_multiple "python gdb.prompt_hook = None" "Delete hook" {
262 -re "\[\r\n\]$newprompt2 $" {
263 pass "Delete old hook"
267 gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" {
268 -re "\[\r\n\]$gdb_prompt $" {
269 pass "set default prompt"
273 gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" {
274 -re "\[\r\n\]$gdb_prompt $" {
275 pass "set programming hook"
279 gdb_test_multiple "python" "test we ignore substituion for seconday prompts" {
281 pass "readline secondary are not substituted"
285 gdb_test_multiple "end" "end programming" {
286 -re "\[\r\n\]$gdb_prompt $" {
287 pass "end programming"
291 gdb_py_test_multiple "prompt substitution readline" \
293 "import gdb.command.prompt" "" \
296 gdb_test_multiple "set extended-prompt one two three " \
297 "set basic extended prompt" {
298 -re "\[\r\n\]one two three $" {
299 pass "set basic extended prompt"
303 gdb_test_multiple "set extended-prompt \\w " \
304 "set extended prompt working directory" {
305 -re "\[\r\n\].*gdb.*testsuite.* $" {
306 pass "set extended prompt working directory"
310 gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \
311 "set extended prompt parameter" {
312 -re "\[\r\n\]some param True $" {
313 pass "set extended prompt parameter"