1 # Copyright (C) 2002-2020 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, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
17 # test debuginfod with readelf and objdump
21 if {[which debuginfod] == 0} {
22 unsupported "$test (not found)"
26 if {[which curl] == 0} {
27 unsupported "$test (curl not found)"
31 if { ![is_elf_format] } {
32 unsupported "$test (unsupported target)"
35 if { [which $OBJDUMP] == 0} {
36 perror "$test $OBJDUMP (does not exist)"
40 if { [which $READELF] == 0} {
41 perror "$test $READELF (does not exist)"
45 # Compile testprog.c, move the debuginfo to a separate file and add .gnu_debuglink.
46 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog executable debug] != ""} {
47 fail "$test (compilation failed)"
51 if { [binutils_run $OBJCOPY "--only-keep-debug tmpdir/testprog tmpdir/testprog.debug"] != "" } {
52 fail "$test (create separate debug info file)"
56 if { [binutils_run $OBJCOPY "--strip-debug tmpdir/testprog"] != "" } {
57 fail "$test (strip debug info)"
61 if { [binutils_run $OBJCOPY "--add-gnu-debuglink=tmpdir/testprog.debug tmpdir/testprog"] != "" } {
62 fail "$test (add debuglink)"
66 # Assemble an elf file with a debugaltlink
67 if { ![binutils_assemble $srcdir/$subdir/debuglink.s tmpdir/debuglink.o] } {
68 fail "$test (assemble debuglink)"
71 if { ![binutils_assemble $srcdir/$subdir/linkdebug.s tmpdir/linkdebug.debug] } {
72 fail "$test (assemble linkdebug)"
75 set cache [file join [pwd] "tmpdir/.debuginfod_cache"]
76 set db [file join [pwd] "tmpdir/.debuginfod.db"]
78 setenv DEBUGINFOD_URLS ""
79 setenv DEBUGINFOD_TIMEOUT 30
80 setenv DEBUGINFOD_CACHE_PATH $cache
82 # Move debug files into another directory so that readelf and objdump cannot
83 # find them without debuginfod.
85 file rename -force tmpdir/testprog.debug tmpdir/dbg
86 file rename -force tmpdir/linkdebug.debug tmpdir/dbg
88 # Remove old cache and database if they exist.
89 file delete -force $cache
90 file delete -force $db
92 # Check whether objdump and readelf are configured with debuginfod.
93 # To check this we attempt to follow a broken debuglink. If configured
94 # with debuginfod the output will contain the debuginfod URLs that were
95 # queried (these queries fail since the server is not yet running).
96 set conf_objdump [binutils_run $OBJDUMP "-WK tmpdir/testprog"]
97 set conf_readelf [binutils_run $READELF "-wK tmpdir/testprog"]
104 if { $port == 65536 } {
105 untested "$test (no available ports)"
109 spawn debuginfod -vvvv -d $db -p $port -F tmpdir/dbg
111 "started http server on IPv4 IPv6 port=$port" {
114 "Failed to bind to port" {
115 exec kill -INT -[exp_pid]
116 catch {close}; catch {wait -i $spawn_id}
119 fail "$test (find port timeout)"
125 set metrics [list "ready 1" \
126 "thread_work_total{role=\"traverse\"} 1" \
127 "thread_work_pending{role=\"scan\"} 0" \
128 "thread_busy{role=\"scan\"} 0"]
130 # Check server metrics to confirm init has completed.
133 while { $timelim != 0 } {
136 catch {exec curl -s http://127.0.0.1:$port/metrics} got
138 if { [regexp $m $got] } {
145 if { $timelim == 0 } {
146 fail "$test (server init timeout)"
147 exec kill -INT -[exp_pid]
148 catch {close}; catch {wait -i $spawn_id}
153 setenv DEBUGINFOD_URLS http://127.0.0.1:$port
155 # Test whether prog can fetch separate debuginfo using debuginfod
156 # if it's configured to do so.
157 proc test_fetch_debuglink { prog progargs } {
161 set got [binutils_run $prog "$progargs tmpdir/testprog"]
163 if { [regexp ".*Found separate debug info file.*Contents\[^\n\]*loaded from\[^\n\]*$cache.*" $got] } {
164 pass "$test ($prog debuglink)"
166 fail "$test ($prog debuglink)"
170 # Test whether prog can fetch debugaltlink files using debuginfod
171 # if it's configured to do so.
172 proc test_fetch_debugaltlink { prog progargs } {
176 set got [binutils_run $prog "$progargs tmpdir/debuglink.o"]
177 set buildid "00112233445566778899aabbccddeeff0123456789abcdef"
179 if { [regexp ".*Found separate debug info file\[^\n\]*$cache/$buildid" $got] } {
180 pass "$test ($prog debugaltlink)"
182 fail "$test ($prog debugaltlink)"
186 if { [regexp ".*DEBUGINFOD.*" $conf_objdump] } {
187 test_fetch_debuglink $OBJDUMP "-W"
188 test_fetch_debugaltlink $OBJDUMP "-WK"
190 untested "$test (objdump not configured with debuginfod)"
193 if { [regexp ".*DEBUGINFOD.*" $conf_readelf] } {
194 test_fetch_debuglink $READELF "-w"
195 test_fetch_debugaltlink $READELF "-wK"
197 untested "$test (readelf not configured with debuginfod)"