1 # Copyright 2014-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/>.
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
22 proc search_dir_list { dirs } {
23 set output "\r\nSource directories searched: "
24 append output [join $dirs "\[:;\]"]
29 # Check that adding directories to the search path changes the order
30 # in which directories are searched.
31 proc test_changing_search_directory {} {
36 gdb_test "directory $foo/a $foo/b $foo/c" \
37 [search_dir_list [list \
43 gdb_test "directory $foo/b $foo/d $foo/c" \
44 [search_dir_list [list \
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.
56 proc test_truncated_comp_dir {} {
57 global srcfile srcdir subdir binfile
60 # When we run this test the current directory will be something
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/
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/
72 # As a result the debug information will look like this:
74 # DW_AT_name : source-dir.c
75 # DW_AT_comp_dir : /gdb.base/source-dir
77 # Finally we switch back to this directory:
78 # /some/path/to/gdb/build/testsuite/
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.
84 # All of these pathname and directory manipulations assume
85 # host == build, so do not attempt this set of tests on remote host.
90 set working_dir [standard_output_file ""]
91 with_cwd $working_dir {
92 set strip_dir [file normalize "${working_dir}/../.."]
94 set new_srcfile [standard_output_file ${srcfile}]
95 set fd [open "$new_srcfile" w]
104 "debug additional_flags=-fdebug-prefix-map=${strip_dir}="
105 if { [gdb_compile "${srcfile}" "${binfile}" \
106 executable ${options}] != "" } {
107 untested "failed to compile"
112 clean_restart ${binfile}
114 if { [ishost *-*-mingw*] } {
115 gdb_test_no_output "set directories \$cdir;\$cwd"
117 gdb_test_no_output "set directories \$cdir:\$cwd"
119 gdb_test "show directories" \
120 "\r\nSource directories searched: \\\$cdir\[:;\]\\\$cwd"
126 gdb_test "info source" \
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"
136 gdb_test "dir $strip_dir" \
137 [search_dir_list [list \
141 "setup source path search directory"
142 gdb_test "list" [multi_line \
144 "2\[ \t\]+main \\(\\)" \
146 "4\[ \t\]+return 0;" \
149 gdb_test "info source" \
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\]+" \
159 "info source after setting directory search list"
162 proc test_change_search_directory_with_empty_dirname {} {
165 # Add 3 entries to the source directories list:
169 # Since /foo and /bar probably do not exist, ignore the warnings printed by
171 if { [ishost *-*-mingw*] } {
172 gdb_test "set directories ;/foo;/bar" ".*"
174 gdb_test "set directories :/foo:/bar" ".*"
177 # The first entry added ("") should be ignored, only /foo and /bar are
179 with_test_prefix "initial_directory_state" {
180 gdb_test "show directories" \
181 [search_dir_list [list \
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 \"\""
192 with_test_prefix "directory_after_reset" {
193 gdb_test "show directories" \
194 [search_dir_list [list \
202 test_changing_search_directory
203 test_change_search_directory_with_empty_dirname
204 test_truncated_comp_dir