Home | History | Annotate | Line # | Download | only in gdb.base
      1 # Copyright 2017-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 
     17 # This test case verifies that GDB gracefully handles a shared library file
     18 # vanishing after being dlopen'ed.  This consists of three nested function calls:
     19 #
     20 # main() -> foo() -> bar()
     21 #
     22 # where:
     23 # - foo exists in solib-vanish-lib1.so, which is dlopen'ed by main()
     24 # - bar exists in solib-vanish-lib2.so, which is dynamically linked into
     25 #   solib-vanish-lib1.so
     26 #
     27 # Immediately after dlopen'ing solib-vanish-lib1.so, the so file is moved aside
     28 # by renaming.  The main executable and solib-vanish-lib2.so are still
     29 # accessible.
     30 #
     31 # If a breakpoint is set on bar(), gdb throws an error when this breakpoint is
     32 # hit:
     33 #
     34 #   (gdb) r
     35 #   Starting program: /local/gdb/git/pr_16577_repro/simple/solib-vanish-main
     36 #
     37 #   Breakpoint 1, BFD: reopening ./solib-vanish-lib1.so: No such file or directory
     38 #
     39 #   BFD: reopening ./solib-vanish-lib1.so: No such file or directory
     40 #
     41 #   (gdb)
     42 #
     43 # Notice that this does not print the current frame, i.e.:
     44 #   bar (y=1) at solib-vanish-lib2.c:19
     45 #   19	  return y + 1; /* break here */
     46 #   (gdb)
     47 #
     48 # The current gdb git tip segfaults if we then try to step:
     49 #   (gdb) n
     50 #   Segmentation fault
     51 
     52 # This test verifies that:
     53 # 1) GDB does not segfault when stepping
     54 # 2) The stack frame is printed
     55 
     56 require allow_shlib_tests
     57 
     58 # Library 2
     59 set lib2name "solib-vanish-lib2"
     60 set srcfile_lib2 ${srcdir}/${subdir}/${lib2name}.c
     61 set binfile_lib2 [standard_output_file ${lib2name}.so]
     62 set lib2_flags {debug}
     63 
     64 # Library 1
     65 set lib1name "solib-vanish-lib1"
     66 set srcfile_lib1 ${srcdir}/${subdir}/${lib1name}.c
     67 set binfile_lib1 [standard_output_file ${lib1name}.so]
     68 set lib1_flags [list debug shlib=${binfile_lib2}]
     69 
     70 if { [gdb_compile_shlib ${srcfile_lib2} ${binfile_lib2} $lib2_flags] != ""
     71      || [gdb_compile_shlib ${srcfile_lib1} ${binfile_lib1} $lib1_flags] != "" } {
     72     untested "failed to compile"
     73     return -1
     74 }
     75 
     76 # Main program
     77 set testfile "solib-vanish-main"
     78 set srcfile ${srcdir}/${subdir}/${testfile}.c
     79 set executable ${testfile}
     80 set binfile [standard_output_file ${executable}]
     81 set bin_flags [list debug shlib_load]
     82 set binfile_lib1_target [gdb_download_shlib $binfile_lib1]
     83 lappend bin_flags additional_flags=-DVANISH_LIB=\"$binfile_lib1_target\"
     84 
     85 if {[is_remote target]} {
     86     lappend cleanfiles_target $binfile_lib1_target.renamed
     87 }
     88 
     89 if { [gdb_compile ${srcfile} ${binfile} executable $bin_flags] != "" } {
     90     untested "failed to compile"
     91     return -1
     92 }
     93 
     94 clean_restart $testfile
     95 
     96 gdb_locate_shlib $binfile_lib1
     97 gdb_load_shlib $binfile_lib2
     98 
     99 if { ![runto_main] } {
    100     return
    101 }
    102 
    103 delete_breakpoints
    104 
    105 set lib2_lineno [gdb_get_line_number "break here" ${srcfile_lib2}]
    106 
    107 gdb_breakpoint "${lib2name}.c:${lib2_lineno}" {allow-pending}
    108 
    109 # Verify that both the location and source code are displayed
    110 gdb_continue_to_breakpoint "bar" \
    111     ".*${lib2name}.c:${lib2_lineno}.*break here.*"
    112 
    113 # This should not segfault
    114 gdb_test "next" \
    115     "" \
    116     "next succeeds"
    117 
    118