scope.exp revision 1.1.1.7 1 # Copyright 1992-2023 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 # This file was written by Fred Fish. (fnf (at) cygnus.com)
17
18
19 standard_testfile scope0.c scope1.c
20
21 if {[build_executable "failed to build" ${testfile} \
22 [list $srcfile $srcfile2] {debug}]} {
23 return -1
24 }
25
26 # Test locating various things when stopped just inside main, after
27 # running init0(). To prevent cascading of errors, we report the
28 # first one and quit. If all pass, then we print the pass results.
29
30 proc_with_prefix test_at_main {} {
31 global gdb_prompt
32 global decimal
33 global det_file
34 global srcdir
35 global subdir
36
37 # skip past init0.
38 # This used to do an extra "next" if the first one didn't get us
39 # over the call to init0, to handle calls to __main in the
40 # prologue, etc. But if a breakpoint at main doesn't leave us on
41 # the first line of real code in the function, that's a GDB bug.
42 gdb_test "next" "$decimal.*foo \\(\\);" "next over init0() in main"
43
44 # Print scope0.c::filelocal, which is 1
45 gdb_test "print filelocal" "\\\$$decimal = 1"
46 gdb_test "print 'scope0.c'::filelocal" "\\\$$decimal = 1" "print 'scope0.c'::filelocal at main"
47
48 # Print scope0.c::filelocal_bss, which is 101
49 gdb_test "print filelocal_bss" "\\\$$decimal = 101"
50 gdb_test "print 'scope0.c'::filelocal_bss" "\\\$$decimal = 101" "print 'scope0.c'::filelocal_bss in test_at_main"
51
52 # Print scope0.c::filelocal_ro, which is 201
53
54 # No clue why the rs6000 fails this test.
55 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
56 gdb_test "print filelocal_ro" "\\\$$decimal = 201" "print filelocal_ro in test_at_main"
57
58 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
59 gdb_test "print 'scope0.c'::filelocal_ro" "\\\$$decimal = 201" "print 'scope0.c'::filelocal_ro"
60
61 # Print scope1.c::filelocal, which is 2
62 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
63 gdb_test "print 'scope1.c'::filelocal" "\\\$$decimal = 2" "print 'scope1.c'::filelocal"
64
65 # Print scope1.c::filelocal_bss, which is 102
66 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
67 gdb_test "print 'scope1.c'::filelocal_bss" "\\\$$decimal = 102" "print 'scope1.c'::filelocal_bss"
68
69 # Print scope1.c::filelocal_ro, which is 202
70 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
71 gdb_test "print 'scope1.c'::filelocal_ro" "\\\$$decimal = 202" "print 'scope1.c'::filelocal_ro"
72
73 # Print scope1.c::foo::funclocal, which is 3
74 gdb_test "print foo::funclocal" "\\\$$decimal = 3"
75
76 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
77 gdb_test "print 'scope1.c'::foo::funclocal" "\\\$$decimal = 3" "print 'scope1.c'::foo::funclocal"
78
79 # Print scope1.c::foo::funclocal_ro, which is 203
80 gdb_test "print foo::funclocal_ro" "\\\$$decimal = 203"
81
82 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
83 gdb_test "print 'scope1.c'::foo::funclocal_ro" "\\\$$decimal = 203" "print 'scope1.c'::foo::funclocal_ro"
84
85 # Print scope1.c::bar::funclocal, which is 4
86 gdb_test "print bar::funclocal" "\\\$$decimal = 4"
87
88 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
89 gdb_test "print 'scope1.c'::bar::funclocal" "\\\$$decimal = 4" "print 'scope1.c'::bar::funclocal"
90 }
91
92 proc_with_prefix test_at_foo {} {
93 global gdb_prompt
94 global decimal
95 global det_file
96 global srcdir
97 global subdir
98
99 gdb_test "next" ".*bar \\(\\);"
100
101 # Print scope0.c::filelocal, which is 1
102 gdb_test "print 'scope0.c'::filelocal" "\\\$$decimal = 1" "print 'scope0.c'::filelocal at foo"
103
104 # Print scope0.c::filelocal_bss, which is 101
105 gdb_test "print 'scope0.c'::filelocal_bss" "\\\$$decimal = 101" "print 'scope0.c'::filelocal_bss in test_at_foo"
106
107 # Print scope0.c::filelocal_ro, which is 201
108 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
109 gdb_test "print 'scope0.c'::filelocal_ro" "\\\$$decimal = 201" "print 'scope0.c'::filelocal_ro"
110
111
112 # Print scope1.c::filelocal, which is 2
113 gdb_test "print filelocal" "\\\$$decimal = 2" "print filelocal at foo"
114
115 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
116 gdb_test "print 'scope1.c'::filelocal" "\\\$$decimal = 2" "print 'scope1.c'::filelocal at foo"
117
118 gdb_test "print filelocal_bss" "\\\$$decimal = 102" \
119 "print filelocal_bss at foo"
120
121 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
122 gdb_test "print 'scope1.c'::filelocal_bss" "\\\$$decimal = 102" "print 'scope1.c'::filelocal_bss at foo"
123
124
125 gdb_test "print filelocal_ro" "\\\$$decimal = 202" \
126 "print filelocal_ro at foo"
127
128 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
129 gdb_test "print 'scope1.c'::filelocal_ro" "\\\$$decimal = 202" "print 'scope1.c'::filelocal_ro at foo"
130
131
132 # Print scope1.c::foo::funclocal, which is 3
133
134 gdb_test "print funclocal" "\\\$$decimal = 3" "print funclocal at foo"
135
136 gdb_test "print foo::funclocal" "\\\$$decimal = 3" \
137 "print foo::funclocal at foo"
138
139 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
140 gdb_test "print 'scope1.c'::foo::funclocal" "\\\$$decimal = 3" "print 'scope1.c'::foo::funclocal at foo"
141
142
143 # Print scope1.c::foo::funclocal_bss, which is 103
144
145 gdb_test "print funclocal_bss" "\\\$$decimal = 103" \
146 "print funclocal_bss at foo"
147
148 gdb_test "print foo::funclocal_bss" "\\\$$decimal = 103" \
149 "print foo::funclocal_bss at foo"
150
151 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
152 gdb_test "print 'scope1.c'::foo::funclocal_bss" "\\\$$decimal = 103" "print 'scope1.c'::foo::funclocal_bss at foo"
153
154
155 # Print scope1.c::foo::funclocal_ro, which is 203
156
157 gdb_test "print funclocal_ro" "\\\$$decimal = 203" \
158 "print funclocal_ro at foo"
159
160 gdb_test "print foo::funclocal_ro" "\\\$$decimal = 203" \
161 "print foo::funclocal_ro at foo"
162
163 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
164 gdb_test "print 'scope1.c'::foo::funclocal_ro" "\\\$$decimal = 203" "print 'scope1.c'::foo::funclocal_ro at foo"
165
166
167 # Print scope1.c::bar::funclocal, which is 4
168
169 gdb_test "print bar::funclocal" "\\\$$decimal = 4" \
170 "print bar::funclocal at foo"
171
172 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
173 gdb_test "print 'scope1.c'::bar::funclocal" "\\\$$decimal = 4" "print 'scope1.c'::bar::funclocal at foo"
174
175 }
176
177 proc_with_prefix test_at_bar {} {
178 global gdb_prompt
179 global decimal
180 global det_file
181 global srcdir
182 global subdir
183
184 gdb_test "next"
185
186 # Print scope0.c::filelocal, which is 1
187 gdb_test "print 'scope0.c'::filelocal" "\\\$$decimal = 1" "print 'scope0.c'::filelocal at bar"
188
189 # Print scope0.c::filelocal_bss, which is 101
190 gdb_test "print 'scope0.c'::filelocal_bss" "\\\$$decimal = 101" "print 'scope0.c'::filelocal_bss in test_at_bar"
191
192 # Print scope0.c::filelocal_ro, which is 201
193 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
194 gdb_test "print 'scope0.c'::filelocal_ro" "\\\$$decimal = 201" "print 'scope0.c'::filelocal_ro at bar"
195
196 # Print scope1.c::filelocal, which is 2
197 gdb_test "print filelocal" "\\\$$decimal = 2" "print filelocal at bar"
198
199 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
200 gdb_test "print 'scope1.c'::filelocal" "\\\$$decimal = 2" "print 'scope1.c'::filelocal at bar"
201
202 # Print scope1.c::filelocal_bss, which is 102
203 gdb_test "print filelocal_bss" "\\\$$decimal = 102" "print filelocal_bss at bar"
204
205 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
206 gdb_test "print 'scope1.c'::filelocal_bss" "\\\$$decimal = 102" "print 'scope1.c'::filelocal_bss at bar"
207
208 # Print scope1.c::filelocal_ro, which is 202
209 gdb_test "print filelocal_ro" "\\\$$decimal = 202" "print filelocal_ro in test_at_bar"
210
211 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
212 gdb_test "print 'scope1.c'::filelocal_ro" "\\\$$decimal = 202" "print 'scope1.c'::filelocal_ro at bar"
213
214 # Print scope1.c::foo::funclocal, which is 3
215 gdb_test "print foo::funclocal" "\\\$$decimal = 3" "print foo::funclocal at bar"
216
217 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
218 gdb_test "print 'scope1.c'::foo::funclocal" "\\\$$decimal = 3" "print 'scope1.c'::foo::funclocal at bar"
219
220 # Print scope1.c::foo::funclocal_bss, which is 103
221 gdb_test "print foo::funclocal_bss" "\\\$$decimal = 103" "print foo::funclocal_bss at bar"
222
223 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
224 gdb_test "print 'scope1.c'::foo::funclocal_bss" "\\\$$decimal = 103" "print 'scope1.c'::foo::funclocal_bss at bar"
225
226 # Print scope1.c::foo::funclocal_ro, which is 203
227 gdb_test "print foo::funclocal_ro" "\\\$$decimal = 203" "print foo::funclocal_ro at bar"
228
229 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
230 gdb_test "print 'scope1.c'::foo::funclocal_ro" "\\\$$decimal = 203" "print 'scope1.c'::foo::funclocal_ro at bar"
231
232 # Print scope1.c::bar::funclocal, which is 4
233 gdb_test "print funclocal" "\\\$$decimal = 4" "print funclocal at bar"
234 gdb_test "print bar::funclocal" "\\\$$decimal = 4" "print bar::funclocal at bar"
235
236 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
237 gdb_test "print 'scope1.c'::bar::funclocal" "\\\$$decimal = 4" "print 'scope1.c'::bar::funclocal at bar"
238
239 # Print scope1.c::bar::funclocal_bss, which is 104
240 gdb_test "print funclocal_bss" "\\\$$decimal = 104" "print funclocal_bss at bar"
241 gdb_test "print bar::funclocal_bss" "\\\$$decimal = 104" "print bar::funclocal_bss at bar"
242
243 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
244 gdb_test "print 'scope1.c'::bar::funclocal_bss" "\\\$$decimal = 104" "print 'scope1.c'::bar::funclocal_bss at bar"
245 }
246
247 # This test has little to do with local scopes, but it is in scope.exp anyway.
248 # That's life.
249
250 proc_with_prefix test_at_autovars {} {
251 global gdb_prompt
252 global decimal
253 global hex
254 global srcfile
255
256 # Test symbol table lookup with 100 local (auto) variables.
257
258 gdb_breakpoint marker1
259
260 gdb_test "cont" "Break.* marker1 \\(\\) at .*:$decimal.*" "continue to marker1"
261 gdb_test "up" ".*autovars.*" "up from marker1"
262
263 set count 0
264 while {$count < 100} {
265 gdb_test "print i$count" ".* = $count" ""
266 set count [expr $count+1]
267 }
268 clear_xfail "*-*-*"
269 pass "$count auto variables correctly initialized"
270
271 # Test that block variable sorting is not screwing us.
272 gdb_test "frame" "#.*autovars \\(bcd=5, abc=6\\).*" "args in correct order"
273 }
274
275 proc_with_prefix test_at_localscopes {} {
276 global gdb_prompt
277 global decimal
278 global hex
279 global srcfile
280
281 gdb_breakpoint marker2
282 gdb_breakpoint marker3
283 gdb_breakpoint marker4
284
285 gdb_test "cont" "Break.* marker2 \\(\\) at .*:$decimal.*" "continue to marker2"
286 gdb_test "up" ".*localscopes.*" "up from marker2"
287
288 # Should be at first (outermost) scope. Check values.
289
290 gdb_test "print localval" " = 10" "print localval, outer scope"
291 gdb_test "print localval1" " = 11" "print localval1, outer scope"
292 gdb_test "print localval2" "No symbol \"localval2\" in current context." \
293 "print localval2, outer scope"
294 gdb_test "print localval3" "No symbol \"localval3\" in current context." \
295 "print localval3, outer scope"
296
297 gdb_test "cont" "Break.* marker3 \\(\\) at .*:$decimal.*" \
298 "continue to marker3"
299 gdb_test "up" ".*localscopes.*" "up from marker3"
300
301 # Should be at next (first nested) scope. Check values.
302
303 gdb_test "print localval" " = 20" \
304 "print localval, first nested scope"
305 gdb_test "print localval1" " = 11" "print localval1, first nested scope"
306 gdb_test "print localval2" " = 12" "print localval2, first nested scope"
307 gdb_test "print localval3" "No symbol \"localval3\" in current context." \
308 "print localval3, first nested scope"
309
310 # This test will only fail if the file was compiled by gcc, but
311 # there's no way to check that.
312 gdb_test "cont" "Break.* marker4.*at .*:$decimal.*" \
313 "continue to marker4"
314 gdb_test "up" ".*localscopes.*" "up from marker4"
315
316 gdb_test "print localval" " = 30" "print localval, innermost scope"
317 gdb_test "print localval1" " = 11" "print localval1, innermost scope"
318 gdb_test "print localval2" " = 12" "print localval2, innermost scope"
319 gdb_test "print localval3" " = 13" "print localval3, innermost scope"
320 }
321
322 # Note, we don't pass a binfile to clean_restart because we need to
323 # avoid gdb_load here. With remote targets, gdb_load connects to the
324 # remote target, and we specificaly want the following tests to run
325 # before the program is started.
326 clean_restart
327 gdb_file_cmd $binfile
328
329 # Test that variables in various segments print out correctly before
330 # the program is run.
331
332 # AIX--sections get mapped to the same address so we can't get the right one.
333 setup_xfail "rs6000-*-*"
334
335 gdb_test "print 'scope0.c'::filelocal_ro" "= 201"
336
337 # Check that gdb can access bss memory if the inferior is not running.
338 #
339 # Note the care above about avoiding gdb_load. Otherwise, for
340 # embedded stub-like boards, this test would randomly fail. If we'd
341 # already put target remote on the target stack, we'd read memory from
342 # the board instead of the program's binary, and we would have
343 # connected before crt0.o had had a chance to clear bss.
344 gdb_test "print 'scope0.c'::filelocal_bss" "= 0" \
345 "print 'scope0.c'::filelocal_bss before run"
346
347 gdb_test "print 'scope0.c'::filelocal" "= 1" \
348 "print 'scope0.c'::filelocal before run"
349
350 if {[runto_main]} { test_at_main }
351 if {[runto foo]} { test_at_foo }
352 if {[runto bar]} { test_at_bar }
353 if {[runto localscopes]} { test_at_localscopes }
354 if {[runto autovars]} { test_at_autovars }
355