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