Add translations for various sub-directories
[binutils-gdb.git] / gdb / testsuite / gdb.debuginfod / fetch_src_and_symbols.exp
blob450d890f189601d33e71601e7550d1850294fa29
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
18 standard_testfile
20 load_lib dwarf.exp
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"
32     return -1
35 if { [gdb_compile "$sourcetmp" "$binfile" executable {debug build-id}] != "" } {
36     untested "failed to compile"
37     return -1
40 if { [gdb_compile "$sourcetmp" "${binfile}2" executable {debug build-id}] != "" } {
41     fail "compile"
42     return -1
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 {
51         upvar dwzname dwzname
52         upvar buildid buildid
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.
58         cu {} {
59             compile_unit {{language @DW_LANG_C}} {
60             }
61         }
62     }
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
73         upvar buildid buildid
74         upvar value value
76         build_id $buildid
78         cu {} {
79             compile_unit {{language @DW_LANG_C}} {
80                 int_label2: base_type {
81                     {name int}
82                     {byte_size 4 sdata}
83                     {encoding @DW_ATE_signed}
84                 }
86                 constant {
87                     {name the_int}
88                     {type :$int_label2}
89                     {const_value $value data1}
90                 }
91             }
92         }
93     }
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
103 # running.
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"
119         return -1
120     }
122     set debugdir [standard_output_file "debug"]
123     set debuginfo [standard_output_file "fetch_src_and_symbols.debug"]
125     file mkdir $debugdir
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 \
135         $buildid
136     write_dwarf_file ${binfile}_dwz.S $buildid
138     if {[gdb_compile ${binfile}_has_altlink.S ${binfile}_alt.o object \
139              nodebug] != ""} {
140         fail "compile main with altlink"
141         return -1
142     }
144     if {[gdb_compile ${binfile}_dwz.S ${binfile}_dwz.o object \
145              nodebug] != ""} {
146         fail "compile altlink"
147         return -1
148     }
150     file rename -force ${binfile}_dwz.o $debugdir
152     # Test that GDB cannot find dwz without debuginfod.
153     clean_restart
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
159     # executable.
160     clean_restart ${binfile}2
161     if ![runto_main] {
162         return -1
163     }
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
172     clean_restart
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
179 # the test name.
181 proc test_urls {urls pattern_re test} {
182     setenv DEBUGINFOD_URLS $urls
183     clean_restart
185     if {$pattern_re != ""} {
186         set urls_re " +${pattern_re}\r\n"
187     } else {
188         set urls_re ""
189     }
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 .*" \
195         $test
198 # Uses the global variables DEBUGDIR and DB which are setup elsewhere
199 # in this script.
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]
207     if { $url == "" } {
208         unresolved "failed to start debuginfod server"
209         return
210     }
212     # Point the client to the server.
213     setenv DEBUGINFOD_URLS $url
215     # GDB should now find the symbol and source files.
216     clean_restart
217     gdb_test_no_output "set debuginfod enabled on" \
218         "enabled debuginfod for initial test"
219     gdb_load $binfile
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]} {
234         return
235     }
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\\\]\\) "
243     clean_restart
244     gdb_test "core $::corefile" ".*return 0.*" "file [file tail $::corefile]" \
245         $enable_debuginfod_question "y"
247     # GDB should now find the debugaltlink file.
248     clean_restart
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
256     clean_restart
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
264     # disabled.
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 ""
277     clean_restart
278     # Disable confirmation to avoid having to deal with a query.  See
279     # test_urls.
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.*" {
283             fail $gdb_test_name
284         }
285         -re -wrap "" {
286             pass $gdb_test_name
287         }
288     }
290     # Whitespace-only URLS disables Debuginfod.
291     setenv DEBUGINFOD_URLS "    "
292     clean_restart
293     gdb_test_multiple $file_cmd "notice whitespace URL" {
294         -re -wrap "This GDB supports auto-downloading.*" {
295             fail $gdb_test_name
296         }
297         -re -wrap "" {
298             pass $gdb_test_name
299         }
300     }
302     test_urls $url \
303         "<$url>" \
304         "notice 1 URL"
306     test_urls "  $url  " \
307         "<$url>" \
308         "notice 1 URL with whitespace"
310     set url2 [regsub "^http://" $url ""]
312     test_urls "$url $url2" \
313         "<$url>\r\n +<$url2>" \
314         "notice 2 URLs"
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 {
325     no_url
326     local_url
329 stop_debuginfod