2012-01-16 Pedro Alves <palves@redhat.com>
[external/binutils.git] / gdb / testsuite / gdb.python / py-finish-breakpoint.exp
1 # Copyright (C) 2011-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 if {[skip_shlib_tests]} {
20         untested py-finish-breakpoint.exp
21     return 0
22 }
23
24 load_lib gdb-python.exp
25
26 set libfile "py-events-shlib"
27 set libsrc  $srcdir/$subdir/$libfile.c
28 set lib_sl  $objdir/$subdir/$libfile-nodebug.so
29 set lib_opts ""
30
31 set testfile "py-finish-breakpoint"
32 set srcfile ${testfile}.c
33 set binfile ${objdir}/${subdir}/${testfile}
34 set exec_opts [list debug shlib=$lib_sl]
35
36 if [get_compiler_info ${binfile}] {
37     return -1
38 }
39
40 if { [gdb_compile_shlib $libsrc $lib_sl $lib_opts] != ""
41      || [gdb_compile $srcdir/$subdir/$srcfile $binfile executable $exec_opts] != ""} {
42     untested "Could not compile either $libsrc or $srcdir/$subdir/$srcfile."
43     return -1
44 }
45
46 # Start with a fresh gdb.
47 clean_restart ${testfile}
48
49 set python_file ${srcdir}/${subdir}/${testfile}.py
50
51
52 # Skip all tests if Python scripting is not enabled.
53 if { [skip_python_tests] } { continue }
54
55 #
56 # Test FinishBreakpoint in normal conditions
57 #
58
59 clean_restart ${testfile}
60 gdb_load_shlibs ${lib_sl}
61
62 if ![runto_main] then {
63     fail "Cannot run to main."
64     return 0
65 }
66
67 gdb_test_no_output "set confirm off" "disable confirmation"
68 gdb_test "source $python_file" "Python script imported.*" \
69          "import python scripts"
70 gdb_breakpoint "increase_1"
71 gdb_test "continue" "Breakpoint .*at.*" "continue to the function to finish"
72
73 # set FinishBreakpoint
74
75 gdb_test "python finishbp_default = gdb.FinishBreakpoint ()" \
76          "Temporary breakpoint.*" "set FinishBreakpoint with default frame value"
77 gdb_test "python finishbp = MyFinishBreakpoint (gdb.parse_and_eval ('a'), gdb.newest_frame ())" \
78          "Temporary breakpoint.*" "set FinishBreakpoint"
79 gdb_test "python print finishbp.return_value" "None.*" \
80          "check return_value at init"
81
82 # check normal bp hit
83
84 gdb_test "continue" "MyFinishBreakpoint stop with.*return_value is: -5.*#0.*increase.*" \
85          "check MyFinishBreakpoint hit"
86 gdb_test "python print finishbp.return_value" "-5.*" "check return_value"
87
88 gdb_test "python print finishbp_default.hit_count" "1.*" "check finishBP on default frame has been hit"
89 gdb_test "python print finishbp.is_valid()" "False.*"\
90          "ensure that finish bp is invalid afer normal hit"
91
92 # check FinishBreakpoint in main no allowed
93
94 gdb_test "finish" "main.*" "return to main()"
95 gdb_test "python MyFinishBreakpoint (None, gdb.selected_frame ())" \
96          "ValueError: \"FinishBreakpoint\" not meaningful in the outermost frame..*" \
97          "check FinishBP not allowed in main"
98
99 #
100 # Test FinishBreakpoint with no debug symbol 
101 #
102
103 clean_restart ${testfile}
104 gdb_load_shlibs ${lib_sl}
105
106 gdb_test "source $python_file" "Python script imported.*" \
107          "import python scripts"
108 set cond_line [gdb_get_line_number "Condition Break."]
109
110 if ![runto_main] then {
111     fail "Cannot run to main."
112     return 0
113 }
114
115 gdb_test "print do_nothing" "no debug info.*" "ensure that shared lib has no debug info"
116 gdb_breakpoint "do_nothing" {temporary}
117 gdb_test "continue" "Temporary breakpoint .*in \\.?do_nothing.*" \
118          "continue to do_nothing"
119
120 gdb_test "python finishBP = SimpleFinishBreakpoint(gdb.newest_frame())" \
121          "SimpleFinishBreakpoint init" \
122          "set finish breakpoint"
123 gdb_test "continue" "SimpleFinishBreakpoint stop.*" "check FinishBreakpoint hit"
124 gdb_test "python print finishBP.return_value" "None" "check return value without debug symbol"
125
126 #
127 # Test FinishBreakpoint in function returned by longjmp 
128 #
129
130 clean_restart ${testfile}
131 gdb_load_shlibs ${lib_sl}
132
133 gdb_test "source $python_file" "Python script imported.*" \
134          "import python scripts"
135
136 if ![runto call_longjmp_1] then {
137     perror "couldn't run to breakpoint call_longjmp"
138     continue
139 }
140
141 gdb_test "python finishbp = SimpleFinishBreakpoint(gdb.newest_frame())" \
142          "SimpleFinishBreakpoint init" \
143          "set finish breakpoint" 
144 gdb_test "break [gdb_get_line_number "after longjmp."]" "Breakpoint.* at .*" \
145          "set BP after the jump"
146 gdb_test "continue" "SimpleFinishBreakpoint out of scope.*" \
147          "check FinishBP out of scope notification"
148 gdb_test "python print finishbp.is_valid()" "False.*"\
149          "ensure that finish bp is invalid afer out of scope notification"
150
151 #
152 # Test FinishBreakpoint in BP condition evaluation 
153 # (finish in dummy frame)
154 #
155
156 clean_restart ${testfile}
157 gdb_load_shlibs ${lib_sl}
158
159 gdb_test "source $python_file" "Python script imported.*" \
160          "import python scripts"
161
162
163 if ![runto_main] then {
164     fail "Cannot run to main."
165     return 0
166 }
167          
168 gdb_test "break ${cond_line} if test_1(i,8)" "Breakpoint .* at .*" \
169          "set a conditional BP"
170 gdb_test "python TestBreakpoint()" "TestBreakpoint init" \
171          "set FinishBP in a breakpoint condition"
172 gdb_test "continue" \
173          "\"FinishBreakpoint\" cannot be set on a dummy frame.*" \
174          "don't allow FinishBreakpoint on dummy frames"
175 gdb_test "print i" "8" "check stopped location"
176
177 #
178 # Test FinishBreakpoint in BP condition evaluation 
179 # (finish in normal frame)
180 #
181
182 clean_restart ${testfile}
183 gdb_load_shlibs ${lib_sl}
184
185 gdb_test "source $python_file" "Python script imported.*" \
186          "import python scripts"
187
188 if ![runto_main] then {
189     fail "Cannot run to main."
190     return 0
191 }
192
193 gdb_test "break ${cond_line} if test(i,8)" \
194          "Breakpoint .* at .*" "set conditional BP"
195 gdb_test "python TestBreakpoint()" "TestBreakpoint init" "set BP in condition"
196
197 gdb_test "continue" \
198          "test don't stop: 1.*test don't stop: 2.*test stop.*Error in testing breakpoint condition.*The program being debugged stopped while in a function called from GDB.*" \
199          "stop in condition function"
200
201 gdb_test "continue" "Continuing.*" "finish condition evaluation"
202 gdb_test "continue" "Breakpoint.*" "stop at conditional breakpoint"
203 gdb_test "print i" "8" "check stopped location"
204
205 #
206 # Test FinishBreakpoint in explicit inferior function call
207 #
208
209 clean_restart ${testfile}
210 gdb_load_shlibs ${lib_sl}
211
212 gdb_test "source $python_file" "Python script imported.*" \
213          "import python scripts"
214
215 if ![runto_main] then {
216     fail "Cannot run to main."
217     return 0
218 }
219
220 # return address in dummy frame
221
222 gdb_test "python TestExplicitBreakpoint('increase_1')" "Breakpoint.*at.*" \
223          "prepare TestExplicitBreakpoint"
224 gdb_test "print increase_1(&i)" \
225          "\"FinishBreakpoint\" cannot be set on a dummy frame.*" \
226          "don't allow FinishBreakpoint on dummy frames"
227
228 # return address in normal frame
229
230 delete_breakpoints
231 gdb_test "python TestExplicitBreakpoint(\"increase_1\")" "Breakpoint.*at.*" \
232          "prepare TestExplicitBreakpoint"
233 gdb_test "print increase(&i)" \
234          "SimpleFinishBreakpoint init.*SimpleFinishBreakpoint stop.*The program being debugged stopped while in a function called from GDB.*" \
235          "FinishBP stop at during explicit function call"
236
237
238 #
239 # Test FinishBreakpoint when inferior exits
240 #
241
242 if ![runto "test_exec_exit"] then {
243     fail "Cannot run to test_exec_exit."
244     return 0
245 }
246
247 gdb_test_no_output "set var self_exec = 0" "switch to exit() test"
248 gdb_test "python SimpleFinishBreakpoint(gdb.newest_frame())" "SimpleFinishBreakpoint init" "set FinishBP after the exit()"
249 gdb_test "continue" "SimpleFinishBreakpoint out of scope.*" "catch out of scope after exit"
250
251 #
252 # Test FinishBreakpoint when inferior execs
253 #
254
255 if ![runto "test_exec_exit"] then {
256     fail "Cannot run to test_exec_exit."
257     return 0
258 }     
259
260 gdb_test "python SimpleFinishBreakpoint(gdb.newest_frame())" "SimpleFinishBreakpoint init" "set FinishBP after the exec"
261 gdb_test "catch exec" "Catchpoint.*\(exec\).*" "catch exec"
262 gdb_test "continue" "SimpleFinishBreakpoint out of scope.*" "catch out of scope after exec"