Core file build-id support
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / corefile-buildid.exp
1 # Copyright 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, see <http://www.gnu.org/licenses/>.
15
16 # Based on break.exp, written by Rob Savoye. (rob@cygnus.com)
17 # Modified to test gdb's handling of separate debug info files.
18 # Modified to test gdb's handling of a debug-id retrieval.
19
20 # Build-id-related tests for core files.
21
22 standard_testfile
23
24 # Build a non-shared executable.
25
26 proc build_corefile_buildid_exec {} {
27 global testfile srcfile binfile execdir
28
29 if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
30 untested "failed to compile"
31 return false
32 }
33
34 # Move executable to non-default path.
35 set builddir [standard_output_file $execdir]
36 remote_exec build "rm -rf $builddir"
37 remote_exec build "mkdir $builddir"
38 remote_exec build "mv $binfile [file join $builddir [file tail $binfile]]"
39
40 return true
41 }
42
43 # Build a shared executable.
44
45 proc build_corefile_buildid_shared {} {
46 global srcdir subdir testfile binfile srcfile sharedir
47
48 set builddir [standard_output_file $sharedir]
49
50 # Compile DSO.
51 set srcdso [file join $srcdir $subdir $testfile-shlib-shr.c]
52 set objdso [standard_output_file $testfile-shlib-shr.so]
53 if {[gdb_compile_shlib $srcdso $objdso {debug}] != ""} {
54 untested "failed to compile dso"
55 return false
56 }
57
58 # Compile shared library.
59 set srclib [file join $srcdir $subdir $testfile-shlib.c]
60 set libname lib$testfile.so
61 set objlib [standard_output_file $libname]
62 set dlopen_lib [shlib_target_file \
63 [file join $builddir [file tail $objdso]]]
64 set opts [list debug shlib_load \
65 additional_flags=-DSHLIB_NAME=\"$dlopen_lib\"]
66 if {[gdb_compile_shlib $srclib $objlib $opts] != ""} {
67 untested "failed to compile shared library"
68 return false
69 }
70
71 # Compile main program.
72 set srcexec [file join $srcdir $subdir $srcfile]
73 set binfile [standard_output_file $testfile-shared]
74 set opts [list debug shlib=$objlib additional_flags=-DTEST_SHARED]
75 if {[gdb_compile $srcexec $binfile executable $opts] != ""} {
76 untested "failed to compile shared executable"
77 return false
78 }
79
80 # Move objects to non-default path.
81 remote_exec build "rm -rf $builddir"
82 remote_exec build "mkdir $builddir"
83 remote_exec build "mv $binfile $builddir"
84 remote_exec build "mv $objdso $builddir"
85 remote_exec build "mv $objlib $builddir"
86
87 return true
88 }
89
90 # Append DEBUGDIR to the debug-file-directory path.
91
92 proc append_debug_dir {debugdir} {
93 global gdb_prompt
94
95 set orig_debugdir {}
96 gdb_test_multiple "show debug-file-directory" \
97 "get debug-file-directory" {
98 -re "The directory where separate debug symbols are searched for is \"(.*)\"\.\[\r\n\]+$gdb_prompt $" {
99 set orig_debugdir $expect_out(1,string)
100 pass "get debug-file-directory"
101 }
102 }
103 gdb_test_no_output "set debug-file-directory $debugdir:$orig_debugdir" \
104 "append debug directory"
105 }
106
107 # A convenience procedure to check if "info files" mentions the exec file
108 # FILE.
109
110 proc check_exec_file {file} {
111 send_log "expecting exec file \"$file\"\n"
112 gdb_test "info files" "Local exec file:\[\r\n\t\ \]+`[string_to_regexp $file]'.*"
113 }
114
115 # Test whether gdb can find an exec file from a core file's build-id.
116 # The executable (and separate debuginfo if SEPDEBUG is true) is
117 # copied to the .build-id directory.
118 #
119 # SUFFIX is appended to the .builid-id parent directory name to
120 # keep all tests separate.
121 # SYMLINK specifies whether build-id files should be copied or symlinked.
122 # SHARED is a boolean indicating whether we are testing the shared
123 # library core dump test case.
124
125 proc locate_exec_from_core_build_id {corefile buildid suffix \
126 sepdebug symlink shared} {
127 global testfile binfile srcfile
128
129 clean_restart
130
131 # Set up the build-id directory and symlink the binary there.
132 if {$symlink} {
133 set d "symlinkdir"
134 } else {
135 set d "debugdir"
136 }
137 set debugdir [standard_output_file $d-$suffix]
138 remote_exec build "rm -rf $debugdir"
139 remote_exec build \
140 "mkdir -p [file join $debugdir [file dirname $buildid]]"
141
142 set files_list {}
143 if {$sepdebug} {
144 lappend files_list "$binfile.stripped" $buildid
145 lappend files_list "$binfile.debug" "$buildid.debug"
146 } else {
147 lappend files_list $binfile $buildid
148 }
149 if {$shared} {
150 global sharedir
151 set builddir [standard_output_file $sharedir]
152 } else {
153 global execdir
154 set builddir [standard_output_file $execdir]
155 }
156 foreach {target name} $files_list {
157 set t [file join $builddir [file tail $target]]
158 if {$symlink} {
159 remote_exec build "ln -s $t [file join $debugdir $name]"
160 } else {
161 remote_exec build "cp $t [file join $debugdir $name]"
162 }
163 }
164
165 # Append the debugdir to the separate debug directory search path.
166 append_debug_dir $debugdir
167
168 gdb_test "core-file $corefile" "Program terminated with .*" \
169 "load core file"
170 if {$symlink} {
171 if {$sepdebug} {
172 set expected_file [file join $builddir \
173 [file tail "$binfile.stripped"]]
174 } else {
175 set expected_file [file join $builddir [file tail $binfile]]
176 }
177 } else {
178 set expected_file $buildid
179 }
180 check_exec_file [file join $debugdir $expected_file]
181 }
182
183 # Run a build-id tests on a core file.
184 # Supported options: "-shared" and "-sepdebug" for running tests
185 # of shared and/or stripped/.debug executables.
186
187 proc do_corefile_buildid_tests {args} {
188 global binfile testfile srcfile execdir sharedir
189
190 # Parse options.
191 parse_args [list {sepdebug} {shared}]
192
193 # PROGRAM to run to generate core file. This could be different
194 # than the program that was originally built, e.g., for a stripped
195 # executable.
196 if {$shared} {
197 set builddir [standard_output_file $sharedir]
198 } else {
199 set builddir [standard_output_file $execdir]
200 }
201 set program_to_run [file join $builddir [file tail $binfile]]
202
203 # A list of suffixes to use to describe the test and the .build-id
204 # directory for the test. The suffix will be used, joined with spaces,
205 # to prefix all tests for the given run. It will be used, joined with
206 # dashes, to create a unique build-id directory.
207 set suffix {}
208 if {$shared} {
209 lappend suffix "shared"
210 } else {
211 lappend suffix "exec"
212 }
213
214 if {$sepdebug} {
215 # Strip debuginfo into its own file.
216 if {[gdb_gnu_strip_debug [standard_output_file $program_to_run]] \
217 != 0} {
218 untested "could not strip executable for [join $suffix \ ]"
219 return
220 }
221
222 # Run the stripped program instead of the original.
223 set program_to_run [file join $builddir \
224 [file tail "$binfile.stripped"]]
225 lappend suffix "sepdebug"
226 }
227
228 # Find the core file.
229 set corefile [core_find $program_to_run]
230 if {$corefile == ""} {
231 untested "could not generate core file"
232 return
233 }
234 verbose -log "corefile is $corefile"
235
236 # Grab the build-id from the binary, removing ".debug" from the end.
237 set buildid [build_id_debug_filename_get $program_to_run]
238 if {$buildid == ""} {
239 untested "binary for [join $suffix \ ] has no build-id"
240 }
241 regsub {\.debug$} $buildid {} buildid
242 verbose -log "build-id is $buildid"
243
244 with_test_prefix "[join $suffix \ ]" {
245 locate_exec_from_core_build_id $corefile $buildid \
246 [join $suffix -] $sepdebug false $shared
247 }
248
249 with_test_prefix "symlink [join $suffix \ ]" {
250 locate_exec_from_core_build_id $corefile $buildid \
251 [join $suffix -] $sepdebug true $shared
252 }
253 }
254
255 # Directories where executables will be moved before testing.
256 set execdir "build-exec"
257 set sharedir "build-shared"
258
259 #
260 # Do tests
261 #
262
263 build_corefile_buildid_exec
264 do_corefile_buildid_tests
265 do_corefile_buildid_tests -sepdebug
266
267 if {![skip_shlib_tests]} {
268 build_corefile_buildid_shared
269 do_corefile_buildid_tests -shared
270 do_corefile_buildid_tests -shared -sepdebug
271 }
This page took 0.036536 seconds and 4 git commands to generate.