Commit | Line | Data |
---|---|---|
6a1939f5 NC |
1 | # Copyright (C) 2002-2019 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, 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 | ||
19 | set test "debuginfod" | |
20 | ||
21 | if {[which debuginfod] == 0} { | |
22 | unsupported "$test (not found)" | |
23 | return | |
24 | } | |
25 | ||
26 | if {[which curl] == 0} { | |
27 | unsupported "$test (curl not found)" | |
28 | return | |
29 | } | |
30 | ||
31 | if { ![is_elf_format] } { | |
32 | unsupported "$test (unsupported target)" | |
33 | } | |
34 | ||
35 | if { [which $OBJDUMP] == 0} { | |
36 | perror "$test $OBJDUMP (does not exist)" | |
37 | return | |
38 | } | |
39 | ||
40 | if { [which $READELF] == 0} { | |
41 | perror "$test $READELF (does not exist)" | |
42 | return | |
43 | } | |
44 | ||
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)" | |
48 | return | |
49 | } | |
50 | ||
51 | if { [binutils_run $OBJCOPY "--only-keep-debug tmpdir/testprog tmpdir/testprog.debug"] != "" } { | |
52 | fail "$test (create separate debug info file)" | |
53 | return | |
54 | } | |
55 | ||
56 | if { [binutils_run $OBJCOPY "--strip-debug tmpdir/testprog"] != "" } { | |
57 | fail "$test (strip debug info)" | |
58 | return | |
59 | } | |
60 | ||
61 | if { [binutils_run $OBJCOPY "--add-gnu-debuglink=tmpdir/testprog.debug tmpdir/testprog"] != "" } { | |
62 | fail "$test (add debuglink)" | |
63 | return | |
64 | } | |
65 | ||
66 | # Assemble an elf file with a debugaltlink | |
67 | if { ![binutils_assemble $srcdir/$subdir/debuglink.s tmpdir/debuglink.o] } { | |
68 | fail "$test (assemble debuglink)" | |
69 | } | |
70 | ||
71 | if { ![binutils_assemble $srcdir/$subdir/linkdebug.s tmpdir/linkdebug.debug] } { | |
72 | fail "$test (assemble linkdebug)" | |
73 | } | |
74 | ||
75 | # Find an unused port | |
76 | set port [exec sh -c "while true; do PORT=`expr '(' \$RANDOM % 1000 ')' + 9000`; ss -atn | fgrep \":\$PORT\" || break; done; echo \$PORT"] | |
77 | ||
78 | # Specify the directory that files retrieved from the server are written to. | |
79 | set cache [file join [pwd] "tmpdir/.debuginfod_cache"] | |
80 | ||
678d457f AM |
81 | setenv DEBUGINFOD_URLS http://127.0.0.1:$port |
82 | setenv DEBUGINFOD_TIMEOUT 30 | |
83 | setenv DEBUGINFOD_CACHE_PATH $cache | |
6a1939f5 NC |
84 | |
85 | # Move debug files into another directory so that readelf and objdump cannot | |
86 | # find them without debuginfod. | |
87 | file mkdir tmpdir/dbg | |
88 | file rename -force tmpdir/testprog.debug tmpdir/dbg | |
89 | file rename -force tmpdir/linkdebug.debug tmpdir/dbg | |
90 | ||
91 | # Remove an old cache if it exists | |
92 | file delete -force $cache | |
93 | ||
94 | # Check whether objdump and readelf are configured with debuginfod. | |
95 | # To check this we attempt to follow a broken debuglink. If configured | |
96 | # with debuginfod the output will contain the debuginfod URLs that were | |
97 | # queried (these queries fail since the server is not yet running). | |
98 | set conf_objdump [binutils_run $OBJDUMP "-WK tmpdir/testprog"] | |
99 | set conf_readelf [binutils_run $READELF "-wK tmpdir/testprog"] | |
100 | ||
101 | set debuginfod_pid 0 | |
102 | ||
103 | # Kill the server if we abort early | |
104 | proc sigint_handler {} { | |
105 | global debuginfod_pid | |
106 | ||
107 | if { $debuginfod_pid != 0 } { | |
108 | catch {exec kill -INT $debuginfod_pid} | |
109 | } | |
110 | ||
111 | exit | |
112 | } | |
113 | ||
114 | trap sigint_handler INT | |
115 | ||
116 | # Start a debuginfod server. | |
117 | set debuginfod_pid [exec debuginfod -p $port -F tmpdir/dbg 2>/dev/null &] | |
118 | ||
119 | if { !$debuginfod_pid } { | |
120 | fail "$test (server init)" | |
121 | return | |
122 | } | |
123 | ||
124 | # Wait for debuginfod indicate it's ready. | |
125 | set ready 0 | |
126 | for {set timelim 30} {$timelim != 0} {incr timelim -1} { | |
127 | sleep 1 | |
128 | set want ".*ready 1.*" | |
129 | catch {exec curl -s http://127.0.0.1:$port/metrics} got | |
130 | ||
131 | if { [regexp $want $got] } { | |
132 | set ready 1 | |
133 | break | |
134 | } | |
135 | } | |
136 | ||
137 | if { !$ready } { | |
138 | fail "$test (server ready)" | |
139 | catch {exec kill -INT $debuginfod_pid} | |
140 | return | |
141 | } | |
142 | ||
143 | # Test whether prog can fetch separate debuginfo using debuginfod | |
144 | # if it's configured to do so. | |
145 | proc test_fetch_debuglink { prog progargs } { | |
146 | global test | |
147 | global cache | |
148 | ||
149 | set got [binutils_run $prog "$progargs tmpdir/testprog"] | |
150 | ||
151 | if { [regexp ".*Found separate debug info file.*Contents\[^\n\]*loaded from\[^\n\]*$cache.*" $got] } { | |
152 | pass "$test ($prog debuglink)" | |
153 | } else { | |
154 | fail "$test ($prog debuglink)" | |
155 | } | |
156 | } | |
157 | ||
158 | # Test whether prog can fetch debugaltlink files using debuginfod | |
159 | # if it's configured to do so. | |
160 | proc test_fetch_debugaltlink { prog progargs } { | |
161 | global test | |
162 | global cache | |
163 | ||
164 | set got [binutils_run $prog "$progargs tmpdir/debuglink.o"] | |
165 | set buildid "00112233445566778899aabbccddeeff0123456789abcdef" | |
166 | ||
167 | if { [regexp ".*Found separate debug info file\[^\n\]*$cache/$buildid" $got] } { | |
168 | pass "$test ($prog debugaltlink)" | |
169 | } else { | |
170 | fail "$test ($prog debugaltlink)" | |
171 | } | |
172 | } | |
173 | ||
174 | if { [regexp ".*DEBUGINFOD.*" $conf_objdump] } { | |
175 | test_fetch_debuglink $OBJDUMP "-W" | |
176 | test_fetch_debugaltlink $OBJDUMP "-WK" | |
177 | } else { | |
178 | untested "$test (objdump not configured with debuginfod)" | |
179 | } | |
180 | ||
181 | if { [regexp ".*DEBUGINFOD.*" $conf_readelf] } { | |
182 | test_fetch_debuglink $READELF "-w" | |
183 | test_fetch_debugaltlink $READELF "-wK" | |
184 | } else { | |
185 | untested "$test (readelf not configured with debuginfod)" | |
186 | } | |
187 | ||
188 | file delete -force $cache | |
189 | catch {exec kill -INT $debuginfod_pid} |