]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/call-sc.exp
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / call-sc.exp
CommitLineData
71d7dd7c
AC
1# This testcase is part of GDB, the GNU debugger.
2
6aba47ca 3# Copyright 2004, 2007 Free Software Foundation, Inc.
71d7dd7c
AC
4
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19# Test "return", "finish", and "call" of functions that a scalar (int,
20# float, enum) and/or take a single scalar parameter.
21
22if $tracelevel then {
23 strace $tracelevel
24}
25
26set prms_id 0
27set bug_id 0
28
29# Some targets can't call functions, so don't even bother with this
30# test.
31
32if [target_info exists gdb,cannot_call_functions] {
33 setup_xfail "*-*-*"
34 fail "This target can not call functions"
35 continue
36}
37
38set testfile "call-sc"
39set srcfile ${testfile}.c
40set binfile ${objdir}/${subdir}/${testfile}
41
42# Create and source the file that provides information about the
43# compiler used to compile the test case.
44
45if [get_compiler_info ${binfile}] {
46 return -1;
47}
48
49# Use the file name, compiler and tuples to set up any needed KFAILs.
50
51proc setup_kfails { file tuples bug } {
52 global testfile
53 if [string match $file $testfile] {
54 foreach f $tuples { setup_kfail $f $bug }
55 }
56}
57
58proc setup_compiler_kfails { file compiler format tuples bug } {
59 global testfile
60 if {[string match $file $testfile] && [test_compiler_info $compiler] && [test_debug_format $format]} {
61 foreach f $tuples { setup_kfail $f $bug }
62 }
63}
64
65# Compile a variant of scalars.c using TYPE to specify the type of the
66# parameter and return-type. Run the compiled program up to "main".
67# Also updates the global "testfile" to reflect the most recent build.
68
69proc start_scalars_test { type } {
70 global testfile
71 global srcfile
72 global binfile
73 global objdir
74 global subdir
75 global srcdir
76 global gdb_prompt
77 global expect_out
78
79 # Create the additional flags
80 set flags "debug additional_flags=-DT=${type}"
81 set testfile "call-sc-${type}"
82
83 set binfile ${objdir}/${subdir}/${testfile}
84 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "${flags}"] != "" } {
85 # built the second test case since we can't use prototypes
86 warning "Prototypes not supported, rebuilding with -DNO_PROTOTYPES"
87 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "${flags} additional_flags=-DNO_PROTOTYPES"] != "" } {
b60f0898
JB
88 untested call-sc.exp
89 return -1
71d7dd7c
AC
90 }
91 }
92
93 # Start with a fresh gdb.
94 gdb_exit
95 gdb_start
96 gdb_reinitialize_dir $srcdir/$subdir
97 gdb_load ${binfile}
98
99 # Make certain that the output is consistent
100 gdb_test "set print sevenbit-strings" "" \
101 "set print sevenbit-strings; ${testfile}"
102 gdb_test "set print address off" "" \
103 "set print address off; ${testfile}"
104 gdb_test "set width 0" "" \
105 "set width 0; ${testfile}"
106
107 # Advance to main
108 if { ![runto_main] } then {
109 gdb_suppress_tests;
110 }
111
112 # Get the debug format
113 get_debug_format
114
115 # check that type matches what was passed in
116 set test "ptype; ${testfile}"
117 set foo_t "xxx"
118 gdb_test_multiple "ptype ${type}" "${test}" {
0ef32fd9 119 -re "type = (\[^\r\n\]*)\r\n$gdb_prompt $" {
71d7dd7c
AC
120 set foo_t "$expect_out(1,string)"
121 pass "$test (${foo_t})"
122 }
123 }
124 gdb_test "ptype foo" "type = ${foo_t}" "ptype foo; ${testfile} $expect_out(1,string)"
125}
126
127
128# Given N (0..25), return the corresponding alphabetic letter in lower
129# or upper case. This is ment to be i18n proof.
130
131proc i2a { n } {
132 return [string range "abcdefghijklmnopqrstuvwxyz" $n $n]
133}
134
135proc I2A { n } {
136 return [string toupper [i2a $n]]
137}
138
139
140# Use the file name, compiler and tuples to set up any needed KFAILs.
141
142proc setup_kfails { file tuples bug } {
143 global testfile
144 if [string match $file $testfile] {
145 foreach f $tuples { setup_kfail $f $bug }
146 }
147}
148
149proc setup_compiler_kfails { file compiler format tuples bug } {
150 global testfile
151 if {[string match $file $testfile] && [test_compiler_info $compiler] && [test_debug_format $format]} {
152 foreach f $tuples { setup_kfail $f $bug }
153 }
154}
155
156# Test GDB's ability to make inferior function calls to functions
157# returning (or passing) in a single scalar.
158
159# start_scalars_test() will have previously built a program with a
160# specified scalar type. To ensure robustness of the output, "p/c" is
161# used.
162
163# This tests the code paths "which return-value convention?" and
164# "extract return-value from registers" called by "infcall.c".
165
166proc test_scalar_calls { } {
167 global testfile
168 global gdb_prompt
169
170 # Check that GDB can always extract a scalar-return value from an
171 # inferior function call. Since GDB always knows the location of
172 # an inferior function call's return value these should never fail
173
174 # Implemented by calling the parameterless function "fun" and then
175 # examining the return value printed by GDB.
176
177 set tests "call ${testfile}"
178
179 # Call fun, checking the printed return-value.
180 gdb_test "p/c fun()" "= 49 '1'" "p/c fun(); ${tests}"
181
182 # Check that GDB can always pass a structure to an inferior function.
183 # This test can never fail.
184
185 # Implemented by calling the one parameter function "Fun" which
186 # stores its parameter in the global variable "L". GDB then
187 # examining that global to confirm that the value is as expected.
188
189 gdb_test "call Fun(foo)" "" "call Fun(foo); ${tests}"
190 gdb_test "p/c L" " = 49 '1'" "p/c L; ${tests}"
191}
192
193# Test GDB's ability to both return a function (with "return" or
194# "finish") and correctly extract/store any corresponding
195# return-value.
196
197# Check that GDB can consistently extract/store structure return
198# values. There are two cases - returned in registers and returned in
199# memory. For the latter case, the return value can't be found and a
200# failure is "expected". However GDB must still both return the
201# function and display the final source and line information.
202
203# N identifies the number of elements in the struct that will be used
204# for the test case. FAILS is a list of target tuples that will fail
205# this test.
206
207# This tests the code paths "which return-value convention?", "extract
208# return-value from registers", and "store return-value in registers".
209# Unlike "test struct calls", this test is expected to "fail" when the
210# return-value is in memory (GDB can't find the location). The test
211# is in three parts: test "return"; test "finish"; check that the two
212# are consistent. GDB can sometimes work for one command and not the
213# other.
214
215proc test_scalar_returns { } {
216 global gdb_prompt
217 global testfile
218
219 set tests "return ${testfile}"
220
221
222 # Check that "return" works.
223
224 # GDB must always force the return of a function that has
225 # a struct result. Dependant on the ABI, it may, or may not be
226 # possible to store the return value in a register.
227
228 # The relevant code looks like "L{n} = fun{n}()". The test forces
229 # "fun{n}" to "return" with an explicit value. Since that code
230 # snippet will store the the returned value in "L{n}" the return
231 # is tested by examining "L{n}". This assumes that the
232 # compiler implemented this as fun{n}(&L{n}) and hence that when
233 # the value isn't stored "L{n}" remains unchanged. Also check for
234 # consistency between this and the "finish" case.
235
236 # Get into a call of fun
237 gdb_test "advance fun" \
238 "fun .*\[\r\n\]+\[0-9\].*return foo.*" \
239 "advance to fun for return; ${tests}"
240
241 # Check that the program invalidated the relevant global.
242 gdb_test "p/c L" " = 90 'Z'" "zed L for return; ${tests}"
243
244 # Force the "return". This checks that the return is always
245 # performed, and that GDB correctly reported this to the user.
246 # GDB 6.0 and earlier, when the return-value's location wasn't
247 # known, both failed to print a final "source and line" and misplaced
248 # the frame ("No frame").
249
250 # The test is writen so that it only reports one FAIL/PASS for the
251 # entire operation. The value returned is checked further down.
252 # "return_value_unknown", if non-empty, records why GDB realised
253 # that it didn't know where the return value was.
254
255 set test "return foo; ${tests}"
256 set return_value_unknown 0
257 set return_value_unimplemented 0
71d7dd7c
AC
258 gdb_test_multiple "return foo" "${test}" {
259 -re "The location" {
260 # Ulgh, a struct return, remember this (still need prompt).
261 set return_value_unknown 1
262 exp_continue
263 }
264 -re "A structure or union" {
265 # Ulgh, a struct return, remember this (still need prompt).
266 set return_value_unknown 1
267 # Double ulgh. Architecture doesn't use return_value and
268 # hence hasn't implemented small structure return.
269 set return_value_unimplemented 1
270 exp_continue
271 }
272 -re "Make fun return now.*y or n. $" {
273 gdb_test_multiple "y" "${test}" {
274 -re "L *= fun.*${gdb_prompt} $" {
275 # Need to step off the function call
276 gdb_test "next" "zed.*" "${test}"
277 }
2f193b69 278 -re "zed \\(\\);.*$gdb_prompt $" {
71d7dd7c
AC
279 pass "${test}"
280 }
281 }
282 }
283 }
284
3a77aa28
MC
285 # If the previous test did not work, the program counter might
286 # still be inside foo() rather than main(). Make sure the program
287 # counter is is main().
288 #
289 # This happens on ppc64 GNU/Linux with gcc 3.4.1 and a buggy GDB
290
291 set test "return foo; synchronize pc to main()"
292 for {set loop_count 0} {$loop_count < 2} {incr loop_count} {
293 gdb_test_multiple "backtrace 1" $test {
294 -re "#0.*main \\(\\).*${gdb_prompt} $" {
295 pass $test
296 set loop_count 2
297 }
298 -re "#0.*fun \\(\\).*${gdb_prompt} $" {
299 if {$loop_count < 1} {
300 gdb_test "finish" ".*" ""
301 } else {
302 fail $test
303 set loop_count 2
304 }
305 }
306 }
307 }
308
71d7dd7c
AC
309 # Check that the return-value is as expected. At this stage we're
310 # just checking that GDB has returned a value consistent with
311 # "return_value_unknown" set above.
312
313 set test "value foo returned; ${tests}"
71d7dd7c
AC
314 gdb_test_multiple "p/c L" "${test}" {
315 -re " = 49 '1'.*${gdb_prompt} $" {
316 if $return_value_unknown {
317 # This contradicts the above claim that GDB didn't
318 # know the location of the return-value.
319 fail "${test}"
320 } else {
321 pass "${test}"
322 }
323 }
324 -re " = 90 .*${gdb_prompt} $" {
325 if $return_value_unknown {
326 # The struct return case. Since any modification
327 # would be by reference, and that can't happen, the
328 # value should be unmodified and hence Z is expected.
329 # Is this a reasonable assumption?
330 pass "${test}"
331 } else {
332 # This contradicts the above claim that GDB knew
333 # the location of the return-value.
334 fail "${test}"
335 }
336 }
337 -re ".*${gdb_prompt} $" {
338 if $return_value_unimplemented {
339 # What a suprize. The architecture hasn't implemented
340 # return_value, and hence has to fail.
341 kfail "$test" gdb/1444
342 } else {
343 fail "$test"
344 }
345 }
346 }
347
348 # Check that a "finish" works.
349
350 # This is almost but not quite the same as "call struct funcs".
351 # Architectures can have subtle differences in the two code paths.
352
353 # The relevant code snippet is "L{n} = fun{n}()". The program is
354 # advanced into a call to "fun{n}" and then that function is
355 # finished. The returned value that GDB prints, reformatted using
356 # "p/c", is checked.
357
358 # Get into "fun()".
359 gdb_test "advance fun" \
360 "fun .*\[\r\n\]+\[0-9\].*return foo.*" \
361 "advance to fun for finish; ${tests}"
362
363 # Check that the program invalidated the relevant global.
364 gdb_test "p/c L" " = 90 'Z'" "zed L for finish; ${tests}"
365
366 # Finish the function, set 'finish_value_unknown" to non-empty if the
367 # return-value was not found.
368 set test "finish foo; ${tests}"
369 set finish_value_unknown 0
370 gdb_test_multiple "finish" "${test}" {
371 -re "Value returned is .*${gdb_prompt} $" {
372 pass "${test}"
373 }
374 -re "Cannot determine contents.*${gdb_prompt} $" {
375 # Expected bad value. For the moment this is ok.
376 set finish_value_unknown 1
377 pass "${test}"
378 }
379 }
380
381 # Re-print the last (return-value) using the more robust
382 # "p/c". If no return value was found, the 'Z' from the previous
383 # check that the variable was cleared, is printed.
384 set test "value foo finished; ${tests}"
385 gdb_test_multiple "p/c" "${test}" {
386 -re " = 49 '1'\[\r\n\]+${gdb_prompt} $" {
387 if $finish_value_unknown {
388 # This contradicts the above claim that GDB didn't
389 # know the location of the return-value.
390 fail "${test}"
391 } else {
392 pass "${test}"
393 }
394 }
395 -re " = 90 'Z'\[\r\n\]+${gdb_prompt} $" {
396 # The value didn't get found. This is "expected".
397 if $finish_value_unknown {
398 pass "${test}"
399 } else {
400 # This contradicts the above claim that GDB did
401 # know the location of the return-value.
402 fail "${test}"
403 }
404 }
405 }
406
407 # Finally, check that "return" and finish" have consistent
408 # behavior.
409
410 # Since both "return" and "finish" use equivalent "which
411 # return-value convention" logic, both commands should have
412 # identical can/can-not find return-value messages.
413
414 # Note that since "call" and "finish" use common code paths, a
415 # failure here is a strong indicator of problems with "store
416 # return-value" code paths. Suggest looking at "return_value"
417 # when investigating a fix.
418
419 set test "return and finish use same convention; ${tests}"
420 if {$finish_value_unknown == $return_value_unknown} {
421 pass "${test}"
422 } else {
423 kfail gdb/1444 "${test}"
424 }
425}
426
427# ABIs pass anything >8 or >16 bytes in memory but below that things
428# randomly use register and/and structure conventions. Check all
429# possible sized char scalars in that range. But only a restricted
430# range of the other types.
431
432# NetBSD/PPC returns "unnatural" (3, 5, 6, 7) sized scalars in memory.
433
434# d10v is weird. 5/6 byte scalars go in memory. 2 or more char
435# scalars go in memory. Everything else is in a register!
436
437# Test every single char struct from 1..17 in size. This is what the
438# original "scalars" test was doing.
439
440start_scalars_test tc
441test_scalar_calls
442test_scalar_returns
443
444
445# Let the fun begin.
446
447# Assuming that any integer struct larger than 8 bytes goes in memory,
448# come up with many and varied combinations of a return struct. For
449# "struct calls" test just beyond that 8 byte boundary, for "struct
450# returns" test up to that boundary.
451
452# For floats, assumed that up to two struct elements can be stored in
453# floating point registers, regardless of their size.
454
455# The approx size of each structure it is computed assumed that tc=1,
456# ts=2, ti=4, tl=4, tll=8, tf=4, td=8, tld=16, and that all fields are
457# naturally aligned. Padding being added where needed. Note that
458# these numbers are just approx, the d10v has ti=2, a 64-bit has has
459# tl=8.
460
461# Approx size: 2, 4, ...
462start_scalars_test ts
463test_scalar_calls
464test_scalar_returns
465
466# Approx size: 4, 8, ...
467start_scalars_test ti
468test_scalar_calls
469test_scalar_returns
470
471# Approx size: 4, 8, ...
472start_scalars_test tl
473test_scalar_calls
474test_scalar_returns
475
476# Approx size: 8, 16, ...
477start_scalars_test tll
478test_scalar_calls
479test_scalar_returns
480
481# Approx size: 4, 8, ...
482start_scalars_test tf
483test_scalar_calls
484test_scalar_returns
485
486# Approx size: 8, 16, ...
487start_scalars_test td
488test_scalar_calls
489test_scalar_returns
490
491# Approx size: 16, 32, ...
492start_scalars_test tld
493test_scalar_calls
494test_scalar_returns
495
496# Approx size: 4, 8, ...
497start_scalars_test te
498test_scalar_calls
499test_scalar_returns
500
501return 0