Update copyright year range in header of all files managed by GDB
[binutils-gdb.git] / gdb / testsuite / gdb.base / solib-search.exp
blob8d2dacfb9474bee2cdb7f7a9987f979e7ec994bb
1 # Copyright 2013-2023 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 solib-search-path, and in the case of solib-svr4.c whether l_addr_p
17 # is properly reset when the path is changed.
19 if {[is_remote target] || [skip_shlib_tests]} {
20     untested "skipping remote target and shared library tests"
21     return -1
24 # Build "wrong" and "right" versions of the libraries in separate directories.
25 set wrong_lib_subdir "solib-search-wrong"
26 set right_lib_subdir "solib-search-right"
28 # First library file.
29 set libname1 "solib-search-lib1"
30 set srcfile1_lib ${srcdir}/${subdir}/${libname1}.c
31 set wrong_binfile1_lib \
32     [standard_output_file ${wrong_lib_subdir}/${libname1}.so]
33 set right_binfile1_lib \
34     [standard_output_file ${right_lib_subdir}/${libname1}.so]
35 # Second library file.
36 set libname2 "solib-search-lib2"
37 set srcfile2_lib ${srcdir}/${subdir}/${libname2}.c
38 set wrong_binfile2_lib \
39     [standard_output_file ${wrong_lib_subdir}/${libname2}.so]
40 set right_binfile2_lib \
41     [standard_output_file ${right_lib_subdir}/${libname2}.so]
42 # Link with the library that lives here.
43 # This is so that we can replace what gdb sees with the wrong copy,
44 # and then tell gdb to use the right copy that lives someplace else.
45 set binfile1_lib [standard_output_file ${libname1}.so]
46 set binfile2_lib [standard_output_file ${libname2}.so]
48 set lib_flags [list debug ldflags=-Wl,-Bsymbolic]
49 set wrong_lib_flags "$lib_flags additional_flags=-DARRAY_SIZE=1"
50 set right_lib_flags "$lib_flags additional_flags=-DARRAY_SIZE=8192 additional_flags=-DRIGHT"
52 # Binary file.
53 standard_testfile .c
54 set bin_flags [list debug shlib=${binfile1_lib} shlib=${binfile2_lib}]
56 remote_exec build "rm -rf [standard_output_file ${wrong_lib_subdir}]"
57 remote_exec build "rm -rf [standard_output_file ${right_lib_subdir}]"
58 remote_exec build "mkdir [standard_output_file ${wrong_lib_subdir}]"
59 remote_exec build "mkdir [standard_output_file ${right_lib_subdir}]"
61 if { [gdb_compile_shlib ${srcfile1_lib} ${wrong_binfile1_lib} $wrong_lib_flags] != ""
62      || [gdb_compile_shlib ${srcfile2_lib} ${wrong_binfile2_lib} $wrong_lib_flags] != ""
63      || [gdb_compile_shlib ${srcfile1_lib} ${right_binfile1_lib} $right_lib_flags] != ""
64      || [gdb_compile_shlib ${srcfile2_lib} ${right_binfile2_lib} $right_lib_flags] != "" } {
65     untested "failed to compile shared library"
66     return -1
69 # Build the test binary using the right copies of the libraries.
70 remote_exec build "ln -sf ${right_lib_subdir}/${libname1}.so ${binfile1_lib}"
71 remote_exec build "ln -sf ${right_lib_subdir}/${libname2}.so ${binfile2_lib}"
72 if { [gdb_compile $srcdir/$subdir/${srcfile} ${binfile} \
73           executable $bin_flags] != "" } {
74     untested "failed to compile"
75     return -1
78 clean_restart $testfile
80 if { ![runto_main] } {
81     return
84 gdb_breakpoint "break_here"
85 gdb_continue "break_here"
87 set corefile [standard_output_file solib-search.core]
88 set core_supported [gdb_gcore_cmd "$corefile" "save a corefile"]
90 if {!$core_supported} {
91   return -1
94 # Before we restart gdb, replace the libs with the "wrong" ones.
95 remote_exec build "ln -sf ${wrong_lib_subdir}/${libname1}.so ${binfile1_lib}"
96 remote_exec build "ln -sf ${wrong_lib_subdir}/${libname2}.so ${binfile2_lib}"
98 clean_restart $testfile
100 set core_loaded [gdb_core_cmd $corefile "re-load generated corefile"]
101 if { $core_loaded == -1 } {
102     # No use proceeding from here.
103     return
106 proc test_backtrace { expect_fail } {
107     global gdb_prompt
109     set count 0
110     set total_expected 5
112     if { $expect_fail } {
113         set testname "backtrace (with wrong libs)"
114     } else {
115         set testname "backtrace (with right libs)"
116     }
117     # N.B. The order of the tests here is important.
118     # We need to count each function in the backtrace, and expect matches
119     # the first one it finds.
120     gdb_test_multiple "backtrace" $testname {
121         -re "\[^\r\n\]* in lib2_func4 \[^\r\n\]*" {
122             incr count
123             exp_continue
124         }
125         -re "\[^\r\n\]* in lib1_func3 \[^\r\n\]*" {
126             incr count
127             exp_continue
128         }
129         -re "\[^\r\n\]* in lib2_func2 \[^\r\n\]*" {
130             incr count
131             exp_continue
132         }
133         -re "\[^\r\n\]* in lib1_func1 \[^\r\n\]*" {
134             incr count
135             exp_continue
136         }
137         -re "\[^\r\n\]* in main \[^\r\n\]*" {
138             incr count
139             exp_continue
140         }
141         -re "\[\r\n\]$gdb_prompt $" {
142             pass "$testname (data collection)"
143         }
144     }
146     set fail 0
147     if { $expect_fail } {
148         # If the backtrace output is correct the test isn't sufficiently
149         # testing what it should.
150         if { $count == $total_expected } {
151             set fail 1
152         }
153     } else {
154         if { $count != $total_expected } {
155             set fail 1
156         }
157     }
158     if { $fail } {
159         fail $testname
160     } else {
161         pass $testname
162     }
165 # Verify the backtrace is messed up.
166 test_backtrace 1
168 # Remove the copies gdb currently sees: the absolute path is encoded in
169 # the core file and we want gdb to find the right copies elsewhere.
170 remote_exec build "rm -f ${binfile1_lib}"
171 remote_exec build "rm -f ${binfile2_lib}"
172 # Set solib-search-path to use the correct copies of libraries.
173 gdb_test "set solib-search-path [standard_output_file ${right_lib_subdir}]" \
174     "" \
175     "set solib-search-path"
177 # Verify gdb has properly updated the location of the libraries.
178 test_backtrace 0
179 gdb_test "p lib1_size" " = 8192"
180 gdb_test "p lib2_size" " = 8192"