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