Remove path from gdb.python/py-pp-integral.exp "source" test.
[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 # 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"
38
39 # If Python is not supported, verify that sourcing a python script
40 # causes an error.
41 gdb_test "source $srcdir/$subdir/source2.py" \
42 "Error in sourced command file:.*" \
43 "source source2.py when python disabled"
44 return -1
45 }
46 -re "$gdb_prompt $" {}
47 }
48
49 gdb_py_test_multiple "multi-line python command" \
50 "python" "" \
51 "print (23)" "" \
52 "end" "23"
53
54 gdb_py_test_multiple "show python command" \
55 "define zzq" "Type commands for definition of .* just \"end\"\\.*" \
56 "python" "" \
57 "print (23)" "" \
58 "end" "" \
59 "end" "" \
60 "show user zzq" "User command \"zzq\":.* python.*print \\(23\\).* end"
61
62 gdb_py_test_multiple "indented multi-line python command" \
63 "python" "" \
64 "def foo ():" "" \
65 " print ('hello, world!')" "" \
66 "foo ()" "" \
67 "end" "hello, world!"
68
69 gdb_test "source $srcdir/$subdir/source2.py" "yes" "source source2.py"
70
71 gdb_test "source -s source2.py" "yes" "source -s source2.py"
72
73 gdb_test "python print (gdb.current_objfile())" "None"
74 gdb_test "python print (gdb.objfiles())" "\\\[\\\]"
75
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"
78
79 gdb_test_no_output \
80 "python x = gdb.execute('printf \"%d\", 23', to_string = True)"
81 gdb_test "python print (x)" "23"
82
83 # Test post_event.
84 gdb_py_test_multiple "post event insertion" \
85 "python" "" \
86 "someVal = 0" "" \
87 "class Foo(object):" "" \
88 " def __call__(self):" "" \
89 " global someVal" "" \
90 " someVal += 1" "" \
91 "gdb.post_event(Foo())" "" \
92 "end" ""
93
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"
96
97 # Test (no) pagination of the executed command.
98 gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
99 set lines 10
100 gdb_test_no_output "set height $lines"
101
102 set test "verify pagination beforehand"
103 gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
104 -re "---Type <return>" {
105 exp_continue
106 }
107 -re " to continue, or q <return>" {
108 exp_continue
109 }
110 -re " to quit---$" {
111 pass $test
112 }
113 }
114 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination beforehand: q"
115
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"
117
118 set test "verify pagination afterwards"
119 gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
120 -re "---Type <return>" {
121 exp_continue
122 }
123 -re " to continue, or q <return>" {
124 exp_continue
125 }
126 -re " to quit---$" {
127 pass $test
128 }
129 }
130 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination afterwards: q"
131
132 gdb_test_no_output "set height 0"
133
134 gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout"
135
136 gdb_test "python print (a)" ".*aliases -- Aliases of other commands.*" "verify help to uiout"
137
138 # Test PR 12212, using InfThread.selected_thread() when no inferior is
139 # loaded.
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"
142
143 gdb_py_test_multiple "register atexit function" \
144 "python" "" \
145 "import atexit" "" \
146 "def printit(arg):" "" \
147 " print (arg)" "" \
148 "atexit.register(printit, 'good bye world')" "" \
149 "end" ""
150
151 send_gdb "quit\n"
152 gdb_expect {
153 -re "good bye world" {
154 pass "atexit handling"
155 }
156 default {
157 fail "atexit handling"
158 }
159 }
160
161 # Start with a fresh gdb.
162 clean_restart ${testfile}
163
164 # The following tests require execution.
165
166 if ![runto_main] then {
167 fail "Can't run to main"
168 return 0
169 }
170
171 runto [gdb_get_line_number "Break to end."]
172
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"
176
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"
183
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"
190
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"
201
202 # gdb.write
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"
209
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
213
214 # Test prompt substituion
215
216 gdb_py_test_multiple "prompt substitution" \
217 "python" "" \
218 "someCounter = 0" "" \
219 "def prompt(current):" "" \
220 " global someCounter" "" \
221 " if (current == \"testfake \"):" "" \
222 " return None" "" \
223 " someCounter = someCounter + 1" "" \
224 " return \"py prompt \" + str (someCounter) + \" \"" "" \
225 "end" ""
226
227 gdb_py_test_multiple "prompt substitution readline" \
228 "python" "" \
229 "pCounter = 0" "" \
230 "def program_prompt(current):" "" \
231 " global pCounter" "" \
232 " if (current == \">\"):" "" \
233 " pCounter = pCounter + 1" "" \
234 " return \"python line \" + str (pCounter) + \": \"" "" \
235 " return None" "" \
236 "end" ""
237
238 set newprompt "py prompt 1"
239 set newprompt2 "py prompt 2"
240 set testfake "testfake"
241
242 gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" {
243 -re "\[\r\n\]$newprompt $" {
244 pass "set hook"
245 }
246 }
247
248 gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" {
249 -re "\[\r\n\]$testfake $" {
250 pass "set prompt testfake"
251 }
252 }
253
254 gdb_test_multiple "show prompt" "show testfake prompt" {
255 -re "Gdb's prompt is \"$testfake \"..* $" {
256 pass "show prompt shows guarded prompt"
257 }
258 }
259
260 gdb_test_multiple "set prompt blah " "set blah in GDB" {
261 -re "\[\r\n\]$newprompt2 $" {
262 pass "set prompt blah overriden"
263 }
264 }
265
266 gdb_test_multiple "python gdb.prompt_hook = None" "Delete hook" {
267 -re "\[\r\n\]$newprompt2 $" {
268 pass "Delete old hook"
269 }
270 }
271
272 gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" {
273 -re "\[\r\n\]$gdb_prompt $" {
274 pass "set default prompt"
275 }
276 }
277
278 gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" {
279 -re "\[\r\n\]$gdb_prompt $" {
280 pass "set programming hook"
281 }
282 }
283
284 gdb_test_multiple "python" "test we ignore substituion for seconday prompts" {
285 -re "\r\n>$" {
286 pass "readline secondary are not substituted"
287 }
288 }
289
290 gdb_test_multiple "end" "end programming" {
291 -re "\[\r\n\]$gdb_prompt $" {
292 pass "end programming"
293 }
294 }
295
296 gdb_py_test_multiple "prompt substitution readline" \
297 "python" "" \
298 "import gdb.command.prompt" "" \
299 "end" ""
300
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"
305 }
306 }
307
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"
312 }
313 }
314
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"
319 }
320 }
321
322 # Start with a fresh gdb.
323 clean_restart ${testfile}
324
325 # The following tests require execution.
326
327 if ![runto_main] then {
328 fail "Can't run to main"
329 return 0
330 }
331
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"
346
347 gdb_py_test_silent_cmd "set python print-stack message" \
348 "Test print-stack set setting to message" 1
349
350 gdb_py_test_multiple "prompt substitution readline" \
351 "python" "" \
352 "pCounter = 0" "" \
353 "def error_prompt(current):" "" \
354 " raise RuntimeError(\"Python exception called\")" "" \
355 "end" ""
356
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.*" {
359 pass "set hook"
360 }
361 }
362
363 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
364 "set the hook to default" 1
365
366 gdb_py_test_silent_cmd "set python print-stack full" \
367 "set print-stack full for prompt error test" 1
368
369 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
370 -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*" {
371 pass "set hook"
372 }
373 }
374
375 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
376 "set the hook to default" 1
377
378 # Start with a fresh gdb.
379 clean_restart ${testfile}
380
381 # The following tests require execution.
382
383 if ![runto_main] then {
384 fail "Can't run to main"
385 return 0
386 }
387
388 runto [gdb_get_line_number "Break at func2 call site."]
389
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"
392
393 gdb_py_test_silent_cmd "step" "Step into func2" 1
394 gdb_py_test_silent_cmd "up" "Step out of func2" 1
395
396 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.038072 seconds and 4 git commands to generate.