* gdb.python/py-breakpoint.exp: Split up into several functions,
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-breakpoint.exp
1 # Copyright (C) 2010-2013 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 breakpoints to Python.
18
19 load_lib gdb-python.exp
20
21 standard_testfile
22
23 if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
24 return -1
25 }
26
27 # Skip all tests if Python scripting is not enabled.
28 if { [skip_python_tests] } { continue }
29
30 proc test_bkpt_basic { } {
31 global srcfile testfile hex decimal
32
33 with_test_prefix "test_bkpt_basic" {
34 # Start with a fresh gdb.
35 clean_restart ${testfile}
36
37 if ![runto_main] then {
38 fail "Cannot run to main."
39 return 0
40 }
41
42 # Initially there should be one breakpoint: main.
43 gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" "Get Breakpoint List" 0
44 gdb_test "python print (blist\[0\])" "<gdb.Breakpoint object at $hex>" "Check obj exists"
45 gdb_test "python print (blist\[0\].location)" "main." "Check breakpoint location"
46
47 set mult_line [gdb_get_line_number "Break at multiply."]
48 gdb_breakpoint ${mult_line}
49 gdb_continue_to_breakpoint "Break at multiply."
50
51 # Check that the Python breakpoint code noted the addition of a
52 # breakpoint "behind the scenes".
53 gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" "Get Breakpoint List" 0
54 gdb_test "python print (len(blist))" "2" "Check for two breakpoints"
55 gdb_test "python print (blist\[0\])" "<gdb.Breakpoint object at $hex>" "Check obj exists"
56 gdb_test "python print (blist\[0\].location)" "main." "Check breakpoint location"
57 gdb_test "python print (blist\[1\])" "<gdb.Breakpoint object at $hex>" "Check obj exists"
58
59 gdb_test "python print (blist\[1\].location)" "py-breakpoint\.c:${mult_line}*" \
60 "Check breakpoint location"
61
62 # Check hit and ignore counts.
63 gdb_test "python print (blist\[1\].hit_count)" "1" "Check breakpoint hit count"
64 gdb_py_test_silent_cmd "python blist\[1\].ignore_count = 4" "Set breakpoint hit count" 0
65 gdb_continue_to_breakpoint "Break at multiply."
66 gdb_test "python print (blist\[1\].hit_count)" "6" "Check breakpoint hit count"
67 gdb_test "print result" " = 545" "Check expected variable result after 6 iterations"
68
69 # Test breakpoint is enabled and disabled correctly..
70 gdb_breakpoint [gdb_get_line_number "Break at add."]
71 gdb_continue_to_breakpoint "Break at add."
72 gdb_test "python print (blist\[1\].enabled)" "True" "Check breakpoint enabled."
73 gdb_py_test_silent_cmd "python blist\[1\].enabled = False" "Set breakpoint disabled." 0
74 gdb_continue_to_breakpoint "Break at add."
75 gdb_py_test_silent_cmd "python blist\[1\].enabled = True" "Set breakpoint enabled." 0
76 gdb_continue_to_breakpoint "Break at multiply."
77
78 # Test other getters and setters.
79 gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" "Get Breakpoint List" 0
80 gdb_test "python print (blist\[1\].thread)" "None" "Check breakpoint thread"
81 gdb_test "python print (blist\[1\].type == gdb.BP_BREAKPOINT)" "True" "Check breakpoint type"
82 gdb_test "python print (blist\[0\].number)" "1" "Check breakpoint number"
83 gdb_test "python print (blist\[1\].number)" "2" "Check breakpoint number"
84 gdb_test "python print (blist\[2\].number)" "3" "Check breakpoint number"
85 }
86 }
87
88 proc test_bkpt_deletion { } {
89 global srcfile testfile hex decimal
90
91 with_test_prefix test_bkpt_deletion {
92 # Start with a fresh gdb.
93 clean_restart ${testfile}
94
95 if ![runto_main] then {
96 fail "Cannot run to main."
97 return 0
98 }
99
100 # Test breakpoints are deleted correctly.
101 set deltst_location [gdb_get_line_number "Break at multiply."]
102 set end_location [gdb_get_line_number "Break at end."]
103 gdb_py_test_silent_cmd "python dp1 = gdb.Breakpoint (\"$deltst_location\")" "Set breakpoint" 0
104 gdb_breakpoint [gdb_get_line_number "Break at end."]
105 gdb_py_test_silent_cmd "python del_list = gdb.breakpoints()" "Get Breakpoint List" 0
106 gdb_test "python print (len(del_list))" "3" "Number of breakpoints before delete"
107 gdb_continue_to_breakpoint "Break at multiply." ".*/$srcfile:$deltst_location.*"
108 gdb_py_test_silent_cmd "python dp1.delete()" "Delete Breakpoint" 0
109 gdb_test "python print (dp1.number)" "RuntimeError: Breakpoint 2 is invalid.*" "Check breakpoint invalidated"
110 gdb_py_test_silent_cmd "python del_list = gdb.breakpoints()" "Get Breakpoint List" 0
111 gdb_test "python print (len(del_list))" "2" "Number of breakpoints after delete"
112 gdb_continue_to_breakpoint "Break at end." ".*/$srcfile:$end_location.*"
113 }
114 }
115
116 proc test_bkpt_cond_and_cmds { } {
117 global srcfile testfile hex decimal
118
119 with_test_prefix test_bkpt_cond_and_cmds {
120 # Start with a fresh gdb.
121 clean_restart ${testfile}
122
123 if ![runto_main] then {
124 fail "Cannot run to main."
125 return 0
126 }
127
128 # Test conditional setting.
129 set bp_location1 [gdb_get_line_number "Break at multiply."]
130 gdb_py_test_silent_cmd "python bp1 = gdb.Breakpoint (\"$bp_location1\")" "Set breakpoint" 0
131 gdb_continue_to_breakpoint "Break at multiply."
132 gdb_py_test_silent_cmd "python bp1.condition = \"i == 5\"" "Set breakpoint" 0
133 gdb_test "python print (bp1.condition)" "i == 5" "Test conditional has been set"
134 gdb_continue_to_breakpoint "Break at multiply."
135 gdb_test "print i" "5" "Test conditional breakpoint stopped after five iterations"
136 gdb_py_test_silent_cmd "python bp1.condition = None" "Clear condition" 0
137 gdb_test "python print (bp1.condition)" "None" "Test conditional read"
138 gdb_continue_to_breakpoint "Break at multiply."
139 gdb_test "print i" "6" "Test breakpoint stopped after six iterations"
140
141 # Test commands.
142 gdb_breakpoint [gdb_get_line_number "Break at add."]
143 set test {commands $bpnum}
144 gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
145 set test {print "Command for breakpoint has been executed."}
146 gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
147 set test {print result}
148 gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
149 gdb_test "end"
150
151 gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" "Get Breakpoint List" 0
152 gdb_test "python print (blist\[len(blist)-1\].commands)" "print \"Command for breakpoint has been executed.\".*print result"
153 }
154 }
155
156 proc test_bkpt_invisible { } {
157 global srcfile testfile hex decimal
158
159 with_test_prefix test_bkpt_invisible {
160 # Start with a fresh gdb.
161 clean_restart ${testfile}
162
163 if ![runto_main] then {
164 fail "Cannot run to main."
165 return 0
166 }
167
168 delete_breakpoints
169 set ibp_location [gdb_get_line_number "Break at multiply."]
170 gdb_py_test_silent_cmd "python ibp = gdb.Breakpoint(\"$ibp_location\", internal=False)" "Set invisible breakpoint" 0
171 gdb_py_test_silent_cmd "python ilist = gdb.breakpoints()" "Get Breakpoint List" 0
172 gdb_test "python print (ilist\[0\])" "<gdb.Breakpoint object at $hex>" "Check invisible bp obj exists"
173 gdb_test "python print (ilist\[0\].location)" "py-breakpoint\.c:$ibp_location*" "Check breakpoint location"
174 gdb_test "python print (ilist\[0\].visible)" "True" "Check breakpoint visibility"
175 gdb_test "info breakpoints" "py-breakpoint\.c:$ibp_location.*" "Check info breakpoints shows visible breakpoints"
176 delete_breakpoints
177 gdb_py_test_silent_cmd "python ibp = gdb.Breakpoint(\"$ibp_location\", internal=True)" "Set invisible breakpoint" 0
178 gdb_py_test_silent_cmd "python ilist = gdb.breakpoints()" "Get Breakpoint List" 0
179 gdb_test "python print (ilist\[0\])" "<gdb.Breakpoint object at $hex>" "Check invisible bp obj exists"
180 gdb_test "python print (ilist\[0\].location)" "py-breakpoint\.c:$ibp_location*" "Check breakpoint location"
181 gdb_test "python print (ilist\[0\].visible)" "False" "Check breakpoint visibility"
182 gdb_test "info breakpoints" "No breakpoints or watchpoints.*" "Check info breakpoints does not show invisible breakpoints"
183 gdb_test "maint info breakpoints" "py-breakpoint\.c:$ibp_location.*" "Check maint info breakpoints shows invisible breakpoints"
184 }
185 }
186
187 proc test_watchpoints { } {
188 global srcfile testfile hex decimal
189
190 with_test_prefix test_watchpoints {
191 # Start with a fresh gdb.
192 clean_restart ${testfile}
193
194 # Disable hardware watchpoints if necessary.
195 if [target_info exists gdb,no_hardware_watchpoints] {
196 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
197 }
198
199 if ![runto_main] then {
200 fail "Cannot run to main."
201 return 0
202 }
203
204 gdb_py_test_silent_cmd "python wp1 = gdb.Breakpoint (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE )" "Set watchpoint" 0
205 gdb_test "continue" ".*\[Ww\]atchpoint.*result.*Old value = 0.*New value = 25.*main.*" "Test watchpoint write"
206 }
207 }
208
209 proc test_bkpt_internal { } {
210 global srcfile testfile hex decimal
211
212 with_test_prefix test_bkpt_internal {
213 # Start with a fresh gdb.
214 clean_restart ${testfile}
215
216 # Disable hardware watchpoints if necessary.
217 if [target_info exists gdb,no_hardware_watchpoints] {
218 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
219 }
220 if ![runto_main] then {
221 fail "Cannot run to main."
222 return 0
223 }
224 delete_breakpoints
225 gdb_py_test_silent_cmd "python wp1 = gdb.Breakpoint (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE, internal=True )" "Set watchpoint" 0
226 gdb_test "info breakpoints" "No breakpoints or watchpoints.*" "Check info breakpoints does not show invisible breakpoints"
227 gdb_test "maint info breakpoints" ".*watchpoint.*result.*" "Check maint info breakpoints shows invisible breakpoints"
228 gdb_test "continue" ".*\[Ww\]atchpoint.*result.*Old value = 0.*New value = 25.*" "Test watchpoint write"
229 }
230 }
231
232 proc test_bkpt_eval_funcs { } {
233 global srcfile testfile hex decimal
234
235 with_test_prefix test_bkpt_eval_funcs {
236 # Start with a fresh gdb.
237 clean_restart ${testfile}
238
239 # Disable hardware watchpoints if necessary.
240 if [target_info exists gdb,no_hardware_watchpoints] {
241 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
242 }
243 if ![runto_main] then {
244 fail "Cannot run to main."
245 return 0
246 }
247 delete_breakpoints
248
249 gdb_py_test_multiple "Sub-class a breakpoint" \
250 "python" "" \
251 "class bp_eval (gdb.Breakpoint):" "" \
252 " inf_i = 0" "" \
253 " count = 0" "" \
254 " def stop (self):" "" \
255 " self.count = self.count + 1" "" \
256 " self.inf_i = gdb.parse_and_eval(\"i\")" "" \
257 " if self.inf_i == 3:" "" \
258 " return True" "" \
259 " return False" "" \
260 "end" ""
261
262 gdb_py_test_multiple "Sub-class a second breakpoint" \
263 "python" "" \
264 "class bp_also_eval (gdb.Breakpoint):" "" \
265 " count = 0" "" \
266 " def stop (self):" "" \
267 " self.count = self.count + 1" "" \
268 " if self.count == 9:" "" \
269 " return True" "" \
270 " return False" "" \
271 "end" ""
272
273 gdb_py_test_multiple "Sub-class a third breakpoint" \
274 "python" "" \
275 "class basic (gdb.Breakpoint):" "" \
276 " count = 0" "" \
277 "end" ""
278
279 set bp_location2 [gdb_get_line_number "Break at multiply."]
280 set end_location [gdb_get_line_number "Break at end."]
281 gdb_py_test_silent_cmd "python eval_bp1 = bp_eval(\"$bp_location2\")" "Set breakpoint" 0
282 gdb_py_test_silent_cmd "python also_eval_bp1 = bp_also_eval(\"$bp_location2\")" "Set breakpoint" 0
283 gdb_py_test_silent_cmd "python never_eval_bp1 = bp_also_eval(\"$end_location\")" "Set breakpoint" 0
284 gdb_continue_to_breakpoint "Break at multiply." ".*/$srcfile:$bp_location2.*"
285 gdb_test "print i" "3" "Check inferior value matches python accounting"
286 gdb_test "python print (eval_bp1.inf_i)" "3" "Check python accounting matches inferior"
287 gdb_test "python print (also_eval_bp1.count)" "4" \
288 "Check non firing same-location breakpoint eval function was also called at each stop."
289 gdb_test "python print (eval_bp1.count)" "4" \
290 "Check non firing same-location breakpoint eval function was also called at each stop."
291
292 delete_breakpoints
293 set cond_bp [gdb_get_line_number "Break at multiply."]
294 gdb_py_test_silent_cmd "python eval_bp1 = bp_eval(\"$cond_bp\")" "Set breakpoint" 0
295 set test_cond {cond $bpnum}
296 gdb_test "$test_cond \"foo==3\"" "Cannot set a condition where a Python.*" \
297 "Check you cannot add a CLI condition to a Python breakpoint that" \
298 "has defined stop"
299 gdb_py_test_silent_cmd "python eval_bp2 = basic(\"$cond_bp\")" "Set breakpoint" 0
300 gdb_py_test_silent_cmd "python eval_bp2.condition = \"1==1\"" "Set a condition" 0
301 gdb_py_test_multiple "Construct an eval function" \
302 "python" "" \
303 "def stop_func ():" "" \
304 " return True" "" \
305 "end" ""
306
307 gdb_test "python eval_bp2.stop = stop_func" \
308 "RuntimeError: Cannot set 'stop' method.*" \
309 "Assign stop function to a breakpoint that has a condition"
310
311 delete_breakpoints
312 gdb_breakpoint [gdb_get_line_number "Break at multiply."]
313 gdb_py_test_silent_cmd "python check_eval = bp_eval(\"$bp_location2\")" "Set breakpoint" 0
314 gdb_test "python print (check_eval.count)" "0" \
315 "Test that evaluate function has not been yet executed (ie count = 0)"
316 gdb_continue_to_breakpoint "Break at multiply." ".*/$srcfile:$bp_location2.*"
317 gdb_test "python print (check_eval.count)" "1" \
318 "Test that evaluate function is run when location also has normal bp"
319
320 gdb_py_test_multiple "Sub-class a watchpoint" \
321 "python" "" \
322 "class wp_eval (gdb.Breakpoint):" "" \
323 " def stop (self):" "" \
324 " self.result = gdb.parse_and_eval(\"result\")" "" \
325 " if self.result == 788:" "" \
326 " return True" "" \
327 " return False" "" \
328 "end" ""
329
330 delete_breakpoints
331 gdb_py_test_silent_cmd "python wp1 = wp_eval (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE)" "Set watchpoint" 0
332 gdb_test "continue" ".*\[Ww\]atchpoint.*result.*Old value =.*New value = 788.*" "Test watchpoint write"
333 gdb_test "python print (never_eval_bp1.count)" "0" \
334 "Check that this unrelated breakpoints eval function was never called."
335 }
336 }
337
338 proc test_bkpt_temporary { } {
339 global srcfile testfile hex decimal
340
341 with_test_prefix test_bkpt_temporary {
342 # Start with a fresh gdb.
343 clean_restart ${testfile}
344
345 if ![runto_main] then {
346 fail "Cannot run to main."
347 return 0
348 }
349 delete_breakpoints
350
351 gdb_py_test_multiple "Sub-class and check temporary breakpoint" \
352 "python" "" \
353 "class temp_bp (gdb.Breakpoint):" "" \
354 " count = 0" "" \
355 " def stop (self):" "" \
356 " self.count = self.count + 1" "" \
357 " return True" "" \
358 "end" ""
359 set ibp_location [gdb_get_line_number "Break at multiply."]
360 gdb_py_test_silent_cmd "python ibp = temp_bp(\"$ibp_location\", temporary=True)" \
361 "Set temporary breakpoint" 0
362 gdb_test "info breakpoints" "2.*breakpoint.*del.*py-breakpoint\.c:$ibp_location.*" \
363 "Check info breakpoints shows breakpoint with temporary status"
364 gdb_test "python print (ibp.location)" "py-breakpoint\.c:$ibp_location*" \
365 "Check temporary breakpoint location"
366 gdb_test "python print (ibp.temporary)" "True" \
367 "Check breakpoint temporary status"
368 gdb_continue_to_breakpoint "Break at multiply." ".*/$srcfile:$ibp_location.*"
369 gdb_test "python print (ibp.count)" "1" \
370 "Check temporary stop callback executed before deletion."
371 gdb_test "python print (ibp.temporary)" "RuntimeError: Breakpoint 2 is invalid.*" \
372 "Check temporary breakpoint is deleted after being hit"
373 gdb_test "info breakpoints" "No breakpoints or watchpoints.*" \
374 "Check info breakpoints shows temporary breakpoint is deleted"
375 }
376 }
377
378 test_bkpt_basic
379 test_bkpt_deletion
380 test_bkpt_cond_and_cmds
381 test_bkpt_invisible
382 test_watchpoints
383 test_bkpt_internal
384 test_bkpt_eval_funcs
385 test_bkpt_temporary
This page took 0.039971 seconds and 4 git commands to generate.