1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2004, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
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 3 of the License, or
8 # (at your option) any later version.
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.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Test "return", "finish", and "call" of functions that a scalar (int,
19 # float, enum) and/or take a single scalar parameter.
26 # Some targets can't call functions, so don't even bother with this
29 if [target_info exists gdb,cannot_call_functions] {
31 fail "This target can not call functions"
35 set testfile "call-sc"
36 set srcfile ${testfile}.c
37 set binfile ${objdir}/${subdir}/${testfile}
39 # Create and source the file that provides information about the
40 # compiler used to compile the test case.
42 if [get_compiler_info ${binfile}] {
46 # Compile a variant of scalars.c using TYPE to specify the type of the
47 # parameter and return-type. Run the compiled program up to "main".
48 # Also updates the global "testfile" to reflect the most recent build.
50 proc start_scalars_test { type } {
60 # Create the additional flags
61 set flags "debug additional_flags=-DT=${type}"
62 set testfile "call-sc-${type}"
64 set binfile ${objdir}/${subdir}/${testfile}
65 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "${flags}"] != "" } {
66 # built the second test case since we can't use prototypes
67 warning "Prototypes not supported, rebuilding with -DNO_PROTOTYPES"
68 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "${flags} additional_flags=-DNO_PROTOTYPES"] != "" } {
74 # Start with a fresh gdb.
77 gdb_reinitialize_dir $srcdir/$subdir
80 # Make certain that the output is consistent
81 gdb_test "set print sevenbit-strings" "" \
82 "set print sevenbit-strings; ${testfile}"
83 gdb_test "set print address off" "" \
84 "set print address off; ${testfile}"
85 gdb_test "set width 0" "" \
86 "set width 0; ${testfile}"
89 if { ![runto_main] } then {
93 # Get the debug format
96 # check that type matches what was passed in
97 set test "ptype; ${testfile}"
99 gdb_test_multiple "ptype ${type}" "${test}" {
100 -re "type = (\[^\r\n\]*)\r\n$gdb_prompt $" {
101 set foo_t "$expect_out(1,string)"
102 pass "$test (${foo_t})"
105 gdb_test "ptype foo" "type = ${foo_t}" "ptype foo; ${testfile} $expect_out(1,string)"
109 # Given N (0..25), return the corresponding alphabetic letter in lower
110 # or upper case. This is ment to be i18n proof.
113 return [string range "abcdefghijklmnopqrstuvwxyz" $n $n]
117 return [string toupper [i2a $n]]
121 # Test GDB's ability to make inferior function calls to functions
122 # returning (or passing) in a single scalar.
124 # start_scalars_test() will have previously built a program with a
125 # specified scalar type. To ensure robustness of the output, "p/c" is
128 # This tests the code paths "which return-value convention?" and
129 # "extract return-value from registers" called by "infcall.c".
131 proc test_scalar_calls { } {
135 # Check that GDB can always extract a scalar-return value from an
136 # inferior function call. Since GDB always knows the location of
137 # an inferior function call's return value these should never fail
139 # Implemented by calling the parameterless function "fun" and then
140 # examining the return value printed by GDB.
142 set tests "call ${testfile}"
144 # Call fun, checking the printed return-value.
145 gdb_test "p/c fun()" "= 49 '1'" "p/c fun(); ${tests}"
147 # Check that GDB can always pass a structure to an inferior function.
148 # This test can never fail.
150 # Implemented by calling the one parameter function "Fun" which
151 # stores its parameter in the global variable "L". GDB then
152 # examining that global to confirm that the value is as expected.
154 gdb_test "call Fun(foo)" "" "call Fun(foo); ${tests}"
155 gdb_test "p/c L" " = 49 '1'" "p/c L; ${tests}"
158 # Test GDB's ability to both return a function (with "return" or
159 # "finish") and correctly extract/store any corresponding
162 # Check that GDB can consistently extract/store structure return
163 # values. There are two cases - returned in registers and returned in
164 # memory. For the latter case, the return value can't be found and a
165 # failure is "expected". However GDB must still both return the
166 # function and display the final source and line information.
168 # N identifies the number of elements in the struct that will be used
169 # for the test case. FAILS is a list of target tuples that will fail
172 # This tests the code paths "which return-value convention?", "extract
173 # return-value from registers", and "store return-value in registers".
174 # Unlike "test struct calls", this test is expected to "fail" when the
175 # return-value is in memory (GDB can't find the location). The test
176 # is in three parts: test "return"; test "finish"; check that the two
177 # are consistent. GDB can sometimes work for one command and not the
180 proc test_scalar_returns { } {
184 set tests "return ${testfile}"
187 # Check that "return" works.
189 # GDB must always force the return of a function that has
190 # a struct result. Dependant on the ABI, it may, or may not be
191 # possible to store the return value in a register.
193 # The relevant code looks like "L{n} = fun{n}()". The test forces
194 # "fun{n}" to "return" with an explicit value. Since that code
195 # snippet will store the the returned value in "L{n}" the return
196 # is tested by examining "L{n}". This assumes that the
197 # compiler implemented this as fun{n}(&L{n}) and hence that when
198 # the value isn't stored "L{n}" remains unchanged. Also check for
199 # consistency between this and the "finish" case.
201 # Get into a call of fun
202 gdb_test "advance fun" \
203 "fun .*\[\r\n\]+\[0-9\].*return foo.*" \
204 "advance to fun for return; ${tests}"
206 # Check that the program invalidated the relevant global.
207 gdb_test "p/c L" " = 90 'Z'" "zed L for return; ${tests}"
209 # Force the "return". This checks that the return is always
210 # performed, and that GDB correctly reported this to the user.
211 # GDB 6.0 and earlier, when the return-value's location wasn't
212 # known, both failed to print a final "source and line" and misplaced
213 # the frame ("No frame").
215 # The test is writen so that it only reports one FAIL/PASS for the
216 # entire operation. The value returned is checked further down.
217 # "return_value_unknown", if non-empty, records why GDB realised
218 # that it didn't know where the return value was.
220 set test "return foo; ${tests}"
221 set return_value_unknown 0
222 set return_value_unimplemented 0
223 gdb_test_multiple "return foo" "${test}" {
225 # Ulgh, a struct return, remember this (still need prompt).
226 set return_value_unknown 1
229 -re "A structure or union" {
230 # Ulgh, a struct return, remember this (still need prompt).
231 set return_value_unknown 1
232 # Double ulgh. Architecture doesn't use return_value and
233 # hence hasn't implemented small structure return.
234 set return_value_unimplemented 1
237 -re "Make fun return now.*y or n. $" {
238 gdb_test_multiple "y" "${test}" {
239 -re "L *= fun.*${gdb_prompt} $" {
240 # Need to step off the function call
241 gdb_test "next" "zed.*" "${test}"
243 -re "zed \\(\\);.*$gdb_prompt $" {
250 # If the previous test did not work, the program counter might
251 # still be inside foo() rather than main(). Make sure the program
252 # counter is is main().
254 # This happens on ppc64 GNU/Linux with gcc 3.4.1 and a buggy GDB
256 set test "return foo; synchronize pc to main()"
257 for {set loop_count 0} {$loop_count < 2} {incr loop_count} {
258 gdb_test_multiple "backtrace 1" $test {
259 -re "#0.*main \\(\\).*${gdb_prompt} $" {
263 -re "#0.*fun \\(\\).*${gdb_prompt} $" {
264 if {$loop_count < 1} {
265 gdb_test "finish" ".*" ""
274 # Check that the return-value is as expected. At this stage we're
275 # just checking that GDB has returned a value consistent with
276 # "return_value_unknown" set above.
278 set test "value foo returned; ${tests}"
279 gdb_test_multiple "p/c L" "${test}" {
280 -re " = 49 '1'.*${gdb_prompt} $" {
281 if $return_value_unknown {
282 # This contradicts the above claim that GDB didn't
283 # know the location of the return-value.
289 -re " = 90 .*${gdb_prompt} $" {
290 if $return_value_unknown {
291 # The struct return case. Since any modification
292 # would be by reference, and that can't happen, the
293 # value should be unmodified and hence Z is expected.
294 # Is this a reasonable assumption?
297 # This contradicts the above claim that GDB knew
298 # the location of the return-value.
302 -re ".*${gdb_prompt} $" {
303 if $return_value_unimplemented {
304 # What a suprize. The architecture hasn't implemented
305 # return_value, and hence has to fail.
306 kfail "$test" gdb/1444
313 # Check that a "finish" works.
315 # This is almost but not quite the same as "call struct funcs".
316 # Architectures can have subtle differences in the two code paths.
318 # The relevant code snippet is "L{n} = fun{n}()". The program is
319 # advanced into a call to "fun{n}" and then that function is
320 # finished. The returned value that GDB prints, reformatted using
324 gdb_test "advance fun" \
325 "fun .*\[\r\n\]+\[0-9\].*return foo.*" \
326 "advance to fun for finish; ${tests}"
328 # Check that the program invalidated the relevant global.
329 gdb_test "p/c L" " = 90 'Z'" "zed L for finish; ${tests}"
331 # Finish the function, set 'finish_value_unknown" to non-empty if the
332 # return-value was not found.
333 set test "finish foo; ${tests}"
334 set finish_value_unknown 0
335 gdb_test_multiple "finish" "${test}" {
336 -re "Value returned is .*${gdb_prompt} $" {
339 -re "Cannot determine contents.*${gdb_prompt} $" {
340 # Expected bad value. For the moment this is ok.
341 set finish_value_unknown 1
346 # Re-print the last (return-value) using the more robust
347 # "p/c". If no return value was found, the 'Z' from the previous
348 # check that the variable was cleared, is printed.
349 set test "value foo finished; ${tests}"
350 gdb_test_multiple "p/c" "${test}" {
351 -re " = 49 '1'\[\r\n\]+${gdb_prompt} $" {
352 if $finish_value_unknown {
353 # This contradicts the above claim that GDB didn't
354 # know the location of the return-value.
360 -re " = 90 'Z'\[\r\n\]+${gdb_prompt} $" {
361 # The value didn't get found. This is "expected".
362 if $finish_value_unknown {
365 # This contradicts the above claim that GDB did
366 # know the location of the return-value.
372 # Finally, check that "return" and finish" have consistent
375 # Since both "return" and "finish" use equivalent "which
376 # return-value convention" logic, both commands should have
377 # identical can/can-not find return-value messages.
379 # Note that since "call" and "finish" use common code paths, a
380 # failure here is a strong indicator of problems with "store
381 # return-value" code paths. Suggest looking at "return_value"
382 # when investigating a fix.
384 set test "return and finish use same convention; ${tests}"
385 if {$finish_value_unknown == $return_value_unknown} {
388 kfail gdb/1444 "${test}"
392 # ABIs pass anything >8 or >16 bytes in memory but below that things
393 # randomly use register and/and structure conventions. Check all
394 # possible sized char scalars in that range. But only a restricted
395 # range of the other types.
397 # NetBSD/PPC returns "unnatural" (3, 5, 6, 7) sized scalars in memory.
399 # d10v is weird. 5/6 byte scalars go in memory. 2 or more char
400 # scalars go in memory. Everything else is in a register!
402 # Test every single char struct from 1..17 in size. This is what the
403 # original "scalars" test was doing.
405 start_scalars_test tc
412 # Assuming that any integer struct larger than 8 bytes goes in memory,
413 # come up with many and varied combinations of a return struct. For
414 # "struct calls" test just beyond that 8 byte boundary, for "struct
415 # returns" test up to that boundary.
417 # For floats, assumed that up to two struct elements can be stored in
418 # floating point registers, regardless of their size.
420 # The approx size of each structure it is computed assumed that tc=1,
421 # ts=2, ti=4, tl=4, tll=8, tf=4, td=8, tld=16, and that all fields are
422 # naturally aligned. Padding being added where needed. Note that
423 # these numbers are just approx, the d10v has ti=2, a 64-bit has has
426 # Approx size: 2, 4, ...
427 start_scalars_test ts
431 # Approx size: 4, 8, ...
432 start_scalars_test ti
436 # Approx size: 4, 8, ...
437 start_scalars_test tl
441 # Approx size: 8, 16, ...
442 start_scalars_test tll
446 # Approx size: 4, 8, ...
447 start_scalars_test tf
451 # Approx size: 8, 16, ...
452 start_scalars_test td
456 # Approx size: 16, 32, ...
457 start_scalars_test tld
461 # Approx size: 4, 8, ...
462 start_scalars_test te