summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/scope.exp
blob: d1a1e0f8ffe48eef5da2f1f9a1a35fc3129778a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# Copyright 1992-2023 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This file was written by Fred Fish. (fnf@cygnus.com)


standard_testfile scope0.c scope1.c

if {[build_executable "failed to build" ${testfile} \
	 [list $srcfile $srcfile2] {debug}]} {
    return -1
}

# Test locating various things when stopped just inside main, after
# running init0().  To prevent cascading of errors, we report the
# first one and quit.  If all pass, then we print the pass results.

proc_with_prefix test_at_main {} {
    global gdb_prompt
    global decimal
    global det_file
    global srcdir
    global subdir

    # skip past init0.
    # This used to do an extra "next" if the first one didn't get us
    # over the call to init0, to handle calls to __main in the
    # prologue, etc.  But if a breakpoint at main doesn't leave us on
    # the first line of real code in the function, that's a GDB bug.
    gdb_test "next" "$decimal.*foo \\(\\);" "next over init0() in main"

    # Print scope0.c::filelocal, which is 1
    gdb_test "print filelocal" "\\\$$decimal = 1"
    gdb_test "print 'scope0.c'::filelocal" "\\\$$decimal = 1" "print 'scope0.c'::filelocal at main"

    # Print scope0.c::filelocal_bss, which is 101
    gdb_test "print filelocal_bss" "\\\$$decimal = 101"
    gdb_test "print 'scope0.c'::filelocal_bss" "\\\$$decimal = 101" "print 'scope0.c'::filelocal_bss in test_at_main"

    # Print scope0.c::filelocal_ro, which is 201

    # No clue why the rs6000 fails this test.
    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print filelocal_ro" "\\\$$decimal = 201" "print filelocal_ro in test_at_main"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope0.c'::filelocal_ro" "\\\$$decimal = 201" "print 'scope0.c'::filelocal_ro"

    # Print scope1.c::filelocal, which is 2
    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::filelocal" "\\\$$decimal = 2" "print 'scope1.c'::filelocal"

    # Print scope1.c::filelocal_bss, which is 102
    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::filelocal_bss" "\\\$$decimal = 102" "print 'scope1.c'::filelocal_bss"

    # Print scope1.c::filelocal_ro, which is 202
    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::filelocal_ro" "\\\$$decimal = 202" "print 'scope1.c'::filelocal_ro"

    # Print scope1.c::foo::funclocal, which is 3
    gdb_test "print foo::funclocal" "\\\$$decimal = 3"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::foo::funclocal" "\\\$$decimal = 3" "print 'scope1.c'::foo::funclocal"

    # Print scope1.c::foo::funclocal_ro, which is 203
    gdb_test "print foo::funclocal_ro" "\\\$$decimal = 203"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::foo::funclocal_ro" "\\\$$decimal = 203" "print 'scope1.c'::foo::funclocal_ro"

    # Print scope1.c::bar::funclocal, which is 4
    gdb_test "print bar::funclocal" "\\\$$decimal = 4"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::bar::funclocal" "\\\$$decimal = 4" "print 'scope1.c'::bar::funclocal"
}

