1 # Copyright 2019 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 # 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.
20 # Build-id-related tests for core files.
24 # Build a non-shared executable.
26 proc build_corefile_buildid_exec {} {
27 global testfile srcfile binfile execdir
29 if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
30 untested "failed to compile"
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]]"
43 # Build a shared executable.
45 proc build_corefile_buildid_shared {} {
46 global srcdir subdir testfile binfile srcfile sharedir
48 set builddir [standard_output_file $sharedir]
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"
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"
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"
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"
90 # Append DEBUGDIR to the debug-file-directory path.
92 proc append_debug_dir {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"
103 gdb_test_no_output "set debug-file-directory $debugdir:$orig_debugdir" \
104 "append debug directory"
107 # A convenience procedure to check if "info files" mentions the exec file
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]'.*"
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.
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.
125 proc locate_exec_from_core_build_id {corefile buildid suffix \
126 sepdebug symlink shared} {
127 global testfile binfile srcfile
131 # Set up the build-id directory and symlink the binary there.
137 set debugdir [standard_output_file $d-$suffix]
138 remote_exec build "rm -rf $debugdir"
140 "mkdir -p [file join $debugdir [file dirname $buildid]]"
144 lappend files_list "$binfile.stripped" $buildid
145 lappend files_list "$binfile.debug" "$buildid.debug"
147 lappend files_list $binfile $buildid
151 set builddir [standard_output_file $sharedir]
154 set builddir [standard_output_file $execdir]
156 foreach {target name} $files_list {
157 set t [file join $builddir [file tail $target]]
159 remote_exec build "ln -s $t [file join $debugdir $name]"
161 remote_exec build "cp $t [file join $debugdir $name]"
165 # Append the debugdir to the separate debug directory search path.
166 append_debug_dir $debugdir
168 gdb_test "core-file $corefile" "Program terminated with .*" \
172 set expected_file [file join $builddir \
173 [file tail "$binfile.stripped"]]
175 set expected_file [file join $builddir [file tail $binfile]]
178 set expected_file $buildid
180 check_exec_file [file join $debugdir $expected_file]
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.
187 proc do_corefile_buildid_tests {args} {
188 global binfile testfile srcfile execdir sharedir
191 parse_args [list {sepdebug} {shared}]
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
197 set builddir [standard_output_file $sharedir]
199 set builddir [standard_output_file $execdir]
201 set program_to_run [file join $builddir [file tail $binfile]]
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.
209 lappend suffix "shared"
211 lappend suffix "exec"
215 # Strip debuginfo into its own file.
216 if {[gdb_gnu_strip_debug [standard_output_file $program_to_run]] \
218 untested "could not strip executable for [join $suffix \ ]"
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"
228 # Find the core file.
229 set corefile [core_find $program_to_run]
230 if {$corefile == ""} {
231 untested "could not generate core file"
234 verbose -log "corefile is $corefile"
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"
241 regsub {\.debug$} $buildid {} buildid
242 verbose -log "build-id is $buildid"
244 with_test_prefix "[join $suffix \ ]" {
245 locate_exec_from_core_build_id $corefile $buildid \
246 [join $suffix -] $sepdebug false $shared
249 with_test_prefix "symlink [join $suffix \ ]" {
250 locate_exec_from_core_build_id $corefile $buildid \
251 [join $suffix -] $sepdebug true $shared
255 # Directories where executables will be moved before testing.
256 set execdir "build-exec"
257 set sharedir "build-shared"
263 build_corefile_buildid_exec
264 do_corefile_buildid_tests
265 do_corefile_buildid_tests -sepdebug
267 if {![skip_shlib_tests]} {
268 build_corefile_buildid_shared
269 do_corefile_buildid_tests -shared
270 do_corefile_buildid_tests -shared -sepdebug