2012-05-13 Siva Chandra Reddy <sivachandra@google.com>
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / python.exp
1 # Copyright (C) 2008-2012 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 load_lib gdb-python.exp
20
21 set testfile "python"
22 set srcfile ${testfile}.c
23 set srcfile1 ${testfile}-1.c
24 set binfile ${objdir}/${subdir}/${testfile}
25
26 if { [gdb_compile "${srcdir}/${subdir}/${srcfile} ${srcdir}/${subdir}/${srcfile1}" \
27 ${binfile} executable {debug}] != "" } {
28 untested "Could not compile $binfile."
29 return -1
30 }
31
32 # Start with a fresh gdb.
33 gdb_exit
34 gdb_start
35 gdb_reinitialize_dir $srcdir/$subdir
36
37 # Skip all tests if Python scripting is not enabled.
38 if { [skip_python_tests] } { continue }
39
40 gdb_test_multiple "python print 23" "verify python support" {
41 -re "not supported.*$gdb_prompt $" {
42 unsupported "python support is disabled"
43
44 # If Python is not supported, verify that sourcing a python script
45 # causes an error.
46 gdb_test "source $srcdir/$subdir/source2.py" \
47 "Error in sourced command file:.*" \
48 "source source2.py when python disabled"
49 return -1
50 }
51 -re "$gdb_prompt $" {}
52 }
53
54 gdb_py_test_multiple "multi-line python command" \
55 "python" "" \
56 "print 23" "" \
57 "end" "23"
58
59 gdb_py_test_multiple "show python command" \
60 "define zzq" "Type commands for definition of .* just \"end\"\\.*" \
61 "python" "" \
62 "print 23" "" \
63 "end" "" \
64 "end" "" \
65 "show user zzq" "User command \"zzq\":.* python.*print 23.* end"
66
67 gdb_py_test_multiple "indented multi-line python command" \
68 "python" "" \
69 "def foo ():" "" \
70 " print 'hello, world!'" "" \
71 "foo ()" "" \
72 "end" "hello, world!"
73
74 gdb_test "source $srcdir/$subdir/source2.py" "yes" "source source2.py"
75
76 gdb_test "source -s source2.py" "yes" "source -s source2.py"
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>" {
110 exp_continue
111 }
112 -re " to continue, or q <return>" {
113 exp_continue
114 }
115 -re " to quit---$" {
116 pass $test
117 }
118 }
119 gdb_test "q" "Quit" "verify pagination beforehand: q"
120
121 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"
122
123 set test "verify pagination afterwards"
124 gdb_test_multiple "python print \"\\n\" * $lines" $test {
125 -re "---Type <return>" {
126 exp_continue
127 }
128 -re " to continue, or q <return>" {
129 exp_continue
130 }
131 -re " to quit---$" {
132 pass $test
133 }
134 }
135 gdb_test "q" "Quit" "verify pagination afterwards: q"
136
137 gdb_test_no_output "set height 0"
138
139 gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout"
140
141 gdb_test "python print a" ".*aliases -- Aliases of other commands.*" "verify help to uiout"
142
143 # Test PR 12212, using InfThread.selected_thread() when no inferior is
144 # loaded.
145 gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1
146 gdb_test "python print nothread == None" "True" "Ensure that no threads are returned"
147
148 # Start with a fresh gdb.
149 clean_restart ${testfile}
150
151 # The following tests require execution.
152
153 if ![runto_main] then {
154 fail "Can't run to main"
155 return 0
156 }
157
158 runto [gdb_get_line_number "Break to end."]
159
160 # Test gdb.decode_line.
161 gdb_test "python gdb.decode_line(\"main.c:43\")" \
162 "gdb.error: No source file named main.c.*" "test decode_line no source named main"
163
164 gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1
165 gdb_test "python print len(symtab)" "2" "Test decode_line current location"
166 gdb_test "python print symtab\[0\]" "None" "Test decode_line expression parse"
167 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line current location"
168 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python.c.*" "Test decode_line current locationn filename"
169 gdb_test "python print symtab\[1\]\[0\].line" "22" "Test decode_line current location line number"
170
171 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1
172 gdb_test "python print len(symtab)" "2" "Test decode_line python.c:26 length"
173 gdb_test "python print symtab\[0\]" "if foo" "Test decode_line expression parse"
174 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line python.c:26 length"
175 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python.c.*" "Test decode_line python.c:26 filename"
176 gdb_test "python print symtab\[1\]\[0\].line" "26" "Test decode_line python.c:26 line number"
177
178 gdb_test "python gdb.decode_line(\"randomfunc\")" \
179 "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc"
180 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1
181 gdb_test "python print len(symtab)" "2" "Test decode_line func1 length"
182 gdb_test "python print len(symtab\[1\])" "1" "Test decode_line func1 length"
183 gdb_test "python print symtab\[1\]\[0\].symtab" ".*gdb.python/python-1.c.*" "Test decode_line func1 filename"
184 gdb_test "python print symtab\[1\]\[0\].line" "19" "Test decode_line func1 line number"
185 gdb_py_test_silent_cmd {python symtab = gdb.decode_line ("func1,func2")} \
186 "test decode_line func1,func2" 1
187 gdb_test {python print symtab[0]} ",func2" "stop at comma in linespec"
188
189 # gdb.write
190 gdb_test "python print sys.stderr" ".*__main__.GdbOutputErrorFile instance at.*" "Test stderr location"
191 gdb_test "python print sys.stdout" ".*__main__.GdbOutputFile instance at.*" "Test stdout location"
192 gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "Test default write"
193 gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "Test stderr write"
194 gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "Test stdout write"
195 gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "Test stdlog write"
196
197 # print-stack
198 gdb_test "show python print-stack" \
199 "The mode of Python stack printing on error is \"message\".*" \
200 "Test print-backtrace show setting. Default is message."
201 gdb_py_test_silent_cmd "set python print-stack full" \
202 "Test print-backtrace set setting" 1
203 gdb_test "show python print-stack" \
204 "The mode of Python stack printing on error is \"full\".*" \
205 "Test print-backtrace show setting to full."
206
207 # Test prompt substituion
208
209 gdb_py_test_multiple "prompt substitution" \
210 "python" "" \
211 "someCounter = 0" "" \
212 "def prompt(current):" "" \
213 " global someCounter" "" \
214 " if (current == \"testfake \"):" "" \
215 " return None" "" \
216 " someCounter = someCounter + 1" "" \
217 " return \"py prompt \" + str (someCounter) + \" \"" "" \
218 "end" ""
219
220 gdb_py_test_multiple "prompt substitution readline" \
221 "python" "" \
222 "pCounter = 0" "" \
223 "def program_prompt(current):" "" \
224 " global pCounter" "" \
225 " if (current == \">\"):" "" \
226 " pCounter = pCounter + 1" "" \
227 " return \"python line \" + str (pCounter) + \": \"" "" \
228 " return None" "" \
229 "end" ""
230
231 set newprompt "py prompt 1"
232 set newprompt2 "py prompt 2"
233 set testfake "testfake"
234
235 gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" {
236 -re "\[\r\n\]$newprompt $" {
237 pass "set hook"
238 }
239 }
240
241 gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" {
242 -re "\[\r\n\]$testfake $" {
243 pass "set prompt testfake"
244 }
245 }
246
247 gdb_test_multiple "show prompt" "show testfake prompt" {
248 -re "Gdb's prompt is \"$testfake \"..* $" {
249 pass "show prompt shows guarded prompt"
250 }
251 }
252
253 gdb_test_multiple "set prompt blah " "set blah in GDB" {
254 -re "\[\r\n\]$newprompt2 $" {
255 pass "set prompt blah overriden"
256 }
257 }
258
259 gdb_test_multiple "python gdb.prompt_hook = None" "Delete hook" {
260 -re "\[\r\n\]$newprompt2 $" {
261 pass "Delete old hook"
262 }
263 }
264
265 gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" {
266 -re "\[\r\n\]$gdb_prompt $" {
267 pass "set default prompt"
268 }
269 }
270
271 gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" {
272 -re "\[\r\n\]$gdb_prompt $" {
273 pass "set programming hook"
274 }
275 }
276
277 gdb_test_multiple "python" "test we ignore substituion for seconday prompts" {
278 -re "\r\n>$" {
279 pass "readline secondary are not substituted"
280 }
281 }
282
283 gdb_test_multiple "end" "end programming" {
284 -re "\[\r\n\]$gdb_prompt $" {
285 pass "end programming"
286 }
287 }
288
289 gdb_py_test_multiple "prompt substitution readline" \
290 "python" "" \
291 "import gdb.command.prompt" "" \
292 "end" ""
293
294 gdb_test_multiple "set extended-prompt one two three " \
295 "set basic extended prompt" {
296 -re "\[\r\n\]one two three $" {
297 pass "set basic extended prompt"
298 }
299 }
300
301 gdb_test_multiple "set extended-prompt \\w " \
302 "set extended prompt working directory" {
303 -re "\[\r\n\].*gdb.*testsuite.* $" {
304 pass "set extended prompt working directory"
305 }
306 }
307
308 gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \
309 "set extended prompt parameter" {
310 -re "\[\r\n\]some param full $" {
311 pass "set extended prompt parameter"
312 }
313 }
314
315 # Start with a fresh gdb.
316 clean_restart ${testfile}
317
318 # The following tests require execution.
319
320 if ![runto_main] then {
321 fail "Can't run to main"
322 return 0
323 }
324
325 # print-stack settings
326 gdb_test "show python print-stack" \
327 "The mode of Python stack printing on error is \"message\".*" \
328 "Test print-backtrace show setting. Default is message."
329 gdb_py_test_silent_cmd "set python print-stack full" \
330 "Test print-backtrace set setting" 1
331 gdb_test "show python print-stack" \
332 "The mode of Python stack printing on error is \"full\".*" \
333 "Test print-backtrace show setting to full."
334 gdb_py_test_silent_cmd "set python print-stack none" \
335 "Test print-backtrace set setting" 1
336 gdb_test "show python print-stack" \
337 "The mode of Python stack printing on error is \"none\".*" \
338 "Test print-backtrace show setting to none."
339
340 gdb_py_test_silent_cmd "set python print-stack message" \
341 "Test print-backtrace set setting" 1
342
343 gdb_py_test_multiple "prompt substitution readline" \
344 "python" "" \
345 "pCounter = 0" "" \
346 "def error_prompt(current):" "" \
347 " raise RuntimeError(\"Python exception called\")" "" \
348 "end" ""
349
350 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
351 -re "Python Exception <type 'exceptions.RuntimeError'> Python exception called.*" {
352 pass "set hook"
353 }
354 }
355
356 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
357 "set the hook to default" 1
358
359 gdb_py_test_silent_cmd "set python print-stack full" \
360 "Test print-backtrace set setting" 1
361
362 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
363 -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*" {
364 pass "set hook"
365 }
366 }
367
368 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
369 "set the hook to default" 1
370
371 # Start with a fresh gdb.
372 clean_restart ${testfile}
373
374 # The following tests require execution.
375
376 if ![runto_main] then {
377 fail "Can't run to main"
378 return 0
379 }
380
381 runto [gdb_get_line_number "Break at func2 call site."]
382
383 gdb_py_test_silent_cmd "python line = gdb.selected_frame().find_sal().line" "Get line number of func2 call site" 1
384 gdb_test "python print gdb.find_pc_line(gdb.selected_frame().pc()).line == line" "True" "Test find_pc_line at func2 call site"
385
386 gdb_py_test_silent_cmd "step" "Step into func2" 1
387 gdb_py_test_silent_cmd "up" "Step out of func2" 1
388
389 gdb_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.040749 seconds and 5 git commands to generate.