Copy .py files to remote host
[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 set remote_source2_py [gdb_remote_download host \
34 ${srcdir}/${subdir}/source2.py]
35
36 # Do this instead of the skip_python_check.
37 # We want to do some tests when Python is not present.
38 gdb_test_multiple "python print (23)" "verify python support" {
39 -re "not supported.*$gdb_prompt $" {
40 unsupported "python support is disabled"
41
42 # If Python is not supported, verify that sourcing a python script
43 # causes an error.
44 gdb_test "source $remote_source2_py" \
45 "Error in sourced command file:.*" \
46 "source source2.py when python disabled"
47
48 # Verify multi-line python commands cause an error.
49 gdb_py_test_multiple "multi-line python command" \
50 "python" "" \
51 "print (23)" "" \
52 "end" "not supported.*"
53
54 return -1
55 }
56 -re "$gdb_prompt $" {}
57 }
58
59 gdb_py_test_multiple "multi-line python command" \
60 "python" "" \
61 "print (23)" "" \
62 "end" "23"
63
64 gdb_py_test_multiple "show python command" \
65 "define zzq" "Type commands for definition of .* just \"end\"\\.*" \
66 "python" "" \
67 "print (23)" "" \
68 "end" "" \
69 "end" "" \
70 "show user zzq" "User command \"zzq\":.* python.*print \\(23\\).* end"
71
72 gdb_py_test_multiple "indented multi-line python command" \
73 "python" "" \
74 "def foo ():" "" \
75 " print ('hello, world!')" "" \
76 "foo ()" "" \
77 "end" "hello, world!"
78
79 gdb_test "source $remote_source2_py" "yes" "source source2.py"
80
81 gdb_test "source -s source2.py" "yes" "source -s source2.py"
82
83 gdb_test "python print (gdb.current_objfile())" "None"
84 gdb_test "python print (gdb.objfiles())" "\\\[\\\]"
85
86 # Test http://bugs.python.org/issue4434 workaround in configure.ac
87 gdb_test "python import itertools; print ('IMPOR'+'TED')" "IMPORTED" "pythonX.Y/lib-dynload/*.so"
88
89 gdb_test_no_output \
90 "python x = gdb.execute('printf \"%d\", 23', to_string = True)"
91 gdb_test "python print (x)" "23"
92
93 # Test post_event.
94 gdb_py_test_multiple "post event insertion" \
95 "python" "" \
96 "someVal = 0" "" \
97 "class Foo(object):" "" \
98 " def __call__(self):" "" \
99 " global someVal" "" \
100 " someVal += 1" "" \
101 "gdb.post_event(Foo())" "" \
102 "end" ""
103
104 gdb_test "python print (someVal)" "1" "test post event execution"
105 gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" "Test non callable class"
106
107 # Test (no) pagination of the executed command.
108 gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
109 set lines 10
110 gdb_test_no_output "set height $lines"
111
112 set test "verify pagination beforehand"
113 gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
114 -re "---Type <return>" {
115 exp_continue
116 }
117 -re " to continue, or q <return>" {
118 exp_continue
119 }
120 -re " to quit---$" {
121 pass $test
122 }
123 }
124 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination beforehand: q"
125
126 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"
127
128 set test "verify pagination afterwards"
129 gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
130 -re "---Type <return>" {
131 exp_continue
132 }
133 -re " to continue, or q <return>" {
134 exp_continue
135 }
136 -re " to quit---$" {
137 pass $test
138 }
139 }
140 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination afterwards: q"
141
142 gdb_test_no_output "set height 0"
143
144 gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout"
145
146 gdb_test "python print (a)" ".*aliases -- Aliases of other commands.*" "verify help to uiout"
147
148 # Test PR 12212, using InfThread.selected_thread() when no inferior is
149 # loaded.
150 gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1
151 gdb_test "python print (nothread == None)" "True" "Ensure that no threads are returned"
152
153 gdb_py_test_multiple "register atexit function" \
154 "python" "" \
155 "import atexit" "" \
156 "def printit(arg):" "" \
157 " print (arg)" "" \
158 "atexit.register(printit, 'good bye world')" "" \
159 "end" ""
160
161 send_gdb "quit\n"
162 gdb_expect {
163 -re "good bye world" {
164 pass "atexit handling"
165 }
166 default {
167 fail "atexit handling"
168 }
169 }
170
171 # Start with a fresh gdb.
172 clean_restart ${testfile}
173
174 # The following tests require execution.
175
176 if ![runto_main] then {
177 fail "Can't run to main"
178 return 0
179 }
180
181 set lineno [gdb_get_line_number "Break to end."]
182 runto $lineno
183
184 # Test gdb.decode_line.
185 gdb_test "python gdb.decode_line(\"main.c:43\")" \
186 "gdb.error: No source file named main.c.*" "test decode_line no source named main"
187
188 gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1
189 gdb_test "python print (len(symtab))" "2" "Test decode_line current location"
190 gdb_test "python print (symtab\[0\])" "None" "Test decode_line expression parse"
191 gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line current location"
192 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*gdb.python/python.c.*" "Test decode_line current locationn filename"
193 gdb_test "python print (symtab\[1\]\[0\].line)" "$lineno" "Test decode_line current location line number"
194
195 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1
196 gdb_test "python print (len(symtab))" "2" "Test decode_line python.c:26 length"
197 gdb_test "python print (symtab\[0\])" "if foo" "Test decode_line expression parse"
198 gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line python.c:26 length"
199 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*gdb.python/python.c.*" "Test decode_line python.c:26 filename"
200 gdb_test "python print (symtab\[1\]\[0\].line)" "26" "Test decode_line python.c:26 line number"
201
202 gdb_test "python gdb.decode_line(\"randomfunc\")" \
203 "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc"
204 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1
205 gdb_test "python print (len(symtab))" "2" "Test decode_line func1 length"
206 gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line func1 length"
207 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*gdb.python/python-1.c.*" "Test decode_line func1 filename"
208 gdb_test "python print (symtab\[1\]\[0\].line)" "19" "Test decode_line func1 line number"
209 gdb_py_test_silent_cmd {python symtab = gdb.decode_line ("func1,func2")} \
210 "test decode_line func1,func2" 1
211 gdb_test {python print (symtab[0])} ",func2" "stop at comma in linespec"
212
213 # gdb.write
214 gdb_test "python print (sys.stderr)" ".*gdb.GdbOutputErrorFile (instance|object) at.*" "Test stderr location"
215 gdb_test "python print (sys.stdout)" ".*gdb.GdbOutputFile (instance|object) at.*" "Test stdout location"
216 gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "Test default write"
217 gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "Test stderr write"
218 gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "Test stdout write"
219 gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "Test stdlog write"
220
221 # Turn on full stack printing for subsequent tests.
222 gdb_py_test_silent_cmd "set python print-stack full" \
223 "Set print-stack full for prompt tests" 1
224
225 # Test prompt substituion
226
227 gdb_py_test_multiple "prompt substitution" \
228 "python" "" \
229 "someCounter = 0" "" \
230 "def prompt(current):" "" \
231 " global someCounter" "" \
232 " if (current == \"testfake \"):" "" \
233 " return None" "" \
234 " someCounter = someCounter + 1" "" \
235 " return \"py prompt \" + str (someCounter) + \" \"" "" \
236 "end" ""
237
238 gdb_py_test_multiple "prompt substitution readline" \
239 "python" "" \
240 "pCounter = 0" "" \
241 "def program_prompt(current):" "" \
242 " global pCounter" "" \
243 " if (current == \">\"):" "" \
244 " pCounter = pCounter + 1" "" \
245 " return \"python line \" + str (pCounter) + \": \"" "" \
246 " return None" "" \
247 "end" ""
248
249 set newprompt "py prompt 1"
250 set newprompt2 "py prompt 2"
251 set testfake "testfake"
252
253 gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" {
254 -re "\[\r\n\]$newprompt $" {
255 pass "set hook"
256 }
257 }
258
259 gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" {
260 -re "\[\r\n\]$testfake $" {
261 pass "set prompt testfake"
262 }
263 }
264
265 gdb_test_multiple "show prompt" "show testfake prompt" {
266 -re "Gdb's prompt is \"$testfake \"..* $" {
267 pass "show prompt shows guarded prompt"
268 }
269 }
270
271 gdb_test_multiple "set prompt blah " "set blah in GDB" {
272 -re "\[\r\n\]$newprompt2 $" {
273 pass "set prompt blah overriden"
274 }
275 }
276
277 gdb_test_multiple "python gdb.prompt_hook = None" "Delete hook" {
278 -re "\[\r\n\]$newprompt2 $" {
279 pass "Delete old hook"
280 }
281 }
282
283 gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" {
284 -re "\[\r\n\]$gdb_prompt $" {
285 pass "set default prompt"
286 }
287 }
288
289 gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" {
290 -re "\[\r\n\]$gdb_prompt $" {
291 pass "set programming hook"
292 }
293 }
294
295 gdb_test_multiple "python" "test we ignore substituion for seconday prompts" {
296 -re "\r\n>$" {
297 pass "readline secondary are not substituted"
298 }
299 }
300
301 gdb_test_multiple "end" "end programming" {
302 -re "\[\r\n\]$gdb_prompt $" {
303 pass "end programming"
304 }
305 }
306
307 gdb_py_test_multiple "prompt substitution readline" \
308 "python" "" \
309 "import gdb.command.prompt" "" \
310 "end" ""
311
312 gdb_test_multiple "set extended-prompt one two three " \
313 "set basic extended prompt" {
314 -re "\[\r\n\]one two three $" {
315 pass "set basic extended prompt"
316 }
317 }
318
319 gdb_test_multiple "set extended-prompt \\w " \
320 "set extended prompt working directory" {
321 -re "\[\r\n\].*gdb.*testsuite.* $" {
322 pass "set extended prompt working directory"
323 }
324 }
325
326 gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \
327 "set extended prompt parameter" {
328 -re "\[\r\n\]some param full $" {
329 pass "set extended prompt parameter"
330 }
331 }
332
333 # Start with a fresh gdb.
334 clean_restart ${testfile}
335
336 # The following tests require execution.
337
338 if ![runto_main] then {
339 fail "Can't run to main"
340 return 0
341 }
342
343 # print-stack settings
344 gdb_test "show python print-stack" \
345 "The mode of Python stack printing on error is \"message\".*" \
346 "Test print-stack show setting. Default is message."
347 gdb_py_test_silent_cmd "set python print-stack full" \
348 "Test print-stack set setting to full" 1
349 gdb_test "show python print-stack" \
350 "The mode of Python stack printing on error is \"full\".*" \
351 "Test print-stack show setting to full"
352 gdb_py_test_silent_cmd "set python print-stack none" \
353 "Test print-stack set setting to none" 1
354 gdb_test "show python print-stack" \
355 "The mode of Python stack printing on error is \"none\".*" \
356 "Test print-stack show setting to none"
357
358 gdb_py_test_silent_cmd "set python print-stack message" \
359 "Test print-stack set setting to message" 1
360
361 gdb_py_test_multiple "prompt substitution readline" \
362 "python" "" \
363 "pCounter = 0" "" \
364 "def error_prompt(current):" "" \
365 " raise RuntimeError(\"Python exception called\")" "" \
366 "end" ""
367
368 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
369 -re "Python Exception (exceptions.RuntimeError|<(type 'exceptions.|class ')RuntimeError'>) Python exception called.*" {
370 pass "set hook"
371 }
372 }
373
374 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
375 "set the hook to default" 1
376
377 gdb_py_test_silent_cmd "set python print-stack full" \
378 "set print-stack full for prompt error test" 1
379
380 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
381 -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*" {
382 pass "set hook"
383 }
384 }
385
386 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
387 "set the hook to default" 1
388
389 # Start with a fresh gdb.
390 clean_restart ${testfile}
391
392 # The following tests require execution.
393
394 if ![runto_main] then {
395 fail "Can't run to main"
396 return 0
397 }
398
399 runto [gdb_get_line_number "Break at func2 call site."]
400
401 gdb_py_test_silent_cmd "python line = gdb.selected_frame().find_sal().line" "Get line number of func2 call site" 1
402 gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line == line)" "True" "Test find_pc_line at func2 call site"
403
404 gdb_py_test_silent_cmd "step" "Step into func2" 1
405 gdb_py_test_silent_cmd "up" "Step out of func2" 1
406
407 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.046767 seconds and 5 git commands to generate.