Home | History | Annotate | Line # | Download | only in gdb.base
      1 #   Copyright 1988-2024 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 was written by Rob Savoye. (rob (at) cygnus.com)
     17 
     18 if { [build_executable "failed to prepare" "break" {break.c break1.c} {debug nowarnings}] } {
     19     return -1
     20 }
     21 set srcfile break.c
     22 set srcfile1 break1.c
     23 
     24 set bp_location1 [gdb_get_line_number "set breakpoint 1 here"]
     25 set bp_location2 [gdb_get_line_number "set breakpoint 2 here"]
     26 set bp_location3 [gdb_get_line_number "set breakpoint 3 here"]
     27 set bp_location4 [gdb_get_line_number "set breakpoint 4 here"]
     28 set bp_location6 [gdb_get_line_number "set breakpoint 6 here"]
     29 set bp_location7 [gdb_get_line_number "set breakpoint 7 here"]
     30 set bp_location8 [gdb_get_line_number "set breakpoint 8 here" $srcfile1]
     31 set bp_location11 [gdb_get_line_number "set breakpoint 11 here"]
     32 
     33 set main_line $bp_location6
     34 
     35 # In C++ mode, we see a full prototype; in C mode, we only see the
     36 # function name, with no parameter info.
     37 proc func {name} {
     38     return "${name}(?:\(\[^\r\n\]*\))?"
     39 }
     40 
     41 # test simple breakpoint setting commands
     42 
     43 proc_with_prefix test_break {} {
     44     clean_restart break
     45 
     46     # Test deleting all breakpoints when there are none installed,
     47     # GDB should not prompt for confirmation.
     48     # Note that lib/gdb.exp provides a "delete_breakpoints" proc
     49     # for general use elsewhere.
     50     send_gdb "delete breakpoints\n"
     51     gdb_expect {
     52 	 -re "Delete all breakpoints, watchpoints, tracepoints, and catchpoints.*$" {
     53 		send_gdb "y\n"
     54 		gdb_expect {
     55 		    -re "$::gdb_prompt $" {
     56 			fail "delete all breakpoints, watchpoints, tracepoints, and catchpoints when none (unexpected prompt)"
     57 		    }
     58 		    timeout	{ fail "delete all breakpoints, watchpoints, tracepoints, and catchpoints when none (timeout after unexpected prompt)" }
     59 		}
     60 	    }
     61 	 -re ".*$::gdb_prompt $"       { pass "delete all breakpoints, watchpoints, tracepoints, and catchpoints when none" }
     62 	timeout	            { fail "delete all breakpoints, watchpoints, tracepoints, and catchpoints when none (timeout)" }
     63     }
     64 
     65     # test break at function
     66     gdb_test "break -q main" \
     67 	"Breakpoint.*at.* file .*$::srcfile, line.*" \
     68 	"breakpoint function"
     69 
     70     # test break at quoted function
     71     gdb_test "break \"marker2\"" \
     72 	"Breakpoint.*at.* file .*$::srcfile1, line.*" \
     73 	"breakpoint quoted function"
     74 
     75     # test break at function in file
     76     gdb_test "break $::srcfile:factorial" \
     77 	"Breakpoint.*at.* file .*$::srcfile, line.*" \
     78 	"breakpoint function in file"
     79 
     80     # test break at line number
     81     #
     82     # Note that the default source file is the last one whose source text
     83     # was printed.  For native debugging, before we've executed the
     84     # program, this is the file containing main, but for remote debugging,
     85     # it's wherever the processor was stopped when we connected to the
     86     # board.  So, to be sure, we do a list command.
     87     gdb_test "list -q main" \
     88 	".*main \\(int argc, char \\*\\*argv, char \\*\\*envp\\).*" \
     89 	"use `list' to establish default source file"
     90 
     91     gdb_test "break $::bp_location1" \
     92 	"Breakpoint.*at.* file .*$::srcfile, line $::bp_location1\\." \
     93 	"breakpoint line number"
     94 
     95     # test duplicate breakpoint
     96     gdb_test "break $::bp_location1" \
     97 	"Note: breakpoint \[0-9\]+ also set at pc.*Breakpoint \[0-9\]+ at.* file .*$::srcfile, line $::bp_location1\\." \
     98 	"breakpoint duplicate"
     99 
    100     # test break at line number in file
    101     gdb_test "break $::srcfile:$::bp_location2" \
    102 	"Breakpoint.*at.* file .*$::srcfile, line $::bp_location2\\." \
    103 	"breakpoint line number in file"
    104 
    105     # Test putting a break at the start of a multi-line if conditional.
    106     # Verify the breakpoint was put at the start of the conditional.
    107     gdb_test "break multi_line_if_conditional" \
    108 	"Breakpoint.*at.* file .*$::srcfile, line $::bp_location3\\." \
    109 	"breakpoint at start of multi line if conditional"
    110 
    111     gdb_test "break multi_line_while_conditional" \
    112 	"Breakpoint.*at.* file .*$::srcfile, line $::bp_location4\\." \
    113 	"breakpoint at start of multi line while conditional"
    114 
    115     gdb_test "info break" \
    116 	[multi_line "Num     Type\[ \]+Disp Enb Address\[ \]+What.*" \
    117 		    "$::decimal\[\t \]+breakpoint     keep y.* in [func main] at .*$::srcfile:$::main_line.*" \
    118 		    "$::decimal\[\t \]+breakpoint     keep y.* in [func marker2] at .*$::srcfile1:$::bp_location8.*" \
    119 		    "$::decimal\[\t \]+breakpoint     keep y.* in [func factorial] at .*$::srcfile:$::bp_location7.*" \
    120 		    "$::decimal\[\t \]+breakpoint     keep y.* in [func main] at .*$::srcfile:$::bp_location1.*" \
    121 		    "$::decimal\[\t \]+breakpoint     keep y.* in [func main] at .*$::srcfile:$::bp_location1.*" \
    122 		    "$::decimal\[\t \]+breakpoint     keep y.* in [func main] at .*$::srcfile:$::bp_location2.*" \
    123 		    "$::decimal\[\t \]+breakpoint     keep y.* in [func multi_line_if_conditional] at .*$::srcfile:$::bp_location3.*" \
    124 		    "$::decimal\[\t \]+breakpoint     keep y.* in [func multi_line_while_conditional] at .*$::srcfile:$::bp_location4"] \
    125 	"breakpoint info"
    126 
    127     #
    128     # Test info breakpoint with arguments
    129     #
    130 
    131     set see1 0
    132     set see2 0
    133     set see3 0
    134     set see4 0
    135     set see5 0
    136     set see6 0
    137 
    138     gdb_test_multiple "info break 2 4 6" "info break 2 4 6" {
    139 	-re "1\[\t \]+breakpoint *keep y\[^\r\n\]*:$::main_line\[^\r\n\]*" {
    140 	    set see1 1
    141 	    exp_continue
    142 	}
    143 	-re "2\[\t \]+breakpoint *keep y\[^\r\n\]* in [func marker2] at \[^\r\n\]*" {
    144 	    set see2 1
    145 	    exp_continue
    146 	}
    147 	-re "3\[\t \]+breakpoint *keep y\[^\r\n\]*$::bp_location7\[^\r\n\]*" {
    148 	    set see3 1
    149 	    exp_continue
    150 	}
    151 	-re "4\[\t \]+breakpoint *keep y\[^\r\n\]*$::bp_location1\[^\r\n\]*" {
    152 	    set see4 1
    153 	    exp_continue
    154 	}
    155 	-re "5\[\t \]+breakpoint *keep y\[^\r\n\]*$::bp_location1\[^\r\n\]*" {
    156 	    set see5 1
    157 	    exp_continue
    158 	}
    159 	-re "6\[\t \]+breakpoint *keep y\[^\r\n\]*$::bp_location2\[^\r\n\]*" {
    160 	    set see6 1
    161 	    exp_continue
    162 	}
    163 	-re ".*$::gdb_prompt $" {
    164 	    if {!$see1 && $see2 && !$see3 && $see4 && !$see5 && $see6} {
    165 		pass "info break 2 4 6"
    166 	    } else {
    167 		fail "info break 2 4 6"
    168 	    }
    169 	}
    170     }
    171 
    172     set see1 0
    173     set see2 0
    174     set see3 0
    175     set see4 0
    176     set see5 0
    177     set see6 0
    178 
    179     gdb_test_multiple "info break 3-5" "info break 3-5" {
    180 	-re "1\[\t \]+breakpoint *keep y.* in [func main] at .*:$::main_line\[^\r\n\]*" {
    181 	    set see1 1
    182 	    exp_continue
    183 	}
    184 	-re "2\[\t \]+breakpoint *keep y\[^\r\n\]* in [func marker2] at \[^\r\n\]*" {
    185 	    set see2 1
    186 	    exp_continue
    187 	}
    188 	-re "3\[\t \]+breakpoint *keep y\[^\r\n\]*$::bp_location7\[^\r\n\]*" {
    189 	    set see3 1
    190 	    exp_continue
    191 	}
    192 	-re "4\[\t \]+breakpoint *keep y\[^\r\n\]*$::bp_location1\[^\r\n\]*" {
    193 	    set see4 1
    194 	    exp_continue
    195 	}
    196 	-re "5\[\t \]+breakpoint *keep y\[^\r\n\]*$::bp_location1\[^\r\n\]*" {
    197 	    set see5 1
    198 	    exp_continue
    199 	}
    200 	-re "6\[\t \]+breakpoint *keep y\[^\r\n\]*$::bp_location2\[^\r\n\]*" {
    201 	    set see6 1
    202 	    exp_continue
    203 	}
    204 	-re ".*$::gdb_prompt $" {
    205 	    if {!$see1 && !$see2 && $see3 && $see4 && $see5 && !$see6} {
    206 		pass "info break 3-5"
    207 	    } else {
    208 		fail "info break 3-5"
    209 	    }
    210 	}
    211     }
    212 
    213     #
    214     # Test disable/enable with arguments
    215     #
    216 
    217     # Test with value history
    218 
    219     with_test_prefix "with value history" {
    220 	gdb_test "print 1"
    221 	gdb_test "print 2"
    222 	gdb_test "print 3"
    223 	gdb_test "print 4"
    224 	gdb_test "print 5"
    225 	gdb_test "print 6"
    226 
    227 	# $2 is 2 and $$ is 5
    228 	gdb_test_no_output "disable \$2 \$\$" "disable using history values"
    229 
    230 	set see1 0
    231 	set see2 0
    232 	set see3 0
    233 	set see4 0
    234 	set see5 0
    235 	set see6 0
    236 
    237 	gdb_test_multiple "info break" "check disable with history values" {
    238 	    -re "1\[\t \]+breakpoint *keep y.* in [func main] at .*:$::main_line\[^\r\n\]*" {
    239 		set see1 1
    240 		exp_continue
    241 	    }
    242 	    -re "2\[\t \]+breakpoint *keep n\[^\r\n\]* in [func marker2] at \[^\r\n\]*" {
    243 		set see2 1
    244 		exp_continue
    245 	    }
    246 	    -re "3\[\t \]+breakpoint *keep y\[^\r\n\]*$::bp_location7\[^\r\n\]*" {
    247 		set see3 1
    248 		exp_continue
    249 	    }
    250 	    -re "4\[\t \]+breakpoint *keep y\[^\r\n\]*$::bp_location1\[^\r\n\]*" {
    251 		set see4 1
    252 		exp_continue
    253 	    }
    254 	    -re "5\[\t \]+breakpoint *keep n\[^\r\n\]*$::bp_location1\[^\r\n\]*" {
    255 		set see5 1
    256 		exp_continue
    257 	    }
    258 	    -re "6\[\t \]+breakpoint *keep y\[^\r\n\]*$::bp_location2\[^\r\n\]*" {
    259 		set see6 1
    260 		exp_continue
    261 	    }
    262 	    -re ".*$::gdb_prompt $" {
    263 		if {$see1 && $see2 && $see3 && $see4 && $see5 && $see6} {
    264 		    pass "check disable with history values"
    265 		} else {
    266 		    fail "check disable with history values"
    267 		}
    268 	    }
    269 	}
    270     }
    271 
    272     with_test_prefix "with convenience vars" {
    273 	gdb_test "enable"
    274 	gdb_test "set \$foo = 3"
    275 	gdb_test "set \$bar = 6"
    276 	gdb_test_no_output "disable \$foo \$bar" "disable with convenience values"
    277 
    278 	set see1 0
    279 	set see2 0
    280 	set see3 0
    281 	set see4 0
    282 	set see5 0
    283 	set see6 0
    284 
    285 	gdb_test_multiple "info break" "check disable with convenience values" {
    286 	    -re "1\[\t \]+breakpoint *keep y.* in [func main] at .*:$::main_line\[^\r\n\]*" {
    287 		set see1 1
    288 		exp_continue
    289 	    }
    290 	    -re "2\[\t \]+breakpoint *keep y\[^\r\n\]* in [func marker2] at \[^\r\n\]*" {
    291 		set see2 1
    292 		exp_continue
    293 	    }
    294 	    -re "3\[\t \]+breakpoint *keep n\[^\r\n\]*$::bp_location7\[^\r\n\]*" {
    295 		set see3 1
    296 		exp_continue
    297 	    }
    298 	    -re "4\[\t \]+breakpoint *keep y\[^\r\n\]*$::bp_location1\[^\r\n\]*" {
    299 		set see4 1
    300 		exp_continue
    301 	    }
    302 	    -re "5\[\t \]+breakpoint *keep y\[^\r\n\]*$::bp_location1\[^\r\n\]*" {
    303 		set see5 1
    304 		exp_continue
    305 	    }
    306 	    -re "6\[\t \]+breakpoint *keep n\[^\r\n\]*$::bp_location2\[^\r\n\]*" {
    307 		set see6 1
    308 		exp_continue
    309 	    }
    310 	    -re ".*$::gdb_prompt $" {
    311 		if {$see1 && $see2 && $see3 && $see4 && $see5 && $see6} {
    312 		    pass "check disable with convenience values"
    313 		} else {
    314 		    fail "check disable with convenience values"
    315 		}
    316 	    }
    317 	}
    318     }
    319 
    320     # test with bad values
    321 
    322     with_test_prefix "bad values" {
    323 	gdb_test "enable"
    324 	gdb_test "disable 10" "No breakpoint number 10." \
    325 	    "disable non-existent breakpoint 10"
    326 
    327 	gdb_test_no_output "set \$baz = 1.234"
    328 	gdb_test "disable \$baz" \
    329 	    "Convenience variable must have integer value.*" \
    330 	    "disable with non-integer convenience var"
    331 	gdb_test "disable \$grbx" \
    332 	    "Convenience variable must have integer value.*" \
    333 	    "disable with non-existent convenience var"
    334 	gdb_test "disable \$10" \
    335 	    "History has not yet reached .10." \
    336 	    "disable with non-existent history value"
    337 	gdb_test "disable \$1foo" \
    338 	    "Convenience variable must have integer value.*" \
    339 	    "disable with badly formed history value"
    340     }
    341 
    342     # FIXME: The rest of this test doesn't work with anything that can't
    343     # handle arguments.
    344     # Huh? There doesn't *appear* to be anything that passes arguments
    345     # below.
    346 
    347     #
    348     # run until the breakpoint at main is hit. For non-stubs-using targets.
    349     #
    350     gdb_run_cmd
    351     gdb_test "" \
    352 	"Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$::srcfile:$::bp_location6.*$::bp_location6\[\t \]+if .argc.* \{.*" \
    353 	"run until function breakpoint"
    354 
    355     # Test the 'list' commands sets current file for the 'break LINENO' command.
    356     set bp_marker1 [gdb_get_line_number "set breakpoint 15 here" $::srcfile1]
    357     gdb_test "list marker1" ".*"
    358     gdb_test "break $bp_marker1" "Breakpoint \[0-9\]+ at 0x\[0-9a-f\]+: file .*$::srcfile1, line ${bp_marker1}\\." \
    359 	     "break lineno"
    360     gdb_test_no_output {delete $bpnum}
    361 
    362     #
    363     # run until the breakpoint at a line number
    364     #
    365     gdb_test continue "Continuing\\..*Breakpoint \[0-9\]+, main \\(argc=.*, argv=.*, envp=.*\\) at .*$::srcfile:$::bp_location1.*$::bp_location1\[\t \]+printf.*factorial.*" \
    366 			    "run until breakpoint set at a line number"
    367 
    368     #
    369     # Run until the breakpoint set in a function in a file
    370     #
    371     for {set i 6} {$i >= 1} {incr i -1} {
    372 	    gdb_test continue "Continuing\\..*Breakpoint \[0-9\]+, factorial \\(value=$i\\) at .*$::srcfile:$::bp_location7.*$::bp_location7\[\t \]+.*if .value > 1. \{.*" \
    373 			    "run until file:function($i) breakpoint"
    374     }
    375 
    376     #
    377     # Run until the breakpoint set at a quoted function
    378     #
    379     gdb_test continue "Continuing\\..*Breakpoint \[0-9\]+, (0x\[0-9a-f\]+ in )?marker2 \\(a=43\\) at .*$::srcfile1:$::bp_location8.*" \
    380 		    "run until quoted breakpoint"
    381     #
    382     # run until the file:function breakpoint at a line number in a file
    383     #
    384     gdb_test continue "Continuing\\..*Breakpoint \[0-9\]+, main \\(argc=.*, argv=.*, envp=.*\\) at .*$::srcfile:$::bp_location2.*$::bp_location2\[\t \]+argc = \\(argc == 12345\\);.*" \
    385 		    "run until file:linenum breakpoint"
    386 
    387     # Test break at offset +1
    388     set bp_location10 [gdb_get_line_number "set breakpoint 10 here"]
    389 
    390     gdb_test "break +1" \
    391 	"Breakpoint.*at.* file .*$::srcfile, line $bp_location10\\." \
    392 	"breakpoint offset +1"
    393 
    394     # Check to see if breakpoint is hit when stepped onto
    395 
    396     gdb_test "step" \
    397 	".*Breakpoint \[0-9\]+, main \\(argc=.*, argv=.*, envp=.*\\) at .*$::srcfile:$bp_location10.*$bp_location10\[\t \]+return argc;.*breakpoint 10 here.*" \
    398 	"step onto breakpoint"
    399 
    400     # Check to see if breakpoint can be set on ending brace of function
    401     set bp_location10a [gdb_get_line_number "set breakpoint 10a here"]
    402 
    403     gdb_test "break $bp_location10a" \
    404 	"Breakpoint.*at.* file .*$::srcfile, line $bp_location10a\\." \
    405 	"setting breakpoint at \}"
    406 
    407     gdb_test "continue" \
    408 	".*Breakpoint \[0-9\]+, main \\(argc=.*, argv=.*, envp=.*\\) at .*$::srcfile:$bp_location10a.*$bp_location10a\[\t \]+\}.*breakpoint 10a here.*" \
    409 	"continue to breakpoint at \}"
    410 }
    411 
    412 test_break
    413 
    414 proc_with_prefix test_tbreak {} {
    415     clean_restart break
    416 
    417     # test temporary breakpoint at function
    418     gdb_test "tbreak -q main" "Temporary breakpoint.*at.* file .*$::srcfile, line.*" "temporary breakpoint function"
    419 
    420     # test break at function in file
    421     gdb_test "tbreak $::srcfile:factorial" "Temporary breakpoint.*at.* file .*$::srcfile, line.*" \
    422 	    "Temporary breakpoint function in file"
    423 
    424     # test break at line number
    425     gdb_test "tbreak $::bp_location1" \
    426 	"Temporary breakpoint.*at.* file .*$::srcfile, line $::bp_location1.*" \
    427 	"temporary breakpoint line number #1"
    428 
    429     gdb_test "tbreak $::bp_location6" "Temporary breakpoint.*at.* file .*$::srcfile, line $::bp_location6.*" "temporary breakpoint line number #2"
    430 
    431     # test break at line number in file
    432     gdb_test "tbreak $::srcfile:$::bp_location2" \
    433 	"Temporary breakpoint.*at.* file .*$::srcfile, line $::bp_location2.*" \
    434 	"temporary breakpoint line number in file #1"
    435 
    436     gdb_test  "tbreak $::srcfile:$::bp_location11" "Temporary breakpoint.*at.* file .*$::srcfile, line $::bp_location11.*" "Temporary breakpoint line number in file #2"
    437 
    438     # check to see what breakpoints are set (temporary this time)
    439     gdb_test "info break" \
    440 	[multi_line "Num     Type.*Disp Enb Address.*What.*" \
    441 		    "$::decimal\[\t \]+breakpoint     del.*y.*in [func main] at .*$::srcfile:$::main_line.*" \
    442 		    "$::decimal\[\t \]+breakpoint     del.*y.*in [func factorial] at .*$::srcfile:$::bp_location7.*" \
    443 		    "$::decimal\[\t \]+breakpoint     del.*y.*in [func main] at .*$::srcfile:$::bp_location1.*" \
    444 		    "$::decimal\[\t \]+breakpoint     del.*y.*in [func main] at .*$::srcfile:$::bp_location6.*" \
    445 		    "$::decimal\[\t \]+breakpoint     del.*y.*in [func main] at .*$::srcfile:$::bp_location2.*" \
    446 		    "$::decimal\[\t \]+breakpoint     del.*y.*in [func main] at .*$::srcfile:$::bp_location11.*"] \
    447 	"Temporary breakpoint info"
    448 }
    449 
    450 test_tbreak
    451 
    452 #***********
    453 
    454 # Verify that catchpoints for fork, vfork and exec don't trigger
    455 # inappropriately.  (There are no calls to those system functions
    456 # in this test program.)
    457 
    458 proc_with_prefix test_no_break_on_catchpoint {} {
    459     clean_restart break
    460 
    461     if {![runto_main]} {
    462 	return
    463     }
    464 
    465     gdb_test "catch fork" "Catchpoint \[0-9\]+ \\(fork\\)" \
    466 	"set catch fork, never expected to trigger"
    467 
    468     gdb_test "catch vfork" "Catchpoint \[0-9\]+ \\(vfork\\)" \
    469 	"set catch vfork, never expected to trigger"
    470 
    471     gdb_test "catch exec" "Catchpoint \[0-9\]+ \\(exec\\)" \
    472 	"set catch exec, never expected to trigger"
    473 
    474     gdb_continue_to_end
    475 }
    476 
    477 test_no_break_on_catchpoint
    478 
    479 proc_with_prefix test_break_nonexistent_line {} {
    480     clean_restart break
    481 
    482     if {![runto_main]} {
    483 	return
    484     }
    485 
    486     # Verify that GDB responds gracefully when asked to set a
    487     # breakpoint on a nonexistent source line.
    488     gdb_test_no_output "set breakpoint pending off"
    489     gdb_test "break 999" \
    490 	"^No compiled code for line 999 in the current file\\." \
    491 	"break on non-existent source line"
    492 }
    493 
    494 test_break_nonexistent_line
    495 
    496 proc_with_prefix test_break_default {} {
    497     clean_restart break
    498 
    499     if {![runto_main]} {
    500 	return
    501     }
    502 
    503     # Run to the desired default location. If not positioned here, the
    504     # tests below don't work.
    505     #
    506     gdb_test "until $::bp_location1" "main .* at .*:$::bp_location1.*" \
    507 	"until bp_location1"
    508 
    509     # Verify that GDB allows one to just say "break", which is treated
    510     # as the "default" breakpoint.  Note that GDB gets cute when printing
    511     # the informational message about other breakpoints at the same
    512     # location.  We'll hit that bird with this stone too.
    513     #
    514     gdb_test "break" "Breakpoint \[0-9\]*.*" \
    515 	"break on default location, 1st time"
    516 
    517     gdb_test "break" \
    518 	"Note: breakpoint \[0-9\]* also set at .*Breakpoint \[0-9\]*.*" \
    519 	"break on default location, 2nd time"
    520 
    521     gdb_test "break" \
    522 	"Note: breakpoints \[0-9\]* and \[0-9\]* also set at .*Breakpoint \[0-9\]*.*" \
    523 	"break on default location, 3rd time"
    524 
    525     gdb_test "break" \
    526 	"Note: breakpoints \[0-9\]*, \[0-9\]* and \[0-9\]* also set at .*Breakpoint \[0-9\]*.*" \
    527 	"break on default location, 4th time"
    528 
    529     # Check setting a breakpoint at the default location with a condition attached.
    530     gdb_test "break if (1)" \
    531 	"Note: breakpoints \[0-9\]*, \[0-9\]*, \[0-9\]* and \[0-9\]* also set at .*Breakpoint \[0-9\]*.*" \
    532 	"break on the default location, 5th time, but with a condition"
    533 }
    534 
    535 test_break_default
    536 
    537 # Verify that a "silent" breakpoint can be set, and that GDB is indeed
    538 # "silent" about its triggering.
    539 
    540 proc_with_prefix test_break_silent_and_more {} {
    541     clean_restart break
    542 
    543     if {![runto_main]} {
    544 	return
    545     }
    546 
    547     gdb_test_multiple "break $::bp_location1" \
    548 	"set to-be-silent break bp_location1" {
    549 	    -re "Breakpoint (\[0-9\]*) at .*, line $::bp_location1.*$::gdb_prompt $" {
    550 		set bpno $expect_out(1,string)
    551 		pass "set to-be-silent break bp_location1"
    552 	    }
    553 	}
    554 
    555     gdb_test "commands $bpno\nsilent\nend" ">end" "set silent break bp_location1"
    556 
    557     gdb_test "info break $bpno" \
    558 	"\[0-9\]*\[ \t\]*breakpoint.*:$::bp_location1\r\n\[ \t\]*silent.*" \
    559 	"info silent break bp_location1"
    560 
    561     gdb_test "continue" "Continuing." \
    562 	"hit silent break bp_location1"
    563 
    564     gdb_test "bt" "#0  main .* at .*:$::bp_location1.*" \
    565 	"stopped for silent break bp_location1"
    566 
    567     # Verify the $_hit_bpnum convenience variable is set to the silent hit bpno.
    568     gdb_test "printf \"%d\\n\", \$_hit_bpnum" "$bpno" \
    569 	"Silent breakpoint hit \$_hit_bpnum is silent $bpno"
    570 
    571     # Verify that GDB can at least parse a breakpoint with the
    572     # "thread" keyword.  (We won't attempt to test here that a
    573     # thread-specific breakpoint really triggers appropriately.
    574     # The gdb.threads subdirectory contains tests for that.)
    575     #
    576     set bp_location12 [gdb_get_line_number "set breakpoint 12 here"]
    577     gdb_test "break $bp_location12 thread 999" "Unknown thread 999.*" \
    578 	"thread-specific breakpoint on non-existent thread disallowed"
    579 
    580     gdb_test "break $bp_location12 thread foo" \
    581 	"Invalid thread ID: foo" \
    582 	"thread-specific breakpoint on bogus thread ID disallowed"
    583 
    584     # Verify that GDB responds gracefully to a breakpoint command with
    585     # trailing garbage.
    586     #
    587     gdb_test "break $bp_location12 foo" \
    588 	"malformed linespec error: unexpected string, \"foo\".*" \
    589 	"breakpoint with trailing garbage disallowed"
    590 
    591     # Verify that GDB responds gracefully to a "clear" command that has
    592     # no matching breakpoint.  (First, get us off the current source line,
    593     # which we know has a breakpoint.)
    594     #
    595     gdb_test "next" "marker1.*" "step over breakpoint"
    596 
    597     gdb_test "clear 81" "No breakpoint at 81.*" \
    598 	"clear line has no breakpoint disallowed"
    599 
    600     gdb_test "clear" "No breakpoint at this line.*" \
    601 	"clear current line has no breakpoint disallowed"
    602 
    603     # Verify that we can set and clear multiple breakpoints.
    604     #
    605     # We don't test that it deletes the correct breakpoints.  We do at
    606     # least test that it deletes more than one breakpoint.
    607     #
    608     gdb_test "break marker3" "Breakpoint.*at.*" "break marker3 #1"
    609     gdb_test "break marker3" "Breakpoint.*at.*" "break marker3 #2"
    610     gdb_test "clear marker3" {Deleted breakpoints [0-9]+ [0-9]+.*}
    611 }
    612 
    613 test_break_silent_and_more
    614 
    615 # Verify that a breakpoint can be set via a convenience variable.
    616 
    617 proc_with_prefix test_break_line_convenience_var {} {
    618     clean_restart break
    619 
    620     if { ![runto_main] } {
    621 	return
    622     }
    623 
    624     gdb_test_no_output "set \$foo=$::bp_location11" \
    625 	"set convenience variable \$foo to bp_location11"
    626 
    627     gdb_test "break \$foo" \
    628 	"Breakpoint (\[0-9\]*) at .*, line $::bp_location11.*"
    629 
    630     # Verify that GDB responds gracefully to an attempt to set a
    631     # breakpoint via a convenience variable whose type is not integer.
    632 
    633     gdb_test_no_output "set \$foo=81.5" \
    634 	"set convenience variable \$foo to 81.5"
    635 
    636     gdb_test "break \$foo" \
    637 	"Convenience variables used in line specs must have integer values.*" \
    638 	"non-integer convenience variable disallowed"
    639 }
    640 
    641 test_break_line_convenience_var
    642 
    643 # Verify that we can set and trigger a breakpoint in a user-called function.
    644 
    645 proc_with_prefix test_break_user_call {} {
    646     clean_restart break
    647 
    648     if { ![runto_main] } {
    649 	return
    650     }
    651 
    652     gdb_test "break marker2" \
    653 	"Breakpoint (\[0-9\]*) at .*, line $::bp_location8.*" \
    654 	"set breakpoint on to-be-called function"
    655 
    656     gdb_test "print marker2(99)" \
    657 	"The program being debugged stopped while in a function called from GDB.\r\nEvaluation of the expression containing the function\r\n.[func marker2]. will be abandoned.\r\nWhen the function is done executing, GDB will silently stop.*" \
    658 	"hit breakpoint on called function"
    659 
    660     # As long as we're stopped (breakpointed) in a called function,
    661     # verify that we can successfully backtrace & such from here.
    662     gdb_test "bt" \
    663 	"#0\[ \t\]*($::hex in )?marker2.*:$::bp_location8\r\n#1\[ \t\]*<function called from gdb>.*" \
    664 	"backtrace while in called function"
    665 
    666     # Return from the called function.  For remote targets, it's important to do
    667     # this before runto_main, which otherwise may silently stop on the dummy
    668     # breakpoint inserted by GDB at the program's entry point.
    669     #
    670     gdb_test_multiple "finish" "finish from called function" {
    671 	-re "Run till exit from .*marker2.* at .*$::bp_location8\r\n.*function called from gdb.*$::gdb_prompt $" {
    672 	    pass "finish from called function"
    673 	}
    674 	-re "Run till exit from .*marker2.* at .*$::bp_location8\r\n.*Value returned.*$::gdb_prompt $" {
    675 	    pass "finish from called function"
    676 	}
    677     }
    678 }
    679 
    680 test_break_user_call
    681 
    682 # Verify that GDB responds gracefully to a "finish" command with
    683 # arguments.
    684 
    685 proc_with_prefix test_finish_arguments {} {
    686     clean_restart break
    687 
    688     if {![runto_main]} {
    689 	return
    690     }
    691 
    692     send_gdb "finish 123\n"
    693     gdb_expect {
    694       -re "The \"finish\" command does not take any arguments.\r\n$::gdb_prompt $"\
    695 	      {pass "finish with arguments disallowed"}
    696       -re "$::gdb_prompt $"\
    697 	      {fail "finish with arguments disallowed"}
    698       timeout {fail "(timeout) finish with arguments disallowed"}
    699     }
    700 
    701     # Verify that GDB responds gracefully to a request to "finish" from
    702     # the outermost frame.  On a stub that never exits, this will just
    703     # run to the stubs routine, so we don't get this error...  Thus the
    704     # second condition.
    705     #
    706 
    707     gdb_test_multiple "finish" "finish from outermost frame disallowed" {
    708 	-re "\"finish\" not meaningful in the outermost frame.\r\n$::gdb_prompt $" {
    709 	    pass "finish from outermost frame disallowed"
    710 	}
    711 	-re "Run till exit from.*\r\n$::gdb_prompt $" {
    712 	    pass "finish from outermost frame disallowed"
    713 	}
    714     }
    715 }
    716 
    717 test_finish_arguments
    718 
    719 #********
    720 
    721 
    722 #
    723 # Test "next" over recursive function call.
    724 #
    725 
    726 proc_with_prefix test_next_with_recursion {} {
    727     global gdb_prompt
    728     global decimal
    729     global binfile
    730 
    731     gdb_test "kill" "" "kill program" "Kill the program being debugged.*y or n. $" "y"
    732     delete_breakpoints
    733 
    734     gdb_test "break factorial" "Breakpoint $decimal at .*" "break at factorial"
    735 
    736     # Run until we call factorial with 6
    737 
    738     gdb_run_cmd
    739     gdb_test "" "Break.* factorial .value=6. .*" "run to factorial(6)"
    740 
    741     # Continue until we call factorial recursively with 5.
    742 
    743     gdb_test "continue" \
    744 	"Continuing.*Break.* factorial .value=5. .*" \
    745 	"continue to factorial(5)"
    746 
    747     # Do a backtrace just to confirm how many levels deep we are.
    748 
    749     gdb_test "backtrace" \
    750 	"#0\[ \t\]+ factorial .value=5..*" \
    751 	"backtrace from factorial(5)"
    752 
    753     # Now a "next" should position us at the recursive call, which
    754     # we will be performing with 4.
    755 
    756     gdb_test "next" \
    757 	".* factorial .value - 1.;.*" \
    758 	"next to recursive call"
    759 
    760     # Disable the breakpoint at the entry to factorial by deleting them all.
    761     # The "next" should run until we return to the next line from this
    762     # recursive call to factorial with 4.
    763     # Buggy versions of gdb will stop instead at the innermost frame on
    764     # the line where we are trying to "next" to.
    765 
    766     delete_breakpoints
    767 
    768     if [istarget "mips*tx39-*"] {
    769 	set timeout 60
    770     }
    771     # We used to set timeout here for all other targets as well.  This
    772     # is almost certainly wrong.  The proper timeout depends on the
    773     # target system in use, and how we communicate with it, so there
    774     # is no single value appropriate for all targets.  The timeout
    775     # should be established by the Dejagnu config file(s) for the
    776     # board, and respected by the test suite.
    777     #
    778     # For example, if I'm running GDB over an SSH tunnel talking to a
    779     # portmaster in California talking to an ancient 68k board running
    780     # a crummy ROM monitor (a situation I can only wish were
    781     # hypothetical), then I need a large timeout.  But that's not the
    782     # kind of knowledge that belongs in this file.
    783 
    784     gdb_test next "\[0-9\]*\[\t \]+return \\(value\\);.*" \
    785 	    "next over recursive call"
    786 
    787     # OK, we should be back in the same stack frame we started from.
    788     # Do a backtrace just to confirm.
    789 
    790     gdb_test "backtrace" \
    791 	    "#0\[ \t\]+ factorial .value=120.*\r\n#1\[ \t\]+ \[0-9a-fx\]+ in factorial .value=6..*" \
    792 	    "backtrace from factorial(5.1)"
    793 
    794     if { ![target_info exists gdb,noresults] } {
    795 	gdb_continue_to_end "recursive next test"
    796     }
    797 }
    798 
    799 test_next_with_recursion
    800 
    801 
    802 #********
    803 
    804 # build a new file with optimization enabled so that we can try breakpoints
    805 # on targets with optimized prologues
    806 
    807 if { [build_executable "failed to prepare" "breako2" {break.c break1.c} {debug nowarnings optimize=-O2}] } {
    808     return -1
    809 }
    810 
    811 proc_with_prefix test_break_optimized_prologue {} {
    812     clean_restart breako2
    813 
    814     # test break at function
    815     gdb_test "break -q main" \
    816 	"Breakpoint.*at.* file .*, line.*" \
    817 	"breakpoint function, optimized file"
    818 
    819     # test break at function
    820     gdb_test "break marker4" \
    821 	"Breakpoint.*at.* file .*$::srcfile1, line.*" \
    822 	"breakpoint small function, optimized file"
    823 
    824     # run until the breakpoint at main is hit. For non-stubs-using targets.
    825     gdb_run_cmd
    826 
    827     set test "run until function breakpoint, optimized file"
    828     gdb_test_multiple "" $test {
    829 	-re "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$::srcfile:$::bp_location6.*$::bp_location6\[\t \]+if .argc.* \{.*$::gdb_prompt $" {
    830 	    pass $test
    831 	}
    832 	-re "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$::gdb_prompt $" {
    833 	    pass "$test (code motion)"
    834 	}
    835     }
    836 
    837     # run until the breakpoint at a small function
    838     #
    839     # Add a second pass pattern.  The behavior differs here between stabs
    840     # and dwarf for one-line functions.  Stabs preserves two line symbols
    841     # (one before the prologue and one after) with the same line number,
    842     # but dwarf regards these as duplicates and discards one of them.
    843     # Therefore the address after the prologue (where the breakpoint is)
    844     # has no exactly matching line symbol, and GDB reports the breakpoint
    845     # as if it were in the middle of a line rather than at the beginning.
    846 
    847     set bp_location14 [gdb_get_line_number "set breakpoint 14 here" $::srcfile1]
    848 
    849     gdb_test_multiple "continue" \
    850 	"run until breakpoint set at small function, optimized file" {
    851 	    -re "Breakpoint $::decimal, marker4 \\(d=(d@entry=)?177601976\\) at .*$::srcfile1:$bp_location14\[\r\n\]+$bp_location14\[\t \]+void marker4.*" {
    852 		pass "run until breakpoint set at small function, optimized file (line bp_location14)"
    853 	    }
    854 	    -re "Breakpoint $::decimal, factorial \\(.*\\) .*\{\r\n$::gdb_prompt" {
    855 		# GCC 4.3 emits bad line number information - see gcc/36748.
    856 		if { [test_compiler_info "gcc-4-3-*"] } {
    857 		    setup_xfail *-*-*
    858 		}
    859 		fail "run until breakpoint set at small function, optimized file"
    860 	    }
    861 	}
    862 }
    863 
    864 test_break_optimized_prologue
    865 
    866 # test that 'rbreak' on a symbol that may be from a shared library doesn't
    867 # cause a "Junk at end of arguments." error.
    868 #
    869 # On x86 GNU/Linux, this test will choke on e.g. __libc_start_main@plt.
    870 #
    871 # Note that this test won't necessarily choke on all targets even if
    872 # all the rbreak issue is present.  rbreak needs to match and set a
    873 # breakpoint on a symbol causes 'break' to choke.
    874 
    875 proc_with_prefix test_rbreak_shlib {} {
    876     clean_restart breako2
    877 
    878     gdb_test_no_output "set breakpoint pending on" "rbreak junk pending setup"
    879 
    880     # We expect at least one breakpoint to be set when we "rbreak main".
    881     gdb_test "rbreak main" \
    882 	".*Breakpoint.*at.* file .*$::srcfile, line.*"
    883 
    884     # Run to a breakpoint.  Fail if we see "Junk at end of arguments".
    885     gdb_run_cmd
    886 
    887     gdb_test_multiple "" "rbreak junk" {
    888 	-re -wrap "Junk at end of arguments.*" {
    889 	    fail $gdb_test_name
    890 	}
    891 	-re -wrap ".*Breakpoint \[0-9\]+,.*" {
    892 	    pass $gdb_test_name
    893 	}
    894     }
    895 }
    896 
    897 test_rbreak_shlib
    898 
    899 # Test break via convenience variable with file name
    900 
    901 proc_with_prefix test_break_file_line_convenience_var {} {
    902     clean_restart breako2
    903 
    904     set line [gdb_get_line_number "set breakpoint 1 here"]
    905     gdb_test_no_output "set \$l = $line"
    906 
    907     set line_actual "-1"
    908     set test "break $::srcfile:\$l"
    909     gdb_test_multiple "$test" $test {
    910 	-re "Breakpoint $::decimal at $::hex: file .*break\\.c, line ($::decimal)\\.\r\n$::gdb_prompt $" {
    911 	    # Save the actual line number on which the breakpoint was
    912 	    # actually set. On some systems (Eg: Ubuntu 16.04 with GCC
    913 	    # version 5.4.0), that line gets completely inlined, including
    914 	    # the call to printf, and so we end up inserting the breakpoint
    915 	    # on one of the following lines instead.
    916 	    set line_actual $expect_out(1,string)
    917 	    pass $test
    918 	}
    919     }
    920 
    921     gdb_test_no_output "set \$foo=81.5" \
    922 	"set convenience variable \$foo to 81.5"
    923     gdb_test "break $::srcfile:\$foo" \
    924 	"Convenience variables used in line specs must have integer values.*" \
    925 	"non-integer convenience variable disallowed"
    926 }
    927 
    928 test_break_file_line_convenience_var
    929 
    930 # Test that commands can be cleared without error.
    931 
    932 proc_with_prefix test_break_commands_clear {} {
    933     clean_restart breako2
    934 
    935     set line [gdb_get_line_number "set breakpoint 1 here"]
    936     gdb_breakpoint $line
    937 
    938     gdb_test "commands\nprint 232323\nend" ">end" "set some breakpoint commands"
    939     gdb_test "commands\nend" ">end" "clear breakpoint commands"
    940 
    941     # We verify that the commands were cleared by ensuring that the last
    942     # breakpoint's location ends the output -- if there were commands,
    943     # they would have been printed after the location.
    944     gdb_test "info break" "$::srcfile:$::decimal" "verify that they were cleared"
    945 }
    946 
    947 test_break_commands_clear
    948