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