OSDN Git Service

1f4a2a873eb8ebd4baf9853867fdb54f555bf435
[pf3gnuchains/sourceware.git] / gdb / testsuite / gdb.python / python.exp
1 # Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
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
19 if $tracelevel then {
20     strace $tracelevel
21 }
22
23 set testfile "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}
29
30 if { [gdb_compile_shlib ${srcdir}/${subdir}/${libsrc} ${library} "debug"] != "" } {
31     untested "Could not compile shared library."
32     return -1
33 }
34
35 set exec_opts [list debug shlib=${library}]
36
37 if { [gdb_compile ${srcdir}/${subdir}/${srcfile} ${binfile} executable $exec_opts] != "" } {
38     untested "Could not compile $binfile."
39     return -1
40 }
41
42 # Start with a fresh gdb.
43 gdb_exit
44 gdb_start
45 gdb_reinitialize_dir $srcdir/$subdir
46
47 # Skip all tests if Python scripting is not enabled.
48 if { [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.
52 proc 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
61 gdb_test_multiple "python print 23" "verify python support" {
62     -re "not supported.*$gdb_prompt $"  {
63       unsupported "python support is disabled"
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:.*"
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.
77 proc 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
91 gdb_py_test_multiple "multi-line python command" \
92   "python" "" \
93   "print 23" "" \
94   "end" "23"
95
96 gdb_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" "" \
102   "show user zzq" "User command \"zzq\":.*  python.*print 23.*  end"
103
104 gdb_py_test_multiple "indented multi-line python command" \
105   "python" "" \
106   "def foo ():" "" \
107   "  print 'hello, world!'" "" \
108   "foo ()" "" \
109   "end" "hello, world!"
110
111 gdb_test "source $srcdir/$subdir/source2.py" "yes"
112
113 gdb_test "python print gdb.current_objfile()" "None"
114 gdb_test "python print gdb.objfiles()" "\\\[\\\]"
115
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"
118
119 gdb_test_no_output \
120     "python x = gdb.execute('printf \"%d\", 23', to_string = True)"
121 gdb_test "python print x" "23"
122
123 # Test post_event.
124 gdb_py_test_multiple "post event insertion" \
125   "python" "" \
126   "someVal = 0" "" \
127   "class Foo(object):" "" \
128   "  def __call__(self):" "" \
129   "    global someVal" "" \
130   "    someVal += 1" "" \
131   "gdb.post_event(Foo())" "" \
132   "end" ""
133
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"
136
137 # Test (no) pagination of the executed command.
138 gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
139 set lines 10
140 gdb_test_no_output "set height $lines"
141
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---$" {
145         pass $test
146     }
147 }
148 gdb_test "q" "Quit" "verify pagination beforehand: q"
149
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"
151
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---$" {
155         pass $test
156     }
157 }
158 gdb_test "q" "Quit" "verify pagination afterwards: q"
159
160 # Start with a fresh gdb.
161 clean_restart ${testfile}
162
163 # The following tests require execution.
164
165 if ![runto_main] then {
166     fail "Can't run to main"
167     return 0
168 }
169
170 runto [gdb_get_line_number "Break to end."]
171
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"
175
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"
182
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"
189
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"
197
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"
202
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"