]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/testsuite/binutils-all/debuginfod.exp
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / binutils / testsuite / binutils-all / debuginfod.exp
CommitLineData
a2c58332 1# Copyright (C) 2002-2022 Free Software Foundation, Inc.
6a1939f5
NC
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, write to the Free Software
15# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
16
17# test debuginfod with readelf and objdump
18
19set test "debuginfod"
20
21if {[which debuginfod] == 0} {
22 unsupported "$test (not found)"
23 return
24}
25
26if {[which curl] == 0} {
27 unsupported "$test (curl not found)"
28 return
29}
30
0c3e266d 31if { ![is_elf_format] || ![isnative] } {
6a1939f5 32 unsupported "$test (unsupported target)"
0c3e266d 33 return
6a1939f5
NC
34}
35
36if { [which $OBJDUMP] == 0} {
37 perror "$test $OBJDUMP (does not exist)"
38 return
39}
40
41 if { [which $READELF] == 0} {
42 perror "$test $READELF (does not exist)"
43 return
44}
45
46# Compile testprog.c, move the debuginfo to a separate file and add .gnu_debuglink.
47if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog executable debug] != ""} {
1b8d1f5f 48 unsupported "$test (compilation failed)"
6a1939f5
NC
49 return
50}
51
52if { [binutils_run $OBJCOPY "--only-keep-debug tmpdir/testprog tmpdir/testprog.debug"] != "" } {
53 fail "$test (create separate debug info file)"
54 return
55}
56
57if { [binutils_run $OBJCOPY "--strip-debug tmpdir/testprog"] != "" } {
58 fail "$test (strip debug info)"
59 return
60}
61
62if { [binutils_run $OBJCOPY "--add-gnu-debuglink=tmpdir/testprog.debug tmpdir/testprog"] != "" } {
63 fail "$test (add debuglink)"
64 return
65}
66
67# Assemble an elf file with a debugaltlink
68if { ![binutils_assemble $srcdir/$subdir/debuglink.s tmpdir/debuglink.o] } {
69 fail "$test (assemble debuglink)"
70}
71
72if { ![binutils_assemble $srcdir/$subdir/linkdebug.s tmpdir/linkdebug.debug] } {
73 fail "$test (assemble linkdebug)"
74}
75
6a1939f5 76set cache [file join [pwd] "tmpdir/.debuginfod_cache"]
46471187 77set db [file join [pwd] "tmpdir/.debuginfod.db"]
6a1939f5 78
46471187 79setenv DEBUGINFOD_URLS ""
678d457f
AM
80setenv DEBUGINFOD_TIMEOUT 30
81setenv DEBUGINFOD_CACHE_PATH $cache
6a1939f5
NC
82
83# Move debug files into another directory so that readelf and objdump cannot
84# find them without debuginfod.
85file mkdir tmpdir/dbg
86file rename -force tmpdir/testprog.debug tmpdir/dbg
87file rename -force tmpdir/linkdebug.debug tmpdir/dbg
88
46471187 89# Remove old cache and database if they exist.
6a1939f5 90file delete -force $cache
46471187 91file delete -force $db
6a1939f5
NC
92
93# Check whether objdump and readelf are configured with debuginfod.
94# To check this we attempt to follow a broken debuglink. If configured
95# with debuginfod the output will contain the debuginfod URLs that were
96# queried (these queries fail since the server is not yet running).
1b8d1f5f
NC
97set conf_objdump [binutils_run $OBJDUMP "-Wk tmpdir/testprog"]
98set conf_readelf [binutils_run $READELF "-wk tmpdir/testprog"]
6a1939f5 99
b312fea1
AM
100# Find an unused port
101set port 7999
102set found 0
103while { ! $found } {
104 incr port
105 if { $port == 65536 } {
106 untested "$test (no available ports)"
107 return
108 }
6a1939f5 109
b312fea1
AM
110 spawn debuginfod -vvvv -d $db -p $port -F tmpdir/dbg
111 expect {
112 "started http server on IPv4 IPv6 port=$port" {
113 set found 1
6a1939f5 114 }
b312fea1
AM
115 "Failed to bind to port" {
116 exec kill -INT -[exp_pid]
117 catch {close}; catch {wait -i $spawn_id}
118 }
119 timeout {
120 fail "$test (find port timeout)"
121 return
122 }
123 }
6a1939f5
NC
124}
125
46471187
AM
126set metrics [list "ready 1" \
127 "thread_work_total{role=\"traverse\"} 1" \
128 "thread_work_pending{role=\"scan\"} 0" \
b312fea1 129 "thread_busy{role=\"scan\"} 0"]
46471187
AM
130
131# Check server metrics to confirm init has completed.
132foreach m $metrics {
133 set timelim 20
134 while { $timelim != 0 } {
135 sleep 0.5
136
6a1939f5
NC
137 catch {exec curl -s http://127.0.0.1:$port/metrics} got
138
46471187 139 if { [regexp $m $got] } {
6a1939f5
NC
140 break
141 }
6a1939f5 142
46471187
AM
143 incr timelim -1
144 }
145
146 if { $timelim == 0 } {
147 fail "$test (server init timeout)"
b312fea1
AM
148 exec kill -INT -[exp_pid]
149 catch {close}; catch {wait -i $spawn_id}
6a1939f5 150 return
46471187 151 }
6a1939f5
NC
152}
153
46471187
AM
154setenv DEBUGINFOD_URLS http://127.0.0.1:$port
155
6a1939f5
NC
156# Test whether prog can fetch separate debuginfo using debuginfod
157# if it's configured to do so.
158proc test_fetch_debuglink { prog progargs } {
159 global test
160 global cache
161
162 set got [binutils_run $prog "$progargs tmpdir/testprog"]
163
61ab1364 164 if { [regexp ".*Found separate debug info file.*Contents\[^\n\]*loaded from\[^\n\]*" $got] } {
6a1939f5
NC
165 pass "$test ($prog debuglink)"
166 } else {
61ab1364 167 fail "$test ($prog did not find debuglink to cache $cache)"
6a1939f5
NC
168 }
169}
170
171# Test whether prog can fetch debugaltlink files using debuginfod
172# if it's configured to do so.
173proc test_fetch_debugaltlink { prog progargs } {
174 global test
175 global cache
176
177 set got [binutils_run $prog "$progargs tmpdir/debuglink.o"]
178 set buildid "00112233445566778899aabbccddeeff0123456789abcdef"
179
180 if { [regexp ".*Found separate debug info file\[^\n\]*$cache/$buildid" $got] } {
181 pass "$test ($prog debugaltlink)"
182 } else {
61ab1364 183 fail "$test ($prog could not load debugaltlink)"
6a1939f5
NC
184 }
185}
186
187if { [regexp ".*DEBUGINFOD.*" $conf_objdump] } {
188 test_fetch_debuglink $OBJDUMP "-W"
1b8d1f5f 189 test_fetch_debugaltlink $OBJDUMP "-Wk"
6a1939f5
NC
190} else {
191 untested "$test (objdump not configured with debuginfod)"
192}
193
194if { [regexp ".*DEBUGINFOD.*" $conf_readelf] } {
195 test_fetch_debuglink $READELF "-w"
1b8d1f5f 196 test_fetch_debugaltlink $READELF "-wk"
6a1939f5
NC
197} else {
198 untested "$test (readelf not configured with debuginfod)"
199}