]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.python/py-breakpoint.exp
Update years in copyright notice for the GDB files.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-breakpoint.exp
CommitLineData
8acc9f48 1# Copyright (C) 2010-2013 Free Software Foundation, Inc.
adc36818
PM
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
a2c09bd0
DE
19load_lib gdb-python.exp
20
b4a58790
TT
21standard_testfile
22
adc36818
PM
23if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
24 return -1
25}
26
adc36818
PM
27# Skip all tests if Python scripting is not enabled.
28if { [skip_python_tests] } { continue }
29
30if ![runto_main] then {
31 fail "Cannot run to main."
32 return 0
33}
34
35global hex decimal
36
37# Initially there should be one breakpoint: main.
38
39gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" "Get Breakpoint List" 0
9325cb04
PK
40gdb_test "python print (blist\[0\])" "<gdb.Breakpoint object at $hex>" "Check obj exists"
41gdb_test "python print (blist\[0\].location)" "main." "Check breakpoint location"
adc36818 42
cc72b2a2
KP
43set mult_line [gdb_get_line_number "Break at multiply."]
44gdb_breakpoint ${mult_line}
adc36818
PM
45gdb_continue_to_breakpoint "Break at multiply."
46
47# Check that the Python breakpoint code noted the addition of a
48# breakpoint "behind the scenes".
49gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" "Get Breakpoint List" 0
9325cb04
PK
50gdb_test "python print (len(blist))" "2" "Check for two breakpoints"
51gdb_test "python print (blist\[0\])" "<gdb.Breakpoint object at $hex>" "Check obj exists"
52gdb_test "python print (blist\[0\].location)" "main." "Check breakpoint location"
53gdb_test "python print (blist\[1\])" "<gdb.Breakpoint object at $hex>" "Check obj exists"
cc72b2a2 54
9325cb04 55gdb_test "python print (blist\[1\].location)" "py-breakpoint\.c:${mult_line}*" \
cc72b2a2 56 "Check breakpoint location"
adc36818
PM
57
58# Check hit and ignore counts.
9325cb04 59gdb_test "python print (blist\[1\].hit_count)" "1" "Check breakpoint hit count"
adc36818
PM
60gdb_py_test_silent_cmd "python blist\[1\].ignore_count = 4" "Set breakpoint hit count" 0
61gdb_continue_to_breakpoint "Break at multiply."
9325cb04 62gdb_test "python print (blist\[1\].hit_count)" "6" "Check breakpoint hit count"
8bfb830f 63gdb_test "print result" " = 545" "Check expected variable result after 6 iterations"
adc36818
PM
64
65# Test breakpoint is enabled and disabled correctly..
66gdb_breakpoint [gdb_get_line_number "Break at add."]
67gdb_continue_to_breakpoint "Break at add."
9325cb04 68gdb_test "python print (blist\[1\].enabled)" "True" "Check breakpoint enabled."
adc36818
PM
69gdb_py_test_silent_cmd "python blist\[1\].enabled = False" "Set breakpoint disabled." 0
70gdb_continue_to_breakpoint "Break at add."
71gdb_py_test_silent_cmd "python blist\[1\].enabled = True" "Set breakpoint enabled." 0
72gdb_continue_to_breakpoint "Break at multiply."
73
74# Test other getters and setters.
75gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" "Get Breakpoint List" 0
9325cb04
PK
76gdb_test "python print (blist\[1\].thread)" "None" "Check breakpoint thread"
77gdb_test "python print (blist\[1\].type == gdb.BP_BREAKPOINT)" "True" "Check breakpoint type"
78gdb_test "python print (blist\[0\].number)" "1" "Check breakpoint number"
79gdb_test "python print (blist\[1\].number)" "2" "Check breakpoint number"
80gdb_test "python print (blist\[2\].number)" "3" "Check breakpoint number"
adc36818 81
94b6973e
PM
82# Start with a fresh gdb.
83clean_restart ${testfile}
84
85if ![runto_main] then {
86 fail "Cannot run to main."
87 return 0
88}
89
90# Test breakpoints are deleted correctly.
91set deltst_location [gdb_get_line_number "Break at multiply."]
92set end_location [gdb_get_line_number "Break at end."]
93gdb_py_test_silent_cmd "python dp1 = gdb.Breakpoint (\"$deltst_location\")" "Set breakpoint" 0
94gdb_breakpoint [gdb_get_line_number "Break at end."]
95gdb_py_test_silent_cmd "python del_list = gdb.breakpoints()" "Get Breakpoint List" 0
9325cb04 96gdb_test "python print (len(del_list))" "3" "Number of breakpoints before delete"
94b6973e
PM
97gdb_continue_to_breakpoint "Break at multiply." ".*/$srcfile:$deltst_location.*"
98gdb_py_test_silent_cmd "python dp1.delete()" "Delete Breakpoint" 0
9325cb04 99gdb_test "python print (dp1.number)" "RuntimeError: Breakpoint 2 is invalid.*" "Check breakpoint invalidated"
94b6973e 100gdb_py_test_silent_cmd "python del_list = gdb.breakpoints()" "Get Breakpoint List" 0
9325cb04 101gdb_test "python print (len(del_list))" "2" "Number of breakpoints after delete"
94b6973e
PM
102gdb_continue_to_breakpoint "Break at end." ".*/$srcfile:$end_location.*"
103
104
adc36818
PM
105# Start with a fresh gdb.
106clean_restart ${testfile}
107
108if ![runto_main] then {
109 fail "Cannot run to main."
110 return 0
111}
112
113# Test conditional setting.
114set bp_location1 [gdb_get_line_number "Break at multiply."]
115gdb_py_test_silent_cmd "python bp1 = gdb.Breakpoint (\"$bp_location1\")" "Set breakpoint" 0
116gdb_continue_to_breakpoint "Break at multiply."
117gdb_py_test_silent_cmd "python bp1.condition = \"i == 5\"" "Set breakpoint" 0
9325cb04 118gdb_test "python print (bp1.condition)" "i == 5" "Test conditional has been set"
adc36818
PM
119gdb_continue_to_breakpoint "Break at multiply."
120gdb_test "print i" "5" "Test conditional breakpoint stopped after five iterations"
121gdb_py_test_silent_cmd "python bp1.condition = None" "Clear condition" 0
9325cb04 122gdb_test "python print (bp1.condition)" "None" "Test conditional read"
adc36818
PM
123gdb_continue_to_breakpoint "Break at multiply."
124gdb_test "print i" "6" "Test breakpoint stopped after six iterations"
125
126# Test commands.
127gdb_breakpoint [gdb_get_line_number "Break at add."]
128set test {commands $bpnum}
129gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
130set test {print "Command for breakpoint has been executed."}
131gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
132set test {print result}
133gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
134gdb_test "end"
135
136gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" "Get Breakpoint List" 0
9325cb04 137gdb_test "python print (blist\[len(blist)-1\].commands)" "print \"Command for breakpoint has been executed.\".*print result"
adc36818 138
84f4c1fe
PM
139# Start with a fresh gdb.
140clean_restart ${testfile}
141
142if ![runto_main] then {
143 fail "Cannot run to main."
144 return 0
145}
146
8bfb830f 147# Test invisible breakpoints.
84f4c1fe
PM
148delete_breakpoints
149set ibp_location [gdb_get_line_number "Break at multiply."]
150gdb_py_test_silent_cmd "python ibp = gdb.Breakpoint(\"$ibp_location\", internal=False)" "Set invisible breakpoint" 0
151gdb_py_test_silent_cmd "python ilist = gdb.breakpoints()" "Get Breakpoint List" 0
9325cb04
PK
152gdb_test "python print (ilist\[0\])" "<gdb.Breakpoint object at $hex>" "Check invisible bp obj exists"
153gdb_test "python print (ilist\[0\].location)" "py-breakpoint\.c:$ibp_location*" "Check breakpoint location"
154gdb_test "python print (ilist\[0\].visible)" "True" "Check breakpoint visibility"
84f4c1fe
PM
155gdb_test "info breakpoints" "py-breakpoint\.c:$ibp_location.*" "Check info breakpoints shows visible breakpoints"
156delete_breakpoints
157gdb_py_test_silent_cmd "python ibp = gdb.Breakpoint(\"$ibp_location\", internal=True)" "Set invisible breakpoint" 0
158gdb_py_test_silent_cmd "python ilist = gdb.breakpoints()" "Get Breakpoint List" 0
9325cb04
PK
159gdb_test "python print (ilist\[0\])" "<gdb.Breakpoint object at $hex>" "Check invisible bp obj exists"
160gdb_test "python print (ilist\[0\].location)" "py-breakpoint\.c:$ibp_location*" "Check breakpoint location"
161gdb_test "python print (ilist\[0\].visible)" "False" "Check breakpoint visibility"
84f4c1fe
PM
162gdb_test "info breakpoints" "No breakpoints or watchpoints.*" "Check info breakpoints does not show invisible breakpoints"
163gdb_test "maint info breakpoints" "py-breakpoint\.c:$ibp_location.*" "Check maint info breakpoints shows invisible breakpoints"
164
165
adc36818
PM
166# Watchpoints
167# Start with a fresh gdb.
168clean_restart ${testfile}
169
e0756905
UW
170# Disable hardware watchpoints if necessary.
171if [target_info exists gdb,no_hardware_watchpoints] {
172 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
173}
174
adc36818
PM
175if ![runto_main] then {
176 fail "Cannot run to main."
177 return 0
178}
179
180gdb_py_test_silent_cmd "python wp1 = gdb.Breakpoint (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE )" "Set watchpoint" 0
468b1aa7 181gdb_test "continue" ".*\[Ww\]atchpoint.*result.*Old value = 0.*New value = 25.*main.*" "Test watchpoint write"
adc36818 182
84f4c1fe 183# Internal breakpoints.
adc36818 184
84f4c1fe
PM
185# Start with a fresh gdb.
186clean_restart ${testfile}
adc36818 187
182b9acc
UW
188# Disable hardware watchpoints if necessary.
189if [target_info exists gdb,no_hardware_watchpoints] {
190 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
191}
84f4c1fe
PM
192if ![runto_main] then {
193 fail "Cannot run to main."
194 return 0
195}
196delete_breakpoints
197gdb_py_test_silent_cmd "python wp1 = gdb.Breakpoint (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE, internal=True )" "Set watchpoint" 0
198gdb_test "info breakpoints" "No breakpoints or watchpoints.*" "Check info breakpoints does not show invisible breakpoints"
b9b35694 199gdb_test "maint info breakpoints" ".*watchpoint.*result.*" "Check maint info breakpoints shows invisible breakpoints"
84f4c1fe 200gdb_test "continue" ".*\[Ww\]atchpoint.*result.*Old value = 0.*New value = 25.*" "Test watchpoint write"
7371cf6d
PM
201
202# Breakpoints that have an evaluation function.
203
204# Start with a fresh gdb.
205clean_restart ${testfile}
206
182b9acc
UW
207# Disable hardware watchpoints if necessary.
208if [target_info exists gdb,no_hardware_watchpoints] {
209 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
210}
7371cf6d
PM
211if ![runto_main] then {
212 fail "Cannot run to main."
213 return 0
214}
215delete_breakpoints
216
217gdb_py_test_multiple "Sub-class a breakpoint" \
218 "python" "" \
219 "class bp_eval (gdb.Breakpoint):" "" \
220 " inf_i = 0" "" \
221 " count = 0" "" \
222 " def stop (self):" "" \
223 " self.count = self.count + 1" "" \
224 " self.inf_i = gdb.parse_and_eval(\"i\")" "" \
225 " if self.inf_i == 3:" "" \
226 " return True" "" \
227 " return False" "" \
228 "end" ""
229
230gdb_py_test_multiple "Sub-class a second breakpoint" \
231 "python" "" \
232 "class bp_also_eval (gdb.Breakpoint):" "" \
233 " count = 0" "" \
234 " def stop (self):" "" \
235 " self.count = self.count + 1" "" \
236 " if self.count == 9:" "" \
237 " return True" "" \
238 " return False" "" \
239 "end" ""
240
241gdb_py_test_multiple "Sub-class a third breakpoint" \
242 "python" "" \
243 "class basic (gdb.Breakpoint):" "" \
244 " count = 0" "" \
245 "end" ""
246
247set bp_location2 [gdb_get_line_number "Break at multiply."]
248set end_location [gdb_get_line_number "Break at end."]
249gdb_py_test_silent_cmd "python eval_bp1 = bp_eval(\"$bp_location2\")" "Set breakpoint" 0
250gdb_py_test_silent_cmd "python also_eval_bp1 = bp_also_eval(\"$bp_location2\")" "Set breakpoint" 0
251gdb_py_test_silent_cmd "python never_eval_bp1 = bp_also_eval(\"$end_location\")" "Set breakpoint" 0
252gdb_continue_to_breakpoint "Break at multiply." ".*/$srcfile:$bp_location2.*"
253gdb_test "print i" "3" "Check inferior value matches python accounting"
9325cb04
PK
254gdb_test "python print (eval_bp1.inf_i)" "3" "Check python accounting matches inferior"
255gdb_test "python print (also_eval_bp1.count)" "4" \
7371cf6d 256 "Check non firing same-location breakpoint eval function was also called at each stop."
9325cb04 257gdb_test "python print (eval_bp1.count)" "4" \
7371cf6d
PM
258 "Check non firing same-location breakpoint eval function was also called at each stop."
259
260delete_breakpoints
261set cond_bp [gdb_get_line_number "Break at multiply."]
262gdb_py_test_silent_cmd "python eval_bp1 = bp_eval(\"$cond_bp\")" "Set breakpoint" 0
263set test_cond {cond $bpnum}
264gdb_test "$test_cond \"foo==3\"" "Cannot set a condition where a Python.*" \
265 "Check you cannot add a CLI condition to a Python breakpoint that" \
266 "has defined stop"
267gdb_py_test_silent_cmd "python eval_bp2 = basic(\"$cond_bp\")" "Set breakpoint" 0
268gdb_py_test_silent_cmd "python eval_bp2.condition = \"1==1\"" "Set a condition" 0
269gdb_py_test_multiple "Construct an eval function" \
270 "python" "" \
271 "def stop_func ():" "" \
272 " return True" "" \
273 "end" ""
274
275gdb_test "python eval_bp2.stop = stop_func" \
276 "RuntimeError: Cannot set 'stop' method.*" \
277 "Assign stop function to a breakpoint that has a condition"
278
279delete_breakpoints
280gdb_breakpoint [gdb_get_line_number "Break at multiply."]
281gdb_py_test_silent_cmd "python check_eval = bp_eval(\"$bp_location2\")" "Set breakpoint" 0
9325cb04 282gdb_test "python print (check_eval.count)" "0" \
7371cf6d
PM
283 "Test that evaluate function has not been yet executed (ie count = 0)"
284gdb_continue_to_breakpoint "Break at multiply." ".*/$srcfile:$bp_location2.*"
9325cb04 285gdb_test "python print (check_eval.count)" "1" \
7371cf6d
PM
286 "Test that evaluate function is run when location also has normal bp"
287
288gdb_py_test_multiple "Sub-class a watchpoint" \
289 "python" "" \
290 "class wp_eval (gdb.Breakpoint):" "" \
291 " def stop (self):" "" \
292 " self.result = gdb.parse_and_eval(\"result\")" "" \
293 " if self.result == 788:" "" \
294 " return True" "" \
295 " return False" "" \
296 "end" ""
297
298delete_breakpoints
299gdb_py_test_silent_cmd "python wp1 = wp_eval (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE)" "Set watchpoint" 0
300gdb_test "continue" ".*\[Ww\]atchpoint.*result.*Old value =.*New value = 788.*" "Test watchpoint write"
9325cb04 301gdb_test "python print (never_eval_bp1.count)" "0" \
7371cf6d 302 "Check that this unrelated breakpoints eval function was never called."