]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/python.exp
* gdb.base/source.exp: Don't include full file name in test name.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / python.exp
1 # Copyright (C) 2008, 2009, 2010, 2011 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 if $tracelevel then {
20 strace $tracelevel
21 }
22
23 load_lib gdb-python.exp
24
25 set testfile "python"
26 set srcfile ${testfile}.c
27 set srcfile1 ${testfile}-1.c
28 set binfile ${objdir}/${subdir}/${testfile}
29
30 if { [gdb_compile "${srcdir}/${subdir}/${srcfile} ${srcdir}/${subdir}/${srcfile1}" \
31 ${binfile} executable {debug}] != "" } {
32 untested "Could not compile $binfile."
33 return -1
34 }
35
36 # Start with a fresh gdb.
37 gdb_exit
38 gdb_start
39 gdb_reinitialize_dir $srcdir/$subdir
40
41 # Skip all tests if Python scripting is not enabled.
42 if { [skip_python_tests] } { continue }
43
44 gdb_test_multiple "python print 23" "verify python support" {
45 -re "not supported.*$gdb_prompt $" {
46 unsupported "python support is disabled"
47
48 # If Python is not supported, verify that sourcing a python script
49 # causes an error.
50 gdb_test "source $srcdir/$subdir/source2.py" \
51 "Error in sourced command file:.*" \
52 "source source2.py when python disabled"
53 return -1
54 }
55 -re "$gdb_prompt $" {}
56 }
57
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" "" \
69 "show user zzq" "User command \"zzq\":.* python.*print 23.* end"
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!"
77
78 gdb_test "source $srcdir/$subdir/source2.py" "yes" "source source2.py"
79
80 gdb_test "python print gdb.current_objfile()" "None"
81 gdb_test "python print gdb.objfiles()" "\\\[\\\]"
82
83 # Test http://bugs.python.org/issue4434 workaround in configure.ac
84 gdb_test "python import itertools; print 'IMPOR'+'TED'" "IMPORTED" "pythonX.Y/lib-dynload/*.so"
85
86 gdb_test_no_output \
87 "python x = gdb.execute('printf \"%d\", 23', to_string = True)"
88 gdb_test "python print x" "23"
89
90 # Test post_event.
91 gdb_py_test_multiple "post event insertion" \
92 "python" "" \
93 "someVal = 0" "" \
94 "class Foo(object):" "" \
95 " def __call__(self):" "" \
96 " global someVal" "" \
97 " someVal += 1" "" \
98 "gdb.post_event(Foo())" "" \
99 "end" ""
100
101 gdb_test "python print someVal" "1" "test post event execution"
102 gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" "Test non callable class"
103
104 # Test (no) pagination of the executed command.
105 gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
106 set lines 10
107 gdb_test_no_output "set height $lines"
108
109 set test "verify pagination beforehand"
110 gdb_test_multiple "python print \"\\n\" * $lines" $test {
111 -re "---Type <return>" {
112 exp_continue
113 }
114 -re " to continue, or q <return>" {
115 exp_continue
116 }
117 -re " to quit---$" {
118 pass $test
119 }
120 }
121 gdb_test "q" "Quit" "verify pagination beforehand: q"
122
123 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"
124
125 set test "verify pagination afterwards"
126 gdb_test_multiple "python print \"\\n\" * $lines" $test {
127 -re "---Type <return>" {
128 exp_continue
129 }
130 -re " to continue, or q <return>" {
131 exp_continue
132 }
133 -re " to quit---$" {
134 pass $test
135 }
136 }
137 gdb_test "q" "Quit" "verify pagination afterwards: q"
138
139 gdb_test_no_output "set height 0"
140
141 gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout"
142
143 gdb_test "python print a" ".*aliases -- Aliases of other commands.*" "verify help to uiout"
144
145 # Test PR 12212, using InfThread.selected_thread() when no inferior is
146 # loaded.
147 gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1
148 gdb_test "python print nothread == None" "True" "Ensure that no threads are returned"
149
150 # Start with a fresh gdb.
151 clean_restart ${testfile}
152
153 # The following tests require execution.
154
155 if ![runto_main] then {
156 fail "Can't run to main"
157 return 0
158 }
159
160 runto [gdb_get_line_number "Break to end."]
161
162 # Test gdb.decode_line.
163 gdb_test "python gdb.decode_line(\"main.c:43\")" \
164 "gdb.error: No source file named main.c.*" "test decode_line no source named main"
165
166 gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1
167 gdb_test "python print len(symtab)" "2" "Test decode_line current location"
168 gdb_test "python print symtab\[0\]" "None" "Test decode_line expression parse"
169 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line current location"
170 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python.c.*" "Test decode_line current locationn filename"
171 gdb_test "python print symtab\[1\]\[0\].line" "22" "Test decode_line current location line number"
172
173 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1
174 gdb_test "python print len(symtab)" "2" "Test decode_line python.c:26 length"
175 gdb_test "python print symtab\[0\]" "if foo" "Test decode_line expression parse"
176 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line python.c:26 length"
177 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python.c.*" "Test decode_line python.c:26 filename"
178 gdb_test "python print symtab\[1\]\[0\].line" "26" "Test decode_line python.c:26 line number"
179
180 gdb_test "python gdb.decode_line(\"randomfunc\")" \
181 "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc"
182 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1
183 gdb_test "python print len(symtab)" "2" "Test decode_line func1 length"
184 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line func1 length"
185 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python-1.c.*" "Test decode_line func1 filename"
186 gdb_test "python print symtab\[1\]\[0\].line" "19" "Test decode_line func1 line number"
187
188 # gdb.write
189 gdb_test "python print sys.stderr" ".*__main__.GdbOutputErrorFile instance at.*" "Test stderr location"
190 gdb_test "python print sys.stdout" ".*__main__.GdbOutputFile instance at.*" "Test stdout location"
191 gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "Test default write"
192 gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "Test stderr write"
193 gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "Test stdout write"
194 gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "Test stdlog write"
195
196 # Deprecate maint set/show python print-stack
197 gdb_test "maint show python print-stack" \
198 "Warning: command 'maintenance show python print-stack' is deprecated.*Use 'show python print-stack'.*" \
199 "Test deprecation maint show warning."
200 gdb_test "maint set python print-stack off" \
201 "Warning: command 'maintenance set python print-stack' is deprecated.*Use 'set python print-stack'.*" \
202 "Test deprecation maint set warning."
203 gdb_test "show python print-stack" \
204 "Whether Python stack will be printed on error is off.*" \
205 "Test print-backtrace show setting. Default off."
206 gdb_py_test_silent_cmd "set python print-stack on" \
207 "Test print-backtrace set setting" 1
208 gdb_test "show python print-stack" \
209 "Whether Python stack will be printed on error is on.*" \
210
211 # Test prompt substituion
212
213 gdb_py_test_multiple "prompt substitution" \
214 "python" "" \
215 "someCounter = 0" "" \
216 "def prompt(current):" "" \
217 " global someCounter" "" \
218 " if (current == \"testfake \"):" "" \
219 " return None" "" \
220 " someCounter = someCounter + 1" "" \
221 " return \"py prompt \" + str (someCounter) + \" \"" "" \
222 "end" ""
223
224 gdb_py_test_multiple "prompt substitution readline" \
225 "python" "" \
226 "pCounter = 0" "" \
227 "def program_prompt(current):" "" \
228 " global pCounter" "" \
229 " if (current == \">\"):" "" \
230 " pCounter = pCounter + 1" "" \
231 " return \"python line \" + str (pCounter) + \": \"" "" \
232 " return None" "" \
233 "end" ""
234
235 set newprompt "py prompt 1"
236 set newprompt2 "py prompt 2"
237 set testfake "testfake"
238
239 gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" {
240 -re "\[\r\n\]$newprompt $" {
241 pass "set hook"
242 }
243 }
244
245 gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" {
246 -re "\[\r\n\]$testfake $" {
247 pass "set prompt testfake"
248 }
249 }
250
251 gdb_test_multiple "show prompt" "show testfake prompt" {
252 -re "Gdb's prompt is \"$testfake \"..* $" {
253 pass "show prompt shows guarded prompt"
254 }
255 }
256
257 gdb_test_multiple "set prompt blah " "set blah in GDB" {
258 -re "\[\r\n\]$newprompt2 $" {
259 pass "set prompt blah overriden"
260 }
261 }
262
263 gdb_test_multiple "python gdb.prompt_hook = None" "Delete hook" {
264 -re "\[\r\n\]$newprompt2 $" {
265 pass "Delete old hook"
266 }
267 }
268
269 gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" {
270 -re "\[\r\n\]$gdb_prompt $" {
271 pass "set default prompt"
272 }
273 }
274
275 gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" {
276 -re "\[\r\n\]$gdb_prompt $" {
277 pass "set programming hook"
278 }
279 }
280
281 gdb_test_multiple "python" "test we ignore substituion for seconday prompts" {
282 -re "\r\n>$" {
283 pass "readline secondary are not substituted"
284 }
285 }
286
287 gdb_test_multiple "end" "end programming" {
288 -re "\[\r\n\]$gdb_prompt $" {
289 pass "end programming"
290 }
291 }
292
293 gdb_py_test_multiple "prompt substitution readline" \
294 "python" "" \
295 "import gdb.command.prompt" "" \
296 "end" ""
297
298 gdb_test_multiple "set extended-prompt one two three " \
299 "set basic extended prompt" {
300 -re "\[\r\n\]one two three $" {
301 pass "set basic extended prompt"
302 }
303 }
304
305 gdb_test_multiple "set extended-prompt \\w " \
306 "set extended prompt working directory" {
307 -re "\[\r\n\].*gdb.*testsuite.* $" {
308 pass "set extended prompt working directory"
309 }
310 }
311
312 gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \
313 "set extended prompt parameter" {
314 -re "\[\r\n\]some param True $" {
315 pass "set extended prompt parameter"
316 }
317 }