1 # Copyright 1992-2024 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 # This file was written by Fred Fish. (fnf@cygnus.com)
19 standard_testfile scope0.c scope1.c
21 if {[build_executable "failed to build" ${testfile} \
22 [list $srcfile $srcfile2] {debug}]} {
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.
30 proc_with_prefix test_at_main {} {
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"
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"
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"
52 # Print scope0.c::filelocal_ro, which is 201
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"
58 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
59 gdb_test "print 'scope0.c'::filelocal_ro" "\\\$$decimal = 201" "print 'scope0.c'::filelocal_ro"
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"
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"
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"
73 # Print scope1.c::foo::funclocal, which is 3
74 gdb_test "print foo::funclocal" "\\\$$decimal = 3"
76 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
77 gdb_test "print 'scope1.c'::foo::funclocal" "\\\$$decimal = 3" "print 'scope1.c'::foo::funclocal"
79 # Print scope1.c::foo::funclocal_ro, which is 203
80 gdb_test "print foo::funclocal_ro" "\\\$$decimal = 203"
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"
85 # Print scope1.c::bar::funclocal, which is 4
86 gdb_test "print bar::funclocal" "\\\$$decimal = 4"
88 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
89 gdb_test "print 'scope1.c'::bar::funclocal" "\\\$$decimal = 4" "print 'scope1.c'::bar::funclocal"
92 proc_with_prefix test_at_foo {} {
99 gdb_test "next" ".*bar \\(\\);"
101 # Print scope0.c::filelocal, which is 1
102 gdb_test "print 'scope0.c'::filelocal" "\\\$$decimal = 1" "print 'scope0.c'::filelocal at foo"
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"
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"
112 # Print scope1.c::filelocal, which is 2
113 gdb_test "print filelocal" "\\\$$decimal = 2" "print filelocal at foo"
115 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
116 gdb_test "print 'scope1.c'::filelocal" "\\\$$decimal = 2" "print 'scope1.c'::filelocal at foo"
118 gdb_test "print filelocal_bss" "\\\$$decimal = 102" \
119 "print filelocal_bss at foo"
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"
125 gdb_test "print filelocal_ro" "\\\$$decimal = 202" \
126 "print filelocal_ro at foo"
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"
132 # Print scope1.c::foo::funclocal, which is 3
134 gdb_test "print funclocal" "\\\$$decimal = 3" "print funclocal at foo"
136 gdb_test "print foo::funclocal" "\\\$$decimal = 3" \
137 "print foo::funclocal at foo"
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"
143 # Print scope1.c::foo::funclocal_bss, which is 103
145 gdb_test "print funclocal_bss" "\\\$$decimal = 103" \
146 "print funclocal_bss at foo"
148 gdb_test "print foo::funclocal_bss" "\\\$$decimal = 103" \
149 "print foo::funclocal_bss at foo"
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"
155 # Print scope1.c::foo::funclocal_ro, which is 203
157 gdb_test "print funclocal_ro" "\\\$$decimal = 203" \
158 "print funclocal_ro at foo"
160 gdb_test "print foo::funclocal_ro" "\\\$$decimal = 203" \
161 "print foo::funclocal_ro at foo"
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"
167 # Print scope1.c::bar::funclocal, which is 4
169 gdb_test "print bar::funclocal" "\\\$$decimal = 4" \
170 "print bar::funclocal at foo"
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"
177 proc_with_prefix test_at_bar {} {
186 # Print scope0.c::filelocal, which is 1
187 gdb_test "print 'scope0.c'::filelocal" "\\\$$decimal = 1" "print 'scope0.c'::filelocal at bar"
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"
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"
196 # Print scope1.c::filelocal, which is 2
197 gdb_test "print filelocal" "\\\$$decimal = 2" "print filelocal at bar"
199 if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
200 gdb_test "print 'scope1.c'::filelocal" "\\\$$decimal = 2" "print 'scope1.c'::filelocal at bar"
202 # Print scope1.c::filelocal_bss, which is 102
203 gdb_test "print filelocal_bss" "\\\$$decimal = 102" "print filelocal_bss at bar"
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"
208 # Print scope1.c::filelocal_ro, which is 202
209 gdb_test "print filelocal_ro" "\\\$$decimal = 202" "print filelocal_ro in test_at_bar"
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"
214 # Print scope1.c::foo::funclocal, which is 3
215 gdb_test "print foo::funclocal" "\\\$$decimal = 3" "print foo::funclocal at bar"
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"
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"
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"
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"
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"
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"
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"
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"
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"
247 # This test has little to do with local scopes, but it is in scope.exp anyway.
250 proc_with_prefix test_at_autovars {} {
256 # Test symbol table lookup with 100 local (auto) variables.
258 gdb_breakpoint marker1
260 gdb_test "cont" "Break.* marker1 \\(\\) at .*:$decimal.*" "continue to marker1"
261 gdb_test "up" ".*autovars.*" "up from marker1"
264 while {$count < 100} {
265 gdb_test "print i$count" ".* = $count" ""
266 set count [expr $count+1]
269 pass "$count auto variables correctly initialized"
271 # Test that block variable sorting is not screwing us.
272 gdb_test "frame" "#.*autovars \\(bcd=5, abc=6\\).*" "args in correct order"
275 proc_with_prefix test_at_localscopes {} {
281 gdb_breakpoint marker2
282 gdb_breakpoint marker3
283 gdb_breakpoint marker4
285 gdb_test "cont" "Break.* marker2 \\(\\) at .*:$decimal.*" "continue to marker2"
286 gdb_test "up" ".*localscopes.*" "up from marker2"
288 # Should be at first (outermost) scope. Check values.
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"
297 gdb_test "cont" "Break.* marker3 \\(\\) at .*:$decimal.*" \
298 "continue to marker3"
299 gdb_test "up" ".*localscopes.*" "up from marker3"
301 # Should be at next (first nested) scope. Check values.
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"
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"
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"
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.
327 gdb_file_cmd $binfile
329 # Test that variables in various segments print out correctly before
330 # the program is run.
332 # AIX--sections get mapped to the same address so we can't get the right one.
333 setup_xfail "rs6000-*-*"
335 gdb_test "print 'scope0.c'::filelocal_ro" "= 201"
337 # Check that gdb can access bss memory if the inferior is not running.
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"
347 gdb_test "print 'scope0.c'::filelocal" "= 1" \
348 "print 'scope0.c'::filelocal before run"
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 }