]> Git Repo - binutils.git/blob - gdb/testsuite/gdb.python/python.exp
2012-01-16 Pedro Alves <[email protected]>
[binutils.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
186 # gdb.write
187 gdb_test "python print sys.stderr" ".*__main__.GdbOutputErrorFile instance at.*" "Test stderr location"
188 gdb_test "python print sys.stdout" ".*__main__.GdbOutputFile instance at.*" "Test stdout location"
189 gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "Test default write"
190 gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "Test stderr write"
191 gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "Test stdout write"
192 gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "Test stdlog write"
193
194 # print-stack
195 gdb_test "show python print-stack" \
196     "The mode of Python stack printing on error is \"message\".*" \
197     "Test print-backtrace show setting. Default is message."
198 gdb_py_test_silent_cmd "set python print-stack full" \
199     "Test print-backtrace set setting" 1
200 gdb_test "show python print-stack" \
201     "The mode of Python stack printing on error is \"full\".*" \
202     "Test print-backtrace show setting to full."
203
204 # Test prompt substituion
205
206 gdb_py_test_multiple "prompt substitution" \
207   "python" "" \
208   "someCounter = 0" "" \
209   "def prompt(current):" "" \
210   "   global someCounter" "" \
211   "   if (current == \"testfake \"):" "" \
212   "      return None" "" \
213   "   someCounter = someCounter + 1" "" \
214   "   return \"py prompt \" + str (someCounter) + \" \"" "" \
215   "end" ""
216
217 gdb_py_test_multiple "prompt substitution readline" \
218   "python" "" \
219   "pCounter = 0" "" \
220   "def program_prompt(current):" "" \
221   "   global pCounter" "" \
222   "   if (current == \">\"):" "" \
223   "      pCounter = pCounter + 1" "" \
224   "      return \"python line \" + str (pCounter) + \": \"" "" \
225   "   return None" "" \
226   "end" ""
227
228 set newprompt "py prompt 1"
229 set newprompt2 "py prompt 2"
230 set testfake "testfake"
231
232 gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" {
233     -re "\[\r\n\]$newprompt $" {
234         pass "set hook"
235     }
236 }
237
238 gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" {
239     -re "\[\r\n\]$testfake $" {
240         pass "set prompt testfake"
241     }
242 }
243
244 gdb_test_multiple "show prompt" "show testfake prompt" {
245     -re "Gdb's prompt is \"$testfake \"..* $" {
246         pass "show prompt shows guarded prompt"
247     }
248 }
249
250 gdb_test_multiple "set prompt blah " "set blah in GDB" {
251     -re "\[\r\n\]$newprompt2 $" {
252         pass "set prompt blah overriden"
253     }
254 }
255
256 gdb_test_multiple "python gdb.prompt_hook = None" "Delete hook" {
257     -re "\[\r\n\]$newprompt2 $" {
258         pass "Delete old hook"
259     }
260 }
261
262 gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" {
263     -re "\[\r\n\]$gdb_prompt $" {
264         pass "set default prompt"
265     }
266 }
267
268 gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" {
269     -re "\[\r\n\]$gdb_prompt $" {
270         pass "set programming hook"
271     }
272 }
273
274 gdb_test_multiple "python" "test we ignore substituion for seconday prompts" {
275     -re "\r\n>$" {
276         pass "readline secondary are not substituted"
277     }
278 }
279
280 gdb_test_multiple "end" "end programming" {
281     -re "\[\r\n\]$gdb_prompt $" {
282         pass "end programming"
283     }
284 }
285
286 gdb_py_test_multiple "prompt substitution readline" \
287   "python" "" \
288   "import gdb.command.prompt" "" \
289   "end" ""
290
291 gdb_test_multiple "set extended-prompt one two three " \
292     "set basic extended prompt" {
293     -re "\[\r\n\]one two three $" {
294         pass "set basic extended prompt"
295     }
296 }
297
298 gdb_test_multiple "set extended-prompt \\w " \
299     "set extended prompt working directory" {
300     -re "\[\r\n\].*gdb.*testsuite.* $" {
301         pass "set extended prompt working directory"
302     }
303 }
304
305 gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \
306     "set extended prompt parameter" {
307     -re "\[\r\n\]some param full $" {
308         pass "set extended prompt parameter"
309     }
310 }
311
312 # Start with a fresh gdb.
313 clean_restart ${testfile}
314
315 # The following tests require execution.
316
317 if ![runto_main] then {
318     fail "Can't run to main"
319     return 0
320 }
321
322 # print-stack settings
323 gdb_test "show python print-stack" \
324     "The mode of Python stack printing on error is \"message\".*" \
325     "Test print-backtrace show setting. Default is message."
326 gdb_py_test_silent_cmd "set python print-stack full" \
327     "Test print-backtrace set setting" 1
328 gdb_test "show python print-stack" \
329     "The mode of Python stack printing on error is \"full\".*" \
330     "Test print-backtrace show setting to full."
331 gdb_py_test_silent_cmd "set python print-stack none" \
332     "Test print-backtrace set setting" 1
333 gdb_test "show python print-stack" \
334     "The mode of Python stack printing on error is \"none\".*" \
335     "Test print-backtrace show setting to none."
336
337 gdb_py_test_silent_cmd "set python print-stack message" \
338     "Test print-backtrace set setting" 1
339
340 gdb_py_test_multiple "prompt substitution readline" \
341   "python" "" \
342   "pCounter = 0" "" \
343   "def error_prompt(current):" "" \
344   "   raise RuntimeError(\"Python exception called\")" "" \
345   "end" ""
346
347 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
348     -re "Python Exception <type 'exceptions.RuntimeError'> Python exception called.*" {
349         pass "set hook"
350     }
351 }
352
353 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
354     "set the hook to default" 1
355
356 gdb_py_test_silent_cmd "set python print-stack full" \
357     "Test print-backtrace set setting" 1
358
359 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
360     -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*" {
361         pass "set hook"
362     }
363 }
364
365 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
366     "set the hook to default" 1
This page took 0.047135 seconds and 4 git commands to generate.