xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.base/source-dir.exp (revision 22ebeae4b2252475e0ebe332f69734639cb946ea)
1# Copyright 2014-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
16standard_testfile
17
18# Take a list of directories DIRS, and return a regular expression
19# that will match against the output of the 'directory' command
20# assuming that DIRS are all of the directories that should appear in
21# the results.
22proc search_dir_list { dirs } {
23    set output "\r\nSource directories searched: "
24    append output [join $dirs "\[:;\]"]
25
26    return ${output}
27}
28
29# Check that adding directories to the search path changes the order
30# in which directories are searched.
31proc test_changing_search_directory {} {
32    gdb_start
33
34    set foo "/nOtExStInG"
35
36    gdb_test "directory $foo/a $foo/b $foo/c" \
37	[search_dir_list [list \
38			      "$foo/a" \
39			      "$foo/b" \
40			      "$foo/c" \
41			      "\\\$cdir" \
42			      "\\\$cwd"]]
43    gdb_test "directory $foo/b $foo/d $foo/c" \
44	[search_dir_list [list \
45			      "$foo/b" \
46			      "$foo/d" \
47			      "$foo/c" \
48			      "$foo/a" \
49			      "\\\$cdir" \
50			      "\\\$cwd"]]
51    gdb_exit
52}
53
54# Test that the compilation directory can also be extended with a
55# prefix from the directory search path in order to find source files.
56proc test_truncated_comp_dir {} {
57    global srcfile srcdir subdir binfile
58    global decimal
59
60    # When we run this test the current directory will be something
61    # like this:
62    #     /some/path/to/gdb/build/testsuite/
63    # We are going to copy the source file out of the source tree into
64    # a location like this:
65    #     /some/path/to/gdb/build/testsuite/output/gdb.base/soure-dir/
66    #
67    # We will then switch to this directory and compile the source
68    # file, however, we will ask GCC to remove this prefix from the
69    # compilation directory in the debug info:
70    #     /some/path/to/gdb/build/testsuite/output/
71    #
72    # As a result the debug information will look like this:
73    #
74    #     DW_AT_name        : source-dir.c
75    #     DW_AT_comp_dir    : /gdb.base/source-dir
76    #
77    # Finally we switch back to this directory:
78    #     /some/path/to/gdb/build/testsuite/
79    #
80    # and start GDB.  There was a time when GDB would be unable to
81    # find the source file no matter what we added to the directory
82    # search path, this should now be fixed.
83
84    # All of these pathname and directory manipulations assume
85    # host == build, so do not attempt this set of tests on remote host.
86    if [is_remote host] {
87        return
88    }
89
90    set working_dir [standard_output_file ""]
91    with_cwd $working_dir {
92	set strip_dir [file normalize "${working_dir}/../.."]
93
94	set new_srcfile [standard_output_file ${srcfile}]
95	set fd [open "$new_srcfile" w]
96	puts $fd "int
97	main ()
98	{
99	  return 0;
100	}"
101	close $fd
102
103	set options \
104	    "debug additional_flags=-fdebug-prefix-map=${strip_dir}="
105	if  { [gdb_compile "${srcfile}" "${binfile}" \
106		   executable ${options}] != "" } {
107	    untested "failed to compile"
108	    return -1
109	}
110    }
111
112    clean_restart ${binfile}
113
114    if { [ishost *-*-mingw*] } {
115        gdb_test_no_output "set directories \$cdir;\$cwd"
116    } else {
117	gdb_test_no_output "set directories \$cdir:\$cwd"
118    }
119    gdb_test "show directories" \
120	"\r\nSource directories searched: \\\$cdir\[:;\]\\\$cwd"
121
122    if {![runto_main]} {
123	return 0
124    }
125
126    gdb_test "info source" \
127    [multi_line \
128	 "Current source file is ${srcfile}" \
129	 "Compilation directory is \[^\n\r\]+" \
130	 "Source language is c." \
131	 "Producer is \[^\n\r\]+" \
132	 "Compiled with DWARF $decimal debugging format." \
133	 "Does not include preprocessor macro info." ] \
134	"info source before setting directory search list"
135
136    gdb_test "dir $strip_dir" \
137	[search_dir_list [list \
138			      "$strip_dir" \
139			      "\\\$cdir" \
140			      "\\\$cwd"]] \
141	"setup source path search directory"
142    gdb_test "list" [multi_line \
143			 "1\[ \t\]+int" \
144			 "2\[ \t\]+main \\(\\)" \
145			 "3\[ \t\]+\\{" \
146			 "4\[ \t\]+return 0;" \
147			 "5\[ \t\]+\\}" ]
148
149    gdb_test "info source" \
150	[multi_line \
151	     "Current source file is ${srcfile}" \
152	     "Compilation directory is \[^\n\r\]+" \
153	     "Located in ${new_srcfile}" \
154	     "Contains 5 lines." \
155	     "Source language is c." \
156	     "Producer is \[^\n\r\]+" \
157	     "\[^\n\r\]+" \
158	     "\[^\n\r\]+" ] \
159	"info source after setting directory search list"
160}
161
162proc test_change_search_directory_with_empty_dirname {} {
163    gdb_start
164
165    # Add 3 entries to the source directories list:
166    # - ""
167    # - "/foo"
168    # - "/bar"
169    # Since /foo and /bar probably do not exist, ignore the warnings printed by
170    # GDB.
171    if { [ishost *-*-mingw*] } {
172	gdb_test "set directories ;/foo;/bar" ".*"
173    } else {
174	gdb_test "set directories :/foo:/bar" ".*"
175    }
176
177    # The first entry added ("") should be ignored, only /foo and /bar are
178    # effectively added.
179    with_test_prefix "initial_directory_state" {
180	gdb_test "show directories" \
181	    [search_dir_list [list \
182				  "/foo" \
183				  "/bar" \
184				  "\\\$cdir" \
185				  "\\\$cwd"]]
186    }
187
188    # Arguments can be quoted.  Check a empty string has the same effect as
189    # 'set directory' (i.e. reset to $cdir:$cwd)
190    gdb_test_no_output "set directories \"\""
191
192    with_test_prefix "directory_after_reset" {
193	gdb_test "show directories" \
194	    [search_dir_list [list \
195				  "\\\$cdir" \
196				  "\\\$cwd"]]
197    }
198
199    gdb_exit
200}
201
202test_changing_search_directory
203test_change_search_directory_with_empty_dirname
204test_truncated_comp_dir
205