1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2004-2019 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.
22 # Some targets can't call functions, so don't even bother with this
25 if [target_info exists gdb,cannot_call_functions] {
26 unsupported "this target can not call functions"
32 # Create and source the file that provides information about the
33 # compiler used to compile the test case.
35 if [get_compiler_info] {
38 set skip_float_test [gdb_skip_float_test]
40 # Compile a variant of scalars.c using TYPE to specify the type of the
41 # parameter and return-type. Run the compiled program up to "main".
42 # Also updates the global "testfile" to reflect the most recent build.
44 proc start_scalars_test { type } {
53 # Create the additional flags
54 set flags "debug additional_flags=-DT=${type}"
55 set testfile "call-sc-${type}"
57 set binfile [standard_output_file ${testfile}]
58 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "${flags}"] != "" } {
59 # built the second test case since we can't use prototypes
60 warning "Prototypes not supported, rebuilding with -DNO_PROTOTYPES"
61 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "${flags} additional_flags=-DNO_PROTOTYPES"] != "" } {
62 untested "failed to compile"
67 # Start with a fresh gdb.
70 gdb_reinitialize_dir $srcdir/$subdir
73 # Make certain that the output is consistent
74 gdb_test_no_output "set print sevenbit-strings"
75 gdb_test_no_output "set print address off"
76 gdb_test_no_output "set width 0"
79 if { ![runto_main] } then {
83 # Get the debug format
86 # check that type matches what was passed in
87 set test "ptype; ${testfile}"
89 gdb_test_multiple "ptype/r ${type}" "${test}" {
90 -re "type = (\[^\r\n\]*)\r\n$gdb_prompt $" {
91 set foo_t "$expect_out(1,string)"
92 pass "$test (${foo_t})"
95 gdb_test "ptype/r foo" "type = ${foo_t}" "ptype foo; ${testfile} $expect_out(1,string)"
99 # Given N (0..25), return the corresponding alphabetic letter in lower
100 # or upper case. This is ment to be i18n proof.
103 return [string range "abcdefghijklmnopqrstuvwxyz" $n $n]
107 return [string toupper [i2a $n]]
111 # Test GDB's ability to make inferior function calls to functions
112 # returning (or passing) in a single scalar.
114 # start_scalars_test() will have previously built a program with a
115 # specified scalar type. To ensure robustness of the output, "p/c" is
118 # This tests the code paths "which return-value convention?" and
119 # "extract return-value from registers" called by "infcall.c".
121 proc test_scalar_calls { } {
125 # Check that GDB can always extract a scalar-return value from an
126 # inferior function call. Since GDB always knows the location of
127 # an inferior function call's return value these should never fail
129 # Implemented by calling the parameterless function "fun" and then
130 # examining the return value printed by GDB.
132 set tests "call ${testfile}"
134 # Call fun, checking the printed return-value.
135 gdb_test "p/c fun()" "= 49 '1'" "p/c fun(); ${tests}"
137 # Check that GDB can always pass a structure to an inferior function.
138 # This test can never fail.
140 # Implemented by calling the one parameter function "Fun" which
141 # stores its parameter in the global variable "L". GDB then
142 # examining that global to confirm that the value is as expected.
144 gdb_test_no_output "call Fun(foo)" "call Fun(foo); ${tests}"
145 gdb_test "p/c L" " = 49 '1'" "p/c L; ${tests}"
148 # Test GDB's ability to both return a function (with "return" or
149 # "finish") and correctly extract/store any corresponding
152 # Check that GDB can consistently extract/store structure return
153 # values. There are two cases - returned in registers and returned in
154 # memory. For the latter case, the return value can't be found and a
155 # failure is "expected". However GDB must still both return the
156 # function and display the final source and line information.
158 # N identifies the number of elements in the struct that will be used
159 # for the test case. FAILS is a list of target tuples that will fail
162 # This tests the code paths "which return-value convention?", "extract
163 # return-value from registers", and "store return-value in registers".
164 # Unlike "test struct calls", this test is expected to "fail" when the
165 # return-value is in memory (GDB can't find the location). The test
166 # is in three parts: test "return"; test "finish"; check that the two
167 # are consistent. GDB can sometimes work for one command and not the
170 proc test_scalar_returns { } {
174 set tests "return ${testfile}"
177 # Check that "return" works.
179 # GDB must always force the return of a function that has
180 # a struct result. Dependant on the ABI, it may, or may not be
181 # possible to store the return value in a register.
183 # The relevant code looks like "L{n} = fun{n}()". The test forces
184 # "fun{n}" to "return" with an explicit value. Since that code
185 # snippet will store the returned value in "L{n}" the return
186 # is tested by examining "L{n}". This assumes that the
187 # compiler implemented this as fun{n}(&L{n}) and hence that when
188 # the value isn't stored "L{n}" remains unchanged. Also check for
189 # consistency between this and the "finish" case.
191 # Get into a call of fun
192 gdb_test "advance fun" \
193 "fun .*\[\r\n\]+\[0-9\].*return foo.*" \
194 "advance to fun for return; ${tests}"
196 # Check that the program invalidated the relevant global.
197 gdb_test "p/c L" " = 90 'Z'" "zed L for return; ${tests}"
199 # Force the "return". This checks that the return is always
200 # performed, and that GDB correctly reported this to the user.
201 # GDB 6.0 and earlier, when the return-value's location wasn't
202 # known, both failed to print a final "source and line" and misplaced
203 # the frame ("No frame").
205 # The test is writen so that it only reports one FAIL/PASS for the
206 # entire operation. The value returned is checked further down.
207 # "return_value_unknown", if non-empty, records why GDB realised
208 # that it didn't know where the return value was.
210 set test "return foo; ${tests}"
211 set return_value_unknown 0
212 set return_value_unimplemented 0
213 gdb_test_multiple "return foo" "${test}" {
215 # Ulgh, a struct return, remember this (still need prompt).
216 set return_value_unknown 1
219 -re "A structure or union" {
220 # Ulgh, a struct return, remember this (still need prompt).
221 set return_value_unknown 1
222 # Double ulgh. Architecture doesn't use return_value and
223 # hence hasn't implemented small structure return.
224 set return_value_unimplemented 1
227 -re "Make fun return now.*y or n. $" {
228 gdb_test_multiple "y" "${test}" {
229 -re "L *= fun.*${gdb_prompt} $" {
230 # Need to step off the function call
231 gdb_test "next" "zed.*" "${test}"
233 -re "zed \\(\\);.*$gdb_prompt $" {
240 # If the previous test did not work, the program counter might
241 # still be inside foo() rather than main(). Make sure the program
242 # counter is is main().
244 # This happens on ppc64 GNU/Linux with gcc 3.4.1 and a buggy GDB
246 set test "return foo; synchronize pc to main()"
247 for {set loop_count 0} {$loop_count < 2} {incr loop_count} {
248 gdb_test_multiple "backtrace 1" $test {
249 -re "#0.*main \\(\\).*${gdb_prompt} $" {
253 -re "#0.*fun \\(\\).*${gdb_prompt} $" {
254 if {$loop_count < 1} {
255 gdb_test "finish" ".*" ""
264 # Check that the return-value is as expected. At this stage we're
265 # just checking that GDB has returned a value consistent with
266 # "return_value_unknown" set above.
268 set test "value foo returned; ${tests}"
269 gdb_test_multiple "p/c L" "${test}" {
270 -re " = 49 '1'.*${gdb_prompt} $" {
271 if $return_value_unknown {
272 # This contradicts the above claim that GDB didn't
273 # know the location of the return-value.
279 -re " = 90 .*${gdb_prompt} $" {
280 if $return_value_unknown {
281 # The struct return case. Since any modification
282 # would be by reference, and that can't happen, the
283 # value should be unmodified and hence Z is expected.
284 # Is this a reasonable assumption?
287 # This contradicts the above claim that GDB knew
288 # the location of the return-value.
292 -re ".*${gdb_prompt} $" {
293 if $return_value_unimplemented {
294 # What a suprize. The architecture hasn't implemented
295 # return_value, and hence has to fail.
296 kfail "$test" gdb/1444
303 # Check that a "finish" works.
305 # This is almost but not quite the same as "call struct funcs".
306 # Architectures can have subtle differences in the two code paths.
308 # The relevant code snippet is "L{n} = fun{n}()". The program is
309 # advanced into a call to "fun{n}" and then that function is
310 # finished. The returned value that GDB prints, reformatted using
314 gdb_test "advance fun" \
315 "fun .*\[\r\n\]+\[0-9\].*return foo.*" \
316 "advance to fun for finish; ${tests}"
318 # Check that the program invalidated the relevant global.
319 gdb_test "p/c L" " = 90 'Z'" "zed L for finish; ${tests}"
321 # Finish the function, set 'finish_value_unknown" to non-empty if the
322 # return-value was not found.
323 set test "finish foo; ${tests}"
324 set finish_value_unknown 0
325 gdb_test_multiple "finish" "${test}" {
326 -re "Value returned is .*${gdb_prompt} $" {
329 -re "Cannot determine contents.*${gdb_prompt} $" {
330 # Expected bad value. For the moment this is ok.
331 set finish_value_unknown 1
336 # Re-print the last (return-value) using the more robust
337 # "p/c". If no return value was found, the 'Z' from the previous
338 # check that the variable was cleared, is printed.
339 set test "value foo finished; ${tests}"
340 gdb_test_multiple "p/c" "${test}" {
341 -re " = 49 '1'\[\r\n\]+${gdb_prompt} $" {
342 if $finish_value_unknown {
343 # This contradicts the above claim that GDB didn't
344 # know the location of the return-value.
350 -re " = 90 'Z'\[\r\n\]+${gdb_prompt} $" {
351 # The value didn't get found. This is "expected".
352 if $finish_value_unknown {
355 # This contradicts the above claim that GDB did
356 # know the location of the return-value.
362 # Finally, check that "return" and finish" have consistent
365 # Since both "return" and "finish" use equivalent "which
366 # return-value convention" logic, both commands should have
367 # identical can/can-not find return-value messages.
369 # Note that since "call" and "finish" use common code paths, a
370 # failure here is a strong indicator of problems with "store
371 # return-value" code paths. Suggest looking at "return_value"
372 # when investigating a fix.
374 set test "return and finish use same convention; ${tests}"
375 if {$finish_value_unknown == $return_value_unknown} {
378 kfail gdb/1444 "${test}"
382 # ABIs pass anything >8 or >16 bytes in memory but below that things
383 # randomly use register and/and structure conventions. Check all
384 # possible sized char scalars in that range. But only a restricted
385 # range of the other types.
387 # NetBSD/PPC returns "unnatural" (3, 5, 6, 7) sized scalars in memory.
389 # Test every single char struct from 1..17 in size. This is what the
390 # original "scalars" test was doing.
392 start_scalars_test tc
399 # Assuming that any integer struct larger than 8 bytes goes in memory,
400 # come up with many and varied combinations of a return struct. For
401 # "struct calls" test just beyond that 8 byte boundary, for "struct
402 # returns" test up to that boundary.
404 # For floats, assumed that up to two struct elements can be stored in
405 # floating point registers, regardless of their size.
407 # The approx size of each structure it is computed assumed that tc=1,
408 # ts=2, ti=4, tl=4, tll=8, tf=4, td=8, tld=16, and that all fields are
409 # naturally aligned. Padding being added where needed.
411 # Approx size: 2, 4, ...
412 start_scalars_test ts
416 # Approx size: 4, 8, ...
417 start_scalars_test ti
421 # Approx size: 4, 8, ...
422 start_scalars_test tl
426 # Approx size: 8, 16, ...
427 start_scalars_test tll
431 if {!$skip_float_test} {
432 # Approx size: 4, 8, ...
433 start_scalars_test tf
437 # Approx size: 8, 16, ...
438 start_scalars_test td
442 # Approx size: 16, 32, ...
443 start_scalars_test tld
448 # Approx size: 4, 8, ...
449 start_scalars_test te