Home | History | Annotate | Line # | Download | only in gdb.base
      1  1.1  christos # Copyright 2024 Free Software Foundation, Inc.
      2  1.1  christos #
      3  1.1  christos # This program is free software; you can redistribute it and/or modify
      4  1.1  christos # it under the terms of the GNU General Public License as published by
      5  1.1  christos # the Free Software Foundation; either version 3 of the License, or
      6  1.1  christos # (at your option) any later version.
      7  1.1  christos #
      8  1.1  christos # This program is distributed in the hope that it will be useful,
      9  1.1  christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  1.1  christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  1.1  christos # GNU General Public License for more details.
     12  1.1  christos #
     13  1.1  christos # You should have received a copy of the GNU General Public License
     14  1.1  christos # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15  1.1  christos 
     16  1.1  christos # Test GDB's ability to find debug information by looking within the
     17  1.1  christos # sysroot.
     18  1.1  christos #
     19  1.1  christos # We compile a static binary (to reduce what we need to copy into the
     20  1.1  christos # sysroot), split the debug information from the binary, and setup a
     21  1.1  christos # sysroot.
     22  1.1  christos #
     23  1.1  christos # The debug-file-directory is set to just '/debug', but we're
     24  1.1  christos # expecting GDB to actually look in '$SYSROOT/debug'.
     25  1.1  christos #
     26  1.1  christos # There's a test for using .build-id based lookup, and a test for
     27  1.1  christos # gnu_debuglink based lookup.
     28  1.1  christos 
     29  1.1  christos require {!is_remote host}
     30  1.1  christos 
     31  1.1  christos standard_testfile main.c
     32  1.1  christos 
     33  1.1  christos # Create a copy of BINFILE, split out the debug information, and then
     34  1.1  christos # setup a sysroot.  Hide (by moving) the actual debug information file
     35  1.1  christos # and create a symlink to the hidden debug information from within the
     36  1.1  christos # sysroot.
     37  1.1  christos #
     38  1.1  christos # Start GDB, set the sysroot, and then load the executable, ensure GDB
     39  1.1  christos # finds the debug information, which must have happened by lookin in
     40  1.1  christos # the sysroot.
     41  1.1  christos proc_with_prefix lookup_via_build_id {} {
     42  1.1  christos     set filename ${::binfile}_1
     43  1.1  christos     if { [build_executable "build exec" ${filename} $::srcfile \
     44  1.1  christos 	      {additional_flags=-static debug build-id}] } {
     45  1.1  christos 	return
     46  1.1  christos     }
     47  1.1  christos 
     48  1.1  christos     # Split debug information into a .debug file, remove debug
     49  1.1  christos     # information from FILENAME.  Don't add a .gnu_debuglink to
     50  1.1  christos     # FILENAME, we rely on the build-id.
     51  1.1  christos     if {[gdb_gnu_strip_debug $filename { no-debuglink }] != 0} {
     52  1.1  christos 	unsupported "cannot split debug information from executable"
     53  1.1  christos 	return
     54  1.1  christos     }
     55  1.1  christos 
     56  1.1  christos     set sysroot [standard_output_file "sysroot1"]
     57  1.1  christos     set debug_dir "/debug"
     58  1.1  christos 
     59  1.1  christos     set debug_symlink \
     60  1.1  christos 	${sysroot}${debug_dir}/[build_id_debug_filename_get $filename]
     61  1.1  christos 
     62  1.1  christos     set build_id_dir [file dirname $debug_symlink]
     63  1.1  christos 
     64  1.1  christos     set debug_filename ${filename}_hidden_debug
     65  1.1  christos 
     66  1.1  christos     remote_exec build "mkdir -p $build_id_dir"
     67  1.1  christos     remote_exec build "mv $filename.debug $debug_filename"
     68  1.1  christos     remote_exec build "ln -sf $debug_filename $debug_symlink"
     69  1.1  christos 
     70  1.1  christos     foreach_with_prefix sysroot_prefix { "" "target:" } {
     71  1.1  christos 	clean_restart
     72  1.1  christos 
     73  1.1  christos 	gdb_test_no_output "set sysroot ${sysroot_prefix}$sysroot" "set sysroot"
     74  1.1  christos 	gdb_test_no_output "set debug-file-directory $debug_dir"
     75  1.1  christos 
     76  1.1  christos 	gdb_file_cmd $filename
     77  1.1  christos 
     78  1.1  christos 	gdb_assert { $::gdb_file_cmd_debug_info eq "debug" } \
     79  1.1  christos 	    "ensure debug information was found"
     80  1.1  christos 
     81  1.1  christos 	if { $sysroot_prefix eq "target:"
     82  1.1  christos 	     && [target_info gdb_protocol] == "extended-remote"} {
     83  1.1  christos 	    # Only when using the extended-remote board will we have
     84  1.1  christos 	    # started a remote target by this point.  In this case GDB
     85  1.1  christos 	    # will see the 'target:' prefix as remote, and so the
     86  1.1  christos 	    # reported filename will include the 'target:' prefix.
     87  1.1  christos 	    #
     88  1.1  christos 	    # In all other cases we will still be using the default,
     89  1.1  christos 	    # initial target, in which case GDB considers the
     90  1.1  christos 	    # 'target:' prefix to indicate the local filesystem.
     91  1.1  christos 	    set lookup_filename $sysroot_prefix$debug_symlink
     92  1.1  christos 	} else {
     93  1.1  christos 	    set lookup_filename $debug_filename
     94  1.1  christos 	}
     95  1.1  christos 	set re [string_to_regexp "Reading symbols from $lookup_filename..."]
     96  1.1  christos 	gdb_assert {[regexp $re $::gdb_file_cmd_msg]} \
     97  1.1  christos 	    "debug symbols read from correct file"
     98  1.1  christos     }
     99  1.1  christos }
    100  1.1  christos 
    101  1.1  christos # Create a copy of BINFILE, split out the debug information, and then
    102  1.1  christos # setup a sysroot.  Hide (by moving) the actual debug information file
    103  1.1  christos # and create a symlink to the hidden debug information from within the
    104  1.1  christos # sysroot.
    105  1.1  christos #
    106  1.1  christos # Copy the executable into the sysroot and then start GDB, set the
    107  1.1  christos # sysroot, and load the executable.  Check that GDB finds the debug
    108  1.1  christos # information, which must have happened by lookin in the sysroot.
    109  1.1  christos proc_with_prefix lookup_via_debuglink {} {
    110  1.1  christos     set filename ${::binfile}_2
    111  1.1  christos     if { [build_executable "build exec" ${filename} $::srcfile \
    112  1.1  christos 	      {additional_flags=-static debug no-build-id}] } {
    113  1.1  christos 	return
    114  1.1  christos     }
    115  1.1  christos 
    116  1.1  christos     # Split debug information into a .debug file, remove debug
    117  1.1  christos     # information from FILENAME.
    118  1.1  christos     if {[gdb_gnu_strip_debug $filename] != 0} {
    119  1.1  christos 	unsupported "cannot split debug information from executable"
    120  1.1  christos 	return
    121  1.1  christos     }
    122  1.1  christos 
    123  1.1  christos     # We're going to setup the sysroot like this:
    124  1.1  christos     #
    125  1.1  christos     # sysroot2/
    126  1.1  christos     #    bin/
    127  1.1  christos     #      $FILENAME
    128  1.1  christos     #    debug/
    129  1.1  christos     #      bin/
    130  1.1  christos     #        $FILENAME.debug
    131  1.1  christos     #
    132  1.1  christos     # When looking up debug information via the debuglink, GDB will
    133  1.1  christos     # only search in the sysroot if the original objfile was in the
    134  1.1  christos     # sysroot.  And GDB will resolve symlinks, so if the objfile is
    135  1.1  christos     # symlinked to outside the sysroot, GDB will not search in the
    136  1.1  christos     # sysroot for the debug information.
    137  1.1  christos     #
    138  1.1  christos     # So we have to copy the executable into the sysroot.
    139  1.1  christos     #
    140  1.1  christos     # We are OK to symlink the debug information to a file outside the
    141  1.1  christos     # sysroot though.
    142  1.1  christos 
    143  1.1  christos     set sysroot [standard_output_file "sysroot2"]
    144  1.1  christos 
    145  1.1  christos     foreach path { bin debug/bin } {
    146  1.1  christos 	remote_exec build "mkdir -p $sysroot/$path"
    147  1.1  christos     }
    148  1.1  christos 
    149  1.1  christos     # Copy the executable into the sysroot.
    150  1.1  christos     set file_basename [file tail $filename]
    151  1.1  christos     set exec_in_sysroot ${sysroot}/bin/${file_basename}
    152  1.1  christos     remote_exec build "cp $filename $exec_in_sysroot"
    153  1.1  christos 
    154  1.1  christos     # Rename the debug file outside of the sysroot, this should stop
    155  1.1  christos     # GDB finding this file "by accident".
    156  1.1  christos     set debug_filename ${filename}_hidden_debug
    157  1.1  christos     remote_exec build "mv $filename.debug $debug_filename"
    158  1.1  christos 
    159  1.1  christos     # Symlink the debug information into the sysroot.
    160  1.1  christos     set debug_symlink \
    161  1.1  christos 	${sysroot}/debug/bin/${file_basename}.debug
    162  1.1  christos     remote_exec build "ln -sf $debug_filename $debug_symlink"
    163  1.1  christos 
    164  1.1  christos     foreach_with_prefix sysroot_prefix { "" "target:" } {
    165  1.1  christos 	# Restart GDB and setup the sysroot and debug directory.
    166  1.1  christos 	clean_restart
    167  1.1  christos 	gdb_test_no_output "set sysroot ${sysroot_prefix}$sysroot" "set sysroot"
    168  1.1  christos 	gdb_test_no_output "set debug-file-directory /debug"
    169  1.1  christos 
    170  1.1  christos 	# Load the executable, we expect GDB to find the debug information
    171  1.1  christos 	# in the sysroot.
    172  1.1  christos 	gdb_file_cmd ${sysroot_prefix}$exec_in_sysroot
    173  1.1  christos 
    174  1.1  christos 	gdb_assert { $::gdb_file_cmd_debug_info eq "debug" } \
    175  1.1  christos 	    "ensure debug information was found"
    176  1.1  christos 
    177  1.1  christos 	set re [string_to_regexp "Reading symbols from ${sysroot_prefix}$debug_symlink..."]
    178  1.1  christos 	gdb_assert {[regexp $re $::gdb_file_cmd_msg]} \
    179  1.1  christos 	    "debug symbols read from correct file"
    180  1.1  christos     }
    181  1.1  christos }
    182  1.1  christos 
    183  1.1  christos lookup_via_build_id
    184  1.1  christos lookup_via_debuglink
    185