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