1# Copyright 2011-2023 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# Tests of ambiguous linespecs.
17
18standard_testfile lspec.cc
19
20set exefile $testfile
21
22set baseone base/one/thefile.cc
23set basetwo base/two/thefile.cc
24
25if {[skip_cplus_tests]} {
26    unsupported "skipping c++ tests"
27    return
28}
29
30if {[prepare_for_testing "failed to prepare" $exefile \
31	 [list $srcfile $baseone $basetwo] \
32	 {debug nowarnings c++}]} {
33    return -1
34}
35
36gdb_test_no_output "set multiple-symbols all" \
37    "set multiple-symbols to all for linespec tests"
38
39set l1 [gdb_get_line_number "thefile breakpoint" $baseone]
40set l2 [gdb_get_line_number "thefile breakpoint" $basetwo]
41
42if {$l1 != $l2} {
43    error "somebody incompatibly modified the source files needed by linespec.exp"
44}
45
46gdb_test_no_output "set breakpoint pending off" \
47    "disable pending breakpoints for linespec tests"
48
49# Copying files to a remote host loses the directory prefix during
50# compilation.
51if { [is_remote host] } {
52    untested "breakpoints using dir/file:line"
53} else {
54    gdb_test "break one/thefile.cc:$l1" \
55        "Breakpoint $decimal at $hex: file .*thefile.cc, line $l1." \
56        "single-location break using dir/file:line"
57
58    gdb_test "clear one/thefile.cc:$l1" \
59        "Deleted breakpoint $decimal *" \
60        "clear breakpoint using dir/file:line"
61
62    if { [readline_is_used] } {
63	# There are functions named 'twodup' in both source files.
64	# Both should be found if we restrict the linespec to the
65	# ambiguous "thefile.cc" source filename.  Check both
66	# completion and setting the breakpoint.
67	set tst "complete unique function name in two source files"
68	send_gdb "break thefile.cc:t\t"
69	gdb_test_multiple "" $tst {
70	    -re "break thefile.cc:twodup\\(\\) " {
71		pass $tst
72
73		send_gdb "\n"
74		gdb_test "" \
75		    "Breakpoint $decimal at $hex: thefile.cc:twodup\\(\\). \[(\]2 locations\[)\]" \
76		    "set break at unique function name in two source files"
77	    }
78	}
79
80	# Check both completing and setting a breakpoint on a linespec
81	# with a source component, where there's more than one source
82	# file with the same basename.  We should find the functions
83	# in all matching sources -- one/thefile.cc and
84	# two/thefile.cc.  The "one" file has "z1()", while the "two"
85	# file has "z2()".
86	set tst "complete non-unique function name in two source files"
87	send_gdb "break thefile.cc:z\t"
88	gdb_test_multiple "" $tst {
89	    -re "break thefile.cc:z\\\x07" {
90		send_gdb "\t"
91		gdb_test_multiple "" $tst {
92		    -re "\r\nz1\\(\\)\[ \t\]+z2\\(\\)\[ \t\]+\r\n$gdb_prompt " {
93			pass $tst
94
95			send_gdb "\n"
96			gdb_test "" \
97			    "Function \"z\" not defined in \"thefile.cc\"." \
98			    "set break at non-unique function name in two source files"
99		    }
100		}
101	    }
102	}
103
104	# Now check that disambiguating the source path makes GDB only
105	# match the symbols in that file.  "z" should now have a
106	# unique completion to "z1()", and setting the breakpoint
107	# should find only one location.
108	set tst "complete unique function name in disambiguated source file"
109	send_gdb "break one/thefile.cc:z\t"
110	gdb_test_multiple "" $tst {
111	    -re "break one/thefile.cc:z1\\(\\) " {
112		pass $tst
113
114		send_gdb "\n"
115		gdb_test "" \
116		    "Breakpoint $decimal at $hex: file .*thefile.cc, line \[^\r\n\]*" \
117		    "set break at unique function name in disambiguated source file"
118		}
119	}
120
121	# Check that using a non-existing source path does not confuse
122	# GDB.  It should not match any symbol.
123	set dir_file "one/thefile.cc"
124	set non_existing "/some/non-existing/absolute/path/prefix/$dir_file"
125	set non_existing_re [string_to_regexp $non_existing]
126
127	set tst "complete functions in non-existing absolute path"
128	send_gdb "break $non_existing:\t"
129	gdb_test_multiple "" $tst {
130	    -re "break $non_existing_re:\\\x07" {
131		send_gdb "\t\t"
132		gdb_test_multiple "" $tst {
133		    -re "^\\\x07\\\x07" {
134			pass $tst
135
136			# There's a function called 'twodup' in each
137			# of the thefile.cc files.  Make sure none is
138			# picked.
139			send_gdb "twodup\n"
140			gdb_test "" \
141			    "No source file named $non_existing_re." \
142			    "set break in function in non-existing absolute path"
143		    }
144		}
145	    }
146	}
147    }
148}
149
150gdb_test "break thefile.cc:$l1" \
151    "Breakpoint $decimal at $hex: thefile.cc:$l1. \[(\]2 locations\[)\]" \
152    "multi-location break using file:line"
153
154gdb_test "break dupname" \
155    "Breakpoint $decimal at $hex: dupname. \[(\]2 locations\[)\]" \
156    "multi-location break using duplicate function name"
157
158gdb_test "break dupname:label" \
159    "Breakpoint $decimal at $hex: dupname:label. \[(\]2 locations\[)\]" \
160    "multi-location break using duplicate function name and label"
161
162# Testing if the "condition" command completes only the breakpoints,
163# not the locations.
164gdb_test "complete condition " "condition $decimal\r\ncondition $decimal\r\ncondition $decimal"
165
166# This is PR breakpoints/12856.
167gdb_test "break lspec.cc:nosuchfunction" \
168    "Function \"nosuchfunction\" not defined in \"lspec.cc\"." \
169    "set breakpoint on non-existent function"
170
171gdb_test "break NameSpace::overload" \
172    "Breakpoint \[0-9\]+ at $hex: NameSpace::overload. \[(\]3 locations\[)\]" \
173    "set breakpoint at all instances of NameSpace::overload"
174
175gdb_test "break lspec.cc:NameSpace::overload" \
176    "Breakpoint \[0-9\]+ at $hex: file .*lspec.cc, line 7." \
177    "set breakpoint at lspec.cc instance of NameSpace::overload"
178
179gdb_test "break lspec.cc:NameSpace::overload(double)" \
180    "Function \"NameSpace::overload\\(double\\)\" not defined in \"lspec.cc\"." \
181    "set breakpoint at non-existent lspec.cc instance of NameSpace::overload"
182
183gdb_test "break NameSpace::overload()" \
184    "Breakpoint \[0-9\]+ at $hex: file .*lspec.cc, line 7." \
185    "set breakpoint at specific instance of NameSpace::overload"
186
187# This should manage to set a breakpoint even though body.h does not
188# include all of the function in question.
189set line [gdb_get_line_number "body breakpoint no code" body.h]
190gdb_test "break body.h:$line" \
191    "Breakpoint \[0-9\]+.*" \
192    "set breakpoint in body.h"
193
194# This should only have a single location -- in f1.
195set line [gdb_get_line_number "f1 breakpoint" lspec.h]
196gdb_test "break lspec.h:$line" \
197    "Breakpoint \[0-9\]+ at $hex: file .*lspec.h, line $line." \
198    "set breakpoint in f1"
199
200#
201# Multi-inferior tests.
202#
203
204gdb_test "add-inferior" "Added inferior 2.*" \
205    "add inferior for linespec tests"
206
207gdb_test "inferior 2" "Switching to inferior 2 .*" \
208    "switch to inferior 2 for linespec tests"
209
210# Note that in particular this should not cause errors when re-setting
211# breakpoints.
212gdb_test "file $binfile" \
213    "Reading symbols from .*" \
214    "set the new inferior file for linespec tests"
215
216gdb_test "break -q main" \
217    "Breakpoint \[0-9\]+ at $hex: -qualified main. .2 locations." \
218    "set breakpoint at main in both inferiors"
219