Home | History | Annotate | Line # | Download | only in gdb.base
      1 # Copyright 2013-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 adding and removing a symbol file dynamically:
     17 # 1)  Run to gdb_add_symbol_file in $srcfile.
     18 # 2)  Set a pending breakpoint at bar in $srcfile3.
     19 # 3)  Load the library's symbols using 'add-symbol-file'.
     20 # 4)  'info files' must display ${lib_basename}.
     21 # 5)  Continue to bar in $srcfile3.
     22 # 6)  Set a breakpoint at foo in $srcfile3.
     23 # 7)  Continue to foo in $srcfile3.
     24 # 8)  Set a breakpoint at gdb_remove_symbol_file.
     25 # 9)  Continue to gdb_remove_symbol_file in $srcfile.
     26 # 10) Remove the library's symbols using 'remove-symbol-file'.
     27 # 11) 'info files' must not display ${lib_basename}, anymore.
     28 # 12) Check that the breakpoints at foo and bar are pending.
     29 # 13) Check that the execution can continue without error.
     30 # 14) Regression test for a stale breakpoints bug.
     31 
     32 require is_elf_target
     33 
     34 require allow_shlib_tests
     35 
     36 set target_size TARGET_UNKNOWN
     37 if {[is_lp64_target]} {
     38     set target_size TARGET_LP64
     39 } elseif {[is_ilp32_target]} {
     40    set target_size TARGET_ILP32
     41 } else {
     42     return 0
     43 }
     44 
     45 set main_basename sym-file-main
     46 set loader_basename sym-file-loader
     47 set lib_basename sym-file-lib
     48 
     49 standard_testfile $main_basename.c $loader_basename.c $lib_basename.c
     50 
     51 set libsrc "${srcdir}/${subdir}/${srcfile3}"
     52 set lib_so [standard_output_file ${lib_basename}.so]
     53 set lib_syms [shlib_symbol_file ${lib_so}]
     54 set lib_dlopen [shlib_target_file ${lib_basename}.so]
     55 
     56 set exec_opts [list debug "additional_flags= -I$srcdir/../../include/ -D$target_size\
     57  -DSHLIB_NAME\\=\"$lib_dlopen\""]
     58 
     59 if {[gdb_compile_shlib $libsrc $lib_so {debug}] != ""} {
     60     untested "failed to compile shared library"
     61     return
     62 }
     63 
     64 if {[prepare_for_testing "failed to prepare"  $binfile "$srcfile $srcfile2" $exec_opts]} {
     65     return
     66 }
     67 
     68 gdb_load_shlib ${lib_so}
     69 
     70 proc do_test { remove_expr } {
     71     global lib_basename lib_syms srcfile srcfile3
     72 
     73     clean_restart $::binfile
     74 
     75     if {![runto_main]} {
     76 	return
     77     }
     78 
     79     # 1) Run to gdb_add_symbol_file in $srcfile for adding the library's
     80     #    symbols.
     81     gdb_breakpoint gdb_add_symbol_file
     82     gdb_continue_to_breakpoint gdb_add_symbol_file
     83 
     84     # 2) Set a pending breakpoint at bar in $srcfile3.
     85     set result [gdb_breakpoint bar allow-pending]
     86     if {!$result} {
     87 	return
     88     }
     89 
     90     # 3) Add the library's symbols using 'add-symbol-file'.
     91     set result [gdb_test "add-symbol-file ${lib_syms} addr" \
     92 		    "Reading symbols from .*${lib_syms}\\.\\.\\." \
     93 		    "add-symbol-file ${lib_basename}.so addr" \
     94 		    "add symbol table from file \".*${lib_basename}\\.so\"\
     95  at.*\\(y or n\\) " \
     96 		    "y"]
     97     if {$result != 0} {
     98 	return
     99     }
    100 
    101     # 4) 'info files' must display $srcfile3.
    102     gdb_test "info files" \
    103 	"^(?=(.*${lib_basename})).*" \
    104 	"info files must display ${lib_basename}"
    105 
    106     # 5) Continue to bar in $srcfile3 to ensure that the breakpoint
    107     #    was bound correctly after adding $shilb_name.
    108     set lnum_bar [gdb_get_line_number "break at bar" $srcfile3]
    109     gdb_continue_to_breakpoint bar ".*${lib_basename}\\.c:$lnum_bar.*"
    110 
    111     # 6) Set a breakpoint at foo in $srcfile3.
    112     set result [gdb_breakpoint foo]
    113     if {!$result} {
    114 	return
    115     }
    116 
    117     # 7) Continue to foo in $srcfile3 to ensure that the breakpoint
    118     #    was bound correctly.
    119     set lnum_foo [gdb_get_line_number "break at foo" $srcfile3]
    120     gdb_continue_to_breakpoint foo ".*${lib_basename}\\.c:$lnum_foo.*"
    121 
    122     # 8) Set a breakpoint at gdb_remove_symbol_file in $srcfile for
    123     #    removing the library's symbols.
    124     set result [gdb_breakpoint gdb_remove_symbol_file]
    125     if {!$result} {
    126 	return
    127     }
    128 
    129     # 9) Continue to gdb_remove_symbol_file in $srcfile.
    130     gdb_continue_to_breakpoint gdb_remove_symbol_file
    131 
    132     # 10) Remove the library's symbols using 'remove-symbol-file'.
    133     set result [gdb_test "remove-symbol-file ${remove_expr}" \
    134 		    ""\
    135 		    "remove-symbol-file" \
    136 		    "Remove symbol table from file \".*${lib_basename}\\.so\"\\?\
    137 .*\\(y or n\\) " \
    138 		    "y"]
    139     if {$result != 0} {
    140 	return
    141     }
    142 
    143     # 11) 'info files' must not display ${lib_basename}, anymore.
    144     gdb_test "info files" \
    145 	"^(?!(.*${lib_basename})).*" \
    146 	"info files must not display ${lib_basename}"
    147 
    148     # 12) Check that the breakpoints at foo and bar are pending after
    149     #     removing the library's symbols.
    150     gdb_test "info breakpoints 3" \
    151 	".*PENDING.*" \
    152 	"breakpoint at foo is pending"
    153 
    154     gdb_test "info breakpoints 4" \
    155 	".*PENDING.*" \
    156 	"breakpoint at bar is pending"
    157 
    158     # 13) Check that the execution can continue without error.
    159     set lnum_reload [gdb_get_line_number "reload lib here"]
    160     gdb_breakpoint $lnum_reload
    161     gdb_continue_to_breakpoint reload ".*${srcfile}:$lnum_reload.*"
    162 
    163     # 14) Regression test for a stale breakpoints bug.  Check whether
    164     # unloading symbols manually without the program actually unloading
    165     # the library, when breakpoints are inserted doesn't leave stale
    166     # breakpoints behind.
    167     with_test_prefix "stale bkpts" {
    168 	# Force breakpoints always inserted.
    169 	gdb_test_no_output "set breakpoint always-inserted on"
    170 
    171 	# Get past the library reload.
    172 	gdb_continue_to_breakpoint gdb_add_symbol_file
    173 
    174 	# Load the library's symbols.
    175 	gdb_test "add-symbol-file ${lib_syms} addr" \
    176 	    "Reading symbols from .*${lib_syms}\\.\\.\\." \
    177 	    "add-symbol-file ${lib_basename}.so addr" \
    178 	    "add symbol table from file \".*${lib_syms}\"\
    179 at.*\\(y or n\\) " \
    180 	    "y"
    181 
    182 	# Set a breakpoint at baz, in the library.
    183 	gdb_breakpoint baz
    184 
    185 	gdb_test "info breakpoints 7" ".*y.*0x.*in baz.*" \
    186 	    "breakpoint at baz is resolved"
    187 
    188 	# Unload symbols manually without the program actually unloading
    189 	# the library.
    190 	gdb_test "remove-symbol-file ${remove_expr}" \
    191 	    "" \
    192 	    "remove-symbol-file" \
    193 	    "Remove symbol table from file \".*${lib_basename}\\.so\"\\?\
    194 .*\\(y or n\\) " \
    195 	    "y"
    196 
    197 	gdb_test "info breakpoints 7" ".*PENDING.*" \
    198 	    "breakpoint at baz is pending"
    199 
    200 	# Check that execution can continue without error.  If GDB leaves
    201 	# breakpoints behind, we'll get back a spurious SIGTRAP.
    202 	set lnum_end [gdb_get_line_number "end here"]
    203 	gdb_breakpoint $lnum_end
    204 	gdb_continue_to_breakpoint "end here" ".*end here.*"
    205     }
    206 }
    207 
    208 foreach remove_expr [list addr bar "bar + 0x10" "${lib_syms}" ] {
    209     # Don't use full filenames in the test prefix.  Also, add '-a' to
    210     # all the REMOVE_EXPR values which are addresses rather than
    211     # filenames.
    212     set prefix $remove_expr
    213     if { $prefix == $lib_syms } {
    214 	set prefix [file tail $prefix]
    215     } else {
    216 	set remove_expr "-a $remove_expr"
    217     }
    218 
    219     with_test_prefix "remove_expr=$prefix" {
    220 	do_test $remove_expr
    221     }
    222 }
    223