proc_with_prefix test_at_foo {} {
    global gdb_prompt
    global decimal
    global det_file
    global srcdir
    global subdir

    gdb_test "next" ".*bar \\(\\);"

    # Print scope0.c::filelocal, which is 1
    gdb_test "print 'scope0.c'::filelocal" "\\\$$decimal = 1" "print 'scope0.c'::filelocal at foo"

    # Print scope0.c::filelocal_bss, which is 101
    gdb_test "print 'scope0.c'::filelocal_bss" "\\\$$decimal = 101" "print 'scope0.c'::filelocal_bss in test_at_foo"

    # Print scope0.c::filelocal_ro, which is 201
    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope0.c'::filelocal_ro" "\\\$$decimal = 201" "print 'scope0.c'::filelocal_ro"


    # Print scope1.c::filelocal, which is 2
    gdb_test "print filelocal" "\\\$$decimal = 2" "print filelocal at foo"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::filelocal" "\\\$$decimal = 2" "print 'scope1.c'::filelocal at foo"

    gdb_test "print filelocal_bss" "\\\$$decimal = 102" \
	"print filelocal_bss at foo"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::filelocal_bss" "\\\$$decimal = 102" "print 'scope1.c'::filelocal_bss at foo"


    gdb_test "print filelocal_ro" "\\\$$decimal = 202" \
	"print filelocal_ro at foo"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::filelocal_ro" "\\\$$decimal = 202" "print 'scope1.c'::filelocal_ro at foo"


    # Print scope1.c::foo::funclocal, which is 3

    gdb_test "print funclocal" "\\\$$decimal = 3" "print funclocal at foo"

    gdb_test "print foo::funclocal" "\\\$$decimal = 3" \
	"print foo::funclocal at foo"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::foo::funclocal" "\\\$$decimal = 3" "print 'scope1.c'::foo::funclocal at foo"


    # Print scope1.c::foo::funclocal_bss, which is 103

    gdb_test "print funclocal_bss" "\\\$$decimal = 103" \
	"print funclocal_bss at foo"

    gdb_test "print foo::funclocal_bss" "\\\$$decimal = 103" \
	"print foo::funclocal_bss at foo"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::foo::funclocal_bss" "\\\$$decimal = 103" "print 'scope1.c'::foo::funclocal_bss at foo"


    # Print scope1.c::foo::funclocal_ro, which is 203

    gdb_test "print funclocal_ro" "\\\$$decimal = 203" \
	"print funclocal_ro at foo"

    gdb_test "print foo::funclocal_ro" "\\\$$decimal = 203" \
	"print foo::funclocal_ro at foo"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::foo::funclocal_ro" "\\\$$decimal = 203" "print 'scope1.c'::foo::funclocal_ro at foo"


    # Print scope1.c::bar::funclocal, which is 4

    gdb_test "print bar::funclocal" "\\\$$decimal = 4" \
	"print bar::funclocal at foo"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::bar::funclocal" "\\\$$decimal = 4" "print 'scope1.c'::bar::funclocal at foo"

}

proc_with_prefix test_at_bar {} {
    global gdb_prompt
    global decimal
    global det_file
    global srcdir
    global subdir

    gdb_test "next"

    # Print scope0.c::filelocal, which is 1
    gdb_test "print 'scope0.c'::filelocal" "\\\$$decimal = 1" "print 'scope0.c'::filelocal at bar"

    # Print scope0.c::filelocal_bss, which is 101
    gdb_test "print 'scope0.c'::filelocal_bss" "\\\$$decimal = 101" "print 'scope0.c'::filelocal_bss in test_at_bar"

    # Print scope0.c::filelocal_ro, which is 201
    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope0.c'::filelocal_ro" "\\\$$decimal = 201" "print 'scope0.c'::filelocal_ro at bar"

    # Print scope1.c::filelocal, which is 2
    gdb_test "print filelocal" "\\\$$decimal = 2" "print filelocal at bar"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::filelocal" "\\\$$decimal = 2" "print 'scope1.c'::filelocal at bar"

    # Print scope1.c::filelocal_bss, which is 102
    gdb_test "print filelocal_bss" "\\\$$decimal = 102" "print filelocal_bss at bar"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::filelocal_bss" "\\\$$decimal = 102" "print 'scope1.c'::filelocal_bss at bar"

    # Print scope1.c::filelocal_ro, which is 202
    gdb_test "print filelocal_ro" "\\\$$decimal = 202" "print filelocal_ro in test_at_bar"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::filelocal_ro" "\\\$$decimal = 202" "print 'scope1.c'::filelocal_ro at bar"

    # Print scope1.c::foo::funclocal, which is 3
    gdb_test "print foo::funclocal" "\\\$$decimal = 3" "print foo::funclocal at bar"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::foo::funclocal" "\\\$$decimal = 3" "print 'scope1.c'::foo::funclocal at bar"

    # Print scope1.c::foo::funclocal_bss, which is 103
    gdb_test "print foo::funclocal_bss" "\\\$$decimal = 103" "print foo::funclocal_bss at bar"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::foo::funclocal_bss" "\\\$$decimal = 103" "print 'scope1.c'::foo::funclocal_bss at bar"

    # Print scope1.c::foo::funclocal_ro, which is 203
    gdb_test "print foo::funclocal_ro" "\\\$$decimal = 203" "print foo::funclocal_ro at bar"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::foo::funclocal_ro" "\\\$$decimal = 203" "print 'scope1.c'::foo::funclocal_ro at bar"

    # Print scope1.c::bar::funclocal, which is 4
    gdb_test "print funclocal" "\\\$$decimal = 4" "print funclocal at bar"
    gdb_test "print bar::funclocal" "\\\$$decimal = 4" "print bar::funclocal at bar"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::bar::funclocal" "\\\$$decimal = 4" "print 'scope1.c'::bar::funclocal at bar"

    # Print scope1.c::bar::funclocal_bss, which is 104
    gdb_test "print funclocal_bss" "\\\$$decimal = 104" "print funclocal_bss at bar"
    gdb_test "print bar::funclocal_bss" "\\\$$decimal = 104" "print bar::funclocal_bss at bar"

    if {[test_compiler_info gcc-*-*]} { setup_xfail "rs6000-*-*" }
    gdb_test "print 'scope1.c'::bar::funclocal_bss" "\\\$$decimal = 104" "print 'scope1.c'::bar::funclocal_bss at bar"
}

