Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / source-dir.exp
1 # Copyright 2014-2022 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 standard_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.
22 proc 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.
31 proc 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.
56 proc 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 original_dir [pwd]
91 set working_dir [standard_output_file ""]
92 cd ${working_dir}
93
94 set strip_dir [file normalize "${working_dir}/../.."]
95
96 set new_srcfile [standard_output_file ${srcfile}]
97 set fd [open "$new_srcfile" w]
98 puts $fd "int
99 main ()
100 {
101 return 0;
102 }"
103 close $fd
104
105 set options \
106 "debug additional_flags=-fdebug-prefix-map=${strip_dir}="
107 if { [gdb_compile "${srcfile}" "${binfile}" \
108 executable ${options}] != "" } {
109 untested "failed to compile"
110 return -1
111 }
112
113 cd ${original_dir}
114
115 clean_restart ${binfile}
116
117 if { [ishost *-*-mingw*] } {
118 gdb_test_no_output "set directories \$cdir;\$cwd"
119 } else {
120 gdb_test_no_output "set directories \$cdir:\$cwd"
121 }
122 gdb_test "show directories" \
123 "\r\nSource directories searched: \\\$cdir\[:;\]\\\$cwd"
124
125 if ![runto_main] then {
126 fail "can't run to main"
127 return 0
128 }
129
130 gdb_test "info source" \
131 [multi_line \
132 "Current source file is ${srcfile}" \
133 "Compilation directory is \[^\n\r\]+" \
134 "Source language is c." \
135 "Producer is \[^\n\r\]+" \
136 "Compiled with DWARF $decimal debugging format." \
137 "Does not include preprocessor macro info." ] \
138 "info source before setting directory search list"
139
140 gdb_test "dir $strip_dir" \
141 [search_dir_list [list \
142 "$strip_dir" \
143 "\\\$cdir" \
144 "\\\$cwd"]] \
145 "setup source path search directory"
146 gdb_test "list" [multi_line \
147 "1\[ \t\]+int" \
148 "2\[ \t\]+main \\(\\)" \
149 "3\[ \t\]+\\{" \
150 "4\[ \t\]+return 0;" \
151 "5\[ \t\]+\\}" ]
152
153 gdb_test "info source" \
154 [multi_line \
155 "Current source file is ${srcfile}" \
156 "Compilation directory is \[^\n\r\]+" \
157 "Located in ${new_srcfile}" \
158 "Contains 5 lines." \
159 "Source language is c." \
160 "Producer is \[^\n\r\]+" \
161 "\[^\n\r\]+" \
162 "\[^\n\r\]+" ] \
163 "info source after setting directory search list"
164 }
165
166 proc test_change_search_directory_with_empty_dirname {} {
167 gdb_start
168
169 # Add 3 entries to the source directories list:
170 # - ""
171 # - "/foo"
172 # - "/bar"
173 # Since /foo and /bar probably do not exist, ignore the warnings printed by
174 # GDB.
175 if { [ishost *-*-mingw*] } {
176 gdb_test "set directories ;/foo;/bar" ".*"
177 } else {
178 gdb_test "set directories :/foo:/bar" ".*"
179 }
180
181 # The first entry added ("") should be ignored, only /foo and /bar are
182 # effectively added.
183 with_test_prefix "initial_directory_state" {
184 gdb_test "show directories" \
185 [search_dir_list [list \
186 "/foo" \
187 "/bar" \
188 "\\\$cdir" \
189 "\\\$cwd"]]
190 }
191
192 # Arguments can be quoted. Check a empty string has the same effect as
193 # 'set directory' (i.e. reset to $cdir:$cwd)
194 gdb_test_no_output "set directories \"\""
195
196 with_test_prefix "directory_after_reset" {
197 gdb_test "show directories" \
198 [search_dir_list [list \
199 "\\\$cdir" \
200 "\\\$cwd"]]
201 }
202
203 gdb_exit
204 }
205
206 test_changing_search_directory
207 test_change_search_directory_with_empty_dirname
208 test_truncated_comp_dir
This page took 0.033103 seconds and 4 git commands to generate.