[gdb/syscalls] Sync with strace v6.13
[binutils-gdb.git] / gdb / testsuite / gdb.base / break.exp
blob34ac21982eab79d5399c8e56ef366207d276b156
1 #   Copyright 1988-2024 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file was written by Rob Savoye. (rob@cygnus.com)
18 if { [build_executable "failed to prepare" "break" {break.c break1.c} {debug nowarnings}] } {
19     return -1
21 set srcfile break.c
22 set srcfile1 break1.c
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"]
33 set main_line $bp_location6
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\]*\))?"
41 # test simple breakpoint setting commands
43 proc_with_prefix test_break {} {
44     clean_restart break
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     }
65     # test break at function
66     gdb_test "break -q main" \
67         "Breakpoint.*at.* file .*$::srcfile, line.*" \
68         "breakpoint function"
70     # test break at quoted function
71     gdb_test "break \"marker2\"" \
72         "Breakpoint.*at.* file .*$::srcfile1, line.*" \
73         "breakpoint quoted function"
75     # test break at function in file
76     gdb_test "break $::srcfile:factorial" \
77         "Breakpoint.*at.* file .*$::srcfile, line.*" \
78         "breakpoint function in file"
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"
91     gdb_test "break $::bp_location1" \
92         "Breakpoint.*at.* file .*$::srcfile, line $::bp_location1\\." \
93         "breakpoint line number"
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"
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"
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"
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"
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"
127     #
128     # Test info breakpoint with arguments
129     #
131     set see1 0
132     set see2 0
133     set see3 0
134     set see4 0
135     set see5 0
136     set see6 0
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     }
172     set see1 0
173     set see2 0
174     set see3 0
175     set see4 0
176     set see5 0
177     set see6 0
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     }
213     #
214     # Test disable/enable with arguments
215     #
217     # Test with value history
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"
227         # $2 is 2 and $$ is 5
228         gdb_test_no_output "disable \$2 \$\$" "disable using history values"
230         set see1 0
231         set see2 0
232         set see3 0
233         set see4 0
234         set see5 0
235         set see6 0
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     }
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"
278         set see1 0
279         set see2 0
280         set see3 0
281         set see4 0
282         set see5 0
283         set see6 0
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     }
320     # test with bad values
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"
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     }
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.
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"
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}
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"
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     }
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"
387     # Test break at offset +1
388     set bp_location10 [gdb_get_line_number "set breakpoint 10 here"]
390     gdb_test "break +1" \
391         "Breakpoint.*at.* file .*$::srcfile, line $bp_location10\\." \
392         "breakpoint offset +1"
394     # Check to see if breakpoint is hit when stepped onto
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"
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"]
403     gdb_test "break $bp_location10a" \
404         "Breakpoint.*at.* file .*$::srcfile, line $bp_location10a\\." \
405         "setting breakpoint at \}"
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 \}"
412 test_break
414 proc_with_prefix test_tbreak {} {
415     clean_restart break
417     # test temporary breakpoint at function
418     gdb_test "tbreak -q main" "Temporary breakpoint.*at.* file .*$::srcfile, line.*" "temporary breakpoint function"
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"
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"
429     gdb_test "tbreak $::bp_location6" "Temporary breakpoint.*at.* file .*$::srcfile, line $::bp_location6.*" "temporary breakpoint line number #2"
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"
436     gdb_test  "tbreak $::srcfile:$::bp_location11" "Temporary breakpoint.*at.* file .*$::srcfile, line $::bp_location11.*" "Temporary breakpoint line number in file #2"
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"
450 test_tbreak
452 #***********
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.)
458 proc_with_prefix test_no_break_on_catchpoint {} {
459     clean_restart break
461     if {![runto_main]} {
462         return
463     }
465     gdb_test "catch fork" "Catchpoint \[0-9\]+ \\(fork\\)" \
466         "set catch fork, never expected to trigger"
468     gdb_test "catch vfork" "Catchpoint \[0-9\]+ \\(vfork\\)" \
469         "set catch vfork, never expected to trigger"
471     gdb_test "catch exec" "Catchpoint \[0-9\]+ \\(exec\\)" \
472         "set catch exec, never expected to trigger"
474     gdb_continue_to_end
477 test_no_break_on_catchpoint
479 proc_with_prefix test_break_nonexistent_line {} {
480     clean_restart break
482     if {![runto_main]} {
483         return
484     }
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"
494 test_break_nonexistent_line
496 proc_with_prefix test_break_default {} {
497     clean_restart break
499     if {![runto_main]} {
500         return
501     }
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"
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"
517     gdb_test "break" \
518         "Note: breakpoint \[0-9\]* also set at .*Breakpoint \[0-9\]*.*" \
519         "break on default location, 2nd time"
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"
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"
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"
535 test_break_default
537 # Verify that a "silent" breakpoint can be set, and that GDB is indeed
538 # "silent" about its triggering.
540 proc_with_prefix test_break_silent_and_more {} {
541     clean_restart break
543     if {![runto_main]} {
544         return
545     }
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         }
555     gdb_test "commands $bpno\nsilent\nend" ">end" "set silent break bp_location1"
557     gdb_test "info break $bpno" \
558         "\[0-9\]*\[ \t\]*breakpoint.*:$::bp_location1\r\n\[ \t\]*silent.*" \
559         "info silent break bp_location1"
561     gdb_test "continue" "Continuing." \
562         "hit silent break bp_location1"
564     gdb_test "bt" "#0  main .* at .*:$::bp_location1.*" \
565         "stopped for silent break bp_location1"
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"
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"
580     gdb_test "break $bp_location12 thread foo" \
581         "Invalid thread ID: foo" \
582         "thread-specific breakpoint on bogus thread ID disallowed"
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"
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"
597     gdb_test "clear 81" "No breakpoint at 81.*" \
598         "clear line has no breakpoint disallowed"
600     gdb_test "clear" "No breakpoint at this line.*" \
601         "clear current line has no breakpoint disallowed"
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]+.*}
613 test_break_silent_and_more
615 # Verify that a breakpoint can be set via a convenience variable.
617 proc_with_prefix test_break_line_convenience_var {} {
618     clean_restart break
620     if { ![runto_main] } {
621         return
622     }
624     gdb_test_no_output "set \$foo=$::bp_location11" \
625         "set convenience variable \$foo to bp_location11"
627     gdb_test "break \$foo" \
628         "Breakpoint (\[0-9\]*) at .*, line $::bp_location11.*"
630     # Verify that GDB responds gracefully to an attempt to set a
631     # breakpoint via a convenience variable whose type is not integer.
633     gdb_test_no_output "set \$foo=81.5" \
634         "set convenience variable \$foo to 81.5"
636     gdb_test "break \$foo" \
637         "Convenience variables used in line specs must have integer values.*" \
638         "non-integer convenience variable disallowed"
641 test_break_line_convenience_var
643 # Verify that we can set and trigger a breakpoint in a user-called function.
645 proc_with_prefix test_break_user_call {} {
646     clean_restart break
648     if { ![runto_main] } {
649         return
650     }
652     gdb_test "break marker2" \
653         "Breakpoint (\[0-9\]*) at .*, line $::bp_location8.*" \
654         "set breakpoint on to-be-called function"
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"
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"
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     }
680 test_break_user_call
682 # Verify that GDB responds gracefully to a "finish" command with
683 # arguments.
685 proc_with_prefix test_finish_arguments {} {
686     clean_restart break
688     if {![runto_main]} {
689         return
690     }
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     }
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     #
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     }
717 test_finish_arguments
719 #********
723 # Test "next" over recursive function call.
726 proc_with_prefix test_next_with_recursion {} {
727     global gdb_prompt
728     global decimal
729     global binfile
731     gdb_test "kill" "" "kill program" "Kill the program being debugged.*y or n. $" "y"
732     delete_breakpoints
734     gdb_test "break factorial" "Breakpoint $decimal at .*" "break at factorial"
736     # Run until we call factorial with 6
738     gdb_run_cmd
739     gdb_test "" "Break.* factorial .value=6. .*" "run to factorial(6)"
741     # Continue until we call factorial recursively with 5.
743     gdb_test "continue" \
744         "Continuing.*Break.* factorial .value=5. .*" \
745         "continue to factorial(5)"
747     # Do a backtrace just to confirm how many levels deep we are.
749     gdb_test "backtrace" \
750         "#0\[ \t\]+ factorial .value=5..*" \
751         "backtrace from factorial(5)"
753     # Now a "next" should position us at the recursive call, which
754     # we will be performing with 4.
756     gdb_test "next" \
757         ".* factorial .value - 1.;.*" \
758         "next to recursive call"
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.
766     delete_breakpoints
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.
784     gdb_test next "\[0-9\]*\[\t \]+return \\(value\\);.*" \
785             "next over recursive call"
787     # OK, we should be back in the same stack frame we started from.
788     # Do a backtrace just to confirm.
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)"
794     if { ![target_info exists gdb,noresults] } {
795         gdb_continue_to_end "recursive next test"
796     }
799 test_next_with_recursion
802 #********
804 # build a new file with optimization enabled so that we can try breakpoints
805 # on targets with optimized prologues
807 if { [build_executable "failed to prepare" "breako2" {break.c break1.c} {debug nowarnings optimize=-O2}] } {
808     return -1
811 proc_with_prefix test_break_optimized_prologue {} {
812     clean_restart breako2
814     # test break at function
815     gdb_test "break -q main" \
816         "Breakpoint.*at.* file .*, line.*" \
817         "breakpoint function, optimized file"
819     # test break at function
820     gdb_test "break marker4" \
821         "Breakpoint.*at.* file .*$::srcfile1, line.*" \
822         "breakpoint small function, optimized file"
824     # run until the breakpoint at main is hit. For non-stubs-using targets.
825     gdb_run_cmd
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     }
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.
847     set bp_location14 [gdb_get_line_number "set breakpoint 14 here" $::srcfile1]
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         }
864 test_break_optimized_prologue
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.
869 # On x86 GNU/Linux, this test will choke on e.g. __libc_start_main@plt.
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.
875 proc_with_prefix test_rbreak_shlib {} {
876     clean_restart breako2
878     gdb_test_no_output "set breakpoint pending on" "rbreak junk pending setup"
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.*"
884     # Run to a breakpoint.  Fail if we see "Junk at end of arguments".
885     gdb_run_cmd
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     }
897 test_rbreak_shlib
899 # Test break via convenience variable with file name
901 proc_with_prefix test_break_file_line_convenience_var {} {
902     clean_restart breako2
904     set line [gdb_get_line_number "set breakpoint 1 here"]
905     gdb_test_no_output "set \$l = $line"
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     }
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"
928 test_break_file_line_convenience_var
930 # Test that commands can be cleared without error.
932 proc_with_prefix test_break_commands_clear {} {
933     clean_restart breako2
935     set line [gdb_get_line_number "set breakpoint 1 here"]
936     gdb_breakpoint $line
938     gdb_test "commands\nprint 232323\nend" ">end" "set some breakpoint commands"
939     gdb_test "commands\nend" ">end" "clear breakpoint commands"
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"
947 test_break_commands_clear