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