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