# This test has little to do with local scopes, but it is in scope.exp anyway.
# That's life.

proc_with_prefix test_at_autovars {} {
    global gdb_prompt
    global decimal
    global hex
    global srcfile

    # Test symbol table lookup with 100 local (auto) variables.

    gdb_breakpoint marker1

    gdb_test "cont" "Break.* marker1 \\(\\) at .*:$decimal.*" "continue to marker1"
    gdb_test "up" ".*autovars.*" "up from marker1"

    set count 0
    while {$count < 100} {
	gdb_test "print i$count" ".* = $count" ""
	set count [expr $count+1]
    }
    clear_xfail "*-*-*"
    pass "$count auto variables correctly initialized"

    # Test that block variable sorting is not screwing us.
    gdb_test "frame" "#.*autovars \\(bcd=5, abc=6\\).*" "args in correct order"
}

proc_with_prefix test_at_localscopes {} {
    global gdb_prompt
    global decimal
    global hex
    global srcfile

    gdb_breakpoint marker2
    gdb_breakpoint marker3
    gdb_breakpoint marker4

    gdb_test "cont" "Break.* marker2 \\(\\) at .*:$decimal.*" "continue to marker2"
    gdb_test "up" ".*localscopes.*" "up from marker2"

    # Should be at first (outermost) scope.  Check values.

    gdb_test "print localval" " = 10" "print localval, outer scope"
    gdb_test "print localval1" " = 11" "print localval1, outer scope"
    gdb_test "print localval2" "No symbol \"localval2\" in current context." \
	"print localval2, outer scope"
    gdb_test "print localval3" "No symbol \"localval3\" in current context." \
	"print localval3, outer scope"

    gdb_test "cont" "Break.* marker3 \\(\\) at .*:$decimal.*" \
	"continue to marker3"
    gdb_test "up" ".*localscopes.*" "up from marker3"

    # Should be at next (first nested) scope.  Check values.

    gdb_test "print localval" " = 20" \
	"print localval, first nested scope"
    gdb_test "print localval1" " = 11" "print localval1, first nested scope"
    gdb_test "print localval2" " = 12" "print localval2, first nested scope"
    gdb_test "print localval3" "No symbol \"localval3\" in current context." \
	"print localval3, first nested scope"

    # This test will only fail if the file was compiled by gcc, but
    # there's no way to check that.
    gdb_test "cont" "Break.* marker4.*at .*:$decimal.*" \
	"continue to marker4"
    gdb_test "up" ".*localscopes.*" "up from marker4"

    gdb_test "print localval" " = 30" "print localval, innermost scope"
    gdb_test "print localval1" " = 11" "print localval1, innermost scope"
    gdb_test "print localval2" " = 12" "print localval2, innermost scope"
    gdb_test "print localval3" " = 13" "print localval3, innermost scope"
}

# Note, we don't pass a binfile to clean_restart because we need to
# avoid gdb_load here.  With remote targets, gdb_load connects to the
# remote target, and we specificaly want the following tests to run
# before the program is started.
clean_restart
gdb_file_cmd $binfile

# Test that variables in various segments print out correctly before
# the program is run.  

# AIX--sections get mapped to the same address so we can't get the right one.
setup_xfail "rs6000-*-*"

gdb_test "print 'scope0.c'::filelocal_ro" "= 201"

# Check that gdb can access bss memory if the inferior is not running.
#
# Note the care above about avoiding gdb_load.  Otherwise, for
# embedded stub-like boards, this test would randomly fail.  If we'd
# already put target remote on the target stack, we'd read memory from
# the board instead of the program's binary, and we would have
# connected before crt0.o had had a chance to clear bss.
gdb_test "print 'scope0.c'::filelocal_bss" "= 0" \
    "print 'scope0.c'::filelocal_bss before run"

gdb_test "print 'scope0.c'::filelocal" "= 1" \
    "print 'scope0.c'::filelocal before run"

if {[runto_main]} { test_at_main }
if {[runto foo]} { test_at_foo }
if {[runto bar]} { test_at_bar }
if {[runto localscopes]} { test_at_localscopes }
if {[runto autovars]} { test_at_autovars }