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