]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.python/python.exp
2010-08-11 Tom Tromey <tromey@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / python.exp
CommitLineData
4c38e0a4 1# Copyright (C) 2008, 2009, 2010 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
19if $tracelevel then {
20 strace $tracelevel
21}
22
cb2e07a6
PM
23set testfile "python"
24set srcfile ${testfile}.c
25set libfile "python-sl"
26set libsrc ${libfile}.c
27set library ${objdir}/${subdir}/${libfile}.sl
28set binfile ${objdir}/${subdir}/${testfile}
29
30if { [gdb_compile_shlib ${srcdir}/${subdir}/${libsrc} ${library} "debug"] != "" } {
31 untested "Could not compile shared library."
32 return -1
33}
34
35set exec_opts [list debug shlib=${library}]
36
37if { [gdb_compile ${srcdir}/${subdir}/${srcfile} ${binfile} executable $exec_opts] != "" } {
38 untested "Could not compile $binfile."
39 return -1
40}
d57a3c85 41
cb2e07a6 42# Start with a fresh gdb.
d57a3c85
TJB
43gdb_exit
44gdb_start
45gdb_reinitialize_dir $srcdir/$subdir
46
cb2e07a6
PM
47# Skip all tests if Python scripting is not enabled.
48if { [skip_python_tests] } { continue }
49
50# Run a command in GDB, and report a failure if a Python exception is thrown.
51# If report_pass is true, report a pass if no exception is thrown.
52proc gdb_py_test_silent_cmd {cmd name report_pass} {
53 global gdb_prompt
54
55 gdb_test_multiple $cmd $name {
56 -re "Traceback.*$gdb_prompt $" { fail $name }
57 -re "$gdb_prompt $" { if $report_pass { pass $name } }
58 }
59}
60
d57a3c85
TJB
61gdb_test_multiple "python print 23" "verify python support" {
62 -re "not supported.*$gdb_prompt $" {
63 unsupported "python support is disabled"
9e14a9ba
JB
64
65 # If Python is not supported, verify that sourcing a python script
66 # causes an error.
67 gdb_test "source $srcdir/$subdir/source2.py" "Error in sourced command file:.*"
d57a3c85
TJB
68 return -1
69 }
70 -re "$gdb_prompt $" {}
71}
72
73# Usage: gdb_py_test_multiple NAME INPUT RESULT {INPUT RESULT}...
74# Run a test named NAME, consisting of multiple lines of input.
75# After each input line INPUT, search for result line RESULT.
76# Succeed if all results are seen; fail otherwise.
77proc gdb_py_test_multiple {name args} {
78 global gdb_prompt
79 foreach {input result} $args {
80 if {[gdb_test_multiple $input "$name - $input" {
81 -re "\[\r\n\]*($result)\[\r\n\]+($gdb_prompt | *>)$" {
82 pass "$name - $input"
83 }
84 }]} {
85 return 1
86 }
87 }
88 return 0
89}
90
91gdb_py_test_multiple "multi-line python command" \
92 "python" "" \
93 "print 23" "" \
94 "end" "23"
95
96gdb_py_test_multiple "show python command" \
97 "define zzq" "Type commands for definition of .* just \"end\"\\.*" \
98 "python" "" \
99 "print 23" "" \
100 "end" "" \
101 "end" "" \
adb483fe 102 "show user zzq" "User command \"zzq\":.* python.*print 23.* end"
311a4e6b
TJB
103
104gdb_py_test_multiple "indented multi-line python command" \
105 "python" "" \
106 "def foo ():" "" \
107 " print 'hello, world!'" "" \
108 "foo ()" "" \
109 "end" "hello, world!"
89c73ade 110
9e14a9ba
JB
111gdb_test "source $srcdir/$subdir/source2.py" "yes"
112
89c73ade
TT
113gdb_test "python print gdb.current_objfile()" "None"
114gdb_test "python print gdb.objfiles()" "\\\[\\\]"
3bebe2f2
JK
115
116# Test http://bugs.python.org/issue4434 workaround in configure.ac
117gdb_test "python import itertools; print 'IMPOR'+'TED'" "IMPORTED" "pythonX.Y/lib-dynload/*.so"
bc9f0842
TT
118
119gdb_test_no_output \
120 "python x = gdb.execute('printf \"%d\", 23', to_string = True)"
121gdb_test "python print x" "23"
5da1313b 122
ca5c20b6
PM
123# Test post_event.
124gdb_py_test_multiple "post event insertion" \
125 "python" "" \
126 "someVal = 0" "" \
127 "class Foo():" "" \
128 " def __call__(self):" "" \
129 " global someVal" "" \
130 " someVal += 1" "" \
131 "gdb.post_event(Foo())" "" \
132 "end" ""
133
134gdb_test "python print someVal" "1" "test post event execution"
135gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" "Test non callable class"
136
5da1313b
JK
137# Test (no) pagination of the executed command.
138gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
139set lines 10
140gdb_test_no_output "set height $lines"
141
142set test "verify pagination beforehand"
143gdb_test_multiple "python print \"\\n\" * $lines" $test {
144 -re "---Type <return> to continue, or q <return> to quit---$" {
145 pass $test
146 }
147}
148gdb_test "q" "Quit" "verify pagination beforehand: q"
149
150gdb_test "python if gdb.execute('python print \"\\\\n\" * $lines', to_string=True) == \"\\n\" * [expr $lines + 1]: print \"yes\"" "yes" "gdb.execute does not page"
151
152set test "verify pagination afterwards"
153gdb_test_multiple "python print \"\\n\" * $lines" $test {
154 -re "---Type <return> to continue, or q <return> to quit---$" {
155 pass $test
156 }
157}
158gdb_test "q" "Quit" "verify pagination afterwards: q"
cb2e07a6
PM
159
160# Start with a fresh gdb.
161clean_restart ${testfile}
162
163# The following tests require execution.
164
165if ![runto_main] then {
166 fail "Can't run to main"
167 return 0
168}
169
170runto [gdb_get_line_number "Break to end."]
171
172# Test gdb.decode_line.
173gdb_test "python gdb.decode_line(\"main.c:43\")" \
174 "RuntimeError: No source file named main.c.*" "test decode_line no source named main"
175
176gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1
177gdb_test "python print len(symtab)" "2" "Test decode_line current location"
178gdb_test "python print symtab\[0\]" "None" "Test decode_line expression parse"
179gdb_test "python print len(symtab\[1\])" "1" "Test decode_line current location"
180gdb_test "python print symtab\[1\]\[0\].symtab" "gdb/testsuite/gdb.python/python.c.*" "Test decode_line current locationn filename"
181gdb_test "python print symtab\[1\]\[0\].line" "22" "Test decode_line current location line number"
182
183gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1
184gdb_test "python print len(symtab)" "2" "Test decode_line python.c:26 length"
185gdb_test "python print symtab\[0\]" "if foo" "Test decode_line expression parse"
186gdb_test "python print len(symtab\[1\])" "1" "Test decode_line python.c:26 length"
187gdb_test "python print symtab\[1\]\[0\].symtab" "gdb/testsuite/gdb.python/python.c.*" "Test decode_line python.c:26 filename"
188gdb_test "python print symtab\[1\]\[0\].line" "26" "Test decode_line python.c:26 line number"
189
190gdb_test "python gdb.decode_line(\"randomfunc\")" \
191 "RuntimeError: Function \"randomfunc\" not defined.*" "test decode_line randomfunc"
192gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1
193gdb_test "python print len(symtab)" "2" "Test decode_line func1 length"
194gdb_test "python print len(symtab\[1\])" "1" "Test decode_line func1 length"
195gdb_test "python print symtab\[1\]\[0\].symtab" "gdb/testsuite/gdb.python/python-sl.c.*" "Test decode_line func1 filename"
196gdb_test "python print symtab\[1\]\[0\].line" "19" "Test decode_line func1 line number"
197
198# Test gdb.solib_name
199gdb_test "p &func1" "" "func1 address"
200gdb_py_test_silent_cmd "python func1 = gdb.history(0)" "Aquire func1 address" 1
201gdb_test "python print gdb.solib_name(long(func1))" "gdb/testsuite/gdb.python/python-sl.sl" "test func1 solib location"
202
203gdb_test "p &main" "" "main address"
204gdb_py_test_silent_cmd "python main = gdb.history(0)" "Aquire main address" 1
205gdb_test "python print gdb.solib_name(long(main))" "None" "test main solib location"