1 # Copyright 2012-2019 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 # Tests for explicit locations
18 load_lib completion-support.exp
20 standard_testfile explicit.c explicit2.c 3explicit.c
23 if {[prepare_for_testing "failed to prepare" $exefile \
24 [list $srcfile $srcfile2 $srcfile3] {debug nowarnings}]} {
28 # Wrap the entire test in a namespace to avoid contaminating other tests.
29 namespace eval $testfile {
31 # Test the given (explicit) LINESPEC which should cause gdb to break
33 proc test_breakpoint {linespec location} {
35 set testname "set breakpoint at \"$linespec\""
36 # Delete all breakpoints, set a new breakpoint at LINESPEC,
37 # and attempt to run to it.
39 if {[gdb_breakpoint $linespec]} {
41 send_log "\nexpecting locpattern \"$location\"\n"
42 gdb_continue_to_breakpoint $linespec $location
48 # Add the given LINESPEC to the array named in THEARRAY. GDB is expected
49 # to stop at LOCATION.
50 proc add {thearray linespec location} {
53 lappend ar(linespecs) $linespec
54 lappend ar(locations) $location
57 # A list of all explicit linespec arguments.
58 variable all_arguments
59 set all_arguments {"source" "function" "label" "line"}
61 # Some locations used in this test
64 set lineno(normal) [gdb_get_line_number "myfunction location" $srcfile]
65 set lineno(top) [gdb_get_line_number "top location" $srcfile]
66 foreach v [array names lineno] {
67 set location($v) ".*[string_to_regexp "$srcfile:$lineno($v)"].*"
70 # A list of explicit locations and the corresponding location.
72 set linespecs(linespecs) {}
73 set linespecs(location) {}
75 add linespecs "-source $srcfile -function myfunction" $location(normal)
76 add linespecs "-source $srcfile -function myfunction -label top" \
79 # This isn't implemented yet; -line is silently ignored.
80 add linespecs "-source $srcfile -function myfunction -label top -line 3" \
82 add linespecs "-source $srcfile -line $lineno(top)" $location(top)
83 add linespecs "-function myfunction" $location(normal)
84 add linespecs "-function myfunction -label top" $location(top)
86 # These are also not yet supported; -line is silently ignored.
87 add linespecs "-function myfunction -line 3" $location(normal)
88 add linespecs "-function myfunction -label top -line 3" $location(top)
89 add linespecs "-line 3" $location(normal)
97 gdb_test_no_output "set confirm off"
99 # Simple error tests (many more are tested in ls-err.exp)
100 foreach arg $all_arguments {
101 # Test missing argument
102 gdb_test "break -$arg" \
103 [string_to_regexp "missing argument for \"-$arg\""]
106 set short [string range $arg 0 3]
107 if { $arg != $short } {
108 gdb_test "break -$short" \
109 [string_to_regexp "missing argument for \"-$short\""]
113 # Test invalid arguments
114 foreach arg {"-foo" "-foo bar" "-function myfunction -foo" \
115 "-function -myfunction -foo bar"} {
116 gdb_test "break $arg" \
117 [string_to_regexp "invalid explicit location argument, \"-foo\""]
120 # Test explicit locations, with and without conditions.
121 # For these tests, it is easiest to turn of pending breakpoint.
122 gdb_test_no_output "set breakpoint pending off" \
123 "turn off pending breakpoints"
125 foreach linespec $linespecs(linespecs) loc_pattern $linespecs(locations) {
128 test_breakpoint $linespec $loc_pattern
130 # Test with a valid condition
132 set tst "set breakpoint at \"$linespec\" with valid condition"
133 if {[gdb_breakpoint "$linespec if arg == 0"]} {
136 gdb_test "info break" ".*stop only if arg == 0.*" \
137 "info break of conditional breakpoint at \"$linespec\""
142 # Test with invalid condition
143 gdb_test "break $linespec if foofoofoo == 1" \
144 ".*No symbol \"foofoofoo\" in current context.*" \
145 "set breakpoint at \"$linespec\" with invalid condition"
149 gdb_test "break $linespec thread 123" "Unknown thread 123."
152 # Tests below are about tab-completion, which doesn't work if readline
153 # library isn't used. Check it first.
154 if { [readline_is_used] } {
156 # Test the explicit location completer
157 foreach abbrev {"fun" "so" "lab" "li"} full {"function" "source" "label" "line"} {
158 set tst "complete 'break -$abbrev'"
159 send_gdb "break -${abbrev}\t"
160 gdb_test_multiple "" $tst {
161 -re "break -$full " {
163 gdb_test_multiple "" $tst {
164 -re "missing argument for \"-$full\".*$gdb_prompt " {
170 set tst "complete -$full with no value"
171 send_gdb "break -$full \t"
172 gdb_test_multiple "" $tst {
173 -re ".*break -$full " {
175 gdb_test_multiple "" $tst {
176 -re ".*Source filename requires function, label, or line offset\..*$gdb_prompt " {
177 if {[string equal $full "source"]} {
183 -re "missing argument for \"-$full\".*$gdb_prompt " {
191 set tst "complete unique function name"
192 send_gdb "break -function my_unique_func\t"
193 gdb_test_multiple "" $tst {
194 -re "break -function my_unique_function_name" {
196 gdb_test "" ".*Breakpoint \[0-9\]+.*" $tst
197 gdb_test_no_output "delete \$bpnum" "delete $tst breakpoint"
201 set tst "complete non-unique function name"
202 send_gdb "break -function myfunc\t"
203 gdb_test_multiple "" $tst {
204 -re "break -function myfunc\\\x07tion" {
206 gdb_test_multiple "" $tst {
207 -re "\\\x07\r\nmyfunction\[ \t\]+myfunction2\[ \t\]+myfunction3\[ \t\]+myfunction4\[ \t\]+\r\n$gdb_prompt " {
208 gdb_test "2" ".*Breakpoint \[0-9\]+.*" $tst
209 gdb_test_no_output "delete \$bpnum" "delete $tst breakpoint"
215 set tst "complete non-existant function name"
216 send_gdb "break -function foo\t"
217 gdb_test_multiple "" $tst {
218 -re "break -function foo\\\x07" {
220 gdb_test_multiple "" $tst {
223 gdb_test "" {Function "foo" not defined.} $tst
229 with_test_prefix "complete unique file name" {
230 foreach qc $completion::maybe_quoted_list {
231 set cmd "break -source ${qc}3explicit.c${qc}"
232 test_gdb_complete_unique \
233 "break -source ${qc}3ex" \
236 {Source filename requires function, label, or line offset.}
240 set tst "complete non-unique file name"
241 send_gdb "break -source exp\t"
242 gdb_test_multiple "" $tst {
243 -re "break -source exp\\\x07licit" {
245 gdb_test_multiple "" $tst {
246 -re "\\\x07\r\nexplicit.c\[ \t\]+explicit2.c\[ \t\]+\(expl.*\)?\r\n$gdb_prompt" {
249 {Source filename requires function, label, or line offset.} \
256 set tst "complete non-existant file name"
257 send_gdb "break -source foo\t"
258 gdb_test_multiple "" $tst {
259 -re "break -source foo" {
261 gdb_test_multiple "" $tst {
265 {Source filename requires function, label, or line offset.} \
272 set tst "complete filename and unique function name"
273 send_gdb "break -source explicit.c -function ma\t"
274 gdb_test_multiple "" $tst {
275 -re "break -source explicit.c -function main " {
277 gdb_test "" ".*Breakpoint .*" $tst
278 gdb_test_no_output "delete \$bpnum" "delete $tst breakpoint"
282 set tst "complete filename and non-unique function name"
283 send_gdb "break -so 3explicit.c -func myfunc\t"
284 gdb_test_multiple "" $tst {
285 -re "break -so 3explicit.c -func myfunc\\\x07tion" {
287 gdb_test_multiple "" $tst {
288 -re "\\\x07\r\nmyfunction3\[ \t\]+myfunction4\[ \t\]+\r\n$gdb_prompt " {
289 gdb_test "3" ".*Breakpoint \[0-9\]+.*" $tst
290 gdb_test_no_output "delete \$bpnum" "delete $tst breakpoint"
296 set tst "complete filename and non-existant function name"
297 send_gdb "break -sou 3explicit.c -fun foo\t"
298 gdb_test_multiple "" $tst {
299 -re "break -sou 3explicit.c -fun foo\\\x07" {
301 gdb_test_multiple "" $tst {
305 {Function "foo" not defined in "3explicit.c".} $tst
311 set tst "complete filename and function reversed"
312 send_gdb "break -func myfunction4 -source 3ex\t"
313 gdb_test_multiple "" $tst {
314 -re "break -func myfunction4 -source 3explicit.c " {
316 gdb_test "" "Breakpoint \[0-9\]+.*" $tst
317 gdb_test_no_output "delete \$bpnum" "delete $tst breakpoint"
321 with_test_prefix "complete unique label name" {
322 foreach qc $completion::maybe_quoted_list {
323 test_gdb_complete_unique \
324 "break -function myfunction -label ${qc}to" \
325 "break -function myfunction -label ${qc}top${qc}"
329 with_test_prefix "complete unique label name with source file" {
330 test_gdb_complete_unique \
331 "break -source explicit.c -function myfunction -label to" \
332 "break -source explicit.c -function myfunction -label top"
335 with_test_prefix "complete unique label name reversed" {
336 test_gdb_complete_multiple "b -label top -function " "myfunction" "" {
344 with_test_prefix "complete non-unique label name" {
345 test_gdb_complete_multiple "b -function myfunction -label " "" "" {
351 # The program is stopped at myfunction, so gdb is able to
352 # infer the label's function.
353 with_test_prefix "complete label name with no function" {
354 test_gdb_complete_unique \
357 check_bp_locations_match_list \
359 "-function myfunction -label top"
364 with_test_prefix "complete label name with source file but no function" {
365 test_gdb_complete_unique \
366 "break -source explicit.c -label to" \
367 "break -source explicit.c -label top"
368 check_bp_locations_match_list \
369 "break -source explicit.c -label top" {
370 "-source explicit.c -function myfunction -label top"
374 with_test_prefix "complete label name with wrong source file" {
375 test_gdb_complete_none \
376 "break -source explicit2.c -function myfunction -label to"
377 check_setting_bp_fails \
378 "break -source explicit2.c -function myfunction -label top"
381 # Get rid of symbols from shared libraries, otherwise
382 # "b -source thr<tab>" could find some system library's
384 gdb_test_no_output "nosharedlibrary"
386 # Test that after a seemingly finished option argument,
387 # completion matches both the explicit location options and
388 # the linespec keywords.
389 set completions_list {
399 foreach what { "-function" "-label" "-line" "-source" } {
400 # Also test with "-qualified" appearing before the
402 foreach prefix {"" "-qualified "} {
404 # ... and with "-qualified" appearing after the
406 foreach suffix {"" " -qualified"} {
407 with_test_prefix "complete after $prefix$what$suffix" {
408 if {$what != "-line"} {
409 set w "$prefix$what argument$suffix "
410 test_gdb_complete_multiple \
411 "b $w" "" "" $completions_list
412 test_gdb_complete_unique \
415 test_gdb_complete_unique \
419 # After -line, we expect a number / offset.
420 foreach line {"10" "+10" "-10"} {
421 set w "$prefix-line $line$suffix"
422 test_gdb_complete_multiple \
423 "b $w " "" "" $completions_list
424 test_gdb_complete_unique \
427 test_gdb_complete_unique \
432 # With an invalid -line argument, we don't get any
434 test_gdb_complete_none "b $prefix-line argument$suffix "
441 # These tests don't make sense with "-qualified" after
443 with_test_prefix "complete after $prefix$what" {
444 # Don't complete a linespec keyword ("thread") or
445 # another option name when expecting an option
447 test_gdb_complete_none "b $prefix$what thr"
448 test_gdb_complete_none "b $prefix$what -fun"
453 # Tests that ensure that after "if" we complete on expressions
454 # are in cpcompletion.exp.
456 # Disable the completion limit for the rest of the testcase.
457 gdb_test_no_output "set max-completions unlimited"
459 # Get rid of symbols from shared libraries, otherwise the
460 # completions match list for "break <tab>" is huge and makes
461 # the test below quite long while the gdb_test_multiple loop
462 # below consumes the matches. Not doing this added ~20
463 # seconds at the time of writing. (Actually, already done above.)
464 # gdb_test_no_output "nosharedlibrary"
466 # Test completion with no input argument. We should see all
467 # the options, plus all the functions. To keep it simple, as
468 # proxy, we check for presence of one explicit location
469 # option, one probe location, and one function.
470 set saw_opt_function 0
471 set saw_opt_probe_stap 0
474 set tst "complete with no arguments"
476 gdb_test_multiple "" $tst {
479 gdb_test_multiple "" $tst {
485 set saw_opt_function 1
489 set saw_opt_probe_stap 1
496 -re "\r\n$gdb_prompt " {
497 gdb_assert {$saw_opt_function && $saw_opt_probe_stap && $saw_function} $tst
505 clear_input_line $tst
507 # NOTE: We don't bother testing more elaborate combinations of options,
508 # such as "-func main -sour 3ex\t" (main is defined in explicit.c).
509 # The completer cannot handle these yet.
511 # The following completion tests require having no symbols
516 # The match list you get when you complete with no options
518 set completion_list {
528 with_test_prefix "complete with no arguments and no symbols" {
529 test_gdb_complete_multiple "b " "" "-" $completion_list
530 test_gdb_complete_multiple "b " "-" "" $completion_list
533 # End of completion tests.
535 # Test pending explicit breakpoints
539 set tst "pending invalid conditional explicit breakpoint"
540 if {![gdb_breakpoint "-func myfunction if foofoofoo == 1" \
544 gdb_test "info break" ".*PENDING.*myfunction if foofoofoo == 1.*" $tst
550 set tst "pending valid conditional explicit breakpoint"
551 if {![gdb_breakpoint "-func myfunction if arg == 0" \
555 gdb_test "info break" ".*PENDING.*myfunction if arg == 0" $tst
557 gdb_load [standard_output_file $exefile]
558 gdb_test "info break" \
559 ".*in myfunction at .*$srcfile:.*stop only if arg == 0.*" \
563 # Test interaction of condition command and explicit linespec conditons.
566 gdb_load [standard_output_file $exefile]
568 set tst "condition_command overrides explicit linespec condition"
572 if {![gdb_breakpoint "-func myfunction if arg == 1"]} {
573 fail "set breakpoint with condition 'arg == 1'"
575 gdb_test_no_output "cond 2 arg == 0" \
576 "set new breakpoint condition for explicit linespec"
578 gdb_continue_to_breakpoint $tst $location(normal)
582 gdb_test "cond 2" [string_to_regexp "Breakpoint 2 now unconditional."] \
583 "clear condition for explicit breakpoint"
584 set tst "info break of cleared condition of explicit breakpoint"
585 gdb_test_multiple "info break" $tst {
586 -re ".*in myfunction at .*$srcfile:.*stop only if arg == 0.*" {
589 -re ".*in myfunction at .*$srcfile:.*$gdb_prompt $" {
594 # Test explicit "ranges." Make sure that using explicit
595 # locations doesn't alter the expected outcome.
596 gdb_test "list main" ".*" "list main 1"
597 set list_result [capture_command_output "list -,+" ""]
598 gdb_test "list main" ".*" "list main 2"
599 gdb_test "list -line -,-line +" [string_to_regexp $list_result]
601 # Ditto for the reverse (except that no output is expected).
602 gdb_test "list myfunction" ".*" "list myfunction 1"
603 gdb_test_no_output "list +,-"
604 gdb_test "list myfunction" ".*" "list myfunction 2"
605 gdb_test_no_output "list -line +, -line -"
608 namespace delete $testfile