sysroot-debug-lookup.exp revision 1.1 1 # Copyright 2024 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 # Test GDB's ability to find debug information by looking within the
17 # sysroot.
18 #
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
21 # sysroot.
22 #
23 # The debug-file-directory is set to just '/debug', but we're
24 # expecting GDB to actually look in '$SYSROOT/debug'.
25 #
26 # There's a test for using .build-id based lookup, and a test for
27 # gnu_debuglink based lookup.
28
29 require {!is_remote host}
30
31 standard_testfile main.c
32
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
36 # sysroot.
37 #
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
40 # the sysroot.
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}] } {
45 return
46 }
47
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"
53 return
54 }
55
56 set sysroot [standard_output_file "sysroot1"]
57 set debug_dir "/debug"
58
59 set debug_symlink \
60 ${sysroot}${debug_dir}/[build_id_debug_filename_get $filename]
61
62 set build_id_dir [file dirname $debug_symlink]
63
64 set debug_filename ${filename}_hidden_debug
65
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"
69
70 foreach_with_prefix sysroot_prefix { "" "target:" } {
71 clean_restart
72
73 gdb_test_no_output "set sysroot ${sysroot_prefix}$sysroot" "set sysroot"
74 gdb_test_no_output "set debug-file-directory $debug_dir"
75
76 gdb_file_cmd $filename
77
78 gdb_assert { $::gdb_file_cmd_debug_info eq "debug" } \
79 "ensure debug information was found"
80
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.
87 #
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
92 } else {
93 set lookup_filename $debug_filename
94 }
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"
98 }
99 }
100
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
104 # sysroot.
105 #
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}] } {
113 return
114 }
115
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"
120 return
121 }
122
123 # We're going to setup the sysroot like this:
124 #
125 # sysroot2/
126 # bin/
127 # $FILENAME
128 # debug/
129 # bin/
130 # $FILENAME.debug
131 #
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.
137 #
138 # So we have to copy the executable into the sysroot.
139 #
140 # We are OK to symlink the debug information to a file outside the
141 # sysroot though.
142
143 set sysroot [standard_output_file "sysroot2"]
144
145 foreach path { bin debug/bin } {
146 remote_exec build "mkdir -p $sysroot/$path"
147 }
148
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"
153
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"
158
159 # Symlink the debug information into the sysroot.
160 set debug_symlink \
161 ${sysroot}/debug/bin/${file_basename}.debug
162 remote_exec build "ln -sf $debug_filename $debug_symlink"
163
164 foreach_with_prefix sysroot_prefix { "" "target:" } {
165 # Restart GDB and setup the sysroot and debug directory.
166 clean_restart
167 gdb_test_no_output "set sysroot ${sysroot_prefix}$sysroot" "set sysroot"
168 gdb_test_no_output "set debug-file-directory /debug"
169
170 # Load the executable, we expect GDB to find the debug information
171 # in the sysroot.
172 gdb_file_cmd ${sysroot_prefix}$exec_in_sysroot
173
174 gdb_assert { $::gdb_file_cmd_debug_info eq "debug" } \
175 "ensure debug information was found"
176
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"
180 }
181 }
182
183 lookup_via_build_id
184 lookup_via_debuglink
185