1 # Copyright 2024 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 # Test GDB's ability to find debug information by looking within the
19 # We compile a static binary (to reduce what we need to copy into the
20 # sysroot), split the debug information from the binary, and setup a
23 # The debug-file-directory is set to just '/debug', but we're
24 # expecting GDB to actually look in '$SYSROOT/debug'.
26 # There's a test for using .build-id based lookup, and a test for
27 # gnu_debuglink based lookup.
29 require {!is_remote host}
31 standard_testfile main.c
33 # Create a copy of BINFILE, split out the debug information, and then
34 # setup a sysroot. Hide (by moving) the actual debug information file
35 # and create a symlink to the hidden debug information from within the
38 # Start GDB, set the sysroot, and then load the executable, ensure GDB
39 # finds the debug information, which must have happened by lookin in
41 proc_with_prefix lookup_via_build_id {} {
42 set filename ${::binfile}_1
43 if { [build_executable "build exec" ${filename} $::srcfile \
44 {additional_flags=-static debug build-id}] } {
48 # Split debug information into a .debug file, remove debug
49 # information from FILENAME. Don't add a .gnu_debuglink to
50 # FILENAME, we rely on the build-id.
51 if {[gdb_gnu_strip_debug $filename { no-debuglink }] != 0} {
52 unsupported "cannot split debug information from executable"
56 set sysroot [standard_output_file "sysroot1"]
57 set debug_dir "/debug"
60 ${sysroot}${debug_dir}/[build_id_debug_filename_get $filename]
62 set build_id_dir [file dirname $debug_symlink]
64 set debug_filename ${filename}_hidden_debug
66 remote_exec build "mkdir -p $build_id_dir"
67 remote_exec build "mv $filename.debug $debug_filename"
68 remote_exec build "ln -sf $debug_filename $debug_symlink"
70 foreach_with_prefix sysroot_prefix { "" "target:" } {
73 gdb_test_no_output "set sysroot ${sysroot_prefix}$sysroot" "set sysroot"
74 gdb_test_no_output "set debug-file-directory $debug_dir"
76 gdb_file_cmd $filename
78 gdb_assert { $::gdb_file_cmd_debug_info eq "debug" } \
79 "ensure debug information was found"
81 if { $sysroot_prefix eq "target:"
82 && [target_info gdb_protocol] == "extended-remote"} {
83 # Only when using the extended-remote board will we have
84 # started a remote target by this point. In this case GDB
85 # will see the 'target:' prefix as remote, and so the
86 # reported filename will include the 'target:' prefix.
88 # In all other cases we will still be using the default,
89 # initial target, in which case GDB considers the
90 # 'target:' prefix to indicate the local filesystem.
91 set lookup_filename $sysroot_prefix$debug_symlink
93 set lookup_filename $debug_filename
95 set re [string_to_regexp "Reading symbols from $lookup_filename..."]
96 gdb_assert {[regexp $re $::gdb_file_cmd_msg]} \
97 "debug symbols read from correct file"
101 # Create a copy of BINFILE, split out the debug information, and then
102 # setup a sysroot. Hide (by moving) the actual debug information file
103 # and create a symlink to the hidden debug information from within the
106 # Copy the executable into the sysroot and then start GDB, set the
107 # sysroot, and load the executable. Check that GDB finds the debug
108 # information, which must have happened by lookin in the sysroot.
109 proc_with_prefix lookup_via_debuglink {} {
110 set filename ${::binfile}_2
111 if { [build_executable "build exec" ${filename} $::srcfile \
112 {additional_flags=-static debug no-build-id}] } {
116 # Split debug information into a .debug file, remove debug
117 # information from FILENAME.
118 if {[gdb_gnu_strip_debug $filename] != 0} {
119 unsupported "cannot split debug information from executable"
123 # We're going to setup the sysroot like this:
132 # When looking up debug information via the debuglink, GDB will
133 # only search in the sysroot if the original objfile was in the
134 # sysroot. And GDB will resolve symlinks, so if the objfile is
135 # symlinked to outside the sysroot, GDB will not search in the
136 # sysroot for the debug information.
138 # So we have to copy the executable into the sysroot.
140 # We are OK to symlink the debug information to a file outside the
143 set sysroot [standard_output_file "sysroot2"]
145 foreach path { bin debug/bin } {
146 remote_exec build "mkdir -p $sysroot/$path"
149 # Copy the executable into the sysroot.
150 set file_basename [file tail $filename]
151 set exec_in_sysroot ${sysroot}/bin/${file_basename}
152 remote_exec build "cp $filename $exec_in_sysroot"
154 # Rename the debug file outside of the sysroot, this should stop
155 # GDB finding this file "by accident".
156 set debug_filename ${filename}_hidden_debug
157 remote_exec build "mv $filename.debug $debug_filename"
159 # Symlink the debug information into the sysroot.
161 ${sysroot}/debug/bin/${file_basename}.debug
162 remote_exec build "ln -sf $debug_filename $debug_symlink"
164 foreach_with_prefix sysroot_prefix { "" "target:" } {
165 # Restart GDB and setup the sysroot and debug directory.
167 gdb_test_no_output "set sysroot ${sysroot_prefix}$sysroot" "set sysroot"
168 gdb_test_no_output "set debug-file-directory /debug"
170 # Load the executable, we expect GDB to find the debug information
172 gdb_file_cmd ${sysroot_prefix}$exec_in_sysroot
174 gdb_assert { $::gdb_file_cmd_debug_info eq "debug" } \
175 "ensure debug information was found"
177 set re [string_to_regexp "Reading symbols from ${sysroot_prefix}$debug_symlink..."]
178 gdb_assert {[regexp $re $::gdb_file_cmd_msg]} \
179 "debug symbols read from correct file"