1 # Copyright 2020-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/>.
16 # Test debuginfod functionality
21 load_lib debuginfod-support.exp
23 require allow_debuginfod_tests
25 set sourcetmp [standard_output_file tmp-${srcfile}]
26 set outputdir [standard_output_file {}]
28 # Make a copy source file that we can move around
29 if { [catch {file copy -force ${srcdir}/${subdir}/${srcfile} \
30 [standard_output_file ${sourcetmp}]}] != 0 } {
31 error "create temporary file"
35 if { [gdb_compile "$sourcetmp" "$binfile" executable {debug build-id}] != "" } {
36 untested "failed to compile"
40 if { [gdb_compile "$sourcetmp" "${binfile}2" executable {debug build-id}] != "" } {
45 # Write some assembly that just has a .gnu_debugaltlink section.
46 # Copied from testsuite/gdb.dwarf2/dwzbuildid.exp.
47 proc write_just_debugaltlink {filename dwzname buildid} {
48 set asm_file [standard_output_file $filename]
50 Dwarf::assemble $asm_file {
54 gnu_debugaltlink $dwzname $buildid
56 # Only the DWARF reader checks .gnu_debugaltlink, so make sure
57 # there is a bit of DWARF in here.
59 compile_unit {{language @DW_LANG_C}} {
65 # Write some DWARF that also sets the buildid.
66 # Copied from testsuite/gdb.dwarf2/dwzbuildid.exp.
67 proc write_dwarf_file {filename buildid {value 99}} {
68 set asm_file [standard_output_file $filename]
70 Dwarf::assemble $asm_file {
71 declare_labels int_label int_label2
79 compile_unit {{language @DW_LANG_C}} {
80 int_label2: base_type {
83 {encoding @DW_ATE_signed}
89 {const_value $value data1}
96 set corefile [standard_output_file "corefile"]
98 # Setup the global variable DEBUGDIR as a directory containing the
99 # debug information for the test executable.
101 # Run some tests to confirm that GDB is not able to find any of the
102 # debug information from DEBUGDIR when the debuginfod server is not
104 proc_with_prefix no_url { } {
105 global binfile outputdir debugdir
107 setenv DEBUGINFOD_URLS ""
109 # Test that GDB cannot find source without debuginfod.
110 clean_restart $binfile
111 gdb_test_no_output "set substitute-path $outputdir /dev/null" \
112 "set substitute-path"
113 gdb_test "list" ".*No such file or directory.*"
115 # Strip symbols into separate file and move it so GDB cannot find it
116 # without debuginfod.
117 if { [gdb_gnu_strip_debug $binfile ""] != 0 } {
118 fail "strip debuginfo"
122 set debugdir [standard_output_file "debug"]
123 set debuginfo [standard_output_file "fetch_src_and_symbols.debug"]
126 file rename -force $debuginfo $debugdir
128 # Test that GDB cannot find symbols without debuginfod.
129 clean_restart $binfile
130 gdb_test "file" ".*No symbol file.*"
132 set buildid "01234567890abcdef0123456"
134 write_just_debugaltlink ${binfile}_has_altlink.S ${binfile}_dwz.o \
136 write_dwarf_file ${binfile}_dwz.S $buildid
138 if {[gdb_compile ${binfile}_has_altlink.S ${binfile}_alt.o object \
140 fail "compile main with altlink"
144 if {[gdb_compile ${binfile}_dwz.S ${binfile}_dwz.o object \
146 fail "compile altlink"
150 file rename -force ${binfile}_dwz.o $debugdir
152 # Test that GDB cannot find dwz without debuginfod.
154 gdb_test "file ${binfile}_alt.o" \
155 ".*could not find '.gnu_debugaltlink'.*" \
156 "file [file tail ${binfile}_alt.o]"
158 # Generate a core file and test that GDB cannot find the
160 clean_restart ${binfile}2
165 gdb_breakpoint [gdb_get_line_number "Breakpoint here"]
166 gdb_continue_to_breakpoint "stop at last line of main"
168 gdb_test "generate-core-file $::corefile" "Saved corefile $::corefile" \
169 "file [file tail $::corefile] gen"
170 file rename -force ${binfile}2 $debugdir
173 gdb_test "core $::corefile" ".*in ?? ().*" "file [file tail $::corefile]"
176 # Test that GDB prints the debuginfod URLs when loading files. URLS
177 # is the string set in the DEBUGINFOD_URLS environment variable.
178 # PATTERN_RE is the URLs pattern we expect to see out of GDB. TEST is
181 proc test_urls {urls pattern_re test} {
182 setenv DEBUGINFOD_URLS $urls
185 if {$pattern_re != ""} {
186 set urls_re " +${pattern_re}\r\n"
191 # Use "with confirm off" to avoid having to deal with the
192 # "Enable debuginfod for this session? (y or [n])" question.
193 gdb_test "with confirm off -- file $::binfile" \
194 "following URLs:\r\n${urls_re}Debuginfod .*" \
198 # Uses the global variables DEBUGDIR and DB which are setup elsewhere
201 # Start debuginfod server, and confirm that GDB can now find all the
202 # expected debug information.
203 proc_with_prefix local_url { } {
204 global binfile outputdir debugdir db
206 set url [start_debuginfod $db $debugdir]
208 unresolved "failed to start debuginfod server"
212 # Point the client to the server.
213 setenv DEBUGINFOD_URLS $url
215 # GDB should now find the symbol and source files.
217 gdb_test_no_output "set debuginfod enabled on" \
218 "enabled debuginfod for initial test"
220 gdb_test_no_output "set substitute-path $outputdir /dev/null" \
221 "set substitute-path"
223 set lineno [gdb_get_line_number "Breakpoint here"]
224 gdb_test "list $lineno" "return 0;\[^\r\n\]+Breakpoint here\\. .*"
226 # Verify that a breakpoint re-sets correctly when the actual location
227 # of the source file in the debuginfod client cache differs from
228 # the contents of DW_AT_comp_dir and DW_AT_name.
229 gdb_test "set cwd $debugdir" "" "file [file tail $binfile] cwd"
230 gdb_breakpoint $lineno
232 # Run to main, but don't delete all breakpoints.
233 if {![runto_main no-delete-breakpoints]} {
237 gdb_continue_to_breakpoint "runto breakpoint in main" \
238 ".* Breakpoint here\\. .*"
240 # GDB should now find the executable file.
241 set enable_debuginfod_question \
242 "Enable debuginfod for this session. \\(y or \\\[n\\\]\\) "
244 gdb_test "core $::corefile" ".*return 0.*" "file [file tail $::corefile]" \
245 $enable_debuginfod_question "y"
247 # GDB should now find the debugaltlink file.
249 gdb_test "file ${binfile}_alt.o" \
250 ".*Downloading.*separate debug info.*" \
251 "file [file tail ${binfile}_alt.o]" \
252 $enable_debuginfod_question "y"
254 # Configure debuginfod with commands.
255 unsetenv DEBUGINFOD_URLS
257 gdb_test "file $binfile" ".*No debugging symbols.*" \
258 "file [file tail $binfile] cmd"
259 gdb_test_no_output "set debuginfod enabled off"
260 gdb_test_no_output "set debuginfod urls $url" \
261 "set debuginfod url environment variable"
263 # GDB shouldn't find the debuginfo since debuginfod has been
265 gdb_test "file $binfile" ".*No debugging symbols.*" \
266 "file [file tail $binfile] cmd off"
268 # Enable debuginfod and fetch the debuginfo.
269 gdb_test_no_output "set debuginfod enabled on"
270 gdb_test "file $binfile" ".*Reading symbols from.*debuginfo.*" \
271 "file [file tail $binfile] cmd on"
273 # Test that URLs are printed correctly for the first-use notice.
275 # Empty URLS disables Debuginfod.
276 setenv DEBUGINFOD_URLS ""
278 # Disable confirmation to avoid having to deal with a query. See
280 set file_cmd "with confirm off -- file $binfile"
281 gdb_test_multiple $file_cmd "notice empty URL" {
282 -re -wrap "This GDB supports auto-downloading.*" {
290 # Whitespace-only URLS disables Debuginfod.
291 setenv DEBUGINFOD_URLS " "
293 gdb_test_multiple $file_cmd "notice whitespace URL" {
294 -re -wrap "This GDB supports auto-downloading.*" {
308 "notice 1 URL with whitespace"
310 set url2 [regsub "^http://" $url ""]
312 test_urls "$url $url2" \
313 "<$url>\r\n +<$url2>" \
316 test_urls " $url $url2 " \
317 "<$url>\r\n +<$url2>" \
318 "notice 2 URLs with whitespace"
321 # Create CACHE and DB directories ready for debuginfod to use.
322 prepare_for_debuginfod cache db
324 with_debuginfod_env $cache {