2011-08-17 Phil Muldoon <pmuldoon@redhat.com>
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / python.exp
1 # Copyright (C) 2008, 2009, 2010, 2011 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 load_lib gdb-python.exp
24
25 set testfile "python"
26 set srcfile ${testfile}.c
27 set srcfile1 ${testfile}-1.c
28 set binfile ${objdir}/${subdir}/${testfile}
29
30 if { [gdb_compile "${srcdir}/${subdir}/${srcfile} ${srcdir}/${subdir}/${srcfile1}" \
31 ${binfile} executable {debug}] != "" } {
32 untested "Could not compile $binfile."
33 return -1
34 }
35
36 # Start with a fresh gdb.
37 gdb_exit
38 gdb_start
39 gdb_reinitialize_dir $srcdir/$subdir
40
41 # Skip all tests if Python scripting is not enabled.
42 if { [skip_python_tests] } { continue }
43
44 gdb_test_multiple "python print 23" "verify python support" {
45 -re "not supported.*$gdb_prompt $" {
46 unsupported "python support is disabled"
47
48 # If Python is not supported, verify that sourcing a python script
49 # causes an error.
50 gdb_test "source $srcdir/$subdir/source2.py" "Error in sourced command file:.*"
51 return -1
52 }
53 -re "$gdb_prompt $" {}
54 }
55
56 gdb_py_test_multiple "multi-line python command" \
57 "python" "" \
58 "print 23" "" \
59 "end" "23"
60
61 gdb_py_test_multiple "show python command" \
62 "define zzq" "Type commands for definition of .* just \"end\"\\.*" \
63 "python" "" \
64 "print 23" "" \
65 "end" "" \
66 "end" "" \
67 "show user zzq" "User command \"zzq\":.* python.*print 23.* end"
68
69 gdb_py_test_multiple "indented multi-line python command" \
70 "python" "" \
71 "def foo ():" "" \
72 " print 'hello, world!'" "" \
73 "foo ()" "" \
74 "end" "hello, world!"
75
76 gdb_test "source $srcdir/$subdir/source2.py" "yes"
77
78 gdb_test "python print gdb.current_objfile()" "None"
79 gdb_test "python print gdb.objfiles()" "\\\[\\\]"
80
81 # Test http://bugs.python.org/issue4434 workaround in configure.ac
82 gdb_test "python import itertools; print 'IMPOR'+'TED'" "IMPORTED" "pythonX.Y/lib-dynload/*.so"
83
84 gdb_test_no_output \
85 "python x = gdb.execute('printf \"%d\", 23', to_string = True)"
86 gdb_test "python print x" "23"
87
88 # Test post_event.
89 gdb_py_test_multiple "post event insertion" \
90 "python" "" \
91 "someVal = 0" "" \
92 "class Foo(object):" "" \
93 " def __call__(self):" "" \
94 " global someVal" "" \
95 " someVal += 1" "" \
96 "gdb.post_event(Foo())" "" \
97 "end" ""
98
99 gdb_test "python print someVal" "1" "test post event execution"
100 gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" "Test non callable class"
101
102 # Test (no) pagination of the executed command.
103 gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
104 set lines 10
105 gdb_test_no_output "set height $lines"
106
107 set test "verify pagination beforehand"
108 gdb_test_multiple "python print \"\\n\" * $lines" $test {
109 -re "---Type <return> to continue, or q <return> to quit---$" {
110 pass $test
111 }
112 }
113 gdb_test "q" "Quit" "verify pagination beforehand: q"
114
115 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"
116
117 set test "verify pagination afterwards"
118 gdb_test_multiple "python print \"\\n\" * $lines" $test {
119 -re "---Type <return> to continue, or q <return> to quit---$" {
120 pass $test
121 }
122 }
123 gdb_test "q" "Quit" "verify pagination afterwards: q"
124
125 gdb_test_no_output "set height 0"
126
127 gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout"
128
129 gdb_test "python print a" ".*aliases -- Aliases of other commands.*" "verify help to uiout"
130
131 # Test PR 12212, using InfThread.selected_thread() when no inferior is
132 # loaded.
133 gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1
134 gdb_test "python print nothread == None" "True" "Ensure that no threads are returned"
135
136 # Start with a fresh gdb.
137 clean_restart ${testfile}
138
139 # The following tests require execution.
140
141 if ![runto_main] then {
142 fail "Can't run to main"
143 return 0
144 }
145
146 runto [gdb_get_line_number "Break to end."]
147
148 # Test gdb.decode_line.
149 gdb_test "python gdb.decode_line(\"main.c:43\")" \
150 "gdb.error: No source file named main.c.*" "test decode_line no source named main"
151
152 gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1
153 gdb_test "python print len(symtab)" "2" "Test decode_line current location"
154 gdb_test "python print symtab\[0\]" "None" "Test decode_line expression parse"
155 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line current location"
156 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python.c.*" "Test decode_line current locationn filename"
157 gdb_test "python print symtab\[1\]\[0\].line" "22" "Test decode_line current location line number"
158
159 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1
160 gdb_test "python print len(symtab)" "2" "Test decode_line python.c:26 length"
161 gdb_test "python print symtab\[0\]" "if foo" "Test decode_line expression parse"
162 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line python.c:26 length"
163 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python.c.*" "Test decode_line python.c:26 filename"
164 gdb_test "python print symtab\[1\]\[0\].line" "26" "Test decode_line python.c:26 line number"
165
166 gdb_test "python gdb.decode_line(\"randomfunc\")" \
167 "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc"
168 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1
169 gdb_test "python print len(symtab)" "2" "Test decode_line func1 length"
170 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line func1 length"
171 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python-1.c.*" "Test decode_line func1 filename"
172 gdb_test "python print symtab\[1\]\[0\].line" "19" "Test decode_line func1 line number"
173
174 # gdb.write
175 gdb_test "python print sys.stderr" ".*__main__.GdbOutputErrorFile instance at.*" "Test stderr location"
176 gdb_test "python print sys.stdout" ".*__main__.GdbOutputFile instance at.*" "Test stdout location"
177 gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "Test default write"
178 gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "Test stderr write"
179 gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "Test stdout write"
180 gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "Test stdlog write"
181
182 # Deprecate maint set/show python print-stack
183 gdb_test "maint show python print-stack" \
184 "Warning: command 'maintenance show python print-stack' is deprecated.*Use 'show python print-stack'.*" \
185 "Test deprecation maint show warning."
186 gdb_test "maint set python print-stack off" \
187 "Warning: command 'maintenance set python print-stack' is deprecated.*Use 'set python print-stack'.*" \
188 "Test deprecation maint set warning."
189 gdb_test "show python print-stack" \
190 "Whether Python stack will be printed on error is off.*" \
191 "Test print-backtrace show setting. Default off."
192 gdb_py_test_silent_cmd "set python print-stack on" \
193 "Test print-backtrace set setting" 1
194 gdb_test "show python print-stack" \
195 "Whether Python stack will be printed on error is on.*" \
196
197 # Test prompt substituion
198
199 gdb_py_test_multiple "prompt substitution" \
200 "python" "" \
201 "someCounter = 0" "" \
202 "def prompt(current):" "" \
203 " global someCounter" "" \
204 " if (current == \"testfake \"):" "" \
205 " return None" "" \
206 " someCounter = someCounter + 1" "" \
207 " return \"py prompt \" + str (someCounter) + \" \"" "" \
208 "end" ""
209
210 gdb_py_test_multiple "prompt substitution readline" \
211 "python" "" \
212 "pCounter = 0" "" \
213 "def program_prompt(current):" "" \
214 " global pCounter" "" \
215 " if (current == \">\"):" "" \
216 " pCounter = pCounter + 1" "" \
217 " return \"python line \" + str (pCounter) + \": \"" "" \
218 " return None" "" \
219 "end" ""
220
221 set newprompt "py prompt 1"
222 set newprompt2 "py prompt 2"
223 set testfake "testfake"
224
225 gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" {
226 -re "\[\r\n\]$newprompt $" {
227 pass "set hook"
228 }
229 }
230
231 gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" {
232 -re "\[\r\n\]$testfake $" {
233 pass "set prompt testfake"
234 }
235 }
236
237 gdb_test_multiple "show prompt" "show testfake prompt" {
238 -re "Gdb's prompt is \"$testfake \"..* $" {
239 pass "show prompt shows guarded prompt"
240 }
241 }
242
243 gdb_test_multiple "set prompt blah " "set blah in GDB" {
244 -re "\[\r\n\]$newprompt2 $" {
245 pass "set prompt blah overriden"
246 }
247 }
248
249 gdb_test_multiple "python gdb.prompt_hook = None" "Delete hook" {
250 -re "\[\r\n\]$newprompt2 $" {
251 pass "Delete old hook"
252 }
253 }
254
255 gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" {
256 -re "\[\r\n\]$gdb_prompt $" {
257 pass "set default prompt"
258 }
259 }
260
261 gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" {
262 -re "\[\r\n\]$gdb_prompt $" {
263 pass "set programming hook"
264 }
265 }
266
267 gdb_test_multiple "python" "test we ignore substituion for seconday prompts" {
268 -re "\r\n>$" {
269 pass "readline secondary are not substituted"
270 }
271 }
272
273 gdb_test_multiple "end" "end programming" {
274 -re "\[\r\n\]$gdb_prompt $" {
275 pass "end programming"
276 }
277 }
278
279 gdb_py_test_multiple "prompt substitution readline" \
280 "python" "" \
281 "import gdb.command.prompt" "" \
282 "end" ""
283
284 gdb_test_multiple "set extended-prompt one two three " \
285 "set basic extended prompt" {
286 -re "\[\r\n\]one two three $" {
287 pass "set basic extended prompt"
288 }
289 }
290
291 gdb_test_multiple "set extended-prompt \\w " \
292 "set extended prompt working directory" {
293 -re "\[\r\n\].*gdb.*testsuite.* $" {
294 pass "set extended prompt working directory"
295 }
296 }
297
298 gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \
299 "set extended prompt parameter" {
300 -re "\[\r\n\]some param True $" {
301 pass "set extended prompt parameter"
302 }
303 }
This page took 0.036869 seconds and 5 git commands to generate.