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