1 # Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file is part of the GDB testsuite. It tests the mechanism
17 # exposing values to Python.
24 set srcfile ${testfile}.c
25 set libfile "python-sl"
26 set libsrc ${libfile}.c
27 set library ${objdir}/${subdir}/${libfile}.sl
28 set binfile ${objdir}/${subdir}/${testfile}
30 if { [gdb_compile_shlib ${srcdir}/${subdir}/${libsrc} ${library} "debug"] != "" } {
31 untested "Could not compile shared library."
35 set exec_opts [list debug shlib=${library}]
37 if { [gdb_compile ${srcdir}/${subdir}/${srcfile} ${binfile} executable $exec_opts] != "" } {
38 untested "Could not compile $binfile."
42 # Start with a fresh gdb.
45 gdb_reinitialize_dir $srcdir/$subdir
47 # Skip all tests if Python scripting is not enabled.
48 if { [skip_python_tests] } { continue }
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.
52 proc gdb_py_test_silent_cmd {cmd name report_pass} {
55 gdb_test_multiple $cmd $name {
56 -re "Traceback.*$gdb_prompt $" { fail $name }
57 -re "$gdb_prompt $" { if $report_pass { pass $name } }
61 gdb_test_multiple "python print 23" "verify python support" {
62 -re "not supported.*$gdb_prompt $" {
63 unsupported "python support is disabled"
65 # If Python is not supported, verify that sourcing a python script
67 gdb_test "source $srcdir/$subdir/source2.py" "Error in sourced command file:.*"
70 -re "$gdb_prompt $" {}
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.
77 proc gdb_py_test_multiple {name args} {
79 foreach {input result} $args {
80 if {[gdb_test_multiple $input "$name - $input" {
81 -re "\[\r\n\]*($result)\[\r\n\]+($gdb_prompt | *>)$" {
91 gdb_py_test_multiple "multi-line python command" \
96 gdb_py_test_multiple "show python command" \
97 "define zzq" "Type commands for definition of .* just \"end\"\\.*" \
102 "show user zzq" "User command \"zzq\":.* python.*print 23.* end"
104 gdb_py_test_multiple "indented multi-line python command" \
107 " print 'hello, world!'" "" \
109 "end" "hello, world!"
111 gdb_test "source $srcdir/$subdir/source2.py" "yes"
113 gdb_test "python print gdb.current_objfile()" "None"
114 gdb_test "python print gdb.objfiles()" "\\\[\\\]"
116 # Test http://bugs.python.org/issue4434 workaround in configure.ac
117 gdb_test "python import itertools; print 'IMPOR'+'TED'" "IMPORTED" "pythonX.Y/lib-dynload/*.so"
120 "python x = gdb.execute('printf \"%d\", 23', to_string = True)"
121 gdb_test "python print x" "23"
124 gdb_py_test_multiple "post event insertion" \
127 "class Foo(object):" "" \
128 " def __call__(self):" "" \
129 " global someVal" "" \
131 "gdb.post_event(Foo())" "" \
134 gdb_test "python print someVal" "1" "test post event execution"
135 gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" "Test non callable class"
137 # Test (no) pagination of the executed command.
138 gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
140 gdb_test_no_output "set height $lines"
142 set test "verify pagination beforehand"
143 gdb_test_multiple "python print \"\\n\" * $lines" $test {
144 -re "---Type <return> to continue, or q <return> to quit---$" {
148 gdb_test "q" "Quit" "verify pagination beforehand: q"
150 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"
152 set test "verify pagination afterwards"
153 gdb_test_multiple "python print \"\\n\" * $lines" $test {
154 -re "---Type <return> to continue, or q <return> to quit---$" {
158 gdb_test "q" "Quit" "verify pagination afterwards: q"
160 # Start with a fresh gdb.
161 clean_restart ${testfile}
163 # The following tests require execution.
165 if ![runto_main] then {
166 fail "Can't run to main"
170 runto [gdb_get_line_number "Break to end."]
172 # Test gdb.decode_line.
173 gdb_test "python gdb.decode_line(\"main.c:43\")" \
174 "RuntimeError: No source file named main.c.*" "test decode_line no source named main"
176 gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1
177 gdb_test "python print len(symtab)" "2" "Test decode_line current location"
178 gdb_test "python print symtab\[0\]" "None" "Test decode_line expression parse"
179 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line current location"
180 gdb_test "python print symtab\[1\]\[0\].symtab" "gdb/testsuite/gdb.python/python.c.*" "Test decode_line current locationn filename"
181 gdb_test "python print symtab\[1\]\[0\].line" "22" "Test decode_line current location line number"
183 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1
184 gdb_test "python print len(symtab)" "2" "Test decode_line python.c:26 length"
185 gdb_test "python print symtab\[0\]" "if foo" "Test decode_line expression parse"
186 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line python.c:26 length"
187 gdb_test "python print symtab\[1\]\[0\].symtab" "gdb/testsuite/gdb.python/python.c.*" "Test decode_line python.c:26 filename"
188 gdb_test "python print symtab\[1\]\[0\].line" "26" "Test decode_line python.c:26 line number"
190 gdb_test "python gdb.decode_line(\"randomfunc\")" \
191 "RuntimeError: Function \"randomfunc\" not defined.*" "test decode_line randomfunc"
192 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1
193 gdb_test "python print len(symtab)" "2" "Test decode_line func1 length"
194 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line func1 length"
195 gdb_test "python print symtab\[1\]\[0\].symtab" "gdb/testsuite/gdb.python/python-sl.c.*" "Test decode_line func1 filename"
196 gdb_test "python print symtab\[1\]\[0\].line" "19" "Test decode_line func1 line number"
198 # Test gdb.solib_name
199 gdb_test "p &func1" "" "func1 address"
200 gdb_py_test_silent_cmd "python func1 = gdb.history(0)" "Aquire func1 address" 1
201 gdb_test "python print gdb.solib_name(long(func1))" "gdb/testsuite/gdb.python/python-sl.sl" "test func1 solib location"
203 gdb_test "p &main" "" "main address"
204 gdb_py_test_silent_cmd "python main = gdb.history(0)" "Aquire main address" 1
205 gdb_test "python print gdb.solib_name(long(main))" "None" "test main solib location"