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