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