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